Catching ROOT error messages

Dear all,

I have some PyROOT code which iterates over a TTree, reading in each event. For some events, also the read seems to succeed, an error message is produced:

R__unzip: error -5 in inflate (zlib)
Error in TBasket::ReadBasketBuffers: fNbytes = 22292787, fKeylen = 82, fObjlen = 25363988, noutot = 16777215, nout=0, nin=7536169, nbuf=8586773

I think that this might be an indication of file corruption, so I’d like to be able to catch this in PyROOT and stop processing the file. However, as it doesn’t throw an exception, I don’t think I can do this with a try/except block. Is there any way to do this, or do I have to accept that ROOT prints these errors directly to the console without any way to intercept them?


Hi James,

you could check the return value of TTree::GetEntry:


Dear Danilo,

That would make sense but I never call TTree::GetEntry directly. My code looks like this:

for event in tree :
do_stuff( event.myInterestingBranch )

and it’s only when the call to event.myInterestingBranch is made that I see this issue. Is your suggestion that I should add an additional call to GetEntry before this?


rootpy automatically turns ROOT errors into Python exceptions. See the docs here:

And the minimum you need to do is import rootpy at the beginning of your code and it will install the error handler. I’ve used this feature of rootpy many times to catch errors in my batch jobs (some being identical to the one you show) that I would have otherwise missed.

Get the latest rootpy on github:

With this error handler you can now “try: except:” around blocks of code that might cause an error on the ROOT side (such as opening a file that doesn’t exist for example).



I’m hesitant to change the behavior in ROOT5 (TBD in ROOT6, where the error handlers are different anyway), but checking (and raising) on GetEntry() returning -1 is something that should have been done.

Put into v5-34-00-patches.