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