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_ = nullptr
 

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::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_
Log< level::Error, false > LogError
edm::propagate_const< HepMC::GenEvent * > evt_

◆ ~HepMCFileReader()

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_
constexpr element_type const * get() const
Log< level::Info, false > LogInfo

Member Function Documentation

◆ fillCurrentEventData()

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()

◆ find_in_map()

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.

References visualization-live-secondInstance_cfg::m, and AlCaHLTBitMon_ParallelJobs::p.

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 }

◆ getStatsFromTuple()

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, particle_to_index, and mps_update::status.

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_
std::map< HepMC::GenParticle *, int > particle_to_index
int find_in_map(const std::map< HepMC::GenParticle *, int > &m, HepMC::GenParticle *p) const

◆ initialize()

void HepMCFileReader::initialize ( const std::string &  filename)
virtual

Definition at line 56 of file HepMCFileReader.cc.

References Exception, corrVsCorr::filename, 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_
Log< level::Error, false > LogError
constexpr element_type const * get() const
int rdstate() const
Log< level::Info, false > LogInfo
bool isInitialized() const

◆ input() [1/2]

HepMC::IO_BaseClass const* HepMCFileReader::input ( ) const
inlineprivate

Definition at line 53 of file HepMCFileReader.h.

References edm::get_underlying_safe(), and input_.

Referenced by rdstate().

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

◆ input() [2/2]

HepMC::IO_BaseClass*& HepMCFileReader::input ( )
inlineprivate

Definition at line 54 of file HepMCFileReader.h.

References edm::get_underlying_safe(), and input_.

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

◆ instance()

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_

◆ isInitialized()

bool HepMCFileReader::isInitialized ( ) const
inline

Definition at line 71 of file HepMCFileReader.h.

References input_.

Referenced by initialize().

71 { return input_ != nullptr; }
edm::propagate_const< HepMC::IO_BaseClass * > input_

◆ printEvent()

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 }
virtual void getStatsFromTuple(int &mo1, int &mo2, int &da1, int &da2, int &status, int &pid, int j) const
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_
T sqrt(T t)
Definition: SSEVec.h:23

◆ printHepMcEvent()

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_

◆ rdstate()

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

◆ readCurrentEvent()

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_
Log< level::Info, false > LogInfo

◆ ReadStats()

void HepMCFileReader::ReadStats ( )
virtual

Definition at line 166 of file HepMCFileReader.cc.

References evt_, index_to_particle, LaserDQM_cfg::p1, SiStripOfflineCRack_cfg::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

◆ setEvent()

bool HepMCFileReader::setEvent ( int  event)
virtual

Definition at line 98 of file HepMCFileReader.cc.

98 { return true; }

Member Data Documentation

◆ evt_

edm::propagate_const<HepMC::GenEvent *> HepMCFileReader::evt_
private

◆ index_to_particle

std::vector<HepMC::GenParticle *> HepMCFileReader::index_to_particle
private

Definition at line 65 of file HepMCFileReader.h.

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

◆ input_

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

◆ instance_

HepMCFileReader * HepMCFileReader::instance_ = nullptr
staticprivate

Definition at line 60 of file HepMCFileReader.h.

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

◆ particle_to_index

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

Definition at line 66 of file HepMCFileReader.h.

Referenced by getStatsFromTuple(), and ReadStats().