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