Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf202_extendedmlfit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Setting up an extended maximum likelihood fit.
5///
6/// \macro_image
7/// \macro_code
8/// \macro_output
9///
10/// \date July 2008
11/// \author Wouter Verkerke
12
13#include "RooRealVar.h"
14#include "RooDataSet.h"
15#include "RooGaussian.h"
16#include "RooChebychev.h"
17#include "RooAddPdf.h"
18#include "RooExtendPdf.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "RooPlot.h"
22using namespace RooFit;
23
25{
26
27 // S e t u p c o m p o n e n t p d f s
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 //----------------
51 // M E T H O D 1
52 //================
53
54 // C o n s t r u c t e x t e n d e d c o m p o s i t e m o d e l
55 // -------------------------------------------------------------------
56
57 // Sum the composite signal and background into an extended pdf nsig*sig+nbkg*bkg
58 RooRealVar nsig("nsig", "number of signal events", 500, 0., 10000);
59 RooRealVar nbkg("nbkg", "number of background events", 500, 0, 10000);
60 RooAddPdf model("model", "(g1+g2)+a", RooArgList(bkg, sig), RooArgList(nbkg, nsig));
61
62 // S a m p l e , f i t a n d p l o t e x t e n d e d m o d e l
63 // ---------------------------------------------------------------------
64
65 // Generate a data sample of expected number events in x from model
66 // = model.expectedEvents() = nsig+nbkg
67 std::unique_ptr<RooDataSet> data{model.generate(x)};
68
69 // Fit model to data, extended ML term automatically included
70 model.fitTo(*data, PrintLevel(-1));
71
72 // Plot data and PDF overlaid, use expected number of events for pdf projection normalization
73 // rather than observed number of events (==data->numEntries())
74 RooPlot *xframe = x.frame(Title("extended ML fit example"));
75 data->plotOn(xframe);
76 model.plotOn(xframe, Normalization(1.0, RooAbsReal::RelativeExpected));
77
78 // Overlay the background component of model with a dashed line
79 model.plotOn(xframe, Components(bkg), LineStyle(kDashed), Normalization(1.0, RooAbsReal::RelativeExpected));
80
81 // Overlay the background+sig2 components of model with a dotted line
82 model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted),
84
85 // Print structure of composite pdf
86 model.Print("t");
87
88 //----------------
89 // M E T H O D 2
90 //================
91
92 // C o n s t r u c t e x t e n d e d c o m p o n e n t s f i r s t
93 // ---------------------------------------------------------------------
94
95 // Associated nsig/nbkg as expected number of events with sig/bkg
96 RooExtendPdf esig("esig", "extended signal pdf", sig, nsig);
97 RooExtendPdf ebkg("ebkg", "extended background pdf", bkg, nbkg);
98
99 // S u m e x t e n d e d c o m p o n e n t s w i t h o u t c o e f s
100 // -------------------------------------------------------------------------
101
102 // Construct sum of two extended pdf (no coefficients required)
103 RooAddPdf model2("model2", "(g1+g2)+a", RooArgList(ebkg, esig));
104
105 // Draw the frame on the canvas
106 new TCanvas("rf202_composite", "rf202_composite", 600, 600);
107 gPad->SetLeftMargin(0.15);
108 xframe->GetYaxis()->SetTitleOffset(1.4);
109 xframe->Draw();
110}
@ kDashed
Definition TAttLine.h:48
@ kDotted
Definition TAttLine.h:48
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
Efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:33
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Chebychev polynomial p.d.f.
RooExtendPdf is a wrapper around an existing PDF that adds a parameteric extended likelihood term to ...
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:185
TAxis * GetYaxis() const
Definition RooPlot.cxx:1224
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:597
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
The Canvas class.
Definition TCanvas.h:23
RooCmdArg PrintLevel(Int_t code)
RooCmdArg Components(Args_t &&... argsOrArgSet)
RooCmdArg Normalization(double scaleFactor)
RooCmdArg LineStyle(Style_t style)
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:64
const char * Title
Definition TXMLSetup.cxx:68