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-2019 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 
26 namespace HepMC3 {
27 
28 
29 class ReaderAscii : public Reader {
30 public:
31 
32  /// @brief Constructor
33  ReaderAscii(const std::string& filename);
34 #ifndef HEPMC3_PYTHON_BINDINGS
35  /// The ctor to read from stdin
36  ReaderAscii(std::istream &);
37 #endif
38  /// @brief Destructor
39  ~ReaderAscii();
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 
59 private:
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 
156 private:
157 
158  std::ifstream m_file; //!< Input file
159  std::istream* m_stream; ///< For ctor when reading from stdin
160  bool m_isstream; ///< toggles usage of m_file or m_stream
161 
162 
163  /** @brief Store attributes global to the run being written/read. */
164  std::map< std::string, shared_ptr<Attribute> > m_global_attributes;
165 
166  /** @brief Temp storage for outgoing particle ids */
167  std::map<GenVertexPtr, std::set<int> > m_forward_mothers;
168  /** @brief Temp storage for prod vertex ids */
169  std::map<GenParticlePtr, int > m_forward_daughters;
170 
171 };
172 
173 
174 } // namespace HepMC3
175 
176 #endif
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
Definition: ReaderAscii.cc:476
HepMC3 main namespace.
bool parse_cross_section(GenEvent &evt, const char *buf)
Parse struct GenCrossSection information.
Definition of interface Reader.
ReaderAscii(const std::string &filename)
Constructor.
Definition: ReaderAscii.cc:22
bool parse_units(GenEvent &evt, const char *buf)
Parse units.
Definition: ReaderAscii.cc:270
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:516
bool read_event(GenEvent &evt) override
Load event from file.
Definition: ReaderAscii.cc:63
std::pair< int, int > parse_event_information(GenEvent &evt, const char *buf)
Parse event.
Definition: ReaderAscii.cc:206
std::ifstream m_file
Input file.
Definition: ReaderAscii.h:158
std::map< GenVertexPtr, std::set< int > > m_forward_mothers
Temp storage for outgoing particle ids.
Definition: ReaderAscii.h:167
bool parse_weight_names(const char *buf)
Parse run-level weight names.
Definition: ReaderAscii.cc:499
bool parse_attribute(GenEvent &evt, const char *buf)
Parse attribute.
Definition: ReaderAscii.cc:451
Stores event-related information.
Definition: GenEvent.h:41
std::istream * m_stream
For ctor when reading from stdin.
Definition: ReaderAscii.h:159
bool parse_heavy_ion(GenEvent &evt, const char *buf)
Parse struct GenHeavyIon information.
bool failed() override
Return status of the stream.
Definition: ReaderAscii.cc:553
bool skip(const int) override
skip events
Definition: ReaderAscii.cc:46
std::map< GenParticlePtr, int > m_forward_daughters
Temp storage for prod vertex ids.
Definition: ReaderAscii.h:169
bool m_isstream
toggles usage of m_file or m_stream
Definition: ReaderAscii.h:160
std::map< std::string, shared_ptr< Attribute > > m_global_attributes
Store attributes global to the run being written/read.
Definition: ReaderAscii.h:164
bool parse_vertex_information(GenEvent &evt, const char *buf)
Parse vertex.
Definition: ReaderAscii.cc:291
std::string unescape(const std::string &s)
Unsecape '\' and ' ' characters in string.
Definition: ReaderAscii.cc:536
Definition of class GenEvent.
bool parse_particle_information(GenEvent &evt, const char *buf)
Parse particle.
Definition: ReaderAscii.cc:363
Base class for all I/O readers.
Definition: Reader.h:25
~ReaderAscii()
Destructor.
Definition: ReaderAscii.cc:44
void close() override
Close file stream.
Definition: ReaderAscii.cc:555
bool parse_pdf_info(GenEvent &evt, const char *buf)
Parse struct GenPdfInfo information.
bool parse_weight_values(GenEvent &evt, const char *buf)
Parse weight value lines.
Definition: ReaderAscii.cc:253