StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
main42.cc
1 // main42.cc is a part of the PYTHIA event generator.
2 // Copyright (C) 2012 Mikhail Kirsanov, Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL version 2, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 // This is a simple test program.
7 // It illustrates how a file with HepMC events can be generated by Pythia8.
8 // Input and output files are specified on the command line, e.g. like
9 // ./main42.exe main42.cmnd hepmcout41.dat > out
10 // The main program contains no analysis; this is intended to happen later.
11 // It therefore "never" has to be recompiled to handle different tasks.
12 
13 // WARNING: typically one needs 25 MB/100 events at the LHC.
14 // Therefore large event samples may be impractical.
15 
16 #include "Pythia.h"
17 #include "HepMCInterface.h"
18 
19 #include "HepMC/GenEvent.h"
20 #include "HepMC/IO_GenEvent.h"
21 
22 // Following line is a deprecated alternative, removed in recent versions.
23 //#include "HepMC/IO_Ascii.h"
24 //#include "HepMC/IO_AsciiParticles.h"
25 
26 // Following line to be used with HepMC 2.04 onwards.
27 #ifdef HEPMC_HAS_UNITS
28 #include "HepMC/Units.h"
29 #endif
30 
31 using namespace Pythia8;
32 
33 int main(int argc, char* argv[]) {
34 
35  // Check that correct number of command-line arguments
36  if (argc != 3) {
37  cerr << " Unexpected number of command-line arguments. \n You are"
38  << " expected to provide one input and one output file name. \n"
39  << " Program stopped! " << endl;
40  return 1;
41  }
42 
43  // Check that the provided input name corresponds to an existing file.
44  ifstream is(argv[1]);
45  if (!is) {
46  cerr << " Command-line file " << argv[1] << " was not found. \n"
47  << " Program stopped! " << endl;
48  return 1;
49  }
50 
51  // Confirm that external files will be used for input and output.
52  cout << "\n >>> PYTHIA settings will be read from file " << argv[1]
53  << " <<< \n >>> HepMC events will be written to file "
54  << argv[2] << " <<< \n" << endl;
55 
56  // Interface for conversion from Pythia8::Event to HepMC one.
57  HepMC::I_Pythia8 ToHepMC;
58  // ToHepMC.set_crash_on_problem();
59 
60  // Specify file where HepMC events will be stored.
61  HepMC::IO_GenEvent ascii_io(argv[2], std::ios::out);
62  // Following line is a deprecated alternative, removed in recent versions
63  // HepMC::IO_Ascii ascii_io("hepmcout32.dat", std::ios::out);
64  // Line below is an eye-readable one-way output, uncomment the include above
65  // HepMC::IO_AsciiParticles ascii_io("hepmcout32.dat", std::ios::out);
66 
67  // Generator.
68  Pythia pythia;
69 
70  // Read in commands from external file.
71  pythia.readFile(argv[1]);
72 
73  // Extract settings to be used in the main program.
74  int nEvent = pythia.mode("Main:numberOfEvents");
75  int nAbort = pythia.mode("Main:timesAllowErrors");
76 
77  // Initialization.
78  pythia.init();
79 
80  // Begin event loop.
81  int iAbort = 0;
82  for (int iEvent = 0; iEvent < nEvent; ++iEvent) {
83 
84  // Generate event.
85  if (!pythia.next()) {
86 
87  // If failure because reached end of file then exit event loop.
88  if (pythia.info.atEndOfFile()) {
89  cout << " Aborted since reached end of Les Houches Event File\n";
90  break;
91  }
92 
93  // First few failures write off as "acceptable" errors, then quit.
94  if (++iAbort < nAbort) continue;
95  cout << " Event generation aborted prematurely, owing to error!\n";
96  break;
97  }
98 
99  // Construct new empty HepMC event.
100 #ifdef HEPMC_HAS_UNITS
101  // This form with arguments is only meaningful for HepMC 2.04 onwards,
102  // and even then unnecessary if HepMC was built with GeV and mm as units.
103  HepMC::GenEvent* hepmcevt = new HepMC::GenEvent(
104  HepMC::Units::GEV, HepMC::Units::MM);
105 #else
106  // This form is needed for backwards compatibility.
107  // In HepMCInterface.cc a conversion from GeV to MeV will be done.
108  HepMC::GenEvent* hepmcevt = new HepMC::GenEvent();
109 #endif
110 
111  // Fill HepMC event, including PDF info.
112  ToHepMC.fill_next_event( pythia, hepmcevt );
113  // This alternative older method fills event, without PDF info.
114  // ToHepMC.fill_next_event( pythia.event, hepmcevt );
115 
116  // Write the HepMC event to file. Done with it.
117  ascii_io << hepmcevt;
118  delete hepmcevt;
119 
120  // End of event loop. Statistics.
121  }
122  pythia.stat();
123 
124  // Done.
125  return 0;
126 }
The GenEvent class is the core of HepMC.
Definition: GenEvent.h:155
IO_GenEvent also deals with HeavyIon and PdfInfo.
Definition: IO_GenEvent.h:63