File indexing completed on 2025-01-18 10:01:14
0001
0002
0003
0004
0005
0006 #ifndef HEPMC3_WRITERGZ_H
0007 #define HEPMC3_WRITERGZ_H
0008
0009
0010
0011
0012
0013
0014
0015
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
0029
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
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
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
0048 ~WriterGZ() {};
0049
0050
0051
0052
0053 void write_event(const GenEvent& evt) override { if (m_writer) m_writer->write_event(evt); };
0054
0055
0056 bool failed() override { if (m_writer) return m_writer->failed(); return true; };
0057
0058
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
0066 void set_run_info(std::shared_ptr<GenRunInfo> run) override { if (m_writer) m_writer->set_run_info(run); }
0067
0068
0069 std::shared_ptr<GenRunInfo> run_info() const override { return m_writer?m_writer->run_info():nullptr; }
0070
0071
0072 std::shared_ptr<Writer> writer() { return m_writer;}
0073
0074 private:
0075 std::shared_ptr< std::ostream > m_zstr = nullptr;
0076 std::shared_ptr<Writer> m_writer = nullptr;
0077
0078 };
0079
0080 }
0081 #endif