Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pythia_main.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file pythia_main.cpp
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 
5 // GNU long option variation of getopt, for command line option parsing.
6 #include <getopt.h>
7 
8 // ROOT headers.
9 #include <TFile.h>
10 #include <TObjString.h>
11 #include <TTree.h>
12 
13 #include "factory.h"
14 #include "pythia_commons.h"
15 #include "pythia_erhic.h"
16 
17 // Forward declarations
18 std::string initialise(int argc, char* argv[]);
19 void write_run_data(TFile&);
20 
25 int main(int argc, char* argv[]) {
26  std::string filename = initialise(argc, argv);
27 
28  // Open the output file/tree and create an event factory,
29  // which we use to add the event branch to the tree.
30  TFile file(filename.c_str(), "recreate");
31  TTree tree("EICTree", "Output direct from pythia!");
32  Factory factory;
33  factory.Branch(tree, "event");
34 
35  std::stringstream stream;
36  // The PYTHIA6 generate() subroutine handles tracking the event
37  // count. Continue looping until it returns a non-zero value,
38  // indicating the requested number of events have been generated.
39  while(__pythia6_MOD_generate() == 0) {
40  erhic::EventPythia* event = factory.Create();
41  event->SetN(tree.GetEntries() + 1);
42  tree.Fill();
43  // \todo Add radiative photon
44  std::cout << std::endl;
45  } // for
46  file.Write(); // Writes the ROOT TTree
49  return 0;
50 }
51 
56 std::string initialise(int argc, char* argv[]) {
57  std::string filename("pythia.root");
58  // Parse command line options.
59  __pythia6_MOD_printascii = 1; // Generate ASCII output by default
60  static struct option long_options[] = {
61  // These set a flag.
62  {"ascii", no_argument, &__pythia6_MOD_printascii, 1},
63  {"noascii", no_argument, &__pythia6_MOD_printascii, 0},
64  // These don't set a flag.
65  // The arguments are processed below.
66  {"out", required_argument, NULL, 'o'},
67  {NULL, 0, NULL, 0}
68  };
69  // Loop through options.
70  int option_index = 0;
71  int code(0);
72  while((code = getopt_long(argc, argv, "o:r:",
73  long_options, &option_index)) not_eq -1) {
74  switch(code) {
75  case 0:
76  if(long_options[option_index].flag not_eq 0) {
77  break;
78  } // if
79  printf("option %s", long_options[option_index].name);
80  if(optarg) {
81  printf (" with arg %s", optarg);
82  } // if
83  printf("\n");
84  break;
85  case 'o':
86  filename = optarg;
87  break;
88  default:
89  abort();
90  } // switch
91  } // while
92  // Now that we have processed all command line arguments, call
93  // the Fortran PYTHIA6 initialisation routine.
94  // This handles processing options from the steer file.
95  // If the return value is non-zero, exit.
96  int result = __pythia6_MOD_initialise();
97  if(0 not_eq result) {
98  exit(result);
99  } // if
100  return filename;
101 }
102 
112 void write_run_data(TFile& file) {
113  TObjString text;
114  std::stringstream stream;
115  // Total cross section is stored in PYTHIA in pari(1) in millibarns.
116  // Multiply by 1000 to get it in microbarn.
117  stream << pari(1) * 1000.;
118  text.SetString(stream.str().c_str());
119  file.WriteObject(&text, "crossSection");
120  // Total number of generated events is stored in PYTHIA in msti(5).
121  stream.str("");
122  stream.clear();
123  stream << msti(5);
124  text.SetString(stream.str().c_str());
125  file.WriteObject(&text, "nEvents");
126  // Total number of generateds is stored in PYTHIA in ngen(0, 3).
127  stream.str("");
128  stream.clear();
129  stream << ngen(0, 3);
130  text.SetString(stream.str().c_str());
131  file.WriteObject(&text, "nTrials");
132 }