Filling TTree in a pyrrot function

Dear ROOT experts,

I am trying to fill a TTree in a pyroot function.
I have found the nice recommendations of the page root.cern/doc/v628/classTTree.html
I tried the basic pyroot example and it works out of the box.

But, when trying to do this in a function, I am facing segmentation-violation crashes.
I tried by passing the tree as argument:

#! /usr/bin/env python
###############################################
import array
from ROOT import TTree
###############################################
def fill_tree(tree):
    n = array.array('f', [ 1.5 ])
    tree.Branch('floatb', n, 'floatb/F')
    tree.Fill()
    return
tree = TTree("tree", "tree")
fill_tree(tree)
tree.Print()
tree.Scan()

or considering the tree as the result of the function:

#! /usr/bin/env python
###############################################
import array
from ROOT import TTree
###############################################
def fill_tree():
    tree = TTree("tree", "tree")
    n = array.array('f', [ 1.5 ])
    tree.Branch('floatb', n, 'floatb/F')
    tree.Fill()
    return tree

t = fill_tree()
t.Print()
t.Scan()

Do I miss something?

Thanks for your help,

Benjamin

ROOT Version: 6.22.06
Platform: CC IN2P3


def fill_tree():
tree = TTree(“tree”, “tree”)
n = array.array(‘f’, [ 1.5 ])
tree.Branch(‘floatb’, n, ‘floatb/F’)
tree.Fill()
tree.ResetBranchAddresses() # To forget about n which is about to go out of ‘scope’ (alternatively, find a way to extend the lifetime of n to be the same as the lifetime of the tree
return tree

Thanks a lot for the tip. I should have thought of this.

It works perfectly.

Best regards

Benjamin

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.