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:
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?
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?
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.
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.