Define multiple histograms


I want to define multiple histograms in this way:

TH1* h_var_2D_D1_j1_k1[eta];
TH1* h_var_2D_D1_j1_k2[eta];
TH1* h_var_2D_D1_j1_k3[eta];
TH1* h_var_2D_D1_j1_k4[eta];
TH1* h_var_2D_D1_j1_k5[eta];
TH1* h_var_2D_D1_j1_k6[eta];
TH1* h_var_2D_D1_j1_k7[eta];
TH1* h_var_2D_D1_j1_k8[eta];
TH1* h_var_2D_D1_j1_k9[eta];
TH1* h_var_2D_D1_j1_k10[eta];
and so on …

And then in later stage I want to fill those histograms.
Is there a way to define those histograms using for loop? Basically to avoid writing those histograms multiple times.


What about a map? Maybe nested maps if you have an array of them for the eta values.

std::map<std::string, TH1*> th1s;
for(int j = 1; j < 10; j++){
  for(int k = 1; k < 11; k++){
    auto map_name = make_name(j, k, ...);
    th1s[map_name] = make_histo_pointer(j, k, ...);

Iterating through via…

for(auto th1_iter = th1s.begin(); th1_iter !=th1s.end(); ++th1_iter){
    //The histo name is...
    std::string histo_name = static_cast<std::string>(th1_iter->second->GetName());
    // The std::map name is in th1_iter->first, so you can access the histogram via...

If performance is really paramount, an integer key or different container type might be superior.

Hi Soumyadip,

or you can do it easier:

TH1F *h_var_2D_D1_j1[10][10];
for (int i=0; i<10; i++)
    for (int j=0; j<10; j++)
        h_var_2D_D1_j1[i][j] = new TH1F(Form("h_var_2D_D1_j1_%i_%i", i, j), Form("My histogram with k=%i and eta = %i", i, j), nbins, lowX, highX); 

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