HepMC3 event record library: Writerprotobuf.h Source File

HepMC3 event record library

Writerprotobuf.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_WRITERPROTOBUF_H
7#define HEPMC3_WRITERPROTOBUF_H
8/**
9 * @file Writerprotobuf.h
10 * @brief Definition of \b class Writerprotobuf
11 *
12 * @class HepMC3::Writerprotobuf
13 * @brief GenEvent I/O serialization for protobuf-based binary files
14 *
15 * If HepMC was compiled with protobuf available, this class can be used
16 * for writing in the same manner as with HepMC::WriterAscii class.
17 *
18 * @ingroup IO
19 *
20 */
21
22#include "HepMC3/Writer.h"
23
24#include "HepMC3/GenEvent.h"
25
26#include <fstream>
27#include <memory>
28#include <string>
29
30namespace HepMC3 {
31
32 class Writerprotobuf : public Writer {
33 //
34 // Constructors
35 //
36public:
37 /** @brief New file constructor
38 * @warning If file exists, it will be overwritten
39 */
40 Writerprotobuf(
41 const std::string &filename,
42 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
43
44 /** @brief ostream constructor
45 *
46 * @details Attempts to write a binary HepMC3 protobuf event stream to the
47 * passed ostream object
48 */
49 Writerprotobuf(std::ostream &out_stream, std::shared_ptr<GenRunInfo> run =
50 std::shared_ptr<GenRunInfo>());
51
52 /** @brief ostream constructor
53 *
54 * @details Attempts to write a binary HepMC3 protobuf event stream to the
55 * passed ostream object
56 */
57 Writerprotobuf(
58 std::shared_ptr<std::ostream> out_stream,
59 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
60
61 //
62 // Functions
63 //
64public:
65 /** @brief Write event to file
66 *
67 * @param[in] evt Event to be serialized
68 */
69 void write_event(const GenEvent &evt) override;
70
71 /** @brief Close file stream */
72 void close() override;
73
74 /** @brief Get stream error state flag */
75 bool failed() override;
76
77 /** @brief Standard destructor */
78 virtual ~Writerprotobuf() { close(); }
79
80 /** @brief Write the GenRunInfo object to file. */
81 void write_run_info();
82
83private:
84 /** @brief Write non-event front matter to the output stream. */
85 void start_file();
86
87 /** @brief The output file stream
88 *
89 * @details This is non-null and owned by this class if the instance was
90 * constructed with the string constructor, it is null otherwise.
91 */
92 std::unique_ptr<std::ofstream> m_out_file;
93 /** @brief Passed in shared_ptr to an output stream
94 *
95 * @details This is non-null and shared by this class if constructed with the
96 * stream constructor
97 */
98 std::shared_ptr<std::ostream> m_shared_stream;
99 /** @brief The stream object that is written to
100 *
101 * @details If constructed with the string constructor, this just points to
102 * m_out_file.get())
103 */
104 std::ostream *m_out_stream;
105
106 /** @brief The number of events written to the stream */
107 size_t m_events_written;
108 /** @brief The number of event bytes written to the stream */
109 size_t m_event_bytes_written;
110};
111
112} // namespace HepMC3
113
114#endif
GenEvent.h
Definition of class GenEvent.
Writer.h
Definition of interface Writer.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::Writer::Writer
Writer()
Constructor.
Definition Writer.h:29
HepMC3::Writerprotobuf::Writerprotobuf
Writerprotobuf(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
New file constructor.
Definition Writerprotobuf.cc:51
HepMC3::Writerprotobuf::failed
bool failed() override
Get stream error state flag.
Definition Writerprotobuf.cc:156
HepMC3::Writerprotobuf::close
void close() override
Close file stream.
Definition Writerprotobuf.cc:132
HepMC3::Writerprotobuf::m_out_file
std::unique_ptr< std::ofstream > m_out_file
The output file stream.
Definition Writerprotobuf.h:92
HepMC3::Writerprotobuf::~Writerprotobuf
virtual ~Writerprotobuf()
Standard destructor.
Definition Writerprotobuf.h:78
HepMC3::Writerprotobuf::start_file
void start_file()
Write non-event front matter to the output stream.
Definition Writerprotobuf.cc:98
HepMC3::Writerprotobuf::m_shared_stream
std::shared_ptr< std::ostream > m_shared_stream
Passed in shared_ptr to an output stream.
Definition Writerprotobuf.h:98
HepMC3::Writerprotobuf::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition Writerprotobuf.cc:121
HepMC3::Writerprotobuf::write_run_info
void write_run_info()
Write the GenRunInfo object to file.
Definition Writerprotobuf.cc:127
HepMC3::Writerprotobuf::m_events_written
size_t m_events_written
The number of events written to the stream.
Definition Writerprotobuf.h:107
HepMC3::Writerprotobuf::m_out_stream
std::ostream * m_out_stream
The stream object that is written to.
Definition Writerprotobuf.h:104
HepMC3::Writerprotobuf::m_event_bytes_written
size_t m_event_bytes_written
The number of event bytes written to the stream.
Definition Writerprotobuf.h:109
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24