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