Accessing folder in a histogram root file

Hi,

I am experiencing problems accessing my histograms which are written to a subfolder of my histogram root file. In ordinary ROOT I always just did something like this:

TFile *sfile=new TFile('histograms.root');
sfile->cd(rootdir);

and in pyroot I try

roottemp = TFile(histograms.root')
roottemp.cd(histodir)

But it seems like I am not able to enter the folder, as I cannot see the histograms, and get the error message:

It seems strange that I cannot just enter a directory by using .cd(), maybe I have misunderstood what seems to be the problem?

Thanks
Maiken

Maiken,

sorry, but I can’t reproduce this problem: TFile.cd() works fine for me …

The error message also states not that there’s a problem with .cd() but that there’s a python exception (and if from the .cd(), then the only thing I can think of is a translated CINT error at that point) and that that is handled incorrectly by the excepthook.

However, the line 248 that is reported results from a CLI call of the kind “.x somefile.py” and that could then only happen if the .cd() would raise a SyntaxError of that kind. That doesn’t add up, and then it should still be something that in addition causes an exception. If it did, then the line coming after what you quoted is the important one, and beyond that should be a printout of the original exception.

Cheers,
Wim

Thank you very much for looking into it. It was very strange that cd should give such an error.

Point is, though, if I try

rootfile.cd("mydirectory')
rootfile.GetListOfKeys().Print()

I still get the keys from the folder above, so I do still not manage to enter the folder. I can easily enter it opening the root file on the command line in cint doing it like this, but it wont work in my pyroot script for some reason.

I have had similar problems before, (at least for me it sounds similar, that for instance after opening a histogram, my script would not see it after leaving that specific function anymore, some python specifiic ownership stuff I believe). I do not get any error-message when trying to cd into the folder, but if I then do as above, try to print out they keys, the folder is forgotten it seems.

Thank you
Maiken

Maiken,

without an example, there’s little else I can do then speculate. Another possibility is that a python exception is set, then not reported. It will then “linger” until the next time errors are checked, which is then an odd place. Again, the only thing that I can think of to cause that is a translated CINT error.

Any way I can access the actual root file to try it out?

Cheers,
Wim

Hi,

I have now packed down all the histograms and files that my plotting script uses, and it is available here:
fys.uio.no/~maikenp/forhypernews/

if you get hold of the .tgz file here and untar-uncompress it, it will give you the folder structure I use for plotting.

To run the plotter script should be out of the box, I usually do
pyroot -i
.x myPlotter2.py

Then a series of plots should appear. Hopefully it will reconstruct my problem, as I am not able to plot anything since I do not manage to enter the folder within the histograms.

Thank you very much again for looking into this.
Maiken

Maiken,

okay, now things start to make sense. As said above, the message looked like it came from a".x somefile.py" which is indeed what you’re doing. The problem is then indeed reported right after the part that you quoted.

Yes, there’s a problem with cd() and consequently the histogram lookup fails, but I get the same behavior when running in root.exe: the code works in both cases, but the found histogram is NULL (i.e. it isn’t actually found, but the name is clearly known). In the python script, that null variable is then accessed, resulting in an error. If the variable were to be accessed in root.exe, an error would occur as well.

Now, I’m not sure how this is supposed to work, so I searched for some examples and none of them call Get() on the TFile, but instead call it on gDirectory after the .cd().

Compare:root [0] TFile* f = new TFile("histofiles/hist_mc_J0_pythia_jetjet_1muon.root") root [1] f->cd("OL") (Bool_t)1 root [2] f->Get("h_2L_OL_MM_Dr") (class TObject*)0x0 root [3] gDirectory->Get("h_2L_OL_MM_Dr") (class TObject*)0x84d69d8 root [4]
as well as:[code]>>> f = TFile(“histofiles/hist_mc_J0_pythia_jetjet_1muon.root”)

f.cd(“OL”)
1

print f.Get(“h_2L_OL_MM_Dr”)
None

print gDirectory.Get(“h_2L_OL_MM_Dr”)
<ROOT.TH1F object (“h_2L_OL_MM_Dr”) at 0x8a72600>

[/code]
In both cases the file Get() does not work, but the gDirectory one does.

Cheers,
Wim

Thanks a lot! At last it worked, only that I had to add the lines again

ht.Reset()
ht.SetDirectory(0)

which is a trick to get python to “own” the histograms or something, have not properly understood. But thank you very much. I have used file->Get for 4 years without problems using cint scripts, but I changed to using pyroot instead, and the problem showed up after I included folders in my root histogram files.

Thanks and have a very good evening
Maiken