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-2020 The HepMC collaboration (see AUTHORS for details)
5 //
6 /**
7  * @file ReaderHEPEVT.cc
8  * @brief Implementation of \b class ReaderHEPEVT
9  *
10  */
11 #include <sstream>
12 
13 #include "HepMC3/ReaderHEPEVT.h"
14 #include "HepMC3/HEPEVT_Wrapper.h"
15 
16 
17 namespace HepMC3
18 {
19 
20 ReaderHEPEVT::ReaderHEPEVT(const std::string &filename)
21  : m_file(filename), m_stream(0), m_isstream(false)
22 {
23  if ( !m_file.is_open() ) {
24  HEPMC3_ERROR("ReaderHEPEVT: could not open input file: " << filename)
25  }
26  else
27  {
28  set_run_info(std::make_shared<GenRunInfo>());
29  set_run_info(std::make_shared<GenRunInfo>());
30  hepevtbuffer = (char*)(new struct HEPEVT());
32  }
33 }
34 
35 ReaderHEPEVT::ReaderHEPEVT(std::istream & stream)
36  : m_stream(&stream), m_isstream(true)
37 {
38  if ( !m_stream->good() ) {
39  HEPMC3_ERROR("ReaderHEPEVT: could not open input stream ")
40  }
41  else
42  {
43  set_run_info(std::make_shared<GenRunInfo>());
44  hepevtbuffer = (char*)(new struct HEPEVT());
46  }
47 }
48 
49 bool ReaderHEPEVT::skip(const int n)
50 {
51  const size_t max_buffer_size = 512*512;
52  char buf[max_buffer_size];
53  int nn = n;
54  while (!failed()) {
55  char peek;
56  if ( (!m_file.is_open()) && (!m_isstream) ) return false;
57  m_isstream ? peek = m_stream->peek() : peek = m_file.peek();
58  if ( peek == 'E' ) nn--;
59  if ( nn < 0 ) return true;
60  m_isstream ? m_stream->getline(buf, max_buffer_size) : m_file.getline(buf, max_buffer_size);
61  }
62  return true;
63 }
64 
65 
66 
68 {
69  const size_t max_e_buffer_size = 512;
70  char buf_e[max_e_buffer_size];
71  bool eventline = false;
72  int m_i = 0, m_p = 0;
73  while (!eventline)
74  {
75  m_isstream ? m_stream->getline(buf_e, max_e_buffer_size) : m_file.getline(buf_e, max_e_buffer_size);
76  if ( strlen(buf_e) == 0 ) return false;
77  std::stringstream st_e(buf_e);
78  char attr = ' ';
79  eventline = false;
80  while (!eventline)
81  {
82  if (!(st_e >> attr)) break;
83  if (attr == ' ') continue;
84  else eventline = false;
85  if (attr == 'E')
86  {
87  eventline = static_cast<bool>(st_e >> m_i >> m_p);
88  }
89  }
90  }
93  return eventline;
94 }
95 
96 
98 {
99  const size_t max_p_buffer_size = 512;
100  const size_t max_v_buffer_size = 512;
101  char buf_p[max_p_buffer_size];
102  char buf_v[max_v_buffer_size];
103  int intcodes[6];
104  double fltcodes1[5];
105  double fltcodes2[4];
106  m_isstream ? m_stream->getline(buf_p, max_p_buffer_size) : m_file.getline(buf_p, max_p_buffer_size);
107  if ( strlen(buf_p) == 0 ) return false;
108  if (m_options.find("vertices_positions_are_absent") == m_options.end())
109  {
110  m_isstream ? m_stream->getline(buf_v, max_v_buffer_size) : m_file.getline(buf_v, max_v_buffer_size);
111  if ( strlen(buf_v) == 0 ) return false;
112  }
113  std::stringstream st_p(buf_p);
114  std::stringstream st_v(buf_v);
115  if (m_options.find("vertices_positions_are_absent") == m_options.end())
116  {
117  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;}
118  if (!static_cast<bool>(st_v >> fltcodes2[0] >> fltcodes2[1] >> fltcodes2[2] >> fltcodes2[3])) { HEPMC3_ERROR("ReaderHEPEVT: HEPMC3_ERROR reading particle vertex"); return false;}
119  }
120  else
121  {
122  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;}
123  intcodes[2] = 0;//FIXME! This is a feature of this format, but maybe there are better ideas.
124  intcodes[3] = 0;//FIXME! This is a feature of this format, but maybe there are better ideas.
125  fltcodes1[3] = std::sqrt(fltcodes1[0]*fltcodes1[0]+fltcodes1[1]*fltcodes1[1]+fltcodes1[2]*fltcodes1[2]+fltcodes1[4]*fltcodes1[4]);
126  fltcodes2[0] = 0;
127  fltcodes2[1] = 0;
128  fltcodes2[2] = 0;
129  fltcodes2[3] = 0;
130  }
131  HEPEVT_Wrapper::set_status(i, intcodes[0]);
132  HEPEVT_Wrapper::set_id(i, intcodes[1]);
133  HEPEVT_Wrapper::set_parents(i, intcodes[2], std::max(intcodes[2], intcodes[3]));/* Pythia writes second mother 0*/
134  HEPEVT_Wrapper::set_children(i, intcodes[4], intcodes[5]);
135  HEPEVT_Wrapper::set_momentum(i, fltcodes1[0], fltcodes1[1], fltcodes1[2], fltcodes1[3]);
136  HEPEVT_Wrapper::set_mass(i, fltcodes1[4]);
137  HEPEVT_Wrapper::set_position(i, fltcodes2[0], fltcodes2[1], fltcodes2[2], fltcodes2[3]);
138  return true;
139 }
140 
142 {
143  evt.clear();
145  bool fileok = read_hepevt_event_header();
146  for (int i = 1; (i <= HEPEVT_Wrapper::number_entries()) && fileok; i++)
147  fileok = read_hepevt_particle(i);
148  bool result = false;
149  if (fileok)
150  {
151  result = HEPEVT_Wrapper::HEPEVT_to_GenEvent(&evt);
152  std::shared_ptr<GenRunInfo> g = std::make_shared<GenRunInfo>();
153  std::vector<std::string> weightnames;
154  weightnames.push_back("0");
155  std::vector<double> wts;
156  wts.push_back(1.0);
157  g->set_weight_names(weightnames);
158  evt.set_run_info(g);
159  evt.weights() = wts;
160  }
161  else
162  {
163  m_isstream ? m_stream->clear(std::ios::badbit) : m_file.clear(std::ios::badbit);
164  }
165  return result;
166 }
167 
169 {
170  if (hepevtbuffer) delete hepevtbuffer;
171  if ( !m_file.is_open()) return;
172  m_file.close();
173 }
174 
176 {
177  return m_isstream ? (bool)m_stream->rdstate() :(bool)m_file.rdstate();
178 }
179 
180 } // namespace HepMC3
bool failed() override
Get stream error state.
bool skip(const int) override
skip events
Definition: ReaderHEPEVT.cc:49
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:75
ReaderHEPEVT(const std::string &filename)
Default constructor.
Definition: ReaderHEPEVT.cc:20
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:67
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:97
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:129
char * hepevtbuffer
Pointer to HEPEVT Fortran common block/C struct.
Definition: ReaderHEPEVT.h:71
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:24
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:73
void clear()
Remove contents of this event.
Definition: GenEvent.cc:599
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:74
static int number_entries()
Get number of entries.