HepMC3 event record library: WriterGZ.h Source File

HepMC3 event record library

WriterGZ.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_WRITERGZ_H
7#define HEPMC3_WRITERGZ_H
8///
9/// @file WriterGZ.h
10/// @brief Definition of class \b WriterGZ
11///
12/// @class HepMC3::WriterGZ
13/// @brief GenEvent I/O serialization for compressed files
14///
15/// @ingroup IO
16///
17#include <string>
18#include <fstream>
19#include "HepMC3/Writer.h"
20#include "HepMC3/GenEvent.h"
21#include "HepMC3/GenRunInfo.h"
22#include "HepMC3/CompressedIO.h"
23namespace HepMC3 {
24
25 template <class T, Compression C = Compression::z> class WriterGZ : public Writer {
26public:
27
28 /// @brief Constructor
29 /// @warning If file already exists, it will be cleared before writing
30 WriterGZ(const std::string& filename, std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
31 m_zstr = std::shared_ptr< std::ostream >(new ofstream(filename.c_str(), C));
32 m_writer = std::make_shared<T>(*(m_zstr.get()), run);
33 }
34
35 /// @brief Constructor from ostream
36 WriterGZ(std::ostream& stream, std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
37 m_zstr = std::shared_ptr< std::ostream >(new ostream(stream, C));
38 m_writer = std::make_shared<T>(*(m_zstr.get()), run);
39 }
40
41 /// @brief Constructor from ostream
42 WriterGZ(std::shared_ptr<std::ostream> s_stream, std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
43 m_zstr = std::shared_ptr< std::ostream >(new ostream(*(s_stream.get()), C));
44 m_writer = std::make_shared<T>(m_zstr, run);
45 }
46
47 /// @brief Destructor
48 ~WriterGZ() {};
49
50 /// @brief Write event to file
51 ///
52 /// @param[in] evt Event to be serialized
53 void write_event(const GenEvent& evt) override { if (m_writer) m_writer->write_event(evt); };
54
55 /// @brief Return status of the stream
56 bool failed() override { if (m_writer) return m_writer->failed(); return true; };
57
58 /// @brief Close file stream
59 void close() override {
60 if (m_writer) m_writer->close();
61 m_zstr->flush();
62 if(dynamic_pointer_cast<ofstream>(m_zstr)) dynamic_pointer_cast<ofstream>(m_zstr)->close();
63 }
64
65 /// Set the act writer's GenRunInfo object.
66 void set_run_info(std::shared_ptr<GenRunInfo> run) override { if (m_writer) m_writer->set_run_info(run); }
67
68 /// Get the act writer's GenRunInfo object.
69 std::shared_ptr<GenRunInfo> run_info() const override { return m_writer?m_writer->run_info():nullptr; }
70
71 /// Return writer
72 std::shared_ptr<Writer> writer() { return m_writer;}
73
74private:
75 std::shared_ptr< std::ostream > m_zstr = nullptr; ///< Stream to write
76 std::shared_ptr<Writer> m_writer = nullptr; //!< actual writter
77
78};
79
80} // namespace HepMC3
81#endif
CompressedIO.h
HepMC3 interface to bxzstr library and some routines.
GenEvent.h
Definition of class GenEvent.
GenRunInfo.h
Definition of class GenRunInfo.
Writer.h
Definition of interface Writer.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::WriterGZ::m_writer
std::shared_ptr< Writer > m_writer
actual writter
Definition WriterGZ.h:76
HepMC3::WriterGZ::set_run_info
void set_run_info(std::shared_ptr< GenRunInfo > run) override
Set the act writer's GenRunInfo object.
Definition WriterGZ.h:66
HepMC3::WriterGZ::failed
bool failed() override
Return status of the stream.
Definition WriterGZ.h:56
HepMC3::WriterGZ::~WriterGZ
~WriterGZ()
Destructor.
Definition WriterGZ.h:48
HepMC3::WriterGZ::WriterGZ
WriterGZ(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
Definition WriterGZ.h:30
HepMC3::WriterGZ::close
void close() override
Close file stream.
Definition WriterGZ.h:59
HepMC3::WriterGZ::WriterGZ
WriterGZ(std::ostream &stream, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor from ostream.
Definition WriterGZ.h:36
HepMC3::WriterGZ::m_zstr
std::shared_ptr< std::ostream > m_zstr
Stream to write.
Definition WriterGZ.h:75
HepMC3::WriterGZ::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterGZ.h:53
HepMC3::WriterGZ::writer
std::shared_ptr< Writer > writer()
Return writer.
Definition WriterGZ.h:72
HepMC3::WriterGZ::run_info
std::shared_ptr< GenRunInfo > run_info() const override
Get the act writer's GenRunInfo object.
Definition WriterGZ.h:69
HepMC3::WriterGZ::WriterGZ
WriterGZ(std::shared_ptr< std::ostream > s_stream, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor from ostream.
Definition WriterGZ.h:42
HepMC3::Writer::Writer
Writer()
Constructor.
Definition Writer.h:29
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::ostream
bxz::ostream ostream
ostream
Definition CompressedIO.h:34
HepMC3::ofstream
bxz::ofstream ofstream
ofstream
Definition CompressedIO.h:33