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"
29 using namespace RooFit;
30 
31 void rf204_extrangefit()
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 }
RooChebychev.h
RooAddPdf
Definition: RooAddPdf.h:32
RooExtendPdf.h
TObject::Print
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:552
RooChebychev
Definition: RooChebychev.h:25
r
ROOT::R::TRInterface & r
Definition: Object.C:4
RooArgList
Definition: RooArgList.h:21
RooGaussian.h
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
RooAddPdf.h
TCanvas.h
RooDataSet.h
RooFitResult
Definition: RooFitResult.h:40
RooFit
Definition: RooCFunction1Binding.h:29
RooPlot.h
RooRealVar.h
RooFitResult.h
TCanvas
Definition: TCanvas.h:23
TAxis.h
RooDataSet
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooRealVar
Definition: RooRealVar.h:35
RooFit::Save
RooCmdArg Save(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:187
rf204_extrangefit
Definition: rf204_extrangefit.py:1
RooArgSet
Definition: RooArgSet.h:28