HepMC Reference Documentation

HepMC

example_MyPythia.cc

example to generate events and write output example to generate events and perform simple event selection example to read the file written by pythia_out example to generate events, write them, and read them back

00001 
00002 // Matt.Dobbs@Cern.CH, December 1999
00003 // November 2000, updated to use Pythia 6.1
00004 // 
00046 
00047 
00048 #include <iostream>
00049 #include "HepMC/PythiaWrapper.h"
00050 #include "HepMC/IO_HEPEVT.h"
00051 #include "HepMC/IO_GenEvent.h"
00052 #include "HepMC/IO_Ascii.h"
00053 #include "HepMC/IO_AsciiParticles.h"
00054 #include "HepMC/GenEvent.h"
00055 #include "PythiaHelper.h"
00056 
00058 
00062 class IsGoodEventMyPythia {
00063 public:
00065     bool operator()( const HepMC::GenEvent* evt ) { 
00066         for ( HepMC::GenEvent::particle_const_iterator p 
00067                   = evt->particles_begin(); p != evt->particles_end(); ++p ){
00068             if ( (*p)->pdg_id() == 22 && (*p)->momentum().perp() > 25. ) {
00069                 //std::cout << "Event " << evt->event_number()
00070                 //     << " is a good event." << std::endl;
00071                 //(*p)->print();
00072                 return 1;
00073             }
00074         }
00075         return 0;
00076     }
00077 };
00078     
00079 
00080 void pythia_out();
00081 void pythia_in();
00082 void pythia_in_out();
00083 void event_selection();
00084 void pythia_particle_out();
00085 
00086 int main() { 
00087     // example to generate events and write output
00088     pythia_out();
00089     // example to generate events and perform simple event selection
00090     event_selection();
00091     // example to read the file written by pythia_out
00092     pythia_in();
00093     // example to generate events, write them, and read them back
00094     pythia_in_out();
00095 
00096     return 0;
00097 }
00098 
00099 
00100 void pythia_out()
00101 {
00102     std::cout << std::endl;
00103     std::cout << "Begin pythia_out()" << std::endl;
00104    //........................................HEPEVT
00105     // Pythia 6.1 uses HEPEVT with 4000 entries and 8-byte floating point
00106     //  numbers. We need to explicitly pass this information to the 
00107     //  HEPEVT_Wrapper.
00108     //
00109     HepMC::HEPEVT_Wrapper::set_max_number_entries(4000);
00110     HepMC::HEPEVT_Wrapper::set_sizeof_real(8);
00111     //
00112     //........................................PYTHIA INITIALIZATIONS
00113     initPythia();
00114 
00115     //........................................HepMC INITIALIZATIONS
00116     //
00117     // Instantiate an IO strategy for reading from HEPEVT.
00118     HepMC::IO_HEPEVT hepevtio;
00119     //
00120     { // begin scope of ascii_io
00121         // Instantiate an IO strategy to write the data to file 
00122         HepMC::IO_GenEvent ascii_io("example_MyPythia.dat",std::ios::out);
00123         //
00124         //........................................EVENT LOOP
00125         for ( int i = 1; i <= 100; i++ ) {
00126             if ( i%50==1 ) std::cout << "Processing Event Number " 
00127                                      << i << std::endl;
00128             call_pyevnt();      // generate one event with Pythia
00129             // pythia pyhepc routine converts common PYJETS in common HEPEVT
00130             call_pyhepc( 1 );
00131             HepMC::GenEvent* evt = hepevtio.read_next_event();
00132             // add some information to the event
00133             evt->set_event_number(i);
00134             evt->set_signal_process_id(20);
00135             // set number of multi parton interactions
00136             evt->set_mpi( pypars.msti[31-1] );
00137             // write the event out to the ascii files
00138             ascii_io << evt;
00139             // we also need to delete the created event from memory
00140             delete evt;
00141         }
00142         //........................................TERMINATION
00143         // write out some information from Pythia to the screen
00144         call_pystat( 1 );    
00145     } // end scope of ascii_io
00146 }
00147 
00148  
00149 void event_selection()
00150 {
00151     std::cout << std::endl;
00152     std::cout << "Begin event_selection()" << std::endl;
00153     //........................................HEPEVT
00154     // Pythia 6.1 uses HEPEVT with 4000 entries and 8-byte floating point
00155     //  numbers. We need to explicitly pass this information to the 
00156     //  HEPEVT_Wrapper.
00157     //
00158     HepMC::HEPEVT_Wrapper::set_max_number_entries(4000);
00159     HepMC::HEPEVT_Wrapper::set_sizeof_real(8);
00160     //  
00161     //........................................PYTHIA INITIALIZATIONS
00162     initPythia();
00163     //
00164     //........................................HepMC INITIALIZATIONS
00165     // Instantiate an IO strategy for reading from HEPEVT.
00166     HepMC::IO_HEPEVT hepevtio;
00167     // declare an instance of the event selection predicate
00168     IsGoodEventMyPythia is_good_event;
00169     //........................................EVENT LOOP
00170     int icount=0;
00171     int num_good_events=0;
00172     for ( int i = 1; i <= 100; i++ ) {
00173         icount++;
00174         if ( i%50==1 ) std::cout << "Processing Event Number " 
00175                                  << i << std::endl;
00176         call_pyevnt(); // generate one event with Pythia
00177         // pythia pyhepc routine convert common PYJETS in common HEPEVT
00178         call_pyhepc( 1 );
00179         HepMC::GenEvent* evt = hepevtio.read_next_event();
00180         // set number of multi parton interactions
00181         evt->set_mpi( pypars.msti[31-1] );
00182         // do event selection
00183         if ( is_good_event(evt) ) {
00184             std::cout << "Good Event Number " << i << std::endl;
00185             ++num_good_events;
00186         }
00187         // we also need to delete the created event from memory
00188         delete evt;
00189     }
00190     //........................................TERMINATION
00191     // write out some information from Pythia to the screen
00192     call_pystat( 1 );    
00193     //........................................PRINT RESULTS
00194     std::cout << num_good_events << " out of " << icount 
00195               << " processed events passed the cuts. Finished." << std::endl;
00196 }
00197 
00198 void pythia_in()
00199 {
00200     std::cout << std::endl;
00201     std::cout << "Begin pythia_in()" << std::endl;
00202     std::cout << "reading example_MyPythia.dat" << std::endl;
00203     //........................................define an input scope
00204     {
00205         // open input stream
00206         std::ifstream istr( "example_MyPythia.dat" );
00207         if( !istr ) {
00208           std::cerr << "example_ReadMyPythia: cannot open example_MyPythia.dat" << std::endl;
00209           exit(-1);
00210         }
00211         HepMC::IO_GenEvent ascii_in(istr);
00212         // open output stream (alternate method)
00213         HepMC::IO_GenEvent ascii_out("example_MyPythia2.dat",std::ios::out);
00214         // now read the file
00215         int icount=0;
00216         HepMC::GenEvent* evt = ascii_in.read_next_event();
00217         while ( evt ) {
00218             icount++;
00219             if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00220                                           << " its # " << evt->event_number() 
00221                                           << std::endl;
00222             // write the event out to the ascii file
00223             ascii_out << evt;
00224             delete evt;
00225             ascii_in >> evt;
00226         }
00227         //........................................PRINT RESULT
00228         std::cout << icount << " events found. Finished." << std::endl;
00229     } // ascii_out and istr destructors are called here
00230 }
00231 
00232 void pythia_in_out()
00233 {
00234     std::cout << std::endl;
00235     std::cout << "Begin pythia_in_out()" << std::endl;
00236     //........................................HEPEVT
00237     // Pythia 6.3 uses HEPEVT with 4000 entries and 8-byte floating point
00238     //  numbers. We need to explicitly pass this information to the 
00239     //  HEPEVT_Wrapper.
00240     //
00241     HepMC::HEPEVT_Wrapper::set_max_number_entries(4000);
00242     HepMC::HEPEVT_Wrapper::set_sizeof_real(8);
00243     //
00244     //........................................PYTHIA INITIALIZATIONS
00245     initPythia();
00246 
00247     //........................................HepMC INITIALIZATIONS
00248     //
00249     // Instantiate an IO strategy for reading from HEPEVT.
00250     HepMC::IO_HEPEVT hepevtio;
00251     //
00252     //........................................define the output scope
00253     {
00254         // Instantial an IO strategy to write the data to file - it uses the 
00255         //  same ParticleDataTable
00256         HepMC::IO_GenEvent ascii_io("example_MyPythiaRead.dat",std::ios::out);
00257         //
00258         //........................................EVENT LOOP
00259         for ( int i = 1; i <= 100; i++ ) {
00260             if ( i%50==1 ) std::cout << "Processing Event Number " 
00261                                      << i << std::endl;
00262             call_pyevnt();      // generate one event with Pythia
00263             // pythia pyhepc routine converts common PYJETS in common HEPEVT
00264             call_pyhepc( 1 );
00265             HepMC::GenEvent* evt = hepevtio.read_next_event();
00266             // add some information to the event
00267             evt->set_event_number(i);
00268             evt->set_signal_process_id(20);
00269             // write the event out to the ascii file
00270             ascii_io << evt;
00271             // we also need to delete the created event from memory
00272             delete evt;
00273         }
00274         //........................................TERMINATION
00275         // write out some information from Pythia to the screen
00276         call_pystat( 1 );    
00277     }  // ascii_io destructor is called here
00278     //
00279     //........................................define an input scope
00280     {
00281         // now read the file we wrote
00282         HepMC::IO_GenEvent ascii_in("example_MyPythiaRead.dat",std::ios::in);
00283         HepMC::IO_GenEvent ascii_io2("example_MyPythiaRead2.dat",std::ios::out);
00284         int icount=0;
00285         HepMC::GenEvent* evt = ascii_in.read_next_event();
00286         while ( evt ) {
00287             icount++;
00288             if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00289                                           << " its # " << evt->event_number() 
00290                                           << std::endl;
00291             // write the event out to the ascii file
00292             ascii_io2 << evt;
00293             delete evt;
00294             ascii_in >> evt;
00295         }
00296         //........................................PRINT RESULT
00297         std::cout << icount << " events found. Finished." << std::endl;
00298     } // ascii_io2 and ascii_in destructors are called here
00299 }
00300 
00301 void pythia_particle_out()
00302 {
00303     std::cout << std::endl;
00304     std::cout << "Begin pythia_particle_out()" << std::endl;
00305     //........................................HEPEVT
00306     // Pythia 6.1 uses HEPEVT with 4000 entries and 8-byte floating point
00307     //  numbers. We need to explicitly pass this information to the 
00308     //  HEPEVT_Wrapper.
00309     //
00310     HepMC::HEPEVT_Wrapper::set_max_number_entries(4000);
00311     HepMC::HEPEVT_Wrapper::set_sizeof_real(8);
00312     //
00313     //........................................PYTHIA INITIALIZATIONS
00314     initPythia();
00315 
00316     //........................................HepMC INITIALIZATIONS
00317     //
00318     // Instantiate an IO strategy for reading from HEPEVT.
00319     HepMC::IO_HEPEVT hepevtio;
00320     //
00321     { // begin scope of ascii_io
00322         // Instantiate an IO strategy to write the data to file 
00323         HepMC::IO_AsciiParticles ascii_io("example_PythiaParticle.dat",std::ios::out);
00324         //
00325         //........................................EVENT LOOP
00326         for ( int i = 1; i <= 100; i++ ) {
00327             if ( i%50==1 ) std::cout << "Processing Event Number " 
00328                                      << i << std::endl;
00329             call_pyevnt();      // generate one event with Pythia
00330             // pythia pyhepc routine converts common PYJETS in common HEPEVT
00331             call_pyhepc( 1 );
00332             HepMC::GenEvent* evt = hepevtio.read_next_event();
00333             // add some information to the event
00334             evt->set_event_number(i);
00335             evt->set_signal_process_id(20);
00336             // write the event out to the ascii file
00337             ascii_io << evt;
00338             // we also need to delete the created event from memory
00339             delete evt;
00340         }
00341         //........................................TERMINATION
00342         // write out some information from Pythia to the screen
00343         call_pystat( 1 );    
00344     } // end scope of ascii_io
00345 }
00346 

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