Reading an array of histograms from a file

Hi,

I could use some pointers to read an array of histograms from a file. I wrote them out using the commands hist[j]->Write(); When I look at the file using TBrowser, the names have the form hist;1, hist;2, but that may be the way that I created them. My code to read them in is

for (int j=0;j<numhists;j++) {
   TString xa=TString::Format("dstempright;%d",j+1);
   cout<<"File names to read:"<<xa<<endl;
   dsdtright[j] = (TH1F*)histfile->Get(xa);
   TH1F* tempd = (TH1F*)histfile->Get(xa); *** Alternate version***
   dsdtright[j]=tempd->Clone();
   // dsdtright[j]=tempd;
 }

I have tried a number of approaches to read in the histograms. The TH1F* tempd histogram is filled as expected, but I have not been able to either read the histogram into dsdtright[j], or to transfer the information from tempd to dsdtright[j], using either an equals sign or tempd-> Clone()

When I try dsdtright[j] = (TH1F*)histfile->Get(xa); I get a segmentation violation.

When I try dsdtright[j]=tempd->Clone(); the error message is: error: assigning to ‘TH1F *’ from incompatible type ‘TObject *’

I’d appreciate it if someone could tell me how to make this work. More generally, a tutorial on histogram arrays would be really useful. This seems like a fairly common technique, but there seem to be many subtleties.

Thanks.
Spencer Klein


_ROOT Version:6.06/08
_Platform:Mac High Sierra 10.13.6
Compiler: Not Provided


I assume 'histfile is a pointer to a TFile and dsdtright is an array of TH1F*. The Get method takes a const char* as parameter.
I guess you should do:

dsdtright[j] = (TH1F*)histfile->Get(xa.Data());

To help you further we would need a small reproducer example showing the problem.

Oliver:

‘histfile’ is indeed a pointer to a TFile. I believe that Get(xa) is not my problem, since if I replace this line with

TH1F* tempd = (TH1F*)histfile->Get(xa);

i. e. copying the on-disk histogram into a histogram that is not part of an array, things work OK. The problem only comes when I try to put things into an array of histograms. A small reproducer program is:

{/* Test for histogram copying into an array */
gROOT->Reset();

TH1F* dsdtright[12];
TH1F* tempd = new TF1*(“tempd”,“title”,100,0.,1.);
for (int i=0;i<12;i++) {
tempd->FillRandom(“gaus”,1000);
dsdtright[j]=tempd->Clone();
}
dsdtright[3]->Draw();
}

I can’t find the right syntax for dsdtright[j]=tempd->Clone();
-Spencer

Clone returns a TObject*, so try this:

 dsdtright[j] = (TH1F*)tempd->Clone();

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