Re: TProfile2D

From: Axel Naumann <>
Date: Tue, 15 Feb 2005 13:52:05 -0600

Hi Federico,

> Why "i", the bin number, does not go from 1 to 4 (=nbinsx*nbinsy) ?

It does, but you forgot that under- and overflow bins exist. So instead of two bins per dim, you end up with 4. See the doc of TH1::GetBin.

> Why only one bin results with a content > 0 while 2 are plotted?

You're using GetBin instead of FindBin. GetBin operates on bin numbers, FindBin on axis values. Your x and y are axis value.

> Is it possible to:
> -retrieve the total number of bins

That's TH1::GetNbinsX()*TH1::GetNbinsY()*TH1::GetNbinsZ() - which will contain the under- and overflow bins.

> -scan with some index over all the bins and get bincenter (in 2D),

This depends on the dimension, so you'll have to go via the axes, using e.g. TAxis::GetBinCenter(ybin).

> bins contents

For this you can either use
Int_t globalBinNo=TH1::GetBin(xbin,ybin,zbin); TH1::GetBinContent(globalBinNo)
or directly TH1::GetBinContent(xbin, ybin, zbin))

> bin entries etc...

Entries are only defined on a per-histogram basis. Per bin it's the bin content.

So the only fix your amcro needs is FindBin instead of GetBin. Corrected macro is attached.


  Int_t nbinsx=2;
  Double_t xmin=-4;
  Double_t xmax=4;

  Int_t nbinsy=2;
  Double_t ymin=-4;
  Double_t ymax=4;

  TCanvas *c1 = new TCanvas("c1","Profile histogram example",200,10,700,500);
  h2 = new TProfile2D("h2","h2",nbinsx,xmin,xmax,nbinsy,ymin,ymax,0,20);

  Double_t bwidthx=(xmax-xmin)/nbinsx;
  Double_t bwidthy=(ymax-ymin)/nbinsy;
  Int_t i=0;

  for (i=0; i<5; i++)



  Double_t x,y,z;
  while(x<xmax) {

	  while(y<ymax) {
		   cout << i << "\t" << x << "\t" << y << "\t" << z << endl;

} Received on Tue Feb 15 2005 - 20:52:15 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:05 MET