LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lar::util::details::SimplePolyFitterBase< T, D > Class Template Referenceabstract

Base class providing virtual fitting interface for polynomial fitters. More...

#include "SimpleFits.h"

Inheritance diagram for lar::util::details::SimplePolyFitterBase< T, D >:
lar::util::details::SimpleFitterInterface< T, D+1 > lar::util::details::SimplePolyFitterDataBase< T, D >

Public Types

using Data_t = typename Base_t::Data_t
 type of the data More...
 
using FitParameters_t = typename Interface_t::FitParameters_t
 type of set of fit parameters More...
 
using FitMatrix_t = typename Interface_t::FitMatrix_t
 type of matrix for covariance (a std::array) More...
 
using MatrixOps = FastMatrixOperations< Data_t, NParams >
 
using Measurement_t = typename Collector_t::Measurement_t
 type of measurement without uncertainty More...
 
using MeasurementAndUncertainty_t = typename Collector_t::MeasurementAndUncertainty_t
 type of measurement with uncertainty More...
 

Public Member Functions

virtual bool isValid () const override
 Returns if the fit has valid results. More...
 
bool FillResults (FitParameters_t &params, FitMatrix_t &Xmat, Data_t &det, FitMatrix_t &Smat) const override
 Fills the specified parameters. More...
 
bool FillResults (FitParameters_t &params, FitParameters_t &paramerrors, FitMatrix_t &Xmat, Data_t &det, FitMatrix_t &Smat) const override
 Fills the specified parameters. More...
 
bool FillResults (FitParameters_t &params, FitParameters_t &paramerrors) const override
 Fills the specified parameters. More...
 
virtual Data_t Evaluate (Data_t x) const override
 Evaluates the fitted function at the specified point. More...
 
virtual bool FillResults (FitParameters_t &params, FitMatrix_t &Xmat, Data_t &det, FitMatrix_t &Smat) const =0
 Fills the specified parameters. More...
 
virtual bool FillResults (FitParameters_t &params, FitParameters_t &paramerrors, FitMatrix_t &Xmat, Data_t &det, FitMatrix_t &Smat) const =0
 Fills the specified parameters. More...
 
virtual bool FillResults (FitParameters_t &params, FitParameters_t &paramerrors) const =0
 Fills the specified parameters. More...
 
virtual Data_t Evaluate (Data_t x) const =0
 Evaluates the fitted function at the specified point. More...
 
Data_t operator() (Data_t x) const
 Evaluates the fitted function; alias of Evaluate() More...
 
void clear ()
 Clears all the statistics. More...
 
template<typename Stream >
void PrintStats (Stream &out) const
 Prints the collected statistics into a stream. More...
 
Fitting
virtual FitParameters_t FitParameters () const override
 Computes and returns all the parameters of the fit result. More...
 
virtual FitParameters_t FitParameterErrors () const override
 Computes and returns all the parameter errors of the fit result. More...
 
virtual FitMatrix_t FitParameterCovariance () const override
 Computes and returns all the covariance matrix of the fit result. More...
 
virtual Data_t FitParameter (unsigned int n) const override
 Returns the parameter n of the fit result. More...
 
virtual Data_t FitParameterError (unsigned int n) const override
 Returns the error on parameter n of the fit result. More...
 
virtual Data_t ChiSquare () const override
 Returns the $ \chi^{2} $ of the fit. More...
 
virtual int NDF () const override
 Returns the degrees of freedom in the determination of the fit. More...
 
Add elements
bool add (Data_t x, Data_t y, Data_t sy=Data_t(1.0))
 
bool add (Measurement_t value, Data_t sy=Data_t(1.0))
 
bool add (MeasurementAndUncertainty_t value)
 
template<typename Iter >
void add_without_uncertainty (Iter begin, Iter end)
 
template<typename Iter , typename Pred >
void add_without_uncertainty (Iter begin, Iter end, Pred extractor)
 
template<typename Cont , typename Pred >
void add_without_uncertainty (Cont cont, Pred extractor)
 
template<typename Cont >
void add_without_uncertainty (Cont cont)
 
template<typename VIter , typename UIter , typename VPred , typename UPred = identity>
unsigned int add_with_uncertainty (VIter begin_value, VIter end_value, UIter begin_uncertainty, VPred value_extractor, UPred uncertainty_extractor=UPred())
 
template<typename Iter >
unsigned int add_with_uncertainty (Iter begin, Iter end)
 
template<typename Cont >
unsigned int add_with_uncertainty (Cont cont)
 
Statistic retrieval
int N () const
 
Data_t AverageUncertainty () const
 

Static Public Member Functions

static FitParameters_t ExtractParameterErrors (FitMatrix_t const &Smat)
 Extracts parameter errors from diagonal of the covarriance matrix. More...
 
static constexpr Data_t sqr (Data_t v)
 Returns the square of the specified data value. More...
 
static constexpr Data_t cube (Data_t v)
 Returns the cube of the specified data value. More...
 
template<typename V >
static constexpr V sqr (V const &v)
 Returns the square of the specified value. More...
 

Static Public Attributes

static constexpr unsigned int Degree = Base_t::Degree
 Degree of the fit. More...
 
static constexpr unsigned int NParams = Interface_t::NParams
 Number of parameters in the fit. More...
 

Protected Member Functions

virtual FitMatrix_t MakeMatrixX () const
 Fills and returns the matrix of x^n sum coefficients ( { x^(i+j) } ) More...
 
