Service class for 2-Dim histogram classes
TH2C a 2-D histogram with one byte per cell (char)
TH2S a 2-D histogram with two bytes per cell (short integer)
TH2I a 2-D histogram with four bytes per cell (32 bits integer)
TH2F a 2-D histogram with four bytes per cell (float)
TH2D a 2-D histogram with eight bytes per cell (double)

Fill histogram with all entries in the buffer.
action = -1 histogram is reset and refilled from the buffer (called by THistPainter::Paint)
action = 0 histogram is filled from the buffer
action = 1 histogram is filled and buffer is deleted
The buffer is automatically deleted when the number of entries
in the buffer is greater than the number of entries in the histogram

Increment cell defined by x,y by 1.
if x or/and y is less than the low-edge of the corresponding axis first bin,
the Underflow cell is incremented.
if x or/and y is greater than the upper edge of corresponding axis last bin,
the Overflow cell is incremented.
If the storage of the sum of squares of weights has been triggered,
via the function Sumw2, then the sum of the squares of weights is incremented
by 1 in the cell corresponding to x,y.
The function returns the corresponding global bin number which has its content
incremented by 1

Increment cell defined by x,y by a weight w.
if x or/and y is less than the low-edge of the corresponding axis first bin,
the Underflow cell is incremented.
if x or/and y is greater than the upper edge of corresponding axis last bin,
the Overflow cell is incremented.
If the weight is not equal to 1, the storage of the sum of squares of
weights is automatically triggered and the sum of the squares of weights is incremented
by w^2 in the bin corresponding to x,y
The function returns the corresponding global bin number which has its content
incremented by w

Increment cell defined by namex,namey by a weight w
if x or/and y is less than the low-edge of the corresponding axis first bin,
the Underflow cell is incremented.
if x or/and y is greater than the upper edge of corresponding axis last bin,
the Overflow cell is incremented.
If the weight is not equal to 1, the storage of the sum of squares of
weights is automatically triggered and the sum of the squares of weights is incremented
by w^2 in the bin corresponding to namex,namey
The function returns the corresponding global bin number which has its content
incremented by w

Increment cell defined by namex,y by a weight w
if x or/and y is less than the low-edge of the corresponding axis first bin,
the Underflow cell is incremented.
if x or/and y is greater than the upper edge of corresponding axis last bin,
the Overflow cell is incremented.
If the weight is not equal to 1, the storage of the sum of squares of
weights is automatically triggered and the sum of the squares of weights is incremented
by w^2 in the bin corresponding to namex,y
The function returns the corresponding global bin number which has its content
incremented by w

Increment cell defined by x,namey by a weight w
if x or/and y is less than the low-edge of the corresponding axis first bin,
the Underflow cell is incremented.
if x or/and y is greater than the upper edge of corresponding axis last bin,
the Overflow cell is incremented.
If the weight is not equal to 1, the storage of the sum of squares of
weights is automatically triggered and the sum of the squares of weights is incremented
by w^2 in the bin corresponding to x,y.
The function returns the corresponding global bin number which has its content
incremented by w

Fill a 2-D histogram with an array of values and weights.
ntimes: number of entries in arrays x and w (array size must be ntimes*stride)
x: array of x values to be histogrammed
y: array of y values to be histogrammed
w: array of weights
stride: step size through arrays x, y and w
If the weight is not equal to 1, the storage of the sum of squares of
weights is automatically triggered and the sum of the squares of weights is incremented
by w[i]^2 in the bin corresponding to x[i],y[i].
If w is NULL each entry is assumed a weight=1
NB: function only valid for a TH2x object

Fill histogram following distribution in function fname.
The distribution contained in the function fname (TF2) is integrated
over the channel contents.
It is normalized to 1.
Getting one random number implies:
- Generating a random number between 0 and 1 (say r1)
- Look in which bin in the normalized integral r1 corresponds to
- Fill histogram channel
ntimes random numbers are generated
One can also call TF2::GetRandom2 to get a random variate from a function.

Fill histogram following distribution in histogram h.
The distribution contained in the histogram h (TH2) is integrated
over the channel contents.
It is normalized to 1.
Getting one random number implies:
- Generating a random number between 0 and 1 (say r1)
- Look in which bin in the normalized integral r1 corresponds to
- Fill histogram channel
ntimes random numbers are generated

