Accessing a list of histograms

I’m new to C++ and root and have what is probably an easy problem. I have an existing file with many histograms named blank_2, blank_3, blank_4, etc.

I’d like to access the histograms one at a time and use GetBinContent() to grab a quantity and then do a calculation. My problem is that I get errors when trying to create a pointer to a histogram. I used a loop and sprintf() more or less like this:


TFile f(“result.root”,“UPDATE”);
char finalname[8];
Int_t bin = 179;

for (Int_t i = 2; i < 11; i++) {
sprintf(finalname, “blank_%d”,i);
TH1D final = (TH1D)f.Get(finalname);
Float_t finalpeak = final->GetBinContent(bin);


I get an “illegal pointer to class object final” error at the last line. I’ve tried many variants on the last two lines and am stuck. Am I using the Get() command incorrectly? What is the proper way to do this? Any thoughts would be appreciated.
subtractMo95.C (660 Bytes)

Are you sure that your histograms are in this directory?
Why do you cd to “subtracted”, then change again to “final.root”, may be you meant
f.cd(“subtracted/final.root”);
simply use f.ls() to list objects in a given directory.

Rene

Yes, the histograms are there. The other cd() command was leftover from something else I had done. I changed the limits of my loop because it included files that didn’t exist, but still the same problem it seems, in the first iteration of the loop. I’ve attached my root file.
subtractMo95.C (659 Bytes)
result.root (302 KB)

Replace line

TH1D *final = (TH1D*)f.Get(finalname); by

TH1D *final = (TH1D*)gDirectory->Get(finalname); otherwise you fetch the histogram from the top level directory 'f"

Rene

Thank you, this makes complete sense and works.