# Looping over the bins of two 2D hists in a root file

**URL:** <https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169>\
**Category:** Newbie\
**Created:** [August 26, 2020, 7:54pm UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169 "2020-08-26T19:54:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [August 26, 2020, 7:54pm UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/1 "2020-08-26T19:54:28Z")

</div>

So I have a root file named _histfile.root_ that has two histograms named _myhist1_ and _myhist2_. So what I want to do now is loop over the bins of first histogram. Then, loop over the bins of the second histogram in the same order and add these two numbers on a bin-by-bin basis. I know the easier way is to probably just add these two histograms and then look at the bin contents. However, this is a practice exercise for me to understand how to loop over the bins of one histogram and then move on to the second histogram and loop over its bins again.

[histfile.root](https://root-forum.cern.ch/uploads/short-url/uXfQRo2w8nQY15ylBWsWf6sry8N.root) (4.4 KB)

_ROOT Version:_ 5.18  
_Platform:_ Ubuntu 18.04  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [August 27, 2020, 9:14am UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/2 "2020-08-27T09:14:46Z")

</div>

Hi,

if the histograms have identical binning, it’s as easy as:

```auto
for (auto x=0; ...) {
  for (auto y=0; ...) {
    histo1->GetBinContent(x, y);
    histo2->GetBinContent(x, y);
  }
}

```

Note that bin #0 is the underflow, bin #nBins+1 is the overflow bin (see TH1 docs I link below). Maybe you want to skip those.

If the binning is not identical, you need to use `histo...->GetXaxis()->FindBin(coordinate)` [link](https://root.cern.ch/doc/master/classTAxis.html#a677abd11ccfc67cded1722234ac2fbb0) to find the bin index on the x or y axis that corresponds to a certain x or y coordinate.

Helpful resources are:

- [TH1 documentation](https://root.cern.ch/doc/master/classTH1.html) for the functions of histograms
- [TAxis documentation](https://root.cern.ch/doc/master/classTAxis.html) for computing coordinates or indices on an axis

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [August 27, 2020, 1:32pm UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/3 "2020-08-27T13:32:17Z")

</div>

Hi @StephanH, thank you for your reply. In terms of the things inside the nested loop, I don’t think this is what I want. Perhaps, I phrased the question poorly. So let me ask here what I actually wanted to ask. Let’s say I have 4 histograms with identical binning. Let’s call them _hist1_, _hist2_, _hist3_, _hist4_. What I want to do is take the first hist, grab all of its bin contents using the

```auto
GetBinContent()

```

method. Then, let’s stay do some operations on those numbers. And only then, move on to the next hist until all the hists are completed. So my rough idea is to have a vector where those four hists are stored and then have a triple nested loop. But I am not sure if what I am thinking of makes sense.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [August 27, 2020, 3:28pm UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/4 "2020-08-27T15:28:53Z")

</div>

With identical binning, it’s much easier, because you can use single-value bin numbers. The code would look something like:

```auto
std::vector<double> bins(histo1->GetNcells());
for (auto histo : std::initializer_list<TH2*>{histo1, histo2, histo3, histo4}) {
  for (int bin = 0; bin < histo->GetNcells(); ++i) {
    const double binContent = histo->GetBinContent(i);
    ... do stuff with each bin, e.g.
    bins[bin] += binContent;
  }
}

```

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [August 30, 2020, 2:32pm UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/5 "2020-08-30T14:32:39Z")

</div>

Hi @StephanH, do you mind explaining what the part

```auto
histo1->GetNcells()

```

is supposed to do? Cause when I load the root file and try something like this:

```auto
Int_t x = histo1->GetNcells()

```

it gives me an error saying

> Error: Can’t call TH2D::GetNcells() in current scope (tmpfile):1:  
> Possible candidates are…  
> (in TH2D)  
> (in TH2)  
> (in TH1)  
> \*\*\* Interpreter error recovered \*\*\*

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [August 30, 2020, 9:12pm UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/6 "2020-08-30T21:12:37Z")

</div>

It literally gets the number of cells in the histogram. Every histogram (TH1x, TH2x, …) has it:  
[https://root.cern.ch/doc/master/classTH1.html#a588785f73495e46d76064496254347c7](https://root.cern.ch/doc/master/classTH1.html#a588785f73495e46d76064496254347c7)

If it doesn’t work, this indicates that you tried to call this method on something that’s not a histogram.

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [August 31, 2020, 1:25am UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/7 "2020-08-31T01:25:33Z")

</div>

The root file that I attached in the main question does have two histograms, and I even tried plotting them, which works. The GetNcells() method,however, gives the same error.

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [August 31, 2020, 1:51am UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/8 "2020-08-31T01:51:37Z")

</div>

And I also tried just drawing a 1D hist in ROOT and calling the function to see what happens. Still the same error.

 ![Screenshot from 2020-08-30 21-49-21](https://root-forum.cern.ch/uploads/default/original/3X/c/6/c6946166d1b0af2d5e09393f106b6878ee8d054d.png)

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [August 31, 2020, 7:52am UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/9 "2020-08-31T07:52:20Z")

</div>

Which ROOT version?

```auto
root [0] TH1D histo("x", "x", 10, 0, 10)
(TH1D &) Name: x Title: x NbinsX: 10
root [1] histo.GetN<Invoke Tab completion>
GetName
GetNbinsX
GetNbinsY
GetNbinsZ
GetNcells
GetNdivisions
GetNormFactor
root [1] histo.GetNcells()
(int) 12

```

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [August 31, 2020, 7:55am UTC](https://root-forum.cern.ch/t/looping-over-the-bins-of-two-2d-hists-in-a-root-file/41169/10 "2020-08-31T07:55:47Z")

</div>

Oh, I saw it now. 5.18 …

That’s probably more than 10 years old, so a lot might have changed in the mean time. You can probably try to do `histo->GetNbinsX() * histo->GetNbinsY() * histo->GetNbinsZ()` etc instead, but you have to manually keep track of how many dimensions the histogram has.
