TTRee::SetEntrylist and TTree.__iter__

TTree::SetEntryList has no effect on the iteration of a TTree

t = TTree(....)
n = t.GetEntries()
t.SetEntryList(...)
n1 = t.GetEntries("")
k = 0
for i in t:
  k += 1
print n,n1,k

at the end k == n != n1

I think that the problem is in the strange internal representation of a TTrre that it’s not similar to an iterable object like a list.[/code]

Hi,

the TTree iteration happens over the entries, not over the entry list.

Cheers,
Wim

[quote=“wlav”]Hi,

the TTree iteration happens over the entries, not over the entry list.

Cheers,
Wim[/quote]

Yes, this is obvious, but I think this is not the best way and not what people want. If I want to iterate only the selected events I need to do something like this:

data = TTree(...)
entry = TEntryList(...)
for i in range(0,entry.GetN()):
         n = entry.Next()
         data.GetEntry(n)

I think that this solution is terrible. python is a very powerfull language, please use its power. In my opinion the best way to iterate over the selected events is using generators docs.python.org/tutorial/classes.html#generators

PyROOT does internally use a generator for TTrees. That’s what TTree.iter is, when you do “for event in tree”, you are calling that function which creates a generator. See TTree__iter_ in ROOT.py (the thing you import when you write “import ROOT”.)

You could write your own generator, if you wanted.

[code]def _TTree_MyIterator( tree ):
for i in xrange( tree.GetEntries() ):
tree.GetEntry( tree.GetEntryNumber( i ) )
yield tree
TTree.iter = _TTree_MyIterator # Maybe a bit hackish

for event in _TTree_MyIterator( tree ):
print tree.px

or even:

for event in tree:
print tree.px
[/code]