Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf702_efficiencyfit_2D.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
5## to a dataset D(x,cut), cut is a category encoding a selection whose efficiency as function
6## of x should be described by eff(x)
7##
8## \macro_code
9##
10## \date February 2018
11## \authors Clemens Lange, Wouter Verkerke (C++ version)
12
13import ROOT
14
15
16flat = False
17# Construct efficiency function e(x,y)
18# -----------------------------------------------------------------------
19
20# Declare variables x,mean, with associated name, title, value and allowed
21# range
22x = ROOT.RooRealVar("x", "x", -10, 10)
23y = ROOT.RooRealVar("y", "y", -10, 10)
24
25# Efficiency function eff(x;a,b)
26ax = ROOT.RooRealVar("ax", "ay", 0.6, 0, 1)
27bx = ROOT.RooRealVar("bx", "by", 5)
28cx = ROOT.RooRealVar("cx", "cy", -1, -10, 10)
29
30ay = ROOT.RooRealVar("ay", "ay", 0.2, 0, 1)
31by = ROOT.RooRealVar("by", "by", 5)
32cy = ROOT.RooRealVar("cy", "cy", -1, -10, 10)
33
34effFunc = ROOT.RooFormulaVar(
35 "effFunc", "((1-ax)+ax*cos((x-cx)/bx))*((1-ay)+ay*cos((y-cy)/by))", [ax, bx, cx, x, ay, by, cy, y]
36)
37
38# Acceptance state cut (1 or 0)
39cut = ROOT.RooCategory("cut", "cutr", {"accept": 1, "reject": 0})
40
41# Construct conditional efficiency pdf E(cut|x,y)
42# ---------------------------------------------------------------------------------------------
43
44# Construct efficiency pdf eff(cut|x)
45effPdf = ROOT.RooEfficiency("effPdf", "effPdf", effFunc, cut, "accept")
46
47# Generate data(x,y,cut) from a toy model
48# -------------------------------------------------------------------------------
49
50# Construct global shape pdf shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
51# (These are _only_ needed to generate some toy MC here to be used later)
52shapePdfX = ROOT.RooPolynomial("shapePdfX", "shapePdfX", x, [0 if flat else -0.095])
53shapePdfY = ROOT.RooPolynomial("shapePdfY", "shapePdfY", y, [0 if flat else +0.095])
54shapePdf = ROOT.RooProdPdf("shapePdf", "shapePdf", [shapePdfX, shapePdfY])
55model = ROOT.RooProdPdf("model", "model", {shapePdf}, Conditional=({effPdf}, {cut}))
56
57# Generate some toy data from model
58data = model.generate({x, y, cut}, 10000)
59
60# Fit conditional efficiency pdf to data
61# --------------------------------------------------------------------------
62
63# Fit conditional efficiency pdf to data
64effPdf.fitTo(data, ConditionalObservables={x, y})
65
66# Plot fitted, data efficiency
67# --------------------------------------------------------
68
69# Make 2D histograms of all data, data and efficiency function
70hh_data_all = ROOT.RooAbsData.createHistogram(data, "hh_data_all", x, Binning=(8), YVar=dict(var=y, Binning=8))
71hh_data_sel = ROOT.RooAbsData.createHistogram(
72 data, "hh_data_sel", x, Binning=8, YVar=dict(var=y, Binning=8), Cut="cut==cut::accept"
73)
74hh_eff = effFunc.createHistogram("hh_eff", x, Binning=50, YVar=dict(var=y, Binning=50))
75
76# Some adjustsment for good visualization
77hh_data_all.SetMinimum(0)
78hh_data_sel.SetMinimum(0)
79hh_eff.SetMinimum(0)
80hh_eff.SetLineColor(ROOT.kBlue)
81
82# Draw all frames on a canvas
83ca = ROOT.TCanvas("rf702_efficiency_2D", "rf702_efficiency_2D", 1200, 400)
84ca.Divide(3)
85ca.cd(1)
86ROOT.gPad.SetLeftMargin(0.15)
87hh_data_all.GetZaxis().SetTitleOffset(1.8)
88hh_data_all.Draw("lego")
89ca.cd(2)
90ROOT.gPad.SetLeftMargin(0.15)
91hh_data_sel.GetZaxis().SetTitleOffset(1.8)
92hh_data_sel.Draw("lego")
93ca.cd(3)
94ROOT.gPad.SetLeftMargin(0.15)
95hh_eff.GetZaxis().SetTitleOffset(1.8)
96hh_eff.Draw("surf")
97
98ca.SaveAs("rf702_efficiency_2D.png")