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