Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf508_listsetmanip.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #508
5##
6## RooArgSet and RooArgList tools and tricks
7##
8## \macro_code
9## \macro_output
10##
11## \date February 2018
12## \authors Clemens Lange, Wouter Verkerke (C version)
13
14import ROOT
15
16
17# Create dummy objects
18# ---------------------------------------
19
20# Create some variables
21a = ROOT.RooRealVar("a", "a", 1, -10, 10)
22b = ROOT.RooRealVar("b", "b", 2, -10, 10)
23c = ROOT.RooRealVar("c", "c", 3, -10, 10)
24d = ROOT.RooRealVar("d", "d", 4, -10, 10)
25x = ROOT.RooRealVar("x", "x", 0, -10, 10)
26c.setError(0.5)
27a.setConstant()
28b.setConstant()
29
30# Create a category
31e = ROOT.RooCategory("e", "e")
32e.defineType("sig")
33e.defineType("bkg")
34
35# Create a pdf
36g = ROOT.RooGaussian("g", "g", x, a, b)
37
38# Creating, killing RooArgSets
39# -------------------------------------------------------
40
41# A ROOT.RooArgSet is a set of RooAbsArg objects. Each object in the set must have
42# a unique name
43
44# Set constructors exists with up to 9 initial arguments
45s = ROOT.RooArgSet(a, b)
46
47# At any time objects can be added with add()
48s.add(e)
49
50# Add up to 9 additional arguments in one call
51# s.add(ROOT.RooArgSet(c, d))
52s.add(c)
53s.add(d)
54
55# Sets can contain any type of RooAbsArg, pdf and functions
56s.add(g)
57
58# Remove element d
59s.remove(d)
60
61# Accessing RooArgSet contents
62# -------------------------------------------------------
63
64# You can look up objects by name
65aptr = s.find("a")
66
67# Construct a subset by name
68subset1 = s.selectByName("a,b,c")
69
70# Construct asubset by attribute
71subset2 = s.selectByAttrib("Constant", ROOT.kTRUE)
72
73# Construct the subset of overlapping contents with another set
74s1 = ROOT.RooArgSet(a, b, c)
75s2 = ROOT.RooArgSet(c, d, e)
76subset3 = s1.selectCommon(s2)
77
78# Owning RooArgSets
79# ---------------------------------
80
81# You can create a RooArgSet that owns copies of the objects instead of
82# referencing the originals. A set either owns all of its components or none,
83# so once addClone() is used, add() can no longer be used and will result in an
84# error message
85s3 = ROOT.RooArgSet()
86for arg in [a, b, c, d, e, g]:
87 s3.addClone(arg)
88
89# A clone of a owning set is non-owning and its
90# contents is owned by the originating owning set
91sclone = s3.Clone("sclone")
92
93# To make a clone of a set and its contents use
94# the snapshot method
95sclone2 = s3.snapshot()
96
97# If a set contains function objects, the head node
98# is cloned in a snapshot. To make a snapshot of all
99# servers of a function object do as follows. The result
100# of a RooArgSet snapshot with deepCloning option is a set
101# of cloned objects, all their clone (recursive) server
102# dependencies, together form a self-consistent
103# set that is free of external dependencies
104
105sclone3 = s3.snapshot(True)
106
107# Set printing
108# ------------------------
109
110# Inline printing only show list of names of contained objects
111print("sclone = ", sclone)
112
113# Plain print shows the same, by name of the set
114sclone.Print()
115
116# Standard printing shows one line for each item with the items name, name
117# and value
118sclone.Print("s")
119
120# Verbose printing adds each items arguments, and 'extras' as defined by
121# the object
122sclone.Print("v")
123
124# Using RooArgLists
125# ---------------------------------
126
127# List constructors exists with up to 9 initial arguments
128l = ROOT.RooArgList(a, b, c, d)
129
130# Lists have an explicit order and allow multiple arguments with the same
131# name
132l.add(ROOT.RooArgList(a, b, c, d))
133
134# Access by index is provided
135arg4 = l.at(4)