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