Getting a vector from TTree by its branch name


This is probably a stupid question but it is taking me way too long to figure out.

I wanted to access the contents of my TTree in python using the name of the branch rather than Tree.branchname.

This is because I am trying to convert all the branches into a different structure so I wanted to do
for branchname in branches:
and do slightly different things depending on the type of the branch

For floats I can do
branchDict[branchname] = Tree.GetBranch(branchname)
and then
if branchDict[branchname].GetListOfLeaves()[0].GetTypeName() == ‘Float_t’:
value = branchDict[branchname].GetListOfLeaves()[0].GetValue()

however for those branches std::vector , I need something slightly different.
seems to return a pointer but then I can’t seem to get the vector contents.

I’m sure there is some easy way to do this - so apologies(!)




you can bind the returned pointer value using ROOT.BindObject(), which takes the address and a class name that the pointer should be bound to.

However, what is wrong with getattr(Tree, branchname)? Or if the branchname is not a string variable but the name itself, Tree.branchname? Seems easier and has better support.


Ah yes, getattr is indeed perfect for that case.
And BindObject will no doubt be useful too…

Thanks !