MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
HepMC3::ReaderAscii Class Reference

GenEvent I/O parsing for structured text files. More...

#include <ReaderAscii.h>

Inheritance diagram for HepMC3::ReaderAscii:
HepMC3::Reader

Public Member Functions

 ReaderAscii (const std::string &filename)
 Constructor.
 
 ReaderAscii (std::istream &)
 The ctor to read from stream.
 
 ReaderAscii (std::shared_ptr< std::istream > s_stream)
 The ctor to read from stream. Useful for temp. streams.
 
 ~ReaderAscii ()
 Destructor.
 
void close () override
 Close file stream.
 
bool failed () override
 Return status of the stream.
 
bool read_event (GenEvent &evt) override
 Load event from file.
 
bool skip (const int) override
 skip events
 
- Public Member Functions inherited from HepMC3::Reader
 Reader ()
 Constructor.
 
 Reader (const Reader &)=delete
 deleted copy constructor
 
virtual ~Reader ()
 Virtual destructor.
 
virtual std::map< std::string, std::string > get_options () const
 Get options.
 
Readeroperator= (const Reader &)=delete
 deleted copy assignment operator
 
virtual std::shared_ptr< GenRunInforun_info () const
 Get the global GenRunInfo object.
 
virtual void set_options (const std::map< std::string, std::string > &options)
 Set options.
 
virtual void set_run_info (std::shared_ptr< GenRunInfo > run)
 Set the global GenRunInfo object.
 

Additional Inherited Members

- Protected Attributes inherited from HepMC3::Reader
std::map< std::string, std::string > m_options
 Options.
 

Detailed Description

GenEvent I/O parsing for structured text files.

Definition at line 31 of file ReaderAscii.h.

Constructor & Destructor Documentation

◆ ReaderAscii() [1/3]

HepMC3::ReaderAscii::ReaderAscii ( const std::string & filename)

Constructor.

Definition at line 2142 of file marley_hepmc3.cc.

2143 : m_file(filename), m_isstream(false)
2144{
2145 if ( !m_file.is_open() ) {
2146 HEPMC3_ERROR_LEVEL(100,"ReaderAscii: could not open input file: " << filename)
2147 }
2148 set_run_info(std::make_shared<GenRunInfo>());
2149}
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Reader.h:56

References HepMC3::Reader::set_run_info().

◆ ReaderAscii() [2/3]

HepMC3::ReaderAscii::ReaderAscii ( std::istream & stream)

The ctor to read from stream.

Definition at line 2151 of file marley_hepmc3.cc.

2152 : m_stream(&stream), m_isstream(true)
2153{
2154 if ( !m_stream->good() ) {
2155 HEPMC3_ERROR_LEVEL(100,"ReaderAscii: could not open input stream ")
2156 }
2157 set_run_info(std::make_shared<GenRunInfo>());
2158}

References HepMC3::Reader::set_run_info().

◆ ReaderAscii() [3/3]

HepMC3::ReaderAscii::ReaderAscii ( std::shared_ptr< std::istream > s_stream)

The ctor to read from stream. Useful for temp. streams.

Definition at line 2161 of file marley_hepmc3.cc.

2162 : m_shared_stream(s_stream), m_stream(s_stream.get()), m_isstream(true)
2163{
2164 if ( !m_stream->good() ) {
2165 HEPMC3_ERROR_LEVEL(100,"ReaderAscii: could not open input stream ")
2166 }
2167 set_run_info(std::make_shared<GenRunInfo>());
2168}

References HepMC3::Reader::set_run_info().

◆ ~ReaderAscii()

HepMC3::ReaderAscii::~ReaderAscii ( )

Destructor.

Definition at line 2170 of file marley_hepmc3.cc.

2170{ if (!m_isstream) close(); }
void close() override
Close file stream.

References close().

Member Function Documentation

◆ close()

void HepMC3::ReaderAscii::close ( )
overridevirtual

Close file stream.

Implements HepMC3::Reader.

Definition at line 2716 of file marley_hepmc3.cc.

2716 {
2717 if ( !m_file.is_open()) return;
2718 m_file.close();
2719}

Referenced by ~ReaderAscii().

◆ failed()

bool HepMC3::ReaderAscii::failed ( )
overridevirtual

Return status of the stream.

Implements HepMC3::Reader.

Definition at line 2714 of file marley_hepmc3.cc.

2714{ return m_isstream ? (bool)m_stream->rdstate() :(bool)m_file.rdstate(); }

Referenced by read_event(), and skip().

◆ read_event()

bool HepMC3::ReaderAscii::read_event ( GenEvent & evt)
overridevirtual

Load event from file.

Parameters
[out]evtEvent to be filled

Insert the implicit vertices in the gaps of explicit vertices: Find the gaps looping over the explicit vertices

Found a gap in ids, insert an implicit vertex into a list of gaps.

Implements HepMC3::Reader.

Definition at line 2208 of file marley_hepmc3.cc.

