HepMC3 event record library
basic_tree.cc
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5 //
6 /// @example basic_tree.cc
7 /// @brief Basic example of building HepMC3 tree by hand
8 ///
9 /// Based on HepMC2/examples/example_BuildEventFromScratch.cc
10 
11 #include "HepMC3/GenEvent.h"
12 #include "HepMC3/GenVertex.h"
13 #include "HepMC3/GenParticle.h"
14 #include "HepMC3/Print.h"
15 #include "HepMC3/Selector.h"
16 
17 using namespace HepMC3;
18 
19 
20 /** Main program */
21 int main() {
22  //
23  // In this example we will place the following event into HepMC "by hand"
24  //
25  // name status pdg_id parent Px Py Pz Energy Mass
26  // 1 !p+! 3 2212 0,0 0.000 0.000 7000.000 7000.000 0.938
27  // 3 !p+! 3 2212 0,0 0.000 0.000-7000.000 7000.000 0.938
28  //=========================================================================
29  // 2 !d! 3 1 1,1 0.750 -1.569 32.191 32.238 0.000
30  // 4 !u~! 3 -2 2,2 -3.047 -19.000 -54.629 57.920 0.000
31  // 5 !W-! 3 -24 1,2 1.517 -20.68 -20.605 85.925 80.799
32  // 6 !gamma! 1 22 1,2 -3.813 0.113 -1.833 4.233 0.000
33  // 7 !d! 1 1 5,5 -2.445 28.816 6.082 29.552 0.010
34  // 8 !u~! 1 -2 5,5 3.962 -49.498 -26.687 56.373 0.006
35 
36  // now we build the graph, which will looks like
37  // p7 #
38  // p1 / #
39  // \v1__p3 p5---v4 #
40  // \_v3_/ \ #
41  // / \ p8 #
42  // v2__p4 \ #
43  // / p6 #
44  // p2 #
45  // #
46  GenEvent evt(Units::GEV,Units::MM);
47 
48  // px py pz e pdgid status
49  GenParticlePtr p1 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, 7000.0, 7000.0 ),2212, 3 );
50  GenParticlePtr p2 = std::make_shared<GenParticle>( FourVector( 0.750, -1.569, 32.191, 32.238), 1, 3 );
51  GenParticlePtr p3 = std::make_shared<GenParticle>( FourVector( 0.0, 0.0, -7000.0, 7000.0 ),2212, 3 );
52  GenParticlePtr p4 = std::make_shared<GenParticle>( FourVector(-3.047,-19.0, -54.629, 57.920), -2, 3 );
53 
54  GenVertexPtr v1 = std::make_shared<GenVertex>();
55  v1->add_particle_in (p1);
56  v1->add_particle_out(p2);
57  evt.add_vertex(v1);
58 
59  // Set vertex status if needed
60  v1->set_status(4);
61 
62  GenVertexPtr v2 = std::make_shared<GenVertex>();
63  v2->add_particle_in (p3);
64  v2->add_particle_out(p4);
65  evt.add_vertex(v2);
66 
67  GenVertexPtr v3 = std::make_shared<GenVertex>();
68  v3->add_particle_in(p2);
69  v3->add_particle_in(p4);
70  evt.add_vertex(v3);
71 
72  GenParticlePtr p5 = std::make_shared<GenParticle>( FourVector(-3.813, 0.113, -1.833, 4.233), 22, 1 );
73  GenParticlePtr p6 = std::make_shared<GenParticle>( FourVector( 1.517,-20.68, -20.605,85.925), -24, 3 );
74 
75  v3->add_particle_out(p5);
76  v3->add_particle_out(p6);
77 
78  GenVertexPtr v4 =std:: make_shared<GenVertex>();
79  v4->add_particle_in (p6);
80  evt.add_vertex(v4);
81 
82  GenParticlePtr p7 = std::make_shared<GenParticle>( FourVector(-2.445, 28.816, 6.082,29.552), 1, 1 );
83  GenParticlePtr p8 = std::make_shared<GenParticle>( FourVector( 3.962,-49.498,-26.687,56.373), -2, 1 );
84 
85  v4->add_particle_out(p7);
86  v4->add_particle_out(p8);
87 
88  //
89  // Example of adding event attributes
90  //
91  std::shared_ptr<GenPdfInfo> pdf_info = std::make_shared<GenPdfInfo>();
92  evt.add_attribute("GenPdfInfo",pdf_info);
93 
94  pdf_info->set(1,2,3.4,5.6,7.8,9.0,1.2,3,4);
95 
96  std::shared_ptr<GenHeavyIon> heavy_ion = std::make_shared<GenHeavyIon>();
97  evt.add_attribute("GenHeavyIon",heavy_ion);
98 
99  heavy_ion->set( 1,2,3,4,5,6,7,8,9,0.1,2.3,4.5,6.7);
100 
101  std::shared_ptr<GenCrossSection> cross_section = std::make_shared<GenCrossSection>();
102  evt.add_attribute("GenCrossSection",cross_section);
103 
104  cross_section->set_cross_section(1.2,3.4);
105 
106  //
107  // Example of manipulating the attributes
108  //
109 
110  std::cout << std::endl << " Manipulating attributes:" << std::endl;
111 
112  // get attribute
113  std::shared_ptr<GenCrossSection> cs = evt.attribute<GenCrossSection>("GenCrossSection");
114 
115  // if attribute exists - do something with it
116  if(cs) {
117  cs->set_cross_section(-1.0,0.0);
118  Print::line(cs);
119  }
120  else std::cout << "Problem accessing attribute!" <<std::endl;
121 
122  // remove attribute
123  evt.remove_attribute("GenCrossSection");
124  evt.remove_attribute("GenCrossSection"); // This call will do nothing
125 
126  // now this should be null
127  cs = evt.attribute<GenCrossSection>("GenCrossSection");
128 
129  if(!cs)std::cout << "Successfully removed attribute" <<std::endl;
130  else std::cout << "Problem removing attribute!" <<std::endl;
131 
132  //
133  // Example of adding attributes and finding particles with attributes
134  //
135 
136  std::shared_ptr<Attribute> tool1 = std::make_shared<IntAttribute>(1);
137  std::shared_ptr<Attribute> tool999 = std::make_shared<IntAttribute>(999);
138  std::shared_ptr<Attribute> test_attribute = std::make_shared<StringAttribute>("test attribute");
139  std::shared_ptr<Attribute> test_attribute2 = std::make_shared<StringAttribute>("test attribute2");
140 
141  p2->add_attribute( "tool" , tool1 );
142  p2->add_attribute( "other" , test_attribute );
143 
144  p4->add_attribute( "tool" , tool1 );
145 
146  p6->add_attribute( "tool" , tool999 );
147  p6->add_attribute( "other" , test_attribute2 );
148 
149  v3->add_attribute( "vtx_att" , test_attribute );
150  v4->add_attribute( "vtx_att" , test_attribute2 );
151 /* TODO: Make this code portable
152 
153  std::cout << std::endl << "Find all particles with attribute 'tool' "<< std::endl;
154  std::cout << "(should return particles 2,4,6):" << std::endl;
155 
156  /// @todo can we add some utility funcs to simplify creation of Features from Attributes and check they exist.
157  /// Features and Attributes are quite similar concepts anyway, can they be unified (but Features can also be
158  /// non-attribute-like e.g. pT, rapidity or any quantity it is possible to obtain from a particle)
159 
160  for(ConstGenParticlePtr p: applyFilter(Selector::ATTRIBUTE("tool"), evt.particles())){
161  Print::line(p);
162  }
163 
164  std::cout <<std::endl << "Find all particles with attribute 'tool' equal 1 "<< std::endl;
165  std::cout << "(should return particles 2,4):" <<std::endl;
166 
167  for(ConstGenParticlePtr p: applyFilter(Selector::ATTRIBUTE("tool") && Selector::ATTRIBUTE("tool") == tool1, evt.particles())){
168  Print::line(p);
169  }
170 
171  std::cout << std::endl << "Find all particles with a string attribute 'other' equal 'test attribute' "<< std::endl;
172  std::cout << "(should return particle 2):" << std::endl;
173 
174 
175  for(ConstGenParticlePtr p: applyFilter(Selector::ATTRIBUTE("other") && Selector::ATTRIBUTE("other") == "test_attribute", evt.particles())){
176  Print::line(p);
177  }
178 */
179 
180  std::cout << std::endl << "Offsetting event position by 5,5,5,5" << std::endl;
181 
182  evt.shift_position_by( FourVector(5,5,5,5) );
183 
184  Print::listing(evt);
185 
186  std::cout << std::endl << "Printing full content of the GenEvent object " << std::endl
187  << "(including particles and vertices in one-line format):" << std::endl << std::endl;
188 
189  Print::content(evt);
190 
191  std::cout <<std::endl << "Now: removing particle with id 6 and printing again:" <<std::endl <<std::endl;
192  evt.remove_particle(p6);
193 
194  Print::listing(evt);
195  Print::content(evt);
196 
197  std::cout <<std::endl << "Now: removing beam particles, leaving an empty event" <<std::endl <<std::endl;
198  evt.remove_particles( evt.beams() );
199 
200  Print::listing(evt);
201  Print::content(evt);
202  return 0;
203 }
definition of /b Selector class
Definition of class GenParticle.
Definition of class GenVertex.
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:50
void set_cross_section(const double &xs, const double &xs_err, const long &n_acc=-1, const long &n_att=-1)
Set all fields.
Stores event-related information.
Definition: GenEvent.h:41
Generic 4-vector.
Definition: FourVector.h:35
static void line(std::ostream &os, const GenEvent &event, bool attributes=false)
Print one-line info.
Definition: Print.cc:202
int main(int argc, char **argv)
Definition of class GenEvent.
Stores additional information about cross-section.
static void content(std::ostream &os, const GenEvent &event)
Print content of all GenEvent containers.
Definition: Print.cc:17