Filling histos into histo array via loop

Dear Rooters,

I’m using root Version 3.05/07 Linux SuSe 8.2

My Problem is the following:
I have a TH1F array histo[i][j] that is filled into a .root File. Afterwards I’m accessing the data File from an other routine. I want to include the histos into a new array histos2[i][j], so I want to loop over all the histos from the old file with

for(Int_t i=0;i<7;i++)
{
for(Int_t k=0;k<11;k++)
{
histo2[i][j] = (static_cast<TH1F*>(gDirectory->Get(Form(“histo%d%d”,i,k))));
}
}

I can access all the histos from the first file with the above structure (static_cast etc…), but I can not fill them into the histo2[i][j] array correctly. I also tried Clone() but it returned

Error: illegal pointer to class object det_mean_E_T_jetshape[i][j] 0x0 163

The way for accesing histos via loop as above was described in other posts here, but does not seem to work correctly in this case for some reason.

Any help would be gladly appreciated, also if there is a better or more elegant way to do this

cheers

Erik

If your histograms are already in memory, and you want to move them to a new directory (TDirectory *newdir), do
myhist->SetDirectory(newdir);
see:http://root.cern.ch/root/htmldoc/src/TH1.cxx.html#TH1:SetDirectory

If you have two files TFile *f1, *f2;
f2->cd(); //important to set the target directory first

for(Int_t i=0;i<7;i++)
{
for(Int_t k=0;k<11;k++)
{
histo2[i][j] = (static_cast<TH1F*>(f1->Get(Form(“histo%d%d”,i,k))));
}
}

Rene

Hello Rene,

I’m sorry, I think I did not emphasize my problem well enough in the first place.
The problem is that the histograms from the old file are not filled into the histogramm correctly. Whenever I try to access them via e.g. ->GetBinContent(i); it returns:

Error: illegal pointer to class object histo2[i][j] 0x0 163

and the same for .GetBinContent(i); .

So the problem really seems to be that the array is not filled correctly, no matter which current directory I set.

kind regards

Erik

As usual to make progress I need the shortest possible running script showing the problem.

Rene