virtual FitParameters_t MakeMatrixY () const
 Fills and returns the matrix (vector) of x^n y sum coefficients. More...
 
virtual FitParameters_t FitParameterErrors (FitMatrix_t const &Smat) const
 Computes and returns all the parameter errors of the fit result. More...
 
virtual Data_t Determinant (FitMatrix_t const &mat) const
 Computes the determinant of a matrix. More...
 
virtual FitMatrix_t InvertMatrix (FitMatrix_t const &mat, Data_t det) const
 Computes the inverse of a matrix (using provided determinant) More...
 
virtual FitMatrix_t InvertMatrix (FitMatrix_t const &mat) const
 Computes the inverse of a matrix. More...
 
virtual FitParameters_t MatrixProduct (FitMatrix_t const &mat, FitParameters_t const &vec) const
 Computes the product of a FitMatrix_t and a FitParameters_t. More...
 
Data_t XN (unsigned int n) const
 Returns the weighted sum of x^n. More...
 
Data_t XNY (unsigned int n) const
 Returns the weighted sum of x^n y. More...
 
virtual FitParameters_t FitParameters (FitMatrix_t const &Xmat) const
 Returns the fitted parameters using the provided information. More...
 
virtual FitParameters_t FitParameters (FitMatrix_t const &Smat, Data_t) const
 Returns the fitted parameters using the provided information. More...
 
virtual Data_t Param (unsigned int n, FitMatrix_t const &Xmat) const
 Computes a single fit parameter using the given information. More...
 
virtual Data_t Param (unsigned int n, FitMatrix_t const &Xmat, Data_t detXmat) const
 Computes a single fit parameter using the given information. More...
 

Protected Attributes

Collector_t stats
 statistics collected from fit data input More...
 

Private Types

using Interface_t = SimpleFitterInterface< T, D+1 >
 interface More...
 
using Base_t = SimplePolyFitterDataBase< T, D >
 class storing input More...
 

Detailed Description

template<typename T, unsigned int D>
class lar::util::details::SimplePolyFitterBase< T, D >

Base class providing virtual fitting interface for polynomial fitters.

Definition at line 635 of file SimpleFits.h.

Member Typedef Documentation

template<typename T, unsigned int D>
using lar::util::details::SimplePolyFitterBase< T, D >::Base_t = SimplePolyFitterDataBase<T, D>
private

class storing input

Definition at line 638 of file SimpleFits.h.

template<typename T, unsigned int D>
using lar::util::details::SimplePolyFitterBase< T, D >::Data_t = typename Base_t::Data_t

type of the data

Definition at line 649 of file SimpleFits.h.

template<typename T, unsigned int D>
using lar::util::details::SimplePolyFitterBase< T, D >::FitMatrix_t = typename Interface_t::FitMatrix_t

type of matrix for covariance (a std::array)

Definition at line 655 of file SimpleFits.h.

template<typename T, unsigned int D>
using lar::util::details::SimplePolyFitterBase< T, D >::FitParameters_t = typename Interface_t::FitParameters_t

type of set of fit parameters

Definition at line 652 of file SimpleFits.h.

template<typename T, unsigned int D>
using lar::util::details::SimplePolyFitterBase< T, D >::Interface_t = SimpleFitterInterface<T, D + 1>
private

interface

Definition at line 637 of file SimpleFits.h.

template<typename T , unsigned int D>
using lar::util::details::SimplePolyFitterDataBase< T, D >::Measurement_t = typename Collector_t::Measurement_t
inherited

type of measurement without uncertainty

Definition at line 352 of file SimpleFits.h.

template<typename T , unsigned int D>
using lar::util::details::SimplePolyFitterDataBase< T, D >::MeasurementAndUncertainty_t = typename Collector_t::MeasurementAndUncertainty_t
inherited

type of measurement with uncertainty

Definition at line 355 of file SimpleFits.h.

Member Function Documentation

template<typename T , unsigned int D>
bool lar::util::details::SimplePolyFitterDataBase< T, D >::add ( Data_t  x,
Data_t  y,
Data_t  sy = Data_t(1.0) 
)
inlineinherited

Definition at line 363 of file SimpleFits.h.

Referenced by cluster::ClusterParamsAlg::IntegrateFitCharge().

363 { return stats.add(x, y, sy); }
Float_t x
Definition: compare.C:6
Float_t y
Definition: compare.C:6
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
bool add(Data_t x, Data_t y, Data_t sy=Data_t(1.0))
Adds one entry with specified x, y and uncertainty.
Definition: SimpleFits.h:1412
template<typename T , unsigned int D>
bool lar::util::details::SimplePolyFitterDataBase< T, D >::add ( Measurement_t  value,
Data_t  sy = Data_t(1.0) 
)
inlineinherited

Definition at line 365 of file SimpleFits.h.

365 { return stats.add(value, sy); }
double value
Definition: spectrum.C:18
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
bool add(Data_t x, Data_t y, Data_t sy=Data_t(1.0))
Adds one entry with specified x, y and uncertainty.
Definition: SimpleFits.h:1412
template<typename T , unsigned int D>
bool lar::util::details::SimplePolyFitterDataBase< T, D >::add ( MeasurementAndUncertainty_t  value)
inlineinherited

Definition at line 367 of file SimpleFits.h.

