22.8. Fit Failures
Table of Contents
Understanding Fit Failures in ROOT
Fitting is central to many analyses, and when it fails it can be confusing. This chapter focuses on how fit failures typically appear in ROOT, how to diagnose their cause, and practical strategies to fix them. It assumes you already know the basics of fitting from the curve fitting chapter, and concentrates on debugging and recovery.
How Fit Failures Look in ROOT
When a fit fails, ROOT rarely produces a single clear error message. Instead, you usually see a combination of:
Text messages printed in the ROOT terminal, often from MINUIT or Fumili (the minimizers used by ROOT). Examples include “ERROR: Failed to converge,” “HESSE fails,” “Matrix not positive-definite,” or “EDM is above tolerance.”
Fit results with strange parameter values. For example, parameters at the allowed limits, absurdly large errors, or parameters that jump to unphysical values.
A fit curve that clearly does not follow the data, or a function that does not appear at all on the plot.
NaN or Inf values in the covariance matrix or parameter errors, or a null pointer returned for some operations.
A chi-square or likelihood value that is obviously unreasonable given the data.
Whenever a fit behaves oddly, always read the terminal output from ROOT. It contains hints about why the fit did not succeed, even if the graphical output looks somewhat reasonable.
Common Causes of Fit Failures
Many different issues can cause the minimizer to fail or to find a meaningless result. The most common categories are poor starting values, inappropriate fit range, incompatible fit model, numerical problems in the function, statistics issues, and conflicts between error definitions and fit options.
Small variations of these issues can produce similar symptoms, so it is useful to think in terms of a few broad patterns and then test them systematically.
Parameter Initialization Problems
ROOT’s minimizers start from the initial parameter values that you give to TF1 or to the fitting interface of a histogram or graph. If these starting values are very far from any reasonable minimum, the algorithm may:
Fail to converge within the maximum number of iterations.
Get stuck in a local minimum that does not describe the data.
Drive parameters into regions where your function misbehaves, such as returning NaN or Inf.
You can recognize initialization problems when small changes in the starting values dramatically change the result, or when the fit only succeeds if you manually provide “good” initial parameters.
To diagnose initialization issues, always inspect the starting parameters and ask whether they reflect the rough shape of the data. For example, for a Gaussian fit, the initial mean should be close to the visible peak, the initial sigma somewhat similar to the peak width, and the amplitude near the peak height or the area. If the automatic guesses are far off, set them explicitly before calling Fit.
Complex custom functions are especially sensitive to starting values. Whenever you define a multi-parameter function, test it by evaluating TF1::Eval at a few points with your initial parameters to check that it returns sensible values across the region you will fit.
Important rule: Always set physically reasonable initial parameter values for TF1 before fitting, especially for multi-parameter or custom functions. Poor initialization is one of the most frequent causes of ROOT fit failures.
Parameter Limits and Constrained Fits
Parameter limits are often used to keep a fit physically meaningful, for example to enforce positive widths or to keep a rate between 0 and 1. However, overly tight or inconsistent limits can prevent the minimizer from reaching a good solution.
Typical problems include:
Parameters stuck at bounds. If a parameter’s fitted value is exactly equal to a limit, the minimizer likely wanted to go beyond it. The fit result is then suspect.
Impossible combinations of limits. For example, a constraint that indirectly implies a negative variance or an impossible relation among parameters.
Very wide limits that allow the minimizer to explore extreme regions where the function or its derivatives behave poorly.
You can identify limit-related issues by printing parameters after the fit and checking whether any are equal to their set limits. If so, relax these bounds or temporarily remove them and repeat the fit. You can also examine the terminal messages; MINUIT often warns about parameters at limits or non-positive-definite covariance matrices.
It is often safer to use mild but physically motivated bounds rather than extremely tight restrictions. For example, for a Gaussian width, you might set a lower bound slightly above zero but keep the upper bound loose enough to accommodate the data.
Important rule: If a fit parameter ends up exactly at its limit, do not trust the result. Loosen or remove the limits and refit to check whether the minimum was artificially constrained.
Fit Range and Data Selection Issues
The fit range plays a critical role in convergence and stability. Several common issues arise from inappropriate choice of range or data selection:
Too broad a range. If you fit a simple model to an interval where the data have structure the model cannot describe, the minimizer may try to compromise across incompatible regions and never converge well.
Too narrow a range. If the range contains too few points or does not include enough of the characteristic shape, parameters become poorly constrained, leading to large uncertainties or unstable values.
Range inconsistent with the function definition. If the function has singularities or undefined values in the chosen range, the fitting algorithm may fail immediately or generate NaNs.
In histogram fits, you must consider bin statistics. If the range includes many empty bins or bins with very low counts, the chosen error model (for example chi-square based on Gaussian approximations) can become inappropriate and lead to unreliable chi-square values and unstable minimization.
If a fit fails or produces unreasonable results, experiment with changing the range. Visual inspection is essential: restrict the fit to the region where your model is expected to hold and where the data show the corresponding behavior. For example, when fitting a narrow peak, exclude distant background regions if you are not modeling the background yet.
Important rule: Choose the fit range to match the validity region of your model. Do not blindly fit over the full plotted range if the model does not apply everywhere.
Model Mismatch and Overfitting
A fit will also fail conceptually when the model you chose is simply not appropriate for the data. This may still produce a “converged” result numerically, but it is effectively a failure from the analysis point of view.
Common symptoms include:
Structured residuals. The difference between data and fit shows clear patterns rather than random fluctuations, indicating missing components in the model.
Systematically poor chi-square or likelihood across reasonable variations of the range or starting values.
Parameter values that are unphysical or disagree strongly with independent expectations, even when the curve passes through the data points.
Overly flexible models. Very high-order polynomials or functions with many free parameters can adapt to statistical noise instead of capturing the underlying trend, which can cause wild behavior outside the central region or unstable parameters.
To debug possible model mismatch, look at residual plots and reduced chi-square. If you repeatedly observe structured residuals, consider adding physically motivated terms to the function, or simplifying it if it has too many degrees of freedom. Also compare fits with alternative models and use goodness-of-fit metrics to decide which is more appropriate.
While this chapter does not re-teach model building, remember that the minimizer cannot compensate for a fundamentally wrong functional form. A numerically perfect minimum of a poor model is still a bad fit.
Problems with Bin Statistics and Fit Options
For histogram fits, ROOT gives you options that control how errors and statistics are interpreted, such as using chi-square fits or likelihood fits. Misuse of these options is a common reason for fit instability or nonsense uncertainties.
When the per-bin counts are large, a chi-square fit is appropriate and the default error model based on $\sigma_i = \sqrt{N_i}$ (with $N_i$ the bin content) works reasonably well. However, for low-statistics bins, the Gaussian approximation implicit in chi-square can break down and the fit can be biased or unstable.
If you fit a histogram that contains many empty or low-count bins using a simple chi-square, you may see warnings about zero errors, artificial weighting of certain bins, or fits that appear very sensitive to small changes in content.
ROOT allows likelihood-based fits, which are often preferable for low counts, but they require appropriate options and an understanding of the statistical meaning. If you choose a likelihood fit without understanding the underlying assumptions, misinterpretation of results is easy.
Always verify that the fit option you use matches the nature of your data. If you are working with sparse histograms, consider using likelihood-based options designed for Poisson statistics, and be cautious when interpreting chi-square. Also, confirm that bin errors are defined consistently, for example if you previously scaled the histogram or used Sumw2 to store weighted bin errors.
Important rule: For low-statistics histograms, standard chi-square fits can be unreliable. Use fit options consistent with Poisson statistics or reconsider the binning and model.
Numerical Instabilities in Custom Functions
Custom C++ functions and complex TF1 expressions are a powerful feature but they introduce many new ways for fits to fail. Numeric instabilities typically arise from:
Division by quantities that can be zero or extremely small in part of the fit range.
Taking logarithms, square roots, or other functions with restricted domains, where the argument sometimes becomes negative or zero.
Exponentials that overflow or underflow in parts of the parameter space.
Discontinuous logic or piecewise definitions that create sharp transitions or undefined derivatives.
If the minimizer evaluates your function at a parameter combination that causes NaN or Inf values, it may abort or produce invalid gradients, leading to failures or nonsensical parameter updates.
To debug numerical issues, evaluate the function explicitly in ROOT over the intended range for several sets of parameters, including values somewhat away from your nominal guesses. Check for NaNs or infinities and ensure the function remains finite and smooth.
You can make your function more robust by:
Adding small regularization terms to denominators.
Clipping arguments to logarithms and square roots to safe ranges in a way that preserves physical meaning.
Avoiding extremely steep exponentials by rescaling variables or parameters.
Introducing piecewise definitions that are continuous and differentiable where possible.
Important rule: Before fitting, always test custom TF1 functions numerically across the intended range. If they ever produce NaN or Inf, fix the function definition before relying on any fit.
Covariance Matrix and Error Problems
A frequent outcome of problematic fits is an ill-conditioned or non-positive-definite covariance matrix. You may see messages about the covariance matrix not being positive-definite, impossibility of computing reliable errors, or HESSE or MINOS failures.
Such problems often indicate that:
Parameters are highly correlated, so small changes in one can be compensated by changes in another, making the minimum flat in some directions.
The data do not constrain some parameters, for example because the model is too flexible or the range is too small.
Parameter values are at or near their limits, distorting the local curvature of the likelihood or chi-square surface.
There is a hidden numerical issue in the function, such as a plateau or discontinuity.
When ROOT reports that the covariance matrix is not valid, treat the parameter uncertainties as unreliable even if the central values look reasonable. Try to reduce correlations by simplifying the model, fixing some parameters to known values, or changing the parametrization. You can also restrict the range to focus on the most informative part of the data or add independent constraints.
It is often instructive to vary one parameter manually while keeping others fixed, using Eval to see how sensitive the function is to that parameter. Nearly flat behavior in the region of the minimum signals that the parameter cannot be determined well from the data.
Important rule: If ROOT warns about an invalid or non-positive-definite covariance matrix, do not trust the reported parameter errors. Simplify the model, fix poorly constrained parameters, or adjust the fit strategy.
Debugging Strategy and Practical Tips
When a fit fails or produces suspicious results, follow a structured debugging process rather than trying random changes.
First, inspect the plot visually and read all fit messages in the terminal. Identify whether the failure is clear (no convergence, error messages) or subtle (odd parameters, structured residuals).
Second, check the basics for histograms or graphs. Ensure the data object is correctly filled, the axis ranges are what you expect, and there are enough points or bins in the chosen fit range.
Third, simplify the problem. Try a simpler model or restrict the range to the most prominent feature. If a Gaussian plus background fails, test a plain Gaussian on the central region, then gradually add complexity if that works.
Fourth, control the starting values. Set initial parameters by hand based on visual estimates from the plot. For histograms, you can approximate the peak position, height, and width by inspection, then assign those to the TF1 before fitting.
Fifth, check the function numerically. Evaluate the TF1 at several points and with different parameter values, looking for NaNs, infinities, or unphysical values. Make sure the function is finite and smooth across the entire fit range.
Sixth, review parameter limits and correlation. Remove or relax overly strict bounds, and consider fixing parameters that are known from other measurements or that the data do not clearly constrain.
Finally, once the fit appears to converge, examine residuals, chi-square or likelihood, parameter correlations, and the covariance matrix. A numerically converged fit with poor residuals or invalid covariance still counts as a failure from an analysis perspective and should be refined.
Persistently difficult fits often indicate deeper issues with the model or data. In such cases, you may need to revisit your assumptions about the functional form, the binning, the selection cuts, or the statistical treatment before expecting ROOT to succeed.
Views: 13
KAHIBARO