#include #include #include "RooRealVar.h" #include "RooDataSet.h" #include "RooGaussian.h" #include "TCanvas.h" #include "RooPlot.h" #include "TAxis.h" using namespace RooFit ; #include void exe2() { // create model TFile *f=new TFile("exe2.root","recreate"); RooWorkspace *w=new RooWorkspace("w",""); w->factory("x[110,160]"); // invariant mass // create exponential model as two components w->factory("a1[ 7.5, -500, 500]"); w->factory("a2[-1.5, -500, 500]"); w->factory("expr::z('-(a1*x/100 + a2*(x/100)^2)', a1, a2, x)"); w->factory("Exponential::bkg_pdf(z, 1)"); // signal model w->factory("mass[130, 110, 150]"); w->factory("width[1, 0.5, 5]"); w->factory("Gaussian::sig_pdf(x, mass, width)"); //w->factory("Gaussian:pdf(x[-10,10],mu[1,-1000,1000],s[2,0,1000])"); // create RooAddPdf in extended mode w->factory("nbackground[1000,0,100000]"); w->factory("nsignal[100, 0.0, 1000.0]"); w->factory("SUM::model(nbackground*bkg_pdf, nsignal*sig_pdf)"); // w.factory("SUM:model(nsig[100,0,10000]*sig_pdf, nbkg[1000,0,10000]*bkg_pdf)"); // for extended model //read RooRealVar *x=w->var("x"); RooAbsPdf *model=w->pdf("model"); RooPlot *plot=x->frame(); TTree *T=new TTree("tree","data from hig.txt"); T->ReadFile("hig.txt","x");//ATTENTION!!x should be consistent with RooRealVar T->Draw("x"); double num=T->GetEntries(); RooDataSet data("data","",*x,Import(*T)); // fit the data and save its result. Use eventually the optional =Minuit2= minimiser RooFitResult * res =model->fitTo(data,/* Minimizer("Minuit2","Migrad"),*/ Save(true) ); data.plotOn(plot); //draw the two separate pdf's RooAbsPdf *sig_pdf=w->pdf("sig_pdf"); RooAbsPdf *bkg_pdf=w->pdf("bkg_pdf"); RooAbsPdf *abs=w->pdf("model"); bkg_pdf->plotOn(plot);//, RooFit::Components("bkg_pdf"), RooFit::LineStyle(kDashed) ); //abs->plotOn(plot); //sig_pdf->plotOn(plot);//,RooFit::Components("sig_pdf"); RooFit::LineColor(kRed); RooFit::LineStyle(kDashed); model->plotOn(plot); plot->Draw(); // to show the RooPlot in the current ROOT Canvas RooRealVar *nbkg=w->var("nbackground"); RooRealVar *nsig=w->var("nsignal"); cout<<"nbkg="<getVal()<