rf508_listsetmanip

'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #508

RooArgSet and RooArgList tools and tricks

Author: Clemens Lange, Wouter Verkerke (C version)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, November 30, 2022 at 11:23 AM.

In [1]:
from __future__ import print_function
import ROOT
Welcome to JupyROOT 6.27/01

Create dummy objects

Create some variables

In [2]:
a = ROOT.RooRealVar("a", "a", 1, -10, 10)
b = ROOT.RooRealVar("b", "b", 2, -10, 10)
c = ROOT.RooRealVar("c", "c", 3, -10, 10)
d = ROOT.RooRealVar("d", "d", 4, -10, 10)
x = ROOT.RooRealVar("x", "x", 0, -10, 10)
c.setError(0.5)
a.setConstant()
b.setConstant()

Create a category

In [3]:
e = ROOT.RooCategory("e", "e")
e.defineType("sig")
e.defineType("bkg")
Out[3]:
False

Create a pdf

In [4]:
g = ROOT.RooGaussian("g", "g", x, a, b)
[#0] WARNING:InputArguments -- The parameter 'b' with range [-10, 10] of the RooGaussian 'g' exceeds the safe range of (0, inf). Advise to limit its range.

Creating, killing RooArgSets

A ROOT.RooArgSet is a set of RooAbsArg objects. Each object in the set must have a unique name

Set constructors exists with up to 9 initial arguments

In [5]:
s = ROOT.RooArgSet(a, b)

At any time objects can be added with add()

In [6]:
s.add(e)
Out[6]:
True

Add up to 9 additional arguments in one call s.add(ROOT.RooArgSet(c, d))

In [7]:
s.add(c)
s.add(d)
Out[7]:
True

Sets can contain any type of RooAbsArg, pdf and functions

In [8]:
s.add(g)
Out[8]:
True

Remove element d

In [9]:
s.remove(d)
Out[9]:
True

Accessing RooArgSet contents

You can look up objects by name

In [10]:
aptr = s.find("a")

Construct a subset by name

In [11]:
subset1 = s.selectByName("a,b,c")

Construct asubset by attribute

In [12]:
subset2 = s.selectByAttrib("Constant", ROOT.kTRUE)

Construct the subset of overlapping contents with another set

In [13]:
s1 = ROOT.RooArgSet(a, b, c)
s2 = ROOT.RooArgSet(c, d, e)
subset3 = s1.selectCommon(s2)

Owning RooArgSets

Create a RooArgSet that owns its components A set either owns all of its components or none, so once addOwned() is used, add() can no longer be used and will result in an error message

In [14]:
ac = a.clone("a")
bc = b.clone("b")
cc = c.clone("c")

s3 = ROOT.RooArgSet()

s3.addOwned(ROOT.RooArgSet(ac, bc, cc))

In [15]:
s3.addOwned(ac)
s3.addOwned(bc)
s3.addOwned(cc)

Another possibility is to add an owned clone of an object instead of the original s3.addClone(ROOT.RooArgSet(d, e, g))

In [16]:
s3.addClone(d)
s3.addClone(e)
s3.addClone(g)

A clone of a owning set is non-owning and its contents is owned by the originating owning set

In [17]:
sclone = s3.Clone("sclone")

To make a clone of a set and its contents use the snapshot method

In [18]:
sclone2 = s3.snapshot()

If a set contains function objects, the head node is cloned in a snapshot. To make a snapshot of all servers of a function object do as follows. The result of a RooArgSet snapshot with deepCloning option is a set of cloned objects, all their clone (recursive) server dependencies, together form a self-consistent set that is free of external dependencies

In [19]:
sclone3 = s3.snapshot(ROOT.kTRUE)

Set printing

Inline printing only show list of names of contained objects

In [20]:
print("sclone = ", sclone)
sclone =  { @0x95113c0, @0x95113c8, @0x95113d0, @0x95113d8, @0x95113e0, @0x95113e8 }

Plain print shows the same, by name of the set

In [21]:
sclone.Print()
RooArgSet::sclone = (a,b,c,d,e,g)

Standard printing shows one line for each item with the items name, name and value

In [22]:
sclone.Print("s")
  1) RooRealVar:: a = 1
  2) RooRealVar:: b = 2
  3) RooRealVar:: c = 3 +/- 0.5
  4) RooRealVar:: d = 4
  5) RooCategory:: e = sig(idx = 0)

  6) RooGaussian:: g = 0.882497

Verbose printing adds each items arguments, and 'extras' as defined by the object

In [23]:
sclone.Print("v")
  1) 0x9797b70 RooRealVar:: a = 1 C  L(-10 - 10)  "a"
  2) 0x28f0990 RooRealVar:: b = 2 C  L(-10 - 10)  "b"
  3) 0x8b5a380 RooRealVar:: c = 3 +/- 0.5  L(-10 - 10)  "c"
  4) 0x97fc580 RooRealVar:: d = 4  L(-10 - 10)  "d"
  5) 0x92b42a0 RooCategory:: e = sig(idx = 0)
  "e"
  6) 0x9708350 RooGaussian:: g[ x=x mean=a sigma=b ] = 0.882497  "g"

Using RooArgLists

List constructors exists with up to 9 initial arguments

In [24]:
l = ROOT.RooArgList(a, b, c, d)

Lists have an explicit order and allow multiple arguments with the same name

In [25]:
l.add(ROOT.RooArgList(a, b, c, d))
Out[25]:
True

Access by index is provided

In [26]:
arg4 = l.at(4)

Draw all canvases

In [27]:
from ROOT import gROOT 
gROOT.GetListOfCanvases().Draw()