HepMC event record
ReaderHEPEVT.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 /**
3  * @file ReaderHEPEVT.cc
4  * @brief Implementation of \b class ReaderHEPEVT
5  *
6  */
7 #include "HepMC/ReaderHEPEVT.h"
8 #include "HepMC/HEPEVT_Wrapper.h"
9 
10 #include <sstream>
11 namespace HepMC
12 {
13 
14 ReaderHEPEVT::ReaderHEPEVT(const std::string &filename):
15  m_events_count(0)
16 {
17  m_failed=false;
18  set_run_info(make_shared<GenRunInfo>());
19 
20  m_file= fopen(filename.c_str(),"r");
21  if (!m_file) {m_failed=true; ERROR( "ReaderHEPEVT: file opening failed" ); }
22  else
23  {
24  hepevtbuffer=(char*)(new struct HEPEVT());
26  }
27 
28 }
29 
30 #define READERHEPEVTBUFFERSIZE 255
32 {
33  char buf_e[READERHEPEVTBUFFERSIZE];
34  bool eventline=false;
35  int m_i=0, m_p=0;
36  while(!eventline)
37  {
38  if (fgets(buf_e,READERHEPEVTBUFFERSIZE,m_file)==NULL) break;
39  std::stringstream st_e(buf_e);
40  char attr=' ';
41  eventline=false;
42  while (!eventline)
43  {
44  if (!(st_e>>attr)) break;
45  if (attr==' ') continue;
46  else eventline=false;
47  if (attr=='E')
48  {
49  eventline=static_cast<bool>(st_e>>m_i>>m_p);
50  }
51  }
52  }
55  return eventline;
56 }
57 
58 
59 bool ReaderHEPEVT::read_hepevt_particle( int i, bool iflong )
60 {
61  char buf_p[READERHEPEVTBUFFERSIZE];
62  char buf_v[READERHEPEVTBUFFERSIZE];
63  int intcodes[6];
64  double fltcodes1[5];
65  double fltcodes2[4];
66  if (fgets(buf_p,READERHEPEVTBUFFERSIZE,m_file)==NULL) return false;
67  if (iflong) if (fgets(buf_v,READERHEPEVTBUFFERSIZE,m_file)==NULL) return false;
68  std::stringstream st_p(buf_p);
69  std::stringstream st_v(buf_v);
70  if (iflong)
71  {
72  if (!static_cast<bool>(st_p>>intcodes[0]>>intcodes[1]>>intcodes[2]>>intcodes[3]>>intcodes[4]>>intcodes[5]>>fltcodes1[0]>>fltcodes1[1]>>fltcodes1[2]>>fltcodes1[3]>>fltcodes1[4])) { ERROR( "ReaderHEPEVT: Error reading particle momenta"); return false;}
73  if (!static_cast<bool>(st_v>>fltcodes2[0]>>fltcodes2[1]>>fltcodes2[2]>>fltcodes2[3])) { ERROR( "ReaderHEPEVT: Error reading particle vertex"); return false;}
74  }
75  else
76  {
77  if (!static_cast<bool>(st_p>>intcodes[0]>>intcodes[1]>>intcodes[4]>>intcodes[5]>>fltcodes1[0]>>fltcodes1[1]>>fltcodes1[2]>>fltcodes1[4])) {ERROR( "ReaderHEPEVT: Error reading particle momenta"); return false;}
78  intcodes[2]=0;//FIXME!
79  intcodes[3]=0;//FIXME!
80  fltcodes1[3]=std::sqrt(fltcodes1[0]*fltcodes1[0]+fltcodes1[1]*fltcodes1[1]+fltcodes1[2]*fltcodes1[2]+fltcodes1[4]*fltcodes1[4]);
81  fltcodes2[0]=0;
82  fltcodes2[1]=0;
83  fltcodes2[2]=0;
84  fltcodes2[3]=0;
85  }
86  HEPEVT_Wrapper::set_status(i,intcodes[0]);
87  HEPEVT_Wrapper::set_id(i,intcodes[1]);
88  HEPEVT_Wrapper::set_parents(i,intcodes[2],std::max(intcodes[2],intcodes[3]));/* Pythia writes second mother 0*/
89  HEPEVT_Wrapper::set_children(i,intcodes[4],intcodes[5]);
90  HEPEVT_Wrapper::set_momentum(i,fltcodes1[0],fltcodes1[1],fltcodes1[2],fltcodes1[3]);
91  HEPEVT_Wrapper::set_mass(i,fltcodes1[4]);
92  HEPEVT_Wrapper::set_position(i,fltcodes2[0],fltcodes2[1],fltcodes2[2],fltcodes2[3]);
93  return true;
94 
95 }
96 
97 bool ReaderHEPEVT::read_event(GenEvent& evt, bool iflong)
98 {
99  evt.clear();
100 
101 
102  bool fileok=true;
104  fileok=read_hepevt_event_header();
105  for (int i=1; (i<=HEPEVT_Wrapper::number_entries())&&fileok; i++)
106  fileok=read_hepevt_particle(i, iflong);
107  bool result=false;
108  if (fileok)
109  {
110  m_events_count++;
112  shared_ptr<GenRunInfo> g=make_shared<GenRunInfo>();
113  std::vector<std::string> weightnames;
114  weightnames.push_back("0");
115  std::vector<double> wts;
116  wts.push_back(1.0);
117  g->set_weight_names(weightnames);
118  evt.set_run_info(g);
119  evt.weights()=wts;
120  }
121  else m_failed=true;
122  return result;
123 }
125 {return read_event(evt,true); }
126 
127 
129 {
130  if (m_file) fclose(m_file);
131 }
132 
134 {
135  return m_failed;
136 }
137 
138 } // namespace HepMC
static void set_number_entries(int noentries)
Set number of entries.
static void set_status(int index, int status)
Set status code.
char * hepevtbuffer
Pointer to HEPEVT Fortran common block/C struct.
static void set_parents(int index, int firstparent, int lastparent)
Set parents.
static void set_hepevt_address(char *c)
Set Fortran block address.
void close()
Close file stream.
virtual bool read_hepevt_particle(int i, bool iflong=true)
read particle from file
Definition: ReaderHEPEVT.cc:59
void clear()
Remove contents of this event.
Definition: GenEvent.cc:374
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Stores event-related information.
static void set_id(int index, int id)
Set PDG particle id.
virtual bool read_hepevt_event_header()
Find and read event header line from file.
Definition: ReaderHEPEVT.cc:31
bool failed()
Get stream error state.
Fortran common block HEPEVT.
static void set_event_number(int evtno)
Set event number.
static void set_children(int index, int firstchild, int lastchild)
Set children.
static void set_mass(int index, double mass)
Set mass.
static int number_entries()
Get number of entries.
Definition of template class SmartPointer.
ReaderHEPEVT(const std::string &filename)
Default constructor.
Definition: ReaderHEPEVT.cc:14
static void zero_everything()
Set all entries in HEPEVT to zero.
static void set_position(int index, double x, double y, double z, double t)
Set position in time-space.
bool read_event(GenEvent &evt, bool iflong)
Read event from file.
Definition: ReaderHEPEVT.cc:97
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
static void set_momentum(int index, double px, double py, double pz, double e)
Set 4-momentum.
const std::vector< double > & weights() const
Get event weight values as a vector.
static bool HEPEVT_to_GenEvent(GenEvent *evt)
Convert HEPEVT to GenEvent.