HepMC Reference Documentation

HepMC

example_PythiaStreamIO.cc

This example generates Pythia events and fills cross section information from pyint5. The example uses streaming I/O to write a file and then read it.

00001 
00002 // example_PythiaStreamIO.cc
00003 //
00004 // garren@fnal.gov, May 2009
00005 // 
00019 
00020 
00021 #include <fstream>
00022 #include <iostream>
00023 #include "HepMC/PythiaWrapper.h"
00024 #include "HepMC/IO_HEPEVT.h"
00025 #include "HepMC/GenEvent.h"
00026 #include "PythiaHelper.h"
00027 
00028 void writePythiaStreamIO();
00029 void readPythiaStreamIO();
00030 
00031 int main() { 
00032 
00033     writePythiaStreamIO();
00034     readPythiaStreamIO();
00035 
00036     return 0;
00037 }
00038    
00039 
00040 void writePythiaStreamIO() {
00041     // example to generate events and write output
00042     std::cout << std::endl;
00043     std::cout << "Begin pythia_out()" << std::endl;
00044    //........................................HEPEVT
00045     // Pythia 6.1 uses HEPEVT with 4000 entries and 8-byte floating point
00046     //  numbers. We need to explicitly pass this information to the 
00047     //  HEPEVT_Wrapper.
00048     //
00049     HepMC::HEPEVT_Wrapper::set_max_number_entries(4000);
00050     HepMC::HEPEVT_Wrapper::set_sizeof_real(8);
00051     //
00052     //........................................PYTHIA INITIALIZATIONS
00053     initPythia();
00054 
00055     //........................................HepMC INITIALIZATIONS
00056     //
00057     // Instantiate an IO strategy for reading from HEPEVT.
00058     HepMC::IO_HEPEVT hepevtio;
00059     //
00060     { // begin scope of ascii_io
00061         // declare an output stream
00062         const char outfile[] = "example_PythiaStreamIO_write.dat";
00063         std::ofstream ascii_io( outfile );
00064         if( !ascii_io ) {
00065           std::cerr << "cannot open " << outfile << std::endl;
00066           exit(-1);
00067         }
00068         // use the default IO_GenEvent precision
00069         ascii_io.precision(16);
00070         // write the line that defines the beginning of a GenEvent block
00071         HepMC::write_HepMC_IO_block_begin( ascii_io );
00072         //
00073         //........................................EVENT LOOP
00074         for ( int i = 1; i <= 100; i++ ) {
00075             if ( i%50==1 ) std::cout << "Processing Event Number " 
00076                                      << i << std::endl;
00077             call_pyevnt();      // generate one event with Pythia
00078             // pythia pyhepc routine converts common PYJETS in common HEPEVT
00079             call_pyhepc( 1 );
00080             HepMC::GenEvent* evt = hepevtio.read_next_event();
00081             // define the units (Pythia uses GeV and mm)
00082             evt->use_units(HepMC::Units::GEV, HepMC::Units::MM);
00083             // add some information to the event
00084             evt->set_event_number(i);
00085             evt->set_signal_process_id(20);
00086             // set number of multi parton interactions
00087             evt->set_mpi( pypars.msti[31-1] );
00088             // set cross section information
00089             evt->set_cross_section( getPythiaCrossSection() );
00090             // write the event out to the ascii files
00091             ascii_io << (*evt);;
00092             // we also need to delete the created event from memory
00093             delete evt;
00094         }
00095         // write the line that defines the end of a GenEvent block
00096         HepMC::write_HepMC_IO_block_end( ascii_io );
00097         //........................................TERMINATION
00098         // write out some information from Pythia to the screen
00099         call_pystat( 1 );    
00100     } // end scope of ascii_io
00101 }
00102 
00103 void readPythiaStreamIO() {
00104     // example to read events written by writePythiaStreamIO
00105     // and write them back out
00106     std::cout << std::endl;
00107     // input units are GeV and mm
00108     const char infile[] = "example_PythiaStreamIO_write.dat";
00109     std::ifstream is( infile );
00110     if( !is ) {
00111       std::cerr << "cannot open " << infile << std::endl;
00112       exit(-1);
00113     }
00114     //
00115     { // begin scope of ascii_io
00116         // declare an output stream
00117         const char outfile[] = "example_PythiaStreamIO_read.dat";
00118         std::ofstream ascii_io( outfile );
00119         if( !ascii_io ) {
00120           std::cerr << "cannot open " << outfile << std::endl;
00121           exit(-1);
00122         }
00123         ascii_io.precision(16);
00124         HepMC::write_HepMC_IO_block_begin( ascii_io );
00125         //
00126         //........................................EVENT LOOP
00127         HepMC::GenEvent evt;
00128         int i = 0;
00129         while ( is ) {
00130             evt.read( is );
00131             // make sure we have a valid event
00132             if( evt.is_valid() ) {
00133                 ++i;
00134                 if ( i%50==1 ) std::cout << "Processing Event Number " 
00135                                          << i << std::endl;
00136                 if ( i%25==2 ) {
00137                     // write the cross section if it exists
00138                     if( evt.cross_section() ) {
00139                         std::cout << "cross section at event " << i << " is " 
00140                                   << evt.cross_section()->cross_section()
00141                                   << std::endl;
00142                     }
00143                 }
00144                 // write the event out to the ascii files
00145                 evt.write( ascii_io );
00146             }
00147         }
00148         //........................................TERMINATION
00149         HepMC::write_HepMC_IO_block_end( ascii_io );
00150     } // end scope of ascii_io
00151 }

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