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