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-2021 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 #include "HepMC3/ReaderHEPEVT.h"
13 
14 namespace HepMC3
15 {
16 
17 ReaderHEPEVT::ReaderHEPEVT(const std::string &filename)
18  : m_file(filename), m_stream(0), m_isstream(false)
19 {
20  if ( !m_file.is_open() ) {
21  HEPMC3_ERROR("ReaderHEPEVT: could not open input file: " << filename)
22  }
23  else
24  {
25  set_run_info(std::make_shared<GenRunInfo>());
27 
28  }
29 }
30 
31 ReaderHEPEVT::ReaderHEPEVT(std::istream & stream)
32  : m_stream(&stream), m_isstream(true)
33 {
34  if ( !m_stream->good() ) {
35  HEPMC3_ERROR("ReaderHEPEVT: could not open input stream ")
36  }
37  else
38  {
39  set_run_info(std::make_shared<GenRunInfo>());
41  }
42 }
43 
44 ReaderHEPEVT::ReaderHEPEVT(std::shared_ptr<std::istream> s_stream)
45  : m_shared_stream(s_stream), m_stream(s_stream.get()), m_isstream(true)
46 {
47  if ( !m_stream->good() ) {
48  HEPMC3_ERROR("ReaderHEPEVT: could not open input stream ")
49  }
50  else
51  {
52  set_run_info(std::make_shared<GenRunInfo>());
54  }
55 }
56 
57 
58 bool ReaderHEPEVT::skip(const int n)
59 {
60  const size_t max_buffer_size = 512*512;
61  char buf[max_buffer_size];
62  int nn = n;
63  while (!failed()) {
64  char peek;
65  if ( (!m_file.is_open()) && (!m_isstream) ) return false;
66  m_isstream ? peek = m_stream->peek() : peek = m_file.peek();
67  if ( peek == 'E' ) nn--;
68  if ( nn < 0 ) return true;
69  m_isstream ? m_stream->getline(buf, max_buffer_size) : m_file.getline(buf, max_buffer_size);
70  }
71  return true;
72 }
73 
74 
75 
77 {
78  const size_t max_e_buffer_size = 512;
79  char buf_e[max_e_buffer_size];
80  bool eventline = false;
81  int m_i = 0, m_p = 0;
82  while (!eventline)
83  {
84  m_isstream ? m_stream->getline(buf_e, max_e_buffer_size) : m_file.getline(buf_e, max_e_buffer_size);
85  if ( strlen(buf_e) == 0 ) return false;
86  std::stringstream st_e(buf_e);
87  char attr = ' ';
88  eventline = false;
89  while (!eventline)
90  {
91  if (!(st_e >> attr)) break;
92  if (attr == ' ') continue;
93  else eventline = false;
94  if (attr == 'E')
95  {
96  eventline = static_cast<bool>(st_e >> m_i >> m_p);
97  }
98  }
99  }
102  return eventline;
103 }
104 
105 
107 {
108  const size_t max_p_buffer_size = 512;
109  const size_t max_v_buffer_size = 512;
110  char buf_p[max_p_buffer_size];
111  char buf_v[max_v_buffer_size];
112  int intcodes[6];
113  double fltcodes1[5];
114  double fltcodes2[4];
115  m_isstream ? m_stream->getline(buf_p, max_p_buffer_size) : m_file.getline(buf_p, max_p_buffer_size);
116  if ( strlen(buf_p) == 0 ) return false;
117  if (m_options.find("vertices_positions_are_absent") == m_options.end())
118  {
119  m_isstream ? m_stream->getline(buf_v, max_v_buffer_size) : m_file.getline(buf_v, max_v_buffer_size);
120  if ( strlen(buf_v) == 0 ) return false;
121  }
122  std::stringstream st_p(buf_p);
123  std::stringstream st_v(buf_v);
124  if (m_options.find("vertices_positions_are_absent") == m_options.end())
125  {
126  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;}
127  if (!static_cast<bool>(st_v >> fltcodes2[0] >> fltcodes2[1] >> fltcodes2[2] >> fltcodes2[3])) { HEPMC3_ERROR("ReaderHEPEVT: HEPMC3_ERROR reading particle vertex"); return false;}
128  }
129  else
130  {
131  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;}
132  intcodes[2] = 0;//FIXME! This is a feature of this format, but maybe there are better ideas.
133  intcodes[3] = 0;//FIXME! This is a feature of this format, but maybe there are better ideas.
134  fltcodes1[3] = std::sqrt(fltcodes1[0]*fltcodes1[0]+fltcodes1[1]*fltcodes1[1]+fltcodes1[2]*fltcodes1[2]+fltcodes1[4]*fltcodes1[4]);
135  fltcodes2[0] = 0;
136  fltcodes2[1] = 0;
137  fltcodes2[2] = 0;
138  fltcodes2[3] = 0;
139  }
140  m_hepevt_interface.set_status(i, intcodes[0]);
141  m_hepevt_interface.set_id(i, intcodes[1]);
142  m_hepevt_interface.set_parents(i, intcodes[2], std::max(intcodes[2], intcodes[3]));/* Pythia writes second mother 0*/
143  m_hepevt_interface.set_children(i, intcodes[4], intcodes[5]);
144  m_hepevt_interface.set_momentum(i, fltcodes1[0], fltcodes1[1], fltcodes1[2], fltcodes1[3]);
145  m_hepevt_interface.set_mass(i, fltcodes1[4]);
146  m_hepevt_interface.set_position(i, fltcodes2[0], fltcodes2[1], fltcodes2[2], fltcodes2[3]);
147  return true;
148 }
149 
151 {
152  evt.clear();
154  bool fileok = read_hepevt_event_header();
155  for (int i = 1; (i <= m_hepevt_interface.number_entries()) && fileok; i++)
156  fileok = read_hepevt_particle(i);
157  bool result = false;
158  if (fileok)
159  {
160  result = m_hepevt_interface.HEPEVT_to_GenEvent(&evt);
161  std::shared_ptr<GenRunInfo> g = std::make_shared<GenRunInfo>();
162  std::vector<std::string> weightnames;
163  weightnames.push_back("0");
164  std::vector<double> wts;
165  wts.push_back(1.0);
166  g->set_weight_names(weightnames);
167  evt.set_run_info(g);
168  evt.weights() = wts;
169  }
170  else
171  {
172  m_isstream ? m_stream->clear(std::ios::badbit) : m_file.clear(std::ios::badbit);
173  }
174  return result;
175 }
176 
178 {
179  if ( !m_file.is_open()) return;
180  m_file.close();
181 }
182 
184 {
185  return m_isstream ? (bool)m_stream->rdstate() :(bool)m_file.rdstate();
186 }
187 
188 } // namespace HepMC3
void set_mass(const int index, double mass)
Set mass.
bool failed() override
Get stream error state.
bool skip(const int) override
skip events
Definition: ReaderHEPEVT.cc:58
Definition of class ReaderHEPEVT.
void close() override
Close file stream.
std::map< std::string, std::string > m_options
options
Definition: Reader.h:68
void set_id(const int index, const int id)
Set PDG particle id.
void set_momentum(const int index, const double px, const double py, const double pz, const double e)
Set 4-momentum.
bool m_isstream
toggles usage of m_file or m_stream
Definition: ReaderHEPEVT.h:77
void allocate_internal_storage()
Allocates m_internal_storage storage in smart pointer to hold HEPEVT of fixed size.
bool HEPEVT_to_GenEvent(GenEvent *evt) const
Convert HEPEVT to GenEvent.
ReaderHEPEVT(const std::string &filename)
Default constructor.
Definition: ReaderHEPEVT.cc:17
bool read_event(GenEvent &evt) override
Read event from file.
void set_parents(const int index, const int firstparent, const int lastparent)
Set parents.
void set_event_number(const int evtno)
Set event number.
virtual bool read_hepevt_event_header()
Find and read event header line from file.
Definition: ReaderHEPEVT.cc:76
Stores event-related information.
Definition: GenEvent.h:41
virtual bool read_hepevt_particle(int i)
read particle from file
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:141
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:98
void set_number_entries(const int noentries)
Set number of entries.
int number_entries() const
Get number of entries.
void set_children(const int index, const int firstchild, const int lastchild)
Set children.
std::ifstream m_file
Input file.
Definition: ReaderHEPEVT.h:74
void set_position(const int index, const double x, const double y, const double z, const double t)
Set position in time-space.
void clear()
Remove contents of this event.
Definition: GenEvent.cc:599
void set_status(const int index, const int status)
Set status code.
void zero_everything()
Set all entries in HEPEVT to zero.
HEPEVT_Wrapper_Template< 100000 > m_hepevt_interface
Templated HEPEVT interface.
Definition: ReaderHEPEVT.h:78
std::istream * m_stream
For ctor when reading from stream.
Definition: ReaderHEPEVT.h:76