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
231#if __clang_major__ > 16
232#pragma clang diagnostic push
233#pragma clang diagnostic ignored "-Wvla-cxx-extension"
242 for (
unsigned int i = 0; i <
fDim; ++i) {
251 vecCore::Load<ROOT::Double_v>(
xx[i],
xBuffer);
253 auto res = (*f)(
xx,
p);
254 return vecCore::Get<ROOT::Double_v>(res, 0);
257#if __clang_major__ > 16
258#pragma clang diagnostic pop
387 unsigned int n =
data.Size();
401 Error(
"FitUtil::EvaluateChi2",
"The vectorized implementation doesn't support Integrals, BinVolume or ExpErrors\n. Aborting operation.");
405 double maxResValue = std::numeric_limits<double>::max() /
n;
406 std::vector<double>
ones{1., 1., 1., 1.};
407 auto vecSize = vecCore::VectorSize<T>();
412 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
420 if(
data.NDim() > 1) {
423 for (
unsigned int j = 1;
j <
data.NDim(); ++
j)
459 Warning(
"FitUtil::EvaluateChi2",
"Multithread execution policy requires IMT, which is disabled. Changing "
460 "to ::ROOT::EExecutionPolicy::kSequential.");
476 Error(
"FitUtil::EvaluateChi2",
"Execution policy unknown. Available choices:\n ::ROOT::EExecutionPolicy::kSequential (default)\n ::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
481 vecCore::MaskedAssign(res, vecCore::Int2Mask<T>(
data.Size() %
vecSize),
484 return vecCore::ReduceAdd(res);
492 unsigned int n =
data.Size();
507 if (
data.NDim() == 1) {
509 vecCore::Load<T>(
x,
data.GetCoordComponent(0, 0));
513 std::vector<T>
x(
data.NDim());
514 for (
unsigned int j = 0;
j <
data.NDim(); ++
j)
515 vecCore::Load<T>(
x[
j],
data.GetCoordComponent(0,
j));
525 std::vector<double>
xmin(
data.NDim());
526 std::vector<double>
xmax(
data.NDim());
529 if (
data.Range().Size() > 0) {
531 for (
unsigned int ir = 0;
ir <
data.Range().Size(); ++
ir) {
542 if (vecCore::ReduceAdd(func(&
xmin_v,
p)) != 0 || vecCore::ReduceAdd(func(&
xmax_v,
p)) != 0) {
543 MATH_ERROR_MSG(
"FitUtil::EvaluateLogLikelihood",
"A range has not been set and the function is not zero at +/- inf");
552 auto vecSize = vecCore::VectorSize<T>();
563 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
564 const T *
x =
nullptr;
565 unsigned int ndim =
data.NDim();
570 for (
unsigned int j = 1;
j < ndim; ++
j)
585 if (ndim == 1) std::cout << i <<
" x " <<
x[0] <<
" fval = " <<
fval;
586 else std::cout << i <<
" x " <<
x[0] <<
" y " <<
x[1] <<
" fval = " <<
fval;
596 if (
data.WeightsPtr(i) ==
nullptr)
599 vecCore::Load<T>(weight,
data.WeightsPtr(i*
vecSize));
606 W2 = weight * weight;
612 std::cout <<
" " <<
fval <<
" logfval " <<
logval << std::endl;
631 Warning(
"FitUtil::EvaluateLogL",
"Multithread execution policy requires IMT, which is disabled. Changing "
632 "to ::ROOT::EExecutionPolicy::kSequential.");
651 Error(
"FitUtil::EvaluateLogL",
"Execution policy unknown. Available choices:\n ::ROOT::EExecutionPolicy::kSequential (default)\n ::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
677 double extendedTerm = 0;
683 std::vector<double>
xmin(
data.NDim());
684 std::vector<double>
xmax(
data.NDim());
687 if (
data.Range().Size() > 0) {
689 for (
unsigned int ir = 0;
ir <
data.Range().Size(); ++
ir) {
700 if (vecCore::ReduceAdd(func(&
xmin_v,
p)) != 0 || vecCore::ReduceAdd(func(&
xmax_v,
p)) != 0) {
701 MATH_ERROR_MSG(
"FitUtil::EvaluateLogLikelihood",
"A range has not been set and the function is not zero at +/- inf");
710 extendedTerm = -
nuTot;
723 logl += extendedTerm;
727 std::cout <<
"Evaluated log L for parameters (";
728 for (
unsigned int ip = 0;
ip < func.NPar(); ++
ip)
729 std::cout <<
" " <<
p[
ip];
730 std::cout <<
") nll = " << -
logl << std::endl;
759 auto vecSize = vecCore::VectorSize<T>();
763 Error(
"FitUtil::EvaluateChi2",
764 "The vectorized implementation doesn't support Integrals or BinVolume\n. Aborting operation.");
772 if (
data.NDim() > 1) {
773 std::vector<T>
x(
data.NDim());
774 for (
unsigned int j = 0;
j <
data.NDim(); ++
j)
777 fval = func(
x.data());
784 vecCore::Load<T>(
x,
data.GetCoordComponent(i *
vecSize, 0));
794 vecCore::MaskedAssign<T>(
fval,
fval < 0.0, 0.0);
806 vecCore::Load<T>(error,
data.ErrorPtr(i *
vecSize));
808 auto m = vecCore::Mask_v<T>(
y != 0.0);
809 auto weight = vecCore::Blend(
m,(error * error) /
y, T(
data.SumOfError2()/
data.SumOfContent()) );
822 vecCore::MaskedAssign<T>(
836 Warning(
"FitUtil::Evaluate<T>::EvalPoissonLogL",
837 "Multithread execution policy requires IMT, which is disabled. Changing "
838 "to ::ROOT::EExecutionPolicy::kSequential.");
845 for (
unsigned int i = 0; i < (
data.Size() /
vecSize); i++) {
856 "FitUtil::Evaluate<T>::EvalPoissonLogL",
857 "Execution policy unknown. Available choices:\n ::ROOT::EExecutionPolicy::kSequential (default)\n ::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
862 vecCore::MaskedAssign(res, vecCore::Int2Mask<T>(
data.Size() %
vecSize),
865 return vecCore::ReduceAdd(res);
870 Error(
"FitUtil::Evaluate<T>::EvalChi2Effective",
"The vectorized evaluation of the Chi2 with coordinate errors is still not supported");
882 vecCore::MaskedAssign(
rval, !
mask, +vecCore::NumericLimits<T>::Max());
885 vecCore::MaskedAssign(
rval, !
mask &&
rval < 0, -vecCore::NumericLimits<T>::Max());
901 if (
data.HaveCoordErrors()) {
903 "Error on the coordinates are not used in calculating Chi2 gradient");
914 Error(
"FitUtil::EvaluateChi2Gradient",
"The vectorized implementation doesn't support Integrals,"
915 "BinVolume or ExpErrors\n. Aborting operation.");
917 unsigned int npar = func.NPar();
918 auto vecSize = vecCore::VectorSize<T>();
932 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
945 const T *
x =
nullptr;
947 unsigned int ndim =
data.NDim();
954 for (
unsigned int j = 1;
j < ndim; ++
j)
971 for (
unsigned int ipar = 0; ipar <
npar; ++ipar) {
1001 std::vector<double>
g(
npar);
1009 Warning(
"FitUtil::EvaluateChi2Gradient",
1010 "Multithread execution policy requires IMT, which is disabled. Changing "
1011 "to ::ROOT::EExecutionPolicy::kSequential.");
1029 "FitUtil::EvaluateChi2Gradient",
1030 "Execution policy unknown. Available choices:\n 0: Serial (default)\n 1: MultiThread (requires IMT)\n");
1039 for (
unsigned int param = 0; param <
npar; param++) {
1044 for (
unsigned int param = 0; param <
npar; param++) {
1045 grad[param] = vecCore::ReduceAdd(
gVec[param]);
1052 [](vecCore::Mask<T>
validPoints) { return !vecCore::MaskFull(validPoints); })) {
1056 for (
unsigned int i = 0; i <
vecSize; i++) {
1066 "Too many points rejected for overflow in gradient calculation");
1073 Error(
"FitUtil::Evaluate<T>::EvalChi2Residual",
"The vectorized evaluation of the Chi2 with the ith residual is still not supported");
1080 Error(
"FitUtil::Evaluate<T>::EvaluatePoissonBinPdf",
"The vectorized evaluation of the BinnedLikelihood fit evaluated point by point is still not supported");
1085 Error(
"FitUtil::Evaluate<T>::EvalPdf",
"The vectorized evaluation of the LogLikelihood fit evaluated point by point is still not supported");
1117 Error(
"FitUtil::EvaluatePoissonLogLGradient",
"The vectorized implementation doesn't support Integrals,"
1118 "BinVolume or ExpErrors\n. Aborting operation.");
1120 unsigned int npar = func.NPar();
1121 auto vecSize = vecCore::VectorSize<T>();
1132 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
1137 const T *
x =
nullptr;
1139 unsigned ndim =
data.NDim();
1144 for (
unsigned int j = 1;
j < ndim; ++
j)
1155 for (
unsigned int ipar = 0; ipar <
npar; ++ipar) {
1164 const T
kdmax1 = vecCore::math::Sqrt(vecCore::NumericLimits<T>::Max());
1174 if (i < 5 || (i >
data.Size()-5) ) {
1175 if (
data.NDim() > 1) std::cout << i <<
" x " <<
x[0] <<
" y " <<
x[1];
1176 else std::cout << i <<
" x " <<
x[0];
1177 std::cout <<
" func " <<
fval <<
" gradient ";
1207 Warning(
"FitUtil::EvaluatePoissonLogLGradient",
1208 "Multithread execution policy requires IMT, which is disabled. Changing "
1209 "to ::ROOT::EExecutionPolicy::kSequential.");
1226 Error(
"FitUtil::EvaluatePoissonLogLGradient",
"Execution policy unknown. Available choices:\n "
1227 "::ROOT::EExecutionPolicy::kSequential (default)\n "
1228 "::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
1238 for (
unsigned int param = 0; param <
npar; param++) {
1243 for (
unsigned int param = 0; param <
npar; param++) {
1244 grad[param] = vecCore::ReduceAdd(
gVec[param]);
1248 std::cout <<
"***** Final gradient : ";
1249 for (
unsigned int ii = 0;
ii<
npar; ++
ii) std::cout << grad[
ii] <<
" ";
1256 double *grad,
unsigned int &,
1268 unsigned int npar = func.NPar();
1269 auto vecSize = vecCore::VectorSize<T>();
1274 std::cout <<
"\n===> Evaluate Gradient for parameters ";
1276 std::cout <<
" " <<
p[
ip];
1282 const T
kdmax1 = vecCore::math::Sqrt(vecCore::NumericLimits<T>::Max());
1290 vecCore::Load<T>(
x1,
data.GetCoordComponent(i *
vecSize, 0));
1292 const T *
x =
nullptr;
1294 unsigned int ndim =
data.NDim();
1295 std::vector<T>
xc(ndim);
1299 for (
unsigned int j = 1;
j < ndim; ++
j)
1312 if (ndim > 1) std::cout << i <<
" x " <<
x[0] <<
" y " <<
x[1] <<
" gradient " <<
gradFunc[0] <<
" " <<
gradFunc[1] <<
" " <<
gradFunc[3] << std::endl;
1313 else std::cout << i <<
" x " <<
x[0] <<
" gradient " <<
gradFunc[0] <<
" " <<
gradFunc[1] <<
" " <<
gradFunc[3] << std::endl;
1349 std::vector<double>
g(
npar);
1357 Warning(
"FitUtil::EvaluateLogLGradient",
1358 "Multithread execution policy requires IMT, which is disabled. Changing "
1359 "to ::ROOT::EExecutionPolicy::kSequential.");
1376 Error(
"FitUtil::EvaluateLogLGradient",
"Execution policy unknown. Available choices:\n "
1377 "::ROOT::EExecutionPolicy::kSequential (default)\n "
1378 "::ROOT::EExecutionPolicy::kMultiThread (requires IMT)\n");
1387 for (
unsigned int param = 0; param <
npar; param++) {
1392 for (
unsigned int param = 0; param <
npar; param++) {
1393 grad[param] = vecCore::ReduceAdd(
gVec[param]);
1397 std::cout <<
"Final gradient ";
1398 for (
unsigned int param = 0; param <
npar; param++) {
1399 std::cout <<
" " << grad[param];
1490#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)