Logo ROOT   6.16/01
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' RooFit tutorial macro #509
6##
7## Easy CINT interactive access to workspace contents through a
8## 'C++' namespace in CINT that maps the workspace contents in a typesafe way
9##
10## NB: ROOT.This macro exploits a feature native to CINT and _cannot_ be compiled
11##
12## \macro_code
13##
14## \date February 2018
15## \author Clemens Lange
16## \author Wouter Verkerke (C version)
17
18
19import ROOT
20
21
22def fillWorkspace(w):
23 # Create pdf and fill workspace
24 # --------------------------------------------------------
25
26 # Declare observable x
27 x = ROOT.RooRealVar("x", "x", 0, 10)
28
29 # Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
30 # their parameters
31 mean = ROOT.RooRealVar("mean", "mean of gaussians", 5, 0, 10)
32 sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
33 sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
34
35 sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
36 sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
37
38 # Build Chebychev polynomial p.d.f.
39 a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0., 1.)
40 a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0., 1.)
41 bkg = ROOT.RooChebychev("bkg", "Background", x, ROOT.RooArgList(a0, a1))
42
43 # Sum the signal components into a composite signal p.d.f.
44 sig1frac = ROOT.RooRealVar(
45 "sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.)
46 sig = ROOT.RooAddPdf(
47 "sig", "Signal", ROOT.RooArgList(sig1, sig2), ROOT.RooArgList(sig1frac))
48
49 # Sum the composite signal and background
50 bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0., 1.)
51 model = ROOT.RooAddPdf(
52 "model", "g1+g2+a", ROOT.RooArgList(bkg, sig), ROOT.RooArgList(bkgfrac))
53
54 getattr(w, 'import')(model)
55
56
57
58# Create and fill workspace
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")