/// \file /// \ingroup tutorial_roofit /// \notebook -nodraw /// 'ADDITION AND CONVOLUTION' RooFit tutorial macro #204 /// /// Extended maximum likelihood fit with alternate range definition /// for observed number of events. /// /// \macro_output /// \macro_code /// \author 07/2008 - Wouter Verkerke #include "RooRealVar.h" #include "RooDataSet.h" #include "RooGaussian.h" #include "RooChebychev.h" #include "RooAddPdf.h" #include "RooExtendPdf.h" #include "RooFitResult.h" #include "TCanvas.h" #include "TAxis.h" #include "RooPlot.h" using namespace RooFit ; RooAddPdf* getSignal(RooRealVar& x) { // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters RooRealVar* mean = new RooRealVar("mean","mean of gaussians",5) ; RooRealVar* sigma1 = new RooRealVar("sigma1","width of gaussians",0.5) ; RooRealVar* sigma2 = new RooRealVar("sigma2","width of gaussians",1) ; RooRealVar* sigma3 = new RooRealVar("sigma3","width of gaussians",1) ; RooGaussian* sig1 = new RooGaussian("sig1","Signal component 1",x,*mean,*sigma1) ; RooGaussian* sig2 = new RooGaussian("sig2","Signal component 2",x,*mean,*sigma2) ; RooGaussian* sig3 = new RooGaussian("sig3","Signal component 3",x,*mean,*sigma3) ; // Sum the signal components into a composite signal p.d.f. RooRealVar* sig1frac = new RooRealVar("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ; RooRealVar* sig2frac = new RooRealVar("sig2frac","fraction of component 2 in signal",0.8,0.,1.) ; RooAddPdf* sig = new RooAddPdf("sig","Signal",RooArgList(*sig1,*sig2),RooArgList(*sig1frac, *sig2frac), /*recursive*/true) ; return sig; } void fit_example() { // S e t u p c o m p o n e n t p d f s // --------------------------------------- // Declare observable x RooRealVar x("x","x",0,10) ; // Build Chebychev polynomial p.d.f. RooRealVar a0("a0","a0",0.5,0.,1.) ; RooRealVar a1("a1","a1",0.2,0.,1.) ; RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ; RooAddPdf* sig = getSignal(x); // C o n s t r u c t e x t e n d e d c o m p s wi t h r a n g e s p e c // ------------------------------------------------------------------------------ // Define signal range in which events counts are to be defined x.setRange("signalRange",4,6) ; // Associated nsig/nbkg as expected number of events with sig/bkg _in_the_range_ "signalRange" RooRealVar nsig("nsig","number of signal events in signalRange",500,0.,10000) ; RooRealVar nbkg("nbkg","number of background events in signalRange",500,0,10000) ; RooAddPdf model("model","(g1+g2)+a",RooArgList(bkg, *sig), RooArgList(nbkg, nsig)) ; // S a m p l e d a t a , f i t m o d e l // ------------------------------------------- // Generate 1000 events from model so that nsig,nbkg come out to numbers <<500 in fit RooDataSet *data = model.generate(x,1000) ; // Perform unbinned extended ML fit to data RooFitResult* r = model.fitTo(*data,Save()) ; r->Print() ; // what's the numer of events from sig3 inside signalRange? // this doesn't give me the correct result, as sig3 is considered independent from model double sig3inRange = 1000*((RooAbsPdf*)model.getComponents()->find("sig3")))->createIntegral(*x, NormSet(*x), Range("signalRange"))->getVal(); // Of course, the correct answer would be sig3inRange*(1-sig2frac)*(1-sig1frac) but they are not in scope // Also, this become unwieldy as you add components }