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_WRITERGZ_H
0007 #define HEPMC3_WRITERGZ_H
0008 ///
0009 /// @file  WriterGZ.h
0010 /// @brief Definition of class \b WriterGZ
0011 ///
0012 /// @class HepMC3::WriterGZ
0013 /// @brief GenEvent I/O serialization for compressed files
0014 ///
0015 /// @ingroup IO
0016 ///
0017 #include <string>
0018 #include <fstream>
0019 #include "HepMC3/Writer.h"
0020 #include "HepMC3/GenEvent.h"
0021 #include "HepMC3/GenRunInfo.h"
0022 #include "HepMC3/CompressedIO.h"
0023 namespace HepMC3 {
0024 
0025 template <class T, Compression C = Compression::z> class WriterGZ : public Writer  {
0026 public:
0027 
0028     /// @brief Constructor
0029     /// @warning If file already exists, it will be cleared before writing
0030     WriterGZ(const std::string& filename, std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
0031         m_zstr = std::shared_ptr< std::ostream >(new ofstream(filename.c_str(), C));
0032         m_writer = std::make_shared<T>(*(m_zstr.get()), run);
0033     }
0034 
0035     /// @brief Constructor from ostream
0036     WriterGZ(std::ostream& stream, std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
0037         m_zstr = std::shared_ptr< std::ostream >(new ostream(stream, C));
0038         m_writer = std::make_shared<T>(*(m_zstr.get()), run);
0039     }
0040 
0041     /// @brief Constructor from ostream
0042     WriterGZ(std::shared_ptr<std::ostream> s_stream, std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
0043         m_zstr = std::shared_ptr< std::ostream >(new ostream(*(s_stream.get()), C));
0044         m_writer = std::make_shared<T>(m_zstr, run);
0045     }
0046 
0047     /// @brief Destructor
0048     ~WriterGZ() {};
0049 
0050     /// @brief Write event to file
0051     ///
0052     /// @param[in] evt Event to be serialized
0053     void write_event(const GenEvent& evt) override { if (m_writer) m_writer->write_event(evt); };
0054 
0055     /// @brief Return status of the stream
0056     bool failed() override { if (m_writer) return  m_writer->failed(); return true; };
0057 
0058     /// @brief Close file stream
0059     void close() override {
0060         if (m_writer)  m_writer->close();
0061         m_zstr->flush();
0062         if(dynamic_pointer_cast<ofstream>(m_zstr)) dynamic_pointer_cast<ofstream>(m_zstr)->close();
0063     }
0064 
0065     /// Set the act writer's GenRunInfo object.
0066     void set_run_info(std::shared_ptr<GenRunInfo> run) override {  if (m_writer) m_writer->set_run_info(run); }
0067 
0068     /// Get the act writer's GenRunInfo object.
0069     std::shared_ptr<GenRunInfo> run_info() const override { return m_writer?m_writer->run_info():nullptr; }
0070 
0071     /// Return writer
0072     std::shared_ptr<Writer> writer() { return m_writer;}
0073 
0074 private:
0075     std::shared_ptr< std::ostream > m_zstr = nullptr;  ///< Stream to write
0076     std::shared_ptr<Writer> m_writer = nullptr; //!< actual writter
0077 
0078 };
0079 
0080 } // namespace HepMC3
0081 #endif