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         HepMC::GenCrossSection xs;
00074         //
00075         //........................................EVENT LOOP
00076         for ( int i = 1; i <= 100; i++ ) {
00077             if ( i%50==1 ) std::cout << "Processing Event Number " 
00078                                      << i << std::endl;
00079             call_pyevnt();      // generate one event with Pythia
00080             // pythia pyhepc routine converts common PYJETS in common HEPEVT
00081             call_pyhepc( 1 );
00082             HepMC::GenEvent* evt = hepevtio.read_next_event();
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             xs.set_cross_section( pyint5.xsec[2][0] );
00090             evt->set_cross_section( xs );
00091             // write the event out to the ascii files
00092             ascii_io << (*evt);;
00093             // we also need to delete the created event from memory
00094             delete evt;
00095         }
00096         // write the line that defines the end of a GenEvent block
00097         HepMC::write_HepMC_IO_block_end( ascii_io );
00098         //........................................TERMINATION
00099         // write out some information from Pythia to the screen
00100         call_pystat( 1 );    
00101     } // end scope of ascii_io
00102 }
00103 
00104 void readPythiaStreamIO() {
00105     // example to read events written by writePythiaStreamIO
00106     // and write them back out
00107     std::cout << std::endl;
00108     // input units are GeV and mm
00109     const char infile[] = "example_PythiaStreamIO_write.dat";
00110     std::ifstream is( infile );
00111     if( !is ) {
00112       std::cerr << "cannot open " << infile << std::endl;
00113       exit(-1);
00114     }
00115     //
00116     { // begin scope of ascii_io
00117         // declare an output stream
00118         const char outfile[] = "example_PythiaStreamIO_read.dat";
00119         std::ofstream ascii_io( outfile );
00120         if( !ascii_io ) {
00121           std::cerr << "cannot open " << outfile << std::endl;
00122           exit(-1);
00123         }
00124         ascii_io.precision(16);
00125         HepMC::write_HepMC_IO_block_begin( ascii_io );
00126         //
00127         //........................................EVENT LOOP
00128         HepMC::GenEvent evt;
00129         int i = 0;
00130         while ( is ) {
00131             evt.read( is );
00132             // make sure we have a valid event
00133             if( evt.is_valid() ) {
00134                 ++i;
00135                 if ( i%50==1 ) std::cout << "Processing Event Number " 
00136                                          << i << std::endl;
00137                 if ( i%25==2 ) {
00138                     // write the cross section if it exists
00139                     if( evt.cross_section() ) {
00140                         std::cout << "cross section at event " << i << " is " 
00141                                   << evt.cross_section()->cross_section()
00142                                   << std::endl;
00143                     }
00144                 }
00145                 // write the event out to the ascii files
00146                 evt.write( ascii_io );
00147             }
00148         }
00149         //........................................TERMINATION
00150         HepMC::write_HepMC_IO_block_end( ascii_io );
00151     } // end scope of ascii_io
00152 }

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