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_file(filename), m_stream(0), m_isstream(false)
20 {
21  if( !m_file.is_open() ) {
22  HEPMC3_ERROR( "ReaderHEPEVT: could not open input file: "<<filename )
23  }
24  else
25  {
26  set_run_info(std::make_shared<GenRunInfo>());
27  set_run_info(std::make_shared<GenRunInfo>());
28  hepevtbuffer=(char*)(new struct HEPEVT());
30  }
31 }
32 
33 ReaderHEPEVT::ReaderHEPEVT(std::istream & stream)
34  : m_stream(&stream), m_isstream(true)
35 {
36  if( !m_stream->good() ) {
37  HEPMC3_ERROR( "ReaderHEPEVT: could not open input stream ")
38  }
39  else
40  {
41  set_run_info(std::make_shared<GenRunInfo>());
42  hepevtbuffer=(char*)(new struct HEPEVT());
44  }
45 }
46 
47 bool ReaderHEPEVT::skip(const int n)
48 {
49  const size_t max_buffer_size=512*512;
50  char buf[max_buffer_size];
51  int nn=n;
52  while(!failed()) {
53  char peek;
54  if ( (!m_file.is_open()) && (!m_isstream) ) return false;
55  m_isstream ? peek = m_stream->peek() : peek = m_file.peek();
56  if( peek=='E' ) nn--;
57  if (nn<0) return true;
58  m_isstream ? m_stream->getline(buf,max_buffer_size) : m_file.getline(buf,max_buffer_size);
59  }
60  return true;
61 }
62 
63 
64 
66 {
67  const size_t max_e_buffer_size=512;
68  char buf_e[max_e_buffer_size];
69  bool eventline=false;
70  int m_i=0, m_p=0;
71  while(!eventline)
72  {
73  m_isstream ? m_stream->getline(buf_e,max_e_buffer_size) : m_file.getline(buf_e,max_e_buffer_size);
74  if( strlen(buf_e) == 0 ) return false;
75  std::stringstream st_e(buf_e);
76  char attr=' ';
77  eventline=false;
78  while (!eventline)
79  {
80  if (!(st_e>>attr)) break;
81  if (attr==' ') continue;
82  else eventline=false;
83  if (attr=='E')
84  {
85  eventline=static_cast<bool>(st_e>>m_i>>m_p);
86  }
87  }
88  }
91  return eventline;
92 }
93 
94 
96 {
97  const size_t max_p_buffer_size=512;
98  const size_t max_v_buffer_size=512;
99  char buf_p[max_p_buffer_size];
100  char buf_v[max_v_buffer_size];
101  int intcodes[6];
102  double fltcodes1[5];
103  double fltcodes2[4];
104  m_isstream ? m_stream->getline(buf_p,max_p_buffer_size) : m_file.getline(buf_p,max_p_buffer_size);
105  if( strlen(buf_p) == 0 ) return false;
106  if (m_options.find("vertices_positions_are_absent")==m_options.end())
107  {
108  m_isstream ? m_stream->getline(buf_v,max_v_buffer_size) : m_file.getline(buf_v,max_v_buffer_size);
109  if( strlen(buf_v) == 0 ) return false;
110  }
111  std::stringstream st_p(buf_p);
112  std::stringstream st_v(buf_v);
113  if (m_options.find("vertices_positions_are_absent")==m_options.end())
114  {
115  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])) { HEPMC3_ERROR( "ReaderHEPEVT: HEPMC3_ERROR reading particle momenta"); return false;}
116  if (!static_cast<bool>(st_v>>fltcodes2[0]>>fltcodes2[1]>>fltcodes2[2]>>fltcodes2[3])) { HEPMC3_ERROR( "ReaderHEPEVT: HEPMC3_ERROR reading particle vertex"); return false;}
117  }
118  else
119  {
120  if (!static_cast<bool>(st_p>>intcodes[0]>>intcodes[1]>>intcodes[4]>>intcodes[5]>>fltcodes1[0]>>fltcodes1[1]>>fltcodes1[2]>>fltcodes1[4])) {HEPMC3_ERROR( "ReaderHEPEVT: HEPMC3_ERROR reading particle momenta"); return false;}
121  intcodes[2]=0;//FIXME! This is a feature of this format, but maybe there are better ideas.
122  intcodes[3]=0;//FIXME! This is a feature of this format, but maybe there are better ideas.
123  fltcodes1[3]=std::sqrt(fltcodes1[0]*fltcodes1[0]+fltcodes1[1]*fltcodes1[1]+fltcodes1[2]*fltcodes1[2]+fltcodes1[4]*fltcodes1[4]);
124  fltcodes2[0]=0;
125  fltcodes2[1]=0;
126  fltcodes2[2]=0;
127  fltcodes2[3]=0;
128  }
129  HEPEVT_Wrapper::set_status(i,intcodes[0]);
130  HEPEVT_Wrapper::set_id(i,intcodes[1]);
131  HEPEVT_Wrapper::set_parents(i,intcodes[2],std::max(intcodes[2],intcodes[3]));/* Pythia writes second mother 0*/
132  HEPEVT_Wrapper::set_children(i,intcodes[4],intcodes[5]);
133  HEPEVT_Wrapper::set_momentum(i,fltcodes1[0],fltcodes1[1],fltcodes1[2],fltcodes1[3]);
134  HEPEVT_Wrapper::set_mass(i,fltcodes1[4]);
135  HEPEVT_Wrapper::set_position(i,fltcodes2[0],fltcodes2[1],fltcodes2[2],fltcodes2[3]);
136  return true;
137 
138 }
139 
141 {
142  evt.clear();
144  bool fileok=read_hepevt_event_header();
145  for (int i=1; (i<=HEPEVT_Wrapper::number_entries())&&fileok; i++)
146  fileok=read_hepevt_particle(i);
147  bool result=false;
148  if (fileok)
149  {
151  std::shared_ptr<GenRunInfo> g=std::make_shared<GenRunInfo>();
152  std::vector<std::string> weightnames;
153  weightnames.push_back("0");
154  std::vector<double> wts;
155  wts.push_back(1.0);
156  g->set_weight_names(weightnames);
157  evt.set_run_info(g);
158  evt.weights()=wts;
159  }
160  else
161  {
162  m_isstream ? m_stream->clear(std::ios::badbit) : m_file.clear(std::ios::badbit);
163  }
164  return result;
165 }
166 
168 {
169  if (hepevtbuffer) delete hepevtbuffer;
170  if( !m_file.is_open()) return;
171  m_file.close();
172 }
173 
175 {
176  return m_isstream ? (bool)m_stream->rdstate() :(bool)m_file.rdstate();
177 }
178 
179 } // namespace HepMC3
bool failed() override
Get stream error state.
bool skip(const int) override
skip events
Definition: ReaderHEPEVT.cc:47
Definition of class ReaderHEPEVT.
void close() override
Close file stream.
std::map< std::string, std::string > m_options
options
Definition: Reader.h:68
bool m_isstream
toggles usage of m_file or m_stream
Definition: ReaderHEPEVT.h:76
ReaderHEPEVT(const std::string &filename)
Default constructor.
Definition: ReaderHEPEVT.cc:18
bool read_event(GenEvent &evt) override
Read event from file.
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:65
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.
Stores event-related information.
Definition: GenEvent.h:41
static void zero_everything()
Check for problems with HEPEVT common block.
Fortran common block HEPEVT.
static void set_number_entries(const int &noentries)
Set number of entries.
static bool HEPEVT_to_GenEvent(GenEvent *evt)
Convert HEPEVT to GenEvent.
virtual bool read_hepevt_particle(int i)
read particle from file
Definition: ReaderHEPEVT.cc:95
static void set_event_number(const int &evtno)
Set event number.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:128
char * hepevtbuffer
Pointer to HEPEVT Fortran common block/C struct.
Definition: ReaderHEPEVT.h:72
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Reader.h:64
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:23
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:86
static void set_status(const int &index, const int &status)
Set status code.
std::ifstream m_file
Input file.
Definition: ReaderHEPEVT.h:74
void clear()
Remove contents of this event.
Definition: GenEvent.cc:608
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.
std::istream * m_stream
For ctor when reading from stdin.
Definition: ReaderHEPEVT.h:75
static int number_entries()
Get number of entries.