Logo ROOT   6.18/05
Reference Guide
rf701_efficiencyfit.py
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 to a dataset D(x,cut), cut is a category encoding a selection, which the efficiency as function of x should be described by eff(x)
6##
7## \macro_code
8##
9## \date February 2018
10## \author Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
13
14
15# Construct efficiency function e(x)
16# -------------------------------------------------------------------
17
18# Declare variables x,mean, with associated name, title, value and allowed
19# range
20x = ROOT.RooRealVar("x", "x", -10, 10)
21
22# Efficiency function eff(x;a,b)
23a = ROOT.RooRealVar("a", "a", 0.4, 0, 1)
24b = ROOT.RooRealVar("b", "b", 5)
25c = ROOT.RooRealVar("c", "c", -1, -10, 10)
26effFunc = ROOT.RooFormulaVar(
27 "effFunc", "(1-a)+a*cos((x-c)/b)", ROOT.RooArgList(a, b, c, x))
28
29# Construct conditional efficiency pdf E(cut|x)
30# ------------------------------------------------------------------------------------------
31
32# Acceptance state cut (1 or 0)
33cut = ROOT.RooCategory("cut", "cutr")
34cut.defineType("accept", 1)
35cut.defineType("reject", 0)
36
37# Construct efficiency p.d.f eff(cut|x)
38effPdf = ROOT.RooEfficiency("effPdf", "effPdf", effFunc, cut, "accept")
39
40# Generate data (x, cut) from a toy model
41# -----------------------------------------------------------------------------
42
43# Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
44# (These are _only_ needed to generate some toy MC here to be used later)
45shapePdf = ROOT.RooPolynomial(
46 "shapePdf", "shapePdf", x, ROOT.RooArgList(ROOT.RooFit.RooConst(-0.095)))
47model = ROOT.RooProdPdf(
48 "model",
49 "model",
50 ROOT.RooArgSet(shapePdf),
51 ROOT.RooFit.Conditional(
52 ROOT.RooArgSet(effPdf),
53 ROOT.RooArgSet(cut)))
54
55# Generate some toy data from model
56data = model.generate(ROOT.RooArgSet(x, cut), 10000)
57
58# Fit conditional efficiency pdf to data
59# --------------------------------------------------------------------------
60
61# Fit conditional efficiency p.d.f to data
62effPdf.fitTo(data, ROOT.RooFit.ConditionalObservables(ROOT.RooArgSet(x)))
63
64# Plot fitted, data efficiency
65# --------------------------------------------------------
66
67# Plot distribution of all events and accepted fraction of events on frame
68frame1 = x.frame(ROOT.RooFit.Bins(
69 20), ROOT.RooFit.Title("Data (all, accepted)"))
70data.plotOn(frame1)
71data.plotOn(
72 frame1,
73 ROOT.RooFit.Cut("cut==cut::accept"),
74 ROOT.RooFit.MarkerColor(
75 ROOT.kRed),
76 ROOT.RooFit.LineColor(
77 ROOT.kRed))
78
79# Plot accept/reject efficiency on data overlay fitted efficiency curve
80frame2 = x.frame(ROOT.RooFit.Bins(
81 20), ROOT.RooFit.Title("Fitted efficiency"))
82data.plotOn(frame2, ROOT.RooFit.Efficiency(cut)) # needs ROOT version >= 5.21
83effFunc.plotOn(frame2, ROOT.RooFit.LineColor(ROOT.kRed))
84
85# Draw all frames on a canvas
86ca = ROOT.TCanvas("rf701_efficiency", "rf701_efficiency", 800, 400)
87ca.Divide(2)
88ca.cd(1)
89ROOT.gPad.SetLeftMargin(0.15)
90frame1.GetYaxis().SetTitleOffset(1.6)
91frame1.Draw()
92ca.cd(2)
93ROOT.gPad.SetLeftMargin(0.15)
94frame2.GetYaxis().SetTitleOffset(1.4)
95frame2.Draw()
96
97ca.SaveAs("rf701_efficiencyfit.png")