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) 2014 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 // Author: Mikhail Kirsanov, Mikhail.Kirsanov@cern.ch.
7 // This program illustrates how a file with HepMC events
8 // can be generated by Pythia8.
9 // Input and output files are specified on the command line, e.g. like
10 // ./main42.exe main42.cmnd hepmcout42.dat > out
11 // The main program contains no analysis; this is intended to happen later.
12 // It therefore "never" has to be recompiled to handle different tasks.
13 
14 // WARNING: typically one needs 25 MB/100 events at the LHC.
15 // Therefore large event samples may be impractical.
16 
17 #include "Pythia8/Pythia.h"
18 #include "Pythia8/Pythia8ToHepMC.h"
19 #include "HepMC/GenEvent.h"
20 #include "HepMC/IO_GenEvent.h"
21 
22 using namespace Pythia8;
23 
24 int main(int argc, char* argv[]) {
25 
26  // Check that correct number of command-line arguments
27  if (argc != 3) {
28  cerr << " Unexpected number of command-line arguments. \n You are"
29  << " expected to provide one input and one output file name. \n"
30  << " Program stopped! " << endl;
31  return 1;
32  }
33 
34  // Check that the provided input name corresponds to an existing file.
35  ifstream is(argv[1]);
36  if (!is) {
37  cerr << " Command-line file " << argv[1] << " was not found. \n"
38  << " Program stopped! " << endl;
39  return 1;
40  }
41 
42  // Confirm that external files will be used for input and output.
43  cout << "\n >>> PYTHIA settings will be read from file " << argv[1]
44  << " <<< \n >>> HepMC events will be written to file "
45  << argv[2] << " <<< \n" << endl;
46 
47  // Interface for conversion from Pythia8::Event to HepMC event.
48  HepMC::Pythia8ToHepMC ToHepMC;
49 
50  // Specify file where HepMC events will be stored.
51  HepMC::IO_GenEvent ascii_io(argv[2], std::ios::out);
52 
53  // Generator.
54  Pythia pythia;
55 
56  // Read in commands from external file.
57  pythia.readFile(argv[1]);
58 
59  // Extract settings to be used in the main program.
60  int nEvent = pythia.mode("Main:numberOfEvents");
61  int nAbort = pythia.mode("Main:timesAllowErrors");
62 
63  // Initialization.
64  pythia.init();
65 
66  // Begin event loop.
67  int iAbort = 0;
68  for (int iEvent = 0; iEvent < nEvent; ++iEvent) {
69 
70  // Generate event.
71  if (!pythia.next()) {
72 
73  // If failure because reached end of file then exit event loop.
74  if (pythia.info.atEndOfFile()) {
75  cout << " Aborted since reached end of Les Houches Event File\n";
76  break;
77  }
78 
79  // First few failures write off as "acceptable" errors, then quit.
80  if (++iAbort < nAbort) continue;
81  cout << " Event generation aborted prematurely, owing to error!\n";
82  break;
83  }
84 
85  // Construct new empty HepMC event and fill it.
86  // Units will be as chosen for HepMC build, but can be changed
87  // by arguments, e.g. GenEvt( HepMC::Units::GEV, HepMC::Units::MM)
88  HepMC::GenEvent* hepmcevt = new HepMC::GenEvent();
89  ToHepMC.fill_next_event( pythia, hepmcevt );
90 
91  // Write the HepMC event to file. Done with it.
92  ascii_io << hepmcevt;
93  delete hepmcevt;
94 
95  // End of event loop. Statistics.
96  }
97  pythia.stat();
98 
99  // Done.
100  return 0;
101 }
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