ROOT.std.vector with SetBranchAddress in python 3.6

ROOT Version: 6.12/06
Platform: macOS 10.13.4
Compiler: GCC 4.2.1

Dear experts,

I was attempting to use some previous code I have for looping over a TTree in pyROOT, by connecting branches and using “while tree.GetEntry(i)”, as was recommended performance-wise (a very long time ago) in Wim’s first response here. I’m finding that doing so in ROOT 6.12/06 with Python 3.6.5 no longer gives me the proper size of ROOT.std.vectors, similar to what was seen in this rootpy thread (though i’m not using rootpy). I’m having no such issue on lxplus with older ROOT 6.10/04 with Python 2.7.13.

When using SetBranchAddress, the vector size is a large random number (i.e. 18446708903992258992), and the entries are trash. When looping directly over the events with “for ev in tree:”, the size and contents of the vector are correct.

I’m attaching a test script and root file for reproducing the error. (476 Bytes)
testTreeForError.root (8.3 KB)

I’ll note i’m running a fresh install of root6 via macports. It’s difficult to test with older python versions locally; my understanding is that I would need to reinstall root6 with a new python version argument. If anyone knows a good way to use pyroot in both 2.7 and 3.6 with a single macports install, I’d be grateful.




Hi Jeff,

Not a solution from me for now but I can confirm that it does not fail with ROOT 6.12/06 and Python 2.7.12 (as you stated for ROOT 6.10/04 with Python 2.7.13).


No solution again, just a more precise reproducer using your ROOT file:

import ROOT

myFile = ROOT.TFile("testTreeForError.root")
tree = myFile.Get("treeName")

mode = "cpp"

if "py" in mode:
    for ev in tree:

if "cpp" in mode:
    pt = ROOT.std.vector('float')(1)  # make std.vector of length 1 to be sure!
    tree.SetBranchAddress('pt', pt)

    for i in range(tree.GetEntries()):
        print(pt.size(), pt[0])

This example even ends up in a segfault, which could lead us more easily to an idea what is going on.

@etejedor could you have a look, please?


I confirm I am able to reproduce both issues (@jdandoy1’s and @swunsch’s) on Linux, gcc62, ROOT 6.12/06 and only in Python 3. This looks indeed like a bug, I will check also with current master and open the corresponding JIRA ticket to follow it up.



This is now being followed here:

Hi @jdandoy1,

If in your code, you do:


instead of

tree.SetBranchAddress('pt', pt)

the size you obtain should be the right one.

Thanks @etejedor, I can confirm this solves my problem.


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