Re: [OT] Histograms on logarithmic scale (constant bin size on logaritmic scale)

From: Roland Kuhn <rkuhn_at_e18.physik.tu-muenchen.de>
Date: Mon, 11 Sep 2006 16:22:18 +0200


Hi Marco!

On 11 Sep 2006, at 15:09, Marco Calviani wrote:

> Hello ROOTers,
> i'm facing a small issue, not necessarily correlated with ROOT: i
> want to build an histogram with the energy on the X scale put in
> logarithmic scale.
> I'm building the histogram with the Fill() function. However, since
> the bin width is constant on the x scale, whenever i'm changing to log
> x scale lower bins appears larger than bigger bins (obviously). One
> solution to this problem could be to fill not directly the x axis with
> Fill(x) but Fill(Log10(x)). This works but the x axis appears with
> linear labels while it should be logarithmic.
>
> My question is twofold:
> 1) is it possible to solve this without using Fill(Log10(x))?
>

Yes, you have to change the binning. Create the histogram to range from log10(min) to log10(max) with the desired number of bins and feed it to a function like this:

void BinLogX(TH1*h)
{

   TAxis *axis = h->GetXaxis();
   int bins = axis->GetNbins();

   Axis_t from = axis->GetXmin();
   Axis_t to = axis->GetXmax();
   Axis_t width = (to - from) / bins;
   Axis_t *new_bins = new Axis_t[bins + 1];

   for (int i = 0; i <= bins; i++) {
     new_bins[i] = TMath::Power(10, from + i * width);
   }
   axis->Set(bins, new_bins);
   delete new_bins;
}

So, e.g.

TH1F *h=new TH1F("a","a",100,-6,0);
BinLogX(h)

to get a histogram from 1e-6 to 1 which displays nicely with gPad-  >SetLogx().

Ciao,

                     Roland

--
TU Muenchen, Physik-Department E18, James-Franck-Str., 85748 Garching
Telefon 089/289-12575; Telefax 089/289-12570
--
CERN office: 892-1-D23 phone: +41 22 7676540 mobile: +41 76 487 4482
--
Any society that would give up a little liberty to gain a little
security will deserve neither and lose both.  - Benjamin Franklin
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GS/CS/M/MU d-(++) s:+ a-> C+++ UL++++ P+++ L+++ E(+) W+ !N K- w--- M 
+ !V Y+
PGP++ t+(++) 5 R+ tv-- b+ DI++ e+++>++++ h---- y+++
------END GEEK CODE BLOCK------



Received on Mon Sep 11 2006 - 16:22:30 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:01 MET