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"
13
14#include <sstream>
15namespace HepMC3
16{
17
18ReaderHEPEVT::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());
29 }
30}
31
32ReaderHEPEVT::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());
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 }
72 return eventline;
73}
74
75
76bool 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
120bool 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
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:23
Definition of class HEPEVT_Wrapper.
Definition of class ReaderHEPEVT.
Stores event-related information.
Definition: GenEvent.h:42
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:129
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:87
void clear()
Remove contents of this event.
Definition: GenEvent.cc:610
static void set_mass(const int &index, double mass)
Set mass.
static void set_momentum(const int &index, const double &px, const double &py, const double &pz, const double &e)
Set 4-momentum.
static void set_number_entries(const int &noentries)
Set number of entries.
static void zero_everything()
Set all entries in HEPEVT to zero.
static void set_id(const int &index, const int &id)
Set PDG particle id.
static void set_status(const int &index, const int &status)
Set status code.
static void set_hepevt_address(char *c)
Set Fortran block address.
static bool HEPEVT_to_GenEvent(GenEvent *evt)
Convert HEPEVT to GenEvent.
static void set_parents(const int &index, const int &firstparent, const int &lastparent)
Set parents.
static void set_position(const int &index, const double &x, const double &y, const double &z, const double &t)
Set position in time-space.
static void set_children(const int &index, const int &firstchild, const int &lastchild)
Set children.
static int number_entries()
Get number of entries.
static void set_event_number(const int &evtno)
Set event number.
bool m_isstream
toggles usage of m_file or m_stream
Definition: ReaderHEPEVT.h:89
ReaderHEPEVT(const std::string &filename)
Default constructor.
Definition: ReaderHEPEVT.cc:18
bool read_event(GenEvent &evt, bool iflong)
Read event from file.
bool get_vertices_positions_present() const
get flag if vertex positions are available
void set_vertices_positions_present(bool iflong)
set flag if vertex positions are available
std::ifstream m_file
Input file.
Definition: ReaderHEPEVT.h:87
void close()
Close file stream.
virtual bool read_hepevt_event_header()
Find and read event header line from file.
Definition: ReaderHEPEVT.cc:46
bool failed()
Get stream error state.
virtual bool read_hepevt_particle(int i, bool iflong=true)
read particle from file
Definition: ReaderHEPEVT.cc:76
char * hepevtbuffer
Pointer to HEPEVT Fortran common block/C struct.
Definition: ReaderHEPEVT.h:85
std::istream * m_stream
For ctor when reading from stdin.
Definition: ReaderHEPEVT.h:88
bool m_vertices_positions_present
true if vertex positions are available
Definition: ReaderHEPEVT.h:90
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Reader.h:46
HepMC3 main namespace.
Definition: ReaderGZ.h:28
Fortran common block HEPEVT.