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-2021 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 "HepMC3/Reader.h"
23#include "HepMC3/GenEvent.h"
24
25
26namespace HepMC3 {
27
28
29class ReaderAscii : public Reader {
30public:
31
32 /// @brief Constructor
33 ReaderAscii(const std::string& filename);
34 /// The ctor to read from stream
35 ReaderAscii(std::istream &);
36 /// The ctor to read from stream. Useful for temp. streams
37 ReaderAscii(std::shared_ptr<std::istream> s_stream);
38 /// @brief Destructor
40
41 /// @brief skip events
42 bool skip(const int) override;
43
44 /// @brief Load event from file
45 ///
46 /// @param[out] evt Event to be filled
47 bool read_event(GenEvent& evt) override;
48
49 /// @todo No-arg version returning GenEvent?
50
51 /// @brief Return status of the stream
52 bool failed() override;
53
54 /// @todo Implicit cast to bool = !failed()?
55
56 /// @brief Close file stream
57 void close() override;
58
59private:
60
61 /// @brief Unsecape '\' and '\n' characters in string
62 std::string unescape(const std::string& s);
63
64 /// @name Read helpers
65 /// @{
66
67 /// @brief Parse event
68 ///
69 /// Helper routine for parsing event information
70 /// @param[out] evt Event that will be filled with new data
71 /// @param[in] buf Line of text that needs to be parsed
72 /// @return vertices count and particles count for verification
73 std::pair<int,int> parse_event_information(GenEvent &evt, const char *buf);
74
75 /// @brief Parse weight value lines
76 ///
77 /// Helper routine for parsing weight value information
78 /// @param[out] evt Event whose GenWeights will be filled with weight values
79 /// @param[in] buf Line of text that needs to be parsed
80 ///
81 bool parse_weight_values(GenEvent &evt, const char *buf);
82
83 /// @brief Parse units
84 ///
85 /// Helper routine for parsing units information
86 /// @param[out] evt Event that will be filled with unit information
87 /// @param[in] buf Line of text that needs to be parsed
88 ///
89 bool parse_units(GenEvent &evt, const char *buf);
90
91 /// @brief Parse struct GenPdfInfo information
92 ///
93 /// Helper routine for parsing PDF information
94 /// @param[out] evt Event that will be filled with unit information
95 /// @param[in] buf Line of text that needs to be parsed
96 bool parse_pdf_info(GenEvent &evt, const char *buf);
97
98 /// @brief Parse struct GenHeavyIon information
99 ///
100 /// Helper routine for parsing heavy ion information
101 /// @param[out] evt Event that will be filled with unit information
102 /// @param[in] buf Line of text that needs to be parsed
103 bool parse_heavy_ion(GenEvent &evt, const char *buf);
104
105 /// @brief Parse struct GenCrossSection information
106 ///
107 /// Helper routine for parsing cross-section information
108 /// @param[out] evt Event that will be filled with unit information
109 /// @param[in] buf Line of text that needs to be parsed
110 bool parse_cross_section(GenEvent &evt, const char *buf);
111
112 /// @brief Parse vertex
113 ///
114 /// Helper routine for parsing single event information
115 /// @param[out] evt Event that will contain parsed vertex
116 /// @param[in] buf Line of text that needs to be parsed
117 ///
118 bool parse_vertex_information(GenEvent &evt, const char *buf);
119
120 /// @brief Parse particle
121 ///
122 /// Helper routine for parsing single particle information
123 /// @param[out] evt Event that will contain parsed particle
124 /// @param[in] buf Line of text that needs to be parsed
125 bool parse_particle_information(GenEvent &evt, const char *buf);
126
127 /// @brief Parse attribute
128 ///
129 /// Helper routine for parsing single attribute information
130 /// @param[out] evt Event that will contain parsed attribute
131 /// @param[in] buf Line of text that needs to be parsed
132 bool parse_attribute(GenEvent &evt, const char *buf);
133
134 /// @brief Parse run-level attribute.
135 ///
136 /// Helper routine for parsing single attribute information
137 /// @param[in] buf Line of text that needs to be parsed
138 bool parse_run_attribute(const char *buf);
139
140 /// @brief Parse run-level weight names.
141 ///
142 /// Helper routine for parsing a line with information about
143 /// weight names.
144 /// @param[in] buf Line of text that needs to be parsed
145 bool parse_weight_names(const char *buf);
146
147 /// @brief Parse run-level tool information.
148 ///
149 /// Helper routine for parsing a line with information about
150 /// tools being used.
151 /// @param[in] buf Line of text that needs to be parsed
152 bool parse_tool(const char *buf);
153 /// @}
154
155
156private:
157
158 std::ifstream m_file; //!< Input file
159 std::shared_ptr<std::istream> m_shared_stream;///< For ctor when reading from temp. stream
160 std::istream* m_stream; ///< For ctor when reading from stream
161 bool m_isstream; ///< toggles usage of m_file or m_stream
162
163
164 /** @brief Store attributes global to the run being written/read. */
165 std::map< std::string, std::shared_ptr<Attribute> > m_global_attributes;
166
167 /** @brief Temp storage for outgoing particle ids */
168 std::map<GenVertexPtr, std::set<int> > m_forward_mothers;
169 /** @brief Temp storage for prod vertex ids */
170 std::map<GenParticlePtr, int > m_forward_daughters;
171
172};
173
174
175} // namespace HepMC3
176
177#endif
Definition of class GenEvent.
Definition of interface Reader.
Stores event-related information.
Definition: GenEvent.h:41
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:29
bool parse_tool(const char *buf)
Parse run-level tool information.
Definition: ReaderAscii.cc:579
bool m_isstream
toggles usage of m_file or m_stream
Definition: ReaderAscii.h:161
bool parse_weight_values(GenEvent &evt, const char *buf)
Parse weight value lines.
Definition: ReaderAscii.cc:320
std::map< GenParticlePtr, int > m_forward_daughters
Temp storage for prod vertex ids.
Definition: ReaderAscii.h:170
bool read_event(GenEvent &evt) override
Load event from file.
Definition: ReaderAscii.cc:90
std::string unescape(const std::string &s)
Unsecape '\' and ' ' characters in string.
Definition: ReaderAscii.cc:598
std::map< std::string, std::shared_ptr< Attribute > > m_global_attributes
Store attributes global to the run being written/read.
Definition: ReaderAscii.h:165
bool failed() override
Return status of the stream.
Definition: ReaderAscii.cc:615
bool parse_pdf_info(GenEvent &evt, const char *buf)
Parse struct GenPdfInfo information.
bool skip(const int) override
skip events
Definition: ReaderAscii.cc:53
std::ifstream m_file
Input file.
Definition: ReaderAscii.h:158
bool parse_units(GenEvent &evt, const char *buf)
Parse units.
Definition: ReaderAscii.cc:336
bool parse_particle_information(GenEvent &evt, const char *buf)
Parse particle.
Definition: ReaderAscii.cc:429
std::map< GenVertexPtr, std::set< int > > m_forward_mothers
Temp storage for outgoing particle ids.
Definition: ReaderAscii.h:168
bool parse_attribute(GenEvent &evt, const char *buf)
Parse attribute.
Definition: ReaderAscii.cc:516
bool parse_cross_section(GenEvent &evt, const char *buf)
Parse struct GenCrossSection information.
void close() override
Close file stream.
Definition: ReaderAscii.cc:617
bool parse_vertex_information(GenEvent &evt, const char *buf)
Parse vertex.
Definition: ReaderAscii.cc:357
~ReaderAscii()
Destructor.
Definition: ReaderAscii.cc:51
bool parse_weight_names(const char *buf)
Parse run-level weight names.
Definition: ReaderAscii.cc:563
std::istream * m_stream
For ctor when reading from stream.
Definition: ReaderAscii.h:160
std::shared_ptr< std::istream > m_shared_stream
For ctor when reading from temp. stream.
Definition: ReaderAscii.h:159
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
Definition: ReaderAscii.cc:541
bool parse_heavy_ion(GenEvent &evt, const char *buf)
Parse struct GenHeavyIon information.
std::pair< int, int > parse_event_information(GenEvent &evt, const char *buf)
Parse event.
Definition: ReaderAscii.cc:274
Base class for all I/O readers.
Definition: Reader.h:25
HepMC3 main namespace.