Logo ROOT   6.16/01
Reference Guide
rf702_efficiencyfit_2D.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4##
5## 'SPECIAL PDFS' RooFit tutorial macro #702
6##
7## Unbinned maximum likelihood fit of an efficiency eff(x) function to
8## a dataset D(x,cut), cut is a category encoding a selection whose
9## efficiency as function of x should be described by eff(x)
10##
11## \macro_code
12##
13## \date February 2018
14## \author Clemens Lange
15## \author Wouter Verkerke (C version)
16
17
18import ROOT
19
20
21flat=ROOT.kFALSE
22# Construct efficiency function e(x,y)
23# -----------------------------------------------------------------------
24
25# Declare variables x,mean, with associated name, title, value and allowed
26# range
27x = ROOT.RooRealVar("x", "x", -10, 10)
28y = ROOT.RooRealVar("y", "y", -10, 10)
29
30# Efficiency function eff(x;a,b)
31ax = ROOT.RooRealVar("ax", "ay", 0.6, 0, 1)
32bx = ROOT.RooRealVar("bx", "by", 5)
33cx = ROOT.RooRealVar("cx", "cy", -1, -10, 10)
34
35ay = ROOT.RooRealVar("ay", "ay", 0.2, 0, 1)
36by = ROOT.RooRealVar("by", "by", 5)
37cy = ROOT.RooRealVar("cy", "cy", -1, -10, 10)
38
39effFunc = ROOT.RooFormulaVar("effFunc", "((1-ax)+ax*cos((x-cx)/bx))*((1-ay)+ay*cos((y-cy)/by))", ROOT.RooArgList(ax, bx, cx, x, ay, by, cy, y))
40
41# Acceptance state cut (1 or 0)
42cut = ROOT.RooCategory("cut", "cutr")
43cut.defineType("accept", 1)
44cut.defineType("reject", 0)
45
46# Construct conditional efficiency pdf E(cut|x,y)
47# ---------------------------------------------------------------------------------------------
48
49# Construct efficiency p.d.f eff(cut|x)
50effPdf = ROOT.RooEfficiency("effPdf", "effPdf", effFunc, cut, "accept")
51
52# Generate data(x,y,cut) from a toy model
53# -------------------------------------------------------------------------------
54
55# Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
56# (These are _only_ needed to generate some toy MC here to be used later)
57shapePdfX = ROOT.RooPolynomial("shapePdfX", "shapePdfX", x, ROOT.RooArgList(ROOT.RooFit.RooConst(0 if flat else -0.095)))
58shapePdfY = ROOT.RooPolynomial("shapePdfY", "shapePdfY", y, ROOT.RooArgList(ROOT.RooFit.RooConst(0 if flat else +0.095)))
59shapePdf = ROOT.RooProdPdf("shapePdf", "shapePdf", ROOT.RooArgList(shapePdfX, shapePdfY))
60model = ROOT.RooProdPdf("model", "model", ROOT.RooArgSet(shapePdf), ROOT.RooFit.Conditional(ROOT.RooArgSet(effPdf), ROOT.RooArgSet(cut)))
61
62# Generate some toy data from model
63data = model.generate(ROOT.RooArgSet(x, y, cut), 10000)
64
65# Fit conditional efficiency pdf to data
66# --------------------------------------------------------------------------
67
68# Fit conditional efficiency p.d.f to data
69effPdf.fitTo(data, ROOT.RooFit.ConditionalObservables(ROOT.RooArgSet(x, y)))
70
71# Plot fitted, data efficiency
72# --------------------------------------------------------
73
74# Make 2D histograms of all data, data and efficiency function
75hh_data_all = ROOT.RooAbsData.createHistogram(data, "hh_data_all", x, ROOT.RooFit.Binning(
76 8), ROOT.RooFit.YVar(y, ROOT.RooFit.Binning(8)))
77hh_data_sel = ROOT.RooAbsData.createHistogram(data, "hh_data_sel", x, ROOT.RooFit.Binning(
78 8), ROOT.RooFit.YVar(y, ROOT.RooFit.Binning(8)), ROOT.RooFit.Cut("cut==cut::accept"))
79hh_eff = effFunc.createHistogram(
80"hh_eff", x, ROOT.RooFit.Binning(
81 50), ROOT.RooFit.YVar(y, ROOT.RooFit.Binning(50)))
82
83# Some adjustsment for good visualization
84hh_data_all.SetMinimum(0)
85hh_data_sel.SetMinimum(0)
86hh_eff.SetMinimum(0)
87hh_eff.SetLineColor(ROOT.kBlue)
88
89# Draw all frames on a canvas
90ca = ROOT.TCanvas("rf702_efficiency_2D", "rf702_efficiency_2D", 1200, 400)
91ca.Divide(3)
92ca.cd(1)
93ROOT.gPad.SetLeftMargin(0.15)
94hh_data_all.GetZaxis().SetTitleOffset(1.8)
95hh_data_all.Draw("lego")
96ca.cd(2)
97ROOT.gPad.SetLeftMargin(0.15)
98hh_data_sel.GetZaxis().SetTitleOffset(1.8)
99hh_data_sel.Draw("lego")
100ca.cd(3)
101ROOT.gPad.SetLeftMargin(0.15)
102hh_eff.GetZaxis().SetTitleOffset(1.8)
103hh_eff.Draw("surf")
104
105ca.SaveAs("rf702_efficiency_2D.png")