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