Logo ROOT  
Reference Guide
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 
12 import ROOT
13 
14 
15 # Create master pdf
16 # ---------------------------------
17 
18 # Construct gauss(x,m,s)
19 x = ROOT.RooRealVar("x", "x", -10, 10)
20 m = ROOT.RooRealVar("m", "m", 0, -10, 10)
21 s = ROOT.RooRealVar("s", "s", 1, -10, 10)
22 gauss = ROOT.RooGaussian("g", "g", x, m, s)
23 
24 # Construct poly(x,p0)
25 p0 = ROOT.RooRealVar("p0", "p0", 0.01, 0., 1.)
26 poly = ROOT.RooPolynomial("p", "p", x, ROOT.RooArgList(p0))
27 
28 # model = f*gauss(x) + (1-f)*poly(x)
29 f = ROOT.RooRealVar("f", "f", 0.5, 0., 1.)
30 model = 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
37 c = ROOT.RooCategory("c", "c")
38 c.defineType("run1")
39 c.defineType("run2")
40 
41 d = ROOT.RooCategory("d", "d")
42 d.defineType("foo")
43 d.defineType("bar")
44 
45 # Set up SimWSTool
46 # -----------------------------
47 
48 # Import ingredients in a workspace
49 w = ROOT.RooWorkspace("w", "w")
50 w.Import(ROOT.RooArgSet(model, c, d))
51 
52 # Make Sim builder tool
53 sct = ROOT.RooSimWSTool(w)
54 
55 # Build a simultaneous model with one split
56 # ---------------------------------------------------------------------------------
57 
58 # Construct a simultaneous pdf 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 pdf is owned by the workspace
66 model_sim = sct.build("model_sim", "model",
67  ROOT.RooFit.SplitParam("m", "c"))
68 
69 # Print tree structure of model
70 model_sim.Print("t")
71 
72 # Adjust model_sim parameters in workspace
73 w.var("m_run1").setVal(-3)
74 w.var("m_run2").setVal(+3)
75 
76 # Print contents of workspace
77 w.Print("v")
78 
79 # Build a simultaneous model with product split
80 # -----------------------------------------------------------------------------------------
81 
82 # Build another simultaneous pdf using a composite split in states c X d
83 model_sim2 = sct.build("model_sim2", "model",
84  ROOT.RooFit.SplitParam("p0", "c,d"))
85 
86 # Print tree structure of self model
87 model_sim2.Print("t")