18#include "HepMC3/HepMC3.pb.h"
32size_t write_message(std::ostream *out_stream, std::
string const &msg_str,
33 HepMC3_pb::MessageDigest::MessageType type) {
35 HepMC3_pb::MessageDigest md;
36 md.set_bytes(msg_str.size());
37 md.set_message_type(type);
40 md.SerializeToString(&md_str);
43 HEPMC3_ERROR_LEVEL(100,
"When writing protobuf message, the message digest was not the expected length ("<< MDBytesLength <<
" bytes), but was instead "<< md_str.size() <<
" bytes.")
46 (*out_stream) << md_str;
47 (*out_stream) << msg_str;
48 return md_str.size() + msg_str.size();
52 std::shared_ptr<GenRunInfo> run)
56 run = std::make_shared<GenRunInfo>();
62 new ofstream(filename, ios::out | ios::trunc | ios::binary));
75 std::shared_ptr<GenRunInfo> run)
79 HEPMC3_ERROR_LEVEL(100,
"Cannot initialize Writerprotobuf on ostream which is not good().")
84 run = std::make_shared<GenRunInfo>();
93 std::shared_ptr<GenRunInfo> run)
102 HepMC3_pb::Header hdr;
109 hdr.set_protobuf_version_maj((GOOGLE_PROTOBUF_VERSION / 1000000) % 1000);
110 hdr.set_protobuf_version_min((GOOGLE_PROTOBUF_VERSION / 1000) % 1000);
111 hdr.set_protobuf_version_patch(GOOGLE_PROTOBUF_VERSION % 1000);
114 hdr.SerializeToString(&hdr_msg);
123 HepMC3_pb::MessageDigest::Event);
129 HepMC3_pb::MessageDigest::RunInfo);
138 HEPMC3_ERROR_LEVEL(100,
"No events were written, the output file will not be parseable.")
141 HepMC3_pb::Footer ftr;
145 ftr.SerializeToString(&ftr_msg);
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Declaration of the Verrion functions and some macros.
#define HEPMC3_VERSION_CODE
HepMC version as an integer, HepMC X.Y.Z = 1000000*X + 1000*Y + Z.
Definition of class Writerprotobuf.
Stores event-related information.
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 serialization for protobuf-based binary files.
Writerprotobuf(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
New file constructor.
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.
void start_file()
Write non-event front matter to the output stream.
std::shared_ptr< std::ostream > m_shared_stream
Passed in shared_ptr to an 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.
static size_t const MDBytesLength
Constant.
size_t write_message(std::ostream *out_stream, std::string const &msg_str, HepMC3_pb::MessageDigest::MessageType type)
Write a message.
std::string version()
Get the HepMC library version string.
bxz::ofstream ofstream
ofstream
std::string const ProtobufMagicHeader
Header of the protobuf file.
Definition of utility functions for protobufIO.