KAHIBARO
Discord Login Register

11.3. Polynomial Fits

Linear fits

Polynomial fitting in ROOT is most commonly done with the generic polynomial function class TF1("polN", ...), where N is the degree of the polynomial. The simplest case is a linear fit, which corresponds to pol1 in ROOT. The model is
$$
f(x) = p_0 + p_1 x
$$
where p0 is the intercept and p1 is the slope.

For a histogram h, a typical linear fit in the current X axis range looks like:

cpp
h->Fit("pol1");

ROOT internally constructs a TF1 with name "pol1" and adjusts its parameters to minimize the chi square. If you want to restrict the fit to a particular range, you can either set a range on the histogram before fitting, or create your own TF1 with explicit limits:

cpp
TF1 *f_lin = new TF1("f_lin", "pol1", x_min, x_max);
h->Fit("f_lin", "R");  // "R" tells ROOT to use the TF1 range

The same idea applies to fitting a TGraph or TGraphErrors:

cpp
TGraph *g = ...;
g->Fit("pol1");

After the fit, you can inspect the parameters through the TF1 object:

cpp
TF1 *fit = h->GetFunction("pol1");
double intercept = fit->GetParameter(0);  // p0
double slope     = fit->GetParameter(1);  // p1
double e_intercept = fit->GetParError(0);
double e_slope     = fit->GetParError(1);

For linear fits with ROOT:

  1. Use "pol1" as the built-in model.
  2. Parameter index 0 is the intercept, parameter index 1 is the slope.
  3. Use the option "R" and a TF1 with explicit limits to control the fit range.

Be careful that the quality of a linear fit depends strongly on the chosen X interval. A straight line can approximate many shapes over a small region, so always check that the linear model is physically or logically justified for your data in that range, and inspect residuals rather than trusting the line visually.

Quadratic fits

Quadratic fits use the pol2 model, which has three parameters:
$$
f(x) = p_0 + p_1 x + p_2 x^2.
$$
This allows you to capture simple curvature in your data, such as parabolic peaks or troughs.

To fit a histogram with a quadratic function over its default range:

cpp
h->Fit("pol2");

For more control, define an explicit TF1:

cpp
TF1 *f_quad = new TF1("f_quad", "pol2", x_min, x_max);
h->Fit("f_quad", "R");

The meaning of the parameters is fixed by the polynomial definition:

You can extract parameters and their uncertainties just as for the linear case:

cpp
TF1 *fit = h->GetFunction("f_quad");
double p0 = fit->GetParameter(0);
double p1 = fit->GetParameter(1);
double p2 = fit->GetParameter(2);
double e_p2 = fit->GetParError(2);

Quadratic fits are particularly useful to approximate a smooth maximum or minimum when you do not want to assume a specific physical line shape. For example, to find the position of a smooth peak you can fit with pol2 in a limited window around the maximum. The extremum of the fitted parabola is at
$$
x_{\text{ext}} = -\frac{p_1}{2 p_2}
$$
provided p2 is not zero.

In a quadratic fit with pol2:

  1. The model is $f(x) = p_0 + p_1 x + p_2 x^2$.
  2. Parameter 2 controls the curvature, and its sign determines if the parabola opens up or down.
  3. The extremum of the parabola is at $x_{\text{ext}} = -p_1/(2 p_2)$, which you must compute from the fit parameters.

When using pol2, always restrict the fit to a region where a quadratic approximation is reasonable. Over a very wide range, even a small higher order structure in the true data can make a simple parabola a poor model and distort the extracted extremum or other derived quantities.

Higher-order polynomials

Higher-order polynomial fits generalize the previous cases to arbitrary degree. ROOT provides built-in polynomial functions "polN" where N can be 0, 1, 2, 3, and so on. The general model is
$$
f(x) = \sum_{k=0}^{N} p_k x^k
$$
with N + 1 free parameters. For instance, a cubic polynomial pol3 has
$$
f(x) = p_0 + p_1 x + p_2 x^2 + p_3 x^3.
$$

To perform a cubic fit over a chosen interval:

cpp
TF1 *f_cubic = new TF1("f_cubic", "pol3", x_min, x_max);
h->Fit("f_cubic", "R");

For an even higher order, say order 5:

cpp
TF1 *f5 = new TF1("f5", "pol5", x_min, x_max);
h->Fit("f5", "R");

You can also fit graphs with higher-order polynomials in exactly the same way:

cpp
TGraphErrors *g = ...;
g->Fit("pol3");               // simple cubic fit
g->Fit("pol4", "R");          // quartic in the graph range

For large N, the parameters individually have less obvious physical meaning. Instead of interpreting each coefficient, you usually use high-order polynomials as a flexible empirical model for a smooth background or a slowly varying trend. ROOT still provides parameter values, uncertainties, and correlations, but the main information is often the fitted curve itself.

Higher-order fits can be powerful, but they come with two important caveats.

First, there is a risk of overfitting. With many free parameters, a polynomial can reproduce small statistical fluctuations in the data rather than the underlying trend. This often leads to a curve that oscillates between points, especially near the edges of the fitted range, and to unstable extrapolation beyond the fitted region.

Second, numerical stability can degrade as the order increases, especially if the X values are very large or the fit range is very wide. The powers of x can become huge and highly correlated, which makes the parameter estimates more sensitive to small changes in the data.

When using higher-order polynomials in ROOT:

  1. "polN" has $N+1$ parameters and models $f(x) = \sum_{k=0}^{N} p_k x^k$.
  2. Always limit the fit to a range where such a flexible polynomial is justified.
  3. More parameters increase the risk of overfitting and numerical instability, especially for wide ranges or large X values.

To reduce these issues, keep the polynomial order as low as possible, work in a limited X interval, and choose an order that is motivated by the physics or by clear structure in the data rather than by the desire to minimize chi square at all costs. After fitting, always inspect the residuals and, if possible, compare different polynomial orders to see whether the extra complexity is really needed.

Views: 12

Comments

Please login to add a comment.

Don't have an account? Register now!