EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Pythia6.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Pythia6.cxx
1 
10 #include "eicsmear/erhic/Pythia6.h"
11 
12 #include <iostream>
13 #include <stdexcept>
14 #include <sstream>
15 #include <string>
16 
17 #include <TFile.h>
18 #include <TObjString.h>
19 #include <TPythia6.h>
20 #include <TStopwatch.h>
21 #include <TTree.h>
22 
26 
27 namespace erhic {
28 
30  VirtualEventFactory* factory,
31  int nEvents,
32  const std::string& treeName,
33  const std::string& branchName,
34  int printInterval)
35 : mPrintInterval(printInterval)
36 , mFile(file)
37 , mTree(NULL)
38 , mNEvents(nEvents)
39 , mNGenerated(0)
40 , mNTrials(0)
41 , mFactory(factory) {
42  try {
43  if (!file) {
44  throw std::runtime_error("No file provided");
45  } // if
46  if (!file->IsWritable()) {
47  throw std::runtime_error("File is not writable");
48  } // if
49  file->cd();
50  mTree = new TTree(treeName.c_str(), "PYTHIA 6 events");
51  mFactory->Branch(*mTree, branchName);
52  } // try
53  catch(std::exception& e) {
54  std::cout << "Caught exception in erhic::Pythia6::Pythia6() - " <<
55  e.what() << std::endl;
56  exit(1);
57  } // catch
58 }
59 
61  // Don't delete the file as that was externally provided
62  // Don't delete the tree as that is associated with the file
63  // and will be deleted when the file closes.
64 }
65 
66 bool Pythia6::Run() {
67  TPythia6* pythia = TPythia6::Instance();
68  TStopwatch timer;
69  double lastTime(0.);
70  while (mTree->GetEntries() < mNEvents) {
71  const int initialNGenerated = pythia->GetMSTI(5);
72  const int initialNTrials = pythia->GetPyint5()->NGEN[2][0];
73  TBranch* branch = mTree->GetBranch("event");
74  mFactory->Fill(*branch);
75  mTree->Fill();
76  // Count the number of generations and trials for this event
77  mNGenerated += pythia->GetMSTI(5) - initialNGenerated;
78  const int trials = pythia->GetPyint5()->NGEN[2][0] - initialNTrials;
79  mNTrials += trials;
80  if ((mTree->GetEntries() % mPrintInterval) == 0) {
81  double time = timer.RealTime();
82  std::cout << mTree->GetEntries() << " events in " <<
83  time << " seconds (+" << time - lastTime << ")" << std::endl;
84  lastTime = time;
85  timer.Start(false);
86  } // if
87  } // while
88  // Write the tree
89  mFile->cd();
90  mTree->Write();
91  mFile->Purge();
92  // Write run information
93  std::stringstream ss;
94  std::string s;
95  // Write the total cross section
96  ss << pythia->GetPARI(1) * 1000.; // * 1000 --> microbarn
97  ss >> s;
98  TObjString(s.c_str()).Write("crossSection");
99  // Write the number of generated events
100  ss.str("");
101  ss.clear();
102  ss << mNGenerated;
103  ss >> s;
104  TObjString(s.c_str()).Write("nEvents");
105  // Write the number of trials required to make the events
106  ss.str("");
107  ss.clear();
108  ss << mNTrials;
109  ss >> s;
110  TObjString(s.c_str()).Write("nTrials");
111  return true;
112 }
113 
114 } // namespace erhic