367 { return stats.add(value); }
double value
Definition: spectrum.C:18
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
bool add(Data_t x, Data_t y, Data_t sy=Data_t(1.0))
Adds one entry with specified x, y and uncertainty.
Definition: SimpleFits.h:1412
template<typename T , unsigned int D>
template<typename VIter , typename UIter , typename VPred , typename UPred = identity>
unsigned int lar::util::details::SimplePolyFitterDataBase< T, D >::add_with_uncertainty ( VIter  begin_value,
VIter  end_value,
UIter  begin_uncertainty,
VPred  value_extractor,
UPred  uncertainty_extractor = UPred() 
)
inlineinherited

Definition at line 394 of file SimpleFits.h.

399  {
401  begin_value, end_value, begin_uncertainty, value_extractor, uncertainty_extractor);
402  }
unsigned int add_with_uncertainty(VIter begin_value, VIter end_value, UIter begin_uncertainty, VPred value_extractor, UPred uncertainty_extractor=UPred())
Adds measurements with uncertainties from a sequence.
Definition: SimpleFits.h:1442
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
template<typename Iter >
unsigned int lar::util::details::SimplePolyFitterDataBase< T, D >::add_with_uncertainty ( Iter  begin,
Iter  end 
)
inlineinherited

Definition at line 405 of file SimpleFits.h.

406  {
408  }
unsigned int add_with_uncertainty(VIter begin_value, VIter end_value, UIter begin_uncertainty, VPred value_extractor, UPred uncertainty_extractor=UPred())
Adds measurements with uncertainties from a sequence.
Definition: SimpleFits.h:1442
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
template<typename Cont >
unsigned int lar::util::details::SimplePolyFitterDataBase< T, D >::add_with_uncertainty ( Cont  cont)
inlineinherited

Definition at line 411 of file SimpleFits.h.

412  {
413  return stats.add_with_uncertainty(cont);
414  }
unsigned int add_with_uncertainty(VIter begin_value, VIter end_value, UIter begin_uncertainty, VPred value_extractor, UPred uncertainty_extractor=UPred())
Adds measurements with uncertainties from a sequence.
Definition: SimpleFits.h:1442
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
template<typename Iter >
void lar::util::details::SimplePolyFitterDataBase< T, D >::add_without_uncertainty ( Iter  begin,
Iter  end 
)
inlineinherited

Definition at line 370 of file SimpleFits.h.

371  {
373  }
void add_without_uncertainty(Iter begin, Iter end)
Adds measurements from a sequence, with no uncertainty.
Definition: SimpleFits.h:136
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
template<typename Iter , typename Pred >
void lar::util::details::SimplePolyFitterDataBase< T, D >::add_without_uncertainty ( Iter  begin,
Iter  end,
Pred  extractor 
)
inlineinherited

Definition at line 376 of file SimpleFits.h.

377  {
378  stats.add_without_uncertainty(begin, end, extractor);
379  }
void add_without_uncertainty(Iter begin, Iter end)
Adds measurements from a sequence, with no uncertainty.
Definition: SimpleFits.h:136
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
template<typename Cont , typename Pred >
void lar::util::details::SimplePolyFitterDataBase< T, D >::add_without_uncertainty ( Cont  cont,
Pred  extractor 
)
inlineinherited

Definition at line 382 of file SimpleFits.h.

383  {
384  stats.add_without_uncertainty(cont, extractor);
385  }
void add_without_uncertainty(Iter begin, Iter end)
Adds measurements from a sequence, with no uncertainty.
Definition: SimpleFits.h:136
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
template<typename Cont >
void lar::util::details::SimplePolyFitterDataBase< T, D >::add_without_uncertainty ( Cont  cont)
inlineinherited

Definition at line 388 of file SimpleFits.h.

389  {
391  }
void add_without_uncertainty(Iter begin, Iter end)
Adds measurements from a sequence, with no uncertainty.
Definition: SimpleFits.h:136
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
Data_t lar::util::details::SimplePolyFitterDataBase< T, D >::AverageUncertainty ( ) const
inlineinherited

Definition at line 427 of file SimpleFits.h.

427 { return stats.AverageUncertainty(); }
Data_t AverageUncertainty() const
Returns an average of the uncertainties.
Definition: SimpleFits.h:280
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::ChiSquare ( ) const
overridevirtual

Returns the $ \chi^{2} $ of the fit.

Returns
the $ \chi^{2} $ of the fit (not divided by NDF())

Implements lar::util::details::SimpleFitterInterface< T, D+1 >.

Reimplemented in lar::util::QuadraticFit< T >, and lar::util::LinearFit< T >.

Definition at line 1678 of file SimpleFits.h.

1679 {
1680  // the generic implementation of ChiSquare from sums is complex enough that
1681  // I freaked out
1682  throw std::logic_error("SimplePolyFitterBase::ChiSquare() not implemented for generic fit");
1683 } // SimplePolyFitterBase<>::ChiSquare()
template<typename T , unsigned int D>
void lar::util::details::SimplePolyFitterDataBase< T, D >::clear ( )
inlineinherited

Clears all the statistics.

Definition at line 419 of file SimpleFits.h.

419 { stats.clear(); }
void clear()
Clears all the statistics.
Definition: SimpleFits.h:1469
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
static constexpr Data_t lar::util::details::SimpleFitterInterface< T, N >::cube ( Data_t  v)
inlinestaticinherited

Returns the cube of the specified data value.

Definition at line 603 of file SimpleFits.h.

603 { return v * v * v; }
virtual Data_t lar::util::details::SimpleFitterInterface< T, N >::Determinant ( FitMatrix_t const &  mat) const
inlineprotectedvirtualinherited

