Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
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 getattr(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 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
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 p.d.f 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")