CMS 3D CMS Logo

HepMCFileReader.cc
Go to the documentation of this file.
1 
9 #include <iostream>
10 #include <iomanip>
11 #include <string>
12 
13 #include "HepMC/GenEvent.h"
14 #include "HepMC/GenParticle.h"
15 #include "HepMC/IO_GenEvent.h"
16 
17 #include "CLHEP/Units/GlobalPhysicalConstants.h"
18 
22 
23 using namespace std;
24 
26 
27 //-------------------------------------------------------------------------
29  // Implement a HepMCFileReader singleton.
30 
31  if (instance_ == nullptr) {
32  instance_ = new HepMCFileReader();
33  }
34  return instance_;
35 }
36 
37 //-------------------------------------------------------------------------
38 HepMCFileReader::HepMCFileReader() : evt_(nullptr), input_(nullptr) {
39  // Default constructor.
40  if (instance_ == nullptr) {
41  instance_ = this;
42  } else {
43  edm::LogError("HepMCFileReader") << "Constructing a new instance";
44  }
45 }
46 
47 //-------------------------------------------------------------------------
49  edm::LogInfo("HepMCFileReader") << "Destructing HepMCFileReader";
50 
51  instance_ = nullptr;
52  delete input_.get();
53 }
54 
55 //-------------------------------------------------------------------------
57  if (isInitialized()) {
58  edm::LogError("HepMCFileReader") << "Was already initialized... reinitializing";
59  delete input_.get();
60  }
61 
62  edm::LogInfo("HepMCFileReader") << "Opening file" << filename << "using HepMC::IO_GenEvent";
63  input_ = new HepMC::IO_GenEvent(filename, std::ios::in);
64 
65  if (rdstate() == std::ios::failbit) {
66  throw cms::Exception("FileNotFound", "HepMCFileReader::initialize()") << "File " << filename << " was not found.\n";
67  }
68 }
69 
70 //-------------------------------------------------------------------------
72  // work around a HepMC IO_ inheritence shortfall
73 
74  HepMC::IO_GenEvent const *p = dynamic_cast<HepMC::IO_GenEvent const *>(input());
75  if (p)
76  return p->rdstate();
77 
78  return std::ios::failbit;
79 }
80 
81 //-------------------------------------------------------------------------
83  evt_ = input_->read_next_event();
84  if (evt_) {
85  edm::LogInfo("HepMCFileReader") << "| --- Event Nr. " << evt_->event_number() << " with " << evt_->particles_size()
86  << " particles --- !";
87  ReadStats();
88  // printHepMcEvent();
89  // printEvent();
90  } else {
91  edm::LogInfo("HepMCFileReader") << "Got no event" << endl;
92  }
93 
94  return evt_ != nullptr;
95 }
96 
97 //-------------------------------------------------------------------------
98 bool HepMCFileReader::setEvent(int event) { return true; }
99 
100 //-------------------------------------------------------------------------
102  if (evt_ != nullptr)
103  evt_->print();
104  return true;
105 }
106 
107 //-------------------------------------------------------------------------
110  return evt_;
111 }
112 
113 //-------------------------------------------------------------------------
114 // Print out in old CMKIN style for comparisons
116  int mo1 = 0, mo2 = 0, da1 = 0, da2 = 0, status = 0, pid = 0;
117  if (evt_ != nullptr) {
118  cout << "---#-------pid--st---Mo1---Mo2---Da1---Da2------px------py------pz-------E-";
119  cout << "------m---------x---------y---------z---------t-";
120  cout << endl;
121  cout.setf(ios::right, ios::adjustfield);
122  for (int n = 1; n <= evt_->particles_size(); n++) {
124  getStatsFromTuple(mo1, mo2, da1, da2, status, pid, n);
125  cout << setw(4) << n << setw(8) << pid << setw(5) << status << setw(6) << mo1 << setw(6) << mo2 << setw(6) << da1
126  << setw(6) << da2;
127  cout.setf(ios::fixed, ios::floatfield);
128  cout.setf(ios::right, ios::adjustfield);
129  cout << setw(10) << setprecision(2) << g->momentum().x();
130  cout << setw(8) << setprecision(2) << g->momentum().y();
131  cout << setw(10) << setprecision(2) << g->momentum().z();
132  cout << setw(8) << setprecision(2) << g->momentum().t();
133  cout << setw(8) << setprecision(2) << g->generatedMass();
134  // tau=L/(gamma*beta*c)
135  if (g->production_vertex() != nullptr && g->end_vertex() != nullptr && status == 2) {
136  cout << setw(10) << setprecision(2) << g->production_vertex()->position().x();
137  cout << setw(10) << setprecision(2) << g->production_vertex()->position().y();
138  cout << setw(10) << setprecision(2) << g->production_vertex()->position().z();
139 
140  double xm = g->production_vertex()->position().x();
141  double ym = g->production_vertex()->position().y();
142  double zm = g->production_vertex()->position().z();
143  double xd = g->end_vertex()->position().x();
144  double yd = g->end_vertex()->position().y();
145  double zd = g->end_vertex()->position().z();
146  double decl = sqrt((xd - xm) * (xd - xm) + (yd - ym) * (yd - ym) + (zd - zm) * (zd - zm));
147  double labTime = decl / c_light;
148  // convert lab time to proper time
149  double properTime = labTime / g->momentum().rho() * (g->generatedMass());
150  // set the proper time in nanoseconds
151  cout << setw(8) << setprecision(2) << properTime;
152  } else {
153  cout << setw(10) << setprecision(2) << 0.0;
154  cout << setw(10) << setprecision(2) << 0.0;
155  cout << setw(10) << setprecision(2) << 0.0;
156  cout << setw(8) << setprecision(2) << 0.0;
157  }
158  cout << endl;
159  }
160  } else {
161  cout << " HepMCFileReader: No event available !" << endl;
162  }
163 }
164 
165 //-------------------------------------------------------------------------
167  unsigned int particle_counter = 0;
168  index_to_particle.reserve(evt_->particles_size() + 1);
169  index_to_particle[0] = nullptr;
170  for (HepMC::GenEvent::vertex_const_iterator v = evt_->vertices_begin(); v != evt_->vertices_end(); ++v) {
171  // making a list of incoming particles of the vertices
172  // so that the mother indices in HEPEVT can be filled properly
173  for (HepMC::GenVertex::particles_in_const_iterator p1 = (*v)->particles_in_const_begin();
174  p1 != (*v)->particles_in_const_end();
175  ++p1) {
176  ++particle_counter;
177  //particle_counter can be very large for heavy ions
178  if (particle_counter >= index_to_particle.size()) {
179  //make it large enough to hold up to this index
180  index_to_particle.resize(particle_counter + 1);
181  }
182  index_to_particle[particle_counter] = *p1;
183  particle_to_index[*p1] = particle_counter;
184  }
185  // daughters are entered only if they aren't a mother of
186  // another vertex
187  for (HepMC::GenVertex::particles_out_const_iterator p2 = (*v)->particles_out_const_begin();
188  p2 != (*v)->particles_out_const_end();
189  ++p2) {
190  if (!(*p2)->end_vertex()) {
191  ++particle_counter;
192  //particle_counter can be very large for heavy ions
193  if (particle_counter >= index_to_particle.size()) {
194  //make it large enough to hold up to this index
195  index_to_particle.resize(particle_counter + 1);
196  }
197  index_to_particle[particle_counter] = *p2;
198  particle_to_index[*p2] = particle_counter;
199  }
200  }
201  }
202 }
203 
204 //-------------------------------------------------------------------------
205 void HepMCFileReader::getStatsFromTuple(int &mo1, int &mo2, int &da1, int &da2, int &status, int &pid, int j) const {
206  if (!evt_) {
207  cout << "HepMCFileReader: Got no event :-( Game over already ?" << endl;
208  } else {
209  status = index_to_particle[j]->status();
210  pid = index_to_particle[j]->pdg_id();
211  if (index_to_particle[j]->production_vertex()) {
212  //HepLorentzVector p = index_to_particle[j]->
213  //production_vertex()->position();
214 
215  int num_mothers = index_to_particle[j]->production_vertex()->particles_in_size();
216  int first_mother =
217  find_in_map(particle_to_index, *(index_to_particle[j]->production_vertex()->particles_in_const_begin()));
218  int last_mother = first_mother + num_mothers - 1;
219  if (first_mother == 0)
220  last_mother = 0;
221  mo1 = first_mother;
222  mo2 = last_mother;
223  } else {
224  mo1 = 0;
225  mo2 = 0;
226  }
227  if (!index_to_particle[j]->end_vertex()) {
228  //find # of 1. daughter
229  int first_daughter =
230  find_in_map(particle_to_index, *(index_to_particle[j]->end_vertex()->particles_begin(HepMC::children)));
231  //cout <<"first_daughter "<< first_daughter << "num_daughters " << num_daughters << endl;
232  HepMC::GenVertex::particle_iterator ic;
233  int last_daughter = 0;
234  //find # of last daughter
235  for (ic = index_to_particle[j]->end_vertex()->particles_begin(HepMC::children);
236  ic != index_to_particle[j]->end_vertex()->particles_end(HepMC::children);
237  ++ic)
238  last_daughter = find_in_map(particle_to_index, *ic);
239 
240  if (first_daughter == 0)
241  last_daughter = 0;
242  da1 = first_daughter;
243  da2 = last_daughter;
244  } else {
245  da1 = 0;
246  da2 = 0;
247  }
248  }
249 }
250 
251 //-------------------------------------------------------------------------
252 int HepMCFileReader::find_in_map(const std::map<HepMC::GenParticle *, int> &m, HepMC::GenParticle *p) const {
253  std::map<HepMC::GenParticle *, int>::const_iterator iter = m.find(p);
254  return (iter == m.end()) ? 0 : iter->second;
255 }
alignBH_cfg.fixed
fixed
Definition: alignBH_cfg.py:54
MessageLogger.h
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
HepMCFileReader::getStatsFromTuple
virtual void getStatsFromTuple(int &mo1, int &mo2, int &da1, int &da2, int &status, int &pid, int j) const
Definition: HepMCFileReader.cc:205
mps_update.status
status
Definition: mps_update.py:68
class-composition.children
children
Definition: class-composition.py:98
gather_cfg.cout
cout
Definition: gather_cfg.py:144
HepMCFileReader::fillCurrentEventData
HepMC::GenEvent * fillCurrentEventData()
Definition: HepMCFileReader.cc:108
edm::propagate_const::get
constexpr element_type const * get() const
Definition: propagate_const.h:64
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
findQualityFiles.v
v
Definition: findQualityFiles.py:179
HepMCFileReader::evt_
edm::propagate_const< HepMC::GenEvent * > evt_
Definition: HepMCFileReader.h:57
HepMC::GenEvent
Definition: hepmc_rootio.cc:9
HepMCFileReader::initialize
virtual void initialize(const std::string &filename)
Definition: HepMCFileReader.cc:56
HepMCFileReader::printHepMcEvent
virtual bool printHepMcEvent() const
Definition: HepMCFileReader.cc:101
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:78
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
p2
double p2[4]
Definition: TauolaWrapper.h:90
HepMCFileReader::printEvent
void printEvent() const
Definition: HepMCFileReader.cc:115
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
HepMCFileReader::ReadStats
virtual void ReadStats()
Definition: HepMCFileReader.cc:166
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
recoMuon::in
Definition: RecoMuonEnumerators.h:6
p1
double p1[4]
Definition: TauolaWrapper.h:89
HepMCFileReader::setEvent
virtual bool setEvent(int event)
Definition: HepMCFileReader.cc:98
HepMCFileReader::instance
static HepMCFileReader * instance()
Definition: HepMCFileReader.cc:28
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
HepMCFileReader::particle_to_index
std::map< HepMC::GenParticle *, int > particle_to_index
Definition: HepMCFileReader.h:66
GenParticle.GenParticle
GenParticle
Definition: GenParticle.py:18
std
Definition: JetResolutionObject.h:76
HepMCFileReader::HepMCFileReader
HepMCFileReader()
Definition: HepMCFileReader.cc:38
Exception
Definition: hltDiff.cc:245
HepMCFileReader::input_
edm::propagate_const< HepMC::IO_BaseClass * > input_
Definition: HepMCFileReader.h:58
Exception.h
HepMCFileReader::input
const HepMC::IO_BaseClass * input() const
Definition: HepMCFileReader.h:53
HepMCFileReader::rdstate
int rdstate() const
Definition: HepMCFileReader.cc:71
HepMCFileReader::~HepMCFileReader
virtual ~HepMCFileReader()
Definition: HepMCFileReader.cc:48
HepMCFileReader.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
event
Definition: event.py:1
HepMCFileReader::readCurrentEvent
virtual bool readCurrentEvent()
Definition: HepMCFileReader.cc:82
HepMCFileReader::find_in_map
int find_in_map(const std::map< HepMC::GenParticle *, int > &m, HepMC::GenParticle *p) const
Definition: HepMCFileReader.cc:252
HepMCFileReader
Definition: HepMCFileReader.h:27
HepMCFileReader::instance_
static HepMCFileReader * instance_
Definition: HepMCFileReader.h:60
HepMCFileReader::index_to_particle
std::vector< HepMC::GenParticle * > index_to_particle
Definition: HepMCFileReader.h:65
HepMCFileReader::isInitialized
bool isInitialized() const
Definition: HepMCFileReader.h:71
g
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4