Computes the determinant of a matrix.

Definition at line 607 of file SimpleFits.h.

608  {
609  return MatrixOps::Determinant(mat);
610  }
Float_t mat
Definition: plot.C:38
virtual Data_t lar::util::details::SimpleFitterInterface< T, N >::Evaluate ( Data_t  x) const
pure virtualinherited

Evaluates the fitted function at the specified point.

Parameters
xthe point where to evaluate the fit function
Returns
the value of the fit function

No check is performed whether the fit is valid.

template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::Evaluate ( Data_t  x) const
overridevirtual

Evaluates the fitted function at the specified point.

Parameters
xthe point where to evaluate the fit function
Returns
the value of the fit function

No check is performed whether the fit is valid.

Definition at line 1589 of file SimpleFits.h.

1590 {
1591  FitParameters_t params = FitParameters();
1592  unsigned int iParam = NParams - 1; // point to last parameter (highest degree)
1593  Data_t v = params[iParam];
1594  while (iParam > 0)
1595  v = v * x + params[--iParam];
1596  return v;
1597 } // SimplePolyFitterBase<>::Evaluate()
Float_t x
Definition: compare.C:6
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
typename Base_t::Data_t Data_t
type of the data
Definition: SimpleFits.h:649
typename Interface_t::FitParameters_t FitParameters_t
type of set of fit parameters
Definition: SimpleFits.h:652
virtual FitParameters_t FitParameters() const override
Computes and returns all the parameters of the fit result.
Definition: SimpleFits.h:1518
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::ExtractParameterErrors ( FitMatrix_t const &  Smat)
static

Extracts parameter errors from diagonal of the covarriance matrix.

Definition at line 1653 of file SimpleFits.h.

References lar::util::details::FitDataCollector< T, D >::Degree.

1655 {
1656  FitParameters_t fit_errors;
1657  for (unsigned int iParam = 0; iParam <= Degree; ++iParam)
1658  fit_errors[iParam] = std::sqrt(Smat[iParam * (NParams + 1)]);
1659  return fit_errors;
1660 } // SimplePolyFitterBase<>::FitParameterErrors(FitMatrix_t)
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
typename Interface_t::FitParameters_t FitParameters_t
type of set of fit parameters
Definition: SimpleFits.h:652
static constexpr unsigned int Degree
Degree of the fit.
Definition: SimpleFits.h:644
virtual bool lar::util::details::SimpleFitterInterface< T, N >::FillResults ( FitParameters_t params,
FitMatrix_t Xmat,
Data_t det,
FitMatrix_t Smat 
) const
pure virtualinherited

Fills the specified parameters.

Parameters
paramsthe fitted values of the parameters
Xmatthe matrix of the x^n/s^2 sums
Smatthe covariance matrix
detthe determinant of Xmat
Returns
true if the fit is valid (i.e. if a unique solution exists)
virtual bool lar::util::details::SimpleFitterInterface< T, N >::FillResults ( FitParameters_t params,
FitParameters_t paramerrors,
FitMatrix_t Xmat,
Data_t det,
FitMatrix_t Smat 
) const
pure virtualinherited

Fills the specified parameters.

Parameters
paramsthe fitted values of the parameters
paramerrorsthe uncertainty on the fitted parameters
Xmatthe matrix of the x^n/s^2 sums
Smatthe covariance matrix
detthe determinant of Xmat
Returns
true if the fit is valid (i.e. if a unique solution exists)
virtual bool lar::util::details::SimpleFitterInterface< T, N >::FillResults ( FitParameters_t params,
FitParameters_t paramerrors 
) const
pure virtualinherited

Fills the specified parameters.

Parameters
paramsthe fitted values of the parameters
paramerrorsthe uncertainty on the fitted parameters
Returns
true if the fit is valid (i.e. if a unique solution exists)

Implemented in lar::util::details::SimplePolyFitterBase< T, 1U >, and lar::util::details::SimplePolyFitterBase< T, 2U >.

template<typename T , unsigned int D>
bool lar::util::details::SimplePolyFitterBase< T, D >::FillResults ( FitParameters_t params,
FitMatrix_t Xmat,
Data_t det,
FitMatrix_t Smat 
) const
override

Fills the specified parameters.

Parameters
paramsthe fitted values of the parameters
Xmatthe matrix of the x^n/s^2 sums
Smatthe covariance matrix
detthe determinant of Xmat
Returns
true if the fit is valid (i.e. if a unique solution exists)

Definition at line 1546 of file SimpleFits.h.

1550 {
1551 
1552  Xmat = MakeMatrixX();
1553  det = Determinant(Xmat);
1554  if (!std::isnormal(det)) {
1555  Smat.fill(Data_t(0));
1556  params.fill(Data_t(0));
1557  return false;
1558  }
1559  Smat = InvertMatrix(Xmat, det);
1560  params = FitParameters(Smat, det);
1561  return true;
1562 } // SimplePolyFitterBase<>::FillResults(params, matrices, determinant)
virtual FitMatrix_t InvertMatrix(FitMatrix_t const &mat, Data_t det) const
Computes the inverse of a matrix (using provided determinant)
Definition: SimpleFits.h:613
virtual FitMatrix_t MakeMatrixX() const
Fills and returns the matrix of x^n sum coefficients ( { x^(i+j) } )
Definition: SimpleFits.h:1601
typename Base_t::Data_t Data_t
type of the data
Definition: SimpleFits.h:649
virtual FitParameters_t FitParameters() const override
Computes and returns all the parameters of the fit result.
Definition: SimpleFits.h:1518
virtual Data_t Determinant(FitMatrix_t const &mat) const
Computes the determinant of a matrix.
Definition: SimpleFits.h:607
template<typename T , unsigned int D>
bool lar::util::details::SimplePolyFitterBase< T, D >::FillResults ( FitParameters_t params,
FitParameters_t paramerrors,
FitMatrix_t Xmat,
Data_t det,
FitMatrix_t Smat 
) const
override

