HepMC Reference Documentation

HepMC

main32.cc

Go to the documentation of this file.
00001 // main32.cc is a part of the PYTHIA event generator.
00002 // Copyright (C) 2011 Mikhail Kirsanov, Torbjorn Sjostrand.
00003 // PYTHIA is licenced under the GNU GPL version 2, see COPYING for details.
00004 // Please respect the MCnet Guidelines, see GUIDELINES for details.
00005 
00006 // This is a simple test program.
00007 // It illustrates how a file with HepMC events can be generated by Pythia8.
00008 // Input and output files are specified on the command line, e.g. like
00009 // ./main32.exe main32.cmnd hepmcout32.dat > out
00010 // The main program contains no analysis; this is intended to happen later.
00011 // It therefore "never" has to be recompiled to handle different tasks.
00012 
00013 // WARNING: typically one needs 25 MB/100 events at the LHC.
00014 // Therefore large event samples may be impractical.
00015 
00016 #include "Pythia.h"
00017 #include "HepMCInterface.h"
00018 
00019 #include "HepMC/GenEvent.h"   
00020 #include "HepMC/IO_GenEvent.h"
00021 
00022 // Following line is a deprecated alternative, removed in recent versions.
00023 //#include "HepMC/IO_Ascii.h"
00024 //#include "HepMC/IO_AsciiParticles.h"
00025 
00026 // Following line to be used with HepMC 2.04 onwards.
00027 #ifdef HEPMC_HAS_UNITS
00028 #include "HepMC/Units.h"
00029 #endif
00030 
00031 using namespace Pythia8; 
00032 
00033 int main(int argc, char* argv[]) {
00034 
00035   // Check that correct number of command-line arguments
00036   if (argc != 3) {
00037     cerr << " Unexpected number of command-line arguments. \n You are"
00038          << " expected to provide one input and one output file name. \n"
00039          << " Program stopped! " << endl;
00040     return 1;
00041   }
00042 
00043   // Check that the provided input name corresponds to an existing file.
00044   ifstream is(argv[1]);  
00045   if (!is) {
00046     cerr << " Command-line file " << argv[1] << " was not found. \n"
00047          << " Program stopped! " << endl;
00048     return 1;
00049   }
00050 
00051   // Confirm that external files will be used for input and output.
00052   cout << " PYTHIA settings will be read from file " << argv[1] << endl;
00053   cout << " HepMC events will be written to file " << argv[2] << endl;
00054 
00055   // Interface for conversion from Pythia8::Event to HepMC one. 
00056   HepMC::I_Pythia8 ToHepMC;
00057   //  ToHepMC.set_crash_on_problem();
00058 
00059   // Specify file where HepMC events will be stored.
00060   HepMC::IO_GenEvent ascii_io(argv[2], std::ios::out);
00061   // Following line is a deprecated alternative, removed in recent versions
00062   // HepMC::IO_Ascii ascii_io("hepmcout32.dat", std::ios::out);
00063   // Line below is an eye-readable one-way output, uncomment the include above
00064   // HepMC::IO_AsciiParticles ascii_io("hepmcout32.dat", std::ios::out);
00065  
00066   // Generator. 
00067   Pythia pythia;
00068 
00069   // Read in commands from external file.
00070   pythia.readFile(argv[1]);    
00071 
00072   // Extract settings to be used in the main program.
00073   int    nEvent    = pythia.mode("Main:numberOfEvents");
00074   int    nShow     = pythia.mode("Main:timesToShow");
00075   int    nAbort    = pythia.mode("Main:timesAllowErrors");
00076   bool   showCS    = pythia.flag("Main:showChangedSettings");
00077   bool   showAS    = pythia.flag("Main:showAllSettings");
00078   bool   showCPD   = pythia.flag("Main:showChangedParticleData");
00079   bool   showAPD   = pythia.flag("Main:showAllParticleData");
00080  
00081   // Initialization. Beam parameters set in .cmnd file.
00082   pythia.init();
00083 
00084   // List settings.
00085   if (showCS) pythia.settings.listChanged();
00086   if (showAS) pythia.settings.listAll();
00087 
00088   // List particle data.  
00089   if (showCPD) pythia.particleData.listChanged();
00090   if (showAPD) pythia.particleData.listAll();
00091 
00092   // Begin event loop.
00093   int nPace  = max(1, nEvent / max(1, nShow) ); 
00094   int iAbort = 0; 
00095   for (int iEvent = 0; iEvent < nEvent; ++iEvent) {
00096     if (nShow > 0 && iEvent%nPace == 0) 
00097       cout << " Now begin event " << iEvent << endl;
00098 
00099     // Generate event. 
00100     if (!pythia.next()) {
00101 
00102       // If failure because reached end of file then exit event loop.
00103       if (pythia.info.atEndOfFile()) {
00104         cout << " Aborted since reached end of Les Houches Event File\n"; 
00105         break; 
00106       }
00107 
00108       // First few failures write off as "acceptable" errors, then quit.
00109       if (++iAbort < nAbort) continue;
00110       cout << " Event generation aborted prematurely, owing to error!\n"; 
00111       break;
00112     }
00113 
00114     // Construct new empty HepMC event. 
00115 #ifdef HEPMC_HAS_UNITS
00116     // This form with arguments is only meaningful for HepMC 2.04 onwards, 
00117     // and even then unnecessary if HepMC was built with GeV and mm as units..
00118     HepMC::GenEvent* hepmcevt = new HepMC::GenEvent( 
00119       HepMC::Units::GEV, HepMC::Units::MM);
00120 #else
00121     // This form is needed for backwards compatibility. 
00122     // In HepMCInterface.cc a conversion from GeV to MeV will be done.
00123     HepMC::GenEvent* hepmcevt = new HepMC::GenEvent();
00124 #endif
00125 
00126     // Fill HepMC event, including PDF info.
00127     ToHepMC.fill_next_event( pythia, hepmcevt );
00128     // This alternative older method fills event, without PDF info.
00129     // ToHepMC.fill_next_event( pythia.event, hepmcevt );
00130 
00131     // Write the HepMC event to file. Done with it.
00132     ascii_io << hepmcevt;
00133     delete hepmcevt;
00134 
00135   // End of event loop. Statistics. 
00136   }
00137   pythia.statistics();
00138 
00139   // Done.
00140   return 0;
00141 }

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