Logo ROOT  
Reference Guide
rf510_wsnamedsets.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 #510
6##
7## Working with named parameter sets and parameter snapshots in
8## workspaces
9##
10## \macro_code
11##
12## \date February 2018
13## \author Clemens Lange
14## \author Wouter Verkerke (C version)
15
16
17import ROOT
18
19
20def fillWorkspace(w):
21 # Create model
22 # -----------------------
23
24 # Declare observable x
25 x = ROOT.RooRealVar("x", "x", 0, 10)
26
27 # Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
28 # their parameters
29 mean = ROOT.RooRealVar("mean", "mean of gaussians", 5, 0, 10)
30 sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
31 sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
32
33 sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
34 sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
35
36 # Build Chebychev polynomial p.d.f.
37 a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
38 a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0.0, 1.0)
39 bkg = ROOT.RooChebychev("bkg", "Background", x, [a0, a1])
40
41 # Sum the signal components into a composite signal p.d.f.
42 sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
43 sig = ROOT.RooAddPdf("sig", "Signal", [sig1, sig2], [sig1frac])
44
45 # Sum the composite signal and background
46 bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0.0, 1.0)
47 model = ROOT.RooAddPdf("model", "g1+g2+a", [bkg, sig], [bkgfrac])
48
49 # Import model into p.d.f.
50 w.Import(model)
51
52 # Encode definition of parameters in workspace
53 # ---------------------------------------------------------------------------------------
54
55 # Define named sets "parameters" and "observables", list which variables should be considered
56 # parameters and observables by the users convention
57 #
58 # Variables appearing in sets _must_ live in the workspace already, the autoImport flag
59 # of defineSet must be set to import them on the fly. Named sets contain only references
60 # to the original variables, the value of observables in named sets already
61 # reflect their 'current' value
62 params = model.getParameters({x})
63 w.defineSet("parameters", params)
64 w.defineSet("observables", {x})
65
66 # Encode reference value for parameters in workspace
67 # ---------------------------------------------------------------------------------------------------
68
69 # Define a parameter 'snapshot' in the p.d.f.
70 # Unlike a named set, parameter snapshot stores an independent set of values for
71 # a given set of variables in the workspace. The values can be stored and reloaded
72 # into the workspace variable objects using the loadSnapshot() and saveSnapshot()
73 # methods. A snapshot saves the value of each variable, errors that are stored
74 # with it as well as the 'Constant' flag that is used in fits to determine if a
75 # parameter is kept fixed or not.
76
77 # Do a dummy fit to a (supposedly) reference dataset here and store the results
78 # of that fit into a snapshot
79 refData = model.generate({x}, 10000)
80 model.fitTo(refData, PrintLevel=-1)
81
82 # The kTRUE flag imports the values of the objects in (*params) into the workspace
83 # If not set, present values of the workspace parameters objects are stored
84 w.saveSnapshot("reference_fit", params, True)
85
86 # Make another fit with the signal componentforced to zero
87 # and save those parameters too
88
89 bkgfrac.setVal(1)
90 bkgfrac.setConstant(True)
91 bkgfrac.removeError()
92 model.fitTo(refData, PrintLevel=-1)
93
94 w.saveSnapshot("reference_fit_bkgonly", params, True)
95
96
97# Create model and dataset
98# -----------------------------------------------
99
100w = ROOT.RooWorkspace("w")
101fillWorkspace(w)
102
103# Exploit convention encoded in named set "parameters" and "observables"
104# to use workspace contents w/o need for introspected
105model = w["model"]
106
107# Generate data from p.d.f. in given observables
108data = model.generate(w.set("observables"), 1000)
109
110# Fit model to data
111model.fitTo(data)
112
113# Plot fitted model and data on frame of first (only) observable
114frame = (w.set("observables").first()).frame()
115data.plotOn(frame)
116model.plotOn(frame)
117
118# Overlay plot with model with reference parameters as stored in snapshots
119w.loadSnapshot("reference_fit")
120model.plotOn(frame, LineColor="r")
121w.loadSnapshot("reference_fit_bkgonly")
122model.plotOn(frame, LineColor="r", LineStyle="--")
123
124# Draw the frame on the canvas
125c = ROOT.TCanvas("rf510_wsnamedsets", "rf503_wsnamedsets", 600, 600)
126ROOT.gPad.SetLeftMargin(0.15)
127frame.GetYaxis().SetTitleOffset(1.4)
128frame.Draw()
129
130c.SaveAs("rf510_wsnamedsets.png")
131
132# Print workspace contents
133w.Print()
134
135# Workspace will remain in memory after macro finishes
136ROOT.gDirectory.Add(w)
Definition: first.py:1