Crombie Tools
FitTools.cc
Go to the documentation of this file.
1 #include <vector>
2 
3 #include "TROOT.h"
4 #include "TH1D.h"
5 #include "TList.h"
6 
7 #include "RooAddPdf.h"
8 #include "RooArgSet.h"
9 #include "RooRealVar.h"
10 #include "RooCategory.h"
11 #include "RooDataSet.h"
12 #include "RooKeysPdf.h"
13 
14 #include "FitTools.h"
15 
16 using namespace RooFit;
17 
19 
20 
21 //--------------------------------------------------------------------
23 {
24  SetMultiplyLumi(true);
25 }
26 
27 //--------------------------------------------------------------------
29 { }
30 
31 //--------------------------------------------------------------------
32 void
33 FitTools::FitCategories(Double_t Shape_Min, Double_t Shape_Max, const char* ShapeLabel)
34 {
35 
36  DisplayFunc(__func__);
37  Message(eDebug, "About to fit %i categories %s to shape of %s",
38  fCategoryNames.size(), fCategoryBranch, fDefaultExpr.Data());
39  Message(eDebug, "Data cut is %s", fDataBaseCut.GetTitle());
40  Message(eDebug, " MC cut is %s", fBaseCut.GetTitle());
41  if (fSignalName != "")
42  Message(eDebug, "Only reweighting %s", fSignalName.Data());
43 
44  Double_t XBins[2];
45  ConvertToArray(1, Shape_Min, Shape_Max, XBins);
46 
47  // Create the variable being plotted
48  RooRealVar variable = RooRealVar(fDefaultExpr.Data(), ShapeLabel, Shape_Min, Shape_Max);
49 
50  // Create weight variable
51  RooRealVar MCWeight = RooRealVar(fMCWeight.GetTitle(), "MC Weight", 0.0);
52 
53  // Create category variable
54  RooCategory category = RooCategory(fCategoryBranch, "categories");
55 
56  // Set category names
57  for (UInt_t iCat = 0; iCat != fCategoryNames.size(); ++iCat)
58  category.defineType(fCategoryNames[iCat], iCat);
59 
60  TH1D *FloatSizer = GetHist(1, XBins, fSignalType, fSignalName, fSearchBy, true);
61  Double_t FloatSize = FloatSizer->Integral("width");
62 
63  // Open the files and get the TTrees
64  OpenFiles(ReturnFileNames(fSignalType, fSignalName, fSearchBy, true));
65 
66  // Initialize an empty dataset
67  TString dataname = TString::Format("Dataset_%s", "Floating");
68 
69  Message(eDebug, "Created dataset with name %s", dataname.Data());
70 
71  RooArgSet argset(variable, MCWeight, category);
72  RooDataSet dataset(dataname, dataname, argset);
73 
74  // Add the data from each tree to the dataset
75  gROOT->cd();
76  for (UInt_t iTree = 0; iTree != fInTrees.size(); ++iTree) {
77  Message(eDebug, "Getting data from tree %i / %i", iTree, fInTrees.size());
78 
79  TString datasetname = TString::Format("Dataset_%s_%i", "Floating", iTree);
80  TTree *copiedTree = fInTrees[iTree]->CopyTree(fBaseCut.GetTitle());
81  RooDataSet tempData {datasetname, datasetname, copiedTree, argset, 0, fMCWeight};
82 
83  dataset.append(tempData);
84  Message(eDebug, "Dataset now has %i entries", dataset.numEntries());
85  }
86 
87  // Split and add these pdfs together with the proper relative weights, if needed
88  TString pdfName = TString::Format("pdf_%s", "Floating");
89 
90  Message(eDebug, "Creating pdf: %s", pdfName.Data());
91 
92  // create the pdf
93  TList *dataList = dataset.split(category);
94 
95  Message(eDebug, "Dataset list size: %i", dataList->GetSize());
96 
97  RooArgSet pdfSet;
98  RooArgSet coeffSet;
99 
100  for (UInt_t iBin = 0; iBin != fCategoryNames.size(); ++iBin) {
101 
102  Message(eDebug, "Getting pdf for category: %s", fCategoryNames[iBin].Data());
103 
104  TString pdfNameTemp = TString::Format("pdf_%s_%i", "Floating", iBin);
105  Message(eDebug, "The pdf name will be %s", pdfNameTemp.Data());
106  RooKeysPdf tempPdf {pdfNameTemp, pdfNameTemp, variable, *(static_cast<RooDataSet*>(dataList->At(iBin)))};
107  Message(eDebug, "Keys pdf created at %p", &tempPdf);
108  pdfSet.add(tempPdf);
109 
110  if (iBin != fCategoryNames.size() - 1) {
111  TString coeffName = TString::Format("coeff_%s_%i", "Floating", iBin);
112  Message(eDebug, "Added coefficient named %s", coeffName.Data());
113  RooRealVar tempVar {coeffName, coeffName, 0.0, 1.0};
114  coeffSet.add(tempVar);
115  }
116  }
117 
118  RooAddPdf floatPdf(pdfName, pdfName, pdfSet, coeffSet);
119 
120  TH1D *StaticSizer = GetHist(1, XBins, kBackground, fSignalName, fSearchBy, false);
121  Double_t StaticSize = StaticSizer->Integral("width") * (1.0 + fBackgroundChange);
122 
123  // Get the other background files that will be static, named "pdf_Static"
124  // Open the files and get the TTrees
125  OpenFiles(ReturnFileNames(kBackground, fSignalName, fSearchBy, false));
126 
127  // Initialize an empty dataset
128  dataname = TString::Format("Dataset_%s", "Static");
129 
130  Message(eDebug, "Created dataset with name %s", dataname.Data());
131 
132  RooDataSet static_dataset(dataname, dataname, argset);
133 
134  // Add the data from each tree to the dataset
135  gROOT->cd();
136  for (UInt_t iTree = 0; iTree != fInTrees.size(); ++iTree) {
137  Message(eDebug, "Getting data from tree %i / %i", iTree, fInTrees.size());
138 
139  TString datasetname = TString::Format("Dataset_%s_%i", "Static", iTree);
140  TTree *copiedTree = fInTrees[iTree]->CopyTree(fBaseCut.GetTitle());
141  RooDataSet tempData {datasetname, datasetname, copiedTree, argset, 0, fMCWeight};
142 
143  static_dataset.append(tempData);
144  Message(eDebug, "Dataset now has %i entries", static_dataset.numEntries());
145  }
146 
147  // Split and add these pdfs together with the proper relative weights, if needed
148  pdfName = TString::Format("pdf_%s", "Static");
149 
150  Message(eDebug, "Creating pdf: %s", pdfName.Data());
151 
152  RooKeysPdf staticPdf(pdfName, pdfName, variable, static_dataset);
153 
154  Double_t relativeSize = FloatSize/(FloatSize + StaticSize);
155 
156  Message(eDebug, "Finished importing pdfs. Floating: %f, Static: %f, Relative fraction: %f", FloatSize, StaticSize, relativeSize);
157 
158  RooRealVar relativePdfs = RooRealVar("Relative", "Relative", relativeSize);
159  RooAddPdf finalPdf = RooAddPdf("Final Pdf", "Final Pdf", floatPdf, staticPdf, relativePdfs);
160 
161  Message(eDebug, "Final pdf created at %p", &finalPdf);
162 
163  // Get the dataset to fit
164  TChain *dataChain = ReturnTChain(fTreeName, kData);
165  TTree *dataTree = dataChain->CopyTree(fDataBaseCut.GetTitle());
166 
167  Message(eDebug, "TTree at %p created from TChain at %p", dataTree, dataChain);
168 
169  RooDataSet dataDataset = RooDataSet("data", "data", dataTree, RooArgSet(variable));
170 
171  Message(eDebug, "Data imported with size %i", dataDataset.numEntries());
172 
173  for (UInt_t iBin = 0; iBin != fCategoryNames.size() - 1; ++iBin) {
174  TString coeffName = TString::Format("coeff_%s_%i", "Floating", iBin);
175  Message(eInfo, "Coefficient %s has value %f", coeffName.Data(), static_cast<RooRealVar*>(coeffSet.find(coeffName))->getValV());
176  }
177 
178  finalPdf.fitTo(dataDataset);
179 
180  for (UInt_t iBin = 0; iBin != fCategoryNames.size() - 1; ++iBin) {
181  TString coeffName = TString::Format("coeff_%s_%i", "Floating", iBin);
182  Message(eInfo, "Coefficient %s has value %f", coeffName.Data(), static_cast<RooRealVar*>(coeffSet.find(coeffName))->getValV());
183  }
184 
185  CloseFiles();
186 
187 }