Project slices along X in case of a 2-D histogram, then fit each slice
with function f1 and make a histogram for each fit parameter
Only bins along Y between firstybin and lastybin are considered.
By default (firstybin == 0, lastybin == -1), all bins in y including
over- and underflows are taken into account.
If f1=0, a gaussian is assumed
Before invoking this function, one can set a subrange to be fitted along X
via f1->SetRange(xmin,xmax)
The argument option (default="QNR") can be used to change the fit options.
"Q" means Quiet mode
"N" means do not show the result of the fit
"R" means fit the function in the specified function range
"G2" merge 2 consecutive bins along X
"G3" merge 3 consecutive bins along X
"G4" merge 4 consecutive bins along X
"G5" merge 5 consecutive bins along X
"S" sliding merge: merge n consecutive bins along X accordingly to what Gn is given.
It makes sense when used together with a Gn option
The generated histograms are returned by adding them to arr, if arr is not NULL.
arr's SetOwner() is called, to signal that it is the user's respponsability to
delete the histograms, possibly by deleting the arrary.
TObjArray aSlices;
h2->FitSlicesX(func, 0, -1, 0, "QNR", &aSlices);
will already delete the histograms once aSlice goes out of scope. aSlices will
contain the histogram for the i-th parameter of the fit function at aSlices[i];
aSlices[n] (n being the number of parameters) contains the chi2 distribution of
the fits.
If arr is NULL, the generated histograms are added to the list of objects
in the current directory. It is the user's responsability to delete
these histograms.
Example: Assume a 2-d histogram h2
Root > h2->FitSlicesX(); produces 4 TH1D histograms
with h2_0 containing parameter 0(Constant) for a Gaus fit
of each bin in Y projected along X
with h2_1 containing parameter 1(Mean) for a gaus fit
with h2_2 containing parameter 2(StdDev) for a gaus fit
with h2_chi2 containing the chisquare/number of degrees of freedom for a gaus fit
Root > h2->FitSlicesX(0,15,22,10);
same as above, but only for bins 15 to 22 along Y
and only for bins in Y for which the corresponding projection
along X has more than cut bins filled.
NOTE: To access the generated histograms in the current directory, do eg:
TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");

Project slices along Y in case of a 2-D histogram, then fit each slice
with function f1 and make a histogram for each fit parameter
Only bins along X between firstxbin and lastxbin are considered.
By default (firstxbin == 0, lastxbin == -1), all bins in x including
over- and underflows are taken into account.
If f1=0, a gaussian is assumed
Before invoking this function, one can set a subrange to be fitted along Y
via f1->SetRange(ymin,ymax)
The argument option (default="QNR") can be used to change the fit options.
"Q" means Quiet mode
"N" means do not show the result of the fit
"R" means fit the function in the specified function range
"G2" merge 2 consecutive bins along Y
"G3" merge 3 consecutive bins along Y
"G4" merge 4 consecutive bins along Y
"G5" merge 5 consecutive bins along Y
"S" sliding merge: merge n consecutive bins along Y accordingly to what Gn is given.
It makes sense when used together with a Gn option
The generated histograms are returned by adding them to arr, if arr is not NULL.
arr's SetOwner() is called, to signal that it is the user's respponsability to
delete the histograms, possibly by deleting the arrary.
TObjArray aSlices;
h2->FitSlicesY(func, 0, -1, 0, "QNR", &aSlices);
will already delete the histograms once aSlice goes out of scope. aSlices will
contain the histogram for the i-th parameter of the fit function at aSlices[i];
aSlices[n] (n being the number of parameters) contains the chi2 distribution of
the fits.
If arr is NULL, the generated histograms are added to the list of objects
in the current directory. It is the user's responsability to delete
these histograms.
Example: Assume a 2-d histogram h2
Root > h2->FitSlicesY(); produces 4 TH1D histograms
with h2_0 containing parameter 0(Constant) for a Gaus fit
of each bin in X projected along Y
with h2_1 containing parameter 1(Mean) for a gaus fit
with h2_2 containing parameter 2(StdDev) for a gaus fit
with h2_chi2 containing the chisquare/number of degrees of freedom for a gaus fit
Root > h2->FitSlicesY(0,15,22,10);
same as above, but only for bins 15 to 22 along X
and only for bins in X for which the corresponding projection
along Y has more than cut bins filled.
NOTE: To access the generated histograms in the current directory, do eg:
TH1D *h2_1 = (TH1D*)gDirectory->Get("h2_1");
A complete example of this function is given in

