29 #include <RooFitResult.h>
30 #include <RooRealVar.h>
31 #include <RooAbsPdf.h>
32 #include <RooDataSet.h>
35 using namespace RooFit;
52 if(PDF==
NULL || SeparationVariable==
NULL)
57 for(
unsigned int i=0;
i < Regions.size();
i++)
60 cout<<
"Integrating over Range: "<<Regions[
i].RegionName<<
" from "<<Regions[
i].min<<
" to "<<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;
71 histo->SetName(name.c_str());
72 histo->SetTitle(title.c_str());
73 if(axis_label ==
"GeV/c^2")
74 axis_label =
"Mass (" + axis_label +
")";
75 if(axis_label ==
"GeV/c")
76 axis_label =
"Momentum (" + axis_label +
")";
77 histo->GetXaxis()->SetTitle(axis_label.c_str());
83 cerr <<
"ERROR: Data or SeparationVariable is NULL returning now!\n";
86 TH1F* SideBandHist = (TH1F*)BaseHistos[index]->Clone();
87 setHistOptions(SideBandHist,(
string)variable->GetName()+
"Sideband",(string)SideBandHist->GetTitle() +
" Sideband",(string)variable->getUnit());
89 TH1F* SignalHist = (TH1F*)BaseHistos[index]->Clone();
90 setHistOptions(SignalHist,(
string)variable->GetName()+
"SignalHist",(string)SignalHist->GetTitle() +
" Raw Signal",(string)variable->getUnit());
96 TIterator* iter = (TIterator*)
Data->get()->createIterator();
98 RooRealVar *sep_var=
NULL;
99 while((var = (RooAbsArg*)iter->Next()))
101 if((
string)var->GetName()==(string)SeparationVariable->GetName())
103 sep_var = (RooRealVar*)var;
107 for(
int i=0;
i <
Data->numEntries();
i++)
110 Double_t
value = variable->getVal();
111 Double_t cutval = sep_var->getVal();
113 for(
unsigned int j=0;
j < SideBandRegions.size();
j++)
115 if(cutval > SideBandRegions[
j].
min && cutval < SideBandRegions[
j].
max)
116 SideBandHist->Fill(value);
118 for(
unsigned int j=0;
j < SignalRegions.size();
j++)
120 if(cutval > SignalRegions[
j].
min && cutval < SignalRegions[
j].
max)
121 SignalHist->Fill(value);
125 SignalHist->Sumw2(); SideBandHist->Sumw2();
127 RawHistos.push_back(*SignalHist);
129 SignalHist->Add(SideBandHist, -stsratio);
131 SignalHist->SetTitle(((
string)BaseHistos[index]->GetTitle() +
" SBS Signal").c_str());
133 SBSHistos.push_back(*SignalHist);
135 SideBandHistos.push_back(*SideBandHist);
137 if(SideBandHist)
delete SideBandHist;
142 TCanvas genericCanvas;
144 outname = outname +
".eps";
145 genericCanvas.SaveAs(outname.c_str());
146 outname.replace(outname.size()-3,3,
"gif");
147 genericCanvas.SaveAs(outname.c_str());
153 if(SeparationVariable==
NULL)
155 cerr <<
"ERROR: printResults, SeparationVariable is NULL!\n";
159 for(
unsigned int i=0;
i < RawHistos.size(); ++
i)
161 filename=prefix +
"Raw_" + (string)RawHistos[
i].GetName();
164 for(
unsigned int i=0;
i < SBSHistos.size(); ++
i)
166 filename=prefix +
"SBS_" + (string)RawHistos[
i].GetName();
171 for(
unsigned int i=0;
i < SideBandHistos.size(); ++
i)
173 filename=prefix +
"SideBand_" + (string)RawHistos[
i].GetName();
178 string outname = prefix + (string)SeparationVariable->GetName() +
"_fitted.eps";
181 RooPlot *SepVarFrame = SeparationVariable->frame();
182 Data->plotOn(SepVarFrame);
183 ModelPDF->plotOn(SepVarFrame);
186 Canvas.SaveAs(outname.c_str());
187 outname.replace(outname.size()-3,3,
"gif");
188 Canvas.SaveAs(outname.c_str());
191 cerr <<
"ERROR: printResults, Data or ModelPDF is NULL!\n";
193 string result_outname = prefix +
"_fit_results.txt";
197 cout <<
"ERROR: Could not open file for writing!\n";
203 #if ROOT_VERSION_CODE <= ROOT_VERSION(5,19,0)
206 fit_result->printMultiline(
output,kTRUE);
217 TFile
output(outname.c_str(),
"UPDATE");
225 TIter nextkey(
output.GetListOfKeys());
227 TDirectory* curDir=
NULL;
228 while((key=(TKey*)nextkey.Next()))
233 TObject *
obj = key->ReadObj();
234 if(obj->IsA()->InheritsFrom(
"TDirectory"))
235 dirname=obj->GetName();
242 curDir =
output.mkdir(
"run0",
"Run 0");
249 Int_t run_num = dirname.Atoi();
252 curDir =
output.mkdir(dirname.Data(),(
"Run "+
stringify(run_num)).c_str());
253 output.cd(dirname.Data());
256 curDir =
output.GetDirectory(
"",kTRUE,
"");
264 for(
unsigned int i=0;
i < SideBandHistos.size(); ++
i)
266 SideBandHistos[
i].SetDirectory(curDir);
267 SideBandHistos[
i].Write();
269 for(
unsigned int i=0;
i < RawHistos.size(); ++
i)
271 RawHistos[
i].SetDirectory(curDir);
272 RawHistos[
i].Write();
274 for(
unsigned int i=0;
i < SBSHistos.size(); ++
i)
276 SBSHistos[
i].SetDirectory(curDir);
277 SBSHistos[
i].Write();
281 RooPlot *sep_varFrame = SeparationVariable->frame();
282 Data->plotOn(sep_varFrame);
283 ModelPDF->plotOn(sep_varFrame);
284 BackgroundPDF->plotOn(sep_varFrame);
285 sep_varFrame->Write();
288 cerr <<
"ERROR: saveResults, did not save RooPlot of data and fit\n";
301 cerr <<
"ERROR: print_plot, Data or ModelPDF are NULL\n";
305 RooPlot *genericFrame = printVar->frame();
306 Data->plotOn(genericFrame);
307 ModelPDF->plotOn(genericFrame);
308 TCanvas genericCanvas;
309 genericFrame->Draw();
310 outname = outname +
".eps";
311 genericCanvas.SaveAs(outname.c_str());
312 outname.replace(outname.size()-3,3,
"gif");
313 genericCanvas.SaveAs(outname.c_str());
319 SeparationVariable(0),
328 SignalSidebandRatio(0)
333 RooAbsPdf *bkg_shape,
339 : BackgroundPDF(bkg_shape),
340 ModelPDF(model_shape),
342 SeparationVariable(sep_var),
352 SignalSidebandRatio(0)
444 cout <<
"Beginning SideBand Subtraction\n";
452 cerr <<
"ERROR: doGobalFit, no ModelPDF, SeparationVariable or Data specified\n";
462 cout <<
"Finished fitting background!\n";
471 TIterator* iter = (TIterator*)
Data->get()->createIterator();
473 while((variable = (RooAbsArg*)iter->Next()))
478 && (string)variable->GetName()==(string)
BaseHistos[
i]->GetName())
484 if(variable)
delete variable;
485 if(iter)
delete iter;
490 Int_t binMin = Total.FindBin(leftRegion.
max,0.0,0.0);
491 Int_t binMax = Total.FindBin(leftRegion.
min,0.0,0.0);
492 double numLeft = Total.Integral( binMin, binMax );
494 binMin = Total.FindBin(rightRegion.
max,0.0,0.0);
495 binMax = Total.FindBin(rightRegion.
min,0.0,0.0);
496 double numRight = Total.Integral( binMin, binMax );
498 const unsigned int nbinsx = Total.GetNbinsX();
499 const double x1 = (leftRegion.
max + leftRegion.
min)/2.0;
500 const double x2 = (rightRegion.
max + rightRegion.
min)/2.0;
502 const double y1 = numLeft/(leftRegion.
max - leftRegion.
min);
503 const double y2 = numRight/(rightRegion.
max - rightRegion.
min);
505 const double Slope = (y2 - y1)/(x2 - x1);
506 const double Intercept = y1 - Slope*x1;
509 TF1
function(
"sbs_function_line",
"[0]*x + [1]",Total.GetMinimum(), Total.GetMaximum());
510 for (
unsigned int binx = 1; binx <= nbinsx; ++binx )
512 double binWidth = Total.GetBinWidth(binx);
513 function.SetParameter(0,Slope*binWidth);
514 function.SetParameter(1,Intercept*binWidth);
516 double xx = Total.GetBinCenter(binx);
517 double cu = Total.GetBinContent(binx) -
function.Eval(xx);
519 double error1 = Total.GetBinError(binx);
521 Result.SetBinContent(binx, cu);
522 Result.SetBinError(binx, error1);
524 Result.SetEntries(Result.Integral() );
RooAbsPdf * BackgroundPDF
RooFitResult * getFitResult()
std::vector< TH1F > SideBandHistos
std::vector< TH1F > getSBSHistos()
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)
Double_t getYield(std::vector< SbsRegion > Regions, RooAbsPdf *PDF)
static void setHistOptions(TH1F *histo, string name, string title, string axis_label)
std::vector< TH1F > SBSHistos
const T & max(const T &a, const T &b)
RooRealVar * SeparationVariable
std::vector< SbsRegion > SideBandRegions
void printResults(std::string prefix="")
void addSignalRegion(Double_t min, Double_t max)
PixelRecoRange< float > Range
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