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), m_vertices_positions_present(true)
20 {
21  if( !m_file.is_open() ) {
22  ERROR( "ReaderHEPEVT: could not open input file: "<<filename )
23  }
24  else
25  {
26  set_run_info(make_shared<GenRunInfo>());
27  hepevtbuffer=(char*)(new struct HEPEVT());
28  HEPEVT_Wrapper::set_hepevt_address(hepevtbuffer);
29  }
30 }
31 
32 ReaderHEPEVT::ReaderHEPEVT(std::istream & stream)
33  : m_stream(&stream), m_isstream(true), m_vertices_positions_present(true)
34 {
35  if( !m_stream->good() ) {
36  ERROR( "ReaderHEPEVT: could not open input stream ")
37  }
38  else
39  {
40  set_run_info(make_shared<GenRunInfo>());
41  hepevtbuffer=(char*)(new struct HEPEVT());
42  HEPEVT_Wrapper::set_hepevt_address(hepevtbuffer);
43  }
44 }
45 
47 {
48  const size_t max_e_buffer_size=512;
49  char buf_e[max_e_buffer_size];
50  bool eventline=false;
51  int m_i=0, m_p=0;
52  while(!eventline)
53  {
54  m_isstream ? m_stream->getline(buf_e,max_e_buffer_size) : m_file.getline(buf_e,max_e_buffer_size);
55  if( strlen(buf_e) == 0 ) return false;
56  std::stringstream st_e(buf_e);
57  char attr=' ';
58  eventline=false;
59  while (!eventline)
60  {
61  if (!(st_e>>attr)) break;
62  if (attr==' ') continue;
63  else eventline=false;
64  if (attr=='E')
65  {
66  eventline=static_cast<bool>(st_e>>m_i>>m_p);
67  }
68  }
69  }
70  HEPEVT_Wrapper::set_event_number(m_i);
71  HEPEVT_Wrapper::set_number_entries(m_p);
72  return eventline;
73 }
74 
75 
76 bool ReaderHEPEVT::read_hepevt_particle( int i, bool iflong )
77 {
78  const size_t max_p_buffer_size=512;
79  const size_t max_v_buffer_size=512;
80  char buf_p[max_p_buffer_size];
81  char buf_v[max_v_buffer_size];
82  int intcodes[6];
83  double fltcodes1[5];
84  double fltcodes2[4];
85  m_isstream ? m_stream->getline(buf_p,max_p_buffer_size) : m_file.getline(buf_p,max_p_buffer_size);
86  if( strlen(buf_p) == 0 ) return false;
87  if (iflong) {
88  m_isstream ? m_stream->getline(buf_v,max_v_buffer_size) : m_file.getline(buf_v,max_v_buffer_size);
89  if( strlen(buf_v) == 0 ) return false;
90  }
91  std::stringstream st_p(buf_p);
92  std::stringstream st_v(buf_v);
93  if (iflong)
94  {
95  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;}
96  if (!static_cast<bool>(st_v>>fltcodes2[0]>>fltcodes2[1]>>fltcodes2[2]>>fltcodes2[3])) { ERROR( "ReaderHEPEVT: Error reading particle vertex"); return false;}
97  }
98  else
99  {
100  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;}
101  intcodes[2]=0;//FIXME!
102  intcodes[3]=0;//FIXME!
103  fltcodes1[3]=std::sqrt(fltcodes1[0]*fltcodes1[0]+fltcodes1[1]*fltcodes1[1]+fltcodes1[2]*fltcodes1[2]+fltcodes1[4]*fltcodes1[4]);
104  fltcodes2[0]=0;
105  fltcodes2[1]=0;
106  fltcodes2[2]=0;
107  fltcodes2[3]=0;
108  }
109  HEPEVT_Wrapper::set_status(i,intcodes[0]);
110  HEPEVT_Wrapper::set_id(i,intcodes[1]);
111  HEPEVT_Wrapper::set_parents(i,intcodes[2],std::max(intcodes[2],intcodes[3]));/* Pythia writes second mother 0*/
112  HEPEVT_Wrapper::set_children(i,intcodes[4],intcodes[5]);
113  HEPEVT_Wrapper::set_momentum(i,fltcodes1[0],fltcodes1[1],fltcodes1[2],fltcodes1[3]);
114  HEPEVT_Wrapper::set_mass(i,fltcodes1[4]);
115  HEPEVT_Wrapper::set_position(i,fltcodes2[0],fltcodes2[1],fltcodes2[2],fltcodes2[3]);
116  return true;
117 
118 }
119 
120 bool ReaderHEPEVT::read_event(GenEvent& evt, bool iflong)
121 {
122  evt.clear();
124  bool fileok=read_hepevt_event_header();
125  for (int i=1; (i<=HEPEVT_Wrapper::number_entries())&&fileok; i++)
126  fileok=read_hepevt_particle(i, iflong);
127  bool result=false;
128  if (fileok)
129  {
131  shared_ptr<GenRunInfo> g=make_shared<GenRunInfo>();
132  std::vector<std::string> weightnames;
133  weightnames.push_back("0");
134  std::vector<double> wts;
135  wts.push_back(1.0);
136  g->set_weight_names(weightnames);
137  evt.set_run_info(g);
138  evt.weights()=wts;
139  }
140  else
141  {
142  m_isstream ? m_stream->clear(ios::badbit) : m_file.clear(ios::badbit);
143  }
144  return result;
145 }
147 {
148  if ( (!m_file.is_open()) && (!m_isstream) ) return false;
150 }
151 
152 
154 {
155  if (hepevtbuffer) delete hepevtbuffer;
156  if( !m_file.is_open()) return;
157  m_file.close();
158 }
159 
161 {
162  return m_isstream ? (bool)m_stream->rdstate() :(bool)m_file.rdstate();
163 }
164 
167 
168 } // namespace HepMC3
ERROR
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:23
HepMC3::ReaderHEPEVT::read_hepevt_particle
virtual bool read_hepevt_particle(int i, bool iflong=true)
read particle from file
Definition: ReaderHEPEVT.cc:76
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:42
HepMC3::ReaderHEPEVT::read_event
bool read_event(GenEvent &evt, bool iflong)
Read event from file.
Definition: ReaderHEPEVT.cc:120
HepMC3::ReaderHEPEVT::m_file
std::ifstream m_file
Input file.
Definition: ReaderHEPEVT.h:87
HepMC3::ReaderHEPEVT::close
void close()
Close file stream.
Definition: ReaderHEPEVT.cc:153
HepMC3::HEPEVT_Wrapper::set_mass
static void set_mass(const int &index, double mass)
Set mass.
Definition: HEPEVT_Wrapper.h:225
HepMC3::ReaderHEPEVT::m_vertices_positions_present
bool m_vertices_positions_present
true if vertex positions are available
Definition: ReaderHEPEVT.h:90
HepMC3::HEPEVT_Wrapper::set_children
static void set_children(const int &index, const int &firstchild, const int &lastchild)
Set children.
Definition: HEPEVT_Wrapper.h:211
HepMC3
HepMC3 main namespace.
Definition: ReaderGZ.h:28
HepMC3::ReaderHEPEVT::failed
bool failed()
Get stream error state.
Definition: ReaderHEPEVT.cc:160
HEPEVT_Wrapper.h
Definition of class HEPEVT_Wrapper.
HepMC3::HEPEVT_Wrapper::set_momentum
static void set_momentum(const int &index, const double &px, const double &py, const double &pz, const double &e)
Set 4-momentum.
Definition: HEPEVT_Wrapper.h:217
HepMC3::GenEvent::clear
void clear()
Remove contents of this event.
Definition: GenEvent.cc:610
HepMC3::ReaderHEPEVT::get_vertices_positions_present
bool get_vertices_positions_present() const
get flag if vertex positions are available
Definition: ReaderHEPEVT.cc:166
HEPEVT
C structure representing Fortran common block HEPEVT T. Sjöstrand et al., "A proposed standard event ...
Definition: HEPEVT_Wrapper.h:53
HepMC3::GenEvent::set_run_info
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:129
HepMC3::HEPEVT_Wrapper::set_position
static void set_position(const int &index, const double &x, const double &y, const double &z, const double &t)
Set position in time-space.
Definition: HEPEVT_Wrapper.h:230
HepMC3::ReaderHEPEVT::hepevtbuffer
char * hepevtbuffer
Pointer to HEPEVT Fortran common block/C struct.
Definition: ReaderHEPEVT.h:85
HepMC3::GenEvent::weights
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:87
HepMC3::HEPEVT_Wrapper::set_parents
static void set_parents(const int &index, const int &firstparent, const int &lastparent)
Set parents.
Definition: HEPEVT_Wrapper.h:205
ReaderHEPEVT.h
Definition of class ReaderHEPEVT.
HepMC3::ReaderHEPEVT::set_vertices_positions_present
void set_vertices_positions_present(bool iflong)
set flag if vertex positions are available
Definition: ReaderHEPEVT.cc:165
HepMC3::HEPEVT_Wrapper::zero_everything
static void zero_everything()
Set all entries in HEPEVT to zero.
Definition: HEPEVT_Wrapper.h:180
HepMC3::ReaderHEPEVT::ReaderHEPEVT
ReaderHEPEVT(const std::string &filename)
Default constructor.
Definition: ReaderHEPEVT.cc:18
HepMC3::ReaderHEPEVT::m_isstream
bool m_isstream
toggles usage of m_file or m_stream
Definition: ReaderHEPEVT.h:89
HepMC3::Reader::set_run_info
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Reader.h:46
HepMC3::ReaderHEPEVT::m_stream
std::istream * m_stream
For ctor when reading from stdin.
Definition: ReaderHEPEVT.h:88
HepMC3::ReaderHEPEVT::read_hepevt_event_header
virtual bool read_hepevt_event_header()
Find and read event header line from file.
Definition: ReaderHEPEVT.cc:46
HepMC3::HEPEVT_Wrapper::HEPEVT_to_GenEvent
static bool HEPEVT_to_GenEvent(GenEvent *evt)
Convert HEPEVT to GenEvent.
Definition: HEPEVT_Wrapper.cc:107