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