HepMC3 event record library: ReaderHEPEVT.cc Source File

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_isstream(false)
20{
21 if ( !m_file.is_open() ) {
22 HEPMC3_ERROR_LEVEL(100,"ReaderHEPEVT: could not open input file: " << filename)
23 }
24 else
25 {
26 set_run_info(std::make_shared<GenRunInfo>());
27 m_hepevt_interface.allocate_internal_storage();
28
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_LEVEL(100,"ReaderHEPEVT: could not open input stream ")
37 }
38 else
39 {
40 set_run_info(std::make_shared<GenRunInfo>());
41 m_hepevt_interface.allocate_internal_storage();
42 }
43}
44
45 ReaderHEPEVT::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_LEVEL(100,"ReaderHEPEVT: could not open input stream ")
50 }
51 else
52 {
53 set_run_info(std::make_shared<GenRunInfo>());
54 m_hepevt_interface.allocate_internal_storage();
55 }
56}
57
58
59 bool 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
76 bool ReaderHEPEVT::read_hepevt_event_header()
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 }
100 m_hepevt_interface.set_event_number(m_i);
101 m_hepevt_interface.set_number_entries(m_p);
102 return eventline;
103}
104
105
106 bool ReaderHEPEVT::read_hepevt_particle(int i)
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])) {
125 HEPMC3_ERROR_LEVEL(100,"ReaderHEPEVT: HEPMC3_ERROR reading particle momenta")
126 return false;
127 }
128 if (!static_cast<bool>(st_v >> fltcodes2[0] >> fltcodes2[1] >> fltcodes2[2] >> fltcodes2[3])) {
129 HEPMC3_ERROR_LEVEL(100,"ReaderHEPEVT: HEPMC3_ERROR reading particle vertex")
130 return false;
131 }
132 }
133 else
134 {
135 if (!static_cast<bool>(st_p>> intcodes[0]>> intcodes[1] >> intcodes[4] >> intcodes[5] >> fltcodes1[0] >> fltcodes1[1] >> fltcodes1[2] >> fltcodes1[4])) {
136 HEPMC3_ERROR_LEVEL(100,"ReaderHEPEVT: HEPMC3_ERROR reading particle momenta")
137 return false;
138 }
139 intcodes[2] = 0;//FIXME! This is a feature of this format, but maybe there are better ideas.
140 intcodes[3] = 0;//FIXME! This is a feature of this format, but maybe there are better ideas.
141 fltcodes1[3] = std::sqrt(fltcodes1[0]*fltcodes1[0]+fltcodes1[1]*fltcodes1[1]+fltcodes1[2]*fltcodes1[2]+fltcodes1[4]*fltcodes1[4]);
142 fltcodes2[0] = 0;
143 fltcodes2[1] = 0;
144 fltcodes2[2] = 0;
145 fltcodes2[3] = 0;
146 }
147 m_hepevt_interface.set_status(i, intcodes[0]);
148 m_hepevt_interface.set_id(i, intcodes[1]);
149 m_hepevt_interface.set_parents(i, intcodes[2], std::max(intcodes[2], intcodes[3]));/* Pythia writes second mother 0*/
150 m_hepevt_interface.set_children(i, intcodes[4], intcodes[5]);
151 m_hepevt_interface.set_momentum(i, fltcodes1[0], fltcodes1[1], fltcodes1[2], fltcodes1[3]);
152 m_hepevt_interface.set_mass(i, fltcodes1[4]);
153 m_hepevt_interface.set_position(i, fltcodes2[0], fltcodes2[1], fltcodes2[2], fltcodes2[3]);
154 return true;
155}
156
157 bool ReaderHEPEVT::read_event(GenEvent& evt)
158{
159 evt.clear();
160 m_hepevt_interface.zero_everything();
161 bool fileok = read_hepevt_event_header();
162 for (int i = 1; (i <= m_hepevt_interface.number_entries()) && fileok; i++) {
163 fileok = read_hepevt_particle(i);
164 }
165 bool result = false;
166 if (fileok)
167 {
168 result = m_hepevt_interface.HEPEVT_to_GenEvent(&evt);
169 std::shared_ptr<GenRunInfo> g = std::make_shared<GenRunInfo>();
170 std::vector<std::string> weightnames(1,"0");
171 std::vector<double> wts(1,1.0);
172 g->set_weight_names(weightnames);
173 evt.set_run_info(g);
174 evt.weights() = wts;
175 }
176 else
177 {
178 m_isstream ? m_stream->clear(std::ios::badbit) : m_file.clear(std::ios::badbit);
179 }
180 return result;
181}
182
183 void ReaderHEPEVT::close()
184{
185 if ( !m_file.is_open()) return;
186 m_file.close();
187}
188
189 bool ReaderHEPEVT::failed()
190{
191 return m_isstream ? static_cast<bool>(m_stream->rdstate()) : static_cast<bool> (m_file.rdstate());
192}
193
194} // namespace HepMC3
HEPMC3_ERROR_LEVEL
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Definition Errors.h:27
ReaderHEPEVT.h
Definition of class ReaderHEPEVT.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::GenEvent::set_run_info
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition GenEvent.h:148
HepMC3::GenEvent::weights
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition GenEvent.h:105
HepMC3::GenEvent::clear
void clear()
Remove contents of this event.
Definition GenEvent.cc:592
HepMC3::ReaderHEPEVT::m_isstream
bool m_isstream
toggles usage of m_file or m_stream
Definition ReaderHEPEVT.h:77
HepMC3::ReaderHEPEVT::read_event
bool read_event(GenEvent &evt) override
Read event from file.
Definition ReaderHEPEVT.cc:157
HepMC3::ReaderHEPEVT::ReaderHEPEVT
ReaderHEPEVT(const std::string &filename)
Default constructor.
Definition ReaderHEPEVT.cc:18
HepMC3::ReaderHEPEVT::m_hepevt_interface
HEPEVT_Wrapper_Template< 100000 > m_hepevt_interface
Templated HEPEVT interface.
Definition ReaderHEPEVT.h:78
HepMC3::ReaderHEPEVT::failed
bool failed() override
Get stream error state.
Definition ReaderHEPEVT.cc:189
HepMC3::ReaderHEPEVT::skip
bool skip(const int) override
skip events
Definition ReaderHEPEVT.cc:59
HepMC3::ReaderHEPEVT::m_file
std::ifstream m_file
Input file.
Definition ReaderHEPEVT.h:74
HepMC3::ReaderHEPEVT::read_hepevt_particle
virtual bool read_hepevt_particle(int i)
read particle from file
Definition ReaderHEPEVT.cc:106
HepMC3::ReaderHEPEVT::read_hepevt_event_header
virtual bool read_hepevt_event_header()
Find and read event header line from file.
Definition ReaderHEPEVT.cc:76
HepMC3::ReaderHEPEVT::close
void close() override
Close file stream.
Definition ReaderHEPEVT.cc:183
HepMC3::ReaderHEPEVT::m_stream
std::istream * m_stream
For ctor when reading from stream.
Definition ReaderHEPEVT.h:76
HepMC3::ReaderHEPEVT::m_shared_stream
std::shared_ptr< std::istream > m_shared_stream
For ctor when reading from temp stream.
Definition ReaderHEPEVT.h:75
HepMC3::Reader::m_options
std::map< std::string, std::string > m_options
Options.
Definition Reader.h:60
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24