HepMC event record
build/outputs/include/HepMC/ReaderAscii.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2015 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef HEPMC_READERASCII_H
7 #define HEPMC_READERASCII_H
8 ///
9 /// @file ReaderAscii.h
10 /// @brief Definition of class \b ReaderAscii
11 ///
12 /// @class HepMC::ReaderAscii
13 /// @brief GenEvent I/O parsing for structured text files
14 ///
15 /// @ingroup IO
16 ///
17 #include "HepMC/Reader.h"
18 #include "HepMC/GenEvent.h"
19 #include <string>
20 #include <fstream>
21 #include <istream>
22 
23 namespace HepMC {
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 Get global attribute
53  ///
54  /// @todo rewrite global attributes
55  shared_ptr<Attribute> get_global(std::string name);
56 
57  /// @brief Unsecape '\' and '\n' characters in string
58  std::string unescape(const std::string s);
59 
60  /// @name Read helpers
61  //@{
62 
63  /// @brief Parse event
64  ///
65  /// Helper routine for parsing event information
66  /// @param[out] evt Event that will be filled with new data
67  /// @param[in] buf Line of text that needs to be parsed
68  /// @return vertices count and particles count for verification
69  std::pair<int,int> parse_event_information(GenEvent &evt, const char *buf);
70 
71  /// @brief Parse weight value lines
72  ///
73  /// Helper routine for parsing weight value information
74  /// @param[out] evt Event whose GenWeights will be filled with weight values
75  /// @param[in] buf Line of text that needs to be parsed
76  ///
77  bool parse_weight_values(GenEvent &evt, const char *buf);
78 
79  /// @brief Parse units
80  ///
81  /// Helper routine for parsing units information
82  /// @param[out] evt Event that will be filled with unit information
83  /// @param[in] buf Line of text that needs to be parsed
84  ///
85  bool parse_units(GenEvent &evt, const char *buf);
86 
87  /// @brief Parse struct GenPdfInfo information
88  ///
89  /// Helper routine for parsing PDF information
90  /// @param[out] evt Event that will be filled with unit information
91  /// @param[in] buf Line of text that needs to be parsed
92  bool parse_pdf_info(GenEvent &evt, const char *buf);
93 
94  /// @brief Parse struct GenHeavyIon information
95  ///
96  /// Helper routine for parsing heavy ion information
97  /// @param[out] evt Event that will be filled with unit information
98  /// @param[in] buf Line of text that needs to be parsed
99  bool parse_heavy_ion(GenEvent &evt, const char *buf);
100 
101  /// @brief Parse struct GenCrossSection information
102  ///
103  /// Helper routine for parsing cross-section information
104  /// @param[out] evt Event that will be filled with unit information
105  /// @param[in] buf Line of text that needs to be parsed
106  bool parse_cross_section(GenEvent &evt, const char *buf);
107 
108  /// @brief Parse vertex
109  ///
110  /// Helper routine for parsing single event information
111  /// @param[out] evt Event that will contain parsed vertex
112  /// @param[in] buf Line of text that needs to be parsed
113  ///
114  bool parse_vertex_information(GenEvent &evt, const char *buf);
115 
116  /// @brief Parse particle
117  ///
118  /// Helper routine for parsing single particle information
119  /// @param[out] evt Event that will contain parsed particle
120  /// @param[in] buf Line of text that needs to be parsed
121  bool parse_particle_information(GenEvent &evt, const char *buf);
122 
123  /// @brief Parse attribute
124  ///
125  /// Helper routine for parsing single attribute information
126  /// @param[out] evt Event that will contain parsed attribute
127  /// @param[in] buf Line of text that needs to be parsed
128  bool parse_attribute(GenEvent &evt, const char *buf);
129 
130  /// @brief Parse run-level attribute.
131  ///
132  /// Helper routine for parsing single attribute information
133  /// @param[in] buf Line of text that needs to be parsed
134  bool parse_run_attribute(const char *buf);
135 
136  /// @brief Parse run-level weight names.
137  ///
138  /// Helper routine for parsing a line with information about
139  /// weight names.
140  /// @param[in] buf Line of text that needs to be parsed
141  bool parse_weight_names(const char *buf);
142 
143  /// @brief Parse run-level tool information.
144  ///
145  /// Helper routine for parsing a line with information about
146  /// tools being used.
147  /// @param[in] buf Line of text that needs to be parsed
148  bool parse_tool(const char *buf);
149  //@}
150 
151 
152  private:
153 
154  /// @todo Unify file/stream treatment
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 };
164 
165 
166 } // namespace HepMC
167 
168 #endif
bool parse_weight_names(const char *buf)
Parse run-level weight names.
Definition: ReaderAscii.cc:429
Base class for all I/O readers.
bool failed()
Return status of the stream.
ReaderAscii(const std::string &filename)
Constructor.
Definition: ReaderAscii.cc:22
bool read_event(GenEvent &evt)
Load event from file.
Definition: ReaderAscii.cc:47
std::pair< int, int > parse_event_information(GenEvent &evt, const char *buf)
Parse event.
Definition: ReaderAscii.cc:151
~ReaderAscii()
Destructor.
Definition: ReaderAscii.cc:44
void close()
Close file stream.
Definition: ReaderAscii.cc:484
bool parse_cross_section(GenEvent &evt, const char *buf)
Parse struct GenCrossSection information.
Stores event-related information.
std::map< std::string, shared_ptr< Attribute > > m_global_attributes
Store attributes global to the run being written/read.
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
Definition: ReaderAscii.cc:406
bool parse_vertex_information(GenEvent &evt, const char *buf)
Parse vertex.
Definition: ReaderAscii.cc:236
bool parse_attribute(GenEvent &evt, const char *buf)
Parse attribute.
Definition: ReaderAscii.cc:381
bool parse_units(GenEvent &evt, const char *buf)
Parse units.
Definition: ReaderAscii.cc:215
bool parse_tool(const char *buf)
Parse run-level tool information.
Definition: ReaderAscii.cc:446
bool parse_weight_values(GenEvent &evt, const char *buf)
Parse weight value lines.
Definition: ReaderAscii.cc:198
Definition of template class SmartPointer.
bool parse_heavy_ion(GenEvent &evt, const char *buf)
Parse struct GenHeavyIon information.
GenEvent I/O parsing for structured text files.
bool parse_pdf_info(GenEvent &evt, const char *buf)
Parse struct GenPdfInfo information.
bool parse_particle_information(GenEvent &evt, const char *buf)
Parse particle.
Definition: ReaderAscii.cc:306
shared_ptr< Attribute > get_global(std::string name)
Get global attribute.
std::string unescape(const std::string s)
Unsecape &#39;\&#39; and &#39; &#39; characters in string.
Definition: ReaderAscii.cc:466