#include "RooRealVar.h" #include "RooDataSet.h" #include "RooGaussian.h" #include "RooConstVar.h" #include "RooPolynomial.h" #include "RooLinearMorph.h" #include "RooNLLVar.h" #include "TCanvas.h" #include "TAxis.h" #include "RooPlot.h" #include "TH1.h" using namespace RooFit ; void rooFit_Test(){ TF1 *f1 = new TF1("f1","exp([0]*x)",2.4,3.5); TF1 *f2 = new TF1("f2","gaus",2.4,3.5); f1->SetParameter(0,2.0); f2->SetParameter(0,10); f2->SetParameter(1,2.8); f2->SetParameter(2,0.04); f1->Draw(); //return; TH1D *H1 = new TH1D("H1","",100,2.3,3.5); TH1D *H2 = new TH1D("H2","",60,2.3,3.5); for(int i=0;i<20000;i++) H1->Fill(f1->GetRandom()); for(int i=0;i<2000;i++) H1->Fill(f2->GetRandom()); for(int i=0;i<500000;i++) H2->Fill(f1->GetRandom()); H2->Draw(); //------------------------------------------------------------------- // At this point I have a generated background of the shape f1, and // a distribution which is a summ of f1 and f2 with lower statistics // So, let's RooFit-it ! //------------------------------------------------------------------- RooRealVar x("x","x",2.4,3.4); RooRealVar mean("mean","mean",2.7,2.9); RooRealVar sigma("sigma","sigma",0.0005,0.058); RooGaussian gauss("gauss","Theta Peak",x,mean,sigma); RooDataHist bkgShape("simulation","",x,H2); RooHistPdf BackGround("BackGround","Phi Simulation",x,bkgShape,7); RooRealVar nsig("nsig","signal Fracktion",500,0.,100000.); RooRealVar nbkg("nbkg","backgroun Fracktion",500,0.,100000.); RooAddPdf fModel("Model","Model",RooArgList(BackGround,gauss),RooArgList(nbkg,nsig)); RooPlot *xframe = x.frame(); RooDataHist data("data","",x,H1); RooFitResult* res = fModel.fitTo(data,Range(2.4,3.4),Minos(kTRUE),PrintLevel(3),Save()); data.plotOn(xframe); fModel.plotOn(xframe); // BackGround.plotOn(xframe); // This line when uncommented does not plot the right fitted background // gauss.plotOn(xframe); // Same with this ! xframe->Draw(); // ------------------------------------------------------------------------------------------------------------------------- // So far so good, following the examples I was able to fit the generated distribution, but the problem starts here. // ----- // At this point I would like to go over my sample histogram (H1) and evaluate the model, the background and the gaussian // at every H1->BinCenter(1 to NbinsX), for my own calculations of significance // // The values returned below do not add up to the statistical sample that I have. //------- x.setVal(2.8); cout << " Fitted Value = " << fModel.getVal(x) << endl ; cout << " BackGround Value = " << BackGround.getVal(x) << endl ; cout << " Fitted Gauss = " << gauss.getVal(x) << endl ; }