HepMC Reference Documentation

HepMC

testHepMC.cc.in

The HepMC tests can also serve as useful examples 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 Test the new GenCrossSection class

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
00008 // and are written to "testHepMC.out"
00009 // Also write events using IO_AsciiParticles 
00010 //-------------------------------------------------------------------
00011 //
00012 
00013 #include "HepMC/GenEvent.h"
00014 #include "HepMC/GenCrossSection.h"
00015 #ifndef HEPMC_IO_ASCII_REMOVED
00016 #include "HepMC/IO_Ascii.h"
00017 #endif
00018 #ifdef HEPMC_HAS_IO_GENEVENT
00019 #include "HepMC/IO_GenEvent.h"
00020 #endif
00021 #include "HepMC/IO_AsciiParticles.h"
00022 
00023 // define methods and classes used by this test
00024 #include "IsGoodEvent.h"
00025 #include "testHepMCMethods.h"
00026 
00027 void read_testIOGenEvent();
00028 void read_variousFormats();
00029 void writeWithCrossSection();
00030 void readWithCrossSection();
00031 void read_nan();
00032 
00033 int main() { 
00034     read_testIOGenEvent();
00035     read_variousFormats();
00036     read_nan();
00037     writeWithCrossSection();
00038     readWithCrossSection();
00039     return 0;
00040 }
00041 
00042 void read_testIOGenEvent()
00043 {
00044     std::cout << std::endl;
00045     std::cout << "basic IO_GenEvent input and output" << std::endl;
00046     // declare an input strategy to read the data produced with the 
00047     // example_MyPythia - units are GeV and mm
00048     HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in);
00049     ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00050     // declare another IO_GenEvent for writing out the good events
00051     HepMC::IO_GenEvent ascii_out("testHepMC.out",std::ios::out);
00052     // declare an output IO_GenEvent for testing precision
00053     HepMC::IO_GenEvent prec_out("testHepMCprecision.out",std::ios::out);
00054     prec_out.precision(10);
00055     // declare an IO_AsciiParticle for output
00056     HepMC::IO_AsciiParticles particle_out("testHepMCParticle.out",std::ios::out);
00057     // declare an instance of the event selection predicate
00058     IsGoodEvent is_good_event;
00059     //........................................EVENT LOOP
00060     int icount=0;
00061     int num_good_events=0;
00062     HepMC::GenEvent* evt = ascii_in.read_next_event();
00063     while ( evt ) {
00064         ++icount;
00065         if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00066                                       << " its # " << evt->event_number() 
00067                                       << std::endl;
00068         if ( is_good_event(evt) ) {
00069             particleTypes(evt);
00070             ascii_out << evt;
00071             particle_out << evt;
00072             prec_out << evt;
00073             ++num_good_events;
00074         }
00075         
00076         // clean up and get next event
00077         delete evt;
00078         ascii_in >> evt;
00079     }
00080     //........................................PRINT RESULT
00081     std::cout << num_good_events << " out of " << icount 
00082               << " processed events passed the cuts. Finished." << std::endl;
00083 }
00084 
00085 void read_variousFormats()
00086 {
00087     std::cout << std::endl;
00088     std::cout << "process varied input" << std::endl;
00089     // declare an input strategy 
00090     HepMC::IO_GenEvent ascii_in("@srcdir@/testHepMCVarious.input",std::ios::in);
00091     ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00092     // declare another IO_GenEvent for writing out the good events
00093     HepMC::IO_GenEvent ascii_out("testHepMCVarious.out",std::ios::out);
00094     //........................................EVENT LOOP
00095     int icount=0;
00096     HepMC::GenEvent* evt = ascii_in.read_next_event();
00097     while ( evt ) {
00098         icount++;
00099         double pim;
00100         std::cout << "Processing Event Number " << icount
00101                   << " its # " << evt->event_number() 
00102                   << std::endl;
00103         ascii_out << evt;
00104         // units should be unknown
00105         evt->write_units();
00106         pim = findPiZero(evt);
00107         std::cout << " pizero mass: " << pim << std::endl;
00108         // set units to GeV and mm
00109         evt->use_units(HepMC::Units::GEV, HepMC::Units::MM);
00110         evt->write_units();
00111         pim = findPiZero(evt);
00112         std::cout << " pizero mass: " << pim 
00113                   << " " << HepMC::Units::name( evt->momentum_unit() ) << std::endl;
00114         // convert units to MeV
00115         evt->use_units(HepMC::Units::MEV, HepMC::Units::MM);
00116         evt->write_units();
00117         pim = findPiZero(evt);
00118         std::cout << " pizero mass: " << pim 
00119                   << " " << HepMC::Units::name( evt->momentum_unit() ) << std::endl;
00120         // clean up and get next event
00121         delete evt;
00122         ascii_in >> evt;
00123     }
00124     //........................................PRINT RESULT
00125     std::cout << icount << " events processed. Finished." << std::endl;
00126 }
00127 
00128 void writeWithCrossSection()
00129 {
00130     // declare an input strategy to read input data 
00131     // units are GeV and mm
00132     HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in);
00133     ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00134     // declare another IO_GenEvent for writing out some events
00135     HepMC::IO_GenEvent ascii_out("testCrossSection.out",std::ios::out);
00136     // declare an output stream for printing events
00137     std::ofstream xout( "testCrossSection.cout" );
00138     // create an empty GenCrossSection object
00139     HepMC::GenCrossSection cross;
00140     //........................................EVENT LOOP
00141     int icount=0;
00142     const double xs0 = 0.00346;
00143     const double xs1 = 0.12;
00144     const double xs2 = 33.234;
00145     const double xs3 = 459.345;
00146     double xserr = 0.0001;
00147     HepMC::GenEvent* evt = ascii_in.read_next_event();
00148     while ( evt ) {
00149         icount++;
00150         // use a variety of arbitrary cross section values
00151         if( icount < 10 ) {
00152             const double xs = xs0 - 1.34 * xserr;
00153             cross.set_cross_section( xs, xserr );
00154         } else if( icount < 20 ) {
00155             const double xs = xs1 - 1.34 * xserr;
00156             cross.set_cross_section( xs, xserr );
00157         } else if( icount < 30 ) {
00158             const double xs = xs2 - 1.34 * xserr;
00159             cross.set_cross_section( xs, xserr );
00160         } else {
00161             const double xs = xs3 - 1.34 * xserr;
00162             cross.set_cross_section( xs, xserr );
00163         }
00164         xserr *= 0.99;
00165         if ( icount == 10 ) xserr += 0.01;
00166         if ( icount == 20 ) xserr += 0.4;
00167         if ( icount == 30 ) xserr += 1.0;
00168         // attach this cross section to the event
00169         evt->set_cross_section( cross );
00170         evt->write_cross_section();
00171         if ( icount%20==1 ) {
00172             std::cout << "writeWithCrossSection: Processing Event Number " << icount
00173                                       << " its # " << evt->event_number() 
00174                                       << std::endl;
00175             ascii_out << evt;
00176             evt->print(xout);
00177         }
00178         
00179         // clean up and get next event
00180         delete evt;
00181         ascii_in >> evt;
00182     }
00183     //........................................PRINT RESULT
00184     std::cout << "writeWithCrossSection processed " << icount << " events. Finished." << std::endl;
00185 }
00186 
00187 void readWithCrossSection()
00188 {
00189     // read the file we just wrote
00190     HepMC::IO_GenEvent ascii_in("testCrossSection.out",std::ios::in);
00191     // declare another IO_GenEvent for writing out some events
00192     HepMC::IO_GenEvent ascii_out("testCrossSection2.out",std::ios::out);
00193     //........................................EVENT LOOP
00194     int icount=0;
00195     HepMC::GenEvent* evt = ascii_in.read_next_event();
00196     while ( evt ) {
00197         ++icount;
00198         std::cout << "readWithCrossSection: Processing Event Number " << icount
00199                                   << " its # " << evt->event_number() 
00200                                   << std::endl;
00201         if (evt->cross_section()->cross_section() <= 0) {
00202           std::cout << "testReadCrossSection: invalid cross-section!" << std::endl;
00203         }
00204         ascii_out << evt;
00205         
00206         // clean up and get next event
00207         delete evt;
00208         ascii_in >> evt;
00209     }
00210     //........................................PRINT RESULT
00211     std::cout << "readWithCrossSection processed " << icount << " events. Finished." << std::endl;
00212 }
00213 
00214 void read_nan()
00215 {
00216     // Read an input file that has corrupt information (nan's)
00217     //
00218     HepMC::IO_GenEvent xin("@srcdir@/testHepMCVarious.input",std::ios::in);
00219     HepMC::IO_GenEvent xout("testNaN.out",std::ios::out);
00220     //........................................EVENT LOOP
00221     int icount=0;
00222     int invaliddata=0;
00223     bool ok = true;
00224     std::cout << "---------------------------------------- " << std::endl;
00225     std::cout << "Begin NaN test " << std::endl;
00226     HepMC::GenEvent* evt = xin.read_next_event();
00227     //
00228     // To recover from corrupt input, replace "while(evt) {...}" 
00229     // with  "while(ok) { if(evt) {... xin >> evt;} else {...} }"
00230     //
00231     while ( ok ) {
00232         if( evt ) {
00233             ++icount;
00234             std::cout << "read_nan: Processing Event Number " << icount
00235                       << " its # " << evt->event_number() 
00236                       << std::endl;
00237             xout << evt;
00238             // clean up and get next event
00239             delete evt;
00240             xin >> evt;
00241         } else if (xin.error_type() == HepMC::IO_Exception::InvalidData ) {
00242             ++invaliddata;
00243             std::cerr << "INPUT ERROR: " << xin.error_message() << std::endl;
00244             // clean up and get next event
00245             delete evt;
00246             xin >> evt;
00247         } else if (invaliddata > 50 ) {
00248             std::cerr << "INPUT ERROR: " << xin.error_message() << std::endl;
00249             ok = false;
00250         } else {
00251             ok = false;
00252         }
00253     }
00254     // print status of input stream
00255     if ( xin.error_type() != 0 ) {
00256         std::cout << "processing of @srcdir@/testHepMCVarious.input ended with error " 
00257                   << xin.error_type() << std::endl;
00258         std::cout << "  --- " << xin.error_message() << std::endl;
00259     }
00260     std::cout << icount << " events processed and " 
00261               << invaliddata << " events ignored. Finished." 
00262               << std::endl;
00263     std::cout << "End NaN test " << std::endl;
00264     std::cout << "---------------------------------------- " << std::endl;
00265 }
00266 

Generated on Thu Jan 7 13:10:15 2010 for HepMC by  doxygen 1.4.7