Creating branches in python

Hi evrybody,
I am new with PyRoot and I’m having a issue with creating new branches. It seems that the simple:

import ROOT
t=ROOT.TTree(“name”,“title”)
a=0.
t.Branch(“some_name”,a,“some_name/F”)

doesn’t work. I get:

Traceback (most recent call last):
File “prova.py”, line 4, in
newBranch=t.Branch(“some_name”,a,“some_name/F”)
TypeError: none of the 9 overloaded methods succeeded. Full details:
[…]

I used to think that a variable in python was just like a kind of pointer, but obviously it’s not as simple as that :slight_smile:
What’s wrong with the script I wrote?

You have to play some games with Python arrays, even for single values. Try following these instructions:

http://wlav.web.cern.ch/wlav/pyroot/tpytree.html

Jean-François

[quote=“jfcaron”]You have to play some games with Python arrays, even for single values. Try following these instructions:

http://wlav.web.cern.ch/wlav/pyroot/tpytree.html

Jean-François[/quote]

Thank you very much! I didn’t expect it be so needlessly intricate, it’s odd that one has to set up a whole array for just one single number, but anyway things are working, now.

If this is of any help, rootpy (sitting on top of PyROOT) provides a more pythonic way of creating TTrees:

https://github.com/rootpy/rootpy

rootpy essentially hides the ugliness of using Python’s array.array everywhere for simple types and provides these type wrappers:

https://github.com/rootpy/rootpy/blob/master/rootpy/tree/treetypes.py

that all inherit from array.array but provide several pythonizations to allow treating the objects similarly to basic floats, ints, etc.

On top of this, rootpy implements a TreeModel concept that makes it easier to construct a Tree from a collection of branch types:

https://github.com/rootpy/rootpy/blob/master/examples/tree/model_simple.py

from rootpy.tree import Tree, TreeModel, FloatCol, IntCol
from rootpy.io import root_open
from random import gauss

# calls TFile.Open, but returns a File objects that subclasses TFile and adds many features:
f = root_open("test.root", "recreate")

# define the model
class Event(TreeModel):
    x = FloatCol()
    y = FloatCol()
    z = FloatCol()
    i = IntCol()

# all the SetBranchAddress/array.array ugliness is hidden away:
tree = Tree("test", model=Event)

# Tree in rootpy subclasses ROOT's TTree and adds a bunch of features to make it easier to use in Python.
# Similarly with the other subclasses in rootpy. It's still ROOT underneath :)

# fill the tree
for i in xrange(100):
    tree.x = gauss(.5, 1.)
    tree.y = gauss(.3, 2.)
    tree.z = gauss(13., 42.)
    tree.i = i
    tree.fill()
tree.write()
f.close()

You can also extend this TreeModel concept to construct complex event data models by defining a class hierarchy of simple models:

https://github.com/rootpy/rootpy/blob/master/examples/tree/model_hierarchy.py

I hope this helps! rootpy is a few years old now and covers a lot of ROOT (at least the heavily used portions of it). If you are going to write serious frameworks on top of PyROOT in Python then taking advantage of the pythonizations in rootpy should save you time and headaches. Of course we wouldn’t even be using ROOT in Python without Wim’s hard work on the PyROOT bindings, so thanks to him!

1 Like