Fit

Function and classes for fitting histograms

Base Fit class

This class define generic methods


source

Fit

 Fit (name='fit', h=None, fit_range=None, npx:int=1000, invert:bool=False,
      par_names=None)

general class for fitting histograms

Type Default Details
name str fit Name of the fitting function
h NoneType None histogram to fit
fit_range NoneType None list or tuple of fit range borders
npx int 1000 Number of points used to draw the fit function
invert bool False
par_names NoneType None List of parameter names of the fitting function

source

Fit.init_fit

 Fit.init_fit ()

returns root TF1 fit function

Placeholders

Place holder functions, will be overwritten in the child classes


source

Fit.find_fit_range

 Fit.find_fit_range ()

placeholder to be overwritten in child classes, here is simply returns [0, 1000]


source

Fit.get_par_names

 Fit.get_par_names ()

placeholder to be overwritten in child classes, here is simply returns []

Setters

functions that set parameter name, limits, start values, as well as clears previous fit functions


source

Fit.set_par_names

 Fit.set_par_names ()

Sets fit function parameter names by passing a list of parameter names to the TF1.SetParNames() method


source

Fit.set_par_limits

 Fit.set_par_limits ()

placeholder to be overwritten in child classes


source

Fit.set_start_values

 Fit.set_start_values ()

placeholder to be overwritten in child classes


source

Fit.set_parameters

 Fit.set_parameters (*pars)

Sets fit function parameters by passing a parameter list to the TF1.SetParameters() method


source

Fit.clear_old

 Fit.clear_old ()

deletes the previous fit function

Getters

functions that access internal state of the fit class such as parameters and the results of the fit


source

Fit.get_parameter

 Fit.get_parameter (i:int)

returns the value and the error of a parameter

Type Details
i int index of the parameter to get

source

Fit.get_parameters

 Fit.get_parameters ()

returns a list of parameter values


source

Fit.from_hist

 Fit.from_hist (h)

When Fit (TH1:Fit) is invoked, the fitted function is added to the list of function associated with this histogram. Given an histogram h, one can retrieve an associated function

Details
h histogram

source

Fit.get_chi2

 Fit.get_chi2 ()

returns chi2 of the fit divided by the number of degrees of freedom


source

Fit.formula

 Fit.formula ()

return the formula of the fit function

Info functions

Example:

f = Fit()
f
Fit Fit: fit with 0 parameters

source

Fit.print_parameters

 Fit.print_parameters ()

prints parameters names and values

Worker function


source

Fit.fit

 Fit.fit (n:int=1, draw=True, minuit=True, fl=0, fh=0)

The method performs the fit and optionally draws fit function overlayed on the histogram

Type Default Details
n int 1 number of iterations
draw bool True Overlay the fitting function on histogram
minuit bool True Use Minuit2 and Migrad for minimization
fl int 0 lower fitting range
fh int 0 upper fitting range

Display function


source

Fit.draw

 Fit.draw (*args, **kwargs)

plots the fit functions


source

make_fit

 make_fit (h, f, xmin=0, xmax=1, start_values=None, par_names=None,
           name=None, npx=1000)

factory method for making NewFit classes that are inherit from the Fit class

Type Default Details
h histogram to fit
f fitting function
xmin int 0 lower fitting range
xmax int 1 upper fitting range
start_values NoneType None list initial values
par_names NoneType None list of parameter names
name NoneType None name of the fitting function
npx int 1000 number of points used to draw the fitting function

Specific fit classes


source

PoissonI

 PoissonI (h=None, fit_range=None, npx:int=1000, p0:float=None,
           p1:float=None)

general class for fitting histograms

Type Default Details
h NoneType None histogram to fit
fit_range NoneType None list with the low and high values of the fitting range
npx int 1000 number of points to use when drawing the fitting function
p0 float None initial value for constant parameter
p1 float None initial value for lambda parameter

source

Expo

 Expo (h=None, xmin=None, xmax=None, npx=100)

general class for fitting histograms


source

gauss

 gauss (x, scale, mean_, sigma, off=0)

source

Gauss

 Gauss (h=None, fit_range=None, npx=100, fl=3, fh=3, thresh=0.01, **fkw)

general class for fitting histograms


source

Landau

 Landau (h=None, fit_range=None, npx=100)

general class for fitting histograms

Type Default Details
h NoneType None histogram to fit
fit_range NoneType None
npx int 100

source

Erf

 Erf (h=None, fit_range=None, pars=None, npx=100)

general class for fitting histograms


source

crystalball

 crystalball (x, pars, inv=False)

source

Crystalball

 Crystalball (h=None, fit_range=None, inv=False, npx=1000)

general class for fitting histograms

# crystal_ball = Crystalball(fit_range=[-10, 10])
# crystal_ball.draw()
# a = get_last_canvas()
# a.draw()

source

erfland

 erfland (x, pars)

retruns error function merged with Landau

# from ROOT import TCanvas, TFormula, TF1
# from ROOT import gROOT, gObjectTable
# %jsroot on

# c = TCanvas( 'c', 'Example with Formula', 200, 10, 700, 500 )

# fun1 = TF1( 'fun1', erfland, 0, 10, 8)
# c0, mpv, sigma, c1, xoff, w, yoff = 1, 7, 2, 1, 3, 2, 0
# fun1.SetParameters(c0, mpv, sigma, c1, xoff, w, yoff)
# fun1 = TF1( 'fun1', 'abs(sin(x)/x)', 0, 10 )
# c.SetGridx()
# c.SetGridy()
# fun1.Draw()

# c.Update()

source

ErfLand

 ErfLand (h=None, fit_range=None, npx=1000)

general class for fitting histograms


source

Langau

 Langau (h=None, nconv:int=100, fit_range=None, npx:int=1000)

general class for fitting histograms

Type Default Details
h NoneType None histogram to fit
nconv int 100 number of convolutions within the range
fit_range NoneType None fitting range
npx int 1000 number of points to draw the function

source

NLandau

 NLandau (h=None, fit_range=None, npx:int=100, n:int=3)

general class for fitting histograms

Type Default Details
h NoneType None histogram to fit
fit_range NoneType None fitting range
npx int 100 number of points to draw the fitting function
n int 3 number of Landaus

source

langau

 langau (x:float, pars, nconv:int, nsigma:int=5)

Convoluted Landau and Gaussian Fitting Function (using ROOT’s Landau and Gauss functions)

Fit parameters: par[0]=Width (scale) parameter of Landau density par[1]=Most Probable (MP, location) parameter of Landau density par[2]=Total area (integral -inf to inf, normalization constant) par[3]=Width (sigma) of convoluted Gaussian function

In the Landau distribution (represented by the CERNLIB approximation), the maximum is located at x=-0.22278298 with the location parameter=0. This shift is corrected within this function, so that the actual maximum is identical to the MP parameter.

The code is literally converted to python from the original C at https://root.cern.ch/root/html404/examples/langaus.C.html

Type Default Details
x float independent variable
pars list of langau fit parameters
nconv int number of convolutions
nsigma int 5 number of sigmas, extent of the convolution intergral

source

langaupro

 langaupro (params, maxx, FWHM)

Seaches for the location (x value) at the maximum of the Landau-Gaussian convolute and its full width at half-maximum.

The search is probably not very efficient, but it’s a first try.

The code is literally converted to python from the original C at https://root.cern.ch/root/html404/examples/langaus.C.html


source

main

 main ()