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