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