eic-smear  1.0.3
A collection of ROOT classes for Monte Carlo events and a fast-smearing code simulating detector effects for the Electron-Ion Collider task force
SmearTree.cxx
Go to the documentation of this file.
1 
10 #include <iostream>
11 #include <fstream>
12 #include <sstream>
13 #include <cmath>
14 #include <memory>
15 
16 #include <TClass.h>
17 #include <TROOT.h>
18 #include <TSystem.h>
19 #include <TChain.h>
20 #include <TString.h>
21 #include <TRandom2.h>
22 #include <TTree.h>
23 #include <TFile.h>
24 #include <TStopwatch.h>
25 #include <TH1D.h>
26 
31 #include "eicsmear/smear/Smear.h"
32 
33 #ifdef WITH_PYTHIA6
34 #include "eicsmear/hadronic/EventSmear.h"
35 #endif
36 
44 int SmearTree(const Smear::Detector& detector, const TString& inFileName,
45  const TString& outFileName, Long64_t nEvents) {
46  // Open the input file and get the Monte Carlo tree from it.
47  // Complain and quit if we don't find the file or the tree.
48  TFile inFile(inFileName, "READ");
49  if (!inFile.IsOpen()) {
50  std::cerr << "Unable to open " << inFileName << std::endl;
51  } // if
52  TTree* mcTree(NULL);
53  inFile.GetObject("EICTree", mcTree);
54  if (!mcTree) {
55  std::cerr << "Unable to find EICTree in " << inFileName << std::endl;
56  return 1;
57  } // if
58  std::auto_ptr<erhic::VirtualEventFactory> builder;
59  // Need to determine the type of object in the tree to choose
60  // the correct smeared event builder.
61  TClass branchClass(mcTree->GetBranch("event")->GetClassName());
62  if (branchClass.InheritsFrom("erhic::EventDis")) {
63  builder.reset(new Smear::EventDisFactory(detector,
64  *(mcTree->GetBranch("event"))));
65 #ifdef WITH_PYTHIA6
66  } else if (branchClass.InheritsFrom("erhic::hadronic::EventMC")) {
67  builder.reset(new Smear::HadronicEventBuilder(detector,
68  *(mcTree->GetBranch("event"))));
69 #endif
70  } else {
71  std::cerr << branchClass.GetName() << " is not supported for smearing" <<
72  std::endl;
73  } // if
74  // Open the output file.
75  // Complain and quit if something goes wrong.
76  TString outName(outFileName);
77  if (outName.IsNull()) {
78  outName = TString(inFileName).ReplaceAll(".root", ".smear.root");
79  } // if
80  TFile outFile(outName, "RECREATE");
81  if (!outFile.IsOpen()) {
82  std::cerr << "Unable to create " << outName << std::endl;
83  return 1;
84  } // if
85  TTree smearedTree("Smeared", "A tree of smeared Monte Carlo events");
86  TBranch* eventbranch = builder->Branch(smearedTree, "eventS");
87  if (mcTree->GetEntries() < nEvents || nEvents < 1) {
88  nEvents = mcTree->GetEntries();
89  } // if
90  std::cout <<
91  "/-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-/"
92  << std::endl;
93  std::cout <<
94  "/ Commencing Smearing of " << nEvents << " events."
95  << std::endl;
96  std::cout <<
97  "/-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-/"
98  << std::endl;
99  for (Long64_t i(0); i < nEvents; i++) {
100  if (i % 10000 == 0 && i != 0) {
101  std::cout << "Processing event " << i << std::endl;
102  } // if
103  mcTree->GetEntry(i);
104  builder->Fill(*eventbranch);
105  } // for
106  smearedTree.Write();
107  detector.Write("detector");
108  outFile.Purge();
109  std::cout <<
110  "|~~~~~~~~~~~~~~~~~~ Completed Successfully ~~~~~~~~~~~~~~~~~~~|"
111  << std::endl;
112  return 0;
113 }
int SmearTree(const Smear::Detector &detector, const TString &inFileName, const TString &outFileName, Long64_t nEvents)
Definition: SmearTree.cxx:44