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