Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf504_simwstool.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook -nodraw
4## Organization and simultaneous fits: using RooSimWSTool to construct a simultaneous pdf
5## that is built of variations of an input pdf
6##
7## \macro_code
8##
9## \date February 2018
10## \authors 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.0, 1.0)
26poly = ROOT.RooPolynomial("p", "p", x, [p0])
27
28# model = f*gauss(x) + (1-f)*poly(x)
29f = ROOT.RooRealVar("f", "f", 0.5, 0.0, 1.0)
30model = ROOT.RooAddPdf("model", "model", [gauss, poly], [f])
31
32# Create category observables for splitting
33# ----------------------------------------------------------------------------------
34
35# Define two categories that can be used for splitting
36c = ROOT.RooCategory("c", "c")
37c.defineType("run1")
38c.defineType("run2")
39
40d = ROOT.RooCategory("d", "d")
41d.defineType("foo")
42d.defineType("bar")
43
44# Set up SimWSTool
45# -----------------------------
46
47# Import ingredients in a workspace
48w = ROOT.RooWorkspace("w", "w")
49w.Import({model, c, d})
50
51# Make Sim builder tool
52sct = ROOT.RooSimWSTool(w)
53
54# Build a simultaneous model with one split
55# ---------------------------------------------------------------------------------
56
57# Construct a simultaneous pdf with the following form
58#
59# model_run1(x) = f*gauss_run1(x,m_run1,s) + (1-f)*poly
60# model_run2(x) = f*gauss_run2(x,m_run2,s) + (1-f)*poly
61# simpdf(x,c) = model_run1(x) if c=="run1"
62# = model_run2(x) if c=="run2"
63#
64# Returned pdf is owned by the workspace
65model_sim = sct.build("model_sim", "model", SplitParam=("m", "c"))
66
67# Print tree structure of model
68model_sim.Print("t")
69
70# Adjust model_sim parameters in workspace
71w.var("m_run1").setVal(-3)
72w.var("m_run2").setVal(+3)
73
74# Print contents of workspace
75w.Print("v")
76
77# Build a simultaneous model with product split
78# -----------------------------------------------------------------------------------------
79
80# Build another simultaneous pdf using a composite split in states c X d
81model_sim2 = sct.build("model_sim2", "model", SplitParam=("p0", "c,d"))
82
83# Print tree structure of self model
84model_sim2.Print("t")