HepMC3 event record library: protobufUtils.cc Source File

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 */
6#include "HepMC3/protobufUtils.h"
7
8#include "HepMC3/GenParticle.h"
9#include "HepMC3/GenVertex.h"
10
11#include "HepMC3/Data/GenEventData.h"
12#include "HepMC3/Data/GenParticleData.h"
13#include "HepMC3/Data/GenRunInfoData.h"
14#include "HepMC3/Data/GenVertexData.h"
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
336 void 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
435 GenVertex::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
443 GenParticle::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
HEPMC3_WARNING_LEVEL
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition Errors.h:34
HEPMC3_ERROR_LEVEL
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Definition Errors.h:27
GenEventData.h
Definition of struct GenEventData.
GenParticleData.h
Definition of class GenParticleData.
GenParticle.h
Definition of class GenParticle.
GenRunInfoData.h
Definition of struct GenRunInfoData.
GenVertexData.h
Definition of class GenVertexData.
GenVertex.h
Definition of class GenVertex.
Version.h
Declaration of the Verrion functions and some macros.
HepMC3::FourVector
Generic 4-vector.
Definition FourVector.h:36
HepMC3::GenEvent::m_lock_attributes
std::recursive_mutex m_lock_attributes
Mutex lock for the m_attibutes map.
Definition GenEvent.h:406
HepMC3::GenEvent::shift_position_to
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
Definition GenEvent.h:211
HepMC3::GenEvent::write_data
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition GenEvent.cc:625
HepMC3::GenEvent::m_weights
std::vector< double > m_weights
Event weights.
Definition GenEvent.h:381
HepMC3::GenEvent::set_event_number
void set_event_number(const int &num)
Set event number.
Definition GenEvent.h:157
HepMC3::GenEvent::read_data
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition protobufUtils.cc:336
HepMC3::GenEvent::m_attributes
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > m_attributes
Map of event, particle and vertex attributes.
Definition GenEvent.h:397
HepMC3::GenEvent::m_momentum_unit
Units::MomentumUnit m_momentum_unit
Momentum unit.
Definition GenEvent.h:384
HepMC3::GenEvent::m_vertices
std::vector< GenVertexPtr > m_vertices
List of vertices.
Definition GenEvent.h:375
HepMC3::GenEvent::m_rootvertex
GenVertexPtr m_rootvertex
The root vertex is stored outside the normal vertices list to block user access to it.
Definition GenEvent.h:389
HepMC3::GenEvent::m_length_unit
Units::LengthUnit m_length_unit
Length unit.
Definition GenEvent.h:386
HepMC3::GenEvent::m_particles
std::vector< GenParticlePtr > m_particles
List of particles.
Definition GenEvent.h:373
HepMC3::GenParticle::GenParticle
GenParticle(const FourVector &momentum=FourVector::ZERO_VECTOR(), int pid=0, int status=0)
Default constructor.
Definition GenParticle.cc:20
HepMC3::GenRunInfo::write_data
void write_data(GenRunInfoData &data) const
Fill GenRunInfoData object.
Definition GenRunInfo.cc:53
HepMC3::GenVertex::GenVertex
GenVertex(const FourVector &position=FourVector::ZERO_VECTOR())
Default constructor.
Definition GenVertex.cc:22
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
protobufUtils.h
Definition of utility functions for protobufIO.
HepMC3::GenEventData::vertices
std::vector< GenVertexData > vertices
Vertices.
Definition GenEventData.h:32
HepMC3::GenEventData::links2
std::vector< int > links2
Second id of the vertex links.
Definition GenEventData.h:52
HepMC3::GenEventData::event_number
int event_number
Event number.
Definition GenEventData.h:27
HepMC3::GenEventData::attribute_string
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition GenEventData.h:56
HepMC3::GenEventData::particles
std::vector< GenParticleData > particles
Particles.
Definition GenEventData.h:31
HepMC3::GenEventData::links1
std::vector< int > links1
First id of the vertex links.
Definition GenEventData.h:51
HepMC3::GenEventData::attribute_name
std::vector< std::string > attribute_name
Attribute name.
Definition GenEventData.h:55
HepMC3::GenEventData::length_unit
Units::LengthUnit length_unit
Length unit.
Definition GenEventData.h:29
HepMC3::GenEventData::attribute_id
std::vector< int > attribute_id
Attribute owner id.
Definition GenEventData.h:54
HepMC3::GenEventData::event_pos
FourVector event_pos
Event position.
Definition GenEventData.h:35
HepMC3::GenEventData::weights
std::vector< double > weights
Weights.
Definition GenEventData.h:33
HepMC3::GenEventData::momentum_unit
Units::MomentumUnit momentum_unit
Momentum unit.
Definition GenEventData.h:28
HepMC3::GenParticleData::mass
double mass
Generated mass (if set).
Definition GenParticleData.h:28
HepMC3::GenParticleData::momentum
FourVector momentum
Momentum.
Definition GenParticleData.h:29
HepMC3::GenParticleData::is_mass_set
bool is_mass_set
Check if generated mass is set.
Definition GenParticleData.h:27
HepMC3::GenParticleData::status
int status
Status.
Definition GenParticleData.h:26
HepMC3::GenParticleData::pid
int pid
PDG ID.
Definition GenParticleData.h:25
HepMC3::GenRunInfoData::tool_name
std::vector< std::string > tool_name
Tool names.
Definition GenRunInfoData.h:26
HepMC3::GenRunInfoData::tool_version
std::vector< std::string > tool_version
Tool versions.
Definition GenRunInfoData.h:27
HepMC3::GenRunInfoData::attribute_string
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition GenRunInfoData.h:31
HepMC3::GenRunInfoData::attribute_name
std::vector< std::string > attribute_name
Attribute name.
Definition GenRunInfoData.h:30
HepMC3::GenRunInfoData::tool_description
std::vector< std::string > tool_description
Tool descriptions.
Definition GenRunInfoData.h:28
HepMC3::GenRunInfoData::weight_names
std::vector< std::string > weight_names
Weight names.
Definition GenRunInfoData.h:24
HepMC3::GenVertexData::status
int status
Vertex status.
Definition GenVertexData.h:23
HepMC3::GenVertexData::position
FourVector position
Position in time-space.
Definition GenVertexData.h:24