2208 {
2209 if ( (!m_file.is_open()) && (!m_isstream) ) return false;
2210
2211 char peek(0);
2212 std::array<char, 262144> buf{};
2213 bool event_context = false;
2214 bool parsed_weights = false;
2215 bool parsed_particles_or_vertices = false;
2216 bool run_info_context = false;
2217 bool is_parsing_successful = true;
2218 std::pair<int, int> vertices_and_particles(0, 0);
2219
2220 evt.clear();
2221 evt.set_run_info(run_info());
2222 m_io_explicit.clear();
2223 m_io_implicit.clear();
2224 m_io_implicit_ids.clear();
2225 m_io_explicit_ids.clear();
2226 m_data.particles.clear();
2227 m_data.vertices.clear();
2228 m_data.links1.clear();
2229 m_data.links2.clear();
2230 m_data.attribute_id.clear();
2231 m_data.attribute_name.clear();
2232 m_data.attribute_string.clear();
2233 //
2234 // Parse event, vertex and particle information
2235 //
2236 while (!failed()) {
2237 m_isstream ? m_stream->getline(buf.data(), buf.size()) : m_file.getline(buf.data(), buf.size());
2238
2239 if ( strlen(buf.data()) == 0 ) continue;
2240
2241 // Check for ReaderAscii header/footer
2242 if ( strncmp(buf.data(), "HepMC", 5) == 0 ) {
2243 if ( strncmp(buf.data(), "HepMC::Version", 14) != 0 && strncmp(buf.data(), "HepMC::Asciiv3", 14) != 0 )
2244 {
2245 HEPMC3_WARNING_LEVEL(500,"ReaderAscii: found unsupported expression in header. Will close the input.")
2246 std::cout << buf.data() << std::endl;
2247 m_isstream ? m_stream->clear(std::ios::eofbit) : m_file.clear(std::ios::eofbit);
2248 }
2249 if (event_context) {
2250 is_parsing_successful = true;
2251 break;
2252 }
2253 continue;
2254 }
2255
2256 switch (buf[0]) {
2257 case 'E':
2258 vertices_and_particles = parse_event_information( buf.data());
2259 if (vertices_and_particles.second < 0) {
2260 is_parsing_successful = false;
2261 } else {
2262 is_parsing_successful = true;
2263 event_context = true;
2264 parsed_weights = false;
2265 parsed_particles_or_vertices = false;
2266 }
2267
2268
2269 run_info_context = false;
2270 break;
2271 case 'V':
2272 is_parsing_successful = parse_vertex_information( buf.data());
2273 parsed_particles_or_vertices = true;
2274 break;
2275 case 'P':
2276 is_parsing_successful = parse_particle_information( buf.data());
2277 parsed_particles_or_vertices = true;
2278 break;
2279 case 'W':
2280 if ( event_context ) {
2281 is_parsing_successful = parse_weight_values( buf.data());
2282 parsed_weights=true;
2283 } else {
2284 if ( !run_info_context ) {
2285 set_run_info(std::make_shared<GenRunInfo>());
2286 evt.set_run_info(run_info());
2287 }
2288 run_info_context = true;
2289 is_parsing_successful = parse_weight_names(buf.data());
2290 }
2291 break;
2292 case 'U':
2293 is_parsing_successful = parse_units( buf.data());
2294 break;
2295 case 'T':
2296 if ( event_context ) {
2297 //We ignore T in the event context
2298 } else {
2299 if ( !run_info_context ) {
2300 set_run_info(std::make_shared<GenRunInfo>());
2301 evt.set_run_info(run_info());
2302 }
2303 run_info_context = true;
2304 is_parsing_successful = parse_tool(buf.data());
2305 }
2306 break;
2307 case 'A':
2308 if ( event_context ) {
2309 is_parsing_successful = parse_attribute( buf.data());
2310 } else {
2311 if ( !run_info_context ) {
2312 set_run_info(std::make_shared<GenRunInfo>());
2313 evt.set_run_info(run_info());
2314 }
2315 run_info_context = true;
2316 is_parsing_successful = parse_run_attribute(buf.data());
2317 }
2318 break;
2319 default:
2320 HEPMC3_WARNING_LEVEL(500,"ReaderAscii: skipping unrecognised prefix: " << buf[0])
2321 is_parsing_successful = true;
2322 break;
2323 }
2324
2325 if ( !is_parsing_successful ) break;
2326
2327 // Check for next event or run info
2328 m_isstream ? peek = m_stream->peek() : peek = m_file.peek();
2329 //End of event. The next entry is event.
2330 if ( event_context && peek == 'E' ) break;
2331
2332 //End of event. The next entry is run info which starts from weight name.
2333 if ( event_context && peek == 'W' && parsed_weights ) break;
2334
2335 //End of event. The next entry is run info which starts from attribute.
2336 if ( event_context && peek == 'A' && parsed_particles_or_vertices ) break;
2337
2338 //End of event. The next entry is run info which starts from tool.
2339 if ( event_context && peek == 'T' ) break;
2340
2341 }
2342
2345 int currid = -static_cast<int>(m_data.vertices.size());
2346 auto fir = m_io_implicit_ids.rbegin();
2347 for (const auto& iofirst: m_io_explicit_ids) {
2348 for (; currid < iofirst; ++currid, ++fir) {
2349 if (fir == m_io_implicit_ids.rend()) {
2350 HEPMC3_ERROR_LEVEL(600,"ReaderAscii: not enough implicit vertices")
2351 }
2353 m_io_explicit[currid] = std::move(m_io_implicit[*fir]);
2354 }
2355 ++currid;
2356 }
2357
2358 for (const auto& io: m_io_explicit) {
2359 for (const auto& i: io.second.first) { m_data.links1.push_back(i); m_data.links2.push_back(io.first); }
2360 for (const auto& o: io.second.second) { m_data.links1.push_back(io.first); m_data.links2.push_back(o); }
2361 }
2362 evt.read_data(m_data);
2363
2364 // Check if all particles and vertices were parsed
2365 if ((int)evt.particles().size() > vertices_and_particles.second) {
2366 HEPMC3_ERROR_LEVEL(600,"ReaderAscii: too many particles were parsed")
2367 printf("%zu vs %i expected\n", evt.particles().size(), vertices_and_particles.second);
2368 is_parsing_successful = false;
2369 }
2370 if ((int)evt.particles().size() < vertices_and_particles.second) {
2371 HEPMC3_ERROR_LEVEL(600,"ReaderAscii: too few particles were parsed")
2372 printf("%zu vs %i expected\n", evt.particles().size(), vertices_and_particles.second);
2373 is_parsing_successful = false;
2374 }
2375
2376 if ((int)evt.vertices().size() > vertices_and_particles.first) {
2377 HEPMC3_ERROR_LEVEL(600,"ReaderAscii: too many vertices were parsed")
2378 printf("%zu vs %i expected\n", evt.vertices().size(), vertices_and_particles.first);
2379 is_parsing_successful = false;
2380 }
2381
2382 if ((int)evt.vertices().size() < vertices_and_particles.first) {
2383 HEPMC3_ERROR_LEVEL(600,"ReaderAscii: too few vertices were parsed")
2384 printf("%zu vs %i expected\n", evt.vertices().size(), vertices_and_particles.first);
2385 is_parsing_successful = false;
2386 }
2387 // Check if there were HEPMC3_ERRORs during parsing
2388 if ( !is_parsing_successful ) {
2389 HEPMC3_ERROR_LEVEL(600,"ReaderAscii: event parsing failed. Returning empty event")
2390 HEPMC3_DEBUG(1, "Parsing failed at line:" << std::endl << buf.data())
2391
2392 evt.clear();
2393 m_isstream ? m_stream->clear(std::ios::badbit) : m_file.clear(std::ios::badbit);
2394
2395 return false;
2396 }
2397
2398
2399 return true;
2400}
bool failed() override
Return status of the stream.
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Reader.h:44

