1 // main16.cc is a part of the PYTHIA event generator.
2 // Copyright (C) 2012 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.
6 // This is a simple test program.
7 // It illustrates (a) how to collect the analysis code in a separate class
8 // and (b) how to provide the .cmnd filename on the command line
10 // Once you have linked the main program you can run it with a command line
11 // ./main16.exe main16.cmnd > out16
15 using namespace Pythia8;
17 //==========================================================================
19 // Put all your own analysis code in the myAnalysis class.
25 // Constructor can be empty.
28 // Initialization actions.
31 // Analysis of each new event.
32 void analyze(Event& event);
34 // Show final results.
39 // Declare variables and objects that span init - analyze - finish.
41 Hist yH, etaChg, mult;
45 //--------------------------------------------------------------------------
47 // The initialization code.
49 void MyAnalysis::init() {
51 // Initialize counter for number of events.
55 yH.book("Higgs rapidity", 100, -10., 10.);
56 etaChg.book("charged pseudorapidity", 100, -10., 10.);
57 mult.book( "charged multiplicity", 100, -0.5, 799.5);
61 //--------------------------------------------------------------------------
63 // The event analysis code.
65 void MyAnalysis::analyze(Event& event) {
70 // Find latest copy of Higgs and plot its rapidity.
72 for (int i = 0; i < event.size(); ++i)
73 if (event[i].id() == 25) iH = i;
74 yH.fill( event[iH].y() );
76 // Plot pseudorapidity distribution. Sum up charged multiplicity.
78 for (int i = 0; i < event.size(); ++i)
79 if (event[i].isFinal() && event[i].isCharged()) {
80 etaChg.fill( event[i].eta() );
87 //--------------------------------------------------------------------------
89 // The finishing code.
91 void MyAnalysis::finish() {
93 // Normalize histograms.
94 double binFactor = 5. / nEvt;
99 cout << yH << etaChg << mult;
103 //==========================================================================
105 // You should not need to touch the main program: its actions are
106 // determined by the .cmnd file and the rest belongs in MyAnalysis.
108 int main(int argc, char* argv[]) {
110 // Check that correct number of command-line arguments
112 cerr << " Unexpected number of command-line arguments. \n"
113 << " You are expected to provide a file name and nothing else. \n"
114 << " Program stopped! " << endl;
118 // Check that the provided file name corresponds to an existing file.
119 ifstream is(argv[1]);
121 cerr << " Command-line file " << argv[1] << " was not found. \n"
122 << " Program stopped! " << endl;
126 // Confirm that external file will be used for settings..
127 cout << " PYTHIA settings will be read from file " << argv[1] << endl;
129 // Declare generator. Read in commands from external file.
131 pythia.readFile(argv[1]);
136 // Declare user analysis class. Do initialization part of it.
137 MyAnalysis myAnalysis;
140 // Read in number of event and maximal number of aborts.
141 int nEvent = pythia.mode("Main:numberOfEvents");
142 int nAbort = pythia.mode("Main:timesAllowErrors");
146 for (int iEvent = 0; iEvent < nEvent; ++iEvent) {
148 // Generate events. Quit if too many failures.
149 if (!pythia.next()) {
150 if (++iAbort < nAbort) continue;
151 cout << " Event generation aborted prematurely, owing to error!\n";
155 // User Analysis of current event.
156 myAnalysis.analyze( pythia.event);
158 // End of event loop.