# TH1 Histograms and automatic xaxis limits, not always good

According to the TH1 documentation:

[quote]Histograms with automatic bins

When an histogram is created with an axis lower limit greater or equal to its upper limit, the SetBuffer is automatically called with an argument fBufferSize equal to fgBufferSize (default value=1000). fgBufferSize may be reset via the static function TH1::SetDefaultBufferSize. The axis limits will be automatically computed when the buffer will be full or when the function BufferEmpty is called[/quote]

This works partially for me.
I wrote a little software that generates n random numbers uniformly distributed in [0,1] and stores them in an array. Then I calculate the sum of c consecutives elements of this array and store them in another one.
I then draw a frequency histogram of the sums value, tho show the central limit theorem (the sums of c consecutive elements have a gaussian distribution).
My problem is that the automatic axis limits aren’t always good. Here are some examples of the resulting histograms: (n and c are passed as command line arguments)
N= 100000, c = 20

It’s okay, I can see the whole Gaussian

N = 100000, c = 50

Still ok

N = 1000000, c = 200

Beginning of troubles

N = 1000000, c = 1000

More troubles

(I just noticed that from my poor choice of parameters for the screenshots one could think that N is a factor. Actually the problem depends only on c)

Why is this, and how can I always see the entire thing?

I think the problem is that the “xmin” and “xmax” are calculated from the very first “sample” of events (i.e. when the “buffer” gets “full”, after the first 1000 “Fill” calls). So, if this “initial sample” is “biased”, you will get “wrong” limits (“biased” means, for example, that the first 1000 events contained entries from the lower “x” values of the whole “distribution” only).

I’m not sure if it helps, but, before you start to “fill” your histogram, try:
MyHisto->SetBit(TH1::kCanRebin);

You might also try to increase the buffer size, before you start to “fill” your histogram, try:
MyHisto->SetBuffer(10000); // this histogram only
or, before you create your histogram, try:
TH1::SetDefaultBufferSize(10000); // all histograms
(Well, you should make sure that the first 10000 events span across the whole “x” range, otherwise you will again get the same problem with “too small” x-axis limits.)

I had already tried the kCanRebin flag: it always gives axis limits “large” enough to view the entire Gaussian curve but, because of how it works (doubling the bins size), they are “ugly”: in a lot of cases they are unnecessarily large (lot of whitespace) and the gaussian curve is not centered.

Regarding the buffer size I did see that option and thought it was what I was looking for, I had already tried using it, but I made an error: I wrote MyHisto->SetDefaultBufferSize(20000). (it compiles without warnings/errors)
I now changed it to MyHisto->SetBuffer and it works perfectly, thanks!