HepMC Reference Documentation

HepMC

testHepMCIteration.cc.in

Use Matt's example_EventSelection along with example_UsingIterators to check HepMC iteration. Apply an event selection to the events in testHepMC.input Events containing a photon of pT > 25 GeV pass the selection. Use iterators on these events.

00001 
00002 // testHepMCIteration.cc.in
00003 //
00004 // garren@fnal.gov, May 2007
00005 // Use Matt's example_EventSelection along with example_UsingIterators 
00006 // to check HepMC iteration.
00007 // Apply an event selection to the events in testHepMC.input
00008 // Events containing a photon of pT > 25 GeV pass the selection.
00009 // Use iterators on these events.
00011 
00012 #include <list>
00013 
00014 #include "HepMC/IO_GenEvent.h"
00015 #include "HepMC/IO_AsciiParticles.h"
00016 #include "HepMC/GenEvent.h"
00017 #include "HepMC/GenRanges.h"
00018 
00019 // define methods and classes used by this test
00020 #include "IsGoodEvent.h"
00021 #include "testHepMCIteration.h"
00022 
00023 bool findW( HepMC::GenEvent* evt, std::ofstream& os);
00024 bool simpleIter ( HepMC::GenEvent* evt, std::ostream& os = std::cout );
00025 bool simpleIter2( HepMC::GenEvent* evt, std::ostream& os = std::cout );
00026 bool simpleIter3( HepMC::GenEvent* evt, std::ostream& os = std::cout );
00027 bool simpleIter4( HepMC::GenEvent* evt, std::ostream& os = std::cout );
00028 
00033 class PrintW {
00034 public:
00035     PrintW( std::ostream & os, int num ) : m_out( os ),m_event_num( num ) {}
00036     void operator()( HepMC::GenParticle* p ) { 
00037         if ( IsWBoson(p) ) {
00038             m_out << std::endl;
00039             m_out << "A W boson has been found in event: " << m_event_num << std::endl;
00040             p->print( m_out );
00041             // return all parents
00042             // we do this by pointing to the production vertex of the W 
00043             // particle and asking for all particle parents of that vertex
00044             m_out << "\t Its parents are: " << std::endl;
00045             if ( p->production_vertex() ) {
00046                 std::for_each( p->particles_in(HepMC::parents).begin(),
00047                                p->particles_in(HepMC::parents).end(), 
00048                                PrintParticle(m_out));
00049             }
00050 
00051             // return immediate children
00052             m_out << "\t\t" << "Its children are: " << std::endl;
00053             if ( p->end_vertex() ) {
00054                 std::for_each( p->particles_out(HepMC::children).begin(), 
00055                                p->particles_out(HepMC::children).end(), 
00056                                PrintChildren(m_out));
00057             }
00058 
00059             // return all descendants
00060             // we do this by pointing to the end vertex of the W 
00061             // particle and asking for all particle descendants of that vertex
00062             m_out << "\t\t Its descendants are: " << std::endl;
00063             if ( p->end_vertex() ) {
00064                 std::for_each( p->particles_out(HepMC::descendants).begin(), 
00065                                p->particles_out(HepMC::descendants).end(), 
00066                                PrintDescendants(m_out));
00067             }
00068         }       // if IsWBoson
00069     }
00070 private:
00071    std::ostream & m_out;
00072    int            m_event_num;
00073 };
00074 
00079 class PrintConstW {
00080 public:
00081     PrintConstW( std::ostream & os, int num ) : m_out( os ),m_event_num( num ) {}
00082     void operator()( HepMC::GenParticle* p ) { 
00083         if ( IsWBoson(p) ) {
00084             m_out << std::endl;
00085             m_out << "A W boson has been found in event: " << m_event_num << std::endl;
00086             p->print( m_out );
00087             // return all parents
00088             // we do this by pointing to the production vertex of the W 
00089             // particle and asking for all particle parents of that vertex
00090             m_out << "\t Its parents are: " << std::endl;
00091             if ( p->production_vertex() ) {
00092                 std::for_each( p->particles_in(HepMC::parents).begin(), 
00093                                p->particles_in(HepMC::parents).end(), 
00094                                PrintParticle(m_out));
00095             }
00096 
00097             // return immediate children
00098             m_out << "\t\t" << "Its children are: " << std::endl;
00099             if ( p->end_vertex() ) {
00100                 std::for_each( p->particles_out(HepMC::children).begin(), 
00101                                p->particles_out(HepMC::children).end(), 
00102                                PrintChildren(m_out));
00103             }
00104 
00105             // return all descendants
00106             // we do this by pointing to the end vertex of the W 
00107             // particle and asking for all particle descendants of that vertex
00108             m_out << "\t\t Its descendants are: " << std::endl;
00109             if ( p->end_vertex() ) {
00110                 std::for_each( p->particles_out(HepMC::descendants).begin(), 
00111                                p->particles_out(HepMC::descendants).end(), 
00112                                PrintDescendants(m_out));
00113             }
00114         }       // if IsWBoson
00115     }
00116 private:
00117    std::ostream & m_out;
00118    int            m_event_num;
00119 };
00120 
00121 int main() { 
00122     // declare an input strategy to read the data produced with the 
00123     // example_MyPythia
00124     HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in);
00125     // declare an instance of the event selection predicate
00126     IsGoodEvent is_good_event;
00127     // define some output streams
00128     std::ofstream os( "testHepMCIteration.out" );
00129     std::ofstream os2( "testHepMCIteration2.out" );
00130     std::ofstream os3( "testHepMCIteration3.out" );
00131     //........................................EVENT LOOP
00132     int icount=0;
00133     int num_good_events=0;
00134     HepMC::GenEvent* evt = ascii_in.read_next_event();
00135     HepMC::GenEvent* evcopy;
00136     while ( evt ) {
00137         icount++;
00138         if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00139                                       << " its # " << evt->event_number() 
00140                                       << std::endl;
00141         // icount of 100 should be the last event
00142         if ( icount==100 ) std::cout << "Processing Event Number " << icount
00143                                       << " its # " << evt->event_number() 
00144                                       << std::endl;
00145         evcopy = evt;
00146         if ( is_good_event(evcopy) ) {
00147             ++num_good_events;
00148             // simple iteration several different ways
00149             os << "Event " << evcopy->event_number() << " is good " << std::endl;
00150             simpleIter( evcopy, os );
00151             os2 << "Event " << evcopy->event_number() << " is good " << std::endl;
00152             simpleIter2( evcopy, os2 );
00153             os3 << "Event " << evcopy->event_number() << " is good " << std::endl;
00154             simpleIter2( evcopy, os3 );
00155             std::cout << "Event " << evcopy->event_number() << " is good " << std::endl;
00156             simpleIter3( evcopy );
00157             simpleIter4( evcopy );
00158             // test iterators
00159             findW( evcopy, os );
00160             // this is the same as findW except that we use the STL for_each algorithm
00161             std::for_each( evt->particles_begin(), evt->particles_end(), 
00162                            PrintW(os2,evcopy->event_number()));
00163             // repeat, using the const iterator
00164             std::for_each( evt->particles_begin(), evt->particles_end(), 
00165                            PrintConstW(os3,evcopy->event_number()));
00166         }
00167         evcopy->clear();
00168         
00169         // clean up and get next event
00170         delete evt;
00171         evt = ascii_in.read_next_event();
00172     }
00173     //........................................PRINT RESULT
00174     std::cout << num_good_events << " out of " << icount 
00175               << " processed events passed the cuts. Finished." << std::endl;
00176 }
00177 
00178 bool simpleIter( HepMC::GenEvent* evt, std::ostream& os )
00179 {
00180     // use GenEvent::vertex_iterator to fill a list of all 
00181     // vertices in the event
00182     std::list<HepMC::GenVertex*> allvertices;
00183     for ( HepMC::GenEvent::vertex_iterator v = evt->vertices_begin();
00184           v != evt->vertices_end(); ++v ) {
00185         allvertices.push_back(*v);
00186     }
00187 
00188     // fill a list of all final state particles in the event, by requiring
00189     // that each particle satisfyies the IsFinalState predicate
00190     IsFinalState isfinal;
00191     std::list<HepMC::GenParticle*> finalstateparticles;
00192     for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin();
00193           p != evt->particles_end(); ++p ) {
00194         if ( isfinal(*p) ) finalstateparticles.push_back(*p);
00195     }
00196 
00197     // print all photons in the event that satisfy the IsPhoton criteria
00198     os << "photons in event " << evt->event_number() << ":" << std::endl;
00199     for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin();
00200           p != evt->particles_end(); ++p ) {
00201         if ( IsPhoton(*p) ) (*p)->print( os );
00202     }
00203 
00204     return true;
00205 }
00206 
00207 bool simpleIter2( HepMC::GenEvent* evt, std::ostream& os )
00208 {
00209     // illustrates the use various helpful algorithms
00210 
00211     // use the STL copy algorithm to fill a list of all 
00212     // vertices in the event
00213     std::list<HepMC::GenVertex*> allvertices2;
00214     copy( evt->vertices_begin(), evt->vertices_end(), 
00215           back_inserter(allvertices2) );
00216 
00217     // fill a list of all final state particles in the event, by requiring
00218     // that each particle satisfyies the IsFinalState predicate
00219     // an STL-like algorithm called HepMC::copy_if is provided in the
00220     // GenEvent.h header to do this sort of operation more easily
00221     std::list<HepMC::GenParticle*> finalstateparticles2;
00222     HepMC::copy_if( evt->particles_begin(), evt->particles_end(), 
00223                     back_inserter(finalstateparticles2), IsFinalState() );
00224 
00225     // use the STL for_each algorithm to  
00226     // print all photons in the event that satisfy the IsPhoton criteria
00227     os << "photons in event " << evt->event_number() << ":" << std::endl;
00228     std::for_each(evt->particles_begin(), evt->particles_end(),
00229                   PrintPhoton(os));
00230 
00231     return true;
00232 }
00233 
00234 bool simpleIter3( HepMC::GenEvent* evt, std::ostream& os )
00235 {
00236     // very simple illustration of using GenEventVertexRange 
00237     // and GenEventParticleRange
00238     // NOTE that instead of creating this list, 
00239     // you can just use GenEventVertexRange as if it were the list
00240     std::list<HepMC::GenVertex*> allvertices;
00241     HepMC::GenEventVertexRange vc(*evt);
00242     for ( HepMC::GenEvent::vertex_iterator v = vc.begin(); v != vc.end(); ++v ) {
00243         allvertices.push_back(*v);
00244     }
00245 
00246     // fill a list of all final state particles in the event, by requiring
00247     // that each particle satisfyies the IsFinalState predicate
00248     IsFinalState isfinal;
00249     std::list<HepMC::GenParticle*> finalstateparticles;
00250     HepMC::GenEventParticleRange pc(*evt);
00251     for ( HepMC::GenEvent::particle_iterator p = pc.begin(); p != pc.end(); ++p ) {
00252         if ( isfinal(*p) ) finalstateparticles.push_back(*p);
00253     }
00254 
00255     // print all photons in the event that satisfy the IsPhoton criteria
00256     os << "photons in event " << evt->event_number() << ":" << std::endl;
00257     std::for_each(pc.begin(), pc.end(), PrintPhoton(os));
00258 
00259     return true;
00260 }
00261 
00262 bool simpleIter4( HepMC::GenEvent* evt, std::ostream& os )
00263 {
00264     // very simple illustration of using 
00265     // GenEvent::vertex_range(), which returns GenEventVertexRange, 
00266     // and GenEvent::particle_range(), which returns GenEventParticleRange
00267     // NOTE that instead of creating these lists, 
00268     // you can just use GenEvent::vertex_range() and GenEvent::particle_range()
00269     // as if they were a list
00270 
00271     std::list<HepMC::GenVertex*> allvertices;
00272     for ( HepMC::GenEvent::vertex_iterator v = evt->vertex_range().begin(); 
00273           v != evt->vertex_range().end(); ++v ) {
00274         allvertices.push_back(*v);
00275     }
00276 
00277     // fill a list of all final state particles in the event, by requiring
00278     // that each particle satisfyies the IsFinalState predicate
00279     IsFinalState isfinal;
00280     std::list<HepMC::GenParticle*> finalstateparticles;
00281     for ( HepMC::GenEvent::particle_iterator p = evt->particle_range().begin(); 
00282           p != evt->particle_range().end(); ++p ) {
00283         if ( isfinal(*p) ) finalstateparticles.push_back(*p);
00284     }
00285 
00286     // print all photons in the event that satisfy the IsPhoton criteria
00287     os << "photons in event " << evt->event_number() << ":" << std::endl;
00288     std::for_each(evt->particle_range().begin(), 
00289                   evt->particle_range().end(), 
00290                   PrintPhoton(os));
00291 
00292     return true;
00293 }
00294 
00295 bool findW( HepMC::GenEvent* evt, std::ofstream& os )
00296 {
00297     int num_W = 0;
00298     // use GenEvent::particle_iterator to find all W's in the event,
00299     // then 
00300     // (1) for each W user the GenVertex::particle_iterator with a range of
00301     //     parents to return and print the immediate mothers of these W's.
00302     // (2) for each W user the GenVertex::particle_iterator with a range of
00303     //     descendants to return and print all descendants of these W's.
00304     for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin();
00305           p != evt->particles_end(); ++p ) {
00306         if ( IsWBoson(*p) ) {
00307             ++num_W;
00308             os << std::endl;
00309             os << "A W boson has been found in event: " << evt->event_number() << std::endl;
00310             (*p)->print( os );
00311             // return all parents
00312             // we do this by pointing to the production vertex of the W 
00313             // particle and asking for all particle parents of that vertex
00314             os << "\t Its parents are: " << std::endl;
00315             if ( (*p)->production_vertex() ) {
00316                 for ( HepMC::GenVertex::particle_iterator mother 
00317                           = (*p)->production_vertex()->
00318                           particles_begin(HepMC::parents);
00319                       mother != (*p)->production_vertex()->
00320                           particles_end(HepMC::parents); 
00321                       ++mother ) {
00322                     os << "\t";
00323                     (*mother)->print( os );
00324                 }
00325             }
00326 
00327             // return immediate children
00328             os << "\t\t" << "Its children are: " << std::endl;
00329             if ( (*p)->end_vertex() ) {
00330                 for ( HepMC::GenVertex::particle_iterator child = 
00331                       (*p)->end_vertex()->particles_begin(HepMC::children); 
00332                       child != (*p)->end_vertex()->particles_end(HepMC::children); 
00333                       ++child ) {
00334                     // make a copy
00335                     HepMC::GenVertex::particle_iterator cp = child;
00336                     // use the copy and the original
00337                     os << "\t\t\t (id,barcode,status) " 
00338                        << (*cp)->pdg_id() << " " 
00339                        << (*child)->barcode() << " "
00340                        << (*cp)->status() << std::endl;
00341                 }
00342             }
00343 
00344             // return all descendants
00345             // we do this by pointing to the end vertex of the W 
00346             // particle and asking for all particle descendants of that vertex
00347             os << "\t\t Its descendants are: " << std::endl;
00348             if ( (*p)->end_vertex() ) {
00349                 for ( HepMC::GenVertex::particle_iterator des 
00350                           =(*p)->end_vertex()->
00351                           particles_begin(HepMC::descendants);
00352                       des != (*p)->end_vertex()->
00353                           particles_end(HepMC::descendants);
00354                       ++des ) {
00355                     os << "\t\t";
00356                     (*des)->print( os );
00357                 }
00358             }
00359         }       // if IsWBoson
00360     }   // end particle loop
00361     return true;
00362 }

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