31 #include <RooFitResult.h>
32 #include <RooRealVar.h>
33 #include <RooAbsPdf.h>
34 #include <RooDataSet.h>
37 using namespace RooFit;
52 if (
PDF ==
nullptr || SeparationVariable ==
nullptr)
57 for (
unsigned int i = 0;
i <
Regions.size();
i++) {
59 cout <<
"Integrating over Range: " <<
Regions[
i].RegionName <<
" from " <<
Regions[
i].min <<
" to "
61 intPDF =
PDF->createIntegral(*SeparationVariable,
Range(
Regions[
i].RegionName.c_str()));
62 yield += intPDF->getVal();
64 cout <<
"Current yield: " << yield << endl;
71 if (axis_label ==
"GeV/c^2")
72 axis_label =
"Mass (" + axis_label +
")";
73 if (axis_label ==
"GeV/c")
74 axis_label =
"Momentum (" + axis_label +
")";
75 histo->GetXaxis()->SetTitle(axis_label.c_str());
81 if (Data ==
nullptr || SeparationVariable ==
nullptr) {
82 cerr <<
"ERROR: Data or SeparationVariable is NULL returning now!\n";
85 TH1F* SideBandHist = (TH1F*)BaseHistos[
index]->Clone();
87 (
string)
variable->GetName() +
"Sideband",
88 (
string)SideBandHist->GetTitle() +
" Sideband",
91 TH1F* SignalHist = (TH1F*)BaseHistos[
index]->Clone();
93 (
string)
variable->GetName() +
"SignalHist",
94 (
string)SignalHist->GetTitle() +
" Raw Signal",
101 TIterator* iter = (TIterator*)Data->get()->createIterator();
102 RooAbsArg*
var =
nullptr;
103 RooRealVar* sep_var =
nullptr;
104 while ((
var = (RooAbsArg*)iter->Next())) {
105 if ((
string)
var->GetName() == (
string)SeparationVariable->GetName()) {
106 sep_var = (RooRealVar*)
var;
110 for (
int i = 0;
i < Data->numEntries();
i++) {
113 Double_t cutval = sep_var->getVal();
115 for (
unsigned int j = 0;
j < SideBandRegions.size();
j++)
117 if (cutval > SideBandRegions[
j].
min && cutval < SideBandRegions[
j].
max)
118 SideBandHist->Fill(
value);
120 for (
unsigned int j = 0;
j < SignalRegions.size();
j++) {
121 if (cutval > SignalRegions[
j].
min && cutval < SignalRegions[
j].
max)
122 SignalHist->Fill(
value);
127 SideBandHist->Sumw2();
129 RawHistos.push_back(*SignalHist);
131 SignalHist->Add(SideBandHist, -stsratio);
133 SignalHist->SetTitle(((
string)BaseHistos[
index]->GetTitle() +
" SBS Signal").c_str());
135 SBSHistos.push_back(*SignalHist);
137 SideBandHistos.push_back(*SideBandHist);
144 TCanvas genericCanvas;
147 genericCanvas.SaveAs(
outname.c_str());
149 genericCanvas.SaveAs(
outname.c_str());
154 if (SeparationVariable ==
nullptr) {
155 cerr <<
"ERROR: printResults, SeparationVariable is NULL!\n";
159 for (
unsigned int i = 0;
i < RawHistos.size(); ++
i) {
163 for (
unsigned int i = 0;
i < SBSHistos.size(); ++
i) {
168 for (
unsigned int i = 0;
i < SideBandHistos.size(); ++
i) {
175 if (Data !=
nullptr && ModelPDF !=
nullptr) {
176 RooPlot* SepVarFrame = SeparationVariable->frame();
177 Data->plotOn(SepVarFrame);
178 ModelPDF->plotOn(SepVarFrame);
181 Canvas.SaveAs(
outname.c_str());
183 Canvas.SaveAs(
outname.c_str());
185 cerr <<
"ERROR: printResults, Data or ModelPDF is NULL!\n";
187 string result_outname =
prefix +
"_fit_results.txt";
190 cout <<
"ERROR: Could not open file for writing!\n";
194 if (fit_result !=
nullptr) {
195 #if ROOT_VERSION_CODE <= ROOT_VERSION(5, 19, 0)
198 fit_result->printMultiline(
output, kTRUE);
215 TIter nextkey(
output.GetListOfKeys());
217 TDirectory* curDir =
nullptr;
218 while ((
key = (TKey*)nextkey.Next())) {
221 TObject*
obj =
key->ReadObj();
222 if (
obj->IsA()->InheritsFrom(
"TDirectory"))
228 curDir =
output.mkdir(
"run0",
"Run 0");
233 Int_t run_num =
dirname.Atoi();
239 if (curDir ==
nullptr)
240 curDir =
output.GetDirectory(
"", kTRUE,
"");
248 for (
unsigned int i = 0;
i < SideBandHistos.size(); ++
i) {
249 SideBandHistos[
i].SetDirectory(curDir);
250 SideBandHistos[
i].Write();
252 for (
unsigned int i = 0;
i < RawHistos.size(); ++
i) {
253 RawHistos[
i].SetDirectory(curDir);
254 RawHistos[
i].Write();
256 for (
unsigned int i = 0;
i < SBSHistos.size(); ++
i) {
257 SBSHistos[
i].SetDirectory(curDir);
258 SBSHistos[
i].Write();
260 if (Data !=
nullptr && ModelPDF !=
nullptr && BackgroundPDF !=
nullptr && SeparationVariable !=
nullptr) {
261 RooPlot* sep_varFrame = SeparationVariable->frame();
262 Data->plotOn(sep_varFrame);
263 ModelPDF->plotOn(sep_varFrame);
264 BackgroundPDF->plotOn(sep_varFrame);
265 sep_varFrame->Write();
267 cerr <<
"ERROR: saveResults, did not save RooPlot of data and fit\n";
271 if (newData !=
nullptr)
275 if (Data ==
nullptr || ModelPDF ==
nullptr) {
276 cerr <<
"ERROR: print_plot, Data or ModelPDF are NULL\n";
280 RooPlot* genericFrame = printVar->frame();
281 Data->plotOn(genericFrame);
282 ModelPDF->plotOn(genericFrame);
283 TCanvas genericCanvas;
284 genericFrame->Draw();
286 genericCanvas.SaveAs(
outname.c_str());
288 genericCanvas.SaveAs(
outname.c_str());
291 : BackgroundPDF(nullptr),
294 SeparationVariable(nullptr),
303 SignalSidebandRatio(0) {
307 RooAbsPdf* bkg_shape,
310 const vector<TH1F*>&
base,
312 : BackgroundPDF(bkg_shape),
313 ModelPDF(model_shape),
315 SeparationVariable(sep_var),
325 SignalSidebandRatio(0) {}
412 cout <<
"Beginning SideBand Subtraction\n";
417 cerr <<
"ERROR: doGobalFit, no ModelPDF, SeparationVariable or Data specified\n";
426 cout <<
"Finished fitting background!\n";
434 TIterator* iter = (TIterator*)
Data->get()->createIterator();
436 while ((
variable = (RooAbsArg*)iter->Next())) {
452 Int_t binMin =
Total.FindBin(leftRegion.
max, 0.0, 0.0);
456 binMin =
Total.FindBin(rightRegion.
max, 0.0, 0.0);
460 const unsigned int nbinsx =
Total.GetNbinsX();
461 const double x1 = (leftRegion.
max + leftRegion.
min) / 2.0;
462 const double x2 = (rightRegion.
max + rightRegion.
min) / 2.0;
464 const double y1 = numLeft / (leftRegion.
max - leftRegion.
min);
465 const double y2 = numRight / (rightRegion.
max - rightRegion.
min);
471 TF1 function(
"sbs_function_line",
"[0]*x + [1]",
Total.GetMinimum(),
Total.GetMaximum());
472 for (
unsigned int binx = 1; binx <= nbinsx; ++binx) {
473 double binWidth =
Total.GetBinWidth(binx);
474 function.SetParameter(0,
Slope * binWidth);
475 function.SetParameter(1,
Intercept * binWidth);
477 double xx =
Total.GetBinCenter(binx);
478 double cu =
Total.GetBinContent(binx) -
function.Eval(
xx);
480 double error1 =
Total.GetBinError(binx);
482 Result.SetBinContent(binx, cu);
483 Result.SetBinError(binx, error1);
485 Result.SetEntries(Result.Integral());