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