Logo ROOT  
Reference Guide
rf204_extrangefit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4///
5/// 'ADDITION AND CONVOLUTION' RooFit tutorial macro #204
6///
7/// Extended maximum likelihood fit with alternate range definition
8/// for observed number of events.
9/// If multiple ranges are used, or only a part of the data is fitted,
10/// it is advisable to use a RooAddPdf to extend the model. See tutorial
11/// 204a.
12///
13/// \macro_output
14/// \macro_code
15///
16/// \date 07/2008
17/// \author Wouter Verkerke
18
19#include "RooRealVar.h"
20#include "RooDataSet.h"
21#include "RooGaussian.h"
22#include "RooChebychev.h"
23#include "RooAddPdf.h"
24#include "RooExtendPdf.h"
25#include "RooFitResult.h"
26#include "TCanvas.h"
27#include "TAxis.h"
28#include "RooPlot.h"
29using namespace RooFit;
30
32{
33
34 // S e t u p c o m p o n e n t p d f s
35 // ---------------------------------------
36
37 // Declare observable x
38 RooRealVar x("x", "x", 0, 10);
39
40 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
41 RooRealVar mean("mean", "mean of gaussians", 5);
42 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
43 RooRealVar sigma2("sigma2", "width of gaussians", 1);
44
45 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
46 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
47
48 // Build Chebychev polynomial p.d.f.
49 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
50 RooRealVar a1("a1", "a1", 0.2, 0., 1.);
51 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
52
53 // Sum the signal components into a composite signal p.d.f.
54 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
55 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
56
57 // 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
58 // ------------------------------------------------------------------------------
59
60 // Define signal range in which events counts are to be defined
61 x.setRange("signalRange", 4, 6);
62
63 // Associated nsig/nbkg as expected number of events with sig/bkg _in_the_range_ "signalRange"
64 RooRealVar nsig("nsig", "number of signal events in signalRange", 500, 0., 10000) ;
65 RooRealVar nbkg("nbkg", "number of background events in signalRange", 500, 0, 10000) ;
66
67 // Use AddPdf to extend the model:
68 RooAddPdf model("model","(g1+g2)+a", RooArgList(bkg,sig), RooArgList(nbkg,nsig)) ;
69
70 // Clone these models here because the interpretation of normalisation coefficients changes
71 // when different ranges are used:
72 RooAddPdf model2(model);
73 RooAddPdf model3(model);
74
75
76 // S a m p l e d a t a , f i t m o d e l
77 // -------------------------------------------
78
79 // Generate 1000 events from model so that nsig,nbkg come out to numbers <<500 in fit
80 RooDataSet *data = model.generate(x, 1000);
81
82
83 auto canv = new TCanvas("Canvas", "Canvas", 1500, 600);
84 canv->Divide(3,1);
85
86 // Fit full range
87 // -------------------------------------------
88
89 canv->cd(1);
90
91 // Perform unbinned ML fit to data, full range
92 RooFitResult* r = model.fitTo(*data,Save()) ;
93 r->Print() ;
94
95}
ROOT::R::TRInterface & r
Definition: Object.C:4
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
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:28
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
The Canvas class.
Definition: TCanvas.h:27
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:550
RooCmdArg Save(Bool_t flag=kTRUE)
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...