pythontreeroot-frameworkpyroot

pyroot Ntuple handler


I wanted to create class to handle the TTree:

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()

But calling the function getEntry i the error occured:

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'

How can I forced python to remember the type of NtupleHandler.tree?


Solution

  • If you want to use ROOT classes in a Python environment you are better off using rootpy than pyroot. In rootpy the conversion of ROOT files containing trees into HDF5 format with PyTables is already done. Have a look and see if what you want is in rootpy.