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