ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rf602_chi2fit.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #602
4 ///
5 /// Setting up a chi^2 fit to a binned dataset
6 ///
7 ///
8 ///
9 /// \macro_code
10 /// \author 07/2008 - Wouter Verkerke
11 
12 
13 #ifndef __CINT__
14 #include "RooGlobalFunc.h"
15 #endif
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooGaussian.h"
19 #include "RooConstVar.h"
20 #include "RooChebychev.h"
21 #include "RooAddPdf.h"
22 #include "RooChi2Var.h"
23 #include "RooMinuit.h"
24 #include "TCanvas.h"
25 #include "TAxis.h"
26 #include "RooPlot.h"
27 using namespace RooFit ;
28 
29 
30 void rf602_chi2fit()
31 {
32 
33  // S e t u p m o d e l
34  // ---------------------
35 
36  // Declare observable x
37  RooRealVar x("x","x",0,10) ;
38 
39  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
40  RooRealVar mean("mean","mean of gaussians",5) ;
41  RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
42  RooRealVar sigma2("sigma2","width of gaussians",1) ;
43 
44  RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;
45  RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;
46 
47  // Build Chebychev polynomial p.d.f.
48  RooRealVar a0("a0","a0",0.5,0.,1.) ;
49  RooRealVar a1("a1","a1",-0.2,0.,1.) ;
50  RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
51 
52  // Sum the signal components into a composite signal p.d.f.
53  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
54  RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
55 
56  // Sum the composite signal and background
57  RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
58  RooAddPdf model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
59 
60 
61  // C r e a t e b i n n e d d a t a s e t
62  // -----------------------------------------
63 
64  RooDataSet* d = model.generate(x,10000) ;
65  RooDataHist* dh = d->binnedClone() ;
66 
67  // Construct a chi^2 of the data and the model.
68  // When a p.d.f. is used in a chi^2 fit, the probability density scaled
69  // by the number of events in the dataset to obtain the fit function
70  // If model is an extended p.d.f, the expected number events is used
71  // instead of the observed number of events.
72  model.chi2FitTo(*dh) ;
73 
74  // NB: It is also possible to fit a RooAbsReal function to a RooDataHist
75  // using chi2FitTo().
76 
77  // Note that entries with zero bins are _not_ allowed
78  // for a proper chi^2 calculation and will give error
79  // messages
80  RooDataSet* dsmall = (RooDataSet*) d->reduce(EventRange(1,100)) ;
81  RooDataHist* dhsmall = dsmall->binnedClone() ;
82  RooChi2Var chi2_lowstat("chi2_lowstat","chi2",model,*dhsmall) ;
83  cout << chi2_lowstat.getVal() << endl ;
84 
85 
86 }
RooAbsData * reduce(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg())
Create a reduced copy of this dataset.
Definition: RooAbsData.cxx:399
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
Double_t x[n]
Definition: legend1.C:17
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
friend class RooArgSet
Definition: RooAbsArg.h:469
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooCmdArg EventRange(Int_t nStart, Int_t nStop)
RooDataHist * binnedClone(const char *newName=0, const char *newTitle=0) const
Return binned clone of this dataset.
Definition: RooDataSet.cxx:981
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooAbsMoment * mean(RooRealVar &obs)
Definition: RooAbsReal.h:297