rf202_extendedmlfit.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #202
00004 // 
00005 // Setting up an extended maximum likelihood fit
00006 //
00007 //
00008 //
00009 // 07/2008 - Wouter Verkerke 
00010 // 
00011 /////////////////////////////////////////////////////////////////////////
00012 
00013 #ifndef __CINT__
00014 #include "RooGlobalFunc.h"
00015 #endif
00016 #include "RooRealVar.h"
00017 #include "RooDataSet.h"
00018 #include "RooGaussian.h"
00019 #include "RooChebychev.h"
00020 #include "RooAddPdf.h"
00021 #include "RooExtendPdf.h"
00022 #include "TCanvas.h"
00023 #include "TAxis.h"
00024 #include "RooPlot.h"
00025 using namespace RooFit ;
00026 
00027 
00028 void rf202_extendedmlfit()
00029 {
00030 
00031   // S e t u p   c o m p o n e n t   p d f s 
00032   // ---------------------------------------
00033 
00034   // Declare observable x
00035   RooRealVar x("x","x",0,10) ;
00036 
00037   // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
00038   RooRealVar mean("mean","mean of gaussians",5) ;
00039   RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
00040   RooRealVar sigma2("sigma2","width of gaussians",1) ;
00041 
00042   RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
00043   RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
00044   
00045   // Build Chebychev polynomial p.d.f.  
00046   RooRealVar a0("a0","a0",0.5,0.,1.) ;
00047   RooRealVar a1("a1","a1",-0.2,0.,1.) ;
00048   RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
00049 
00050   // Sum the signal components into a composite signal p.d.f.
00051   RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
00052   RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
00053 
00054   /////////////////////
00055   // M E T H O D   1 //
00056   /////////////////////
00057 
00058 
00059   // C o n s t r u c t   e x t e n d e d   c o m p o s i t e   m o d e l 
00060   // -------------------------------------------------------------------
00061 
00062   // Sum the composite signal and background into an extended pdf nsig*sig+nbkg*bkg
00063   RooRealVar nsig("nsig","number of signal events",500,0.,10000) ;
00064   RooRealVar nbkg("nbkg","number of background events",500,0,10000) ;
00065   RooAddPdf  model("model","(g1+g2)+a",RooArgList(bkg,sig),RooArgList(nbkg,nsig)) ;
00066 
00067 
00068 
00069   // S a m p l e ,   f i t   a n d   p l o t   e x t e n d e d   m o d e l 
00070   // ---------------------------------------------------------------------
00071 
00072   // Generate a data sample of expected number events in x from model
00073   // = model.expectedEvents() = nsig+nbkg
00074   RooDataSet *data = model.generate(x) ;
00075 
00076   // Fit model to data, extended ML term automatically included
00077   model.fitTo(*data) ; 
00078   
00079   // Plot data and PDF overlaid, use expected number of events for p.d.f projection normalization
00080   // rather than observed number of events (==data->numEntries())
00081   RooPlot* xframe = x.frame(Title("extended ML fit example")) ;
00082   data->plotOn(xframe) ;
00083   model.plotOn(xframe,Normalization(1.0,RooAbsReal::RelativeExpected)) ;
00084 
00085   // Overlay the background component of model with a dashed line
00086   model.plotOn(xframe,Components(bkg),LineStyle(kDashed),Normalization(1.0,RooAbsReal::RelativeExpected)) ;
00087 
00088   // Overlay the background+sig2 components of model with a dotted line
00089   model.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineStyle(kDotted),Normalization(1.0,RooAbsReal::RelativeExpected)) ;
00090 
00091   // Print structure of composite p.d.f.
00092   model.Print("t") ;
00093 
00094 
00095   /////////////////////
00096   // M E T H O D   2 //
00097   /////////////////////
00098 
00099   // C o n s t r u c t   e x t e n d e d   c o m p o n e n t s   f i r s t
00100   // ---------------------------------------------------------------------
00101 
00102   // Associated nsig/nbkg as expected number of events with sig/bkg
00103   RooExtendPdf esig("esig","extended signal p.d.f",sig,nsig) ;
00104   RooExtendPdf ebkg("ebkg","extended background p.d.f",bkg,nbkg) ;
00105 
00106 
00107   // S u m   e x t e n d e d   c o m p o n e n t s   w i t h o u t   c o e f s 
00108   // -------------------------------------------------------------------------
00109 
00110   // Construct sum of two extended p.d.f. (no coefficients required)
00111   RooAddPdf  model2("model2","(g1+g2)+a",RooArgList(ebkg,esig)) ;
00112 
00113 
00114 
00115   // Draw the frame on the canvas
00116   new TCanvas("rf202_composite","rf202_composite",600,600) ;
00117   gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ;
00118 
00119   
00120 }

Generated on Tue Jul 5 15:45:01 2011 for ROOT_528-00b_version by  doxygen 1.5.1