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 
14 import 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
22 x = ROOT.RooRealVar("x", "x", -10, 10)
23 
24 # Efficiency function eff(x;a,b)
25 a = ROOT.RooRealVar("a", "a", 0.4, 0, 1)
26 b = ROOT.RooRealVar("b", "b", 5)
27 c = ROOT.RooRealVar("c", "c", -1, -10, 10)
28 effFunc = 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)
35 cut = ROOT.RooCategory("cut", "cutr")
36 cut.defineType("accept", 1)
37 cut.defineType("reject", 0)
38 
39 # Construct efficiency p.d.f eff(cut|x)
40 effPdf = 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)
47 shapePdf = ROOT.RooPolynomial(
48  "shapePdf", "shapePdf", x, ROOT.RooArgList(ROOT.RooFit.RooConst(-0.095)))
49 model = 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
58 data = 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
64 effPdf.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
70 frame1 = x.frame(ROOT.RooFit.Bins(
71  20), ROOT.RooFit.Title("Data (all, accepted)"))
72 data.plotOn(frame1)
73 data.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
82 frame2 = x.frame(ROOT.RooFit.Bins(
83  20), ROOT.RooFit.Title("Fitted efficiency"))
84 data.plotOn(frame2, ROOT.RooFit.Efficiency(cut)) # needs ROOT version >= 5.21
85 effFunc.plotOn(frame2, ROOT.RooFit.LineColor(ROOT.kRed))
86 
87 # Draw all frames on a canvas
88 ca = ROOT.TCanvas("rf701_efficiency", "rf701_efficiency", 800, 400)
89 ca.Divide(2)
90 ca.cd(1)
91 ROOT.gPad.SetLeftMargin(0.15)
92 frame1.GetYaxis().SetTitleOffset(1.6)
93 frame1.Draw()
94 ca.cd(2)
95 ROOT.gPad.SetLeftMargin(0.15)
96 frame2.GetYaxis().SetTitleOffset(1.4)
97 frame2.Draw()
98 
99 ca.SaveAs("rf701_efficiencyfit.png")