]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PYTHIA8/pythia8145/examples/main32.cc
Use Output directive instead of the old OutputFile and OUtputArchive. Save fileinfo...
[u/mrichter/AliRoot.git] / PYTHIA8 / pythia8145 / examples / main32.cc
1 // main32.cc is a part of the PYTHIA event generator.
2 // Copyright (C) 2010 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 // ./main32.exe main32.cmnd hepmcout32.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 #include "Pythia.h"
14 #include "HepMCInterface.h"
15
16 #include "HepMC/GenEvent.h"
17 #include "HepMC/IO_GenEvent.h"
18 // Following line to be used with HepMC 2.04 onwards.
19 //#include "HepMC/Units.h"
20 // Following two lines are deprecated alternative.
21 //#include "HepMC/IO_Ascii.h"
22 //#include "HepMC/IO_AsciiParticles.h"
23
24 using namespace Pythia8; 
25
26 int main(int argc, char* argv[]) {
27
28   // Check that correct number of command-line arguments
29   if (argc != 3) {
30     cerr << " Unexpected number of command-line arguments. \n You are"
31          << " expected to provide one input and one output file name. \n"
32          << " Program stopped! " << endl;
33     return 1;
34   }
35
36   // Check that the provided input name corresponds to an existing file.
37   ifstream is(argv[1]);  
38   if (!is) {
39     cerr << " Command-line file " << argv[1] << " was not found. \n"
40          << " Program stopped! " << endl;
41     return 1;
42   }
43
44   // Confirm that external files will be used for input and output.
45   cout << " PYTHIA settings will be read from file " << argv[1] << endl;
46   cout << " HepMC events will be written to file " << argv[2] << endl;
47
48   // Interface for conversion from Pythia8::Event to HepMC one. 
49   HepMC::I_Pythia8 ToHepMC;
50   //  ToHepMC.set_crash_on_problem();
51
52   // Specify file where HepMC events will be stored.
53   HepMC::IO_GenEvent ascii_io(argv[2], std::ios::out);
54   // Following two lines are deprecated alternative.
55   // HepMC::IO_Ascii ascii_io(argv[2], std::ios::out);
56   // HepMC::IO_AsciiParticles ascii_io(argv[2], std::ios::out);
57  
58   // Generator. 
59   Pythia pythia;
60
61   // Read in commands from external file.
62   pythia.readFile(argv[1]);    
63
64   // Extract settings to be used in the main program.
65   int    nEvent    = pythia.mode("Main:numberOfEvents");
66   int    nShow     = pythia.mode("Main:timesToShow");
67   int    nAbort    = pythia.mode("Main:timesAllowErrors");
68   bool   showCS    = pythia.flag("Main:showChangedSettings");
69   bool   showAS    = pythia.flag("Main:showAllSettings");
70   bool   showCPD   = pythia.flag("Main:showChangedParticleData");
71   bool   showAPD   = pythia.flag("Main:showAllParticleData");
72  
73   // Initialization. Beam parameters set in .cmnd file.
74   pythia.init();
75
76   // List settings.
77   if (showCS) pythia.settings.listChanged();
78   if (showAS) pythia.settings.listAll();
79
80   // List particle data.  
81   if (showCPD) pythia.particleData.listChanged();
82   if (showAPD) pythia.particleData.listAll();
83
84   // Begin event loop.
85   int nPace  = max(1, nEvent / max(1, nShow) ); 
86   int iAbort = 0; 
87   for (int iEvent = 0; iEvent < nEvent; ++iEvent) {
88     if (nShow > 0 && iEvent%nPace == 0) 
89       cout << " Now begin event " << iEvent << endl;
90
91     // Generate event. 
92     if (!pythia.next()) {
93
94       // If failure because reached end of file then exit event loop.
95       if (pythia.info.atEndOfFile()) {
96         cout << " Aborted since reached end of Les Houches Event File\n"; 
97         break; 
98       }
99
100       // First few failures write off as "acceptable" errors, then quit.
101       if (++iAbort < nAbort) continue;
102       cout << " Event generation aborted prematurely, owing to error!\n"; 
103       break;
104     }
105
106     // Construct new empty HepMC event. Form with arguments is only
107     // meaningful for HepMC 2.04 onwards, and even then unnecessary  
108     // if HepMC was built with GeV and mm as units from the onset. 
109     HepMC::GenEvent* hepmcevt = new HepMC::GenEvent();
110     //HepMC::GenEvent* hepmcevt = new HepMC::GenEvent(
111     //  HepMC::Units::GEV, HepMC::Units::MM); 
112
113     // Fill HepMC event, including PDF info.
114     ToHepMC.fill_next_event( pythia, hepmcevt );
115     // This alternative older method fills event, without PDF info.
116     // ToHepMC.fill_next_event( pythia.event, hepmcevt );
117
118     // Write the HepMC event to file. Done with it.
119     ascii_io << hepmcevt;
120     delete hepmcevt;
121
122   // End of event loop. Statistics. 
123   }
124   pythia.statistics();
125
126   // Done.
127   return 0;
128 }