Crombie Tools
Corrector.cc
Go to the documentation of this file.
1 /**
2  @file Corrector.cc
3  Describes the Corrector class.
4  @author Daniel Abercrombie <dabercro@mit.edu>
5 */
6 
7 #include <cassert>
8 #include <regex>
9 #include <utility>
10 #include <exception>
11 
12 #include "TAxis.h"
13 #include "Corrector.h"
14 
16 
17 //--------------------------------------------------------------------
18 Corrector::Corrector(TString name) :
19  fName(name)
20 { }
21 
22 //--------------------------------------------------------------------
24 {
25  if (fCorrectionFile != NULL && fCorrectionFile->IsOpen() && !fIsCopy)
26  fCorrectionFile->Close();
27 
28  if (fCutFormula != NULL)
29  delete fCutFormula;
30 
31  for (UInt_t iFormula = 0; iFormula != fFormulas.size(); ++iFormula)
32  delete fFormulas[iFormula];
33 }
34 
35 //--------------------------------------------------------------------
36 void Corrector::SetCorrectionFile(TString fileName)
37 {
38  fCorrectionFile = new TFile(fileName);
39  if (fCorrectionFile == NULL) {
40  Message(eError, "Could not open", fileName.Data());
41  exit(1);
42  }
43 }
44 
45 //--------------------------------------------------------------------
46 void Corrector::SetCorrectionHist(TString histName)
47 {
48  fCorrectionHist = (TH1*) fCorrectionFile->Get(histName);
49  if (fCorrectionHist == NULL) {
50  Message(eError, "Could not load", histName.Data(),
51  ": Looking inside", fCorrectionFile->GetName());
52  throw std::exception{};
53  }
54  SetMinMax();
55 }
56 
57 //--------------------------------------------------------------------
58 void Corrector::SetCorrectionHist(TString hist1, TString hist2)
59 {
60  fCorrectionHist = (TH1*) fCorrectionFile->Get(hist1);
61  if (fCorrectionHist == NULL) {
62  Message(eError, "Could not load", hist1.Data(),
63  ": Looking inside", fCorrectionFile->GetName());
64  throw std::exception{};
65  }
66 
67  TH1* divisorHist = (TH1*) fCorrectionFile->Get(hist2);
68  if (divisorHist == NULL) {
69  Message(eError, "Could not load", hist2.Data(),
70  ": Looking inside", fCorrectionFile->GetName());
71  throw std::exception{};
72  }
73 
74  fCorrectionHist->Divide(divisorHist);
75 
76  SetMinMax();
77 }
78 
79 //--------------------------------------------------------------------
80 Double_t Corrector::GetFormulaResult(Int_t index, Bool_t use_mins)
81 {
82  Double_t eval = fFormulas[index]->EvalInstance();
83  if (not use_mins)
84  return eval;
85 
86  auto minmax = bin_num ?
87  make_pair(bin_min, bin_max) :
88  make_pair(fMins[index], fMaxs[index]);
89 
90  if (eval < minmax.first)
91  eval = minmax.first;
92  else if (eval > minmax.second)
93  eval = minmax.second;
94  return eval;
95 }
96 
97 //--------------------------------------------------------------------
98 Float_t Corrector::DoEval() {
99 
100  Double_t evalX = GetFormulaResult(0);
101 
102  if (fNumDims > 1) {
103  Double_t evalY = GetFormulaResult(1);
104  if (fNumDims > 2) {
105  Double_t evalZ = GetFormulaResult(2);
106  return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX, evalY, evalZ));
107  }
108  return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX, evalY));
109  }
110  if (bin_num) {
111  assert(bin_num == fCorrectionHist->GetNbinsX());
112  Int_t which_bin = (evalX - bin_min)/(bin_max - bin_min) * bin_num + 1;
113  return fCorrectionHist->GetBinContent(which_bin);
114  }
115  return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX));
116 }
117 
118 //--------------------------------------------------------------------
119 
120 /**
121  @returns value of correction histogram using the expressions added
122  through AddInExpression(), unless the event does not pass the cut
123  set by SetInCut(). In that case, a default value is returned
124  (depending on if it's a scale factor or an uncertainty).
125  First part of the pair is a bool determining whether or not that cut was passed
126 */
127 
128 std::pair<bool, Float_t> Corrector::EvaluateWithFlag()
129 {
130  bool flag = false;
131  Float_t Output = (fHistReader == eZeroCenteredUnc) ? 0.0 : 1.0;
132 
133  if (fMatchedFileName && fInTree != NULL && fCutFormula->EvalInstance() != 0) {
134  flag = true;
135  Output = DoEval();
136  }
137 
139  Output -= 1.0;
140 
141  return std::make_pair(flag, Output);
142 
143 }
144 
145 //--------------------------------------------------------------------
146 
147 /**
148  @returns value of correction histogram using the expressions added
149  through AddInExpression(), unless the event does not pass the cut
150  set by SetInCut(). In that case, a default value is returned
151  (depending on if it's a scale factor or an uncertainty).
152 */
153 
155 {
156 
157  return EvaluateWithFlag().second;
158 
159 }
160 
161 //--------------------------------------------------------------------
163 {
164  if (fCutFormula)
165  delete fCutFormula;
166 
167  fCutFormula = new TTreeFormula(fInCut, fInCut, fInTree);
168 
169  if (fFormulas.size() != 0) {
170  for (auto iFormula : fFormulas)
171  delete iFormula;
172  fFormulas.resize(0);
173  }
174 
175  if (fInExpressions.size() != 0) {
176  TTreeFormula* tempFormula;
177  for (auto& iExpression : fInExpressions) {
178  tempFormula = new TTreeFormula(iExpression, iExpression, fInTree);
179  fFormulas.push_back(tempFormula);
180  }
181  }
182 }
183 
184 //--------------------------------------------------------------------
185 Bool_t Corrector::CompareFileName(TString fileName) {
186  fMatchedFileName = (fMatchFileName == "") or (std::regex_match(fileName.Data(), std::regex(fMatchFileName.Data())));
187  return fMatchedFileName;
188 }
189 
190 //--------------------------------------------------------------------
192 {
193  for (Int_t iDim = 0; iDim != fNumDims; ++iDim) {
194  TAxis* theAxis;
195  if (iDim == 0)
196  theAxis = fCorrectionHist->GetXaxis();
197  else if (iDim == 1)
198  theAxis = fCorrectionHist->GetYaxis();
199  else if (iDim == 2)
200  theAxis = fCorrectionHist->GetZaxis();
201  else {
202  Message(eError, "I don't support this many axes at the moment.");
203  exit(3);
204  }
205  fMins.push_back(theAxis->GetBinCenter(theAxis->GetFirst()));
206  fMaxs.push_back(theAxis->GetBinCenter(theAxis->GetLast()));
207  }
208 }
209 
210 //--------------------------------------------------------------------
212 {
214  *newCorrector = *this;
215  newCorrector->fIsCopy = true;
216  return newCorrector;
217 }
218 
219 std::vector<TString> Corrector::GetFormulas() {
220  std::vector<TString> all_formulas {fInExpressions};
221  all_formulas.push_back(fInCut);
222  return all_formulas;
223 }