Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf207_comptools.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## 'ADDITION AND CONVOLUTION' RooFit tutorial macro #207
5## Tools and utilities for manipulation of composite objects
6##
7## \macro_code
8## \macro_output
9##
10## \date February 2018
11## \authors Clemens Lange, Wouter Verkerke (C version)
12
13import ROOT
14
15# Set up composite pdf dataset
16# --------------------------------------------------------
17
18# Declare observable x
19x = ROOT.RooRealVar("x", "x", 0, 10)
20
21# Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
22# their parameters
23mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
24sigma = ROOT.RooRealVar("sigma", "width of gaussians", 0.5)
25sig = ROOT.RooGaussian("sig", "Signal component 1", x, mean, sigma)
26
27# Build Chebychev polynomial p.d.f.
28a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
29a1 = ROOT.RooRealVar("a1", "a1", 0.2, 0.0, 1.0)
30bkg1 = ROOT.RooChebychev("bkg1", "Background 1", x, [a0, a1])
31
32# Build expontential pdf
33alpha = ROOT.RooRealVar("alpha", "alpha", -1)
34bkg2 = ROOT.RooExponential("bkg2", "Background 2", x, alpha)
35
36# Sum the background components into a composite background p.d.f.
37bkg1frac = ROOT.RooRealVar("bkg1frac", "fraction of component 1 in background", 0.2, 0.0, 1.0)
38bkg = ROOT.RooAddPdf("bkg", "Signal", [bkg1, bkg2], [bkg1frac])
39
40# Sum the composite signal and background
41bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0.0, 1.0)
42model = ROOT.RooAddPdf("model", "g1+g2+a", [bkg, sig], [bkgfrac])
43
44# Create dummy dataset that has more observables than the above pdf
45y = ROOT.RooRealVar("y", "y", -10, 10)
46data = ROOT.RooDataSet("data", "data", {x, y})
47
48# Basic information requests
49# ---------------------------------------------
50
51
52# Get list of observables
53# ---------------------------------------------
54
55# Get list of observables of pdf in context of a dataset
56#
57# Observables are define each context as the variables
58# shared between a model and a dataset. In self case
59# that is the variable 'x'
60
61model_obs = model.getObservables(data)
62ROOT.SetOwnership(model_obs, True)
63model_obs.Print("v")
64
65# Get list of parameters
66# -------------------------------------------
67
68# Get list of parameters, list of observables
69model_params = model.getParameters({x})
70ROOT.SetOwnership(model_params, True)
71model_params.Print("v")
72
73# Get list of parameters, a dataset
74# (Gives identical results to operation above)
75model_params2 = model.getParameters(data)
76ROOT.SetOwnership(model_params2, True)
77model_params2.Print()
78
79# Get list of components
80# -------------------------------------------
81
82# Get list of component objects, top-level node
83model_comps = model.getComponents()
84ROOT.SetOwnership(model_comps, True)
85model_comps.Print("v")
86
87# Modifications to structure of composites
88# -------------------------------------------
89
90# Create a second Gaussian
91sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
92sig2 = ROOT.RooGaussian("sig2", "Signal component 1", x, mean, sigma2)
93
94# Create a sum of the original Gaussian plus the second Gaussian
95sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
96sigsum = ROOT.RooAddPdf("sigsum", "sig+sig2", [sig, sig2], [sig1frac])
97
98# Construct a customizer utility to customize model
99cust = ROOT.RooCustomizer(model, "cust")
100
101# Instruct the customizer to replace node 'sig' with node 'sigsum'
102cust.replaceArg(sig, sigsum)
103
104# Build a clone of the input pdf according to the above customization
105# instructions. Each node that requires modified is clone so that the
106# original pdf remained untouched. The name of each cloned node is that
107# of the original node suffixed by the name of the customizer object
108#
109# The returned head node own all nodes that were cloned as part of
110# the build process so when cust_clone is deleted so will all other
111# nodes that were created in the process.
112cust_clone = cust.build(ROOT.kTRUE)
113ROOT.SetOwnership(cust_clone, True)
114
115# Print structure of clone of model with sig.sigsum replacement.
116cust_clone.Print("t")