CMS 3D CMS Logo

HepMC3Product.cc
Go to the documentation of this file.
1 /*************************
2  *
3  * Date: 2005/08 $
4  * \author J Weng - F. Moortgat'
5  */
6 
7 #include <iostream>
8 #include <algorithm> // because we use std::swap
9 
10 #include <HepMC3/GenEvent.h>
11 #include <HepMC3/GenVertex.h>
12 #include <HepMC3/GenParticle.h>
15 
16 using namespace edm;
17 using namespace std;
18 
19 HepMC3Product::HepMC3Product(HepMC3::GenEvent* evt)
20  : evt_(evt), isVtxGenApplied_(false), isVtxBoostApplied_(false), isPBoostApplied_(false) {}
21 
23  delete evt_;
24  evt_ = nullptr;
25  isVtxGenApplied_ = false;
26  isVtxBoostApplied_ = false;
27  isPBoostApplied_ = false;
28 }
29 
30 void HepMC3Product::addHepMCData(HepMC3::GenEvent* evt) {
31  // evt->print();
32  // cout <<sizeof (evt) <<" " << sizeof ( HepMC::GenEvent) << endl;
33  evt_ = evt;
34 
35  // same story about vertex smearing - GenEvent won't know it...
36  // in fact, would be better to implement CmsGenEvent...
37 }
38 
39 void HepMC3Product::applyVtxGen(HepMC3::FourVector const& vtxShift) {
40  //std::cout<< " applyVtxGen called " << isVtxGenApplied_ << endl;
41  //fTimeOffset = 0;
42  if (isVtxGenApplied())
43  return;
44  evt_->shift_position_by(vtxShift);
45  isVtxGenApplied_ = true;
46  return;
47 }
48 
49 void HepMC3Product::boostToLab(TMatrixD const* lorentz, std::string const& type) {
50  //std::cout << "from boostToLab:" << std::endl;
51 
52  if (lorentz == nullptr) {
53  //std::cout << " lorentz = 0 " << std::endl;
54  return;
55  }
56 
57  //lorentz->Print();
58 
59  TMatrixD tmplorentz(*lorentz);
60  //tmplorentz.Print();
61 
62  if (type == "vertex") {
63  if (isVtxBoostApplied()) {
64  //std::cout << " isVtxBoostApplied true " << std::endl;
65  return;
66  }
67 
68  for (const HepMC3::GenVertexPtr& vt : evt_->vertices()) {
69  // change basis to lorentz boost definition: (t,x,z,y)
70  TMatrixD p4(4, 1);
71  p4(0, 0) = vt->position().t();
72  p4(1, 0) = vt->position().x();
73  p4(2, 0) = vt->position().z();
74  p4(3, 0) = vt->position().y();
75 
76  TMatrixD p4lab(4, 1);
77  p4lab = tmplorentz * p4;
78  //std::cout << " vertex lorentz: " << p4lab(1,0) << " " << p4lab(3,0) << " " << p4lab(2,0) << std::endl;
79  vt->set_position(HepMC3::FourVector(p4lab(1, 0), p4lab(3, 0), p4lab(2, 0), p4lab(0, 0)));
80  }
81 
82  isVtxBoostApplied_ = true;
83  } else if (type == "momentum") {
84  if (isPBoostApplied()) {
85  //std::cout << " isPBoostApplied true " << std::endl;
86  return;
87  }
88 
89  for (const HepMC3::GenParticlePtr& part : evt_->particles()) {
90  // change basis to lorentz boost definition: (E,Px,Pz,Py)
91  TMatrixD p4(4, 1);
92  p4(0, 0) = part->momentum().e();
93  p4(1, 0) = part->momentum().x();
94  p4(2, 0) = part->momentum().z();
95  p4(3, 0) = part->momentum().y();
96 
97  TMatrixD p4lab(4, 1);
98  p4lab = tmplorentz * p4;
99  //std::cout << " momentum lorentz: " << p4lab(1,0) << " " << p4lab(3,0) << " " << p4lab(2,0) << std::endl;
100  part->set_momentum(HepMC3::FourVector(p4lab(1, 0), p4lab(3, 0), p4lab(2, 0), p4lab(0, 0)));
101  }
102 
103  isPBoostApplied_ = true;
104  } else {
105  edm::LogWarning("GeneratorProducts") << "no type found for boostToLab(std::string), options are vertex or momentum";
106  //throw edm::Exception(edm::errors::Configuration, "GeneratorProducts")
107  // << "no type found for boostToLab(std::string), options are vertex or momentum \n";
108  }
109 
110  return;
111 }
112 
113 const HepMC3::GenEvent& HepMC3Product::getHepMCData() const { return *evt_; }
114 
115 // copy constructor
117  if (other.evt_)
118  evt_ = new HepMC3::GenEvent(*other.evt_);
119  isVtxGenApplied_ = other.isVtxGenApplied_;
120  isVtxBoostApplied_ = other.isVtxBoostApplied_;
121  isPBoostApplied_ = other.isPBoostApplied_;
122  //fTimeOffset = other.fTimeOffset;
123 }
124 
125 // swap
127  std::swap(evt_, other.evt_);
128  std::swap(isVtxGenApplied_, other.isVtxGenApplied_);
129  std::swap(isVtxBoostApplied_, other.isVtxBoostApplied_);
130  std::swap(isPBoostApplied_, other.isPBoostApplied_);
131  //std::swap(fTimeOffset, other.fTimeOffset);
132 }
133 
134 // assignment: use copy/swap idiom for exception safety.
137  swap(temp);
138  return *this;
139 }
140 
141 // move, needed explicitly as we have raw pointer...
144  swap(other);
145  return *this;
146 }
HepMC3::GenEvent * evt_
Definition: HepMC3Product.h:50
const HepMC3::GenEvent & getHepMCData() const
HepMC3Product & operator=(HepMC3Product const &other)
virtual ~HepMC3Product()
bool isPBoostApplied() const
Definition: HepMC3Product.h:41
void boostToLab(TMatrixD const *lorentz, std::string const &type)
void swap(HepMC3Product &other)
bool isVtxGenApplied() const
Definition: HepMC3Product.h:39
bool isVtxBoostApplied() const
Definition: HepMC3Product.h:40
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void applyVtxGen(HepMC3::FourVector const *vtxShift)
Definition: HepMC3Product.h:30
void addHepMCData(HepMC3::GenEvent *evt)
edm::Ptr< GenParticle > GenParticlePtr
persistent reference to a GenParticle
part
Definition: HCALResponse.h:20
HLT enums.
Log< level::Warning, false > LogWarning