Plotting difference between y and x values of TH2 histogram

Hello,

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.

  1. 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.
  2. 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.

Then when the difference is centered at zero here.

mapping_Crate8_0819_dR_0.001.pdf (15.8 KB)

mapping_Crate8_0819_dR_0.001.pdf (15.8 KB)

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.

-Charlie Clark

Do you have a small reproducer showing the problem ?

Hi @Charlie,

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

hist_23058032_6500002.root (3.0 MB)

mapping_edit.C (18.2 KB)

Hi,

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.

    MapCrates->GetZaxis()->SetRangeUser(Crate,Crate+1);
    MapCrate[iCrate] = (TH2*)MapCrates->Project3D("xy")->Clone();
    MapCrate[iCrate]->SetTitle(Form("Crate %i",Crate));
    MapCrate[iCrate]->GetXaxis()->SetTitle("Project-ID");
    MapCrate[iCrate]->GetYaxis()->SetTitle("Match-ID");
    MapCrate[iCrate]->GetZaxis()->SetRangeUser(0,90);

    DiffCrate[iCrate] = (TH1*)DiffCrates->ProjectionX(Form("Project-ID - Match-ID, Crate %i",Crate),CrateBin,CrateBin);
    DiffCrate[iCrate]->SetTitle(Form("Project-ID - Match-ID, Crate %i",Crate));
    DiffCrate[iCrate]->GetXaxis()->SetTitle("Project-ID-Match-ID");
    DiffCrate[iCrate]->GetXaxis()->SetRangeUser(-50,50);

    GausFitDiff[iCrate] = new TF1(Form("Gauss Fitting Crate %i",Crate),"[0]*TMath::Gaus(x,[1],[2])",-4,4);
    GausFitDiff[iCrate]->SetParameter(0,DiffCrate[iCrate]->GetMaximum());
    GausFitDiff[iCrate]->SetParameter(1,DiffCrate[iCrate]->GetMean());
    GausFitDiff[iCrate]->SetParameter(2,DiffCrate[iCrate]->GetRMS());
    GausFitDiff[iCrate]->SetParNames("Constant","#mu","#sigma");
    DiffCrate[iCrate]->Fit(GausFitDiff[iCrate],"RQL0");

I hope this helps.

-Charlie Clark

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