Re: TProfile2D

From: Axel Naumann <axel-naumann_at_gmx.de>
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.
Axel.

{
  gROOT->Reset();
  gStyle->SetOptStat(0);

  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++)

          h2->Fill((Double_t)i-2,(Double_t)i-1,(Double_t)i);

  h2->Draw("colz");

  Double_t x,y,z;
  x=xmin+bwidthx/2;
  while(x<xmax) {

	  y=ymin+bwidthy/2;
	  while(y<ymax) {
		   i=h2->FindBin(x,y);
		   z=h2->GetBinContent(i);
			
		   cout << i << "\t" << x << "\t" << y << "\t" << z << endl;
		
		   y+=bwidthy;
		
	  }
	  x+=bwidthx;

  }
} 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