# Dynamic TH1F

Dear Rooters,
I would like to create n TH1F histograms dynamically, ie

``````int n= 500;
for(int i=0; i<n; i++) {
TH1F *h_%d  = new TH1F ("h_%d" , "", 0, -0.5, (Double_t) n+0.5 ); //%d = i
// but not delete just yet as I will need to to fill them outside this loop . I just need to create n TH1Fs, giving TH1F pointer name as the index i, indicated above.
}``````

Thanks for the help

``````{
TH1F *h;
for (Int_t i=0; i<500; i++) {
h[i] = new TH1F(Form("h%d",i),"Histogram title",100,-4,4);
}
}``````

```{ const int n = 500; TH1F *h[n]; for(int i = 0; i < n; i++) { h[i] = new TH1F( TString::Format("h_%d", i), // name TString::Format("histogram %d", i), // title 100, -0.5, ((Double_t)n + 0.5) ); } }```

Thanks!

then calling them back via a loop?

``````{
const int n = 500;
TH1F *h[n];
for(int i = 0; i < n; i++) {
h[i] = new TH1F( TString::Format("h_%d", i), // name
TString::Format("histogram %d", i), // title
100, -0.5, ((Double_t)n + 0.5) );
}
//...fill the histograms here then draw

h_0->Draw();
for(int i = 1; i < n; i++) {
h_%d->Draw("same"); // how does one call the histogram?
}

}
``````

h[i]->Draw(“same”);

[code]// Note: save this source code as “trial.cxx”, then: root  .x trial.cxx

#include “TH1.h”
#include “TROOT.h”
#include “TDirectory.h”
#include “TObject.h”

void create(void) {
const int n = 500;
TH1F *h[n];
for(int i = 0; i < n; i++) {
h[i] = new TH1F( TString::Format(“h_%d”, i), // name
TString::Format(“histogram %d”, i), // title
100, -0.5, ((Double_t)n + 0.5) );
}
}

void print(void) {
int i = 0;
while (1) {
#if 1 /* 0 or 1 */
// gROOT->FindObject(…), gROOT->FindObjectAny(…),
// gDirectory->FindObject(…), gDirectory->FindObjectAny(…)
TH1F *h_x = ((TH1F )(gROOT->FindObject(TString::Format(“h_%d”, i++))));
#else /
0 or 1 */
// gROOT->GetObject(…), gDirectory->GetObject(…)
TH1F h_x; gROOT->GetObject(TString::Format(“h_%d”, i++), h_x);
#endif /
0 or 1 */
if (h_x) h_x->Print(); // histogram found
else break;
}
}

void trial(void) {
create();
print();
}[/code]