I have a TH2 histogram and I want to take the difference between the y values and the x values in that histogram. I can plot the TH2 histogram and it shows a linear relationship between y and x. However, when I take the difference and plot it in a TH1, I do not get a distribution centered at 0. The plot seems to shift the points in the positive x direction of the difference plot. I made the theses plot in two ways.

The TH2 and the difference plots and made together where I fill the TH2 like fill(x,y) and I fill the difference plot like fill(y-x). I save the histograms to a root file and extract the histograms from there.

I just make the TH2 and save them to a root file. I extract the TH2s from there and take the difference by getting the bin centers of each x and y bin of the TH2.

Both methods show that the difference is not centered at zero. I can try to explain reason for making these plots, but that will take some time. In short, I’m comparing two methods of reading an events location in a particle physics detector. I will show an example of when I don’t get a distribution at zero here.

The difference between the set of pictures is that the first set was taken from a projection of a TH3 histogram that holds TH2 histograms of the event location in different areas of the detector called crates. The second set of pictures was just plotting a TH2 of one crate. If there are any questions about this please let me know. I hope that someone can help me with this. Hope to hear a response soon. Thanks in advance.

as said by couet sharing a small reproducer will help us to understand better the problem.

Looking at the stats of your histogram the difference is centered roughly at 0.5.

From the picture it seems to me that you have 20 bins from -10 to 10.
Even if the difference between all your x and y will be exactly 0 the plotted histogram will look the same.
Using a finer binning will also help to see better the shape of the distribution

I believe this root file has those TH2 histograms stored in a TH3 and TH1 difference plots stored in a TH2. I have provided my code to make make histograms for all 30 areas in the detector. This root file has one version of the TH3 called “Map__projectid_matchid_crate” and one version of the TH2 called “Diff_projectid_matchid_crate”. However, in my code I have different versions of that TH3 because I’m changing a value for each TH3 called dR. So refer to this area of the code for those histograms that I’m plot the projections of.