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
32class Writerprotobuf : public Writer {
33 //
34 // Constructors
35 //
36public:
37 /** @brief New file constructor
38 * @warning If file exists, it will be overwritten
39 */
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 */
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
85
86 /** @brief Write non-event front matter to the output stream. */
87 void start_file();
88
89 /** @brief The output file stream
90 *
91 * @details This is non-null and owned by this class if the instance was
92 * constructed with the string constructor, it is null otherwise.
93 */
94 std::unique_ptr<std::ofstream> m_out_file;
95 /** @brief The stream object that is written to
96 *
97 * @details If constructed with the string constructor, this just points to
98 * m_out_file.get())
99 */
100 std::ostream *m_out_stream;
101
102 /** @brief The number of events written to the stream */
104 /** @brief The number of event bytes written to the stream */
106};
107
108} // namespace HepMC3
109
110#endif
Definition of class GenEvent.
Definition of interface Writer.
Stores event-related information.
Definition GenEvent.h:41
Base class for all I/O writers.
Definition Writer.h:25
GenEvent I/O serialization for protobuf-based binary files.
bool failed() override
Get stream error state flag.
void close() override
Close file stream.
std::unique_ptr< std::ofstream > m_out_file
The output file stream.
virtual ~Writerprotobuf()
Standard destructor.
void start_file()
Write non-event front matter to the output stream.
void write_event(const GenEvent &evt) override
Write event to file.
void write_run_info()
Write the GenRunInfo object to file.
size_t m_events_written
The number of events written to the stream.
std::ostream * m_out_stream
The stream object that is written to.
size_t m_event_bytes_written
The number of event bytes written to the stream.
HepMC3 main namespace.