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