Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:14

0001 // -*- C++ -*-
0002 //
0003 // This file is part of HepMC
0004 // Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
0005 //
0006 #ifndef HEPMC3_READERGZ_H
0007 #define HEPMC3_READERGZ_H
0008 ///
0009 /// @file  ReaderGZ.h
0010 /// @brief Definition of class \b ReaderGZ
0011 ///
0012 /// @class HepMC3::ReaderGZ
0013 /// @brief GenEvent I/O parsing for compressed files
0014 ///
0015 /// @ingroup IO
0016 ///
0017 #include <set>
0018 #include <string>
0019 #include <fstream>
0020 #include <istream>
0021 #include <iterator>
0022 #include "HepMC3/Reader.h"
0023 #include "HepMC3/GenEvent.h"
0024 #include "HepMC3/CompressedIO.h"
0025 namespace HepMC3 {
0026 
0027 template <class T> class ReaderGZ : public Reader {
0028 public:
0029 
0030     /// @brief Constructor
0031     ReaderGZ(const std::string& filename) {
0032         m_zstr = std::shared_ptr< std::istream >(new ifstream(filename.c_str()));
0033         m_reader = std::make_shared<T>(*(m_zstr.get()));
0034     }
0035     /// @brief The ctor to read from stdin
0036     ReaderGZ(std::istream & is) {
0037         m_zstr = std::shared_ptr< std::istream >(new istream(is));
0038         m_reader = std::make_shared<T>(*(m_zstr.get()));
0039     }
0040     /// @brief The ctor to read from shared pointer to stream
0041     ReaderGZ(std::shared_ptr<std::istream> s_stream) {
0042         m_zstr = s_stream;
0043         m_reader = std::make_shared<T>(*(m_zstr.get()));
0044     }
0045 
0046     /// @brief Destructor
0047     ~ReaderGZ() { close(); }
0048 
0049     /// @brief skip events
0050     bool skip(const int i) override { if (m_reader) return m_reader->skip(i); return false; }
0051 
0052     /// @brief Load event from file
0053     ///
0054     /// @param[out] evt Event to be filled
0055     bool read_event(GenEvent& evt) override { if (m_reader) return m_reader->read_event(evt); return false; }
0056 
0057 
0058     /// @brief Return status of the stream
0059     bool failed() override { if (m_reader) return m_reader->failed(); return false; }
0060 
0061 
0062     /// @brief Close file stream
0063     void close() override {
0064         if (m_reader) return m_reader->close();
0065         if(dynamic_pointer_cast<ifstream>(m_zstr)) dynamic_pointer_cast<ifstream>(m_zstr)->close();
0066     }
0067 
0068     /// Set the act reader's GenRunInfo object.
0069     void set_run_info(std::shared_ptr<GenRunInfo> run) override { if (m_reader) m_reader->set_run_info(run); }
0070 
0071     /// Get the act reader's GenRunInfo object.
0072     std::shared_ptr<GenRunInfo> run_info() const override { return m_reader?m_reader->run_info():nullptr; }
0073 
0074     /// Return reader
0075     std::shared_ptr<Reader> writer() { return m_reader;}
0076 
0077 private:
0078     ///@brief Close file stream
0079     std::shared_ptr< std::istream > m_zstr = nullptr;  ///< Stream to read
0080     std::shared_ptr<Reader> m_reader = nullptr; ///< Actual reader
0081 
0082 };
0083 
0084 } // namespace HepMC3
0085 #endif