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.
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).
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?
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.