TChain error looping in python

Hi everyone!
I stuck upon weird behavior of TChain if I upload more than 1 file in it.
Here is small script which reproduces the problem for me:

from ROOT import TChain


def test1():
    tree = TChain("tree_name")
    tree.Add("file1.root")
    tree.Add("file2.root")

    for idx, event in enumerate(tree):
        if idx == 100:
            break
        print("Event: ", idx, event.variable[0])


def test2():
    tree = TChain("tree_name")
    tree.Add("file1.root")
    tree.Add("file2.root")

    for idx, event in enumerate(tree):
        if idx == 100:
            break
        print("Total number of events: ", tree.GetEntries())
        print("Event: ", idx, event.variable[0])

test1()
test2()

test1() - runs successfully
test2() - returns the error:

AttributeError: 'TChain' object has no attribute 'variable'

How does tree.GetEntries can influence that?

cheers and have a nice day!


ROOT Version: 6.18
Platform: Scientific Linux 7
Compiler: gcc 4.8.5
Python: 3.7.4


tree.GetEntries() for a chain moves the ‘cursor’ to the last file. The following might work better:

def test2():
    tree = TChain("tree_name")
    tree.Add("file1.root")
    tree.Add("file2.root")

    print("Total number of events: ", tree.GetEntries())
    for idx, event in enumerate(tree):
        if idx == 100:
            break
        print("Event: ", idx, event.variable[0])

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