Fills the specified parameters.

Parameters
paramsthe fitted values of the parameters
paramerrorsthe uncertainty on the fitted parameters
Xmatthe matrix of the x^n/s^2 sums
Smatthe covariance matrix
detthe determinant of Xmat
Returns
true if the fit is valid (i.e. if a unique solution exists)

Definition at line 1565 of file SimpleFits.h.

1570 {
1571 
1572  if (!this->FillResults(params, Xmat, det, Smat)) return false;
1573  paramerrors = ExtractParameterErrors(Smat);
1574  return true;
1575 } // SimplePolyFitterBase<>::FillResults(params, errors, matrices, determinant)
bool FillResults(FitParameters_t &params, FitMatrix_t &Xmat, Data_t &det, FitMatrix_t &Smat) const override
Fills the specified parameters.
Definition: SimpleFits.h:1546
static FitParameters_t ExtractParameterErrors(FitMatrix_t const &Smat)
Extracts parameter errors from diagonal of the covarriance matrix.
Definition: SimpleFits.h:1653
template<typename T , unsigned int D>
bool lar::util::details::SimplePolyFitterBase< T, D >::FillResults ( FitParameters_t params,
FitParameters_t paramerrors 
) const
override

Fills the specified parameters.

Parameters
paramsthe fitted values of the parameters
paramerrorsthe uncertainty on the fitted parameters
Returns
true if the fit is valid (i.e. if a unique solution exists)

Definition at line 1578 of file SimpleFits.h.

1580 {
1581  // to compute the parameters, we need all the stuff;
1582  // we just keep it local and discard it in the end. Such a waste.
1583  FitMatrix_t Xmat, Smat;
1584  Data_t det;
1585  return FillResults(params, paramerrors, Xmat, det, Smat);
1586 } // SimplePolyFitterBase<>::FillResults(params, errors)
typename Base_t::Data_t Data_t
type of the data
Definition: SimpleFits.h:649
bool FillResults(FitParameters_t &params, FitMatrix_t &Xmat, Data_t &det, FitMatrix_t &Smat) const override
Fills the specified parameters.
Definition: SimpleFits.h:1546
typename Interface_t::FitMatrix_t FitMatrix_t
type of matrix for covariance (a std::array)
Definition: SimpleFits.h:655
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::FitParameter ( unsigned int  n) const
inlineoverridevirtual

Returns the parameter n of the fit result.

Parameters
ndegree of the parameter; must be no larger than Degree
Returns
the parameter of the fit, in y/x^n units
Exceptions
std::range_errorif there is no unique solution

Reimplemented from lar::util::details::SimpleFitterInterface< T, D+1 >.

Definition at line 1503 of file SimpleFits.h.

References n.

1505 {
1506  return Param(n, MakeMatrixX());
1507 } // SimplePolyFitterBase<>::FitParameter(unsigned int)
virtual Data_t Param(unsigned int n, FitMatrix_t const &Xmat) const
Computes a single fit parameter using the given information.
Definition: SimpleFits.h:1640
virtual FitMatrix_t MakeMatrixX() const
Fills and returns the matrix of x^n sum coefficients ( { x^(i+j) } )
Definition: SimpleFits.h:1601
Char_t n[5]
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::FitParameterCovariance ( ) const
overridevirtual

Computes and returns all the covariance matrix of the fit result.

Returns
the the covariance matrix of the fit
Exceptions
std::range_errorif there is no unique solution

The matrix is symmetric, and stored in a linear way (say, first row, then second row).

Implements lar::util::details::SimpleFitterInterface< T, D+1 >.

Definition at line 1534 of file SimpleFits.h.

1535 {
1536  FitMatrix_t Xmat = MakeMatrixX();
1537  Data_t det = Determinant(Xmat);
1538  if (!std::isnormal(det)) {
1539  throw std::range_error(
1540  "SimplePolyFitterBase::FitParameterCovariance(): determinant 0 while fitting");
1541  }
1542  return InvertMatrix(Xmat, det);
1543 } // SimplePolyFitterBase<>::FitParameterCovariance()
virtual FitMatrix_t InvertMatrix(FitMatrix_t const &mat, Data_t det) const
Computes the inverse of a matrix (using provided determinant)
Definition: SimpleFits.h:613
virtual FitMatrix_t MakeMatrixX() const
Fills and returns the matrix of x^n sum coefficients ( { x^(i+j) } )
Definition: SimpleFits.h:1601
typename Base_t::Data_t Data_t
type of the data
Definition: SimpleFits.h:649
typename Interface_t::FitMatrix_t FitMatrix_t
type of matrix for covariance (a std::array)
Definition: SimpleFits.h:655
virtual Data_t Determinant(FitMatrix_t const &mat) const
Computes the determinant of a matrix.
Definition: SimpleFits.h:607
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::FitParameterError ( unsigned int  n) const
overridevirtual

Returns the error on parameter n of the fit result.

