HepMC Reference Documentation

HepMC

testFlow.cc

Use a modified example_BuildEventFromScratch to test Flow

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     // output streams for copy test
00044     std::ofstream xout4( "testFlow.out4" );
00045     std::ofstream xout5( "testFlow.out5" );
00046  
00047     int numbad = 0;
00048 
00049 
00050     // build the graph, which will look like
00051     //                       p7                   #
00052     // p1                   /                     #
00053     //   \v1__p3      p5---v4                     #
00054     //         \_v3_/       \                     #
00055     //         /    \        p8                   #
00056     //    v2__p4     \                            #
00057     //   /            p6                          #
00058     // p2                                         #
00059     //
00060     // define a flow pattern as  p1 -> p3 -> p6
00061     //                       and p2 -> p4 -> p5
00062     //
00063 
00064     // First create the event container, with Signal Process 20, event number 1
00065     //
00066     HepMC::GenEvent* evt = new HepMC::GenEvent( 20, 1 );
00067     evt->use_units(HepMC::Units::GEV, HepMC::Units::MM);
00068     //
00069     // create vertex 1 and vertex 2, together with their inparticles
00070     HepMC::GenVertex* v1 = new HepMC::GenVertex();
00071     evt->add_vertex( v1 );
00072     HepMC::GenParticle* p1 = new HepMC::GenParticle( HepMC::FourVector(0,0,7000,7000),
00073                                        2212, 3 );
00074     p1->set_flow(1,231);
00075     v1->add_particle_in( p1 );
00076     HepMC::GenVertex* v2 = new HepMC::GenVertex();
00077     evt->add_vertex( v2 );
00078     HepMC::GenParticle* p2 = new HepMC::GenParticle( HepMC::FourVector(0,0,-7000,7000),
00079                                        2212, 3 );
00080     p2->set_flow(1,243);
00081     v2->add_particle_in( p2 );
00082     //
00083     // create the outgoing particles of v1 and v2
00084     HepMC::GenParticle* p3 = 
00085         new HepMC::GenParticle( HepMC::FourVector(.750,-1.569,32.191,32.238),
00086                                 1, 3 );
00087     p3->set_flow(1,231);
00088     v1->add_particle_out( p3 );
00089     HepMC::GenParticle* p4 = 
00090         new HepMC::GenParticle( HepMC::FourVector(-3.047,-19.,-54.629,57.920),
00091                                 -2, 3 );
00092     p4->set_flow(1,243);
00093     v2->add_particle_out( p4 );
00094     //
00095     // create v3
00096     HepMC::GenVertex* v3 = new HepMC::GenVertex();
00097     evt->add_vertex( v3 );
00098     v3->add_particle_in( p3 );
00099     v3->add_particle_in( p4 );
00100     HepMC::GenParticle* p6 = 
00101           new HepMC::GenParticle( HepMC::FourVector(-3.813,0.113,-1.833,4.233 ),
00102                                   22, 1 );
00103     p6->set_flow(1,231);
00104     v3->add_particle_out( p6 );
00105     HepMC::GenParticle* p5 = 
00106         new HepMC::GenParticle( HepMC::FourVector(1.517,-20.68,-20.605,85.925),
00107                                 -24, 3 );
00108     p5->set_flow(1,243);
00109     v3->add_particle_out( p5 );
00110     //
00111     // create v4
00112     HepMC::GenVertex* v4 = new HepMC::GenVertex(HepMC::FourVector(0.12,-0.3,0.05,0.004));
00113     evt->add_vertex( v4 );
00114     v4->add_particle_in( p5 );
00115     HepMC::GenParticle* p7 = new HepMC::GenParticle( HepMC::FourVector(-2.445,28.816,6.082,29.552), 1,1 );
00116     v4->add_particle_out( p7 );
00117     HepMC::GenParticle* p8 = new HepMC::GenParticle( HepMC::FourVector(3.962,-49.498,-26.687,56.373), -2,1 );
00118     v4->add_particle_out( p8 );
00119     //    
00120     // tell the event which vertex is the signal process vertex
00121     evt->set_signal_process_vertex( v3 );
00122     // the event is complete, we now print it out
00123     evt->print( os );
00124     
00125     // look at the flow we created
00126     os << std::endl;
00127     FlowVec result1 = p1->flow().dangling_connected_partners( p1->flow().icode(1) );
00128     FlowVec result2 = p1->flow().connected_partners( p1->flow().icode(1) );
00129     FlowVec::iterator it;
00130     os << "dangling partners of particle " << p1->barcode() << std::endl;
00131     for( it = result1.begin(); it != result1.end(); ++it ) {
00132       os << (*it)->barcode() << " " ;
00133       os.width(8);
00134       os << (*it)->pdg_id() << " " << (*it)->flow(1)  << std::endl;
00135     }
00136     os << "all partners of particle " << p1->barcode() << std::endl;
00137     for( it = result2.begin(); it != result2.end(); ++it ) {
00138       os << (*it)->barcode() << " " ;
00139       os.width(8);
00140       os << (*it)->pdg_id() << " " << (*it)->flow(1)  << std::endl;
00141     }
00142     FlowVec result3 = p2->flow().dangling_connected_partners( p2->flow().icode(1) );
00143     FlowVec result4 = p2->flow().connected_partners( p2->flow().icode(1) );
00144     os << "dangling partners of particle " << p2->barcode() << std::endl;
00145     for( it = result3.begin(); it != result3.end(); ++it ) {
00146       os << (*it)->barcode() << " " ;
00147       os.width(8);
00148       os << (*it)->pdg_id() << " " << (*it)->flow(1)  << std::endl;
00149     }
00150     os << "all partners of particle " << p2->barcode() << std::endl;
00151     for( it = result4.begin(); it != result4.end(); ++it ) {
00152       os << (*it)->barcode() << " " ;
00153       os.width(8);
00154       os << (*it)->pdg_id() << " " << (*it)->flow(1)  << std::endl;
00155     }
00156     // write event
00157     xout1 << evt;
00158     // testing bug #73987 - flow not copied
00159     // call the write method directly
00160     evt->write(xout4);
00161     // make a copy and write it
00162     HepMC::GenEvent(*evt).write(xout5);
00163 
00164     // try changing and erasing flow
00165     p2->set_flow(2,345);
00166             xout2 << evt;
00167     FlowVec result5 = p2->flow().connected_partners( p2->flow().icode(1) );
00168     if ( result4 != result5 ) {
00169         std::cerr << "ERROR: list of partners has changed after adding flow" << std::endl;
00170         ++numbad;
00171     }
00172     // the flow method returns a copy,
00173     // so we must set the flow again to change it
00174     HepMC::Flow f2 = p2->flow();
00175     if( f2.erase(2) ) {
00176         p2->set_flow( f2 );
00177     } else {
00178         std::cerr << "ERROR: first erase was NOT successful" << std::endl;
00179         ++numbad;
00180     }
00181     f2 = p2->flow();
00182     if( f2.erase(2) ) {
00183         std::cerr << "ERROR: second erase was successful" << std::endl;
00184     }
00185             xout3 << evt;
00186     FlowVec result6 = p2->flow().connected_partners( p2->flow().icode(1) );
00187     if ( result4 != result6 ) {
00188         std::cerr << "ERROR: list of partners has changed after removing flow" << std::endl;
00189         ++numbad;
00190     }
00191 
00192     // now clean-up by deleteing all objects from memory
00193     //
00194     // deleting the event deletes all contained vertices, and all particles
00195     // contained in those vertices
00196     delete evt;
00197     
00198     if( numbad > 0 ) std::cerr << numbad << " errors in testFlow" << std::endl;
00199 
00200     return numbad;
00201 }

Generated on Fri Feb 17 00:31:25 2012 for HepMC by  doxygen 1.4.7