NTuple Handler

Hello All,

I would like to create tree handler that could be used further in my analysis.

[code]from ROOT import *
from Exceptions import *
import os.path

class NTupleHandler:

def __init__(self, fileName, eventType):
    if not os.path.isfile(fileName):
        raise InputError( fileName)
    f = TFile(fileName, 'read')
    if f is None:
        raise InputError("openError"+fileName)
    dir=f.Get(eventType)
    if dir is None:
        raise InputError("directory Error"+eventType)
    tree=dir.Get('particle')
    self.tree=tree
    self.tree.GetEntriesFast()
    print

def getEntry(self):
    return self.tree.GetEntriesFast()[/code]

The output looks like this:

Error Traceback (most recent call last): File "/home/ja/PycharmProjects/studyChi2/python/NTupleHandlerTester.py", line 19, in testHandlerShouldReturnNoEvents self.assertLess(handler.getEntry(),10) File "/home/ja/PycharmProjects/studyChi2/python/NTupleHandler.py", line 23, in getEntry return self.tree.GetEntriesFast() AttributeError: 'PyROOT_NoneType' object has no attribute 'GetEntriesFast'

to summarize my issue. I would like to get from TFile TTree. After that I want to has access to the file via class member. But the problem is when I call the GetEntry() function the TTree losing it type. How can I fix it?

Best Regards
A.Dendek

Hi,

you have to keep the file open (e.g. by making it a data member of “NTupleHandler”). Right now, it closes at the end of init, as to local variable ‘f’ is the only outstanding reference, which goes away at that point, taking the TTree with it.

HTH,
Wim

I made so silly mistake :blush:
Thank you a lot.