Parameters
ndegree of the parameter; must be no larger than Degree
Returns
the error on the parameter of the fit, in y/x^n units
Exceptions
std::range_errorif there is no unique solution

Reimplemented from lar::util::details::SimpleFitterInterface< T, D+1 >.

Definition at line 1510 of file SimpleFits.h.

References n.

1512 {
1513  if (n > Degree) return Data_t(0); // no parameter, no error
1514  return std::sqrt(FitParameterCovariance()[n * (NParams + 1)]);
1515 } // SimplePolyFitterBase<>::FitParameterError()
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
typename Base_t::Data_t Data_t
type of the data
Definition: SimpleFits.h:649
virtual FitMatrix_t FitParameterCovariance() const override
Computes and returns all the covariance matrix of the fit result.
Definition: SimpleFits.h:1534
static constexpr unsigned int Degree
Degree of the fit.
Definition: SimpleFits.h:644
Char_t n[5]
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::FitParameterErrors ( ) const
overridevirtual

Computes and returns all the parameter errors of the fit result.

Returns
the full set of parameter errors of the fit
Exceptions
std::range_errorif there is no unique solution

Implements lar::util::details::SimpleFitterInterface< T, D+1 >.

Definition at line 1528 of file SimpleFits.h.

1529 {
1531 } // SimplePolyFitterBase<>::FitParameterErrors()
virtual FitParameters_t FitParameterErrors() const override
Computes and returns all the parameter errors of the fit result.
Definition: SimpleFits.h:1528
virtual FitMatrix_t FitParameterCovariance() const override
Computes and returns all the covariance matrix of the fit result.
Definition: SimpleFits.h:1534
template<typename T, unsigned int D>
virtual FitParameters_t lar::util::details::SimplePolyFitterBase< T, D >::FitParameterErrors ( FitMatrix_t const &  Smat) const
inlineprotectedvirtual

Computes and returns all the parameter errors of the fit result.

Definition at line 785 of file SimpleFits.h.

786  {
787  return ExtractParameterErrors(Smat);
788  }
static FitParameters_t ExtractParameterErrors(FitMatrix_t const &Smat)
Extracts parameter errors from diagonal of the covarriance matrix.
Definition: SimpleFits.h:1653
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::FitParameters ( ) const
overridevirtual

Computes and returns all the parameters of the fit result.

Returns
the full set of parameters of the fit
Exceptions
std::range_errorif there is no unique solution

Implements lar::util::details::SimpleFitterInterface< T, D+1 >.

Definition at line 1518 of file SimpleFits.h.

References lar::util::details::FitDataCollector< T, D >::NParams.

1519 {
1520  FitMatrix_t Xmat = MakeMatrixX();
1521  FitParameters_t fit_params;
1522  for (unsigned int iParam = 0; iParam < NParams; ++iParam)
1523  fit_params[iParam] = Param(iParam, Xmat);
1524  return fit_params;
1525 } // SimplePolyFitterBase<>::FitParameters()
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
virtual Data_t Param(unsigned int n, FitMatrix_t const &Xmat) const
Computes a single fit parameter using the given information.
Definition: SimpleFits.h:1640
virtual FitMatrix_t MakeMatrixX() const
Fills and returns the matrix of x^n sum coefficients ( { x^(i+j) } )
Definition: SimpleFits.h:1601
typename Interface_t::FitParameters_t FitParameters_t
type of set of fit parameters
Definition: SimpleFits.h:652
typename Interface_t::FitMatrix_t FitMatrix_t
type of matrix for covariance (a std::array)
Definition: SimpleFits.h:655
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::FitParameters ( FitMatrix_t const &  Xmat) const
protectedvirtual

Returns the fitted parameters using the provided information.

Definition at line 1622 of file SimpleFits.h.

References lar::util::details::FitDataCollector< T, D >::NParams.

1624 {
1625  FitParameters_t fit_params;
1626  for (unsigned int iParam = 0; iParam < NParams; ++iParam)
1627  fit_params[iParam] = Param(iParam, Xmat);
1628  return fit_params;
1629 } // SimplePolyFitterBase<>::FitParameters(FitMatrix_t)
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
virtual Data_t Param(unsigned int n, FitMatrix_t const &Xmat) const
Computes a single fit parameter using the given information.
Definition: SimpleFits.h:1640
typename Interface_t::FitParameters_t FitParameters_t
type of set of fit parameters
Definition: SimpleFits.h:652
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::FitParameters ( FitMatrix_t const &  Smat,
Data_t   
) const
protectedvirtual

Returns the fitted parameters using the provided information.

Definition at line 1632 of file SimpleFits.h.

1635 {
1636  return MatrixProduct(Smat, MakeMatrixY());
1637 } // SimplePolyFitterBase<>::FitParameters(FitMatrix_t)
virtual FitParameters_t MatrixProduct(FitMatrix_t const &mat, FitParameters_t const &vec) const
Computes the product of a FitMatrix_t and a FitParameters_t.
Definition: SimpleFits.h:625
virtual FitParameters_t MakeMatrixY() const
Fills and returns the matrix (vector) of x^n y sum coefficients.
Definition: SimpleFits.h:1613
virtual FitMatrix_t lar::util::details::SimpleFitterInterface< T, N >::InvertMatrix ( FitMatrix_t const &  mat,
Data_t  det 
) const
inlineprotectedvirtualinherited

Computes the inverse of a matrix (using provided determinant)

