schieti
September 19, 2007, 12:56pm
1
Dear all,
this seems like an awfully simple question but I just can’t find the answer: I want to process a 2D histogram bin by bin for which I need to know the bin boundaries at each bin. In 1D I do that with GetBinLowEdge(ix) and GetBinWidth(ix). What are the equivalents for 2D and 3D histograms? I.e. something like GetBinLowEdgeX(ix,iy), GetBinLowEdgeY(ix,iy) etc.
I guess I can reconstruct them from the overall histogram information, but there’s got to be a simpler way…?
Many thanks,
Thomas
brun
September 19, 2007, 1:03pm
2
h->GetXaxis()->GetBinLowEdge(i); //for 1,2 and 3-d
h->GetYaxis()->GetBinLowEdge(i); //for 2 and 3-d
h->GetZaxis()->GetBinLowEdge(i); //for 3-d
Rene
schieti
September 19, 2007, 2:30pm
3
Sorry Rene, my mind operates in a different rest frame than yours! I don’t get the expected results with your prescription. Maybe my interpretation of bin/global bin is incorrect. What’s wrong with the following example:
{
Int_t nBinX = 5;
Int_t nBinY = 5;
Double_t xMin = 0.;
Double_t xMax = 1.;
Double_t yMin = 0.;
Double_t yMax = 1.;
TH2D* hist = new TH2D("hist","hist", nBinX, xMin, xMax, nBinY, yMin, yMax);
hist->FillRandom("expo",10000);
for (Int_t ibx = 1; ibx <= nBinX; ibx++) {
for (Int_t iby = 1; iby <= nBinY; iby++) {
Double_t xBinMin = hist->GetXaxis()->GetBinLowEdge(hist->GetBin(ibx,iby));
Double_t xBinMax = hist->GetXaxis()->GetBinUpEdge(hist->GetBin(ibx,iby));
Double_t yBinMin = hist->GetYaxis()->GetBinLowEdge(hist->GetBin(ibx,iby));
Double_t yBinMax = hist->GetYaxis()->GetBinUpEdge(hist->GetBin(ibx,iby));
Double_t nBinCont = hist->GetBinContent(ibx,iby);
cout << " bin " << ibx << " " << iby << " (global bin: " << hist->GetBin(ibx,iby) << ")" << endl;
cout << " xBin: " << xBinMin << " - " << xBinMax;
cout << ", yBin: " << yBinMin << " - " << yBinMax;
cout << ", content: " << nBinCont << endl;
}
}
hist->Draw("box");
}
As output I would expect
bin 1 1 (global bin: 1)
xBin: 0.0 - 0.2, yBin: 0.0 - 0.2, content: whatever
bin 1 2 (global bin: 2)
xBin: 0.0 - 0.2, yBin: 0.2 - 0.4, content: whatever’
Instead, I get:
bin 1 1 (global bin: 8)
xBin: 1.4 - 1.6, yBin: 1.4 - 1.6, content: 271
bin 1 2 (global bin: 15)
xBin: 2.8 - 3, yBin: 2.8 - 3, content: 255
Thanks again,
Thomas
brun
September 19, 2007, 3:06pm
4
Simply do:
{
Int_t nBinX = 5;
Int_t nBinY = 5;
Double_t xMin = 0.;
Double_t xMax = 1.;
Double_t yMin = 0.;
Double_t yMax = 1.;
TH2D* hist = new TH2D("hist","hist", nBinX, xMin, xMax, nBinY, yMin, yMax);
hist->FillRandom("expo",10000);
for (Int_t ibx = 1; ibx <= nBinX; ibx++) {
for (Int_t iby = 1; iby <= nBinY; iby++) {
Double_t xBinMin = hist->GetXaxis()->GetBinLowEdge(ibx);
Double_t xBinMax = hist->GetXaxis()->GetBinUpEdge(ibx);
Double_t yBinMin = hist->GetYaxis()->GetBinLowEdge(iby);
Double_t yBinMax = hist->GetYaxis()->GetBinUpEdge(iby;
Double_t nBinCont = hist->GetBinContent(ibx,iby);
cout << " bin " << ibx << " " << iby << " (global bin: " << hist->GetBin(ibx,iby) << ")" << endl;
cout << " xBin: " << xBinMin << " - " << xBinMax;
cout << ", yBin: " << yBinMin << " - " << yBinMax;
cout << ", content: " << nBinCont << endl;
}
}
hist->Draw("box");
}
Rene
schieti
September 19, 2007, 3:12pm
5
D’oh!! Of course!! I should have tried that.
Thank you for your patience,
Thomas