# Having to loop beyond GetNbinsX/Y/Z for TH1/TH2

Hi,

I was trying to loop through a 3D histogram and fill the no. of entries that fits my requirement to a 2D histogram (but defined as TH1* because it was from the Project3D(“yx”) method).
But while I was looping through it, I have to go 2 bins beyond the number of bins I got via GetNbinsX(/Y/Z), otherwise some entries are lost.

I’m wondering why, and whether this would cause any problems?
Code:

``````TH1* hEtaptb= h3D->Project3D("yx");
TH1* effMapAfter=hEtaptb; //copy the hist
effMapAfter->Reset(); //make it a blank hist

int nbinsx = h3D->GetNbinsX();
int nbinsy = h3D->GetNbinsY();
int nbinsz = h3D->GetNbinsZ();

for (ptIt=0;ptIt<=nbinsx+1;ptIt++){
for (etaIt=0;etaIt<=nbinsy+1;etaIt++){
integrated=h3D->Integral(ptIt,ptIt,etaIt,etaIt,cutIndices[ptIt],nbinsz+1);
//cutIndices is an array consist of some bin numbers. Only want the entries with bin numbers above those numbers
effMapAfter->SetBinContent(ptIt,etaIt,integrated);
}
}
``````

My nbinsx, nbinsy and nbinsz are already supposed to be the total no. of bins, i.e. starting from 0th bin, I shouldn’t even have to include the "nbinsx"th bin. However I have to go beyond them (instead of “<nbinsx”, do “<=nbinsx+1”) to get all my entries. I suppose it has something to do with overflow bins? But I have never used overflow bins and don’t think the 3D hist has any (at least not added by me on purpose)…

Thanks for the help!
Best wishes

ROOT Version: 6.22/02
Platform: lxplus
Compiler: Not Provided

In ROOT, underflow bin’s number is `0`, overflow bin’s number is `Nbins + 1` and ordinary histogram bins are numbered from `1` to `Nbins` (inclusive).

1 Like

I see, so that’s completely different from an array or list. Thanks a lot for your explanation!

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