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