31 #include <RooFitResult.h> 32 #include <RooRealVar.h> 33 #include <RooAbsPdf.h> 34 #include <RooDataSet.h> 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 RooRealVar* sep_var =
nullptr;
102 for (
const auto&
var : *Data->get()) {
103 if ((
string)
var->GetName() == (
string)SeparationVariable->GetName()) {
104 sep_var = (RooRealVar*)
var;
109 for (
int i = 0;
i < Data->numEntries();
i++) {
112 Double_t cutval = sep_var->getVal();
114 for (
unsigned int j = 0;
j < SideBandRegions.size();
j++)
116 if (cutval > SideBandRegions[
j].
min && cutval < SideBandRegions[
j].
max)
117 SideBandHist->Fill(
value);
119 for (
unsigned int j = 0;
j < SignalRegions.size();
j++) {
120 if (cutval > SignalRegions[
j].
min && cutval < SignalRegions[
j].
max)
121 SignalHist->Fill(
value);
126 SideBandHist->Sumw2();
128 RawHistos.push_back(*SignalHist);
130 SignalHist->Add(SideBandHist, -stsratio);
132 SignalHist->SetTitle(((
string)BaseHistos[
index]->GetTitle() +
" SBS Signal").c_str());
134 SBSHistos.push_back(*SignalHist);
136 SideBandHistos.push_back(*SideBandHist);
143 TCanvas genericCanvas;
146 genericCanvas.SaveAs(
outname.c_str());
148 genericCanvas.SaveAs(
outname.c_str());
153 if (SeparationVariable ==
nullptr) {
154 cerr <<
"ERROR: printResults, SeparationVariable is NULL!\n";
158 for (
unsigned int i = 0;
i < RawHistos.size(); ++
i) {
162 for (
unsigned int i = 0;
i < SBSHistos.size(); ++
i) {
167 for (
unsigned int i = 0;
i < SideBandHistos.size(); ++
i) {
174 if (Data !=
nullptr && ModelPDF !=
nullptr) {
175 RooPlot* SepVarFrame = SeparationVariable->frame();
176 Data->plotOn(SepVarFrame);
177 ModelPDF->plotOn(SepVarFrame);
180 Canvas.SaveAs(
outname.c_str());
182 Canvas.SaveAs(
outname.c_str());
184 cerr <<
"ERROR: printResults, Data or ModelPDF is NULL!\n";
186 string result_outname =
prefix +
"_fit_results.txt";
189 cout <<
"ERROR: Could not open file for writing!\n";
193 if (fit_result !=
nullptr) {
194 #if ROOT_VERSION_CODE <= ROOT_VERSION(5, 19, 0) 197 fit_result->printMultiline(
output, kTRUE);
214 TIter nextkey(
output.GetListOfKeys());
216 TDirectory* curDir =
nullptr;
217 while ((
key = (TKey*)nextkey.Next())) {
220 TObject*
obj =
key->ReadObj();
221 if (
obj->IsA()->InheritsFrom(
"TDirectory"))
227 curDir =
output.mkdir(
"run0",
"Run 0");
232 Int_t run_num =
dirname.Atoi();
238 if (curDir ==
nullptr)
239 curDir =
output.GetDirectory(
"", kTRUE,
"");
247 for (
unsigned int i = 0;
i < SideBandHistos.size(); ++
i) {
248 SideBandHistos[
i].SetDirectory(curDir);
249 SideBandHistos[
i].Write();
251 for (
unsigned int i = 0;
i < RawHistos.size(); ++
i) {
252 RawHistos[
i].SetDirectory(curDir);
253 RawHistos[
i].Write();
255 for (
unsigned int i = 0;
i < SBSHistos.size(); ++
i) {
256 SBSHistos[
i].SetDirectory(curDir);
257 SBSHistos[
i].Write();
259 if (Data !=
nullptr && ModelPDF !=
nullptr && BackgroundPDF !=
nullptr && SeparationVariable !=
nullptr) {
260 RooPlot* sep_varFrame = SeparationVariable->frame();
261 Data->plotOn(sep_varFrame);
262 ModelPDF->plotOn(sep_varFrame);
263 BackgroundPDF->plotOn(sep_varFrame);
264 sep_varFrame->Write();
266 cerr <<
"ERROR: saveResults, did not save RooPlot of data and fit\n";
270 if (newData !=
nullptr)
274 if (Data ==
nullptr || ModelPDF ==
nullptr) {
275 cerr <<
"ERROR: print_plot, Data or ModelPDF are NULL\n";
279 RooPlot* genericFrame = printVar->frame();
280 Data->plotOn(genericFrame);
281 ModelPDF->plotOn(genericFrame);
282 TCanvas genericCanvas;
283 genericFrame->Draw();
285 genericCanvas.SaveAs(
outname.c_str());
287 genericCanvas.SaveAs(
outname.c_str());
290 : BackgroundPDF(nullptr),
293 SeparationVariable(nullptr),
302 SignalSidebandRatio(0) {
306 RooAbsPdf* bkg_shape,
309 const vector<TH1F*>&
base,
311 : BackgroundPDF(bkg_shape),
312 ModelPDF(model_shape),
314 SeparationVariable(sep_var),
324 SignalSidebandRatio(0) {}
411 cout <<
"Beginning SideBand Subtraction\n";
416 cerr <<
"ERROR: doGobalFit, no ModelPDF, SeparationVariable or Data specified\n";
425 cout <<
"Finished fitting background!\n";
453 const double x1 = (leftRegion.
max + leftRegion.
min) / 2.0;
454 const double x2 = (rightRegion.
max + rightRegion.
min) / 2.0;
456 const double y1 = numLeft / (leftRegion.
max - leftRegion.
min);
457 const double y2 = numRight / (rightRegion.
max - rightRegion.
min);
463 TF1
function(
"sbs_function_line",
"[0]*x + [1]",
Total.GetMinimum(),
Total.GetMaximum());
464 for (
unsigned int binx = 1; binx <=
nbinsx; ++binx) {
465 double binWidth =
Total.GetBinWidth(binx);
466 function.SetParameter(0,
Slope * binWidth);
467 function.SetParameter(1,
Intercept * binWidth);
469 double xx =
Total.GetBinCenter(binx);
470 double cu =
Total.GetBinContent(binx) -
function.Eval(
xx);
472 double error1 =
Total.GetBinError(binx);
474 Result.SetBinContent(binx, cu);
475 Result.SetBinError(binx, error1);
477 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
RooRealVar * SeparationVariable
std::vector< SbsRegion > SideBandRegions
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