CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
HepMCFileReader Class Reference

#include <HepMCFileReader.h>

Public Member Functions

HepMC::GenEventfillCurrentEventData ()
 
virtual void getStatsFromTuple (int &mo1, int &mo2, int &da1, int &da2, int &status, int &pid, int j) const
 
virtual void initialize (const std::string &filename)
 
bool isInitialized () const
 
void printEvent () const
 
virtual bool printHepMcEvent () const
 
virtual bool readCurrentEvent ()
 
virtual void ReadStats ()
 
virtual bool setEvent (int event)
 
virtual ~HepMCFileReader ()
 

Static Public Member Functions

static HepMCFileReaderinstance ()
 

Protected Member Functions

 HepMCFileReader ()
 

Private Member Functions

int find_in_map (const std::map< HepMC::GenParticle *, int > &m, HepMC::GenParticle *p) const
 
HepMC::IO_BaseClass const * input () const
 
HepMC::IO_BaseClass *& input ()
 
int rdstate () const
 

Private Attributes

edm::propagate_const< HepMC::GenEvent * > evt_
 
std::vector< HepMC::GenParticle * > index_to_particle
 
edm::propagate_const< HepMC::IO_BaseClass * > input_
 
std::map< HepMC::GenParticle *, int > particle_to_index
 

Static Private Attributes

static HepMCFileReaderinstance_ = 0
 

Detailed Description

This class is used by the implementation of DaqEventFactory present in this package to read in the full event raw data from a flat binary file. WARNING: If you want to use this class for other purposes you must always invoke the method initialize before starting using the interface it exposes.

Author
G. Bruno - CERN, EP Division

Definition at line 27 of file HepMCFileReader.h.

Constructor & Destructor Documentation

HepMCFileReader::HepMCFileReader ( )
protected

Definition at line 38 of file HepMCFileReader.cc.

References instance_.

38  : 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 }
static HepMCFileReader * instance_
edm::propagate_const< HepMC::IO_BaseClass * > input_
edm::propagate_const< HepMC::GenEvent * > evt_
HepMCFileReader::~HepMCFileReader ( )
virtual

Definition at line 48 of file HepMCFileReader.cc.

References edm::propagate_const< T >::get(), input_, and instance_.

48  {
49  edm::LogInfo("HepMCFileReader") << "Destructing HepMCFileReader";
50 
51  instance_ = nullptr;
52  delete input_.get();
53 }
static HepMCFileReader * instance_
edm::propagate_const< HepMC::IO_BaseClass * > input_
element_type const * get() const

Member Function Documentation

HepMC::GenEvent * HepMCFileReader::fillCurrentEventData ( )

Definition at line 108 of file HepMCFileReader.cc.

References evt_, and readCurrentEvent().

108  {
110  return evt_;
111 }
edm::propagate_const< HepMC::GenEvent * > evt_
virtual bool readCurrentEvent()
int HepMCFileReader::find_in_map ( const std::map< HepMC::GenParticle *, int > &  m,
HepMC::GenParticle *  p 
) const
private

Definition at line 252 of file HepMCFileReader.cc.

Referenced by getStatsFromTuple().

252  {
253  std::map<HepMC::GenParticle *, int>::const_iterator iter = m.find(p);
254  return (iter == m.end()) ? 0 : iter->second;
255 }
void HepMCFileReader::getStatsFromTuple ( int &  mo1,
int &  mo2,
int &  da1,
int &  da2,
int &  status,
int &  pid,
int  j 
) const
virtual

Definition at line 205 of file HepMCFileReader.cc.

References class-composition::children, gather_cfg::cout, evt_, find_in_map(), index_to_particle, dqmiolumiharvest::j, and particle_to_index.

Referenced by printEvent().

205  {
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 }
std::vector< HepMC::GenParticle * > index_to_particle
edm::propagate_const< HepMC::GenEvent * > evt_
int find_in_map(const std::map< HepMC::GenParticle *, int > &m, HepMC::GenParticle *p) const
std::map< HepMC::GenParticle *, int > particle_to_index
void HepMCFileReader::initialize ( const std::string &  filename)
virtual

Definition at line 56 of file HepMCFileReader.cc.

References Exception, edm::propagate_const< T >::get(), recoMuon::in, input_, isInitialized(), and rdstate().

56  {
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 }
edm::propagate_const< HepMC::IO_BaseClass * > input_
int rdstate() const
bool isInitialized() const
element_type const * get() const
HepMC::IO_BaseClass const* HepMCFileReader::input ( ) const
inlineprivate

