HepMC3 event record library: Print.cc Source File

HepMC3 event record library

Print.cc
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6///
7/// @file Print.cc
8/// @brief Implementation of static \b class Print
9///
10///
11#include "HepMC3/Print.h"
12#include "HepMC3/Attribute.h"
13
14
15namespace HepMC3 {
16
17 void Print::content(std::ostream& os, const GenEvent &event) {
18 os << "--------------------------------" << std::endl;
19 os << "--------- EVENT CONTENT --------" << std::endl;
20 os << "--------------------------------" << std::endl;
21 os << std::endl;
22
23 os << "Weights (" << event.weights().size() << "): " << std::endl;
24 for (const auto& w: event.weights()) {
25 os << " " << w;
26 }
27
28 os << "Attributes:" << std::endl;
29
30 for (const auto& vt1: event.attributes()) {
31 for (const auto& vt2: vt1.second) {
32 os << vt2.first << ": " << vt1.first << std::endl;
33 }
34 }
35
36 os << "GenParticlePtr (" << event.particles().size() << ")" << std::endl;
37
38 for (const ConstGenParticlePtr& p: event.particles()) {
39 Print::line(os, p, true);
40 os << std::endl;
41 }
42
43 os << "GenVertexPtr (" << event.vertices().size() << ")" << std::endl;
44 for ( const ConstGenVertexPtr& v: event.vertices() ) {
45 Print::line(os, v, true);
46 os << std::endl;
47 }
48
49 os << "-----------------------------" << std::endl;
50}
51
52 void Print::listing(std::ostream& os, const GenEvent &event, unsigned short precision) {
53 // Find the current stream state
54 std::ios_base::fmtflags orig = os.flags();
55 std::streamsize prec = os.precision();
56
57 // Set precision
58 os.precision(precision);
59
60 os << "________________________________________________________________________" << std::endl;
61 os << "GenEvent: #" << event.event_number() << std::endl;
62 os << " Momentum units: " << Units::name(event.momentum_unit())
63 << " Position units: " << Units::name(event.length_unit()) << std::endl;
64 os << " Entries in this event: " << event.vertices().size() << " vertices, "
65 << event.particles().size() << " particles, "
66 << event.weights().size() << " weights." << std::endl;
67
68 const FourVector &pos = event.event_pos();
69 os << " Position offset: " << pos.x() << ", " << pos.y() << ", " << pos.z() << ", " << pos.t() << std::endl;
70
71 // Print a legend to describe the particle info
72 os << " GenParticle Legend" << std::endl;
73 os << " ID PDG ID "
74 << "( px, py, pz, E )"
75 << " Stat ProdVtx" << std::endl;
76 os << "________________________________________________________________________" << std::endl;
77
78 // Print all vertices
79 for (const ConstGenVertexPtr& v: event.vertices()) {
80 Print::listing(os, v);
81 }
82
83 // Restore the stream state
84 os.flags(orig);
85 os.precision(prec);
86 os << "________________________________________________________________________" << std::endl;
87}
88
89 void Print::listing(std::ostream& os, const GenRunInfo &ri, unsigned short precision) {
90 // Find the current stream state
91 std::ios_base::fmtflags orig = os.flags();
92 std::streamsize prec = os.precision();
93
94 // Set precision
95 os.precision(precision);
96
97 os << "________________________________________________________________________" << std::endl;
98 os << "GenRunInfo:" << std::endl;
99
100 std::vector<std::string> names = ri.weight_names();
101 os << " Names: ( ";
102 for (const auto& n: names) os << n;
103 os << " )" << std::endl;
104
105 os << " Tools: " << std::endl;
106
107 for (const auto& t: ri.tools()) {
108 Print::line(os, t);
109 }
110 os << "Attributes:" << std::endl;
111 for (const auto& att: ri.attributes()) {
112 std::string st;
113 if ( !att.second->to_string(st) ) {
114 HEPMC3_WARNING_LEVEL(300,"Print::listing: problem serializing attribute: " << att.first)
115 }
116 else { os << att.first << " " << st;}
117 os << std::endl;
118 }
119
120 // Restore the stream state
121 os.flags(orig);
122 os.precision(prec);
123 os << "________________________________________________________________________" << std::endl;
124}
125
126 void Print::listing(std::ostream& os, ConstGenVertexPtr v) {
127 if (!v) { os << "Vtx: Empty vertex" << std::endl; return;}
128 os << "Vtx: ";
129 os.width(6);
130 os << v->id() << " stat: ";
131 os.width(3);
132 os << v->status();
133
134 const FourVector &pos = v->position();
135 if ( !pos.is_zero() ) {
136 os << " (X,cT): " << pos.x() << " " << pos.y() << " " << pos.z() << " " << pos.t();
137 }
138 else os << " (X,cT): 0";
139
140 os << std::endl;
141
142 bool printed_header = false;
143
144 // Print out all the incoming particles
145 for (const ConstGenParticlePtr& p: v->particles_in()) {
146 if ( !printed_header ) {
147 os << " I: ";
148 printed_header = true;
149 }
150 else os << " ";
151
152 Print::listing(os, p);
153 }
154
155 printed_header = false;
156
157 // Print out all the outgoing particles
158 for (const ConstGenParticlePtr& p: v->particles_out()) {
159 if ( !printed_header ) {
160 os << " O: ";
161 printed_header = true;
162 }
163 else os << " ";
164
165 Print::listing(os, p);
166 }
167}
168
169 void Print::listing(std::ostream& os, ConstGenParticlePtr p) {
170 if (!p) { os << " Empty particle" << std::endl; return;}
171 os << " ";
172 os.width(6);
173 os << p->id();
174 os.width(9);
175 os << p->pid() << " ";
176 os.width(9);
177 os.setf(std::ios::scientific, std::ios::floatfield);
178 os.setf(std::ios_base::showpos);
179
180 const FourVector &momentum = p->momentum();
181
182 os.width(9);
183 os << momentum.px() << ",";
184 os.width(9);
185 os << momentum.py() << ",";
186 os.width(9);
187 os << momentum.pz() << ",";
188 os.width(9);
189 os << momentum.e() << " ";
190 os.setf(std::ios::fmtflags(0), std::ios::floatfield);
191 os.unsetf(std::ios_base::showpos);
192 os.width(3);
193 os << p->status();
194
195 ConstGenVertexPtr prod = p->production_vertex();
196
197 if ( prod ) {
198 os.width(6);
199 os << prod->id();
200 }
201
202 os << std::endl;
203}
204 void Print::line(std::ostream& os, const GenEvent &event, bool attributes) {
205 os << "GenEvent: #" << event.event_number();
206 if (attributes) {
207 for (const std::string& s: event.attribute_names()) {
208 os << " " << s << "=" <<event.attribute_as_string(s);
209 }
210 }
211}
212
213 void Print::line(std::ostream& os, const GenRunInfo &RunInfo, bool attributes) {
214 os <<"GenRunInfo: Number of tools:" << RunInfo.tools().size();
215
216 if (attributes) {
217 for (const std::string& s: RunInfo.attribute_names()) {
218 os << " " << s << "=" << RunInfo.attribute_as_string(s);
219 }
220 }
221}
222
223 void Print::line(std::ostream& os, const GenRunInfo::ToolInfo& t) {
224 os << "GenRunInfo::ToolInfo " << t.name<< " " << t.version << " " << t.description;
225}
226
227template <class T>
228void line_v(std::ostream& os, T v, bool attributes) {
229 if (!v) { os << "GenVertex: Empty" << std::endl; return;}
230 os << "GenVertex: " << v->id() << " stat: ";
231 os.width(3);
232 os << v->status();
233 os << " in: " << v->particles_in().size();
234 os.width(3);
235 os << " out: " << v->particles_out().size();
236
237 const FourVector &pos = v->position();
238 os << " has_set_position: ";
239 if ( v->has_set_position() ) { os << "true"; }
240 else { os << "false"; }
241
242 os << " (X,cT): " << pos.x() << ", " <<pos.y() << ", " << pos.z() << ", " << pos.t();
243 if (attributes)
244 {
245 auto names = v->attribute_names();
246 for (const auto& ss: names) {
247 os << " " << ss << "=" << (*v).attribute_as_string(ss);
248 }
249 }
250}
251 void Print::line(std::ostream& os, ConstGenVertexPtr v, bool attributes) { line_v(os,v,attributes); }
252 void Print::line(std::ostream& os, GenVertexPtr v, bool attributes) { line_v(os,v,attributes); }
253
254
255
256 void Print::line(std::ostream& os, const FourVector& p) {
257 os << "FourVector: ";
258 // Find the current stream state
259 std::ios_base::fmtflags orig = os.flags();
260 os.setf(std::ios::scientific, std::ios::floatfield);
261 os.setf(std::ios_base::showpos);
262 std::streamsize prec = os.precision();
263 // Set precision
264 os.precision(2);
265 os << " (P,E)=" << p.x()
266 << "," << p.y()
267 << "," << p.z()
268 << "," << p.e();
269
270 // Restore the stream state
271 os.flags(orig);
272 os.precision(prec);
273}
274
275template <class T>
276void line_p(std::ostream& os, T p, bool attributes) {
277 if (!p) { os << "GenParticle: Empty" << std::endl; return;}
278 os << "GenParticle: ";
279 os.width(3);
280 os << p->id() <<" PDGID: ";
281 os.width(5);
282 os << p->pid();
283
284 // Find the current stream state
285 std::ios_base::fmtflags orig = os.flags();
286
287 os.setf(std::ios::scientific, std::ios::floatfield);
288 os.setf(std::ios_base::showpos);
289 std::streamsize prec = os.precision();
290
291 // Set precision
292 os.precision(2);
293
294 const FourVector &momentum = p->momentum();
295
296 os << " (P,E)=" << momentum.px()
297 << "," << momentum.py()
298 << "," << momentum.pz()
299 << "," << momentum.e();
300
301 // Restore the stream state
302 os.flags(orig);
303 os.precision(prec);
304
305 const ConstGenVertexPtr prod = p->production_vertex();
306 const ConstGenVertexPtr end = p->end_vertex();
307 int prod_vtx_id = (prod) ? prod->id() : 0;
308 int end_vtx_id = (end) ? end->id() : 0;
309 auto names = p->attribute_names();
310
311 os << " Stat: " << p->status()
312 << " PV: " << prod_vtx_id
313 << " EV: " << end_vtx_id
314 << " Attr: " << names.size();
315
316 if (attributes)
317 {
318 for (const auto& ss: names) {
319 os << " " << ss << "=" << (*p).attribute_as_string(ss);
320 }
321 }
322}
323
324 void Print::line(std::ostream& os, ConstGenParticlePtr p, bool attributes) { line_p(os,p,attributes); }
325 void Print::line(std::ostream& os, GenParticlePtr p, bool attributes) { line_p(os,p,attributes); }
326
327 void Print::line(std::ostream& os, std::shared_ptr<GenCrossSection> &cs) {
328 if (!cs) {os << " GenCrossSection: Empty"; return;}
329 os << " GenCrossSection: " << cs->xsec(0)
330 << " " << cs->xsec_err(0)
331 << " " << cs->get_accepted_events()
332 << " " << cs->get_attempted_events();
333}
334
335 void Print::line(std::ostream& os, std::shared_ptr<GenHeavyIon> &hi) {
336 if (!hi) {os << " GenHeavyIon: Empty"; return;}
337 os << " GenHeavyIon: " << hi->Ncoll_hard
338 << " " << hi->Npart_proj
339 << " " << hi->Npart_targ
340 << " " << hi->Ncoll
341 << " " << hi->spectator_neutrons
342 << " " << hi->spectator_protons
343 << " " << hi->N_Nwounded_collisions
344 << " " << hi->Nwounded_N_collisions
345 << " " << hi->Nwounded_Nwounded_collisions
346 << " " << hi->impact_parameter
347 << " " << hi->event_plane_angle
348 << " " << hi->eccentricity
349 << " " << hi->sigma_inel_NN;
350}
351
352 void Print::line(std::ostream& os, std::shared_ptr<GenPdfInfo> &pi) {
353 if (!pi) {os << " GenPdfInfo: Empty"; return;}
354 os << " GenPdfInfo: " << pi->parton_id[0]
355 << " " << pi->parton_id[1]
356 << " " << pi->x[0]
357 << " " << pi->x[1]
358 << " " << pi->scale
359 << " " << pi->xf[0]
360 << " " << pi->xf[1]
361 << " " << pi->pdf_id[0]
362 << " " << pi->pdf_id[1];
363}
364
365} // namespace HepMC3
Attribute.h
Definition of class Attribute, class IntAttribute and class StringAttribute.
HEPMC3_WARNING_LEVEL
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition Errors.h:34
Print.h
Definition of static class Print.
HepMC3::FourVector
Generic 4-vector.
Definition FourVector.h:36
HepMC3::FourVector::e
double e() const
Energy component of momentum.
Definition FourVector.h:135
HepMC3::FourVector::pz
double pz() const
z-component of momentum
Definition FourVector.h:128
HepMC3::FourVector::t
double t() const
Time component of position/displacement.
Definition FourVector.h:106
HepMC3::FourVector::is_zero
bool is_zero() const
Check if the length of this vertex is zero.
Definition FourVector.h:206
HepMC3::FourVector::px
double px() const
x-component of momentum
Definition FourVector.h:114
HepMC3::FourVector::py
double py() const
y-component of momentum
Definition FourVector.h:121
HepMC3::FourVector::x
double x() const
x-component of position/displacement
Definition FourVector.h:85
HepMC3::FourVector::y
double y() const
y-component of position/displacement
Definition FourVector.h:92
HepMC3::FourVector::z
double z() const
z-component of position/displacement
Definition FourVector.h:99
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::GenEvent::attribute_names
std::vector< std::string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition GenEvent.cc:613
HepMC3::GenEvent::vertices
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const).
Definition GenEvent.cc:43
HepMC3::GenEvent::momentum_unit
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Definition GenEvent.h:160
HepMC3::GenEvent::length_unit
const Units::LengthUnit & length_unit() const
Get length unit.
Definition GenEvent.h:162
HepMC3::GenEvent::attributes
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
Definition GenEvent.h:270
HepMC3::GenEvent::weights
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition GenEvent.h:105
HepMC3::GenEvent::particles
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const).
Definition GenEvent.cc:39
HepMC3::GenRunInfo
Stores run-related information.
Definition GenRunInfo.h:33
HepMC3::GenRunInfo::attributes
std::map< std::string, std::shared_ptr< Attribute > > attributes() const
Get a copy of the list of attributes.
Definition GenRunInfo.h:126
HepMC3::GenRunInfo::attribute_names
std::vector< std::string > attribute_names() const
Get list of attribute names.
Definition GenRunInfo.cc:77
HepMC3::GenRunInfo::weight_names
const std::vector< std::string > & weight_names() const
Get the vector of weight names.
Definition GenRunInfo.h:89
HepMC3::GenRunInfo::tools
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition GenRunInfo.h:63
HepMC3::GenRunInfo::attribute_as_string
std::string attribute_as_string(const std::string &name) const
Get attribute of any type as string.
Definition GenRunInfo.cc:40
HepMC3::Print::content
static void content(std::ostream &os, const GenEvent &event)
Print content of all GenEvent containers.
Definition Print.cc:17
HepMC3::Print::listing
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition Print.cc:52
HepMC3::Print::line
static void line(std::ostream &os, const GenEvent &event, bool attributes=false)
Print one-line info.
Definition Print.cc:204
HepMC3::Units::name
static std::string name(MomentumUnit u)
Get name of momentum unit.
Definition Units.h:56
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::GenRunInfo::ToolInfo
Interrnal struct for keeping track of tools.
Definition GenRunInfo.h:38
HepMC3::GenRunInfo::ToolInfo::description
std::string description
Other information about how the tool was used in the run.
Definition GenRunInfo.h:48
HepMC3::GenRunInfo::ToolInfo::version
std::string version
The version of the tool.
Definition GenRunInfo.h:44
HepMC3::GenRunInfo::ToolInfo::name
std::string name
The name of the tool.
Definition GenRunInfo.h:41