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