compute first cell (binx,biny) in the range [firstxbin,lastxbin][firstybin,lastybin] for which
diff = abs(cell_content-c) <= maxdiff
In case several cells in the specified range with diff=0 are found
the first cell found is returned in binx,biny.
In case several cells in the specified range satisfy diff <=maxdiff
the cell with the smallest difference is returned in binx,biny.
In all cases the function returns the smallest difference.
NOTE1: if firstxbin < 0, firstxbin is set to 1
if (lastxbin < firstxbin then lastxbin is set to the number of bins in X
ie if firstxbin=1 and lastxbin=0 (default) the search is on all bins in X except
for X's under- and overflow bins.
if firstybin < 0, firstybin is set to 1
if (lastybin < firstybin then lastybin is set to the number of bins in Y
ie if firstybin=1 and lastybin=0 (default) the search is on all bins in Y except
for Y's under- and overflow bins.
NOTE2: if maxdiff=0 (default), the first cell with content=c is returned.

Fill the array stats from the contents of this histogram
The array stats must be correctly dimensionned in the calling program.
stats[0] = sumw
stats[1] = sumw2
stats[2] = sumwx
stats[3] = sumwx2
stats[4] = sumwy
stats[5] = sumwy2
stats[6] = sumwxy
If no axis-subranges are specified (via TAxis::SetRange), the array stats
is simply a copy of the statistics quantities computed at filling time.
If sub-ranges are specified, the function recomputes these quantities
from the bin contents in the current axis ranges.
Note that the mean value/StdDev is computed using the bins in the currently
defined ranges (see TAxis::SetRange). By default the ranges include
all bins from 1 to nbins included, excluding underflows and overflows.
To force the underflows and overflows in the computation, one must
call the static function TH1::StatOverflows(kTRUE) before filling
the histogram.

Return integral of bin contents. Only bins in the bins range are considered.
By default the integral is computed as the sum of bin contents in the range.
if option "width" is specified, the integral is the sum of
the bin contents multiplied by the bin width in x and in y.

Return integral of bin contents in range [firstxbin,lastxbin],[firstybin,lastybin]
for a 2-D histogram
By default the integral is computed as the sum of bin contents in the range.
if option "width" is specified, the integral is the sum of
the bin contents multiplied by the bin width in x and in y.

Return integral of bin contents in range [firstxbin,lastxbin],[firstybin,lastybin]
for a 2-D histogram. Calculates also the integral error using error propagation
from the bin errors assumming that all the bins are uncorrelated.
By default the integral is computed as the sum of bin contents in the range.
if option "width" is specified, the integral is the sum of
the bin contents multiplied by the bin width in x and in y.

Given a point P(x,y), Interpolate approximates the value via bilinear
interpolation based on the four nearest bin centers
see Wikipedia, Bilinear Interpolation
Andy Mastbaum 10/8/2008
vaguely based on R.Raja 6-Sep-2008

Statistical test of compatibility in shape between
THIS histogram and h2, using Kolmogorov test.
Default: Ignore under- and overflow bins in comparison
option is a character string to specify options
"U" include Underflows in test
"O" include Overflows
"N" include comparison of normalizations
"D" Put out a line of "Debug" printout
"M" Return the Maximum Kolmogorov distance instead of prob
The returned function value is the probability of test
(much less than one means NOT compatible)
The KS test uses the distance between the pseudo-CDF's obtained
from the histogram. Since in 2D the order for generating the pseudo-CDF is
arbitrary, two pairs of pseudo-CDF are used, one starting from the x axis the
other from the y axis and the maximum distance is the average of the two maximum
distances obtained.
Code adapted by Rene Brun from original HBOOK routine HDIFF

Add all histograms in the collection to this histogram.
This function computes the min/max for the axes,
compute a new number of bins, if necessary,
add bin contents, errors and statistics.
If overflows are present and limits are different the function will fail.
The function returns the total number of entries in the result histogram
if the merge is successfull, -1 otherwise.
IMPORTANT remark. The 2 axis x and y may have different number
of bins and different limits, BUT the largest bin width must be
a multiple of the smallest bin width and the upper limit must also
be a multiple of the bin width.

Rebin this histogram grouping nxgroup/nygroup bins along the xaxis/yaxis together.
if newname is not blank a new temporary histogram hnew is created.
else the current histogram is modified (default)
The parameter nxgroup/nygroup indicate how many bins along the xaxis/yaxis of this
have to me merged into one bin of hnew
If the original histogram has errors stored (via Sumw2), the resulting
histograms has new errors correctly calculated.
examples: if hpxpy is an existing TH2 histogram with 40 x 40 bins
hpxpy->Rebin2D(); // merges two bins along the xaxis and yaxis in one in hpxpy
// Carefull: previous contents of hpxpy are lost
hpxpy->RebinX(5); //merges five bins along the xaxis in one in hpxpy
TH2 *hnew = hpxpy->RebinY(5,"hnew"); // creates a new histogram hnew
// merging 5 bins of h1 along the yaxis in one bin
NOTE : If nxgroup/nygroup is not an exact divider of the number of bins,
along the xaxis/yaxis the top limit(s) of the rebinned histogram
is changed to the upper edge of the xbin=newxbins*nxgroup resp.
ybin=newybins*nygroup and the corresponding bins are added to
the overflow bin.
Statistics will be recomputed from the new bin contents.

Project a 2-D histogram into a profile histogram along X.
The projection is made from the channels along the Y axis
ranging from firstybin to lastybin included.
By default, bins 1 to ny are included
When all bins are included, the number of entries in the projection
is set to the number of entries of the 2-D histogram, otherwise
the number of entries is incremented by 1 for all non empty cells.
if option "d" is specified, the profile is drawn in the current pad.
if option "o" original axis range of the target axes will be
kept, but only bins inside the selected range will be filled.
The option can also be used to specify the projected profile error type.
Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
One must create a graphical cut (mouse or C++) and specify the name
of the cut between [] in the option.
For example, with a TCutG named "cutg", one can call:
myhist->ProfileX(" ",firstybin,lastybin,"[cutg]");
To invert the cut, it is enough to put a "-" in front of its name:
myhist->ProfileX(" ",firstybin,lastybin,"[-cutg]");
It is possible to apply several cuts ("," means logical AND):
myhist->ProfileX(" ",firstybin,lastybin,"[cutg1,cutg2]");
NOTE that if a TProfile named "name" exists in the current directory or pad with
a compatible axis the profile is reset and filled again with the projected contents of the TH2.
In the case of axis incompatibility an error is reported and a NULL pointer is returned.
NOTE that the X axis attributes of the TH2 are copied to the X axis of the profile.
NOTE that the default under- / overflow behavior differs from what ProjectionX
does! Profiles take the bin center into account, so here the under- and overflow
bins are ignored by default.

Project a 2-D histogram into a profile histogram along Y.
The projection is made from the channels along the X axis
ranging from firstxbin to lastxbin included.
By default, bins 1 to nx are included
When all bins are included, the number of entries in the projection
is set to the number of entries of the 2-D histogram, otherwise
the number of entries is incremented by 1 for all non empty cells.
if option "d" is specified, the profile is drawn in the current pad.
if option "o" , the original axis range of the target axis will be
kept, but only bins inside the selected range will be filled.
The option can also be used to specify the projected profile error type.
Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
One must create a graphical cut (mouse or C++) and specify the name
of the cut between [] in the option.
For example, with a TCutG named "cutg", one can call:
myhist->ProfileY(" ",firstybin,lastybin,"[cutg]");
To invert the cut, it is enough to put a "-" in front of its name:
myhist->ProfileY(" ",firstybin,lastybin,"[-cutg]");
It is possible to apply several cuts:
myhist->ProfileY(" ",firstybin,lastybin,"[cutg1,cutg2]");
NOTE that if a TProfile named "name" exists in the current directory or pad with
a compatible axis the profile is reset and filled again with the projected contents of the TH2.
In the case of axis incompatibility an error is reported and a NULL pointer is returned.
NOTE that the Y axis attributes of the TH2 are copied to the X axis of the profile.
NOTE that the default under- / overflow behavior differs from what ProjectionX
does! Profiles take the bin center into account, so here the under- and overflow
bins are ignored by default.

Project a 2-D histogram into a 1-D histogram along X.
The projection is always of the type TH1D.
The projection is made from the channels along the Y axis
ranging from firstybin to lastybin included.
By default, all bins including under- and overflow are included.
The number of entries in the projection is estimated from the
number of effective entries for all the cells included in the projection.
To exclude the underflow bins in Y, use firstybin=1.
To exclude the overflow bins in Y, use lastybin=nx.
if option "e" is specified, the errors are computed.
if option "d" is specified, the projection is drawn in the current pad.
if option "o" original axis range of the taget axes will be
kept, but only bins inside the selected range will be filled.
Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
One must create a graphical cut (mouse or C++) and specify the name
of the cut between [] in the option.
For example, with a TCutG named "cutg", one can call:
myhist->ProjectionX(" ",firstybin,lastybin,"[cutg]");
To invert the cut, it is enough to put a "-" in front of its name:
myhist->ProjectionX(" ",firstybin,lastybin,"[-cutg]");
It is possible to apply several cuts:
myhist->ProjectionX(" ",firstybin,lastybin,"[cutg1,cutg2]");
NOTE that if a TH1D named "name" exists in the current directory or pad
the histogram is reset and filled again with the projected contents of the TH2.
NOTE that the X axis attributes of the TH2 are copied to the X axis of the projection.

Project a 2-D histogram into a 1-D histogram along Y.
The projection is always of the type TH1D.
The projection is made from the channels along the X axis
ranging from firstxbin to lastxbin included.
By default, all bins including under- and overflow are included.
The number of entries in the projection is estimated from the
number of effective entries for all the cells included in the projection
To exclude the underflow bins in X, use firstxbin=1.
To exclude the oveerflow bins in X, use lastxbin=nx.
if option "e" is specified, the errors are computed.
if option "d" is specified, the projection is drawn in the current pad.
if option "o" original axis range of the taget axes will be
kept, but only bins inside the selected range will be filled.
Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
One must create a graphical cut (mouse or C++) and specify the name
of the cut between [] in the option.
For example, with a TCutG named "cutg", one can call:
myhist->ProjectionY(" ",firstxbin,lastxbin,"[cutg]");
To invert the cut, it is enough to put a "-" in front of its name:
myhist->ProjectionY(" ",firstxbin,lastxbin,"[-cutg]");
It is possible to apply several cuts:
myhist->ProjectionY(" ",firstxbin,lastxbin,"[cutg1,cutg2]");
NOTE that if a TH1D named "name" exists in the current directory or pad and having
a compatible axis, the histogram is reset and filled again with the projected contents of the TH2.
In the case of axis incompatibility, an error is reported and a NULL pointer is returned.
NOTE that the Y axis attributes of the TH2 are copied to the X axis of the projection.

Compute the X distribution of quantiles in the other variable Y
name is the name of the returned histogram
prob is the probability content for the quantile (0.5 is the default for the median)
An approximate error for the quantile is computed assuming that the distribution in
the other variable is normal.

Compute the Y distribution of quantiles in the other variable X
name is the name of the returned histogram
prob is the probability content for the quantile (0.5 is the default for the median)
An approximate error for the quantile is computed assuming that the distribution in
the other variable is normal.

When the mouse is moved in a pad containing a 2-d view of this histogram
a second canvas shows the projection along X corresponding to the
mouse position along Y.
To stop the generation of the projections, delete the canvas
containing the projection.

When the mouse is moved in a pad containing a 2-d view of this histogram
a second canvas shows the projection along Y corresponding to the
mouse position along X.
To stop the generation of the projections, delete the canvas
containing the projection.

Interface to TSpectrum2::Search
the function finds peaks in this histogram where the width is > sigma
and the peak maximum greater than threshold*maximum bin content of this.
for more detauils see TSpectrum::Search.
note the difference in the default value for option compared to TSpectrum2::Search
option="" by default (instead of "goff")

Smooth bin contents of this 2-d histogram using kernel algorithms
similar to the ones used in the raster graphics community.
Bin contents in the active range are replaced by their smooth values.
If Errors are defined via Sumw2, they are also scaled and computed.
However, note the resulting errors will be correlated between different-bins, so
the errors should not be used blindly to perform any calculation involving several bins,
like fitting the histogram. One would need to compute also the bin by bin correlation matrix.
3 kernels are proposed k5a, k5b and k3a.
k5a and k5b act on 5x5 cells (i-2,i-1,i,i+1,i+2, and same for j)
k5b is a bit more stronger in smoothing
k3a acts only on 3x3 cells (i-1,i,i+1, and same for j).
By default the kernel "k5a" is used. You can select the kernels "k5b" or "k3a"
via the option argument.
If TAxis::SetRange has been called on the x or/and y axis, only the bins
in the specified range are smoothed.
In the current implementation if the first argument is not used (default value=1).
implementation by David McKee (dmckee@bama.ua.edu). Extended by Rene Brun