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, ConstGenVertexPtr v ) {
90  os << "Vtx: ";
91  os.width(6);
92  os << v->id() << " stat: ";
93  os.width(3);
94  os << v->status();
95 
96  const FourVector &pos = v->position();
97  if( !pos.is_zero() ) {
98  os << " (X,cT): " << pos.x()<<" "<<pos.y()<<" "<<pos.z()<<" "<<pos.t();
99  }
100  else os << " (X,cT): 0";
101 
102  os << endl;
103 
104  bool printed_header = false;
105 
106  // Print out all the incoming particles
107  for(ConstGenParticlePtr p: v->particles_in() ) {
108  if( !printed_header ) {
109  os << " I: ";
110  printed_header = true;
111  }
112  else os << " ";
113 
114  Print::listing(os, p);
115  }
116 
117  printed_header = false;
118 
119  // Print out all the outgoing particles
120  for(ConstGenParticlePtr p: v->particles_out() ) {
121  if( !printed_header ) {
122  os << " O: ";
123  printed_header = true;
124  }
125  else os << " ";
126 
127  Print::listing(os, p);
128  }
129 }
130 
131 void Print::listing( std::ostream& os, ConstGenParticlePtr p ) {
132  os << " ";
133  os.width(6);
134  os << p->id();
135  os.width(9);
136  os << p->pid() << " ";
137  os.width(9);
138  os.setf(ios::scientific, ios::floatfield);
139  os.setf(ios_base::showpos);
140 
141  const FourVector &momentum = p->momentum();
142 
143  os.width(9);
144  os << momentum.px() << ",";
145  os.width(9);
146  os << momentum.py() << ",";
147  os.width(9);
148  os << momentum.pz() << ",";
149  os.width(9);
150  os << momentum.e() << " ";
151  os.setf(ios::fmtflags(0), ios::floatfield);
152  os.unsetf(ios_base::showpos);
153  os.width(3);
154  os << p->status();
155 
156  ConstGenVertexPtr prod = p->production_vertex();
157 
158  if( prod ) {
159  os.width(6);
160  os << prod->id();
161  }
162 
163  os << endl;
164 }
165 void Print::line(std::ostream& os, const GenEvent &event, bool attributes) {
166  os <<"GenEvent: #" << event.event_number();
167  if(attributes) for (std::vector<std::string>::const_iterator s=event.attribute_names().begin();s!=event.attribute_names().end();++s)
168  os<<" "<<*s<<"="<<event.attribute_as_string(*s);
169 }
170 
171 void Print::line(std::ostream& os, ConstGenVertexPtr v, bool attributes) {
172  os << "GenVertex: " << v->id() << " stat: ";
173  os.width(3);
174  os << v->status();
175  os << " in: " << v->particles_in().size();
176  os.width(3);
177  os << " out: " << v->particles_out().size();
178 
179  const FourVector &pos = v->position();
180  os << " has_set_position: ";
181  if( v->has_set_position() ) os << "true";
182  else os << "false";
183 
184  os << " (X,cT): " << pos.x()<<", "<<pos.y()<<", "<<pos.z()<<", "<<pos.t();
185  if(attributes)for (std::vector<std::string>::const_iterator s= v->attribute_names().begin();s!= v->attribute_names().end();++s)
186  os<<" "<<*s<<"="<<v->attribute_as_string(*s);
187 
188 }
189 
190 void Print::line(std::ostream& os, ConstGenParticlePtr p, bool attributes) {
191 
192  os << "GenParticle: ";
193  os.width(3);
194  os << p->id() <<" PDGID: ";
195  os.width(5);
196  os << p->pid();
197 
198  // Find the current stream state
199  ios_base::fmtflags orig = os.flags();
200 
201  os.setf(ios::scientific, ios::floatfield);
202  os.setf(ios_base::showpos);
203  streamsize prec = os.precision();
204 
205  // Set precision
206  os.precision( 2 );
207 
208  const FourVector &momentum = p->momentum();
209 
210  os << " (P,E)=" << momentum.px()
211  << "," << momentum.py()
212  << "," << momentum.pz()
213  << "," << momentum.e();
214 
215  // Restore the stream state
216  os.flags(orig);
217  os.precision(prec);
218 
219  ConstGenVertexPtr prod = p->production_vertex();
220  ConstGenVertexPtr end = p->end_vertex();
221  int prod_vtx_id = (prod) ? prod->id() : 0;
222  int end_vtx_id = (end) ? end->id() : 0;
223 
224  os << " Stat: " << p->status()
225  << " PV: " << prod_vtx_id
226  << " EV: " << end_vtx_id
227  << " Attr: " << (*p).attribute_names().size();
228 
229  if(attributes)
230  {
231  std::vector<std::string> names =p->attribute_names();
232  for (auto ss: names)
233  os<<" "<<ss<<"="<<(*p).attribute_as_string(ss);
234  }
235 }
236 
237 void Print::line(std::ostream& os, shared_ptr<GenCrossSection> &cs) {
238  os << " GenCrossSection: " << cs->xsec(0)
239  << " " << cs->xsec_err(0)
240  << " " << cs->get_accepted_events()
241  << " " << cs->get_attempted_events();
242 }
243 
244 void Print::line(std::ostream& os, shared_ptr<GenHeavyIon> &hi) {
245  os << " GenHeavyIon: " << hi->Ncoll_hard
246  << " " << hi->Npart_proj
247  << " " << hi->Npart_targ
248  << " " << hi->Ncoll
249  << " " << hi->spectator_neutrons
250  << " " << hi->spectator_protons
251  << " " << hi->N_Nwounded_collisions
252  << " " << hi->Nwounded_N_collisions
253  << " " << hi->Nwounded_Nwounded_collisions
254  << " " << hi->impact_parameter
255  << " " << hi->event_plane_angle
256  << " " << hi->eccentricity
257  << " " << hi->sigma_inel_NN;
258 }
259 
260 void Print::line(std::ostream& os, shared_ptr<GenPdfInfo> &pi) {
261  os << " GenPdfInfo: " << pi->parton_id[0]
262  << " " << pi->parton_id[1]
263  << " " << pi->x[0]
264  << " " << pi->x[1]
265  << " " << pi->scale
266  << " " << pi->xf[0]
267  << " " << pi->xf[1]
268  << " " << pi->pdf_id[0]
269  << " " << pi->pdf_id[1];
270 }
271 
272 } // namespace HepMC3
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
Definition: GenEvent.cc:44
double t() const
Time component of position/displacement.
Definition: FourVector.h:76
bool is_zero() const
Check if the length of this vertex is zero.
Definition: FourVector.h:154
const Units::LengthUnit & length_unit() const
Get length unit.
Definition: GenEvent.h:143
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:71
double x() const
x-component of position/displacement
Definition: FourVector.h:61
static std::string name(MomentumUnit u)
Get name of momentum unit.
Definition: Units.h:56
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Definition: GenEvent.h:141
double e() const
Energy component of momentum.
Definition: FourVector.h:97
Stores event-related information.
Definition: GenEvent.h:42
Generic 4-vector.
Definition: FourVector.h:34
double px() const
x-component of momentum
Definition: FourVector.h:82
static void line(std::ostream &os, const GenEvent &event, bool attributes=false)
Print one-line info.
Definition: Print.cc:165
string attribute_as_string(const string &name, const int &id=0) const
Get attribute of any type as string.
Definition: GenEvent.cc:623
std::map< string, std::map< int, shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
Definition: GenEvent.h:229
double y() const
y-component of position/displacement
Definition: FourVector.h:66
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:87
double pz() const
z-component of momentum
Definition: FourVector.h:92
double py() const
y-component of momentum
Definition: FourVector.h:87
std::vector< string > attribute_names(const int &id=0) const
Get list of attribute names.
Definition: GenEvent.cc:463
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