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