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