Definition at line 613 of file SimpleFits.h.

614  {
615  return MatrixOps::InvertSymmetricMatrix(mat, det);
616  }
Float_t mat
Definition: plot.C:38
virtual FitMatrix_t lar::util::details::SimpleFitterInterface< T, N >::InvertMatrix ( FitMatrix_t const &  mat) const
inlineprotectedvirtualinherited

Computes the inverse of a matrix.

Definition at line 619 of file SimpleFits.h.

620  {
621  return MatrixOps::InvertSymmetricMatrix(mat);
622  }
Float_t mat
Definition: plot.C:38
template<typename T , unsigned int D>
bool lar::util::details::SimplePolyFitterBase< T, D >::isValid ( ) const
inlineoverridevirtual

Returns if the fit has valid results.

Returns
if the fit has valid results

The fit has no valid results if:

  1. insufficient data has been add()ed (no more than the fit Degree)
  2. if input points are vertically aligned

Note that checking point 2 is expensive in terms of time.

Implements lar::util::details::SimpleFitterInterface< T, D+1 >.

Definition at line 1497 of file SimpleFits.h.

1498 {
1499  return (Base_t::N() > (int)Degree) && std::isnormal(Determinant(MakeMatrixX()));
1500 } // SimplePolyFitterBase<>::isValid()
virtual FitMatrix_t MakeMatrixX() const
Fills and returns the matrix of x^n sum coefficients ( { x^(i+j) } )
Definition: SimpleFits.h:1601
virtual Data_t Determinant(FitMatrix_t const &mat) const
Computes the determinant of a matrix.
Definition: SimpleFits.h:607
static constexpr unsigned int Degree
Degree of the fit.
Definition: SimpleFits.h:644
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::MakeMatrixX ( ) const
protectedvirtual

Fills and returns the matrix of x^n sum coefficients ( { x^(i+j) } )

Definition at line 1601 of file SimpleFits.h.

References lar::util::details::FitDataCollector< T, D >::NParams.

1602 {
1603  FitMatrix_t Xmat;
1604  for (unsigned int i = 0; i < NParams; ++i) { // row
1605  for (unsigned int j = i; j < NParams; ++j) { // column
1606  Xmat[j * NParams + i] = Xmat[i * NParams + j] = Base_t::XN(i + j);
1607  } // for j
1608  } // for i
1609  return Xmat;
1610 } // SimplePolyFitterBase<>::MakeMatrixX()
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
Data_t XN(unsigned int n) const
Returns the weighted sum of x^n.
Definition: SimpleFits.h:449
typename Interface_t::FitMatrix_t FitMatrix_t
type of matrix for covariance (a std::array)
Definition: SimpleFits.h:655
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::MakeMatrixY ( ) const
protectedvirtual

Fills and returns the matrix (vector) of x^n y sum coefficients.

Definition at line 1613 of file SimpleFits.h.

References lar::util::details::FitDataCollector< T, D >::NParams.

1614 {
1615  FitParameters_t Ymat;
1616  for (unsigned int i = 0; i < NParams; ++i)
1617  Ymat[i] = Base_t::XNY(i);
1618  return Ymat;
1619 } // SimplePolyFitterBase<>::MakeMatrixY()
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
Data_t XNY(unsigned int n) const
Returns the weighted sum of x^n y.
Definition: SimpleFits.h:452
typename Interface_t::FitParameters_t FitParameters_t
type of set of fit parameters
Definition: SimpleFits.h:652
virtual FitParameters_t lar::util::details::SimpleFitterInterface< T, N >::MatrixProduct ( FitMatrix_t const &  mat,
FitParameters_t const &  vec 
) const
inlineprotectedvirtualinherited

Computes the product of a FitMatrix_t and a FitParameters_t.

Definition at line 625 of file SimpleFits.h.

627  {
628  return MatrixOps::MatrixVectorProduct(mat, vec);
629  }
Float_t mat
Definition: plot.C:38
template<typename T , unsigned int D>
int lar::util::details::SimplePolyFitterDataBase< T, D >::N ( ) const
inlineinherited

Definition at line 425 of file SimpleFits.h.

425 { return stats.N(); }
int N() const
Returns the number of entries added.
Definition: SimpleFits.h:269
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T, unsigned int D>
virtual int lar::util::details::SimplePolyFitterBase< T, D >::NDF ( ) const
inlineoverridevirtual

Returns the degrees of freedom in the determination of the fit.

Returns
the degrees of freedom in the determination of the fit

The return value may be 0 or negative if insufficient points have been added.

Implements lar::util::details::SimpleFitterInterface< T, D+1 >.

Definition at line 725 of file SimpleFits.h.

725 { return Base_t::N() - NParams; }
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
Data_t lar::util::details::SimpleFitterInterface< T, N >::operator() ( Data_t  x) const
inlineinherited

Evaluates the fitted function; alias of Evaluate()

Definition at line 597 of file SimpleFits.h.

597 { return Evaluate(x); }
Float_t x
Definition: compare.C:6
virtual Data_t Evaluate(Data_t x) const =0
Evaluates the fitted function at the specified point.
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::Param ( unsigned int  n,
FitMatrix_t const &  Xmat 
) const
protectedvirtual

Computes a single fit parameter using the given information.

Definition at line 1640 of file SimpleFits.h.

References n.