Definition at line 53 of file HepMCFileReader.h.

References edm::get_underlying_safe().

Referenced by rdstate().

53 { return get_underlying_safe(input_); }
edm::propagate_const< HepMC::IO_BaseClass * > input_
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
HepMC::IO_BaseClass*& HepMCFileReader::input ( )
inlineprivate

Definition at line 54 of file HepMCFileReader.h.

References edm::get_underlying_safe().

54 { return get_underlying_safe(input_); }
edm::propagate_const< HepMC::IO_BaseClass * > input_
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
HepMCFileReader * HepMCFileReader::instance ( )
static

Definition at line 28 of file HepMCFileReader.cc.

28  {
29  // Implement a HepMCFileReader singleton.
30 
31  if (instance_ == nullptr) {
32  instance_ = new HepMCFileReader();
33  }
34  return instance_;
35 }
static HepMCFileReader * instance_
bool HepMCFileReader::isInitialized ( ) const
inline

Definition at line 71 of file HepMCFileReader.h.

Referenced by initialize().

71 { return input_ != nullptr; }
edm::propagate_const< HepMC::IO_BaseClass * > input_
void HepMCFileReader::printEvent ( ) const

Definition at line 115 of file HepMCFileReader.cc.

References gather_cfg::cout, evt_, alignBH_cfg::fixed, g, GenParticle::GenParticle, getStatsFromTuple(), index_to_particle, dqmiodumpmetadata::n, mathSSE::sqrt(), and mps_update::status.

115  {
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 }
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
std::vector< HepMC::GenParticle * > index_to_particle
edm::propagate_const< HepMC::GenEvent * > evt_
virtual void getStatsFromTuple(int &mo1, int &mo2, int &da1, int &da2, int &status, int &pid, int j) const
T sqrt(T t)
Definition: SSEVec.h:19
bool HepMCFileReader::printHepMcEvent ( ) const
virtual

Definition at line 101 of file HepMCFileReader.cc.

References evt_.

101  {
102  if (evt_ != nullptr)
103  evt_->print();
104  return true;
105 }
edm::propagate_const< HepMC::GenEvent * > evt_
int HepMCFileReader::rdstate ( ) const
private

Definition at line 71 of file HepMCFileReader.cc.

References input(), and AlCaHLTBitMon_ParallelJobs::p.

Referenced by initialize().

71  {
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 }
HepMC::IO_BaseClass const * input() const
bool HepMCFileReader::readCurrentEvent ( )
virtual

Definition at line 82 of file HepMCFileReader.cc.

References evt_, input_, and ReadStats().

Referenced by fillCurrentEventData().

82  {
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 }
virtual void ReadStats()
edm::propagate_const< HepMC::IO_BaseClass * > input_
edm::propagate_const< HepMC::GenEvent * > evt_
void HepMCFileReader::ReadStats ( )
virtual

Definition at line 166 of file HepMCFileReader.cc.

References evt_, index_to_particle, p1, p2, particle_to_index, and findQualityFiles::v.

Referenced by readCurrentEvent().

166  {
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 }
std::vector< HepMC::GenParticle * > index_to_particle
edm::propagate_const< HepMC::GenEvent * > evt_
std::map< HepMC::GenParticle *, int > particle_to_index
double p2[4]
Definition: TauolaWrapper.h:90
double p1[4]
Definition: TauolaWrapper.h:89
bool HepMCFileReader::setEvent ( int  event)
virtual

Definition at line 98 of file HepMCFileReader.cc.

98 { return true; }

Member Data Documentation

edm::propagate_const<HepMC::GenEvent *> HepMCFileReader::evt_
private
std::vector<HepMC::GenParticle *> HepMCFileReader::index_to_particle
private

Definition at line 65 of file HepMCFileReader.h.

Referenced by getStatsFromTuple(), printEvent(), and ReadStats().

edm::propagate_const<HepMC::IO_BaseClass *> HepMCFileReader::input_
private

Definition at line 58 of file HepMCFileReader.h.

Referenced by initialize(), readCurrentEvent(), and ~HepMCFileReader().

HepMCFileReader * HepMCFileReader::instance_ = 0
staticprivate

Definition at line 60 of file HepMCFileReader.h.

Referenced by HepMCFileReader(), and ~HepMCFileReader().

std::map<HepMC::GenParticle *, int> HepMCFileReader::particle_to_index
private

Definition at line 66 of file HepMCFileReader.h.

Referenced by getStatsFromTuple(), and ReadStats().