# Creating multiple histograms in a loop with different names

**URL:** <https://root-forum.cern.ch/t/creating-multiple-histograms-in-a-loop-with-different-names/32852>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [February 22, 2019, 8:44am UTC](https://root-forum.cern.ch/t/creating-multiple-histograms-in-a-loop-with-different-names/32852 "2019-02-22T08:44:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jitrusko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jitrusko/32/19224_2.png) [@Jitrusko](https://root-forum.cern.ch/u/Jitrusko)\
**Post date:** [February 22, 2019, 8:44am UTC](https://root-forum.cern.ch/t/creating-multiple-histograms-in-a-loop-with-different-names/32852/1 "2019-02-22T08:44:27Z")

</div>

Hello rooters,  
beginner here.

My problem is: I have a root file with many histograms in it. Each histogram is called the same, but has diiferent row and column numbers: Energy\_column\_0\_row\_0, Energy\_column\_1\_row\_1, Energy\_column\_0\_row\_1 etc… there are 20 columns and 13 rows in total.

I am trying to make a loop to read all the histograms from file and save them with corresponding row and column numbers.  
How can I loop over the names so it stores histogram _Energy\_column\_0\_row\_1_ to _h\_old\_0\_1_ , etc??

So far I have this, but it doesn’t work for me, because later, when I want to call h\_old[column][row] it gives me error: warning: null passed to a callee that requires a non-null argument [-Wnonnull]

```auto
TFile* Energy = new TFile("Energy.root");
TH1D* h_old[19][12];
for (int row=0;row<13;row++)
     {
      for (int column=0; column<20; column++)
        {
		
		ss << "Energy_column_" << column << "_row_" << row;
		name = ss.str();

		h_old[column][row] = (TH1D*)Energy->Get(name.c_str());
		
        
        }
     }

```

I hope it is clear, what I am trying to do.  
Thanks,  
J.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 22, 2019, 8:56am UTC](https://root-forum.cern.ch/t/creating-multiple-histograms-in-a-loop-with-different-names/32852/2 "2019-02-22T08:56:02Z")

</div>

Try:

```auto
{
  TFile *Energy = TFile::Open("Energy.root");
  if ((!Energy) || (Energy->IsZombie()))
    { delete Energy; return; }; // just a precaution
  const int nrows = 13;
  const int ncolumns = 20;
  TH1D *h_old[ncolumns][nrows];
  for (int row = 0; row < nrows; row++) {
    for (int column = 0; column < ncolumns; column++) {
      TString ss = TString::Format("Energy_column_%d_row_%d", column, row);
      // std::cout << "Retrieving " << ss << std::endl;
      Energy->GetObject(ss, h_old[column][row]);
      if (!h_old[column][row])
	{ std::cout << "NO " << column << " , " << row << std::endl; }
    }
  }
}

```
