I have a 3d histogram that I can see from TBrowser that has entries in it. However when I try to print entries i get AttributeError: ‘TObject’ object has no attribute ‘GetEntries’.
The type of the object is:
<class cppyy.gbl.TObject at 0x644a380>
What else can I do to troubleshoot this?
Thank you very much in advance.
You may want to consider posting the code that you’re using to access the histogram.
The error message means that ROOT/C++/cling considers the object to be a TObject, not a TH3D (or TH3F). If you’re doing something like this:
auto myFile = TFile::Open(“myfile.root”);
auto myHist = myFile->Get(“myHist”);
Then myHist will be a TObject*, not a TH3D*. If my guess is right (and I’m making a few unjustified assumptions about your code here), what you may want to consider is:
auto myHist = (TH3D*)myFile->Get("myHist");
if ( myHist == nullptr ) {
// error; 'myHist' is either not in 'myFile' or it's not a TH3D*
}
else {
auto myEntries = myHist->GetEntries();
}
Thank you very much for your answer. Your guess is correct. I was doing something like this:
my_file = ROOT.TFile.Open(filename)
name = "myhist"
Myhist =my_file.Get(name)
I am using python do you know how I can do this in python?
auto myHist = (TH3D*)myFile->Get("myHist");
Based on my limited experience with Python and pyroot, the code you have should have worked. The Python-C++ bindings are usually pretty good about converting types.
However, when I look at my own Python example code, I see that I’ve done things slightly differently:
inputFile = ROOT.TFile("myFile.root")
tree = ROOT.gDirectory.Get( 'myTree' )
Perhaps gDirectory is more robust than a simple ROOT.TFile.Get().