Logo ROOT  
Reference Guide
rf204a_extrangefit_RooAddPdf.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4///
5/// 'ADDITION AND CONVOLUTION' RooFit tutorial macro #204a
6///
7/// Extended maximum likelihood fit in multiple ranges.
8/// When an extended pdf and multiple ranges are used, the
9/// RooExtendPdf cannot correctly interpret the coefficients
10/// used for extension.
11/// This can be solved by using a RooAddPdf for extending the model.
12///
13/// \macro_output
14/// \macro_code
15///
16/// \date 12/2018
17/// \author Stephan Hageboeck
18
19
20#include "RooRealVar.h"
21#include "RooDataSet.h"
22#include "RooGaussian.h"
23#include "RooChebychev.h"
24#include "RooAddPdf.h"
25#include "RooExtendPdf.h"
26#include "RooFitResult.h"
27#include "TCanvas.h"
28#include "TAxis.h"
29#include "RooPlot.h"
30using namespace RooFit ;
31
32
33void rf204a_extrangefit_RooAddPdf()
34{
35
36
37 // S e t u p c o m p o n e n t p d f s
38 // ---------------------------------------
39
40 // Declare observable x
41 RooRealVar x("x","x",0,11) ;
42
43 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
44 RooRealVar mean("mean","mean of gaussians",5) ;
45 RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
46 RooRealVar sigma2("sigma2","width of gaussians",1) ;
47
48 RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;
49 RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;
50
51 // Build Chebychev polynomial p.d.f.
52 RooRealVar a0("a0","a0",0.5,0.,1.) ;
53 RooRealVar a1("a1","a1",0.2,0.,1.) ;
54 RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
55
56 // Sum the signal components into a composite signal p.d.f.
57 RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
58 RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
59
60
61 // E x t e n d t h e p d f s
62 // -----------------------------
63
64
65 // Define signal range in which events counts are to be defined
66 x.setRange("signalRange",4,6) ;
67
68 // Associated nsig/nbkg as expected number of events with sig/bkg _in_the_range_ "signalRange"
69 RooRealVar nsig("nsig","number of signal events in signalRange",500,0.,10000) ;
70 RooRealVar nbkg("nbkg","number of background events in signalRange",500,0,10000) ;
71
72 // Use AddPdf to extend the model. Giving as many coefficients as pdfs switches
73 // on extension.
74 RooAddPdf model("model","(g1+g2)+a", RooArgList(bkg,sig), RooArgList(nbkg,nsig)) ;
75
76
77 // S a m p l e d a t a , f i t m o d e l
78 // -------------------------------------------
79
80 // Generate 1000 events from model so that nsig,nbkg come out to numbers <<500 in fit
81 RooDataSet *data = model.generate(x,1000) ;
82
83
84
85 auto canv = new TCanvas("Canvas", "Canvas", 1500, 600);
86 canv->Divide(3,1);
87
88 // Fit full range
89 // -------------------------------------------
90
91 canv->cd(1);
92
93 // Perform unbinned ML fit to data, full range
94
95 // IMPORTANT:
96 // The model needs to be copied when fitting with different ranges because
97 // the interpretation of the coefficients is tied to the fit range
98 // that's used in the first fit
99 RooAddPdf model1(model);
100 RooFitResult* r = model1.fitTo(*data,Save()) ;
101 r->Print() ;
102
103 RooPlot * frame = x.frame(Title("Full range fitted"));
104 data->plotOn(frame);
105 model1.plotOn(frame, VisualizeError(*r));
106 model1.plotOn(frame);
107 model1.paramOn(frame);
108 frame->Draw();
109
110
111 // Fit in two regions
112 // -------------------------------------------
113
114 canv->cd(2);
115 x.setRange("left", 0., 4.);
116 x.setRange("right", 6., 10.);
117
118 RooAddPdf model2(model);
119 RooFitResult* r2 = model2.fitTo(*data,
120 Range("left,right"),
121 Save()) ;
122 r2->Print();
123
124
125 RooPlot * frame2 = x.frame(Title("Fit in left/right sideband"));
126 data->plotOn(frame2);
127 model2.plotOn(frame2, VisualizeError(*r2));
128 model2.plotOn(frame2);
129 model2.paramOn(frame2);
130 frame2->Draw();
131
132
133 // Fit in one region
134 // -------------------------------------------
135 // Note how restricting the region to only the left tail increases
136 // the fit uncertainty
137
138 canv->cd(3);
139 x.setRange("leftToMiddle", 0., 5.);
140
141 RooAddPdf model3(model);
142 RooFitResult* r3 = model3.fitTo(*data,
143 Range("leftToMiddle"),
144 Save()) ;
145 r3->Print();
146
147
148 RooPlot * frame3 = x.frame(Title("Fit from left to middle"));
149 data->plotOn(frame3);
150 model3.plotOn(frame3, VisualizeError(*r3));
151 model3.plotOn(frame3);
152 model3.paramOn(frame3);
153 frame3->Draw();
154
155 canv->Draw();
156}
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Calls RooPlot* plotOn(RooPlot* frame, const RooLinkedList& cmdList) const ;.
Definition: RooAbsData.cxx:546
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
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooFitResult.h:66
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:712
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)
RooCmdArg VisualizeError(const RooDataSet &paramData, Double_t Z=1)
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
const char * Title
Definition: TXMLSetup.cxx:67
Ta Range(0, 0, 1, 1)