GetBinCenter() in log

Hi there,
Below’s a small improvement proposal to the TH1::GetBinCenter() method in order that it could also return the bin center in log.

Double_t TH1::GetBinCenter (Int_t ibin, Bool_t log=kFALSE) { Double_t lowEdge = this->GetXaxis()->GetBinLowEdge(ibin) ; Double_t upEdge = this->GetXaxis()->GetBinUpEdge (ibin) ; if ( !log ) { return ( 0.5*(lowEdge+upEdge) ) ; } else { return ( TMath::Sqrt(lowEdge*upEdge) ) ; } }

A funny way to compute the log! Are you sure?
What happens if low is negative and up is positive?


I may have badly expressed myself.
This of course only makes sense with gPad->SetLogx() and no negative values in xbins[].
With a log-equidistant binning, the mean is :
0.5*(ln E1 + ln E2) i.e. sqrt(E1E2) in logx (e.g. sqrt(10^010^2) = 10).
Just a proposal…

If you want to convert a user coordinate to pad coordinate, simply do

pad.XtoPad(usercoordinate). so if you have called pad.SetLogx, then

pad.XtoPad(h->GetXaxis()->GetBinCenter(bin)) will convert the center of the bin to the log coordinate


This just returns TMath::Log10(x) and not the bin center in log.
Imagine a bin with xlow=1 and xup=100 :

  • the center in lin is (100-1)/2=50.5
  • the center in log would be sqrt(1*100)=10 (!=log(50.5))
    This is very easy to compute. If this sounds useless, no problem. This was just a proposal.

I have implemented your suggestion as TAxis::GetBinCenterLog(bin), see: … nCenterLog

Now in the SVN trunk.


Thanks René !

Hi there,

Histogram statistics (mean value, “rms” and their corresponding errors) are computed using x = fXaxis.GetBinCenter(binx); (see TH1::GetStats())

For log-equidistant binned histograms, it woul be proper to use x = fXaxis.GetBinCenterLog(binx); (see above)

Could you please implement this?



We are not supporting log-equidistant histograms, that can be built directly, so we cannot implement this.
Adding this feature would also add quite some complexity to the histogram classes.
If you want to do that from the beginning, why not filling directly the histogram with log(x) ?

Best Regards


Ok, thanks Lorenzo.

Just in case, attached’s a script to compute the mean, rms, mean error and rms error of a log-equidistant histogram and display the corresponding (corrected) statbox.

ztest.cpp (12.2 KB)