rf602_chi2fit.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #602
00004 // 
00005 // Setting up a chi^2 fit to a binned dataset
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 "RooConstVar.h"
00020 #include "RooChebychev.h"
00021 #include "RooAddPdf.h"
00022 #include "RooChi2Var.h"
00023 #include "RooMinuit.h"
00024 #include "TCanvas.h"
00025 #include "TAxis.h"
00026 #include "RooPlot.h"
00027 using namespace RooFit ;
00028 
00029 
00030 void rf602_chi2fit()
00031 {
00032 
00033   // S e t u p   m o d e l
00034   // ---------------------
00035 
00036   // Declare observable x
00037   RooRealVar x("x","x",0,10) ;
00038 
00039   // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
00040   RooRealVar mean("mean","mean of gaussians",5) ;
00041   RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
00042   RooRealVar sigma2("sigma2","width of gaussians",1) ;
00043 
00044   RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
00045   RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
00046   
00047   // Build Chebychev polynomial p.d.f.  
00048   RooRealVar a0("a0","a0",0.5,0.,1.) ;
00049   RooRealVar a1("a1","a1",-0.2,0.,1.) ;
00050   RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
00051 
00052   // Sum the signal components into a composite signal p.d.f.
00053   RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
00054   RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
00055 
00056   // Sum the composite signal and background 
00057   RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
00058   RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
00059 
00060 
00061   // C r e a t e   b i n n e d   d a t a s e t
00062   // -----------------------------------------
00063 
00064   RooDataSet* d = model.generate(x,10000) ;
00065   RooDataHist* dh = d->binnedClone() ;
00066 
00067   // Construct a chi^2 of the data and the model.
00068   // When a p.d.f. is used in a chi^2 fit, the probability density scaled
00069   // by the number of events in the dataset to obtain the fit function
00070   // If model is an extended p.d.f, the expected number events is used
00071   // instead of the observed number of events.
00072   model.chi2FitTo(*dh) ;
00073 
00074   // NB: It is also possible to fit a RooAbsReal function to a RooDataHist
00075   // using chi2FitTo(). 
00076 
00077   // Note that entries with zero bins are _not_ allowed 
00078   // for a proper chi^2 calculation and will give error
00079   // messages  
00080   RooDataSet* dsmall = (RooDataSet*) d->reduce(EventRange(1,100)) ;
00081   RooDataHist* dhsmall = dsmall->binnedClone() ;
00082   RooChi2Var chi2_lowstat("chi2_lowstat","chi2",model,*dhsmall) ;
00083   cout << chi2_lowstat.getVal() << endl ;
00084 
00085 
00086 }

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