 ROOT   Reference Guide rf701_efficiencyfit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Special pdf's: unbinned maximum likelihood fit of an efficiency eff(x) function
5///
6/// to a dataset D(x,cut), where cut is a category encoding a selection, of which the
7/// efficiency as function of x should be described by eff(x)
8///
9/// \macro_image
10/// \macro_output
11/// \macro_code
12///
13/// \date July 2008
14/// \author Wouter Verkerke
15
16#include "RooRealVar.h"
17#include "RooDataSet.h"
18#include "RooGaussian.h"
19#include "RooConstVar.h"
20#include "RooFormulaVar.h"
21#include "RooProdPdf.h"
22#include "RooEfficiency.h"
23#include "RooPolynomial.h"
24#include "RooCategory.h"
25#include "TCanvas.h"
26#include "TAxis.h"
27#include "RooPlot.h"
28using namespace RooFit;
29
31{
32 // C o n s t r u c t e f f i c i e n c y f u n c t i o n e ( x )
33 // -------------------------------------------------------------------
34
35 // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
36 RooRealVar x("x", "x", -10, 10);
37
38 // Efficiency function eff(x;a,b)
39 RooRealVar a("a", "a", 0.4, 0, 1);
40 RooRealVar b("b", "b", 5);
41 RooRealVar c("c", "c", -1, -10, 10);
42 RooFormulaVar effFunc("effFunc", "(1-a)+a*cos((x-c)/b)", RooArgList(a, b, c, x));
43
44 // C o n s t r u c t c o n d i t i o n a l e f f i c i e n c y p d f E ( c u t | x )
45 // ------------------------------------------------------------------------------------------
46
47 // Acceptance state cut (1 or 0)
48 RooCategory cut("cut", "cutr", { {"accept", 1}, {"reject", 0} });
49
50 // Construct efficiency pdf eff(cut|x)
51 RooEfficiency effPdf("effPdf", "effPdf", effFunc, cut, "accept");
52
53 // G e n e r a t e d a t a ( x , c u t ) f r o m a t o y m o d e l
54 // -----------------------------------------------------------------------------
55
56 // Construct global shape pdf shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
57 // (These are _only_ needed to generate some toy MC here to be used later)
58 RooPolynomial shapePdf("shapePdf", "shapePdf", x, RooConst(-0.095));
59 RooProdPdf model("model", "model", shapePdf, Conditional(effPdf, cut));
60
61 // Generate some toy data from model
62 RooDataSet *data = model.generate(RooArgSet(x, cut), 10000);
63
64 // F i t c o n d i t i o n a l e f f i c i e n c y p d f t o d a t a
65 // --------------------------------------------------------------------------
66
67 // Fit conditional efficiency pdf to data
68 effPdf.fitTo(*data, ConditionalObservables(x));
69
70 // P l o t f i t t e d , d a t a e f f i c i e n c y
71 // --------------------------------------------------------
72
73 // Plot distribution of all events and accepted fraction of events on frame
74 RooPlot *frame1 = x.frame(Bins(20), Title("Data (all, accepted)"));
75 data->plotOn(frame1);
76 data->plotOn(frame1, Cut("cut==cut::accept"), MarkerColor(kRed), LineColor(kRed));
77
78 // Plot accept/reject efficiency on data overlay fitted efficiency curve
79 RooPlot *frame2 = x.frame(Bins(20), Title("Fitted efficiency"));
80 data->plotOn(frame2, Efficiency(cut)); // needs ROOT version >= 5.21
81 effFunc.plotOn(frame2, LineColor(kRed));
82
83 // Draw all frames on a canvas
84 TCanvas *ca = new TCanvas("rf701_efficiency", "rf701_efficiency", 800, 400);
85 ca->Divide(2);
86 ca->cd(1);
88 frame1->GetYaxis()->SetTitleOffset(1.6);
89 frame1->Draw();
90 ca->cd(2);
92 frame2->GetYaxis()->SetTitleOffset(1.4);
93 frame2->Draw();
94}
#define b(i)
Definition: RSha256.hxx:100
#define c(i)
Definition: RSha256.hxx:101
@ kRed
Definition: Rtypes.h:66
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
Definition: RooAbsData.cxx:547
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
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:27
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
RooEfficiency is a PDF helper class to fit efficiencies parameterized by a supplied function F.
Definition: RooEfficiency.h:27
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
Definition: RooFormulaVar.h:30
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1263
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:37
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
The Canvas class.
Definition: TCanvas.h:23
Definition: TCanvas.cxx:708
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
RooConstVar & RooConst(Double_t val)
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, Bool_t depsAreCond=kFALSE)
RooCmdArg Bins(Int_t nbin)
RooCmdArg ConditionalObservables(const RooArgSet &set)
RooCmdArg MarkerColor(Color_t color)
RooCmdArg Cut(const char *cutSpec)
RooCmdArg LineColor(Color_t color)
RooCmdArg Efficiency(const RooCategory &cat)
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:68
auto * a
Definition: textangle.C:12