Logo ROOT  
Reference Guide
rf504_simwstool.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook -nodraw
4##
5## Organization and simultaneous fits: using RooSimWSTool to construct a simultaneous p.d.f that is built of variations of an input p.d.f
6##
7## \macro_code
8##
9## \date February 2018
10## \author Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
13
14
15# Create master pdf
16# ---------------------------------
17
18# Construct gauss(x,m,s)
19x = ROOT.RooRealVar("x", "x", -10, 10)
20m = ROOT.RooRealVar("m", "m", 0, -10, 10)
21s = ROOT.RooRealVar("s", "s", 1, -10, 10)
22gauss = ROOT.RooGaussian("g", "g", x, m, s)
23
24# Construct poly(x,p0)
25p0 = ROOT.RooRealVar("p0", "p0", 0.01, 0., 1.)
26poly = ROOT.RooPolynomial("p", "p", x, ROOT.RooArgList(p0))
27
28# model = f*gauss(x) + (1-f)*poly(x)
29f = ROOT.RooRealVar("f", "f", 0.5, 0., 1.)
30model = ROOT.RooAddPdf("model", "model", ROOT.RooArgList(
31 gauss, poly), ROOT.RooArgList(f))
32
33# Create category observables for splitting
34# ----------------------------------------------------------------------------------
35
36# Define two categories that can be used for splitting
37c = ROOT.RooCategory("c", "c")
38c.defineType("run1")
39c.defineType("run2")
40
41d = ROOT.RooCategory("d", "d")
42d.defineType("foo")
43d.defineType("bar")
44
45# Set up SimWSTool
46# -----------------------------
47
48# Import ingredients in a workspace
49w = ROOT.RooWorkspace("w", "w")
50getattr(w, 'import')(ROOT.RooArgSet(model, c, d))
51
52# Make Sim builder tool
53sct = ROOT.RooSimWSTool(w)
54
55# Build a simultaneous model with one split
56# ---------------------------------------------------------------------------------
57
58# Construct a simultaneous p.d.f with the following form
59#
60# model_run1(x) = f*gauss_run1(x,m_run1,s) + (1-f)*poly
61# model_run2(x) = f*gauss_run2(x,m_run2,s) + (1-f)*poly
62# simpdf(x,c) = model_run1(x) if c=="run1"
63# = model_run2(x) if c=="run2"
64#
65# Returned p.d.f is owned by the workspace
66model_sim = sct.build("model_sim", "model",
67 ROOT.RooFit.SplitParam("m", "c"))
68
69# Print tree structure of model
70model_sim.Print("t")
71
72# Adjust model_sim parameters in workspace
73w.var("m_run1").setVal(-3)
74w.var("m_run2").setVal(+3)
75
76# Print contents of workspace
77w.Print("v")
78
79# Build a simultaneous model with product split
80# -----------------------------------------------------------------------------------------
81
82# Build another simultaneous p.d.f using a composite split in states c X d
83model_sim2 = sct.build("model_sim2", "model",
84 ROOT.RooFit.SplitParam("p0", "c,d"))
85
86# Print tree structure of self model
87model_sim2.Print("t")