HepMC Reference Documentation

HepMC

testMass.cc.in

Read events from testIOGenEvent.input Select events containing a photon of pT > 25 GeV Add arbitrary PDF information to one of the good events Write the selected events and read them back in using an istream

00001 //-------------------------------------------------------------------
00002 // testMass.cc.in
00003 //
00004 // garren@fnal.gov, March 2006
00005 // Read events written by example_MyPythia.cc
00006 // Select events containing a photon of pT > 25 GeV 
00007 // Add arbitrary PDF information to one of the good events
00008 // Add arbitrary HeavyIon information to one of the good events
00009 // Write the selected events and read them back in using an istream
00010 //-------------------------------------------------------------------
00011 
00012 #include <cmath>        // for min()
00013 #include <ostream>
00014 
00015 #include "HepMC/IO_GenEvent.h"
00016 #include "HepMC/GenEvent.h"
00017 #include "HepMC/Version.h"
00018 
00019 // define methods and classes used by this test
00020 #include "IsGoodEvent.h"
00021 
00022 void   massInfo( const HepMC::GenEvent*, std::ostream& os );
00023 
00024 int main() { 
00025     // output file
00026     std::ofstream os( "testMass.cout" );
00027     // read and process the input file
00028     {
00029         // declare an input strategy to read the data produced with the 
00030         // example_MyPythia
00031         HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in);
00032         ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00033         // declare another IO_GenEvent for output
00034         HepMC::IO_GenEvent ascii_out("testMass1.out",std::ios::out);
00035         // declare an instance of the event selection predicate
00036         IsGoodEvent is_good_event;
00037         // send version to output
00038         HepMC::version(os);
00039         //........................................EVENT LOOP
00040         int icount=0;
00041         int num_good_events=0;
00042         double x1=0., x2=0., q=0., xf1=0., xf2=0.;
00043         HepMC::GenEvent* evt = ascii_in.read_next_event();
00044         while ( evt ) {
00045             icount++;
00046             if ( icount%50==1 ) os << "Processing Event Number " << icount
00047                                    << " its # " << evt->event_number() 
00048                                    << std::endl;
00049             if ( is_good_event(evt) ) {
00050                 if (num_good_events == 0 ) {
00051                     // add some arbitrary PDF information
00052                     x1 = std::min(0.8, 0.07 * icount);
00053                     x2 = 1-x1;
00054                     q = 1.69 * icount;
00055                     // use beam momentum
00056                     if( evt->valid_beam_particles() ) {
00057                         HepMC::GenParticle* bp1 = evt->beam_particles().first;
00058                         xf1 = x1*bp1->momentum().rho();
00059                         xf2 = x2*bp1->momentum().rho();
00060                     } else {
00061                         xf1 = x1*0.34;
00062                         xf2 = x2*0.34;
00063                     }
00064                     // provide optional pdf set id numbers 
00065                     // (two ints at the end of the constructor)
00066                     HepMC::PdfInfo pdf( 2, 3, x1, x2, q, xf1, xf2, 230, 230);
00067                     evt->set_pdf_info(pdf);
00068                     // add some arbitrary HeavyIon information
00069                     HepMC::HeavyIon ion(23,11,12,15,3,5,0,0,0,0.0145);
00070                     evt->set_heavy_ion( ion );
00071                 }
00072                 os << "saving Event " << evt->event_number() << std::endl;
00073                 if( evt->weights().size() > 0 ) {
00074                     os << "Weights: ";
00075                     evt->weights().print(os);
00076                 }
00077                 ascii_out << evt;
00078                 ++num_good_events;
00079             }
00080 
00081             // clean up and get next event
00082             delete evt;
00083             ascii_in >> evt;
00084         }
00085         //........................................PRINT RESULT
00086         os << num_good_events << " out of " << icount 
00087            << " processed events passed the cuts. Finished." << std::endl;
00088     }
00089     // now read the file we just created
00090     {
00091         // declare an input strategy 
00092         const char infile[] = "testMass1.out";
00093         std::ifstream istr( infile );
00094         if( !istr ) {
00095           std::cerr << "testMass: cannot open " << infile << std::endl;
00096           exit(-1);
00097         }
00098         HepMC::IO_GenEvent xin(istr);
00099         // declare another IO_GenEvent for output
00100         HepMC::IO_GenEvent xout("testMass2.out",std::ios::out);
00101         //........................................EVENT LOOP
00102         int ixin=0;
00103         HepMC::GenEvent* evt = xin.read_next_event();
00104         while ( evt ) {
00105             ixin++;
00106             os << "reading Event " << evt->event_number() << std::endl;
00107             if( evt->weights().size() > 0 ) {
00108                 os << "Weights: ";
00109                 evt->weights().print(os);
00110             }
00111             xout << evt;
00112             // look at mass info
00113             massInfo(evt,os);
00114 
00115             // clean up and get next event
00116             delete evt;
00117             xin >> evt;
00118         }
00119         //........................................PRINT RESULT
00120         os << ixin << " events in the second pass. Finished." << std::endl;
00121     }
00122 }
00123 
00124 void massInfo( const HepMC::GenEvent* e, std::ostream& os )
00125 {
00126    double gm, m, d;
00127    for ( HepMC::GenEvent::particle_const_iterator p = e->particles_begin(); p != e->particles_end();
00128          ++p ) {
00129          
00130        gm = (*p)->generated_mass();
00131        m = (*p)->momentum().m();
00132        d = fabs(m-gm);
00133        if( d > 1.0e-5 ) {
00134             os << "Event " << e->event_number()
00135                << " Particle " << (*p)->barcode() 
00136                << " " << (*p)->pdg_id()
00137                << " generated mass " << gm
00138                << " mass from momentum " << m 
00139                << " difference " << d << std::endl;
00140        } 
00141    }   
00142 }

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