Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
12 import 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
20 x = ROOT.RooRealVar("x", "x", -10, 10)
21 
22 # Efficiency function eff(x;a,b)
23 a = ROOT.RooRealVar("a", "a", 0.4, 0, 1)
24 b = ROOT.RooRealVar("b", "b", 5)
25 c = ROOT.RooRealVar("c", "c", -1, -10, 10)
26 effFunc = 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)
33 cut = ROOT.RooCategory("cut", "cutr")
34 cut.defineType("accept", 1)
35 cut.defineType("reject", 0)
36 
37 # Construct efficiency p.d.f eff(cut|x)
38 effPdf = 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)
45 shapePdf = ROOT.RooPolynomial(
46  "shapePdf", "shapePdf", x, ROOT.RooArgList(ROOT.RooFit.RooConst(-0.095)))
47 model = 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
56 data = 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
62 effPdf.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
68 frame1 = x.frame(ROOT.RooFit.Bins(
69  20), ROOT.RooFit.Title("Data (all, accepted)"))
70 data.plotOn(frame1)
71 data.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
80 frame2 = x.frame(ROOT.RooFit.Bins(
81  20), ROOT.RooFit.Title("Fitted efficiency"))
82 data.plotOn(frame2, ROOT.RooFit.Efficiency(cut)) # needs ROOT version >= 5.21
83 effFunc.plotOn(frame2, ROOT.RooFit.LineColor(ROOT.kRed))
84 
85 # Draw all frames on a canvas
86 ca = ROOT.TCanvas("rf701_efficiency", "rf701_efficiency", 800, 400)
87 ca.Divide(2)
88 ca.cd(1)
89 ROOT.gPad.SetLeftMargin(0.15)
90 frame1.GetYaxis().SetTitleOffset(1.6)
91 frame1.Draw()
92 ca.cd(2)
93 ROOT.gPad.SetLeftMargin(0.15)
94 frame2.GetYaxis().SetTitleOffset(1.4)
95 frame2.Draw()
96 
97 ca.SaveAs("rf701_efficiencyfit.png")