Recast an object in PyROOT

I have the situation

f = ROOT.TFile.Open(“file.root”)
h = f.Get(“histo”)

The problem is that h is not TH1 but TObject to Python:

AttributeError: ‘TObject’ object has no attribute ‘SetDirectory’

How to solve?

Hi @Viesturs,

I believe what is happening is that there is no object called “histo” inside “file.root”, so you get a null TObject instead of a TH1. Could you share file.root?


That is not correct.
I see the TH1F when calling and get “TH1F” from h.ClassName()

Could you please attach your file.root to this ticket so I can try to reproduce with your input data?

What version of ROOT and on which platform are you running?

There was an error that I fixed. In one case the retrieved object was indeed 0.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.