5. Trees and Cuts


The following documentation is still under construction.

rootpy provides pythonized subclasses for ROOT’s TTrees and TCuts

5.1. Trees

ROOT’s TTree is subclassed in rootpy and additional API is introduced to ease their creation in Python:

from rootpy.tree import Tree
from rootpy.io import root_open
from random import gauss

f = root_open("test.root", "recreate")

tree = Tree("test")
    {'x': 'F',
     'y': 'F',
     'z': 'F',
     'i': 'I'})

for i in xrange(10000):
    tree.x = gauss(.5, 1.)
    tree.y = gauss(.3, 2.)
    tree.z = gauss(13., 42.)
    tree.i = i


TTree’s Draw method is overridden to support returning and styling the created histogram:

from rootpy.io import root_open

myfile = root_open('some_file.root')
mytree = myfile.treename
hist = mytree.Draw('x_expression:y_expression',
                   selection='10 < a < 20',

5.2. Tree Models

A more powerful way to create trees is by defining tree models. Easily create complex trees by simple class inheritance (inspired by PyTables):

from rootpy.tree import Tree, TreeModel, FloatCol, IntCol

class FourVect(TreeModel):
    eta = FloatCol(default=-1111.)
    phi = FloatCol(default=-1111.)
    pt = FloatCol()
    m = FloatCol()

class Tau(FourVect):
    numtrack = IntCol()

class Event(Tau.prefix('tau1_'),
    event_number = IntCol()
    run_number = IntCol()

# tree = Tree('data', model=Event)

Branches are constructed according to the requested model:

event_number -> IntCol()
run_number -> IntCol()
tau1_eta -> FloatCol(default=-1111.0)
tau1_m -> FloatCol()
tau1_numtrack -> IntCol()
tau1_phi -> FloatCol(default=-1111.0)
tau1_pt -> FloatCol()
tau2_eta -> FloatCol(default=-1111.0)
tau2_m -> FloatCol()
tau2_numtrack -> IntCol()
tau2_phi -> FloatCol(default=-1111.0)
tau2_pt -> FloatCol()

Support for default values, automatic STL dictionaries, and ROOT objects is included.

5.3. Tree Objects

Documentation coming soon.

5.4. Tree Chains and Queues

Documentation coming soon.

5.5. Cuts

The rootpy rootpy.tree.Cut class inherits from ROOT.TCut and implements logical operators so cuts can be easily combined:

from rootpy.tree import Cut

cut1 = Cut('a < 10')
cut2 = Cut('b % 2 == 0')

cut = cut1 & cut2

# expansion of ternary conditions
cut3 = Cut('10 < a < 20')

# easily combine cuts arbitrarily
cut = ((cut1 & cut2) | - cut3)

the output of which is:


5.6. Categories

rootpy introduces a new mechanism with rootpy.tree.Categories to ease the creation of cuts that describe non-overlapping categories.

>>> from rootpy.tree.categories import Categories
>>> categories = Categories.from_string('{a|1,2,3}x{b|4,5,6}')
>>> for cut in categories:
...     print cut