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