GetXbins() returning 0-size TArrayD


I have issue that after calling GetXbins() in TAxis class I get null-size pointer to array. Maybe I am doing something wrong, please see my code. Histo_Mratio is populated T2D (I am drawing it for a check.)

OldBins= Histo_Mratio.GetXaxis().GetXbins()
#nOldBins = OldBins.GetSize()
nOldBins = 10
print nOldBins
for ibin in xrange(nOldBins):
print binEdge
if binEdge>MRxbins[1]:
if binEdge<MRxbins[0]:

print MRxbins

Thanks for help, Tomas.

1 Like

Your example does not show the creation of Histo_Mratio


Histo_Mratio = TH2D(datafile.Get(“Mratio_dRqs”)).Clone(“Mratio_dRqs”)

It is cloned from root file. As I said I can draw it so I see that histogram is nicely populated.

Thanks, Tomas.

So it should work. Can you provide a small macro (preferably in c++) reproducing the problem ?

O.k. I will try to fill randomly some TH2, and then use the rest of the code.

If your histogram is a “fix bin size” one then “GetXbins” makes no sense (“GetSize” for this array returns 0 as it is not used at all).
BTW. You should have something like:
TH2D *Histo_Mratio = ((TH2D *)((datafile.Get(“Mratio_dRqs”))->Clone(“Mratio_dRqs”)))

1 Like

Ok. I see. I thought it would return array of low edges even for fixed size histograms. And actually, I still think it would make sense. But o.k…

I wanted to use that to merge two bins given histogram. Is there any claver function that could merge just range of bins which are specified and rest of the binnig remains as it was in given histogram?

Try to play with something like this (it should work for fix and variable bin size histograms): TH2D *Histo_Mratio = ((TH2D *)((datafile.Get("Mratio_dRqs"))->Clone("Mratio_dRqs"))); Int_t N = Histo_Mratio->GetXaxis()->GetNbins(); // bin 1 is the first bin, bin (N + 1) is the "overflow bin" for (Int_t i = 1; i <= (N + 1); i++) cout << Histo_Mratio->GetXaxis()->GetBinLowEdge(i) << endl;

Fair enough. Thanks.

May be such loop can be done in GetXbins to build the vector in case of fixed binning.
I am do not know if that will be useful or not.
Lorenzo may have an idea about that.

I have also another issue with FindBin which is always returning the last bin:


whatever ixbin is. Should I open new discussion for this?

Probably you are passing a x value outside the histogram range. And how many bins your histogram is having ?


Actually, I think, for a 2D histogram you should use something like:
Int_t gbin = someTH2->FindBin(someXvalue, someYvalue);
If you use:
Int_t gbin = someTH2->GetXaxis()->FindBin(someXvalue);
then you will get the “global bin” number for the corresponding “underflow bin” in “Y”.

Why not write this important information in reference guide?