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"
26
27
28namespace HepMC3 {
29
30
31class 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
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
Definition of struct GenEventData.
Definition of class GenEvent.
Definition of interface Reader.
Stores event-related information.
Definition GenEvent.h:47
bool parse_weight_values(const char *buf)
Parse weight value lines.
bool parse_tool(const char *buf)
Parse run-level tool information.
bool m_isstream
toggles usage of m_file or m_stream
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.
std::pair< int, int > parse_event_information(const char *buf)
Parse event.
bool read_event(GenEvent &evt) override
Load event from file.
static std::string unescape(const std::string &s)
Unsecape '\' and ' ' characters in string.
bool failed() override
Return status of the stream.
bool skip(const int) override
skip events
std::ifstream m_file
Input file.
bool parse_pdf_info(const char *buf)
Parse struct GenPdfInfo information.
std::set< int > m_io_explicit_ids
Temp storage to keep the order of explicit vertices.
void close() override
Close file stream.
bool parse_heavy_ion(const char *buf)
Parse struct GenHeavyIon information.
std::vector< int > m_io_implicit_ids
Temp storage to keep the order of implicit vertices.
bool parse_vertex_information(const char *buf)
Parse vertex.
GenEventData m_data
To hold event information.
bool parse_attribute(const char *buf)
Parse attribute.
~ReaderAscii()
Destructor.
bool parse_weight_names(const char *buf)
Parse run-level weight names.
bool parse_cross_section(const char *buf)
Parse struct GenCrossSection information.
bool parse_units(const char *buf)
Parse units.
ReaderAscii(const std::string &filename)
Constructor.
std::istream * m_stream
For ctor when reading from stream.
std::shared_ptr< std::istream > m_shared_stream
For ctor when reading from temp. stream.
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.
bool parse_particle_information(const char *buf)
Parse particle.
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
Reader()
Constructor.
Definition Reader.h:28
HepMC3 main namespace.
Stores serializable event information.