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

Generated on Tue Jun 2 20:28:24 2009 for HepMC by  doxygen 1.5.1-3