# TH3D loop through consecutive bins

Hi,

I have a TH3D histogram that has 160 bins each axis which are 16cm.

Then a 3D bin should be 1mm^3.

What is the standard way to loop through the consecutive bins along a line for example the x axis?

I don’t understand how the bin numbering works in 3D.

Thanks,
Tibor

Hi Tibor,

supposing you want to loop over the bins on the x axis along the direction identified by the “yBinNum” and “zBinNum” bin numbers along the y and z axis, what you can do is:

``````auto initialBinNumber =  my3dHist.GetBin(1,xBinNum,yBinNum)
for (int i=0;i<160;i++){
std::cout << my3dHist.GetBinCOntent(initialBinNumber+i)
}``````

Cheers,
Danilo

Hi Danilo,

Thanks!

Does the code below result in printing the contents of all the bins on the x axis?

How would I do the same on the y axis?

``````import ROOT

f = ROOT.TFile("Dosimetry_Detector_tot.root")
hist3D=f.Get("h3D_Dose_t")
f.Print()

binx=hist3D.GetNbinsX()
biny=hist3D.GetNbinsY()
binz=hist3D.GetNbinsZ()
minx=hist3D.GetXaxis().GetXmin()
miny=hist3D.GetYaxis().GetXmin()
minz=hist3D.GetZaxis().GetXmin()
maxx=hist3D.GetXaxis().GetXmax()
maxy=hist3D.GetYaxis().GetXmax()
maxz=hist3D.GetZaxis().GetXmax()

initialBin=hist3D.GetBin(int(minx),int(miny),int(minz))

for i in xrange(0,binx):
print hist3D.GetBinContent(initialBin+i)``````

Ok, got it now!

Thanks!