Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf701_efficiencyfit.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Special pdf's: unbinned maximum likelihood fit of an efficiency eff(x) function to a
5## dataset D(x,cut), cut is a category encoding a selection, which the efficiency as function
6## of x should be described by eff(x)
7##
8## \macro_image
9## \macro_code
10## \macro_output
11##
12## \date February 2018
13## \authors Clemens Lange, Wouter Verkerke (C++ version)
14
15import ROOT
16
17
18# Construct efficiency function e(x)
19# -------------------------------------------------------------------
20
21# Declare variables x,mean, with associated name, title, value and allowed
22# range
23x = ROOT.RooRealVar("x", "x", -10, 10)
24
25# Efficiency function eff(x;a,b)
26a = ROOT.RooRealVar("a", "a", 0.4, 0, 1)
27b = ROOT.RooRealVar("b", "b", 5)
28c = ROOT.RooRealVar("c", "c", -1, -10, 10)
29effFunc = ROOT.RooFormulaVar("effFunc", "(1-a)+a*cos((x-c)/b)", [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", {"accept": 1, "reject": 0})
36
37# Construct efficiency pdf 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 pdf 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("shapePdf", "shapePdf", x, [-0.095])
46model = ROOT.RooProdPdf("model", "model", {shapePdf}, Conditional=({effPdf}, {cut}))
47
48# Generate some toy data from model
49data = model.generate({x, cut}, 10000)
50
51# Fit conditional efficiency pdf to data
52# --------------------------------------------------------------------------
53
54# Fit conditional efficiency pdf to data
55effPdf.fitTo(data, ConditionalObservables={x}, PrintLevel=-1)
56
57# Plot fitted, data efficiency
58# --------------------------------------------------------
59
60# Plot distribution of all events and accepted fraction of events on frame
61frame1 = x.frame(Bins=20, Title="Data (all, accepted)")
62data.plotOn(frame1)
63data.plotOn(frame1, Cut="cut==cut::accept", MarkerColor="r", LineColor="r")
64
65# Plot accept/reject efficiency on data overlay fitted efficiency curve
66frame2 = x.frame(Bins=20, Title="Fitted efficiency")
67data.plotOn(frame2, Efficiency=cut) # needs ROOT version >= 5.21
68effFunc.plotOn(frame2, LineColor="r")
69
70# Draw all frames on a canvas
71ca = ROOT.TCanvas("rf701_efficiency", "rf701_efficiency", 800, 400)
72ca.Divide(2)
73ca.cd(1)
74ROOT.gPad.SetLeftMargin(0.15)
75frame1.GetYaxis().SetTitleOffset(1.6)
76frame1.Draw()
77ca.cd(2)
78ROOT.gPad.SetLeftMargin(0.15)
79frame2.GetYaxis().SetTitleOffset(1.4)
80frame2.Draw()
81
82ca.SaveAs("rf701_efficiencyfit.png")