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
28// protobuf header files
29#include "HepMC3/HepMC3.pb.h"
30
31#include "google/protobuf/io/zero_copy_stream_impl.h"
32
33#include <array>
34#include <fstream>
35#include <string>
36#include <vector>
37
38namespace HepMC3 {
39
40class Readerprotobuf : public Reader {
41public:
42
43 //
44 // Constructors
45 //
46public:
47 /** @brief filename constructor
48 *
49 * @details Attempts to open the passed filename and read protobuf HepMC3
50 * events from it
51 */
52 Readerprotobuf(const std::string &filename);
53
54 /** @brief istream constructor
55 *
56 * @details Attempts to read a binary HepMC3 protobuf event stream from the
57 * passed istream object
58 */
59 Readerprotobuf(std::istream &stream);
60
61 /** @brief istream constructor
62 *
63 * @details Attempts to read a binary HepMC3 protobuf event stream from the
64 * passed istream object
65 */
66 Readerprotobuf(std::shared_ptr<std::istream> stream);
67
68 //
69 // Functions
70 //
71public:
72 /** @brief skips the next n events
73 *
74 * @param[in] n the number of events to skip
75 * @return Whether the reader can still be read from after skipping
76 */
77 bool skip(const int n) override;
78
79 /** @brief Read event from file
80 *
81 * @param[out] evt Contains parsed event
82 * @return Whether the reader can still be read from after reading
83 */
84 bool read_event(GenEvent &evt) override;
85
86 /** @brief Close file stream */
87 void close() override;
88
89 /** @brief Get stream error state */
90 bool failed() override;
91 //
92 // Fields
93 //
94private:
95 /** @brief Read the next protobuf message digest
96 *
97 * @details Determines the type and byte length of the next payload
98 */
99 bool read_digest();
100
101 /** @brief Parse the next protobuf message as a GenRunInfo message
102 *
103 * @return Whether the reader can still be read from after reading
104 */
105 bool read_GenRunInfo();
106
107 /** @brief Parse the next protobuf message as a GenEvent message
108 *
109 * @param[in] skip Whether to bother actually parsing this message to a
110 * GenEvent
111 * @param[out] evt output GenEvent
112 * @return Whether the reader can still be read from after reading
113 */
114 bool read_GenEvent(bool skip, GenEvent & evt);
115
116 /** @brief Parse the next protobuf message as a Header message
117 *
118 * @return Whether the reader can still be read from after reading
119 */
120 bool read_Header();
121
122 /** @brief Parse the front matter of the protobuf message stream before the
123 * events
124 */
125 bool read_file_start();
126
127 /** @brief Passed in shared_ptr to an input stream
128 *
129 * @details This is non-null and shared by this class if constructed with the
130 * stream constructor
131 */
132 std::shared_ptr<std::istream> m_shared_stream;
133 /** @brief The stream object that is read from
134 *
135 * @details If constructed with either stream constructor this lets us check
136 * we can use this to check stream status
137 */
138 std::istream *m_in_stream = nullptr;
139
140 std::unique_ptr<google::protobuf::io::FileInputStream> m_inf_zcstream; //!< File input
141 std::unique_ptr<google::protobuf::io::IstreamInputStream> m_in_zcistream; //!< Stream input
142 google::protobuf::io::ZeroCopyInputStream *m_in_zcstream = nullptr; //!< Zero copy input stream
143
144 HepMC3_pb::MessageDigest m_md_pb; //!< Message digest
145 HepMC3_pb::Header m_hdr_pb; //!< Header
146 HepMC3_pb::GenRunInfoData m_gri_pb; //!< GenRunInfo data
147 HepMC3_pb::GenEventData m_evt_pb; //!< GenEventInfo data
148};
149
150} // namespace HepMC3
151
152#endif
Definition of class GenEvent.
Definition of interface Reader.
Stores event-related information.
Definition GenEvent.h:47
Reader()
Constructor.
Definition Reader.h:28
HepMC3_pb::GenRunInfoData m_gri_pb
GenRunInfo data.
bool read_event(GenEvent &evt) override
Read event from file.
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
HepMC3_pb::MessageDigest m_md_pb
Message digest.
bool skip(const int n) override
skips the next n events
HepMC3_pb::Header m_hdr_pb
Header.
void close() override
Close file stream.
bool read_GenEvent(bool skip, GenEvent &evt)
Parse the next protobuf message as a GenEvent message.
HepMC3_pb::GenEventData m_evt_pb
GenEventInfo data.
std::unique_ptr< google::protobuf::io::IstreamInputStream > m_in_zcistream
Stream input.
bool read_digest()
Read the next protobuf message digest.
bool read_GenRunInfo()
Parse the next protobuf message as a GenRunInfo message.
std::unique_ptr< google::protobuf::io::FileInputStream > m_inf_zcstream
File input.
bool read_Header()
Parse the next protobuf message as a Header message.
bool read_file_start()
Parse the front matter of the protobuf message stream before the events.
std::shared_ptr< std::istream > m_shared_stream
Passed in shared_ptr to an input stream.
google::protobuf::io::ZeroCopyInputStream * m_in_zcstream
Zero copy input stream.
HepMC3 main namespace.