Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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)
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# You can create a RooArgSet that owns copies of the objects instead of
84# referencing the originals. A set either owns all of its components or none,
85# so once addClone() is used, add() can no longer be used and will result in an
86# error message
87s3 = ROOT.RooArgSet()
88for arg in [a, b, c, d, e, g]:
89 s3.addClone(arg)
90
91# A clone of a owning set is non-owning and its
92# contents is owned by the originating owning set
93sclone = s3.Clone("sclone")
94
95# To make a clone of a set and its contents use
96# the snapshot method
97sclone2 = s3.snapshot()
98
99# If a set contains function objects, the head node
100# is cloned in a snapshot. To make a snapshot of all
101# servers of a function object do as follows. The result
102# of a RooArgSet snapshot with deepCloning option is a set
103# of cloned objects, all their clone (recursive) server
104# dependencies, together form a self-consistent
105# set that is free of external dependencies
106
107sclone3 = s3.snapshot(True)
108
109# Set printing
110# ------------------------
111
112# Inline printing only show list of names of contained objects
113print("sclone = ", sclone)
114
115# Plain print shows the same, by name of the set
117
118# Standard printing shows one line for each item with the items name, name
119# and value
120sclone.Print("s")
121
122# Verbose printing adds each items arguments, and 'extras' as defined by
123# the object
124sclone.Print("v")
125
126# Using RooArgLists
127# ---------------------------------
128
129# List constructors exists with up to 9 initial arguments
130l = ROOT.RooArgList(a, b, c, d)
131
132# Lists have an explicit order and allow multiple arguments with the same
133# name
134l.add(ROOT.RooArgList(a, b, c, d))
135
136# Access by index is provided
137arg4 = l.at(4)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.