Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf703_effpdfprod.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ##
5 ## Special p.d.f.'s: using a product of an (acceptance) efficiency and a p.d.f as p.d.f.
6 ##
7 ## \macro_code
8 ##
9 ## \date February 2018
10 ## \author Clemens Lange, Wouter Verkerke (C++ version)
11 
12 import ROOT
13 
14 
15 # Define observables and decay pdf
16 # ---------------------------------------------------------------
17 
18 # Declare observables
19 t = ROOT.RooRealVar("t", "t", 0, 5)
20 
21 # Make pdf
22 tau = ROOT.RooRealVar("tau", "tau", -1.54, -4, -0.1)
23 model = ROOT.RooExponential("model", "model", t, tau)
24 
25 # Define efficiency function
26 # ---------------------------------------------------
27 
28 # Use error function to simulate turn-on slope
29 eff = ROOT.RooFormulaVar(
30  "eff",
31  "0.5*(TMath::Erf((t-1)/0.5)+1)",
32  ROOT.RooArgList(t))
33 
34 # Define decay pdf with efficiency
35 # ---------------------------------------------------------------
36 
37 # Multiply pdf(t) with efficiency in t
38 modelEff = ROOT.RooEffProd("modelEff", "model with efficiency", model, eff)
39 
40 # Plot efficiency, pdf
41 # ----------------------------------------
42 
43 frame1 = t.frame(ROOT.RooFit.Title("Efficiency"))
44 eff.plotOn(frame1, ROOT.RooFit.LineColor(ROOT.kRed))
45 
46 frame2 = t.frame(ROOT.RooFit.Title("Pdf with and without efficiency"))
47 
48 model.plotOn(frame2, ROOT.RooFit.LineStyle(ROOT.kDashed))
49 modelEff.plotOn(frame2)
50 
51 # Generate toy data, fit model eff to data
52 # ------------------------------------------------------------------------------
53 
54 # Generate events. If the input pdf has an internal generator, internal generator
55 # is used and an accept/reject sampling on the efficiency is applied.
56 data = modelEff.generate(ROOT.RooArgSet(t), 10000)
57 
58 # Fit pdf. The normalization integral is calculated numerically.
59 modelEff.fitTo(data)
60 
61 # Plot generated data and overlay fitted pdf
62 frame3 = t.frame(ROOT.RooFit.Title("Fitted pdf with efficiency"))
63 data.plotOn(frame3)
64 modelEff.plotOn(frame3)
65 
66 c = ROOT.TCanvas("rf703_effpdfprod", "rf703_effpdfprod", 1200, 400)
67 c.Divide(3)
68 c.cd(1)
69 ROOT.gPad.SetLeftMargin(0.15)
70 frame1.GetYaxis().SetTitleOffset(1.4)
71 frame1.Draw()
72 c.cd(2)
73 ROOT.gPad.SetLeftMargin(0.15)
74 frame2.GetYaxis().SetTitleOffset(1.6)
75 frame2.Draw()
76 c.cd(3)
77 ROOT.gPad.SetLeftMargin(0.15)
78 frame3.GetYaxis().SetTitleOffset(1.6)
79 frame3.Draw()
80 
81 c.SaveAs("rf703_effpdfprod.png")