Problem on using RebinY for TH2D

I have a TH2D file, say,
TH2D RAW = new TH2D(“spectrum”,“spectrum”,1023,0,1023,2000,0,2000);
here the total bins in yaxis is 2000.
Now I would like to rebin along the yaxis bying merging 2 bins into one bin of a new histogram, for example, bin number 200 and 201 of the RAW to a new bin…

I try to do in the following way

TH2D *hnew = RAW->RebinY(2,“hnew”);

I would expect that now the total bins for the rebinned histogram hnew is 2000/2=1000. However, when I plot it, I still can see 2000 bins in yaxis, but with changed bin content…

Can you tell me what mistake I made here?

The same happen to RebinX…

It works for me:

root [0] TH2D *RAW = new TH2D("spectrum","spectrum",1023,0,1023,2000,0,2000);
root [1] RAW->GetNbinsY()
(const Int_t)2000
root [2] TH2D *hnew = RAW->RebinY(2,"hnew"); 
root [3] hnew->GetNbinsY()
(const Int_t)1000

thank you for your reply, couet.

I got the same as you when I use GetNbins(). However, when I plot it by hnew->Draw(“colz”), I still can see 2000 bins in yaxis… I do not understand this…

I think you are confusing the number of bins and the axis limits.

Yes… you are right. When I zoom in yaxis, I see actually the 2 units in yaxis actually should correspond to 1 bin.

Really confusing to me… Are there any way to avoid this, i mean, when I rebin the histogra, how the axis change accordingly.

Thanks a lot!

The number of bins and the axis limits are two different things see en.wikipedia.org/wiki/Histogram
They are not link together.

thank you again… I am still a newer for ROOT…

One last question: how can I change the yunit in yaxis to 1 bin after rebinning, namely, 1 in the axis actually corresponds to bin 1? In such a way, we will more easily see the actual time (yaxis) from our data.

hnew->GetYaxis()->SetRangeUser(0,1000);
hnew->Draw();