Crombie Tools
LimitTreeMaker.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 
4 #include "TFile.h"
5 #include "TTree.h"
6 #include "TBranch.h"
7 #include "TH1.h"
8 
9 #include "LimitTreeMaker.h"
10 
12 
13 //--------------------------------------------------------------------
14 LimitTreeMaker::LimitTreeMaker(TString outputName) :
15  fReportFrequency(10000),
16  fOutputFileName(outputName),
17  fTreeName("events"),
18  fOutputWeightBranch("weight")
19 {
20  fFriendNames.resize(0);
21  fKeepBranches.resize(0);
22  fWeightBranch.resize(0);
23  fRegionNames.resize(0);
24  fRegionCuts.resize(0);
25 }
26 
27 //--------------------------------------------------------------------
29 { }
30 
31 //--------------------------------------------------------------------
32 void
33 LimitTreeMaker::ReadExceptionConfig(const char* config, TString region, TString fileDir)
34 {
35  if (fileDir != "")
36  SetInDirectory(fileDir);
37 
38  Message(eInfo, "Opening %s for region %s", config, region.Data());
39 
40  std::ifstream configFile;
41  configFile.open(config);
42  TString LimitTreeName;
43  TString FileName;
44  TString XSec;
45  TString LegendEntry;
46  TString ColorStyleEntry;
47 
48  while (!configFile.eof()) {
49  configFile >> LimitTreeName >> FileName;
50 
51  if (configFile.eof())
52  continue;
53 
54  if (LimitTreeName == "skip") {
55  Message(eDebug, "Going to skip %s in region %s", FileName.Data(), region.Data());
56  ExceptionSkip(region, AddInDir(FileName));
57  }
58  else {
59  Message(eDebug, "Adding %s as %s", FileName.Data(), LimitTreeName.Data());
60  configFile >> XSec >> LegendEntry >> ColorStyleEntry;
61  ExceptionAdd(region, AddInDir(FileName), LimitTreeName, XSec.Atof());
62  if (ColorStyleEntry == "rgb") {
63  for (Int_t iColor = 0; iColor != 3; ++iColor)
64  configFile >> ColorStyleEntry;
65  }
66  Message(eDebug, "Now have %i exceptions", fExceptionFileNames[region].size());
67  }
68  }
69  configFile.close();
70 }
71 
72 //--------------------------------------------------------------------
73 void
75 {
76  if (fReportFrequency < 1)
77  fReportFrequency = 10000;
78  TFile* outClear = new TFile(AddOutDir(fOutputFileName),"RECREATE");
79  outClear->Close();
80 
81  for (UInt_t iRegion = 0; iRegion != fRegionNames.size(); ++iRegion) {
82  TString regionName = fRegionNames[iRegion];
83 
84  Message(eInfo, "Entering region: %s", regionName.Data());
85 
86  UInt_t numData = fDataFileInfo.size();
87  UInt_t numDataBackground = numData + fMCFileInfo.size();
88  UInt_t sumOfStandard = numDataBackground + fSignalFileInfo.size();
89  UInt_t numFiles = sumOfStandard + fExceptionFileNames[regionName].size();
90 
91  Message(eInfo, "Number of Data Files: %i", numData);
92  Message(eInfo, "Number of Background Files: %i", numDataBackground);
93  Message(eInfo, "Number of Signal Files: %i", fSignalFileInfo.size());
94  Message(eInfo, "Number of Standard Files (sum of previous): %i", sumOfStandard);
95  Message(eInfo, "Number of files in this region (adding exception): %i", numFiles);
96 
97  for (UInt_t iFile = 0; iFile != numFiles; ++iFile) {
98  Message(eDebug, "Starting file %i/%i", iFile, numFiles);
99 
100  if (iFile % fReportFrequency == 0 && fReportFrequency < numFiles) {
101  std::cout << fOutputFileName << " : ";
102  for (UInt_t jRegion = 0; jRegion != fRegionNames.size(); ++jRegion) {
103  if (jRegion == iRegion)
104  std::cout << "[[" << fRegionNames[jRegion] << "]], ";
105  else
106  std::cout << fRegionNames[jRegion] << ", ";
107  }
108  std::cout << " " << iFile << " / " << numFiles << " Files." << std::endl;
109  }
110 
111  TString fileName;
112  TString outTreeName;
113  Float_t XSec;
114 
115  if (iFile < numData) {
116  Message(eDebug, "Opening data file");
117  outTreeName = fDataFileInfo[iFile]->fTreeName;
118  fileName = fDataFileInfo[iFile]->fFileName;
119  XSec = fDataFileInfo[iFile]->fXSec;
120  }
121  else if (iFile < numDataBackground) {
122  Message(eDebug, "Opening background file");
123  outTreeName = fMCFileInfo[iFile - numData]->fTreeName;
124  fileName = fMCFileInfo[iFile - numData]->fFileName;
125  XSec = fMCFileInfo[iFile - numData]->fXSec;
126  }
127  else if (iFile < sumOfStandard) {
128  Message(eDebug, "Opening signal file");
129  outTreeName = fSignalFileInfo[iFile - numDataBackground]->fTreeName;
130  fileName = fSignalFileInfo[iFile - numDataBackground]->fFileName;
131  XSec = fSignalFileInfo[iFile - numDataBackground]->fXSec;
132  }
133  else {
134  Message(eDebug, "Opening exception file");
135  fileName = (fExceptionFileNames[regionName])[iFile - sumOfStandard];
136  outTreeName = (fExceptionTreeNames[regionName])[iFile - sumOfStandard];
137  XSec = (fExceptionXSecs[regionName])[iFile - sumOfStandard];
138  }
139 
140  Message(eDebug, "Name: %s, Out Tree: %s, Cross Section: %f",
141  fileName.Data(), outTreeName.Data(), XSec);
142 
143  if (iFile < sumOfStandard) {
144  if (fExceptionSkip[regionName].find(outTreeName) != fExceptionSkip[regionName].end()) {
145  Message(eInfo, "Found %s in skip list for Region %s... Skipping.",
146  outTreeName.Data(), regionName.Data());
147  continue;
148  }
149  if (fExceptionSkip[regionName].find(fileName) != fExceptionSkip[regionName].end()) {
150  Message(eInfo, "Found %s in skip list for Region %s... Skipping.",
151  fileName.Data(), regionName.Data());
152  continue;
153  }
154  }
155 
156  TFile* inFile = new TFile(fileName);
157  if (!inFile) {
158  Message(eError, "Could not open file %s", fileName.Data());
159  exit(1);
160  }
161 
162  TTree* inTree = (TTree*) inFile->Get(fTreeName);
163  if (!inTree) {
164  Message(eError, "Could not find tree %s\nin file %s", fTreeName.Data(), fileName.Data());
165  inFile->Close();
166  exit(1);
167  }
168 
169  // Apply selection
170  TFile* tempFile = new TFile(TString("/tmp/") + getenv("USER") + "/" +
171  fOutputFileName + ".tempCopyFileDontUse.root","RECREATE");
172  TString theCut = fRegionCuts[iRegion];
173  if (XSec < 0 && fExceptionDataCuts[regionName] != "")
174  theCut = TString("(") + theCut + ") && (" + fExceptionDataCuts[regionName] + ")";
175 
176  TTree* loopTree = inTree->CopyTree(theCut);
177  // Initialize output tree
178  TFile* outFile = new TFile(AddOutDir(fOutputFileName),"UPDATE");
179  TTree* outTree = new TTree(outTreeName + "_" + regionName,outTreeName + "_" + regionName);
180 
181  // Setup the branches to keep
182  std::map<TString, Float_t> addresses;
183  std::map<TString, Int_t> intAddresses;
184  for (UInt_t iKeep = 0; iKeep != fKeepBranches.size(); ++iKeep) {
185  if (fKeepBranchIsInt[iKeep]) {
186  intAddresses[fKeepBranches[iKeep]] = 0.0;
187  outTree->Branch(fKeepBranches[iKeep],&intAddresses[fKeepBranches[iKeep]],fKeepBranches[iKeep] + "/F");
188  loopTree->SetBranchAddress(fKeepBranches[iKeep],&intAddresses[fKeepBranches[iKeep]]);
189  }
190  else {
191  addresses[fKeepBranches[iKeep]] = 0.0;
192  outTree->Branch(fKeepBranches[iKeep],&addresses[fKeepBranches[iKeep]],fKeepBranches[iKeep] + "/F");
193  loopTree->SetBranchAddress(fKeepBranches[iKeep],&addresses[fKeepBranches[iKeep]]);
194  }
195  }
196 
197  // Get the all histogram and calculate x-section weight
198  TH1* allHist = (TH1*) inFile->Get(fAllHistName);
199  Float_t mcScale = fLuminosity*XSec/(allHist->GetBinContent(1));
200  // Setup the output weight branch
201  Float_t mcWeight = 1.0;
202  TBranch* weightBranch = outTree->Branch(fOutputWeightBranch,&mcWeight,fOutputWeightBranch+"/F");
203  // Get branches for weights
204  for (UInt_t iWeight = 0; iWeight != fWeightBranch.size(); ++iWeight) {
205  addresses[fWeightBranch[iWeight]] = 0.0;
206  loopTree->SetBranchAddress(fWeightBranch[iWeight],&addresses[fWeightBranch[iWeight]]);
207  }
208  for (UInt_t iWeight = 0; iWeight != fExceptionWeightBranches[regionName].size(); ++iWeight) {
209  addresses[(fExceptionWeightBranches[regionName])[iWeight]] = 0.0;
210  loopTree->SetBranchAddress((fExceptionWeightBranches[regionName])[iWeight],
211  &addresses[(fExceptionWeightBranches[regionName])[iWeight]]);
212  }
213 
214  UInt_t nentries = loopTree->GetEntries();
215  for (UInt_t iEntry = 0; iEntry != nentries; ++iEntry) {
216  loopTree->GetEntry(iEntry);
217  if (mcScale > 0) {
218  mcWeight = mcScale;
219  for (UInt_t iWeight = 0; iWeight != fWeightBranch.size(); ++iWeight)
220  mcWeight *= addresses[fWeightBranch[iWeight]];
221  for (UInt_t iWeight = 0; iWeight != fExceptionWeightBranches[regionName].size(); ++iWeight)
222  mcWeight *= addresses[(fExceptionWeightBranches[regionName])[iWeight]];
223  }
224  else
225  mcWeight = 1.0;
226 
227  outTree->Fill();
228  }
229  outFile->cd();
230  outFile->WriteTObject(outTree,outTreeName + "_" + regionName,"Overwrite");
231  outFile->Close();
232 
233  tempFile->Close();
234  inFile->Close();
235  addresses.clear();
236  intAddresses.clear();
237  }
238  }
239 }