13#include "HepMC3/Version.h"
25HEPMC3_DECLARE_WRITER_FILE(Writerprotobuf)
26HEPMC3_DECLARE_WRITER_STREAM(Writerprotobuf)
34 HepMC3_pb::MessageDigest::MessageType type) {
37 msg.SerializeToString(&msg_str);
39 HepMC3_pb::MessageDigest md;
40 md.set_bytes(msg_str.size());
41 md.set_message_type(type);
44 md.SerializeToString(&md_str);
47 HEPMC3_ERROR(
"When writing protobuf message, the message digest was not "
48 "the expected length ("
49 << MDBytesLength <<
" bytes), but was instead "
50 << md_str.size() <<
" bytes.");
53 (*out_stream) << md_str;
55 (*out_stream) << msg_str;
56 return md_str.size() + msg_str.size();
60 std::shared_ptr<GenRunInfo> run)
61 : m_out_file(nullptr), m_events_written(0), m_event_bytes_written(0) {
64 run = std::make_shared<GenRunInfo>();
70 new ofstream(filename, ios::out | ios::trunc | ios::binary));
74 HEPMC3_ERROR(
"Writerprotobuf: problem opening file: " << filename)
83 std::shared_ptr<GenRunInfo> run)
84 : m_out_file(nullptr), m_events_written(0), m_event_bytes_written(0) {
88 "Cannot initialize Writerprotobuf on ostream which is not good().");
93 run = std::make_shared<GenRunInfo>();
102 std::shared_ptr<GenRunInfo> run)
109 HepMC3_pb::Header hdr;
112 hdr.set_version_maj((HEPMC3_VERSION_CODE / 1000000) % 1000);
113 hdr.set_version_min((HEPMC3_VERSION_CODE / 1000) % 1000);
114 hdr.set_version_patch(HEPMC3_VERSION_CODE % 1000);
116 hdr.set_protobuf_version_maj((GOOGLE_PROTOBUF_VERSION / 1000000) % 1000);
117 hdr.set_protobuf_version_min((GOOGLE_PROTOBUF_VERSION / 1000) % 1000);
118 hdr.set_protobuf_version_patch(GOOGLE_PROTOBUF_VERSION % 1000);
130 HepMC3_pb::GenEventData ged_pb;
134 case HepMC3::Units::MEV: {
135 ged_pb.set_momentum_unit(HepMC3_pb::GenEventData::MEV);
138 case HepMC3::Units::GEV: {
139 ged_pb.set_momentum_unit(HepMC3_pb::GenEventData::GEV);
149 case HepMC3::Units::MM: {
150 ged_pb.set_length_unit(HepMC3_pb::GenEventData::MM);
153 case HepMC3::Units::CM: {
154 ged_pb.set_length_unit(HepMC3_pb::GenEventData::CM);
163 for (
auto const &pdata : data.
particles) {
164 auto particle_pb = ged_pb.add_particles();
165 particle_pb->set_pid(pdata.pid);
166 particle_pb->set_status(pdata.status);
167 particle_pb->set_is_mass_set(pdata.is_mass_set);
168 particle_pb->set_mass(pdata.mass);
170 particle_pb->mutable_momentum()->set_m_v1(pdata.momentum.x());
171 particle_pb->mutable_momentum()->set_m_v2(pdata.momentum.y());
172 particle_pb->mutable_momentum()->set_m_v3(pdata.momentum.z());
173 particle_pb->mutable_momentum()->set_m_v4(pdata.momentum.t());
176 for (
auto const &vdata : data.
vertices) {
177 auto vertex_pb = ged_pb.add_vertices();
178 vertex_pb->set_status(vdata.status);
180 vertex_pb->mutable_position()->set_m_v1(vdata.position.x());
181 vertex_pb->mutable_position()->set_m_v2(vdata.position.y());
182 vertex_pb->mutable_position()->set_m_v3(vdata.position.z());
183 vertex_pb->mutable_position()->set_m_v4(vdata.position.t());
186 for (
auto const &s : data.
weights) {
187 ged_pb.add_weights(s);
190 for (
auto const &s : data.
links1) {
191 ged_pb.add_links1(s);
193 for (
auto const &s : data.
links2) {
194 ged_pb.add_links2(s);
197 ged_pb.mutable_event_pos()->set_m_v1(data.
event_pos.
x());
198 ged_pb.mutable_event_pos()->set_m_v2(data.
event_pos.
y());
199 ged_pb.mutable_event_pos()->set_m_v3(data.
event_pos.
z());
200 ged_pb.mutable_event_pos()->set_m_v4(data.
event_pos.
t());
203 ged_pb.add_attribute_id(s);
206 ged_pb.add_attribute_name(s);
209 ged_pb.add_attribute_string(s);
222 HepMC3_pb::GenRunInfoData GenRunInfo_pb;
225 GenRunInfo_pb.add_weight_names(s);
229 GenRunInfo_pb.add_tool_name(s);
232 GenRunInfo_pb.add_tool_version(s);
235 GenRunInfo_pb.add_tool_description(s);
239 GenRunInfo_pb.add_attribute_name(s);
242 GenRunInfo_pb.add_attribute_string(s);
255 "No events were written, the output file will not be parseable.");
258 HepMC3_pb::Footer ftr;
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition of struct GenEventData.
Definition of struct GenRunInfoData.
Definition of class Writerprotobuf.
double t() const
Time component of position/displacement.
double x() const
x-component of position/displacement
double y() const
y-component of position/displacement
double z() const
z-component of position/displacement
Stores event-related information.
void write_data(GenEventData &data) const
Fill GenEventData object.
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.
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, T &msg, HepMC3_pb::MessageDigest::MessageType type)
Write a message.
std::string version()
Get the HepMC library version string.
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 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.