Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf602_chi2fit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4/// Likelihood and minimization: setting up a chi^2 fit to a binned dataset
5///
6/// \macro_output
7/// \macro_code
8///
9/// \date July 2008
10/// \author Wouter Verkerke
11
12#include "RooRealVar.h"
13#include "RooDataSet.h"
14#include "RooGaussian.h"
15#include "RooConstVar.h"
16#include "RooChebychev.h"
17#include "RooAddPdf.h"
18#include "RooChi2Var.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "RooPlot.h"
22using namespace RooFit;
23
24void rf602_chi2fit()
25{
26
27 // S e t u p m o d e l
28 // ---------------------
29
30 // Declare observable x
31 RooRealVar x("x", "x", 0, 10);
32
33 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
34 RooRealVar mean("mean", "mean of gaussians", 5);
35 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
36 RooRealVar sigma2("sigma2", "width of gaussians", 1);
37
38 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
39 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
40
41 // Build Chebychev polynomial pdf
42 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
43 RooRealVar a1("a1", "a1", 0.2, 0., 1.);
44 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
45
46 // Sum the signal components into a composite signal pdf
47 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
48 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
49
50 // Sum the composite signal and background
51 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
52 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
53
54 // C r e a t e b i n n e d d a t a s e t
55 // -----------------------------------------
56
57 RooDataSet *d = model.generate(x, 10000);
58 RooDataHist *dh = d->binnedClone();
59
60 // Construct a chi^2 of the data and the model.
61 // When a pdf is used in a chi^2 fit, the probability density scaled
62 // by the number of events in the dataset to obtain the fit function
63 // If model is an extended pdf, the expected number events is used
64 // instead of the observed number of events.
65 model.chi2FitTo(*dh);
66
67 // NB: It is also possible to fit a RooAbsReal function to a RooDataHist
68 // using chi2FitTo().
69
70 // Note that entries with zero bins are _not_ allowed
71 // for a proper chi^2 calculation and will give error
72 // messages
73 RooDataSet *dsmall = (RooDataSet *)d->reduce(EventRange(1, 100));
74 RooDataHist *dhsmall = dsmall->binnedClone();
75 RooChi2Var chi2_lowstat("chi2_lowstat", "chi2", model, *dhsmall);
76 cout << chi2_lowstat.getVal() << endl;
77}
#define d(i)
Definition RSha256.hxx:102
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.
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:32
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:29
Chebychev polynomial p.d.f.
RooChi2Var implements a simple calculation from a binned dataset and a PDF.
Definition RooChi2Var.h:25
The RooDataHist is a container class to hold N-dimensional binned data.
Definition RooDataHist.h:37
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:33
RooDataHist * binnedClone(const char *newName=0, const char *newTitle=0) const
Return binned clone of this dataset.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
RooCmdArg EventRange(Int_t nStart, Int_t nStop)
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...