14#include "HepMC3/Version.h"
35 : m_msg_type(HepMC3_pb::MessageDigest::unknown) {
39 m_in_file = std::unique_ptr<std::ifstream>(
40 new std::ifstream(filename, ios::in | ios::binary));
42 if (!m_in_file->is_open()) {
43 HEPMC3_ERROR(
"Readerprotobuf: Problem opening file: " << filename)
47 m_in_stream = m_in_file.get();
53 : m_msg_type(HepMC3_pb::MessageDigest::unknown) {
57 "Cannot initialize Readerprotobuf on istream which is not good().");
73 std::string MagicIntro;
80 <<
" bytes, is this really "
81 "a HepMC3::Protobuf file?");
86 HEPMC3_ERROR(
"Readerprotobuf: Problem parsing start of file, expected to "
87 "find Header, but instead found message type: "
93 HEPMC3_ERROR(
"Readerprotobuf: Problem parsing start of file, expected to "
94 "find RunInfo, but instead found message type: "
112 m_msg_type = HepMC3_pb::MessageDigest::unknown;
122 HepMC3_pb::MessageDigest md;
139 HepMC3_pb::MessageDigest::Footer) {
152 if (
m_msg_type != HepMC3_pb::MessageDigest::Header) {
156 HepMC3_pb::Header Header_pb;
169 m_file_header.m_protobuf_version_maj = Header_pb.protobuf_version_maj();
170 m_file_header.m_protobuf_version_min = Header_pb.protobuf_version_min();
171 m_file_header.m_protobuf_version_patch = Header_pb.protobuf_version_patch();
181 if (
m_msg_type != HepMC3_pb::MessageDigest::RunInfo) {
187 HepMC3_pb::GenRunInfoData GenRunInfo_pb;
200 vector_size = GenRunInfo_pb.weight_names_size();
201 for (
int it = 0; it < vector_size; ++it) {
202 gridata.
weight_names.push_back(GenRunInfo_pb.weight_names(it));
205 vector_size = GenRunInfo_pb.tool_name_size();
206 for (
int it = 0; it < vector_size; ++it) {
207 gridata.
tool_name.push_back(GenRunInfo_pb.tool_name(it));
210 vector_size = GenRunInfo_pb.tool_version_size();
211 for (
int it = 0; it < vector_size; ++it) {
212 gridata.
tool_version.push_back(GenRunInfo_pb.tool_version(it));
215 vector_size = GenRunInfo_pb.tool_description_size();
216 for (
int it = 0; it < vector_size; ++it) {
220 vector_size = GenRunInfo_pb.attribute_name_size();
221 for (
int it = 0; it < vector_size; ++it) {
222 gridata.
attribute_name.push_back(GenRunInfo_pb.attribute_name(it));
225 vector_size = GenRunInfo_pb.attribute_string_size();
226 for (
int it = 0; it < vector_size; ++it) {
239 if (
m_msg_type != HepMC3_pb::MessageDigest::Event) {
253 HepMC3_pb::GenEventData ged_pb;
263 switch (ged_pb.momentum_unit()) {
264 case HepMC3_pb::GenEventData::MEV: {
268 case HepMC3_pb::GenEventData::GEV: {
273 HEPMC3_ERROR(
"Unknown momentum unit: " << ged_pb.momentum_unit());
278 switch (ged_pb.length_unit()) {
279 case HepMC3_pb::GenEventData::MM: {
283 case HepMC3_pb::GenEventData::CM: {
288 HEPMC3_ERROR(
"Unknown length unit: " << ged_pb.length_unit());
296 vector_size = ged_pb.particles_size();
297 for (
int it = 0; it < vector_size; ++it) {
298 auto particle_pb = ged_pb.particles(it);
302 pdata.
pid = particle_pb.pid();
303 pdata.
status = particle_pb.status();
305 pdata.
mass = particle_pb.mass();
308 particle_pb.momentum().m_v1(), particle_pb.momentum().m_v2(),
309 particle_pb.momentum().m_v3(), particle_pb.momentum().m_v4()};
315 vector_size = ged_pb.vertices_size();
316 for (
int it = 0; it < vector_size; ++it) {
317 auto vertex_pb = ged_pb.vertices(it);
321 vdata.
status = vertex_pb.status();
324 vertex_pb.position().m_v1(), vertex_pb.position().m_v2(),
325 vertex_pb.position().m_v3(), vertex_pb.position().m_v4()};
331 vector_size = ged_pb.weights_size();
332 for (
int it = 0; it < vector_size; ++it) {
337 vector_size = ged_pb.links1_size();
338 for (
int it = 0; it < vector_size; ++it) {
343 vector_size = ged_pb.links2_size();
344 for (
int it = 0; it < vector_size; ++it) {
350 ged_pb.event_pos().m_v3(), ged_pb.event_pos().m_v4()};
353 vector_size = ged_pb.attribute_id_size();
354 for (
int it = 0; it < vector_size; ++it) {
359 vector_size = ged_pb.attribute_name_size();
360 for (
int it = 0; it < vector_size; ++it) {
365 vector_size = ged_pb.attribute_string_size();
366 for (
int it = 0; it < vector_size; ++it) {
376 for (
int nn = n; nn > 0; --nn) {
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition of struct GenRunInfoData.
Definition of static class Print.
Definition of class Readerprotobuf.
Stores event-related information.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
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.
size_t m_bytes_read
The total number of event bytes read, including message frames.
static size_t const MDBytesLength
Constant.
size_t const ProtobufMagicHeaderBytes
Size of the protobuf file header.
std::string const ProtobufMagicHeader
Header of the protobuf file.
Stores serializable event information.
std::vector< GenVertexData > vertices
Vertices.
std::vector< int > links2
Second id of the vertex links.
int event_number
Event number.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< GenParticleData > particles
Particles.
std::vector< int > links1
First id of the vertex links.
std::vector< std::string > attribute_name
Attribute name.
Units::LengthUnit length_unit
Length unit.
std::vector< int > attribute_id
Attribute owner id.
FourVector event_pos
Event position.
std::vector< double > weights
Weights.
Units::MomentumUnit momentum_unit
Momentum unit.
Stores serializable particle information.
double mass
Generated mass (if set)
FourVector momentum
Momentum.
bool is_mass_set
Check if generated mass is set.
Stores serializable run information.
std::vector< std::string > tool_name
Tool names.
std::vector< std::string > tool_version
Tool versions.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< std::string > attribute_name
Attribute name.
std::vector< std::string > tool_description
Tool descriptions.
std::vector< std::string > weight_names
Weight names.
Stores serializable vertex information.
FourVector position
Position in time-space.