pyROOT Branch handling/filling


Please read tips for efficient and successful posting and posting code

Please fill also the fields below. Note that root -b -q will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug from the ROOT prompt to pre-populate a topic.

ROOT Version: 6.32.06
Platform: x86_64
Compiler: gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0


Hello,

I need a few hints on how to define and fill branches in a TTree from pyRoot.
Sorry if this is quite elementary, but I have forgotten how to do it right, and didn’t find
my answer in the docs/demos. Maybe I should even not use branches but ntuples?
But would still like to understand how to do it right.

Anyway, here is a small reproducer of the error I am encountering

#!/usr/bin/python
import random
from ROOT import TFile, TTree
#
Nev = 1000
fname = 'test_ttree.root'
f = TFile(fname,'recreate')
#
t = TTree('myTree','uniform rn between -1.o and 1.0')
#
# define variables
#
x = 10.0 # arbitray vaue outside of random range
y = -1.0 # dto
#
# set up branches
#
xB = t.Branch('x',x,'x/D')
yB = t.Branch('y',y,'y/D')
for nev in range(Nev):
    x = random.uniform(-1.0,1.0)
    y = random.uniform( 0.0,1.0)
    xB.Fill()
    yB.Fill()
#
f.Write()
f.Close()

And here is the error I get

$ python test_TTree.py 
Traceback (most recent call last):
  File "/home/haimo/test_TTree.py", line 18, in <module>
    xB = t.Branch('x',x,'x/D')
         ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/root_6_32_06/lib/ROOT/_pythonization/_ttree.py", line 218, in _Branch
    res = self._OriginalBranch(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Template method resolution failed:
  none of the 8 overloaded methods succeeded. Full details:
  int TTree::Branch(TList* list, Int_t bufsize = 32000, Int_t splitlevel = 99) =>
    TypeError: could not convert argument 1
  TBranch* TTree::Branch(const char* name, void* address, const char* leaflist, Int_t bufsize = 32000) =>
    TypeError: could not convert argument 2
  TBranch* TTree::Branch(const char* name, char* address, const char* leaflist, Int_t bufsize = 32000) =>
    TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
  TBranch* TTree::Branch(const char* name, int address, const char* leaflist, Int_t bufsize = 32000) =>
    TypeError: could not convert argument 2 (int conversion expects an integer object)
  int TTree::Branch(TCollection* list, Int_t bufsize = 32000, Int_t splitlevel = 99, const char* name = "") =>
    TypeError: could not convert argument 1
  int TTree::Branch(const char* folder, Int_t bufsize = 32000, Int_t splitlevel = 99) =>
    TypeError: could not convert argument 2 (int conversion expects an integer object)
  TBranch* TTree::Branch(const char* name, const char* classname, void* addobj, Int_t bufsize = 32000, Int_t splitlevel = 99) =>
    TypeError: could not convert argument 2 (bad argument type for built-in operation)
  TBranch* TTree::Branch(const char* name, Longptr_t address, const char* leaflist, Int_t bufsize = 32000) =>
    TypeError: could not convert argument 2 (int/long conversion expects an integer object)
  Failed to instantiate "Branch(std::string,double,std::string)"

Search for pyroot and see the t.Branch… examples in the TTree doc:
https://root.cern/doc/master/classTTree.html

Thank you! But this is the C++ example, which indeed I had seen before.
What I needed was its python equivalent, as I couldn’t correctly guess how to
transliterate the C++ into pyROOT. If you know how to modify my non-working
example so that it actually works, that would help me a lot.

As I said, search for pyroot on that page, it shows an example for python.

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