HepMC3 event record library
protobufUtils.cc
Go to the documentation of this file.
1/**
2 * @file protobufUtils.cc
3 * @brief Implementationof utility functions for protobufIO
4 *
5 */
7
9#include "HepMC3/GenVertex.h"
10
15
16#include "HepMC3/Version.h"
17
18namespace HepMC3 {
19
20namespace Serialize {
21
22template <typename T> std::string PBObjToString(T const &o) {
23 std::string ostr;
24 o.SerializeToString(&ostr);
25 return ostr;
26}
27
28std::string GenRunInfo(HepMC3::GenRunInfo const &run_info) {
29 GenRunInfoData data;
30 run_info.write_data(data);
31
32 HepMC3_pb::GenRunInfoData gri_pb;
33
34 for (auto const &s : data.weight_names) {
35 gri_pb.add_weight_names(s);
36 }
37
38 for (auto const &s : data.tool_name) {
39 gri_pb.add_tool_name(s);
40 }
41 for (auto const &s : data.tool_version) {
42 gri_pb.add_tool_version(s);
43 }
44 for (auto const &s : data.tool_description) {
45 gri_pb.add_tool_description(s);
46 }
47
48 for (auto const &s : data.attribute_name) {
49 gri_pb.add_attribute_name(s);
50 }
51 for (auto const &s : data.attribute_string) {
52 gri_pb.add_attribute_string(s);
53 }
54
55 return PBObjToString(gri_pb);
56}
57
58std::string GenEvent(HepMC3::GenEvent const &evt) {
59 GenEventData data;
60 evt.write_data(data);
61
62 HepMC3_pb::GenEventData ged_pb;
63 ged_pb.set_event_number(data.event_number);
64
65 switch (data.momentum_unit) {
66 case HepMC3::Units::MEV: {
67 ged_pb.set_momentum_unit(HepMC3_pb::GenEventData::MEV);
68 break;
69 }
70 case HepMC3::Units::GEV: {
71 ged_pb.set_momentum_unit(HepMC3_pb::GenEventData::GEV);
72 break;
73 }
74 default: {
75 HEPMC3_ERROR_LEVEL(300,"Unknown momentum unit: " << data.momentum_unit)
76 return "ERROR";
77 }
78 }
79
80 switch (data.length_unit) {
81 case HepMC3::Units::MM: {
82 ged_pb.set_length_unit(HepMC3_pb::GenEventData::MM);
83 break;
84 }
85 case HepMC3::Units::CM: {
86 ged_pb.set_length_unit(HepMC3_pb::GenEventData::CM);
87 break;
88 }
89 default: {
90 HEPMC3_ERROR_LEVEL(300,"Unknown length unit: " << data.length_unit)
91 return "ERROR";
92 }
93 }
94
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);
101
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());
106 }
107
108 for (auto const &vdata : data.vertices) {
109 auto* vertex_pb = ged_pb.add_vertices();
110 vertex_pb->set_status(vdata.status);
111
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());
116 }
117
118 for (auto const &s : data.weights) {
119 ged_pb.add_weights(s);
120 }
121
122 for (auto const &s : data.links1) {
123 ged_pb.add_links1(s);
124 }
125 for (auto const &s : data.links2) {
126 ged_pb.add_links2(s);
127 }
128
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());
133
134 for (auto const &s : data.attribute_id) {
135 ged_pb.add_attribute_id(s);
136 }
137 for (auto const &s : data.attribute_name) {
138 ged_pb.add_attribute_name(s);
139 }
140 for (auto const &s : data.attribute_string) {
141 ged_pb.add_attribute_string(s);
142 }
143 return PBObjToString(ged_pb);
144}
145
146} // namespace Serialize
147
148namespace Deserialize {
149
150void FillGenRunInfo(HepMC3_pb::GenRunInfoData const &gri_pb,
151 std::shared_ptr<HepMC3::GenRunInfo> run_info) {
152
153 HepMC3::GenRunInfoData gridata;
154
155 int vector_size = 0;
156
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));
160 }
161
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));
165 }
166
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));
170 }
171
172 vector_size = gri_pb.tool_description_size();
173 for (int it = 0; it < vector_size; ++it) {
174 gridata.tool_description.push_back(gri_pb.tool_description(it));
175 }
176
177 vector_size = gri_pb.attribute_name_size();
178 for (int it = 0; it < vector_size; ++it) {
179 gridata.attribute_name.push_back(gri_pb.attribute_name(it));
180 }
181
182 vector_size = gri_pb.attribute_string_size();
183 for (int it = 0; it < vector_size; ++it) {
184 gridata.attribute_string.push_back(gri_pb.attribute_string(it));
185 }
186
187 run_info->read_data(gridata);
188}
189
190bool GenRunInfo(std::string const &msg,
191 std::shared_ptr<HepMC3::GenRunInfo> run_info) {
192 if (!run_info) { // elide work because we have nowhere to put it
193 return true;
194 }
195
196 HepMC3_pb::GenRunInfoData gri_pb;
197 if (!gri_pb.ParseFromString(msg)) {
198 return false;
199 }
200
201 FillGenRunInfo(gri_pb, run_info);
202
203 return true;
204}
205
206void FillGenEvent(HepMC3_pb::GenEventData const &ged_pb,
207 HepMC3::GenEvent &evt) {
208
209 HepMC3::GenEventData evtdata;
210 evtdata.event_number = ged_pb.event_number();
211
212 switch (ged_pb.momentum_unit()) {
213 case HepMC3_pb::GenEventData::MEV: {
214 evtdata.momentum_unit = HepMC3::Units::MEV;
215 break;
216 }
217 case HepMC3_pb::GenEventData::GEV: {
218 evtdata.momentum_unit = HepMC3::Units::GEV;
219 break;
220 }
221 default: {
222 HEPMC3_ERROR_LEVEL(300,"Unknown momentum unit: " << ged_pb.momentum_unit())
223 return;
224 }
225 }
226
227 switch (ged_pb.length_unit()) {
228 case HepMC3_pb::GenEventData::MM: {
229 evtdata.length_unit = HepMC3::Units::MM;
230 break;
231 }
232 case HepMC3_pb::GenEventData::CM: {
233 evtdata.length_unit = HepMC3::Units::CM;
234 break;
235 }
236 default: {
237 HEPMC3_ERROR_LEVEL(300,"Unknown length unit: " << ged_pb.length_unit())
238 return;
239 }
240 }
241
242 int vector_size = 0;
243
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);
248
249 HepMC3::GenParticleData pdata;
250
251 pdata.pid = particle_pb.pid();
252 pdata.status = particle_pb.status();
253 pdata.is_mass_set = particle_pb.is_mass_set();
254 pdata.mass = particle_pb.mass();
255
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()};
259
260 evtdata.particles.push_back(pdata);
261 }
262
263 evtdata.vertices.clear();
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);
267
268 HepMC3::GenVertexData vdata;
269
270 vdata.status = vertex_pb.status();
271
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()};
275
276 evtdata.vertices.push_back(vdata);
277 }
278
279 evtdata.weights.clear();
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));
283 }
284
285 evtdata.links1.clear();
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));
289 }
290
291 evtdata.links2.clear();
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));
295 }
296
297 evtdata.event_pos =
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()};
300
301 evtdata.attribute_id.clear();
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));
305 }
306
307 evtdata.attribute_name.clear();
308 vector_size = ged_pb.attribute_name_size();
309 for (int it = 0; it < vector_size; ++it) {
310 evtdata.attribute_name.push_back(ged_pb.attribute_name(it));
311 }
312
313 evtdata.attribute_string.clear();
314 vector_size = ged_pb.attribute_string_size();
315 for (int it = 0; it < vector_size; ++it) {
316 evtdata.attribute_string.push_back(ged_pb.attribute_string(it));
317 }
318
319 evt.read_data(evtdata);
320}
321
322bool GenEvent(std::string const &msg, HepMC3::GenEvent &evt) {
323
324 HepMC3_pb::GenEventData ged_pb;
325 if (!ged_pb.ParseFromString(msg)) {
326 return false;
327 }
328
329 FillGenEvent(ged_pb, evt);
330
331 return true;
332}
333
334} // namespace Deserialize
335
336void GenEvent::read_data(HepMC3_pb::GenEventData const &data) {
337 this->clear();
338 this->set_event_number(data.event_number());
339
340 // Note: set_units checks the current unit of event, i.e. applicable only for
341 // fully constructed event.
342 switch (data.momentum_unit()) {
343 case HepMC3_pb::GenEventData::MEV: {
344 m_momentum_unit = HepMC3::Units::MEV;
345 break;
346 }
347 case HepMC3_pb::GenEventData::GEV: {
348 m_momentum_unit = HepMC3::Units::GEV;
349 break;
350 }
351 }
352
353 switch (data.length_unit()) {
354 case HepMC3_pb::GenEventData::MM: {
355 m_length_unit = HepMC3::Units::MM;
356 break;
357 }
358 case HepMC3_pb::GenEventData::CM: {
359 m_length_unit = HepMC3::Units::CM;
360 break;
361 }
362 }
363
364 this->shift_position_to(
365 HepMC3::FourVector{data.event_pos().m_v1(), data.event_pos().m_v2(),
366 data.event_pos().m_v3(), data.event_pos().m_v4()});
367
368 // Fill weights
369 m_weights.resize(data.weights_size());
370 std::copy(data.weights().begin(), data.weights().end(), m_weights.begin());
371
372 m_particles.reserve(data.particles_size());
373 m_vertices.reserve(data.vertices_size());
374
375 // Fill particle information
376 for (auto const &pd : data.particles()) {
377 m_particles.emplace_back(std::make_shared<GenParticle>(pd));
378 m_particles.back()->m_event = this;
379 m_particles.back()->m_id = m_particles.size();
380 }
381
382 // Fill vertex information
383 for (auto const &vd : data.vertices()) {
384 m_vertices.emplace_back(std::make_shared<GenVertex>(vd));
385 m_vertices.back()->m_event = this;
386 m_vertices.back()->m_id = -static_cast<int>(m_vertices.size());
387 }
388
389 // Restore links
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);
393 /* @note:
394 The meaningfull combinations for (id1,id2) are:
395 (+-) -- particle has end vertex
396 (-+) -- particle has production vertex
397 */
398 if ((id1 < 0 && id2 < 0) || (id1 > 0 && id2 > 0)) {
399 HEPMC3_WARNING_LEVEL(600,"GenEvent::read_data: wrong link: " << id1 << " " << id2)
400 continue;
401 }
402
403 if (id1 > 0) {
404 m_vertices[(-id2) - 1]->add_particle_in(m_particles[id1 - 1]);
405 continue;
406 }
407 if (id1 < 0) {
408 m_vertices[(-id1) - 1]->add_particle_out(m_particles[id2 - 1]);
409 continue;
410 }
411 }
412 for (auto &p : m_particles) {
413 if (!p->production_vertex()) m_rootvertex->add_particle_out(p);
414 }
415 // Read attributes
416 std::lock_guard<std::recursive_mutex> lock(m_lock_attributes);
417 for (unsigned int i = 0; i < static_cast<unsigned int>(data.attribute_id_size()); ++i) {
418 /// Disallow empty strings
419 const std::string& name = data.attribute_name(i);
420 if (name.length() == 0) {continue;}
421 const int id = data.attribute_id(i);
422 if (m_attributes.count(name) == 0) { m_attributes[name] = std::map<int, std::shared_ptr<Attribute>>(); }
423 auto att = std::make_shared<StringAttribute>(data.attribute_string(i));
424 att->m_event = this;
425 if (id > 0 && id <= int(m_particles.size())) {
426 att->m_particle = m_particles[id - 1];
427 }
428 if (id < 0 && -id <= int(m_vertices.size())) {
429 att->m_vertex = m_vertices[-id - 1];
430 }
431 m_attributes[name][id] = att;
432 }
433}
434
435GenVertex::GenVertex(HepMC3_pb::GenEventData_GenVertexData const &data)
436 : m_event(nullptr), m_id(0) {
437 m_data.status = data.status();
438 m_data.position =
439 HepMC3::FourVector{data.position().m_v1(), data.position().m_v2(),
440 data.position().m_v3(), data.position().m_v4()};
441}
442
443GenParticle::GenParticle(HepMC3_pb::GenEventData_GenParticleData const &data)
444 : m_event(nullptr), m_id(0) {
445 m_data.pid = data.pid();
446 m_data.momentum =
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();
452}
453
454} // namespace HepMC3
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition Errors.h:34
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Definition Errors.h:27
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.
Generic 4-vector.
Definition FourVector.h:36
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition GenEvent.h:406
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
Definition GenEvent.h:211
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition GenEvent.cc:625
std::vector< double > m_weights
Event weights.
Definition GenEvent.h:381
void set_event_number(const int &num)
Set event number.
Definition GenEvent.h:157
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.
Definition GenEvent.h:397
Units::MomentumUnit m_momentum_unit
Momentum unit.
Definition GenEvent.h:384
std::vector< GenVertexPtr > m_vertices
List of vertices.
Definition GenEvent.h:375
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it.
Definition GenEvent.h:389
Units::LengthUnit m_length_unit
Length unit.
Definition GenEvent.h:386
std::vector< GenParticlePtr > m_particles
List of particles.
Definition GenEvent.h:373
GenParticle(const FourVector &momentum=FourVector::ZERO_VECTOR(), int pid=0, int status=0)
Default constructor.
void write_data(GenRunInfoData &data) const
Fill GenRunInfoData object.
Definition GenRunInfo.cc:53
GenVertex(const FourVector &position=FourVector::ZERO_VECTOR())
Default constructor.
Definition GenVertex.cc:22
HepMC3 main namespace.
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.
int status
Vertex status.
FourVector position
Position in time-space.