ROOT   Reference Guide
rf702_efficiencyfit_2D.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4///
5/// Special p.d.f.'s: unbinned maximum likelihood fit of an efficiency eff(x) function
6/// to a dataset D(x,cut), cut is a category encoding a selection whose efficiency as function
7/// of x should be described by eff(x)
8///
9/// \macro_image
10/// \macro_output
11/// \macro_code
12///
13/// \date February 2018
14/// \authors Clemens Lange, Wouter Verkerke (C++ version)
15
16#include "RooRealVar.h"
17#include "RooDataSet.h"
18#include "RooGaussian.h"
19#include "RooConstVar.h"
20#include "RooCategory.h"
21#include "RooEfficiency.h"
22#include "RooPolynomial.h"
23#include "RooProdPdf.h"
24#include "RooFormulaVar.h"
25#include "TCanvas.h"
26#include "TAxis.h"
27#include "TH1.h"
28#include "RooPlot.h"
29using namespace RooFit;
30
32{
33 // 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 )
34 // -----------------------------------------------------------------------
35
36 // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
37 RooRealVar x("x", "x", -10, 10);
38 RooRealVar y("y", "y", -10, 10);
39
40 // Efficiency function eff(x;a,b)
41 RooRealVar ax("ax", "ay", 0.6, 0, 1);
42 RooRealVar bx("bx", "by", 5);
43 RooRealVar cx("cx", "cy", -1, -10, 10);
44
45 RooRealVar ay("ay", "ay", 0.2, 0, 1);
46 RooRealVar by("by", "by", 5);
47 RooRealVar cy("cy", "cy", -1, -10, 10);
48
49 RooFormulaVar effFunc("effFunc", "((1-ax)+ax*cos((x-cx)/bx))*((1-ay)+ay*cos((y-cy)/by))",
50 RooArgList(ax, bx, cx, x, ay, by, cy, y));
51
52 // Acceptance state cut (1 or 0)
53 RooCategory cut("cut", "cutr", { {"accept", 1}, {"reject", 0} });
54
55 // 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 )
56 // ---------------------------------------------------------------------------------------------
57
58 // Construct efficiency p.d.f eff(cut|x)
59 RooEfficiency effPdf("effPdf", "effPdf", effFunc, cut, "accept");
60
61 // 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
62 // -------------------------------------------------------------------------------
63
64 // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
65 // (These are _only_ needed to generate some toy MC here to be used later)
66 RooPolynomial shapePdfX("shapePdfX", "shapePdfX", x, RooConst(flat ? 0 : -0.095));
67 RooPolynomial shapePdfY("shapePdfY", "shapePdfY", y, RooConst(flat ? 0 : +0.095));
68 RooProdPdf shapePdf("shapePdf", "shapePdf", RooArgSet(shapePdfX, shapePdfY));
69 RooProdPdf model("model", "model", shapePdf, Conditional(effPdf, cut));
70
71 // Generate some toy data from model
72 RooDataSet *data = model.generate(RooArgSet(x, y, cut), 10000);
73
74 // 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
75 // --------------------------------------------------------------------------
76
77 // Fit conditional efficiency p.d.f to data
78 effPdf.fitTo(*data, ConditionalObservables(RooArgSet(x, y)));
79
80 // P l o t f i t t e d , d a t a e f f i c i e n c y
81 // --------------------------------------------------------
82
83 // Make 2D histograms of all data, selected data and efficiency function
84 TH1 *hh_data_all = data->createHistogram("hh_data_all", x, Binning(8), YVar(y, Binning(8)));
85 TH1 *hh_data_sel = data->createHistogram("hh_data_sel", x, Binning(8), YVar(y, Binning(8)), Cut("cut==cut::accept"));
86 TH1 *hh_eff = effFunc.createHistogram("hh_eff", x, Binning(50), YVar(y, Binning(50)));
87
88 // Some adjustment for good visualization
89 hh_data_all->SetMinimum(0);
90 hh_data_sel->SetMinimum(0);
91 hh_eff->SetMinimum(0);
92 hh_eff->SetLineColor(kBlue);
93
94 // Draw all frames on a canvas
95 TCanvas *ca = new TCanvas("rf702_efficiency_2D", "rf702_efficiency_2D", 1200, 400);
96 ca->Divide(3);
97 ca->cd(1);
99 hh_data_all->GetZaxis()->SetTitleOffset(1.8);
100 hh_data_all->Draw("lego");
101 ca->cd(2);
103 hh_data_sel->GetZaxis()->SetTitleOffset(1.8);
104 hh_data_sel->Draw("lego");
105 ca->cd(3);
107 hh_eff->GetZaxis()->SetTitleOffset(1.8);
108 hh_eff->Draw("surf");
109
110 return;
111}
const Bool_t kFALSE
Definition: RtypesCore.h:90
@ kBlue
Definition: Rtypes.h:64
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:28
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:23
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
TH2F * createHistogram(const RooAbsRealLValue &var1, const RooAbsRealLValue &var2, const char *cuts="", const char *name="hist") const
Create a TH2F histogram of the distribution of the specified variable using this dataset.
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:29
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:31
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:294
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
The Canvas class.
Definition: TCanvas.h:27
Definition: TCanvas.cxx:701
The TH1 histogram class.
Definition: TH1.h:56
TAxis * GetZaxis()
Definition: TH1.h:318
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:395
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2998
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)