107-# [Saving/reading histograms to/from a ROOT file](\ref saving-histograms)
108-# [Operations on histograms](\ref operations-on-histograms)
109-# [Miscellaneous operations](\ref misc)
110
111ROOT supports the following histogram types:
112
113 - 1-D histograms:
114 - TH1C : histograms with one byte per channel. Maximum bin content = 127
115 - TH1S : histograms with one short per channel. Maximum bin content = 32767
116 - TH1I : histograms with one int per channel. Maximum bin content = INT_MAX (\ref intmax "*")
117 - TH1L : histograms with one long64 per channel. Maximum bin content = LLONG_MAX (\ref llongmax "**")
118 - TH1F : histograms with one float per channel. Maximum precision 7 digits, maximum integer bin content =
119+/-16777216 (\ref floatmax "***")
120 - TH1D : histograms with one double per channel. Maximum precision 14 digits, maximum integer bin content =
121+/-9007199254740992 (\ref doublemax "****")
122 - 2-D histograms:
123 - TH2C : histograms with one byte per channel. Maximum bin content = 127
124 - TH2S : histograms with one short per channel. Maximum bin content = 32767
125 - TH2I : histograms with one int per channel. Maximum bin content = INT_MAX (\ref intmax "*")
126 - TH2L : histograms with one long64 per channel. Maximum bin content = LLONG_MAX (\ref llongmax "**")
127 - TH2F : histograms with one float per channel. Maximum precision 7 digits, maximum integer bin content =
128+/-16777216 (\ref floatmax "***")
129 - TH2D : histograms with one double per channel. Maximum precision 14 digits, maximum integer bin content =
130+/-9007199254740992 (\ref doublemax "****")
131 - 3-D histograms:
132 - TH3C : histograms with one byte per channel. Maximum bin content = 127
133 - TH3S : histograms with one short per channel. Maximum bin content = 32767
134 - TH3I : histograms with one int per channel. Maximum bin content = INT_MAX (\ref intmax "*")
135 - TH3L : histograms with one long64 per channel. Maximum bin content = LLONG_MAX (\ref llongmax "**")
136 - TH3F : histograms with one float per channel. Maximum precision 7 digits, maximum integer bin content =
137+/-16777216 (\ref floatmax "***")
138 - TH3D : histograms with one double per channel. Maximum precision 14 digits, maximum integer bin content =
139+/-9007199254740992 (\ref doublemax "****")
140 - Profile histograms: See classes TProfile, TProfile2D and TProfile3D.
141 Profile histograms are used to display the mean value of Y and its standard deviation
142 for each bin in X. Profile histograms are in many cases an elegant
143 replacement of two-dimensional histograms : the inter-relation of two
144 measured quantities X and Y can always be visualized by a two-dimensional
145 histogram or scatter-plot; If Y is an unknown (but single-valued)
146 approximate function of X, this function is displayed by a profile
147 histogram with much better precision than by a scatter-plot.
148
149<sup>
150\anchor intmax (*) INT_MAX = 2147483647 is the [maximum value for a variable of type int.](https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits)<br>
151\anchor llongmax (**) LLONG_MAX = 9223372036854775807 is the [maximum value for a variable of type long64.](https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits)<br>
152\anchor floatmax (***) 2^24 = 16777216 is the [maximum integer that can be properly represented by a float32 with 23-bit mantissa.](https://stackoverflow.com/a/3793950/7471760)<br>
153\anchor doublemax (****) 2^53 = 9007199254740992 is the [maximum integer that can be properly represented by a double64 with 52-bit mantissa.](https://stackoverflow.com/a/3793950/7471760)
154</sup>
155
156The inheritance hierarchy looks as follows:
157
158\image html classTH1__inherit__graph_org.svg width=100%
159
160\anchor creating-histograms
161## Creating histograms
162
163Histograms are created by invoking one of the constructors, e.g.
4079/// Fit histogram with the function pointer f1.
4080///
4081/// \param[in] f1 pointer to the function object
4082/// \param[in] option string defining the fit options (see table below).
4083/// \param[in] goption specify a list of graphics options. See TH1::Draw for a complete list of these options.
4084/// \param[in] xxmin lower fitting range
4085/// \param[in] xxmax upper fitting range
4086/// \return A smart pointer to the TFitResult class
4087///
4088/// \anchor HFitOpt
4089/// ### Histogram Fitting Options
4090///
4091/// Here is the full list of fit options that can be given in the parameter `option`.
4092/// Several options can be used together by concatanating the strings without the need of any delimiters.
4093///
4094/// option | description
4095/// -------|------------
4096/// "L" | Uses a log likelihood method (default is chi-square method). To be used when the histogram represents counts.
4097/// "WL" | Weighted log likelihood method. To be used when the histogram has been filled with weights different than 1. This is needed for getting correct parameter uncertainties for weighted fits.
4098/// "P" | Uses Pearson chi-square method. Uses expected errors instead of the observed one (default case). The expected error is instead estimated from the square-root of the bin function value.
4099/// "MULTI" | Uses Loglikelihood method based on multi-nomial distribution. In this case the function must be normalized and one fits only the function shape.
4100/// "W" | Fit using the chi-square method and ignoring the bin uncertainties and skip empty bins.
4101/// "WW" | Fit using the chi-square method and ignoring the bin uncertainties and include the empty bins.
4102/// "I" | Uses the integral of function in the bin instead of the default bin center value.
4103/// "F" | Uses the default minimizer (e.g. Minuit) when fitting a linear function (e.g. polN) instead of the linear fitter.
4104/// "U" | Uses a user specified objective function (e.g. user providedlikelihood function) defined using `TVirtualFitter::SetFCN`
4105/// "E" | Performs a better parameter errors estimation using the Minos technique for all fit parameters.
4106/// "M" | Uses the IMPROVE algorithm (available only in TMinuit). This algorithm attempts improve the found local minimum by searching for a better one.
4107/// "S" | The full result of the fit is returned in the `TFitResultPtr`. This is needed to get the covariance matrix of the fit. See `TFitResult` and the base class `ROOT::Math::FitResult`.
4108/// "Q" | Quiet mode (minimum printing)
4109/// "V" | Verbose mode (default is between Q and V)
4110/// "+" | Adds this new fitted function to the list of fitted functions. By default, the previous function is deleted and only the last one is kept.
4111/// "N" | Does not store the graphics function, does not draw the histogram with the function after fitting.
4112/// "0" | Does not draw the histogram and the fitted function after fitting, but in contrast to option "N", it stores the fitted function in the histogram list of functions.
4113/// "R" | Fit using a fitting range specified in the function range with `TF1::SetRange`.
4114/// "B" | Use this option when you want to fix or set limits on one or more parameters and the fitting function is a predefined one (e.g gaus, expo,..), otherwise in case of pre-defined functions, some default initial values and limits will be used.
4115/// "C" | In case of linear fitting, do no calculate the chisquare (saves CPU time).
4116/// "G" | Uses the gradient implemented in `TF1::GradientPar` for the minimization. This allows to use Automatic Differentiation when it is supported by the provided TF1 function.
4117/// "WIDTH" | Scales the histogran bin content by the bin width (useful for variable bins histograms)
4118/// "SERIAL" | Runs in serial mode. By default if ROOT is built with MT support and MT is enables, the fit is perfomed in multi-thread - "E" Perform better Errors estimation using Minos technique
4119/// "MULTITHREAD" | Forces usage of multi-thread execution whenever possible
4120///
4121/// The default fitting of an histogram (when no option is given) is perfomed as following:
4122/// - a chi-square fit (see below Chi-square Fits) computed using the bin histogram errors and excluding bins with zero errors (empty bins);
4123/// - the full range of the histogram is used, unless TAxis::SetRange or TAxis::SetRangeUser was called before;
4124/// - the default Minimizer with its default configuration is used (see below Minimizer Configuration) except for linear function;
4125/// - for linear functions (`polN`, `chenbyshev` or formula expressions combined using operator `++`) a linear minimization is used.
4126/// - only the status of the fit is returned;
4127/// - the fit is performed in Multithread whenever is enabled in ROOT;
4128/// - only the last fitted function is saved in the histogram;
4129/// - the histogram is drawn after fitting overalyed with the resulting fitting function
4130///
4131/// \anchor HFitMinimizer
4132/// ### Minimizer Configuration
4133///
4134/// The Fit is perfomed using the default Minimizer, defined in the `ROOT::Math::MinimizerOptions` class.
4135/// It is possible to change the default minimizer and its configuration parameters by calling these static functions before fitting (before calling `TH1::Fit`):
4136/// - `ROOT::Math::MinimizerOptions::SetDefaultMinimizer(minimizerName, minimizerAgorithm)` for changing the minmizer and/or the corresponding algorithm.
4137/// For example `ROOT::Math::MinimizerOptions::SetDefaultMinimizer("GSLMultiMin","BFGS");` will set the usage of the BFGS algorithm of the GSL multi-dimensional minimization
4138/// The current defaults are ("Minuit","Migrad").
4139/// See the documentation of the `ROOT::Math::MinimizerOptions` for the available minimizers in ROOT and their corresponding algorithms.
4140/// - `ROOT::Math::MinimizerOptions::SetDefaultTolerance` for setting a different tolerance value for the minimization.
4141/// - `ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls` for setting the maximum number of function calls.
4142/// - `ROOT::Math::MinimizerOptions::SetDefaultPrintLevel` for changing the minimizer print level from level=0 (minimal printing) to level=3 maximum printing
4143///
4144/// Other options are possible depending on the Minimizer used, see the corresponding documentation.
4145/// The default minimizer can be also set in the resource file in etc/system.rootrc. For example
4146///
4147/// ~~~ {.cpp}
4148/// Root.Fitter: Minuit2
4149/// ~~~
4150///
4151/// \anchor HFitChi2
4152/// ### Chi-square Fits
4153///
4154/// By default a chi-square (least-square) fit is performed on the histogram. The so-called modified least-square method
4155/// is used where the residual for each bin is computed using as error the observed value (the bin error) returned by `TH1::GetBinError`
4267/// TMinuit returns 0 (for migrad, minos, hesse or improve) in case of success and 4 in case of error (see the documentation of TMinuit::mnexcm). For example, for an error
4268/// only in Minos but not in Migrad a fitStatus of 40 will be returned.
4269/// Minuit2 returns 0 in case of success and different values in migrad,minos or
4270/// hesse depending on the error. See in this case the documentation of
4271/// Minuit2Minimizer::Minimize for the migrad return status, Minuit2Minimizer::GetMinosError for the
4272/// minos return status and Minuit2Minimizer::Hesse for the hesse return status.
4273/// If other minimizers are used see their specific documentation for the status code returned.
4274/// For example in the case of Fumili, see TFumili::Minimize.
4275///
4276/// \anchor HFitRange
4277/// ### Fitting in a range
4278///
4279/// In order to fit in a sub-range of the histogram you have two options:
4280/// - pass to this function the lower (`xxmin`) and upper (`xxmax`) values for the fitting range;
4281/// - define a specific range in the fitted function and use the fitting option "R".
4282/// For example, if your histogram has a defined range between -4 and 4 and you want to fit a gaussian
4283/// only in the interval 1 to 3, you can do:
4284///
4285/// ~~~ {.cpp}
4286/// TF1 *f1 = new TF1("f1", "gaus", 1, 3);
4287/// histo->Fit("f1", "R");
4288/// ~~~
4289///
4290/// The fitting range is also limited by the histogram range defined using TAxis::SetRange
4291/// or TAxis::SetRangeUser. Therefore the fitting range is the smallest range between the
4292/// histogram one and the one defined by one of the two previous options described above.
4293///
4294/// \anchor HFitInitial
4295/// ### Setting initial conditions
4296///
4297/// Parameters must be initialized before invoking the Fit function.
4298/// The setting of the parameter initial values is automatic for the
4299/// predefined functions such as poln, expo, gaus, landau. One can however disable
4300/// this automatic computation by using the option "B".
4301/// Note that if a predefined function is defined with an argument,
4302/// eg, gaus(0), expo(1), you must specify the initial values for
4303/// the parameters.
4304/// You can specify boundary limits for some or all parameters via
4347/// st->SetX1NDC(newx1); //new x start position
4348/// st->SetX2NDC(newx2); //new x end position
4349///
4350/// \anchor HFitExtra
4351/// ### Additional Notes on Fitting
4352///
4353/// #### Fitting a histogram of dimension N with a function of dimension N-1
4354///
4355/// It is possible to fit a TH2 with a TF1 or a TH3 with a TF2.
4356/// In this case the chi-square is computed from the squared error distance between the function values and the bin centers weighted by the bin content.
4357/// For correct error scaling, the obtained parameter error are corrected as in the case when the
4358/// option "W" is used.
4359///
4360/// #### User defined objective functions
4361///
4362/// By default when fitting a chi square function is used for fitting. When option "L" is used
4363/// a Poisson likelihood function is used. Using option "MULTI" a multinomial likelihood fit is used.
4364/// Thes functions are defined in the header Fit/Chi2Func.h or Fit/PoissonLikelihoodFCN and they
4365/// are implemented using the routines FitUtil::EvaluateChi2 or FitUtil::EvaluatePoissonLogL in
4366/// the file math/mathcore/src/FitUtil.cxx.
4367/// It is possible to specify a user defined fitting function, using option "U" and
6986/// Smooth array xx, translation of Hbook routine `hsmoof.F`.
6987/// Based on algorithm 353QH twice presented by J. Friedman
6988/// in [Proc. of the 1974 CERN School of Computing, Norway, 11-24 August, 1974](https://cds.cern.ch/record/186223).
6989/// See also Section 4.2 in [J. Friedman, Data Analysis Techniques for High Energy Physics](https://www.slac.stanford.edu/pubs/slacreports/reports16/slac-r-176.pdf).