![]() |
HepMC Reference DocumentationHepMC |
00001 00002 // testFlow.cc 00003 // 00004 // garren@fnal.gov, June 2009 00005 // based on example_BuildEventFromScratch.cc 00007 00008 #include <iostream> 00009 #include <fstream> 00010 #include <vector> 00011 00012 #include "HepMC/GenEvent.h" 00013 #include "HepMC/IO_GenEvent.h" 00014 00015 typedef std::vector<HepMC::GenParticle*> FlowVec; 00016 00017 int main() { 00018 // 00019 // In this example we will place the following event into HepMC "by hand" 00020 // 00021 // name status pdg_id parent Px Py Pz Energy Mass 00022 // 1 !p+! 3 2212 0,0 0.000 0.000 7000.000 7000.000 0.938 00023 // 2 !p+! 3 2212 0,0 0.000 0.000-7000.000 7000.000 0.938 00024 //========================================================================= 00025 // 3 !d! 3 1 1,1 0.750 -1.569 32.191 32.238 0.000 00026 // 4 !u~! 3 -2 2,2 -3.047 -19.000 -54.629 57.920 0.000 00027 // 5 !W-! 3 -24 1,2 1.517 -20.68 -20.605 85.925 80.799 00028 // 6 !gamma! 1 22 1,2 -3.813 0.113 -1.833 4.233 0.000 00029 // 7 !d! 1 1 5,5 -2.445 28.816 6.082 29.552 0.010 00030 // 8 !u~! 1 -2 5,5 3.962 -49.498 -26.687 56.373 0.006 00031 00032 // open an output file 00033 const char outfile[] = "testFlow.out"; 00034 std::ofstream os( outfile ); 00035 if( !os ) { 00036 std::cerr << "cannot open " << outfile << std::endl; 00037 exit(-1); 00038 } 00039 // declare several IO_GenEvent instances for comparison 00040 HepMC::IO_GenEvent xout1("testFlow.out1",std::ios::out); 00041 HepMC::IO_GenEvent xout2("testFlow.out2",std::ios::out); 00042 HepMC::IO_GenEvent xout3("testFlow.out3",std::ios::out); 00043 00044 int numbad = 0; 00045 00046 00047 // build the graph, which will look like 00048 // p7 # 00049 // p1 / # 00050 // \v1__p3 p5---v4 # 00051 // \_v3_/ \ # 00052 // / \ p8 # 00053 // v2__p4 \ # 00054 // / p6 # 00055 // p2 # 00056 // 00057 // define a flow pattern as p1 -> p3 -> p6 00058 // and p2 -> p4 -> p5 00059 // 00060 00061 // First create the event container, with Signal Process 20, event number 1 00062 // 00063 HepMC::GenEvent* evt = new HepMC::GenEvent( 20, 1 ); 00064 // 00065 // create vertex 1 and vertex 2, together with their inparticles 00066 HepMC::GenVertex* v1 = new HepMC::GenVertex(); 00067 evt->add_vertex( v1 ); 00068 HepMC::GenParticle* p1 = new HepMC::GenParticle( HepMC::FourVector(0,0,7000,7000), 00069 2212, 3 ); 00070 p1->set_flow(1,231); 00071 v1->add_particle_in( p1 ); 00072 HepMC::GenVertex* v2 = new HepMC::GenVertex(); 00073 evt->add_vertex( v2 ); 00074 HepMC::GenParticle* p2 = new HepMC::GenParticle( HepMC::FourVector(0,0,-7000,7000), 00075 2212, 3 ); 00076 p2->set_flow(1,243); 00077 v2->add_particle_in( p2 ); 00078 // 00079 // create the outgoing particles of v1 and v2 00080 HepMC::GenParticle* p3 = 00081 new HepMC::GenParticle( HepMC::FourVector(.750,-1.569,32.191,32.238), 00082 1, 3 ); 00083 p3->set_flow(1,231); 00084 v1->add_particle_out( p3 ); 00085 HepMC::GenParticle* p4 = 00086 new HepMC::GenParticle( HepMC::FourVector(-3.047,-19.,-54.629,57.920), 00087 -2, 3 ); 00088 p4->set_flow(1,243); 00089 v2->add_particle_out( p4 ); 00090 // 00091 // create v3 00092 HepMC::GenVertex* v3 = new HepMC::GenVertex(); 00093 evt->add_vertex( v3 ); 00094 v3->add_particle_in( p3 ); 00095 v3->add_particle_in( p4 ); 00096 HepMC::GenParticle* p6 = 00097 new HepMC::GenParticle( HepMC::FourVector(-3.813,0.113,-1.833,4.233 ), 00098 22, 1 ); 00099 p6->set_flow(1,231); 00100 v3->add_particle_out( p6 ); 00101 HepMC::GenParticle* p5 = 00102 new HepMC::GenParticle( HepMC::FourVector(1.517,-20.68,-20.605,85.925), 00103 -24, 3 ); 00104 p5->set_flow(1,243); 00105 v3->add_particle_out( p5 ); 00106 // 00107 // create v4 00108 HepMC::GenVertex* v4 = new HepMC::GenVertex(HepMC::FourVector(0.12,-0.3,0.05,0.004)); 00109 evt->add_vertex( v4 ); 00110 v4->add_particle_in( p5 ); 00111 HepMC::GenParticle* p7 = new HepMC::GenParticle( HepMC::FourVector(-2.445,28.816,6.082,29.552), 1,1 ); 00112 v4->add_particle_out( p7 ); 00113 HepMC::GenParticle* p8 = new HepMC::GenParticle( HepMC::FourVector(3.962,-49.498,-26.687,56.373), -2,1 ); 00114 v4->add_particle_out( p8 ); 00115 // 00116 // tell the event which vertex is the signal process vertex 00117 evt->set_signal_process_vertex( v3 ); 00118 // the event is complete, we now print it out 00119 evt->print( os ); 00120 00121 // look at the flow we created 00122 os << std::endl; 00123 FlowVec result1 = p1->flow().dangling_connected_partners( p1->flow().icode(1) ); 00124 FlowVec result2 = p1->flow().connected_partners( p1->flow().icode(1) ); 00125 FlowVec::iterator it; 00126 os << "dangling partners of particle " << p1->barcode() << std::endl; 00127 for( it = result1.begin(); it != result1.end(); ++it ) { 00128 os << (*it)->barcode() << " " ; 00129 os.width(8); 00130 os << (*it)->pdg_id() << " " << (*it)->flow(1) << std::endl; 00131 } 00132 os << "all partners of particle " << p1->barcode() << std::endl; 00133 for( it = result2.begin(); it != result2.end(); ++it ) { 00134 os << (*it)->barcode() << " " ; 00135 os.width(8); 00136 os << (*it)->pdg_id() << " " << (*it)->flow(1) << std::endl; 00137 } 00138 FlowVec result3 = p2->flow().dangling_connected_partners( p2->flow().icode(1) ); 00139 FlowVec result4 = p2->flow().connected_partners( p2->flow().icode(1) ); 00140 os << "dangling partners of particle " << p2->barcode() << std::endl; 00141 for( it = result3.begin(); it != result3.end(); ++it ) { 00142 os << (*it)->barcode() << " " ; 00143 os.width(8); 00144 os << (*it)->pdg_id() << " " << (*it)->flow(1) << std::endl; 00145 } 00146 os << "all partners of particle " << p2->barcode() << std::endl; 00147 for( it = result4.begin(); it != result4.end(); ++it ) { 00148 os << (*it)->barcode() << " " ; 00149 os.width(8); 00150 os << (*it)->pdg_id() << " " << (*it)->flow(1) << std::endl; 00151 } 00152 xout1 << evt; 00153 00154 // try changing and erasing flow 00155 p2->set_flow(2,345); 00156 xout2 << evt; 00157 FlowVec result5 = p2->flow().connected_partners( p2->flow().icode(1) ); 00158 if ( result4 != result5 ) { 00159 std::cerr << "ERROR: list of partners has changed after adding flow" << std::endl; 00160 ++numbad; 00161 } 00162 // the flow method returns a copy, 00163 // so we must set the flow again to change it 00164 HepMC::Flow f2 = p2->flow(); 00165 if( f2.erase(2) ) { 00166 p2->set_flow( f2 ); 00167 } else { 00168 std::cerr << "ERROR: first erase was NOT successful" << std::endl; 00169 ++numbad; 00170 } 00171 f2 = p2->flow(); 00172 if( f2.erase(2) ) { 00173 std::cerr << "ERROR: second erase was successful" << std::endl; 00174 } 00175 xout3 << evt; 00176 FlowVec result6 = p2->flow().connected_partners( p2->flow().icode(1) ); 00177 if ( result4 != result6 ) { 00178 std::cerr << "ERROR: list of partners has changed after removing flow" << std::endl; 00179 ++numbad; 00180 } 00181 00182 // now clean-up by deleteing all objects from memory 00183 // 00184 // deleting the event deletes all contained vertices, and all particles 00185 // contained in those vertices 00186 delete evt; 00187 00188 if( numbad > 0 ) std::cerr << numbad << " errors in testFlow" << std::endl; 00189 00190 return numbad; 00191 }