1642 {
1643  if (n > Degree) return Data_t(0); // no such a degree, its coefficient is 0
1644 
1645  Data_t detXmat = Determinant(Xmat);
1646  if (!std::isnormal(detXmat)) {
1647  throw std::range_error("SimplePolyFitterBase::Param(): Determinant 0 while fitting");
1648  }
1649  return Param(n, Xmat, detXmat);
1650 } // SimplePolyFitterBase<>::Param(unsigned int, FitMatrix_t)
virtual Data_t Param(unsigned int n, FitMatrix_t const &Xmat) const
Computes a single fit parameter using the given information.
Definition: SimpleFits.h:1640
typename Base_t::Data_t Data_t
type of the data
Definition: SimpleFits.h:649
virtual Data_t Determinant(FitMatrix_t const &mat) const
Computes the determinant of a matrix.
Definition: SimpleFits.h:607
static constexpr unsigned int Degree
Degree of the fit.
Definition: SimpleFits.h:644
Char_t n[5]
template<typename T , unsigned int D>
auto lar::util::details::SimplePolyFitterBase< T, D >::Param ( unsigned int  n,
FitMatrix_t const &  Xmat,
Data_t  detXmat 
) const
protectedvirtual

Computes a single fit parameter using the given information.

Definition at line 1663 of file SimpleFits.h.

References n, and lar::util::details::FitDataCollector< T, D >::NParams.

1666 {
1667  if (n > Degree) return Data_t(0); // no such a degree, its coefficient is 0
1668  // XYmat is as Xmat...
1669  FitMatrix_t XYmat(Xmat);
1670  // ... except that the N-th column is replaced with { sum x^i y }
1671  for (unsigned int i = 0; i < NParams; ++i)
1672  XYmat[i * NParams + n] = Base_t::XNY(i);
1673 
1674  return Determinant(XYmat) / detXmat;
1675 } // SimplePolyFitterBase<>::Param(unsigned int, FitMatrix_t, Data_t)
static constexpr unsigned int NParams
Number of parameters in the fit.
Definition: SimpleFits.h:647
Data_t XNY(unsigned int n) const
Returns the weighted sum of x^n y.
Definition: SimpleFits.h:452
typename Base_t::Data_t Data_t
type of the data
Definition: SimpleFits.h:649
typename Interface_t::FitMatrix_t FitMatrix_t
type of matrix for covariance (a std::array)
Definition: SimpleFits.h:655
virtual Data_t Determinant(FitMatrix_t const &mat) const
Computes the determinant of a matrix.
Definition: SimpleFits.h:607
static constexpr unsigned int Degree
Degree of the fit.
Definition: SimpleFits.h:644
Char_t n[5]
template<typename T , unsigned int D>
template<typename Stream >
void lar::util::details::SimplePolyFitterDataBase< T, D >::PrintStats ( Stream &  out) const
inlineinherited

Prints the collected statistics into a stream.

Definition at line 433 of file SimpleFits.h.

434  {
435  stats.Print(out);
436  }
void Print(Stream &out) const
Prints the statistics into a stream.
Definition: SimpleFits.h:1480
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
template<typename V >
static constexpr V lar::util::details::SimplePolyFitterDataBase< T, D >::sqr ( V const &  v)
inlinestaticinherited

Returns the square of the specified value.

Definition at line 440 of file SimpleFits.h.

References sqr().

441  {
442  return Collector_t::sqr(v);
443  }
static constexpr V sqr(V const &v)
Returns the square of the specified value.
Definition: SimpleFits.h:284
static constexpr Data_t lar::util::details::SimpleFitterInterface< T, N >::sqr ( Data_t  v)
inlinestaticinherited

Returns the square of the specified data value.

Definition at line 600 of file SimpleFits.h.

600 { return v * v; }
template<typename T , unsigned int D>
Data_t lar::util::details::SimplePolyFitterDataBase< T, D >::XN ( unsigned int  n) const
inlineprotectedinherited

Returns the weighted sum of x^n.

Definition at line 449 of file SimpleFits.h.

References lar::util::details::FitDataCollector< T, D >::XN().

449 { return stats.XN(n); }
Data_t XN(unsigned int n) const
Returns the weighted sum of x^n.
Definition: SimpleFits.h:290
Char_t n[5]
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446
template<typename T , unsigned int D>
Data_t lar::util::details::SimplePolyFitterDataBase< T, D >::XNY ( unsigned int  n) const
inlineprotectedinherited

Returns the weighted sum of x^n y.

Definition at line 452 of file SimpleFits.h.

References lar::util::details::FitDataCollector< T, D >::XNY().

452 { return stats.XNY(n); }
Data_t XNY(unsigned int n) const
Returns the weighted sum of x^n y.
Definition: SimpleFits.h:293
Char_t n[5]
Collector_t stats
statistics collected from fit data input
Definition: SimpleFits.h:446

Member Data Documentation

template<typename T, unsigned int D>
constexpr unsigned int lar::util::details::SimplePolyFitterBase< T, D >::Degree = Base_t::Degree
static

Degree of the fit.

Definition at line 644 of file SimpleFits.h.

template<typename T, unsigned int D>
constexpr unsigned int lar::util::details::SimplePolyFitterBase< T, D >::NParams = Interface_t::NParams
static

Number of parameters in the fit.

Definition at line 647 of file SimpleFits.h.

template<typename T , unsigned int D>
Collector_t lar::util::details::SimplePolyFitterDataBase< T, D >::stats
protectedinherited

statistics collected from fit data input

Definition at line 446 of file SimpleFits.h.


The documentation for this class was generated from the following file: