Crombie Tools
HistAnalysis.cc
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "TH1D.h"
4 #include "TH1F.h"
5 #include "TMath.h"
6 #include "TString.h"
7 
8 #include "PlotBase.h"
9 #include "HistAnalysis.h"
10 
12 
13 //--------------------------------------------------------------------
14 TH1D*
15 HistAnalysis::DoScaleFactors(TString PlotVar, Int_t NumBins, Double_t *XBins,
16  ScaleFactorMethod method)
17 {
18  DisplayFunc(__func__);
19 
20  TString outputName;
21  outputName.Form("ScaleFactors_%d", fNumCreated++);
22  TH1D* output = new TH1D(outputName, outputName, NumBins, XBins);
23  output->Sumw2();
24 
25  ResetWeight();
26 
27  SetDefaultExpr(PlotVar);
28 
29  // First create the data histograms
30  std::vector<TH1D*> dataHists;
31  SetDefaultWeight(fBaseCut);
32  dataHists.push_back(GetHist(NumBins, XBins, kData));
33  for (UInt_t iCut = 0; iCut != fScaleFactorCuts.size(); ++iCut) {
34  SetDefaultWeight(fBaseCut + fDataSFCuts[iCut]);
35  dataHists.push_back(GetHist(NumBins, XBins, kData));
36  }
37 
38  // Then create the signal and background histograms
39  std::vector<TH1D*> signalHists;
40  std::vector<TH1D*> backgroundHists;
41  SetDefaultWeight(fBaseCut);
42  signalHists.push_back(GetHist(NumBins, XBins, fSignalType, fSignalName, fSearchBy));
43  backgroundHists.push_back(GetHist(NumBins, XBins, fSignalType, fSignalName, fSearchBy, false));
44  for (UInt_t iCut = 0; iCut != fScaleFactorCuts.size(); ++iCut) {
45  SetDefaultWeight(fBaseCut + fScaleFactorCuts[iCut]);
46  signalHists.push_back(GetHist(NumBins, XBins, fSignalType, fSignalName, fSearchBy));
47  backgroundHists.push_back(GetHist(NumBins, XBins, fSignalType, fSignalName, fSearchBy, false));
48  }
49 
50  for (Int_t iBin = 1; iBin != NumBins + 1; ++iBin) {
51 
52  // Subtract out the background
53  Double_t scale = -1.0 - fBackgroundChange;
54  if (fNormalized)
55  scale *= dataHists[0]->GetBinContent(iBin)/
56  (backgroundHists[0]->GetBinContent(iBin) +
57  signalHists[0]->GetBinContent(iBin));
58 
59  for (UInt_t iHist = 0; iHist != dataHists.size(); ++iHist)
60  dataHists[iHist]->Add(backgroundHists[iHist], scale);
61 
62  std::vector<Double_t> data_yields;
63  std::vector<Double_t> data_error;
64  std::vector<Double_t> mc_yields;
65  std::vector<Double_t> mc_error;
66 
67  Double_t factor = 1.0;
68 
69  switch (method) {
70 
71  case kCutAndCount:
72 
73  // Get the yields from each histogram
74  for (UInt_t iHist = 0; iHist != dataHists.size(); ++iHist) {
75  Double_t error = 0.0;
76  Double_t yield = dataHists[iHist]->IntegralAndError(iBin, iBin, error);
77  data_yields.push_back(yield);
78  data_error.push_back(error);
79  error = 0.0;
80  yield = signalHists[iHist]->IntegralAndError(iBin, iBin, error);
81  mc_yields.push_back(yield);
82  mc_error.push_back(error);
83  }
84 
85  factor = mc_yields[0]/data_yields[0];
86 
87  Message(eDebug, "Normalizing factor: %f", factor);
88 
89  break;
90 
91  default:
92  std::cerr << "I do not support that option right now." << std::endl;
93  break;
94  }
95 
96  if (fPrintingMethod != kNone) {
97 
98  std::cout << std::endl << "Bin: " << iBin << " Var: " << PlotVar;
99  std::cout << " " << XBins[iBin - 1] << " to " << XBins[iBin] << std::endl << std::endl;
100 
101  std::cout << "\\hline" << std::endl;
102  std::cout << " & No Cut";
103 
104  for (UInt_t iCut = 0; iCut != fCutNames.size(); ++iCut)
105  std::cout << " & " << fCutNames[iCut];
106 
107  std::cout << " \\\\" << std::endl;
108  std::cout << "\\hline" << std::endl;
109 
110  if (fPrintingMethod == kPresentation)
111  std::cout << "\\makecell{Background \\\\ Subtracted \\\\ Data}";
112  else
113  std::cout << "Background Subtracted Data";
114 
115  for (UInt_t iYield = 0; iYield != data_yields.size(); ++iYield) {
116  std::cout << " & " << TString::Format(fFormat,data_yields[iYield]);
117  std::cout << " $\\pm$ " << TString::Format(fFormat,data_error[iYield]);
118  }
119 
120  std::cout << " \\\\" << std::endl;
121 
122  if (fPrintingMethod == kPresentation)
123  std::cout << "\\makecell{Signal-\\\\ matched MC}";
124  else
125  std::cout << "Signal-matched MC";
126 
127  for (UInt_t iYield = 0; iYield != mc_yields.size(); ++iYield) {
128  std::cout << " & " << TString::Format(fFormat,mc_yields[iYield]);
129  std::cout << " $\\pm$ " << TString::Format(fFormat,mc_error[iYield]);
130  }
131  std::cout << " \\\\" << std::endl;
132  std::cout << "\\hline" << std::endl;
133 
134  if (fPrintingMethod == kPresentation)
135  std::cout << "\\makecell{Normalized \\\\ Ratio}";
136  else
137  std::cout << "Normalized Ratio";
138 
139  for (UInt_t iYield = 0; iYield != data_yields.size(); ++iYield) {
140  std::cout << " & " << TString::Format(fFormat, data_yields[iYield]/mc_yields[iYield] * factor);
141  std::cout << " $\\pm$ " << TString::Format(fFormat,
142  TMath::Sqrt(pow(data_error[iYield]/mc_yields[iYield],2) +
143  pow(data_yields[iYield]/pow(mc_yields[iYield],2) * mc_error[iYield],2)) *
144  factor);
145  }
146 
147  std::cout << " \\\\" << std::endl;
148  std::cout << "\\hline" << std::endl;
149 
150  }
151 
152  Double_t FinalSF = data_yields.back()/mc_yields.back() * factor;
153  Double_t FinalError = TMath::Sqrt(pow(data_error.back()/mc_yields.back(), 2) +
154  pow(data_yields.back()/pow(mc_yields.back(), 2) *
155  mc_error.back(), 2)) * factor;
156 
157  Message(eInfo, "Bin %i final scale factor: %f", iBin, FinalSF);
158  Message(eInfo, "Bin %i final scale error: %f", iBin, FinalError);
159 
160  output->SetBinContent(iBin, FinalSF);
161  output->SetBinError(iBin, FinalError);
162  }
163 
164  CloseFiles();
165  return output;
166 
167 }
168 
169 //--------------------------------------------------------------------
170 TH1D*
171 HistAnalysis::DoScaleFactors(TString PlotVar, Int_t NumBins, Double_t MinX, Double_t MaxX,
172  ScaleFactorMethod method)
173 {
174  Double_t XBins[NumBins+1];
175  ConvertToArray(NumBins, MinX, MaxX, XBins);
176  return DoScaleFactors(PlotVar, NumBins, XBins, method);
177 }
178 
179 
180 //--------------------------------------------------------------------
181 
182 /**
183  @param name is the name that the cut will be given in the table that is printed out
184  @param cut is the cut string to apply for the scale factor cut
185  @param datacut is an optional argument in case you want to have a different cut on data
186 */
187 
188 void
189 HistAnalysis::AddScaleFactorCut(TString name, TCut cut, TCut datacut)
190 {
191 
192  fCutNames.push_back(name);
193  fScaleFactorCuts.push_back(cut);
194  if (datacut == "")
195  fDataSFCuts.push_back(cut);
196  else
197  fDataSFCuts.push_back(datacut);
198 
199 }
200 
201 //--------------------------------------------------------------------
202 void
204 {
205 
206  fScaleFactorCuts.resize(0);
207  fDataSFCuts.resize(0);
208  fCutNames.resize(0);
209 
210 }
211 
212 //--------------------------------------------------------------------
213 void
214 HistAnalysis::MakeReweightHist(TString OutFile, TString OutHist, TString PlotVar, Int_t NumBins, Double_t *XBins)
215 {
216 
217  SetDefaultExpr(PlotVar);
219 
220  // First create the data histogram
221  TH1D *dataHist = GetHist(NumBins, XBins, kData);
222 
223  // Then create the mc histogram
224  TH1D *mcHist = GetHist(NumBins, XBins, fSignalType, fSignalName, fSearchBy, true);
225 
226  // Do background subtraction, if necessary
227  if (fSignalName != "") {
228 
229  TH1D *backgroundHist = GetHist(NumBins, XBins, fSignalType, fSignalName, fSearchBy, false);
230  dataHist->Add(backgroundHist, -1.0);
231  delete backgroundHist;
232 
233  }
234 
235  // Normalize for most applications, unless we are getting transfer factors or something
236  if (fNormalized) {
237 
238  dataHist->Scale(1.0/dataHist->Integral());
239  mcHist->Scale(1.0/mcHist->Integral());
240 
241  }
242 
243  dataHist->Divide(mcHist);
244 
245  TH1F *uncHist = new TH1F("unc", "unc", NumBins, XBins);
246  for (Int_t iBin = 1; iBin != NumBins + 1; ++iBin)
247  uncHist->SetBinContent(iBin, dataHist->GetBinError(iBin)/dataHist->GetBinContent(iBin));
248 
249  TFile *theFile = new TFile(OutFile, "RECREATE");
250  theFile->WriteTObject(dataHist, OutHist);
251  theFile->WriteTObject(uncHist, OutHist + "_unc");
252 
253  delete dataHist;
254  delete mcHist;
255  delete uncHist;
256 
257  CloseFiles();
258 
259 }
260 
261 //--------------------------------------------------------------------
262 void
263 HistAnalysis::MakeReweightHist(TString OutFile, TString OutHist, TString PlotVar, Int_t NumBins, Double_t MinX, Double_t MaxX)
264 {
265 
266  Double_t XBins[NumBins+1];
267  ConvertToArray(NumBins, MinX, MaxX, XBins);
268  MakeReweightHist(OutFile, OutHist, PlotVar, NumBins, XBins);
269 
270 }
271 
272 //--------------------------------------------------------------------
273 void
274 HistAnalysis::PlotScaleFactors(TString FileBase, TString PlotVar, Int_t NumBins, Double_t *XBins,
275  TString XLabel, ScaleFactorMethod method)
276 {
277 
278  DisplayFunc(__func__);
279 
280  std::vector<TH1D*> hists;
281  TH1D* theHist = DoScaleFactors(PlotVar, NumBins, XBins, method);
282  hists.push_back(theHist);
283 
284  SetRatioIndex(0);
285  SetMakeRatio(false);
286  BaseCanvas(FileBase, hists, XLabel, "Scale Factor");
287 
288  delete theHist;
289 
290 }
291 
292 //--------------------------------------------------------------------
293 void
294 HistAnalysis::PlotScaleFactors(TString FileBase, TString PlotVar, Int_t NumBins, Double_t MinX, Double_t MaxX,
295  TString XLabel, ScaleFactorMethod method)
296 {
297 
298  Double_t XBins[NumBins+1];
299  ConvertToArray(NumBins, MinX, MaxX, XBins);
300  PlotScaleFactors(FileBase, PlotVar, NumBins, XBins, XLabel, method);
301 
302 }