Projections of a 2D hist are too different

Hello Root family,

I am doing a projection of a 2D Hist. And I want to project in the Y axis, but select a region in X (-2,-1.5) and (1.5, 2)

double xMin1 = -2;
double xMax1 = -1.5;
double xMin2 = 1.5;
double xMax2 = 2;
TH1D* h1_y2 = h2_div2->ProjectionY("h1_y2", h2_div2->GetXaxis()->FindBin(xMin1), h2_div2->GetXaxis()->FindBin(xMax1));
TH1D* projY1_l2 = h2_div2->ProjectionY("projY1_l2", h2_div2->GetXaxis()->FindBin(xMin2), h2_div2->GetXaxis()->FindBin(xMax2));

TCanvas *canvas9 = new TCanvas("canvas9", "canvas9", 800, 700);
h1_y2->SetStats(0);
h1_y2->Draw();

TCanvas *canvas10 = new TCanvas("canvas10", "canvas10", 800, 700);
projY1_l2->SetStats(0);
projY1_l2->Draw();
    
    

My question is why do the 2 projections have very different values? I was expecting similar values. Someone knows what is it happening? or if I am doing something wrong.
h2_div2 is the 2D histogram.



Thank you for your time

If the x-axis bins of h2_div2 go exactly from -2 to 2, then in the second case (projY1_l2) you are including the overflow bins of h2_div2 when you do h2_div2->GetXaxis()->FindBin(xMax2)) having xMax2=2, and apparently there is a large overflow.
Try with a slightly lower value for xMax2 (but still within the last bin, maybe 1.99… depending on the binning you have).

root [0] TH1D *h=new TH1D("h","h",4,0,4);
root [1] h->GetXaxis()->FindBin(0.)
(int) 1
root [2] h->GetXaxis()->FindBin(4.)
(int) 5

(bin = Nbins+1, or 5 here, is the overflow; bin = 0 is underflow)

I tried reducing the value xMax2 but the difference is still of 2 orders of magnitude, so I checked another case between (-1.6, -1.5) and (1.5, 1.6), and the difference is still 2 orders of magnitude. Am I doing something wrong? or could be a problem with the data?

Can you post a minimal reproducer (and the ‘source’ data or histogram/s) that we can run?

Something weird happened.

In my original code, I have 2 hist, and I divide 1 with another one to obtain h2_div2.
And then when I do the projection it gave me the values that I showed in the image.
But if I save the h2_div2 in a new root file, and then opened in a new code, the projections now have similar values (they are in the same order of magnitude and the values are close). Do you know why is this happening?

Also, I do not know how to check if the values of the projection are right, is there a way that looking the 2d plot and the projection plot you can say that those values are correct?

For your first question as @dastudillo has already mentioned if you could send a minimal working script then it’d be much easier to help you out.
Coming back to second question, what I think you can do is take integral of your projected histogram and check weather if it’s matching with sum of all bin contents from the same region. Below I’m providing a working script to help you understand better what I actually meant.

   TCanvas *c1 = new TCanvas("c1","c1",1000, 571);
   gStyle->SetOptStat(0);

   TPad *rightPad = new TPad("rightPad","rightPad",0.0,0.0,0.5,1.0);
   rightPad->Draw();

   TPad *leftPad = new TPad("leftPad","leftPad",0.5,0.0,1.0,1.0);
   leftPad->Draw();

   TH2F *h2 = new TH2F("h2","",40,-4,4,40,-5,5);

   // Filling up hist
   Float_t x,y,z;
   Int_t nentries = 10000;

   gRandom->SetSeed();

   for (Int_t i=0;i<nentries;i++) {
      gRandom->Rannor(x,y);
      z = x*x+y*y;
      h2->Fill(x,y,z);
   }

   int x_1 = 3;
   int x_2 = 4;

   // Taking Projection
   TH1D *spy = h2->ProjectionY("spy", h2->GetXaxis()->FindBin(x_1), h2->GetXaxis()->FindBin(x_2));

   // Finding Integral/area of projected histogram
   double sum = spy->Integral(0, 40);
   cout << "Integral is: " << sum << endl;

   // Drawing histograms
   rightPad->cd();
   gStyle->SetPalette(1);
   h2->Draw("COLZ");

   leftPad->cd();
   spy->SetFillColor(kBlue+2);
   spy->Draw("E1 BAR");

   // Finding if porjected histogram area is actually equals to sum of all bin contents or not
   double a = 0.0;

   for (int i = h2->GetXaxis()->FindBin(x_1); i < h2->GetXaxis()->FindBin(x_2); ++i)
   {
      for (int j = h2->GetYaxis()->FindBin(-5); j < h2->GetYaxis()->FindBin(5); ++j)
      {
         // cout << "Bin numbers are: " << i << ", " << j << endl;
         double b = h2->GetBinContent(i, j);
         // cout << "Bin content of bin number " << i << " & " << j << " is: " << b << endl;
         a = a + b;
         // cout << a << endl;
      }
   }

   cout << "sum of all the bin content is: " << a << endl;

as you will see after running the code that value of integral is same as sum of all bin contents from same region. If you need any help in explanation or understanding code you can always come back and ask here and I’m not sure if this is the best way or effective way so it’s open for discussion. Hope it helps.

Cheers,
Shiva

Thanks to both of you, I found the mistake in my code, and also the detail of the overflow helps a lot.

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