HepMC Reference Documentation

HepMC

testHepMC.cc.in

based on example_EventSelection Apply an event selection to the events in testHepMC.input Events containing a photon of pT > 25 GeV pass the selection and are written to "testHepMC.out" Add arbitrary PDF information to the good events Also write events using IO_AsciiParticles

00001 
00002 // testHepMC.cc.in
00003 //
00004 // garren@fnal.gov, March 2006
00005 // based on example_EventSelection
00006 // Apply an event selection to the events in testHepMC.input
00007 // Events containing a photon of pT > 25 GeV pass the selection and are
00008 // written to "testHepMC.out"
00009 // Add arbitrary PDF information to the good events
00010 // Also write events using IO_AsciiParticles 
00012 //
00013 
00014 #include "HepMC/GenEvent.h"
00015 #include "HepMC/IO_GenEvent.h"
00016 #include "HepMC/IO_Ascii.h"
00017 #include "HepMC/IO_AsciiParticles.h"
00018 
00019 // define methods and classes used by this test
00020 #include "IsGoodEvent.h"
00021 
00022 void read_testIOGenEvent();
00023 void read_testAscii();
00024 void read_variousFormats();
00025 
00026 double findPiZero( HepMC::GenEvent* );
00027 
00028 int main() { 
00029     read_testIOGenEvent();
00030     read_testAscii();
00031     read_variousFormats();
00032     return 0;
00033 }
00034 
00035 void read_testIOGenEvent()
00036 {
00037     std::cout << std::endl;
00038     std::cout << "basic IO_GenEvent input and output" << std::endl;
00039     // declare an input strategy to read the data produced with the 
00040     // example_MyPythia - units are GeV and mm
00041     HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in);
00042     ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00043     // declare another IO_GenEvent for writing out the good events
00044     HepMC::IO_GenEvent ascii_out("testHepMC.out",std::ios::out);
00045     // declare an IO_AsciiParticle for output
00046     HepMC::IO_AsciiParticles particle_out("testHepMCParticle.out",std::ios::out);
00047     // declare an instance of the event selection predicate
00048     IsGoodEvent is_good_event;
00049     //........................................EVENT LOOP
00050     int icount=0;
00051     int num_good_events=0;
00052     HepMC::GenEvent* evt = ascii_in.read_next_event();
00053     while ( evt ) {
00054         icount++;
00055         if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00056                                       << " its # " << evt->event_number() 
00057                                       << std::endl;
00058         if ( is_good_event(evt) ) {
00059             ascii_out << evt;
00060             particle_out << evt;
00061             ++num_good_events;
00062         }
00063         
00064         // clean up and get next event
00065         delete evt;
00066         ascii_in >> evt;
00067     }
00068     //........................................PRINT RESULT
00069     std::cout << num_good_events << " out of " << icount 
00070               << " processed events passed the cuts. Finished." << std::endl;
00071 }
00072 
00073 void read_testAscii()
00074 {
00075     std::cout << std::endl;
00076     std::cout << "read file written with IO_Ascii" << std::endl;
00077     // declare an input strategy to read the data produced with the 
00078     // example_MyPythia - units are GeV and mm
00079     HepMC::IO_GenEvent ascii_in("@srcdir@/testAscii.input",std::ios::in);
00080     ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00081     if ( ascii_in.rdstate() == std::ios::failbit ) {
00082         std::cerr << "ERROR input file @srcdir@/testAscii.input is needed "
00083                   << "and does not exist.  Exit." << std::endl;
00084         return;
00085     }
00086     // use IO_Ascii because we are checking against the original input file 
00087     HepMC::IO_Ascii ascii_out("testIOAscii.dat",std::ios::out);
00088     if ( ascii_out.rdstate() == std::ios::failbit ) {
00089         std::cerr << "ERROR opening output file testAscii.dat.  Exit."
00090                   << std::endl;
00091         return;
00092     }
00093     //........................................EVENT LOOP
00094     int icount=0;
00095     HepMC::GenEvent* evt = ascii_in.read_next_event();
00096     while ( evt ) {
00097         icount++;
00098         if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00099                                       << " its # " << evt->event_number() 
00100                                       << std::endl;
00101         ascii_out << evt;
00102 
00103         // clean up and get next event
00104         delete evt;
00105         ascii_in >> evt;
00106     }
00107     //........................................PRINT RESULT
00108     std::cout << icount << " events processed. Finished." << std::endl;
00109 }
00110 
00111 void read_variousFormats()
00112 {
00113     std::cout << std::endl;
00114     std::cout << "process varied input" << std::endl;
00115     // declare an input strategy 
00116     HepMC::IO_GenEvent ascii_in("@srcdir@/testHepMCVarious.input",std::ios::in);
00117     ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00118     // declare another IO_GenEvent for writing out the good events
00119     HepMC::IO_GenEvent ascii_out("testHepMCVarious.out",std::ios::out);
00120     //........................................EVENT LOOP
00121     int icount=0;
00122     HepMC::GenEvent* evt = ascii_in.read_next_event();
00123     while ( evt ) {
00124         icount++;
00125         double pim;
00126         std::cout << "Processing Event Number " << icount
00127                   << " its # " << evt->event_number() 
00128                   << std::endl;
00129         ascii_out << evt;
00130         // units should be unknown
00131         evt->write_units();
00132         pim = findPiZero(evt);
00133         std::cout << " pizero mass: " << pim << std::endl;
00134         // set units to GeV and mm
00135         evt->use_units(HepMC::Units::GEV, HepMC::Units::MM);
00136         evt->write_units();
00137         pim = findPiZero(evt);
00138         std::cout << " pizero mass: " << pim 
00139                   << " " << HepMC::Units::name( evt->momentum_unit() ) << std::endl;
00140         // convert units to MeV
00141         evt->use_units(HepMC::Units::MEV, HepMC::Units::MM);
00142         evt->write_units();
00143         pim = findPiZero(evt);
00144         std::cout << " pizero mass: " << pim 
00145                   << " " << HepMC::Units::name( evt->momentum_unit() ) << std::endl;
00146         // clean up and get next event
00147         delete evt;
00148         ascii_in >> evt;
00149     }
00150     //........................................PRINT RESULT
00151     std::cout << icount << " events processed. Finished." << std::endl;
00152 }
00153 
00154 double findPiZero( HepMC::GenEvent* evt )
00155 {
00156     for ( HepMC::GenEvent::particle_const_iterator p 
00157               = evt->particles_begin(); p != evt->particles_end(); ++p ){
00158         if ( (*p)->pdg_id() == 111 ) {
00159             return (*p)->generated_mass();
00160         }
00161     }
00162     return 0.;
00163 }

Generated on Wed Jun 3 16:53:00 2009 for HepMC by  doxygen 1.5.1-3