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 "
60 << Regions[
i].max << endl;
61 intPDF = PDF->createIntegral(*SeparationVariable,
Range(Regions[
i].RegionName.c_str()));
62 yield += intPDF->getVal();
64 cout <<
"Current yield: " << yield << endl;
69 histo->SetName(name.c_str());
70 histo->SetTitle(title.c_str());
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",
89 (
string)variable->getUnit());
91 TH1F* SignalHist = (TH1F*)BaseHistos[index]->Clone();
93 (
string)variable->GetName() +
"SignalHist",
94 (
string)SignalHist->GetTitle() +
" Raw Signal",
95 (
string)variable->getUnit());
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++) {
112 Double_t
value = variable->getVal();
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;
146 outname = outname +
".eps";
147 genericCanvas.SaveAs(outname.c_str());
148 outname.replace(outname.size() - 3, 3,
"gif");
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) {
160 filename = prefix +
"Raw_" + (
string)RawHistos[
i].GetName();
163 for (
unsigned int i = 0;
i < SBSHistos.size(); ++
i) {
164 filename = prefix +
"SBS_" + (
string)RawHistos[
i].GetName();
168 for (
unsigned int i = 0;
i < SideBandHistos.size(); ++
i) {
169 filename = prefix +
"SideBand_" + (
string)RawHistos[
i].GetName();
174 string outname = prefix + (
string)SeparationVariable->GetName() +
"_fitted.eps";
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());
182 outname.replace(outname.size() - 3, 3,
"gif");
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);
207 TFile
output(outname.c_str(),
"UPDATE");
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"))
223 dirname = obj->GetName();
228 curDir =
output.mkdir(
"run0",
"Run 0");
232 dirname.Remove(0, 3);
233 Int_t run_num = dirname.Atoi();
236 curDir =
output.mkdir(dirname.Data(), (
"Run " +
stringify(run_num)).c_str());
237 output.cd(dirname.Data());
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();
285 outname = outname +
".eps";
286 genericCanvas.SaveAs(outname.c_str());
287 outname.replace(outname.size() - 3, 3,
"gif");
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);
453 Int_t binMax = Total.FindBin(leftRegion.
min, 0.0, 0.0);
454 double numLeft = Total.Integral(binMin, binMax);
456 binMin = Total.FindBin(rightRegion.
max, 0.0, 0.0);
457 binMax = Total.FindBin(rightRegion.
min, 0.0, 0.0);
458 double numRight = Total.Integral(binMin, binMax);
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);
467 const double Slope = (y2 - y1) / (x2 - x1);
468 const double Intercept = y1 - Slope * x1;
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());
RooAbsPdf * BackgroundPDF
Double_t getYield(const std::vector< SbsRegion > &Regions, RooAbsPdf *PDF)
RooFitResult * getFitResult()
std::vector< TH1F > SideBandHistos
std::vector< TH1F > getSBSHistos()
PixelRecoRange< float > Range
void saveResults(std::string outname)
RooFitResult * fit_result
Double_t SignalSidebandRatio
std::vector< TH1F > getRawHistos()
static void print_histo(TH1F *plot, string outname)
std::vector< TH1F * > getBaseHistos()
int doSubtraction(RooRealVar *variable, Double_t stsratio, Int_t index)
std::string stringify(const T &t)
static void setHistOptions(TH1F *histo, string name, string title, string axis_label)
std::vector< TH1F > SBSHistos
static constexpr int verbose
list var
if using global norm cols_to_minmax = ['t_delta', 't_hmaxNearP','t_emaxNearP', 't_hAnnular', 't_eAnnular','t_pt','t_nVtx','t_ieta','t_eHcal10', 't_eHcal30','t_rhoh','t_eHcal'] df[cols_to_minmax] = df[cols_to_minmax].apply(lambda x: (x - x.min()) / (x.max() - x.min()) if (x.max() - x.min() > 0) else 1.0/200.0)
RooRealVar * SeparationVariable
std::vector< SbsRegion > SideBandRegions
tuple key
prepare the HTCondor submission files and eventually submit them
void printResults(std::string prefix="")
void addSignalRegion(Double_t min, Double_t max)
std::vector< SbsRegion > SignalRegions
char data[epos_bytes_allocation]
void addSideBandRegion(Double_t min, Double_t max)
void print_plot(RooRealVar *printVar, std::string outname)
void doFastSubtraction(TH1F &Total, TH1F &Result, SbsRegion &leftRegion, SbsRegion &rightRegion)
void setDataSet(RooDataSet *newData)
std::vector< TH1F * > BaseHistos
std::vector< TH1F > RawHistos