HepMC3 event record library
Readerprotobuf.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_READERPROTOBUF_H
7#define HEPMC3_READERPROTOBUF_H
8/**
9 * @file Readerprotobuf.h
10 * @brief Definition of \b class Readerprotobuf
11 *
12 * @class HepMC3::Readerprotobuf
13 * @brief GenEvent I/O parsing and serialization for protobuf-based binary
14 * files
15 *
16 * If HepMC was compiled with path to protobuf available, this class can be
17 * used for protobuf file I/O in the same manner as with HepMC::ReaderAscii
18 * class.
19 *
20 * @ingroup IO
21 *
22 */
23
24#include "HepMC3/Reader.h"
25
26#include "HepMC3/GenEvent.h"
27
29
30#include <array>
31#include <fstream>
32#include <string>
33#include <vector>
34
35namespace HepMC3 {
36
37class Readerprotobuf : public Reader {
38public:
39 /**
40 * @class HepMC3::Readerprotobuf::FileHeader
41 * @brief A copy of the information contained in the protobuf file header
42 */
43 struct FileHeader {
44 std::string m_version_str;
45 unsigned int m_version_maj;
46 unsigned int m_version_min;
47 unsigned int m_version_patch;
48
49 unsigned int m_protobuf_version_maj;
50 unsigned int m_protobuf_version_min;
51 unsigned int m_protobuf_version_patch;
52 };
53
54 //
55 // Constructors
56 //
57public:
58 /** @brief filename constructor
59 *
60 * @details Attempts to open the passed filename and read protobuf HepMC3
61 * events from it
62 */
63 Readerprotobuf(const std::string &filename);
64
65 /** @brief istream constructor
66 *
67 * @details Attempts to read a binary HepMC3 protobuf event stream from the
68 * passed istream object
69 */
70 Readerprotobuf(std::istream &stream);
71
72 /** @brief istream constructor
73 *
74 * @details Attempts to read a binary HepMC3 protobuf event stream from the
75 * passed istream object
76 */
77 Readerprotobuf(std::shared_ptr<std::istream> stream);
78
79 //
80 // Functions
81 //
82public:
83 /** @brief skips the next N events
84 *
85 * @param[in] the number of events to skip
86 * @return Whether the reader can still be read from after skipping
87 */
88 bool skip(const int the) override;
89
90 /** @brief Read event from file
91 *
92 * @param[out] evt Contains parsed event
93 * @return Whether the reader can still be read from after reading
94 */
95 bool read_event(GenEvent &evt) override;
96
97 /** @brief Close file stream */
98 void close() override;
99
100 /** @brief Get the header information read from the protobuf file */
102
103 /** @brief Get stream error state */
104 bool failed() override;
105 //
106 // Fields
107 //
108private:
109 /** @brief Read the next protobuf message into the message buffer
110 *
111 * @details Fills m_msg_buffer with the next message and sets m_msg_type to
112 * signify the message type. Returns true if there is a message in the buffer
113 * ready to parse.
114 */
115 bool buffer_message();
116
117 /** @brief Parse the next protobuf message as a GenRunInfo message
118 *
119 * @return Whether the reader can still be read from after reading
120 */
121 bool read_GenRunInfo();
122
123 /** @brief Parse the next protobuf message as a GenEvent message
124 *
125 * @param[in] skip Whether to bother actually parsing this message to a
126 * GenEvent
127 * @return Whether the reader can still be read from after reading
128 */
129 bool read_GenEvent(bool skip = false);
130
131 /** @brief Parse the next protobuf message as a Header message
132 *
133 * @return Whether the reader can still be read from after reading
134 */
135 bool read_Header();
136
137 /** @brief Parse the front matter of the protobuf message stream before the
138 * events
139 */
140 bool read_file_start();
141
142 /** @brief The total number of event bytes read, including message frames
143 */
144 size_t m_bytes_read = 0;
145
146 /** @brief The file stream of the file being read
147 *
148 * @details This is non-null and owned by this class if constructed with the
149 * string constructor, otherwise it will be null
150 */
151 std::unique_ptr<std::ifstream> m_in_file;
152 /** @brief The stream object that is read from
153 *
154 * @details If constructed with the string constructor, this just points to
155 * m_in_file.get())
156 */
157 std::istream *m_in_stream = nullptr;
158
159 /** @brief The buffer used to hold the current message binary
160 * (header/genruninfo/genevent/footer)
161 */
162 std::string m_msg_buffer;
163 /** @brief The buffer used to hold the current message digest binary
164 * (message frame)
165 */
166 std::string m_md_buffer;
167 /** @brief The type of current message
168 *
169 * @details Defined in HepMC3_pb::MessageDigest::MessageType in the proto file
170 */
172
173 /** @brief The event data parsed from the message
174 */
176
177 /** @brief A copy of the library version info stored in the proto file header
178 *
179 * @details This is a copy so as to avoid passing on protobuf header
180 * dependencies to files that include this header
181 */
183};
184
185} // namespace HepMC3
186
187#endif
Definition of struct GenEventData.
Definition of class GenEvent.
Definition of interface Reader.
Stores event-related information.
Definition GenEvent.h:41
Base class for all I/O readers.
Definition Reader.h:25
GenEvent I/O parsing and serialization for protobuf-based binary files.
bool read_event(GenEvent &evt) override
Read event from file.
std::string m_msg_buffer
The buffer used to hold the current message binary (header/genruninfo/genevent/footer)
std::istream * m_in_stream
The stream object that is read from.
bool failed() override
Get stream error state.
Readerprotobuf(const std::string &filename)
filename constructor
bool skip(const int the) override
skips the next N events
HepMC3::GenEventData m_evdata
The event data parsed from the message.
FileHeader m_file_header
A copy of the library version info stored in the proto file header.
void close() override
Close file stream.
bool buffer_message()
Read the next protobuf message into the message buffer.
bool read_GenRunInfo()
Parse the next protobuf message as a GenRunInfo message.
int m_msg_type
The type of current message.
std::unique_ptr< std::ifstream > m_in_file
The file stream of the file being read.
bool read_Header()
Parse the next protobuf message as a Header message.
std::string m_md_buffer
The buffer used to hold the current message digest binary (message frame)
bool read_GenEvent(bool skip=false)
Parse the next protobuf message as a GenEvent message.
bool read_file_start()
Parse the front matter of the protobuf message stream before the events.
FileHeader const & file_header()
Get the header information read from the protobuf file.
size_t m_bytes_read
The total number of event bytes read, including message frames.
HepMC3 main namespace.
Stores serializable event information.
A copy of the information contained in the protobuf file header.