# Histogram bin sizes for log plots

Hi,

Is there some way to have ROOT create bin sizes for a histogram such that when you plot
the histogram on a log scale, the bins will all appear to have the same width (visually, not numerically) on the plot? Basically, instead of a histogram with fixed bin sizes, I want a histogram with variable bin sizes that increase at the same rate the log axis scale is increasing (small bin sizes at the minimum end of the axis, large bin sizes at the maximum end of the axis). I know how to make a histogram with variable bin sizes, but this would require trying to figure out what all the bin boundaries should be, and then feeding a huge array of numbers to the histogram constructor. How can I get this kind of logarithmically increasing bin size in my histogram?

By definition, this is not a drawing problem.
You have two options;
-create your histogram with variable bin size , then fill it with the variable.
You can select/on/off the log scale when drawing.

-create your histogram with fix bin size
between (log10(xmin) and log10(xmax),
then fill it with log10(x). In this scale
do not select log scale when drawing, but
put an axis title indicating log10(x)

Rene

Salut Rene,

when using variable bin size, one has to weight each entry put into the i-th bin by

(X_MAX - X_MIN) / N_BINS / DX_i

where DX_i is the width of the i-th bin. Is the some option in

``tree->Draw ("expression>>histo")``

which does this automatically? Or do I have to loop over the tree manually like

``````// varbin2.C
// now, after booking the histogram, fill it with a power law
// PN Tue Nov 28 10:51:43 EST 2006

// do a simple E^-1 spectrum for now
TRandom3 rangen (1);

for (Int_t i_event = 0; i_event .lt. NEVENT; i_event++)

{

Float_t e = pow (10, rangen.Uniform (L_MIN, L_MAX));

Int_t i_bin = h_varbin1->GetXaxis ()->FindBin (e);

Float_t w = (X_MAX - X_MIN) / N_BINS h_varbin1->GetXaxis()->GetBinWidth(i_bin);

h_varbin1->Fill (e, w);

h_fixbin1->Fill (e);

}``````

I don’t really like this solution, because it would force me to SetBranchAddresses of the tree, etc, …
Cheers, Peter.

You just need to make a loop (after having filled your histogram) and
call TH1::SetBinContent/SetBinError with the old value divided
by the bin width.

Rene

C’est reussi!

In red, the fixed binning, in black the variable bin size binning.

Cheers & thanks, Peter.
nbins_50.ps (21.6 KB)