Crombie Tools
XSecAdder.cc
Go to the documentation of this file.
1 /**
2  @file XSecAdder.cc
3  Describes the XSecAdder class.
4  @author Daniel Abercrombie <dabercro@mit.edu>
5 */
6 
7 #include <iostream>
8 #include <map>
9 #include "TFile.h"
10 #include "TTree.h"
11 #include "TBranch.h"
12 #include "XSecAdder.h"
13 
15 
16 //--------------------------------------------------------------------
17 XSecAdder::XSecAdder(TString name) :
18  fBranchName(name)
19 {
20  SetKeepAllFiles(true);
21  SetMultiplyLumi(false);
22 }
23 
24 //--------------------------------------------------------------------
26 { }
27 
28 //--------------------------------------------------------------------
30 {
31  RunThreads();
32 }
33 
35 
36  TFile *file = new TFile(info.fFileName,"UPDATE");
37  if (!file)
38  exit(0);
39 
40  TTree *tree = (TTree*) file->Get(fInTreeName);
41  TTree *outTree;
42 
44  outTree = new TTree(fOutTreeName, fOutTreeName);
45  else
46  outTree = tree;
47 
48  Int_t nentries = tree->GetEntries();
49  Float_t weight = info.fXSecWeight;
50 
51  std::map<TString, Float_t> mergeBranches;
52 
53  for (UInt_t iMerge = 0; iMerge != fMergeBranches.size(); ++iMerge) {
54  mergeBranches[fMergeBranches[iMerge]] = 1.0;
55  tree->SetBranchAddress(fMergeBranches[iMerge], &(mergeBranches[fMergeBranches[iMerge]]));
56  }
57 
58  if (fOutTreeName != fInTreeName || (!tree->GetBranch(fBranchName))) {
59 
60  Float_t XSecWeight = 1.;
61  TBranch *XSecWeightBr = outTree->Branch(fBranchName, &XSecWeight,fBranchName + "/F");
62 
63  for (int entry = 0; entry < nentries; entry++) {
64  if (entry % 500000 == 0) { /// @todo Make a ReportFrequency class
65  output_lock.Lock();
66  std::cout << "Processing " << info.fFileName << " ... " << float(entry)/float(nentries)*100 << "%" << std::endl;
67  output_lock.UnLock();
68  }
69  XSecWeight = weight;
70 
71  if (fMergeBranches.size() != 0)
72  tree->GetEntry(entry);
73 
74  for (UInt_t iMerge = 0; iMerge != fMergeBranches.size(); ++iMerge)
75  XSecWeight *= mergeBranches[fMergeBranches[iMerge]];
76 
78  XSecWeightBr->Fill();
79  else
80  outTree->Fill();
81  }
83  outTree->Write();
84  else
85  outTree->Write(fOutTreeName, TObject::kOverwrite);
86  }
87 
88  file->Close();
89 
90 }