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