Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf702_efficiencyfit_2D.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4/// Special pdf's: unbinned maximum likelihood fit of an efficiency eff(x) function
5/// to a dataset D(x,cut), cut is a category encoding a selection whose efficiency as function
6/// of x should be described by eff(x)
7///
8/// \macro_image
9/// \macro_code
10/// \macro_output
11///
12/// \date February 2018
13/// \authors Clemens Lange, Wouter Verkerke (C++ version)
14
15#include "RooRealVar.h"
16#include "RooDataSet.h"
17#include "RooGaussian.h"
18#include "RooConstVar.h"
19#include "RooCategory.h"
20#include "RooEfficiency.h"
21#include "RooPolynomial.h"
22#include "RooProdPdf.h"
23#include "RooFormulaVar.h"
24#include "TCanvas.h"
25#include "TAxis.h"
26#include "TH1.h"
27#include "RooPlot.h"
28using namespace RooFit;
29
30void rf702_efficiencyfit_2D(bool flat = false)
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 , y )
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 RooRealVar y("y", "y", -10, 10);
38
39 // Efficiency function eff(x;a,b)
40 RooRealVar ax("ax", "ay", 0.6, 0, 1);
41 RooRealVar bx("bx", "by", 5);
42 RooRealVar cx("cx", "cy", -1, -10, 10);
43
44 RooRealVar ay("ay", "ay", 0.2, 0, 1);
45 RooRealVar by("by", "by", 5);
46 RooRealVar cy("cy", "cy", -1, -10, 10);
47
48 RooFormulaVar effFunc("effFunc", "((1-ax)+ax*cos((x-cx)/bx))*((1-ay)+ay*cos((y-cy)/by))",
49 RooArgList(ax, bx, cx, x, ay, by, cy, y));
50
51 // Acceptance state cut (1 or 0)
52 RooCategory cut("cut", "cutr", { {"accept", 1}, {"reject", 0} });
53
54 // 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 , y )
55 // ---------------------------------------------------------------------------------------------
56
57 // Construct efficiency pdf eff(cut|x)
58 RooEfficiency effPdf("effPdf", "effPdf", effFunc, cut, "accept");
59
60 // G e n e r a t e d a t a ( x , y , c u t ) f r o m a t o y m o d e l
61 // -------------------------------------------------------------------------------
62
63 // Construct global shape pdf shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
64 // (These are _only_ needed to generate some toy MC here to be used later)
65 RooPolynomial shapePdfX("shapePdfX", "shapePdfX", x, RooConst(flat ? 0 : -0.095));
66 RooPolynomial shapePdfY("shapePdfY", "shapePdfY", y, RooConst(flat ? 0 : +0.095));
67 RooProdPdf shapePdf("shapePdf", "shapePdf", RooArgSet(shapePdfX, shapePdfY));
68 RooProdPdf model("model", "model", shapePdf, Conditional(effPdf, cut));
69
70 // Generate some toy data from model
71 std::unique_ptr<RooDataSet> data{model.generate({x, y, cut}, 10000)};
72
73 // 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
74 // --------------------------------------------------------------------------
75
76 // Fit conditional efficiency pdf to data
77 effPdf.fitTo(*data, ConditionalObservables(RooArgSet(x, y)), PrintLevel(-1));
78
79 // P l o t f i t t e d , d a t a e f f i c i e n c y
80 // --------------------------------------------------------
81
82 // Make 2D histograms of all data, selected data and efficiency function
83 TH1 *hh_data_all = data->createHistogram("hh_data_all", x, Binning(8), YVar(y, Binning(8)));
84 TH1 *hh_data_sel = data->createHistogram("hh_data_sel", x, Binning(8), YVar(y, Binning(8)), Cut("cut==cut::accept"));
85 TH1 *hh_eff = effFunc.createHistogram("hh_eff", x, Binning(50), YVar(y, Binning(50)));
86
87 // Some adjustment for good visualization
88 hh_data_all->SetMinimum(0);
89 hh_data_sel->SetMinimum(0);
90 hh_eff->SetMinimum(0);
91 hh_eff->SetLineColor(kBlue);
92
93 // Draw all frames on a canvas
94 TCanvas *ca = new TCanvas("rf702_efficiency_2D", "rf702_efficiency_2D", 1200, 400);
95 ca->Divide(3);
96 ca->cd(1);
97 gPad->SetLeftMargin(0.15);
98 hh_data_all->GetZaxis()->SetTitleOffset(1.8);
99 hh_data_all->Draw("lego");
100 ca->cd(2);
101 gPad->SetLeftMargin(0.15);
102 hh_data_sel->GetZaxis()->SetTitleOffset(1.8);
103 hh_data_sel->Draw("lego");
104 ca->cd(3);
105 gPad->SetLeftMargin(0.15);
106 hh_eff->GetZaxis()->SetTitleOffset(1.8);
107 hh_eff->Draw("surf");
108
109 return;
110}
@ kBlue
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
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:55
Object to represent discrete states.
Definition RooCategory.h:28
A PDF helper class to fit efficiencies parameterized by a supplied function F.
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
RooPolynomial implements a polynomial p.d.f of the form.
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:33
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
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition TAttPad.cxx:109
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
TAxis * GetZaxis()
Definition TH1.h:326
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3062
virtual void SetMinimum(Double_t minimum=-1111)
Definition TH1.h:404
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
Automatic pad generation by division.
Definition TPad.cxx:1153
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg={})
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, bool depsAreCond=false)
RooConstVar & RooConst(double val)
RooCmdArg PrintLevel(Int_t code)
RooCmdArg ConditionalObservables(Args_t &&... argsOrArgSet)
Create a RooCmdArg to declare conditional observables.
RooCmdArg Binning(const RooAbsBinning &binning)
RooCmdArg Cut(const char *cutSpec)
Double_t y[n]
Definition legend1.C:17
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 JSONIO.h:26