HepMC3 event record library: ReaderAscii.h Source File

HepMC3 event record library

ReaderAscii.h
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#ifndef HEPMC3_READERASCII_H
7#define HEPMC3_READERASCII_H
8///
9/// @file ReaderAscii.h
10/// @brief Definition of class \b ReaderAscii
11///
12/// @class HepMC3::ReaderAscii
13/// @brief GenEvent I/O parsing for structured text files
14///
15/// @ingroup IO
16///
17#include <set>
18#include <string>
19#include <fstream>
20#include <istream>
21#include <iterator>
22#include <unordered_map>
23#include "HepMC3/Reader.h"
24#include "HepMC3/GenEvent.h"
25#include "HepMC3/Data/GenEventData.h"
26
27
28namespace HepMC3 {
29
30
31 class ReaderAscii : public Reader {
32public:
33
34 /// @brief Constructor
35 ReaderAscii(const std::string& filename);
36 /// The ctor to read from stream
37 ReaderAscii(std::istream &);
38 /// The ctor to read from stream. Useful for temp. streams
39 ReaderAscii(std::shared_ptr<std::istream> s_stream);
40 /// @brief Destructor
41 ~ReaderAscii();
42
43 /// @brief skip events
44 bool skip(const int) override;
45
46 /// @brief Load event from file
47 ///
48 /// @param[out] evt Event to be filled
49 bool read_event(GenEvent& evt) override;
50
51 /// @brief Return status of the stream
52 bool failed() override;
53
54 /// @brief Close file stream
55 void close() override;
56
57private:
58
59 /// @brief Unsecape '\' and '\n' characters in string
60 static std::string unescape(const std::string& s);
61
62 /// @name Read helpers
63 /// @{
64
65 /// @brief Parse event
66 ///
67 /// Helper routine for parsing event information
68 /// @param[in] buf Line of text that needs to be parsed
69 /// @return vertices count and particles count for verification
70 std::pair<int,int> parse_event_information(const char *buf);
71
72 /// @brief Parse weight value lines
73 ///
74 /// Helper routine for parsing weight value information
75 /// @param[in] buf Line of text that needs to be parsed
76 ///
77 bool parse_weight_values(const char *buf);
78
79 /// @brief Parse units
80 ///
81 /// Helper routine for parsing units information
82 /// @param[in] buf Line of text that needs to be parsed
83 ///
84 bool parse_units(const char *buf);
85
86 /// @brief Parse struct GenPdfInfo information
87 ///
88 /// Helper routine for parsing PDF information
89 /// @param[in] buf Line of text that needs to be parsed
90 bool parse_pdf_info(const char *buf);
91
92 /// @brief Parse struct GenHeavyIon information
93 ///
94 /// Helper routine for parsing heavy ion information
95 /// @param[in] buf Line of text that needs to be parsed
96 bool parse_heavy_ion(const char *buf);
97
98 /// @brief Parse struct GenCrossSection information
99 ///
100 /// Helper routine for parsing cross-section information
101 /// @param[in] buf Line of text that needs to be parsed
102 bool parse_cross_section( const char *buf);
103
104 /// @brief Parse vertex
105 ///
106 /// Helper routine for parsing single event information
107 /// @param[in] buf Line of text that needs to be parsed
108 ///
109 bool parse_vertex_information(const char *buf);
110
111 /// @brief Parse particle
112 ///
113 /// Helper routine for parsing single particle information
114 /// @param[in] buf Line of text that needs to be parsed
115 bool parse_particle_information(const char *buf);
116
117 /// @brief Parse attribute
118 ///
119 /// Helper routine for parsing single attribute information
120 /// @param[in] buf Line of text that needs to be parsed
121 bool parse_attribute( const char *buf);
122
123 /// @brief Parse run-level attribute.
124 ///
125 /// Helper routine for parsing single attribute information
126 /// @param[in] buf Line of text that needs to be parsed
127 bool parse_run_attribute(const char *buf);
128
129 /// @brief Parse run-level weight names.
130 ///
131 /// Helper routine for parsing a line with information about
132 /// weight names.
133 /// @param[in] buf Line of text that needs to be parsed
134 bool parse_weight_names(const char *buf);
135
136 /// @brief Parse run-level tool information.
137 ///
138 /// Helper routine for parsing a line with information about
139 /// tools being used.
140 /// @param[in] buf Line of text that needs to be parsed
141 bool parse_tool(const char *buf);
142 /// @}
143
144
145private:
146
147 std::ifstream m_file; //!< Input file
148 std::shared_ptr<std::istream> m_shared_stream = nullptr;///< For ctor when reading from temp. stream
149 std::istream* m_stream = nullptr; ///< For ctor when reading from stream
150 bool m_isstream; ///< toggles usage of m_file or m_stream
151
152 /** @brief Temp storage for sets of incoming/outgoing ids for explicit vertices.*/
153 std::map<int, std::pair< std::set<int>, std::set<int> > > m_io_explicit;
154 /** @brief Temp storage for sets of incoming/outgoing ids for implicit vertices.*/
155 std::unordered_map<int, std::pair< std::set<int>, std::set<int> > > m_io_implicit;
156 /** @brief Temp storage to keep the order of implicit vertices.*/
157 std::vector<int> m_io_implicit_ids;
158 /** @brief Temp storage to keep the order of explicit vertices.*/
159 std::set<int> m_io_explicit_ids;
160
161 GenEventData m_data; //!< To hold event information.
162};
163
164
165} // namespace HepMC3
166
167#endif
GenEventData.h
Definition of struct GenEventData.
GenEvent.h
Definition of class GenEvent.
Reader.h
Definition of interface Reader.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::ReaderAscii::parse_weight_values
bool parse_weight_values(const char *buf)
Parse weight value lines.
Definition ReaderAscii.cc:336
HepMC3::ReaderAscii::parse_tool
bool parse_tool(const char *buf)
Parse run-level tool information.
Definition ReaderAscii.cc:558
HepMC3::ReaderAscii::m_isstream
bool m_isstream
toggles usage of m_file or m_stream
Definition ReaderAscii.h:150
HepMC3::ReaderAscii::m_io_explicit
std::map< int, std::pair< std::set< int >, std::set< int > > > m_io_explicit
Temp storage for sets of incoming/outgoing ids for explicit vertices.
Definition ReaderAscii.h:153
HepMC3::ReaderAscii::parse_event_information
std::pair< int, int > parse_event_information(const char *buf)
Parse event.
Definition ReaderAscii.cc:285
HepMC3::ReaderAscii::read_event
bool read_event(GenEvent &evt) override
Load event from file.
Definition ReaderAscii.cc:90
HepMC3::ReaderAscii::unescape
static std::string unescape(const std::string &s)
Unsecape '\' and ' ' characters in string.
Definition ReaderAscii.cc:577
HepMC3::ReaderAscii::failed
bool failed() override
Return status of the stream.
Definition ReaderAscii.cc:596
HepMC3::ReaderAscii::skip
bool skip(const int) override
skip events
Definition ReaderAscii.cc:54
HepMC3::ReaderAscii::m_file
std::ifstream m_file
Input file.
Definition ReaderAscii.h:147
HepMC3::ReaderAscii::parse_pdf_info
bool parse_pdf_info(const char *buf)
Parse struct GenPdfInfo information.
HepMC3::ReaderAscii::m_io_explicit_ids
std::set< int > m_io_explicit_ids
Temp storage to keep the order of explicit vertices.
Definition ReaderAscii.h:159
HepMC3::ReaderAscii::close
void close() override
Close file stream.
Definition ReaderAscii.cc:598
HepMC3::ReaderAscii::parse_heavy_ion
bool parse_heavy_ion(const char *buf)
Parse struct GenHeavyIon information.
HepMC3::ReaderAscii::m_io_implicit_ids
std::vector< int > m_io_implicit_ids
Temp storage to keep the order of implicit vertices.
Definition ReaderAscii.h:157
HepMC3::ReaderAscii::parse_vertex_information
bool parse_vertex_information(const char *buf)
Parse vertex.
Definition ReaderAscii.cc:372
HepMC3::ReaderAscii::m_data
GenEventData m_data
To hold event information.
Definition ReaderAscii.h:161
HepMC3::ReaderAscii::parse_attribute
bool parse_attribute(const char *buf)
Parse attribute.
Definition ReaderAscii.cc:496
HepMC3::ReaderAscii::~ReaderAscii
~ReaderAscii()
Destructor.
Definition ReaderAscii.cc:52
HepMC3::ReaderAscii::parse_weight_names
bool parse_weight_names(const char *buf)
Parse run-level weight names.
Definition ReaderAscii.cc:542
HepMC3::ReaderAscii::parse_cross_section
bool parse_cross_section(const char *buf)
Parse struct GenCrossSection information.
HepMC3::ReaderAscii::parse_units
bool parse_units(const char *buf)
Parse units.
Definition ReaderAscii.cc:353
HepMC3::ReaderAscii::ReaderAscii
ReaderAscii(const std::string &filename)
Constructor.
Definition ReaderAscii.cc:24
HepMC3::ReaderAscii::m_stream
std::istream * m_stream
For ctor when reading from stream.
Definition ReaderAscii.h:149
HepMC3::ReaderAscii::m_shared_stream
std::shared_ptr< std::istream > m_shared_stream
For ctor when reading from temp. stream.
Definition ReaderAscii.h:148
HepMC3::ReaderAscii::m_io_implicit
std::unordered_map< int, std::pair< std::set< int >, std::set< int > > > m_io_implicit
Temp storage for sets of incoming/outgoing ids for implicit vertices.
Definition ReaderAscii.h:155
HepMC3::ReaderAscii::parse_particle_information
bool parse_particle_information(const char *buf)
Parse particle.
Definition ReaderAscii.cc:431
HepMC3::ReaderAscii::parse_run_attribute
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
Definition ReaderAscii.cc:520
HepMC3::Reader::Reader
Reader()
Constructor.
Definition Reader.h:28
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::GenEventData
Stores serializable event information.
Definition GenEventData.h:26