Subtraction TH2F - Wrong result


my problem is: I need to subtract the content of two histograms but at some bins the result of this subtraction is completely wrong and I don’t know why. Here is the part of the code where I do this:

for(binx = 0; binx<20;binx++)
for(biny = 0;biny<60;biny++)
aux1[binx][biny] = h8 -> GetBinContent(binx,biny);
aux2[binx][biny] = h7 -> GetBinContent(binx,biny);
sigmaAUX[binx][biny] = sigmaAUX[binx][biny] + (aux2[binx][biny] - aux1[binx][biny])(aux2[binx][biny] - aux1[binx][biny]);

I want to calculate the standard deviation of every bin of the histogram. If anyone could help me, I would appreciate.


Did you intialise your array sigmaAUX before using it? If not, there could be any value in your array which you then add something to. I took the liberty to simplify your code slightly, this should evaluate to the same as before but without using the potentially uninitialised value.

Double_t aux1 = h8 -> GetBinContent(binx,biny);
Double_t aux2 = h7 -> GetBinContent(binx,biny);
sigmaAUX[binx][biny] = (aux2 - aux1) * (aux2 - aux1);

You know that histograms have a Add and Multiply method, right?

auto diff2 = (TH2D*) h7->Clone("diff2");
diff2->SetTitle("{(h7 - h8)}^{2}")
diff2->Add(h8, -1);

The diff2 histogram is afterwards a 2D histogram with the same binning as h7. For this to work correctly, h7, and h8 need to be of the same shape (in terms of binning), otherwise it will try to interpolate values.

1 Like


Yes, I did it in the begining of the code. I used your solution and the result is a little better but still not completely correct. Here is an example of what is going on: 0.000000 - 0.000000 = 14948650853597184.000000

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