Declaring histograms in a loop

Dear Rooters,

I do simulations with Geant4 and use Root for data storing and analysis.
I need to declare over 200 histograms to store data from the same amount of simulated subdetectors.
Each one must be adressable and accessible (fillable) at any time during the simulation.
Doing this by hand is not very elegant but annoying so I try to do it with a loop.

Question1:
Is there any way to create a variable name using the loop count information?
I mean a construction like this:

[code]for(int histogramIT=1; histogramIT<=200; histogramIT+=1){
ostringstream histogramNameStream;
histogramNameStream << “hDetector” << histogramIT;

TH1F* VariableName(histogramIT) = new TH1F( histogramNameStream.str().c_str() , "" , 1000. , 0. , 1000.));

}[/code]

Question2:
If there is no way doing it like in Question1, I thought I could create the histograms and store them in a TObjArray. But this does not seem to work, because when I try to get one of the TH1F objects out of the TObjArray I get the error “illegal pointer to class object” what means to me there is no Object.
The Code is like this:

[code]void testRoot(){
TObjArray histogramarray(200);
TH1F* Histo;

for(int histogramIT=0; histogramIT<200; histogramIT+=1){
ostringstream histogramNameStream;
histogramNameStream << “hDetector” << histogramIT;

 Histo = new TH1F( histogramNameStream.str().c_str() , "" , 1000 , 0. , 1000));
 histogramarray[histogramIT]= Histo;

}

for(int histogramIT=0; histogramIT<200; histogramIT+=1){
Histo = (TH1F*) histogramarray[histogramIT];
Histo->Fill(histogramIT);
histogramarray[histogramIT]= Histo;
}
}[/code]

Executing this gives as exact error message:

Why isn’t it working?

Regards
Martin

Concerning your question 1, I think you can define a table of TH1F before your loop. Thus, you will have somthing like

[code]TH1F *myhisto[200] = { NULL };

for(int histogramIT=1; histogramIT<=200; histogramIT+=1){
ostringstream histogramNameStream;
histogramNameStream << “hDetector” << histogramIT;

myhisto[histogramIT] = new TH1F( histogramNameStream.str().c_str() , "" , 1000. , 0. , 1000.));

}[/code]

Ok, that is a nice way to create a list, but how do I access the TH1F objects?

and

Histo = (TH1F*) myhisto[histogramIT]; Histo->Fill(something); myhisto[histogramIT]= Histo;

are both returning the known error “illegal pointer to class object”.

Probably because your first loop starts at 1 whereas the second one starts at 0. The first case of the table contains NULL value.
HEre, the following code works fine

[code]{
TH1F *myhisto[200] = { NULL };

for(int histogramIT=1; histogramIT<=200; histogramIT+=1){
ostringstream histogramNameStream;
histogramNameStream << “hDetector” << histogramIT;

myhisto[histogramIT] = new TH1F( histogramNameStream.str().c_str() , "" , 1000 , 0. , 1000.);

}

for(int histogramIT=1; histogramIT<200; histogramIT+=1){
myhisto[histogramIT]->Fill(histogramIT);
}
}[/code]

I should have seen that :blush: .
Thanks for your Help!

Hello,

I think your table index should go from 0 to 199 and not from 1 to 200.

[code]{
TH1F *myhisto[200] = { NULL };

for(int histogramIT = 0; histogramIT < 200; histogramIT++)
{
     ostringstream histogramNameStream;
     histogramNameStream << "hDetector" << histogramIT;
     
    myhisto[histogramIT] = new TH1F( histogramNameStream.str().c_str() , "" , 1000 , 0. , 1000.);
  }

 for(int histogramIT = 0; histogramIT < 200; histogramIT++)
 {
   myhisto[histogramIT]->Fill(histogramIT);
 }

}
[/code]