22template <
typename T> std::string PBObjToString(T
const &o) {
24 o.SerializeToString(&ostr);
28std::string GenRunInfo(HepMC3::GenRunInfo
const &run_info) {
32 HepMC3_pb::GenRunInfoData gri_pb;
34 for (
auto const &s : data.weight_names) {
35 gri_pb.add_weight_names(s);
38 for (
auto const &s : data.tool_name) {
39 gri_pb.add_tool_name(s);
41 for (
auto const &s : data.tool_version) {
42 gri_pb.add_tool_version(s);
44 for (
auto const &s : data.tool_description) {
45 gri_pb.add_tool_description(s);
48 for (
auto const &s : data.attribute_name) {
49 gri_pb.add_attribute_name(s);
51 for (
auto const &s : data.attribute_string) {
52 gri_pb.add_attribute_string(s);
55 return PBObjToString(gri_pb);
58std::string GenEvent(HepMC3::GenEvent
const &evt) {
62 HepMC3_pb::GenEventData ged_pb;
63 ged_pb.set_event_number(data.event_number);
65 switch (data.momentum_unit) {
66 case HepMC3::Units::MEV: {
67 ged_pb.set_momentum_unit(HepMC3_pb::GenEventData::MEV);
70 case HepMC3::Units::GEV: {
71 ged_pb.set_momentum_unit(HepMC3_pb::GenEventData::GEV);
80 switch (data.length_unit) {
81 case HepMC3::Units::MM: {
82 ged_pb.set_length_unit(HepMC3_pb::GenEventData::MM);
85 case HepMC3::Units::CM: {
86 ged_pb.set_length_unit(HepMC3_pb::GenEventData::CM);
95 for (auto const &pdata : data.particles) {
96 auto* particle_pb = ged_pb.add_particles();
97 particle_pb->set_pid(pdata.pid);
98 particle_pb->set_status(pdata.status);
99 particle_pb->set_is_mass_set(pdata.is_mass_set);
100 particle_pb->set_mass(pdata.mass);
102 particle_pb->mutable_momentum()->set_m_v1(pdata.momentum.x());
103 particle_pb->mutable_momentum()->set_m_v2(pdata.momentum.y());
104 particle_pb->mutable_momentum()->set_m_v3(pdata.momentum.z());
105 particle_pb->mutable_momentum()->set_m_v4(pdata.momentum.t());
108 for (
auto const &vdata : data.vertices) {
109 auto* vertex_pb = ged_pb.add_vertices();
110 vertex_pb->set_status(vdata.status);
112 vertex_pb->mutable_position()->set_m_v1(vdata.position.x());
113 vertex_pb->mutable_position()->set_m_v2(vdata.position.y());
114 vertex_pb->mutable_position()->set_m_v3(vdata.position.z());
115 vertex_pb->mutable_position()->set_m_v4(vdata.position.t());
118 for (
auto const &s : data.weights) {
119 ged_pb.add_weights(s);
122 for (
auto const &s : data.links1) {
123 ged_pb.add_links1(s);
125 for (
auto const &s : data.links2) {
126 ged_pb.add_links2(s);
129 ged_pb.mutable_event_pos()->set_m_v1(data.event_pos.x());
130 ged_pb.mutable_event_pos()->set_m_v2(data.event_pos.y());
131 ged_pb.mutable_event_pos()->set_m_v3(data.event_pos.z());
132 ged_pb.mutable_event_pos()->set_m_v4(data.event_pos.t());
134 for (
auto const &s : data.attribute_id) {
135 ged_pb.add_attribute_id(s);
137 for (
auto const &s : data.attribute_name) {
138 ged_pb.add_attribute_name(s);
140 for (
auto const &s : data.attribute_string) {
141 ged_pb.add_attribute_string(s);
143 return PBObjToString(ged_pb);
148namespace Deserialize {
150void FillGenRunInfo(HepMC3_pb::GenRunInfoData
const &gri_pb,
151 std::shared_ptr<HepMC3::GenRunInfo> run_info) {
153 HepMC3::GenRunInfoData gridata;
157 vector_size = gri_pb.weight_names_size();
158 for (
int it = 0; it < vector_size; ++it) {
159 gridata.
weight_names.push_back(gri_pb.weight_names(it));
162 vector_size = gri_pb.tool_name_size();
163 for (
int it = 0; it < vector_size; ++it) {
164 gridata.
tool_name.push_back(gri_pb.tool_name(it));
167 vector_size = gri_pb.tool_version_size();
168 for (
int it = 0; it < vector_size; ++it) {
169 gridata.
tool_version.push_back(gri_pb.tool_version(it));
172 vector_size = gri_pb.tool_description_size();
173 for (
int it = 0; it < vector_size; ++it) {
177 vector_size = gri_pb.attribute_name_size();
178 for (
int it = 0; it < vector_size; ++it) {
182 vector_size = gri_pb.attribute_string_size();
183 for (
int it = 0; it < vector_size; ++it) {
187 run_info->read_data(gridata);
190bool GenRunInfo(std::string
const &msg,
191 std::shared_ptr<HepMC3::GenRunInfo> run_info) {
196 HepMC3_pb::GenRunInfoData gri_pb;
197 if (!gri_pb.ParseFromString(msg)) {
201 FillGenRunInfo(gri_pb, run_info);
206void FillGenEvent(HepMC3_pb::GenEventData
const &ged_pb,
207 HepMC3::GenEvent &evt) {
209 HepMC3::GenEventData evtdata;
212 switch (ged_pb.momentum_unit()) {
213 case HepMC3_pb::GenEventData::MEV: {
217 case HepMC3_pb::GenEventData::GEV: {
227 switch (ged_pb.length_unit()) {
228 case HepMC3_pb::GenEventData::MM: {
232 case HepMC3_pb::GenEventData::CM: {
244 evtdata.particles.clear();
245 vector_size = ged_pb.particles_size();
246 for (
int it = 0; it < vector_size; ++it) {
247 const auto& particle_pb = ged_pb.particles(it);
249 HepMC3::GenParticleData pdata;
251 pdata.
pid = particle_pb.pid();
252 pdata.
status = particle_pb.status();
254 pdata.
mass = particle_pb.mass();
256 pdata.
momentum = HepMC3::FourVector{
257 particle_pb.momentum().m_v1(), particle_pb.momentum().m_v2(),
258 particle_pb.momentum().m_v3(), particle_pb.momentum().m_v4()};
264 vector_size = ged_pb.vertices_size();
265 for (
int it = 0; it < vector_size; ++it) {
266 const auto& vertex_pb = ged_pb.vertices(it);
268 HepMC3::GenVertexData vdata;
270 vdata.
status = vertex_pb.status();
272 vdata.
position = HepMC3::FourVector{
273 vertex_pb.position().m_v1(), vertex_pb.position().m_v2(),
274 vertex_pb.position().m_v3(), vertex_pb.position().m_v4()};
280 vector_size = ged_pb.weights_size();
281 for (
int it = 0; it < vector_size; ++it) {
282 evtdata.
weights.push_back(ged_pb.weights(it));
286 vector_size = ged_pb.links1_size();
287 for (
int it = 0; it < vector_size; ++it) {
288 evtdata.
links1.push_back(ged_pb.links1(it));
292 vector_size = ged_pb.links2_size();
293 for (
int it = 0; it < vector_size; ++it) {
294 evtdata.
links2.push_back(ged_pb.links2(it));
298 HepMC3::FourVector{ged_pb.event_pos().m_v1(), ged_pb.event_pos().m_v2(),
299 ged_pb.event_pos().m_v3(), ged_pb.event_pos().m_v4()};
302 vector_size = ged_pb.attribute_id_size();
303 for (
int it = 0; it < vector_size; ++it) {
304 evtdata.
attribute_id.push_back(ged_pb.attribute_id(it));
308 vector_size = ged_pb.attribute_name_size();
309 for (
int it = 0; it < vector_size; ++it) {
314 vector_size = ged_pb.attribute_string_size();
315 for (
int it = 0; it < vector_size; ++it) {
322bool GenEvent(std::string
const &msg, HepMC3::GenEvent &evt) {
324 HepMC3_pb::GenEventData ged_pb;
325 if (!ged_pb.ParseFromString(msg)) {
329 FillGenEvent(ged_pb, evt);
342 switch (data.momentum_unit()) {
343 case HepMC3_pb::GenEventData::MEV: {
347 case HepMC3_pb::GenEventData::GEV: {
353 switch (data.length_unit()) {
354 case HepMC3_pb::GenEventData::MM: {
358 case HepMC3_pb::GenEventData::CM: {
366 data.event_pos().m_v3(), data.event_pos().m_v4()});
370 std::copy(data.weights().begin(), data.weights().end(),
m_weights.begin());
376 for (
auto const &pd : data.particles()) {
377 m_particles.emplace_back(std::make_shared<GenParticle>(pd));
383 for (
auto const &vd : data.vertices()) {
384 m_vertices.emplace_back(std::make_shared<GenVertex>(vd));
390 for (
unsigned int i = 0; i < static_cast<unsigned int>(data.links1_size()); ++i) {
391 const int id1 = data.links1(i);
392 const int id2 = data.links2(i);
398 if ((id1 < 0 && id2 < 0) || (id1 > 0 && id2 > 0)) {
413 if (!p->production_vertex())
m_rootvertex->add_particle_out(p);
417 for (
unsigned int i = 0; i < static_cast<unsigned int>(data.attribute_id_size()); ++i) {
419 const std::string& name = data.attribute_name(i);
420 if (name.length() == 0) {
continue;}
421 const int id = data.attribute_id(i);
423 auto att = std::make_shared<StringAttribute>(data.attribute_string(i));
428 if (
id < 0 && -
id <=
int(
m_vertices.size())) {
436 : m_event(nullptr), m_id(0) {
437 m_data.status = data.status();
440 data.position().m_v3(), data.position().m_v4()};
444 : m_event(nullptr), m_id(0) {
445 m_data.pid = data.pid();
447 HepMC3::FourVector{data.momentum().m_v1(), data.momentum().m_v2(),
448 data.momentum().m_v3(), data.momentum().m_v4()};
449 m_data.status = data.status();
450 m_data.is_mass_set = data.is_mass_set();
451 m_data.mass = data.mass();
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Definition of struct GenEventData.
Definition of class GenParticleData.
Definition of class GenParticle.
Definition of struct GenRunInfoData.
Definition of class GenVertexData.
Definition of class GenVertex.
Declaration of the Verrion functions and some macros.
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
void write_data(GenEventData &data) const
Fill GenEventData object.
std::vector< double > m_weights
Event weights.
void set_event_number(const int &num)
Set event number.
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > m_attributes
Map of event, particle and vertex attributes.
Units::MomentumUnit m_momentum_unit
Momentum unit.
std::vector< GenVertexPtr > m_vertices
List of vertices.
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it.
Units::LengthUnit m_length_unit
Length unit.
std::vector< GenParticlePtr > m_particles
List of particles.
GenParticle(const FourVector &momentum=FourVector::ZERO_VECTOR(), int pid=0, int status=0)
Default constructor.
void write_data(GenRunInfoData &data) const
Fill GenRunInfoData object.
GenVertex(const FourVector &position=FourVector::ZERO_VECTOR())
Default constructor.
Definition of utility functions for protobufIO.
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.
double mass
Generated mass (if set)
FourVector momentum
Momentum.
bool is_mass_set
Check if generated mass is set.
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.
FourVector position
Position in time-space.