HepMC3 event record library: Readerprotobuf.h Source File

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
40 class 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
GenEvent.h
Definition of class GenEvent.
Reader.h
Definition of interface Reader.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::Reader::Reader
Reader()
Constructor.
Definition Reader.h:28
HepMC3::Readerprotobuf::m_gri_pb
HepMC3_pb::GenRunInfoData m_gri_pb
GenRunInfo data.
Definition Readerprotobuf.h:146
HepMC3::Readerprotobuf::read_event
bool read_event(GenEvent &evt) override
Read event from file.
Definition Readerprotobuf.cc:203
HepMC3::Readerprotobuf::m_in_stream
std::istream * m_in_stream
The stream object that is read from.
Definition Readerprotobuf.h:138
HepMC3::Readerprotobuf::failed
bool failed() override
Get stream error state.
Definition Readerprotobuf.cc:224
HepMC3::Readerprotobuf::Readerprotobuf
Readerprotobuf(const std::string &filename)
filename constructor
Definition Readerprotobuf.cc:37
HepMC3::Readerprotobuf::m_md_pb
HepMC3_pb::MessageDigest m_md_pb
Message digest.
Definition Readerprotobuf.h:144
HepMC3::Readerprotobuf::skip
bool skip(const int n) override
skips the next n events
Definition Readerprotobuf.cc:193
HepMC3::Readerprotobuf::m_hdr_pb
HepMC3_pb::Header m_hdr_pb
Header.
Definition Readerprotobuf.h:145
HepMC3::Readerprotobuf::close
void close() override
Close file stream.
Definition Readerprotobuf.cc:214
HepMC3::Readerprotobuf::read_GenEvent
bool read_GenEvent(bool skip, GenEvent &evt)
Parse the next protobuf message as a GenEvent message.
Definition Readerprotobuf.cc:173
HepMC3::Readerprotobuf::m_evt_pb
HepMC3_pb::GenEventData m_evt_pb
GenEventInfo data.
Definition Readerprotobuf.h:147
HepMC3::Readerprotobuf::m_in_zcistream
std::unique_ptr< google::protobuf::io::IstreamInputStream > m_in_zcistream
Stream input.
Definition Readerprotobuf.h:141
HepMC3::Readerprotobuf::read_digest
bool read_digest()
Read the next protobuf message digest.
Definition Readerprotobuf.cc:110
HepMC3::Readerprotobuf::read_GenRunInfo
bool read_GenRunInfo()
Parse the next protobuf message as a GenRunInfo message.
Definition Readerprotobuf.cc:151
HepMC3::Readerprotobuf::m_inf_zcstream
std::unique_ptr< google::protobuf::io::FileInputStream > m_inf_zcstream
File input.
Definition Readerprotobuf.h:140
HepMC3::Readerprotobuf::read_Header
bool read_Header()
Parse the next protobuf message as a Header message.
Definition Readerprotobuf.cc:131
HepMC3::Readerprotobuf::read_file_start
bool read_file_start()
Parse the front matter of the protobuf message stream before the events.
Definition Readerprotobuf.cc:81
HepMC3::Readerprotobuf::m_shared_stream
std::shared_ptr< std::istream > m_shared_stream
Passed in shared_ptr to an input stream.
Definition Readerprotobuf.h:132
HepMC3::Readerprotobuf::m_in_zcstream
google::protobuf::io::ZeroCopyInputStream * m_in_zcstream
Zero copy input stream.
Definition Readerprotobuf.h:142
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24