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-2020 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
25template <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 Destructor
43
44 /// @brief Write event to file
45 ///
46 /// @param[in] evt Event to be serialized
47 void write_event(const GenEvent& evt) override { if (m_writer) m_writer->write_event(evt); };
48
49 /// @brief Return status of the stream
50 bool failed() override { if (m_writer) return m_writer->failed(); return true; };
51
52 /// @brief Close file stream
53 void close() override {
54 if (m_writer) m_writer->close();
55 m_zstr->flush();
56 if(dynamic_pointer_cast<ofstream>(m_zstr)) dynamic_pointer_cast<ofstream>(m_zstr)->close();
57 }
58
59private:
60 std::shared_ptr< std::ostream > m_zstr; ///< Stream to write
61 std::shared_ptr<Writer> m_writer; //!< actual writter
62
63};
64
65} // namespace HepMC3
66#endif
Definition of class GenEvent.
Definition of class GenRunInfo.
Definition of interface Writer.
Stores event-related information.
Definition: GenEvent.h:41
GenEvent I/O serialization for compressed files.
Definition: WriterGZ.h:25
std::shared_ptr< Writer > m_writer
actual writter
Definition: WriterGZ.h:61
bool failed() override
Return status of the stream.
Definition: WriterGZ.h:50
~WriterGZ()
Destructor.
Definition: WriterGZ.h:42
WriterGZ(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
Definition: WriterGZ.h:30
void close() override
Close file stream.
Definition: WriterGZ.h:53
WriterGZ(std::ostream &stream, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor from ostream.
Definition: WriterGZ.h:36
std::shared_ptr< std::ostream > m_zstr
Stream to write.
Definition: WriterGZ.h:60
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterGZ.h:47
Base class for all I/O writers.
Definition: Writer.h:25
HepMC3 main namespace.