HepMC3 event record library: ReaderGZ.h Source File

HepMC3 event record library

ReaderGZ.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6#ifndef HEPMC3_READERGZ_H
7#define HEPMC3_READERGZ_H
8///
9/// @file ReaderGZ.h
10/// @brief Definition of class \b ReaderGZ
11///
12/// @class HepMC3::ReaderGZ
13/// @brief GenEvent I/O parsing for compressed 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#include "HepMC3/CompressedIO.h"
25namespace HepMC3 {
26
27 template <class T> class ReaderGZ : public Reader {
28public:
29
30 /// @brief Constructor
31 ReaderGZ(const std::string& filename) {
32 m_zstr = std::shared_ptr< std::istream >(new ifstream(filename.c_str()));
33 m_reader = std::make_shared<T>(*(m_zstr.get()));
34 }
35 /// @brief The ctor to read from stdin
36 ReaderGZ(std::istream & is) {
37 m_zstr = std::shared_ptr< std::istream >(new istream(is));
38 m_reader = std::make_shared<T>(*(m_zstr.get()));
39 }
40 /// @brief The ctor to read from shared pointer to stream
41 ReaderGZ(std::shared_ptr<std::istream> s_stream) {
42 m_zstr = s_stream;
43 m_reader = std::make_shared<T>(*(m_zstr.get()));
44 }
45
46 /// @brief Destructor
47 ~ReaderGZ() { close(); }
48
49 /// @brief skip events
50 bool skip(const int i) override { if (m_reader) return m_reader->skip(i); return false; }
51
52 /// @brief Load event from file
53 ///
54 /// @param[out] evt Event to be filled
55 bool read_event(GenEvent& evt) override { if (m_reader) return m_reader->read_event(evt); return false; }
56
57
58 /// @brief Return status of the stream
59 bool failed() override { if (m_reader) return m_reader->failed(); return false; }
60
61
62 /// @brief Close file stream
63 void close() override {
64 if (m_reader) return m_reader->close();
65 if(dynamic_pointer_cast<ifstream>(m_zstr)) dynamic_pointer_cast<ifstream>(m_zstr)->close();
66 }
67
68 /// Set the act reader's GenRunInfo object.
69 void set_run_info(std::shared_ptr<GenRunInfo> run) override { if (m_reader) m_reader->set_run_info(run); }
70
71 /// Get the act reader's GenRunInfo object.
72 std::shared_ptr<GenRunInfo> run_info() const override { return m_reader?m_reader->run_info():nullptr; }
73
74 /// Return reader
75 std::shared_ptr<Reader> writer() { return m_reader;}
76
77private:
78 ///@brief Close file stream
79 std::shared_ptr< std::istream > m_zstr = nullptr; ///< Stream to read
80 std::shared_ptr<Reader> m_reader = nullptr; ///< Actual reader
81
82};
83
84} // namespace HepMC3
85#endif
CompressedIO.h
HepMC3 interface to bxzstr library and some routines.
GenEvent.h
Definition of class GenEvent.
Reader.h
Definition of interface Reader.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::ReaderGZ::ReaderGZ
ReaderGZ(std::shared_ptr< std::istream > s_stream)
The ctor to read from shared pointer to stream.
Definition ReaderGZ.h:41
HepMC3::ReaderGZ::read_event
bool read_event(GenEvent &evt) override
Load event from file.
Definition ReaderGZ.h:55
HepMC3::ReaderGZ::set_run_info
void set_run_info(std::shared_ptr< GenRunInfo > run) override
Set the act reader's GenRunInfo object.
Definition ReaderGZ.h:69
HepMC3::ReaderGZ::failed
bool failed() override
Return status of the stream.
Definition ReaderGZ.h:59
HepMC3::ReaderGZ::~ReaderGZ
~ReaderGZ()
Destructor.
Definition ReaderGZ.h:47
HepMC3::ReaderGZ::m_reader
std::shared_ptr< Reader > m_reader
Actual reader.
Definition ReaderGZ.h:80
HepMC3::ReaderGZ::close
void close() override
Close file stream.
Definition ReaderGZ.h:63
HepMC3::ReaderGZ::writer
std::shared_ptr< Reader > writer()
Return reader.
Definition ReaderGZ.h:75
HepMC3::ReaderGZ::skip
bool skip(const int i) override
skip events
Definition ReaderGZ.h:50
HepMC3::ReaderGZ::ReaderGZ
ReaderGZ(const std::string &filename)
Constructor.
Definition ReaderGZ.h:31
HepMC3::ReaderGZ::ReaderGZ
ReaderGZ(std::istream &is)
The ctor to read from stdin.
Definition ReaderGZ.h:36
HepMC3::ReaderGZ::m_zstr
std::shared_ptr< std::istream > m_zstr
Close file stream.
Definition ReaderGZ.h:79
HepMC3::ReaderGZ::run_info
std::shared_ptr< GenRunInfo > run_info() const override
Get the act reader's GenRunInfo object.
Definition ReaderGZ.h:72
HepMC3::Reader::Reader
Reader()
Constructor.
Definition Reader.h:28
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::istream
bxz::istream istream
istream
Definition CompressedIO.h:36
HepMC3::ifstream
bxz::ifstream ifstream
ifstream
Definition CompressedIO.h:35