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
couet
September 20, 2023, 5:34am
2
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.
couet
September 20, 2023, 6:04am
4
try:
TH1D * h1 = h2->ProjectionX();
h1->LabelsDeflate();
system
Closed
October 4, 2023, 6:05am
5
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.