Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf509_wsinteractive.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Organization and simultaneous fits: easy interactive access to workspace contents - CINT
5## to CLING code migration
6##
7## \macro_code
8##
9## \date February 2018
10## \authors Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
13
14
15def fillWorkspace(w):
16 # Create pdf and fill workspace
17 # --------------------------------------------------------
18
19 # Declare observable x
20 x = ROOT.RooRealVar("x", "x", 0, 10)
21
22 # Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
23 # their parameters
24 mean = ROOT.RooRealVar("mean", "mean of gaussians", 5, 0, 10)
25 sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
26 sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
27
28 sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
29 sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
30
31 # Build Chebychev polynomial pdf
32 a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
33 a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0.0, 1.0)
34 bkg = ROOT.RooChebychev("bkg", "Background", x, [a0, a1])
35
36 # Sum the signal components into a composite signal pdf
37 sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
38 sig = ROOT.RooAddPdf("sig", "Signal", [sig1, sig2], [sig1frac])
39
40 # Sum the composite signal and background
41 bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0.0, 1.0)
42 model = ROOT.RooAddPdf("model", "g1+g2+a", [bkg, sig], [bkgfrac])
43
44 w.Import(model)
45
46
47# Create and fill workspace
48# ------------------------------------------------
49
50
51# Create a workspace named 'w'
52# With CINT w could exports its contents to
53# a same-name C++ namespace in CINT 'namespace w'.
54# but self does not work anymore in CLING.
55# so self tutorial is an example on how to
56# change the code
57w = ROOT.RooWorkspace("w", True)
58
59# Fill workspace with pdf and data in a separate function
60fillWorkspace(w)
61
62# Print workspace contents
63w.Print()
64
65# self does not work anymore with CLING
66# use normal workspace functionality
67
68# Use workspace contents
69# ----------------------------------------------
70
71# Old syntax to use the name space prefix operator to access the workspace contents
72#
73# d = w.model.generate(w.x,1000)
74# r = w.model.fitTo(*d)
75
76# use normal workspace methods
77model = w["model"]
78x = w["x"]
79
80d = model.generate({x}, 1000)
81r = model.fitTo(d)
82
83# old syntax to access the variable x
84# frame = w.x.frame()
85
86frame = x.frame()
87d.plotOn(frame)
88
89# OLD syntax to ommit x.
90# NB: The 'w.' prefix can be omitted if namespace w is imported in local namespace
91# in the usual C++ way
92#
93# using namespace w
94# model.plotOn(frame)
95# model.plotOn(frame, ROOT.RooFit.Components(bkg), ROOT.RooFit.LineStyle(ROOT.kDashed))
96
97# correct syntax
98bkg = w["bkg"]
99model.plotOn(frame)
100model.plotOn(frame, Components=bkg, LineStyle="--")
101
102# Draw the frame on the canvas
103c = ROOT.TCanvas("rf509_wsinteractive", "rf509_wsinteractive", 600, 600)
104ROOT.gPad.SetLeftMargin(0.15)
105frame.GetYaxis().SetTitleOffset(1.4)
106frame.Draw()
107
108c.SaveAs("rf509_wsinteractive.png")