Nbin increased after setting the bin label?

Hi all,

I am having trouble understanding the behavior of the following code.
Why does setting the bin label of h2 increase the number of bin for the projected h1?
Is this expected?

int nbin = 10;
TH2D * h2 = new TH2D("h2", "h2", nbin, 0, nbin, nbin, 0, nbin);

for (int i = 0; i < nbin; ++i)
{
  h2->GetXaxis()->SetBinLabel(i + 1, Form("%d", i));
  h2->GetYaxis()->SetBinLabel(i + 1, Form("%d", i));
}

TH1D * h1 = h2->ProjectionX();

cout << h2->GetNbinsX() << endl;  // output 10
cout << h1->GetNbinsX() << endl;  // output 20 

ROOT Version: 6-28-04
Platform: MacOS
Compiler: Not Provided


I do not see this effect with the latest ROOT version:

root [0] int nbin = 10;
root [1] TH2D * h2 = new TH2D("h2", "h2", nbin, 0, nbin, nbin, 0, nbin);
root [2] 
root [2] for (int i = 0; i < nbin; ++i)
root [3] {
root (cont'ed, cancel with .@) [4]  h2->GetXaxis()->SetBinLabel(i + 1, Form("%d", i));
root (cont'ed, cancel with .@) [5]  h2->GetYaxis()->SetBinLabel(i + 1, Form("%d", i));
root (cont'ed, cancel with .@) [6]}
root [7] TH1D * h1 = h2->ProjectionX();
root [8] cout << h2->GetNbinsX() << endl; 
10
root [9] cout << h1->GetNbinsX() << endl; 
10
root [10] 

Thank you for the reply.

I just checked. It looks like it only show up wrong when I run it as a macro. I got the correct 10 and 10 output same as your when I ran the code in interactive ROOT session.

try:

TH1D * h1 = h2->ProjectionX();
h1->LabelsDeflate();

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