I’ve been happily using root_numpy.array2tree()
to convert a numpy array of floats to a TTree
. But now I need to also write TH1F
to a branch of the tree. rootpy
seems promising, and there are several useful examples of creating TTree
s here:
But I’ve not been able to include a TH1F as a tree branch.
Here’s what I’ve tried so far:
import numpy as np
import ROOT
from rootpy.io import root_open
from rootpy.tree import Tree
h1 = ROOT.TH1F("h1","some title",100, -3, 3)
fout = root_open('output.root', 'recreate')
tout = Tree('mytree')
tout.create_branches(
{'vmin': 'F',
'h1': 'TH1F',
})
nevt = 10
for ievt in range(nevt):
tout.vmin = np.random.uniform()
tout.h1.FillRandom("gaus", int(1e4))
tout.fill()
tout.write()
fout.close()
Which gives error:
Traceback (most recent call last):
File "test_rootpy.py", line 14, in <module>
'h1': 'TH1F',
File "/Users/jbattat/Library/Python/2.7/lib/python/site-packages/rootpy/tree/tree.py", line 138, in create_branches
branches = TreeBuffer(branches)
File "/Users/jbattat/Library/Python/2.7/lib/python/site-packages/rootpy/tree/treebuffer.py", line 47, in __init__
self.__process(branches)
File "/Users/jbattat/Library/Python/2.7/lib/python/site-packages/rootpy/tree/treebuffer.py", line 100, in __process
obj = cls()
File "/Users/jbattat/Library/Python/2.7/lib/python/site-packages/rootpy/plotting/hist.py", line 2136, in __init__
params = self._parse_args(args)
File "/Users/jbattat/Library/Python/2.7/lib/python/site-packages/rootpy/plotting/hist.py", line 323, in _parse_args
raise TypeError("did not receive expected number of arguments")
TypeError: did not receive expected number of arguments
So I’m clearly not using create_branches()
correctly, but have not found documentation about how to create a TH1 branch.
Also, I gather that I could do this in pyROOT
with:
H = TH1F()
tree = TTree("test", "Test histogram tree")
tree.Branch("hist_branch", "TH1F", H)
but was hoping to use rootpy
to avoid (hide?) things like this:
px = array('d',[0])
tree.Branch("px", px, 'normal/D')
for i in xrange(10000):
px[0] = np.random.uniform()
ROOT Version: 6.14
Platform: Mac OSX 10.12 (Sierra)
Compiler: Not Provided