13#ifndef ROOT_Fit_FitUtil
14#define ROOT_Fit_FitUtil
46 vecCore::Set<T>(
x,
j,
j);
47 return vecCore::Mask<T>(
x < T(i));
127 template <
class ParamFunc = ROOT::Math::IParamMultiFunctionTempl<
double>>
160 }
else if (
fDim > 1) {
212 for (
unsigned int i = 0; i <
fDim; ++i)
224 inline double ExecFunc(T *
f,
const double *
x,
const double *
p)
const
236 for (
unsigned int i = 0; i <
fDim; ++i) {
245 vecCore::Load<ROOT::Double_v>(
xx[i],
xBuffer);
247 auto res = (*f)(
xx,
p);
248 return vecCore::Get<ROOT::Double_v>(res, 0);
376 unsigned int n =
data.Size();
390 Error(
"FitUtil::EvaluateChi2",
"The vectorized implementation doesn't support Integrals, BinVolume or ExpErrors\n. Aborting operation.");
394 double maxResValue = std::numeric_limits<double>::max() /
n;
395 std::vector<double>
ones{1., 1., 1., 1.};
396 auto vecSize = vecCore::VectorSize<T>();
401 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
409 if(
data.NDim() > 1) {
412 for (
unsigned int j = 1;
j <
data.NDim(); ++
j)
448 Warning(
"FitUtil::EvaluateChi2",
"Multithread execution policy requires IMT, which is disabled. Changing "
449 "to ::ROOT::EExecutionPolicy::kSequential.");
465 Error(
"FitUtil::EvaluateChi2",
"Execution policy unknown. Available choices:\n ::ROOT::EExecutionPolicy::kSequential (default)\n ::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
470 vecCore::MaskedAssign(res, vecCore::Int2Mask<T>(
data.Size() %
vecSize),
473 return vecCore::ReduceAdd(res);
481 unsigned int n =
data.Size();
496 if (
data.NDim() == 1) {
498 vecCore::Load<T>(
x,
data.GetCoordComponent(0, 0));
502 std::vector<T>
x(
data.NDim());
503 for (
unsigned int j = 0;
j <
data.NDim(); ++
j)
504 vecCore::Load<T>(
x[
j],
data.GetCoordComponent(0,
j));
514 std::vector<double>
xmin(
data.NDim());
515 std::vector<double>
xmax(
data.NDim());
518 if (
data.Range().Size() > 0) {
520 for (
unsigned int ir = 0;
ir <
data.Range().Size(); ++
ir) {
531 if (vecCore::ReduceAdd(func(&
xmin_v,
p)) != 0 || vecCore::ReduceAdd(func(&
xmax_v,
p)) != 0) {
532 MATH_ERROR_MSG(
"FitUtil::EvaluateLogLikelihood",
"A range has not been set and the function is not zero at +/- inf");
541 auto vecSize = vecCore::VectorSize<T>();
552 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
553 const T *
x =
nullptr;
554 unsigned int ndim =
data.NDim();
559 for (
unsigned int j = 1;
j < ndim; ++
j)
574 if (ndim == 1) std::cout << i <<
" x " <<
x[0] <<
" fval = " <<
fval;
575 else std::cout << i <<
" x " <<
x[0] <<
" y " <<
x[1] <<
" fval = " <<
fval;
585 if (
data.WeightsPtr(i) ==
nullptr)
588 vecCore::Load<T>(weight,
data.WeightsPtr(i*
vecSize));
595 W2 = weight * weight;
601 std::cout <<
" " <<
fval <<
" logfval " <<
logval << std::endl;
620 Warning(
"FitUtil::EvaluateLogL",
"Multithread execution policy requires IMT, which is disabled. Changing "
621 "to ::ROOT::EExecutionPolicy::kSequential.");
640 Error(
"FitUtil::EvaluateLogL",
"Execution policy unknown. Available choices:\n ::ROOT::EExecutionPolicy::kSequential (default)\n ::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
666 double extendedTerm = 0;
672 std::vector<double>
xmin(
data.NDim());
673 std::vector<double>
xmax(
data.NDim());
676 if (
data.Range().Size() > 0) {
678 for (
unsigned int ir = 0;
ir <
data.Range().Size(); ++
ir) {
689 if (vecCore::ReduceAdd(func(&
xmin_v,
p)) != 0 || vecCore::ReduceAdd(func(&
xmax_v,
p)) != 0) {
690 MATH_ERROR_MSG(
"FitUtil::EvaluateLogLikelihood",
"A range has not been set and the function is not zero at +/- inf");
699 extendedTerm = -
nuTot;
712 logl += extendedTerm;
716 std::cout <<
"Evaluated log L for parameters (";
717 for (
unsigned int ip = 0;
ip < func.NPar(); ++
ip)
718 std::cout <<
" " <<
p[
ip];
719 std::cout <<
") nll = " << -
logl << std::endl;
748 auto vecSize = vecCore::VectorSize<T>();
752 Error(
"FitUtil::EvaluateChi2",
753 "The vectorized implementation doesn't support Integrals or BinVolume\n. Aborting operation.");
761 if (
data.NDim() > 1) {
762 std::vector<T>
x(
data.NDim());
763 for (
unsigned int j = 0;
j <
data.NDim(); ++
j)
766 fval = func(
x.data());
773 vecCore::Load<T>(
x,
data.GetCoordComponent(i *
vecSize, 0));
783 vecCore::MaskedAssign<T>(
fval,
fval < 0.0, 0.0);
795 vecCore::Load<T>(error,
data.ErrorPtr(i *
vecSize));
797 auto m = vecCore::Mask_v<T>(
y != 0.0);
798 auto weight = vecCore::Blend(
m,(error * error) /
y, T(
data.SumOfError2()/
data.SumOfContent()) );
811 vecCore::MaskedAssign<T>(
825 Warning(
"FitUtil::Evaluate<T>::EvalPoissonLogL",
826 "Multithread execution policy requires IMT, which is disabled. Changing "
827 "to ::ROOT::EExecutionPolicy::kSequential.");
834 for (
unsigned int i = 0; i < (
data.Size() /
vecSize); i++) {
845 "FitUtil::Evaluate<T>::EvalPoissonLogL",
846 "Execution policy unknown. Available choices:\n ::ROOT::EExecutionPolicy::kSequential (default)\n ::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
851 vecCore::MaskedAssign(res, vecCore::Int2Mask<T>(
data.Size() %
vecSize),
854 return vecCore::ReduceAdd(res);
859 Error(
"FitUtil::Evaluate<T>::EvalChi2Effective",
"The vectorized evaluation of the Chi2 with coordinate errors is still not supported");
871 vecCore::MaskedAssign(
rval, !
mask, +vecCore::NumericLimits<T>::Max());
874 vecCore::MaskedAssign(
rval, !
mask &&
rval < 0, -vecCore::NumericLimits<T>::Max());
890 if (
data.HaveCoordErrors()) {
892 "Error on the coordinates are not used in calculating Chi2 gradient");
903 Error(
"FitUtil::EvaluateChi2Gradient",
"The vectorized implementation doesn't support Integrals,"
904 "BinVolume or ExpErrors\n. Aborting operation.");
906 unsigned int npar = func.NPar();
907 auto vecSize = vecCore::VectorSize<T>();
921 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
934 const T *
x =
nullptr;
936 unsigned int ndim =
data.NDim();
943 for (
unsigned int j = 1;
j < ndim; ++
j)
960 for (
unsigned int ipar = 0; ipar <
npar; ++ipar) {
990 std::vector<double>
g(
npar);
998 Warning(
"FitUtil::EvaluateChi2Gradient",
999 "Multithread execution policy requires IMT, which is disabled. Changing "
1000 "to ::ROOT::EExecutionPolicy::kSequential.");
1018 "FitUtil::EvaluateChi2Gradient",
1019 "Execution policy unknown. Available choices:\n 0: Serial (default)\n 1: MultiThread (requires IMT)\n");
1028 for (
unsigned int param = 0; param <
npar; param++) {
1033 for (
unsigned int param = 0; param <
npar; param++) {
1034 grad[param] = vecCore::ReduceAdd(
gVec[param]);
1041 [](vecCore::Mask<T>
validPoints) { return !vecCore::MaskFull(validPoints); })) {
1045 for (
unsigned int i = 0; i <
vecSize; i++) {
1055 "Too many points rejected for overflow in gradient calculation");
1062 Error(
"FitUtil::Evaluate<T>::EvalChi2Residual",
"The vectorized evaluation of the Chi2 with the ith residual is still not supported");
1069 Error(
"FitUtil::Evaluate<T>::EvaluatePoissonBinPdf",
"The vectorized evaluation of the BinnedLikelihood fit evaluated point by point is still not supported");
1074 Error(
"FitUtil::Evaluate<T>::EvalPdf",
"The vectorized evaluation of the LogLikelihood fit evaluated point by point is still not supported");
1106 Error(
"FitUtil::EvaluatePoissonLogLGradient",
"The vectorized implementation doesn't support Integrals,"
1107 "BinVolume or ExpErrors\n. Aborting operation.");
1109 unsigned int npar = func.NPar();
1110 auto vecSize = vecCore::VectorSize<T>();
1121 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
1126 const T *
x =
nullptr;
1128 unsigned ndim =
data.NDim();
1133 for (
unsigned int j = 1;
j < ndim; ++
j)
1144 for (
unsigned int ipar = 0; ipar <
npar; ++ipar) {
1153 const T
kdmax1 = vecCore::math::Sqrt(vecCore::NumericLimits<T>::Max());
1163 if (i < 5 || (i >
data.Size()-5) ) {
1164 if (
data.NDim() > 1) std::cout << i <<
" x " <<
x[0] <<
" y " <<
x[1];
1165 else std::cout << i <<
" x " <<
x[0];
1166 std::cout <<
" func " <<
fval <<
" gradient ";
1196 Warning(
"FitUtil::EvaluatePoissonLogLGradient",
1197 "Multithread execution policy requires IMT, which is disabled. Changing "
1198 "to ::ROOT::EExecutionPolicy::kSequential.");
1215 Error(
"FitUtil::EvaluatePoissonLogLGradient",
"Execution policy unknown. Available choices:\n "
1216 "::ROOT::EExecutionPolicy::kSequential (default)\n "
1217 "::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
1227 for (
unsigned int param = 0; param <
npar; param++) {
1232 for (
unsigned int param = 0; param <
npar; param++) {
1233 grad[param] = vecCore::ReduceAdd(
gVec[param]);
1237 std::cout <<
"***** Final gradient : ";
1238 for (
unsigned int ii = 0;
ii<
npar; ++
ii) std::cout << grad[
ii] <<
" ";
1245 double *grad,
unsigned int &,
1257 unsigned int npar = func.NPar();
1258 auto vecSize = vecCore::VectorSize<T>();
1263 std::cout <<
"\n===> Evaluate Gradient for parameters ";
1265 std::cout <<
" " <<
p[
ip];
1271 const T
kdmax1 = vecCore::math::Sqrt(vecCore::NumericLimits<T>::Max());
1279 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
1281 const T *
x =
nullptr;
1283 unsigned int ndim =
data.NDim();
1284 std::vector<T>
xc(ndim);
1288 for (
unsigned int j = 1;
j < ndim; ++
j)
1301 if (ndim > 1) std::cout << i <<
" x " <<
x[0] <<
" y " <<
x[1] <<
" gradient " <<
gradFunc[0] <<
" " <<
gradFunc[1] <<
" " <<
gradFunc[3] << std::endl;
1302 else std::cout << i <<
" x " <<
x[0] <<
" gradient " <<
gradFunc[0] <<
" " <<
gradFunc[1] <<
" " <<
gradFunc[3] << std::endl;
1338 std::vector<double>
g(
npar);
1346 Warning(
"FitUtil::EvaluateLogLGradient",
1347 "Multithread execution policy requires IMT, which is disabled. Changing "
1348 "to ::ROOT::EExecutionPolicy::kSequential.");
1365 Error(
"FitUtil::EvaluateLogLGradient",
"Execution policy unknown. Available choices:\n "
1366 "::ROOT::EExecutionPolicy::kSequential (default)\n "
1367 "::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
1376 for (
unsigned int param = 0; param <
npar; param++) {
1381 for (
unsigned int param = 0; param <
npar; param++) {
1382 grad[param] = vecCore::ReduceAdd(
gVec[param]);
1386 std::cout <<
"Final gradient ";
1387 for (
unsigned int param = 0; param <
npar; param++) {
1388 std::cout <<
" " << grad[param];
1479#if defined (R__HAS_VECCORE) && defined(R__HAS_VC)
#define MATH_ERROR_MSG(loc, str)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
R__EXTERN TVirtualMutex * gROOTMutex
#define R__LOCKGUARD(mutex)
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
double Integral(const double *x1, const double *x2)
ROOT::Math::IGenFunction * fFunc1Dim
ROOT::Math::IntegratorMultiDim * fIgNDim
void SetFunction(const ParamFunc &func, const double *p=nullptr, ROOT::Math::IntegrationOneDim::Type igType=ROOT::Math::IntegrationOneDim::kDEFAULT)
IntegralEvaluator(const IntegralEvaluator &rhs)=delete
IntegralEvaluator(const ParamFunc &func, const double *p, bool useIntegral=true, ROOT::Math::IntegrationOneDim::Type igType=ROOT::Math::IntegrationOneDim::kDEFAULT)
IntegralEvaluator & operator=(const IntegralEvaluator &rhs)=delete
double F1(double x) const
ROOT::Math::IntegratorOneDim * fIg1Dim
double FN(const double *x) const
ROOT::Math::IMultiGenFunction * fFuncNDim
void SetParameters(const double *p)
double ExecFunc(T *f, const double *x, const double *p) const
double operator()(const double *x1, const double *x2)
LikelihoodAux(double logv=0.0, double w=0.0, double w2=0.0)
LikelihoodAux & operator+=(const LikelihoodAux &l)
LikelihoodAux operator+(const LikelihoodAux &l) const
LikelihoodAux operator+(const LikelihoodAux &l) const
LikelihoodAux(T logv={}, T w={}, T w2={})
LikelihoodAux & operator+=(const LikelihoodAux &l)
Class describing the un-binned data sets (just x coordinates values) of any dimensions.
Documentation for the abstract class IBaseFunctionMultiDim.
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Interface (abstract class) for parametric gradient multi-dimensional functions providing in addition ...
User class for performing multidimensional integration.
double Integral(const double *xmin, const double *xmax)
evaluate the integral with the previously given function between xmin[] and xmax[]
void SetFunction(Function &f, unsigned int dim)
set integration function using a generic function implementing the operator()(double *x) The dimensio...
User Class for performing numerical integration of a function in one dimension.
void SetFunction(Function &f)
method to set the a generic integration function
double Integral(Function &f, double a, double b)
evaluate the Integral of a function f over the defined interval (a,b)
Template class to wrap any member function of a class taking a double and returning a double in a 1D ...
const_iterator begin() const
const_iterator end() const
A pseudo container class which is a generator of indices.
This class provides a simple interface to execute the same task multiple times in parallel threads,...
Type
enumeration specifying the integration types.
@ kDEFAULT
default type specified in the static options
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
ROOT::Math::IParamMultiGradFunction IGradModelFunction
ROOT::Math::IParamMultiFunction IModelFunction
double EvaluatePoissonBinPdf(const IModelFunction &func, const BinData &data, const double *x, unsigned int ipoint, double *g=nullptr, double *h=nullptr, bool hasGrad=false, bool fullHessian=false)
evaluate the pdf contribution to the Poisson LogL given a model function and the BinPoint data.
void EvaluatePoissonLogLGradient(const IModelFunction &func, const BinData &data, const double *p, double *grad, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
evaluate the Poisson LogL given a model function and the data at the point p.
double EvaluateChi2Residual(const IModelFunction &func, const BinData &data, const double *p, unsigned int ipoint, double *g=nullptr, double *h=nullptr, bool hasGrad=false, bool fullHessian=false)
evaluate the residual contribution to the Chi2 given a model function and the BinPoint data and if th...
double EvaluatePoissonLogL(const IModelFunction &func, const BinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
evaluate the Poisson LogL given a model function and the data at the point p.
double EvaluateChi2Effective(const IModelFunction &func, const BinData &data, const double *x, unsigned int &nPoints)
evaluate the effective Chi2 given a model function and the data at the point x.
void EvaluateLogLGradient(const IModelFunction &func, const UnBinData &data, const double *p, double *grad, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
evaluate the LogL gradient given a model function and the data at the point p.
double EvaluatePdf(const IModelFunction &func, const UnBinData &data, const double *p, unsigned int ipoint, double *g=nullptr, double *h=nullptr, bool hasGrad=false, bool fullHessian=false)
evaluate the pdf contribution to the LogL given a model function and the BinPoint data.
unsigned setAutomaticChunking(unsigned nEvents)
double EvaluateLogL(const IModelFunction &func, const UnBinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
evaluate the LogL given a model function and the data at the point x.
double EvaluateChi2(const IModelFunction &func, const BinData &data, const double *p, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
Chi2 Functions.
void EvaluateChi2Gradient(const IModelFunction &func, const BinData &data, const double *p, double *grad, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
evaluate the Chi2 gradient given a model function and the data at the point p.
T EvalLog(T x)
safe evaluation of log(x) with a protections against negative or zero argument to the log smooth line...
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
DataOptions : simple structure holding the options on how the data are filled.
static double EvalLogL(const IModelFunctionTempl< double > &func, const UnBinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
static double EvalChi2Effective(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int &nPoints)
static void EvalLogLGradient(const IModelFunctionTempl< double > &func, const UnBinData &data, const double *p, double *g, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
static void EvalChi2Gradient(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, double *g, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
static double EvalPdf(const IModelFunctionTempl< double > &func, const UnBinData &data, const double *p, unsigned int i, double *g, double *h, bool hasGrad, bool fullHessian)
static double EvalChi2(const IModelFunction &func, const BinData &data, const double *p, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
static double EvalPoissonBinPdf(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int i, double *g, double *h, bool hasGrad, bool fullHessian)
evaluate the pdf (Poisson) contribution to the logl (return actually log of pdf) and its gradient
static void EvalPoissonLogLGradient(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, double *g, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy=::ROOT::EExecutionPolicy::kSequential, unsigned nChunks=0)
static double EvalPoissonLogL(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ::ROOT::EExecutionPolicy executionPolicy, unsigned nChunks=0)
static double EvalChi2Residual(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int i, double *g, double *h, bool hasGrad, bool fullHessian)