StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testFlow.cc
1 // testFlow.cc
3 //
4 // garren@fnal.gov, June 2009
5 // based on example_BuildEventFromScratch.cc
7 
8 #include <iostream>
9 #include <fstream>
10 #include <vector>
11 
12 #include "HepMC/GenEvent.h"
13 #include "HepMC/IO_GenEvent.h"
14 
15 typedef std::vector<HepMC::GenParticle*> FlowVec;
16 
17 int main() {
18  //
19  // In this example we will place the following event into HepMC "by hand"
20  //
21  // name status pdg_id parent Px Py Pz Energy Mass
22  // 1 !p+! 3 2212 0,0 0.000 0.000 7000.000 7000.000 0.938
23  // 2 !p+! 3 2212 0,0 0.000 0.000-7000.000 7000.000 0.938
24  //=========================================================================
25  // 3 !d! 3 1 1,1 0.750 -1.569 32.191 32.238 0.000
26  // 4 !u~! 3 -2 2,2 -3.047 -19.000 -54.629 57.920 0.000
27  // 5 !W-! 3 -24 1,2 1.517 -20.68 -20.605 85.925 80.799
28  // 6 !gamma! 1 22 1,2 -3.813 0.113 -1.833 4.233 0.000
29  // 7 !d! 1 1 5,5 -2.445 28.816 6.082 29.552 0.010
30  // 8 !u~! 1 -2 5,5 3.962 -49.498 -26.687 56.373 0.006
31 
32  // open an output file
33  const char outfile[] = "testFlow.out";
34  std::ofstream os( outfile );
35  if( !os ) {
36  std::cerr << "cannot open " << outfile << std::endl;
37  exit(-1);
38  }
39  // declare several IO_GenEvent instances for comparison
40  HepMC::IO_GenEvent xout1("testFlow.out1",std::ios::out);
41  HepMC::IO_GenEvent xout2("testFlow.out2",std::ios::out);
42  HepMC::IO_GenEvent xout3("testFlow.out3",std::ios::out);
43  // output streams for copy test
44  std::ofstream xout4( "testFlow.out4" );
45  std::ofstream xout5( "testFlow.out5" );
46 
47  int numbad = 0;
48 
49 
50  // build the graph, which will look like
51  // p7 #
52  // p1 / #
53  // \v1__p3 p5---v4 #
54  // \_v3_/ \ #
55  // / \ p8 #
56  // v2__p4 \ #
57  // / p6 #
58  // p2 #
59  //
60  // define a flow pattern as p1 -> p3 -> p6
61  // and p2 -> p4 -> p5
62  //
63 
64  // First create the event container, with Signal Process 20, event number 1
65  //
66  HepMC::GenEvent* evt = new HepMC::GenEvent( 20, 1 );
67  evt->use_units(HepMC::Units::GEV, HepMC::Units::MM);
68  //
69  // create vertex 1 and vertex 2, together with their inparticles
71  evt->add_vertex( v1 );
73  2212, 3 );
74  p1->set_flow(1,231);
75  v1->add_particle_in( p1 );
77  evt->add_vertex( v2 );
78  HepMC::GenParticle* p2 = new HepMC::GenParticle( HepMC::FourVector(0,0,-7000,7000),
79  2212, 3 );
80  p2->set_flow(1,243);
81  v2->add_particle_in( p2 );
82  //
83  // create the outgoing particles of v1 and v2
84  HepMC::GenParticle* p3 =
85  new HepMC::GenParticle( HepMC::FourVector(.750,-1.569,32.191,32.238),
86  1, 3 );
87  p3->set_flow(1,231);
88  v1->add_particle_out( p3 );
89  HepMC::GenParticle* p4 =
90  new HepMC::GenParticle( HepMC::FourVector(-3.047,-19.,-54.629,57.920),
91  -2, 3 );
92  p4->set_flow(1,243);
93  v2->add_particle_out( p4 );
94  //
95  // create v3
97  evt->add_vertex( v3 );
98  v3->add_particle_in( p3 );
99  v3->add_particle_in( p4 );
100  HepMC::GenParticle* p6 =
101  new HepMC::GenParticle( HepMC::FourVector(-3.813,0.113,-1.833,4.233 ),
102  22, 1 );
103  p6->set_flow(1,231);
104  v3->add_particle_out( p6 );
105  HepMC::GenParticle* p5 =
106  new HepMC::GenParticle( HepMC::FourVector(1.517,-20.68,-20.605,85.925),
107  -24, 3 );
108  p5->set_flow(1,243);
109  v3->add_particle_out( p5 );
110  //
111  // create v4
112  HepMC::GenVertex* v4 = new HepMC::GenVertex(HepMC::FourVector(0.12,-0.3,0.05,0.004));
113  evt->add_vertex( v4 );
114  v4->add_particle_in( p5 );
115  HepMC::GenParticle* p7 = new HepMC::GenParticle( HepMC::FourVector(-2.445,28.816,6.082,29.552), 1,1 );
116  v4->add_particle_out( p7 );
117  HepMC::GenParticle* p8 = new HepMC::GenParticle( HepMC::FourVector(3.962,-49.498,-26.687,56.373), -2,1 );
118  v4->add_particle_out( p8 );
119  //
120  // tell the event which vertex is the signal process vertex
121  evt->set_signal_process_vertex( v3 );
122  // the event is complete, we now print it out
123  evt->print( os );
124 
125  // look at the flow we created
126  os << std::endl;
127  FlowVec result1 = p1->flow().dangling_connected_partners( p1->flow().icode(1) );
128  FlowVec result2 = p1->flow().connected_partners( p1->flow().icode(1) );
129  FlowVec::iterator it;
130  os << "dangling partners of particle " << p1->barcode() << std::endl;
131  for( it = result1.begin(); it != result1.end(); ++it ) {
132  os << (*it)->barcode() << " " ;
133  os.width(8);
134  os << (*it)->pdg_id() << " " << (*it)->flow(1) << std::endl;
135  }
136  os << "all partners of particle " << p1->barcode() << std::endl;
137  for( it = result2.begin(); it != result2.end(); ++it ) {
138  os << (*it)->barcode() << " " ;
139  os.width(8);
140  os << (*it)->pdg_id() << " " << (*it)->flow(1) << std::endl;
141  }
142  FlowVec result3 = p2->flow().dangling_connected_partners( p2->flow().icode(1) );
143  FlowVec result4 = p2->flow().connected_partners( p2->flow().icode(1) );
144  os << "dangling partners of particle " << p2->barcode() << std::endl;
145  for( it = result3.begin(); it != result3.end(); ++it ) {
146  os << (*it)->barcode() << " " ;
147  os.width(8);
148  os << (*it)->pdg_id() << " " << (*it)->flow(1) << std::endl;
149  }
150  os << "all partners of particle " << p2->barcode() << std::endl;
151  for( it = result4.begin(); it != result4.end(); ++it ) {
152  os << (*it)->barcode() << " " ;
153  os.width(8);
154  os << (*it)->pdg_id() << " " << (*it)->flow(1) << std::endl;
155  }
156  // write event
157  xout1 << evt;
158  // testing bug #73987 - flow not copied
159  // call the write method directly
160  evt->write(xout4);
161  // make a copy and write it
162  HepMC::GenEvent(*evt).write(xout5);
163 
164  // try changing and erasing flow
165  p2->set_flow(2,345);
166  xout2 << evt;
167  FlowVec result5 = p2->flow().connected_partners( p2->flow().icode(1) );
168  if ( result4 != result5 ) {
169  std::cerr << "ERROR: list of partners has changed after adding flow" << std::endl;
170  ++numbad;
171  }
172  // the flow method returns a copy,
173  // so we must set the flow again to change it
174  HepMC::Flow f2 = p2->flow();
175  if( f2.erase(2) ) {
176  p2->set_flow( f2 );
177  } else {
178  std::cerr << "ERROR: first erase was NOT successful" << std::endl;
179  ++numbad;
180  }
181  f2 = p2->flow();
182  if( f2.erase(2) ) {
183  std::cerr << "ERROR: second erase was successful" << std::endl;
184  }
185  xout3 << evt;
186  FlowVec result6 = p2->flow().connected_partners( p2->flow().icode(1) );
187  if ( result4 != result6 ) {
188  std::cerr << "ERROR: list of partners has changed after removing flow" << std::endl;
189  ++numbad;
190  }
191 
192  // now clean-up by deleteing all objects from memory
193  //
194  // deleting the event deletes all contained vertices, and all particles
195  // contained in those vertices
196  delete evt;
197 
198  if( numbad > 0 ) std::cerr << numbad << " errors in testFlow" << std::endl;
199 
200  return numbad;
201 }
void set_signal_process_vertex(GenVertex *)
set pointer to the vertex containing the signal process
Definition: GenEvent.h:747
void add_particle_in(GenParticle *inparticle)
add incoming particle
Definition: GenVertex.cc:273
int barcode() const
particle barcode
Definition: GenParticle.h:252
void set_flow(const Flow &f)
set particle flow
Definition: GenParticle.h:238
std::vector< HepMC::GenParticle * > connected_partners(int code, int code_index=1, int num_indices=2) const
Definition: Flow.cc:38
GenVertex contains information about decay vertices.
Definition: GenVertex.h:52
The GenEvent class is the core of HepMC.
Definition: GenEvent.h:155
IO_GenEvent also deals with HeavyIon and PdfInfo.
Definition: IO_GenEvent.h:63
void add_particle_out(GenParticle *outparticle)
add outgoing particle
Definition: GenVertex.cc:284
bool add_vertex(GenVertex *vtx)
adds to evt and adopts
Definition: GenEvent.cc:334
std::ostream & write(std::ostream &)
void print(std::ostream &ostr=std::cout) const
dumps to ostr
Definition: GenEvent.cc:277
const Flow & flow() const
particle flow
Definition: GenParticle.h:223
std::vector< HepMC::GenParticle * > dangling_connected_partners(int code, int code_index=1, int num_indices=2) const
Definition: Flow.cc:108
FourVector is a simple representation of a physics 4 vector.
Definition: SimpleVector.h:42
bool erase(int code_index)
empty flow pattern container
Definition: Flow.h:180
void use_units(Units::MomentumUnit, Units::LengthUnit)
Definition: GenEvent.h:856
int icode(int code_index=1) const
flow code
Definition: Flow.h:163
The GenParticle class contains information about generated particles.
Definition: GenParticle.h:60
The flow object.
Definition: Flow.h:66