References HepMC3::GenEvent::clear(), failed(), HepMC3::GenEvent::particles(), HepMC3::GenEvent::read_data(), HepMC3::Reader::run_info(), HepMC3::GenEvent::set_run_info(), HepMC3::Reader::set_run_info(), and HepMC3::GenEvent::vertices().

Referenced by marley::EventFileReader::deduce_file_format().

◆ skip()

bool HepMC3::ReaderAscii::skip ( const int n)
overridevirtual

skip events

Reimplemented from HepMC3::Reader.

Definition at line 2172 of file marley_hepmc3.cc.

2173{
2174 std::array<char, 262144> buf{};
2175 bool event_context = false;
2176 bool run_info_context = false;
2177 int nn = n;
2178 while (!failed()) {
2179 char peek(0);
2180 if ( (!m_file.is_open()) && (!m_isstream) ) return false;
2181 m_isstream ? peek = m_stream->peek() : peek = m_file.peek();
2182 if ( peek == 'E' ) { event_context = true; nn--; }
2183 //We have to read each run info.
2184 if ( !event_context && ( peek == 'W' || peek == 'A' || peek == 'T' ) ) {
2185 m_isstream ? m_stream->getline(buf.data(), buf.size()) : m_file.getline(buf.data(), buf.size());
2186 if (!run_info_context) {
2187 set_run_info(std::make_shared<GenRunInfo>());
2188 run_info_context = true;
2189 }
2190 if ( peek == 'W' ) {
2191 parse_weight_names(buf.data());
2192 }
2193 if ( peek == 'T' ) {
2194 parse_tool(buf.data());
2195 }
2196 if ( peek == 'A' ) {
2197 parse_run_attribute(buf.data());
2198 }
2199 }
2200 if ( event_context && ( peek == 'V' || peek == 'P' ) ) event_context=false;
2201 if (nn < 0) return true;
2202 m_isstream ? m_stream->getline(buf.data(), buf.size()) : m_file.getline(buf.data(), buf.size());
2203 }
2204 return true;
2205}

References failed(), and HepMC3::Reader::set_run_info().


The documentation for this class was generated from the following files: