How to take square root of TH2 histogram bin by bin?


I have to find error of a TH2 histogram bin by bin, and put them in a new histogram. Is there any function that allows me to do so ?


If h2 is your histogram, I would:

  1. book a new empty histogram h2n with the same number of bins and axis limits as h2.
  2. do the following loop:
    for (all the bins in h2) {
    e = h2->GetBinError(i,j)

This loop works but only when the bin numbering is the same as axis points (in other words, when the bin size is of value 1 and when it starts with 1 and ends with maxmum bin number).

What you do in this loop is taking error of bin(i,j), e, and filling this e value in a new histogram with (x,y) value of e.

In my case, the x axis is -180 to +180 and total bins are 72 and resulting picture shows as attachment (original top panel and sqrt on bottom panel).

I just figured out through manual, replacing “Fill” with “SetBinContent” solves the problem.


Yes sure. I meant that… sorry. … anyway you got the idea with the pseudo code I sent you :slight_smile: