/// \file /// \ingroup tutorial_roofit /// \notebook -js /// /// Likelihood and minimization: working with the profile likelihood estimator /// /// \macro_image /// \macro_output /// \macro_code /// /// \date 07/2008 /// \author Wouter Verkerke #include "RooRealVar.h" #include "RooDataSet.h" #include "RooGaussian.h" #include "RooConstVar.h" #include "RooAddPdf.h" #include "RooMinimizer.h" #include "TCanvas.h" #include "TAxis.h" #include "RooPlot.h" using namespace RooFit; void test_asTF_v2() { int E_min = 20; int E_max = 145; RooRealVar mes("mes","Energy (keV)",E_min,E_max); TFile *file_sumBkg = new TFile("allBKG_twoGaus_lowXe133_set45.root","read"); TH1D *xe127 = (TH1D *)file_sumBkg->Get("h_xe127"); TH1D *xe133 = (TH1D *)file_sumBkg->Get("h_xe133"); TH1D *h_all = new TH1D("h_all","h_all",3000,0,3000); h_all->Add(xe127,xe133); RooDataHist h_xe127("h_xe127","h_xe127",mes,xe127); RooDataHist h_xe133("h_xe133","h_xe133",mes,xe133); RooHistPdf pdf_Xe127("pdf_Xe127","pdf_Xe127",mes,h_xe127,0); RooHistPdf pdf_Xe133("pdf_Xe133","pdf_Xe133",mes,h_xe133,0); int input_xe127 = 36; int input_xe133 = 2000; RooRealVar nbkg_xe127("nbkg_xe127","#xe127 events",input_xe127, 0.1*input_xe127, input_xe127*10); RooRealVar nbkg_xe133("nbkg_xe133","#xe133 events",input_xe133, 0.1*input_xe133, input_xe133*10); RooAddPdf model("model","model",RooArgList(pdf_Xe127, pdf_Xe133), RooArgList(nbkg_xe127, nbkg_xe133)); RooDataSet *data = model.generate(mes,2000); RooFitResult* fitresult = model.fitTo(*data, Save()); // plot RooPlot* xframe = mes.frame(Title("try")); data->plotOn(xframe, Name("fake Data"), MarkerColor(kBlack), LineColor(kBlack)); model.plotOn(xframe,Components("pdf_Xe127"), Name("Xe127 bkg"), LineColor(kViolet-3)); model.plotOn(xframe,Components("pdf_Xe133"), Name("Xe133 bkg"), LineColor(kMagenta-3)); model.plotOn(xframe, Name("Fit"), LineColor(4)); //--------------// // draw // //--------------// gStyle->SetOptStat(0); TCanvas* c = new TCanvas("c","c",1); gPad->SetLogy(); xframe->Draw(); // from root forum TF1* normFunc = model.asTF(RooArgList(mes), RooArgList(nbkg_xe127, nbkg_xe133), RooArgList(mes)); TF1 * func = new TF1("plotFunc",[&](double *x, double *p) { return p[0]* normFunc->EvalPar(x);},E_min,E_max,1); func->SetParameter(0, data->sumEntries() * mes.getBinWidth(1)); cout << " 20~145: " << func->Integral(20,145) << endl; func->SetLineStyle(7); func->SetLineColor(2); func->DrawClone("same"); // signal double xe127_value = ((RooRealVar*)fitresult->floatParsFinal().find("nbkg_xe127"))->getVal(); cout << "xe127_value: " << xe127_value << endl; TCanvas* c1 = new TCanvas("c1","c1",1); cout << "--------- xe127 ----------" << endl; TF1* normFunc_127 = pdf_Xe127.asTF(RooArgList(mes), RooArgList(nbkg_xe127), RooArgList(mes)); TF1 * func_127 = new TF1("plotFunc",[&](double *x, double *p) { return p[0]* normFunc_127->EvalPar(x);},E_min,E_max,1); func_127->SetParameter(0, xe127_value * mes.getBinWidth(1)); cout << " 20~60: " << func_127->Integral(20,60) << endl; cout << " 35: " << func_127->Eval(35) << endl; cout << " max " << func_127->GetMaximum() << endl; func_127->DrawClone(""); }