Skip to content

Commit

Permalink
[math] Don't incl. "Math/Error.h" and "Math/Util.h" in Minuit2 headers
Browse files Browse the repository at this point in the history
The "Math/Error.h" header is not shipped with standalone Minuit2.

It is not a perfect solution to just ship it with Minuit2, because the
Error.h header behaves differently depending on the
`MATHCORE_STANDALONE` macro being defined or not. The code would only
work correctly if the user defines the `MATHCORE_STANDALONE` herself in
the user code that uses standalone Minuit2, which would be annoying.

Instead, this commit proposes another solution to the problem: for all
headers also used in Minuit2 standalone, MathCore
moves the definitions of all functions that use `Math/Error.h` out of
the header files in the cxx files. Like this, the `Math/Error.h` is only
a build dependency of standalone Minuit2, and the user doesn't need to
define the `MATHCORE_STANDALONE` macro for it to work.

Including the "Math/Util.h" header needs to be avoided for similar
reasons (it's about another preprocessor macro related to `veccore`).
  • Loading branch information
guitargeek committed Sep 14, 2023
1 parent ca43bce commit 160d396
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 239 deletions.
2 changes: 2 additions & 0 deletions math/mathcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ ROOT_STANDARD_LIBRARY_PACKAGE(MathCore
src/GaussLegendreIntegrator.cxx
src/GenAlgoOptions.cxx
src/GoFTest.cxx
src/IOptions.cxx
src/Integrator.cxx
src/IntegratorOptions.cxx
src/MersenneTwisterEngine.cxx
src/MinimTransformFunction.cxx
src/Minimizer.cxx
src/MinimizerOptions.cxx
src/MinimizerVariableTransformation.cxx
src/MixMaxEngineImpl17.cxx
Expand Down
27 changes: 1 addition & 26 deletions math/mathcore/inc/Fit/ParameterSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

#include <string>

#include "Math/Error.h"


namespace ROOT {

namespace Fit {
Expand Down Expand Up @@ -140,29 +137,7 @@ class ParameterSettings {
void SetValue(double val) {fValue = val;}
/// set the step size
void SetStepSize(double err) {fStepSize = err;}
/// set a double side limit,
/// if low == up the parameter is fixed if low > up the limits are removed
/// The current parameter value should be within the given limits [low,up].
/// If the value is outside the limits, then a new parameter value is set to = (up+low)/2
void SetLimits(double low, double up) {

if ( low > up ) {
RemoveLimits();
return;
}
if (low == up && low == fValue) {
Fix();
return;
}
if (low > fValue || up < fValue) {
MATH_INFO_MSG("ParameterSettings","lower/upper bounds outside current parameter value. The value will be set to (low+up)/2 ");
fValue = 0.5 * (up+low);
}
fLowerLimit = low;
fUpperLimit = up;
fHasLowerLimit = true;
fHasUpperLimit = true;
}
void SetLimits(double low, double up);
/// set a single upper limit
void SetUpperLimit(double up) {
fLowerLimit = 0.;
Expand Down
43 changes: 7 additions & 36 deletions math/mathcore/inc/Math/IOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#ifndef ROOT_Math_IOptions
#define ROOT_Math_IOptions


#include "Math/Error.h"

#include <iostream>
#include <string>

Expand Down Expand Up @@ -47,26 +44,9 @@ class IOptions {
void SetValue(const char * name, const char * val) { SetNamedValue(name,val);}


double RValue(const char * name) const {
double val = 0;
bool ret = GetRealValue(name,val);
if (!ret ) MATH_ERROR_MSGVAL("IOptions::RValue"," return 0 - real option not found",name);
return val;
}

int IValue(const char * name) const {
int val = 0;
bool ret = GetIntValue(name,val);
if (!ret ) MATH_ERROR_MSGVAL("IOptions::IValue"," return 0 - integer option not found",name);
return val;
}

std::string NamedValue(const char * name) const {
std::string val;
bool ret = GetNamedValue(name,val);
if (!ret ) MATH_ERROR_MSGVAL("IOptions::NamedValue"," return empty string - named option not found",name);
return val;
}
double RValue(const char * name) const;
int IValue(const char * name) const;
std::string NamedValue(const char * name) const;


// generic method to retrieve a type
Expand All @@ -80,24 +60,15 @@ class IOptions {

// methods to be re-implemented in the derived classes


virtual bool GetRealValue(const char *, double &) const { return false; }

virtual bool GetIntValue(const char *, int &) const { return false; }

virtual bool GetNamedValue(const char *, std::string &) const { return false; }

/// method wich need to be re-implemented by the derived classes
virtual void SetRealValue(const char * , double ) {MATH_ERROR_MSG("IOptions::SetRealValue","Invalid setter method called"); }

virtual void SetIntValue(const char * , int ) {MATH_ERROR_MSG("IOptions::SetIntValue","Invalid setter method called"); }

virtual void SetNamedValue(const char * , const char * ) {MATH_ERROR_MSG("IOptions::SetNamedValue","Invalid setter method called"); }


/// print options
virtual void Print(std::ostream & = std::cout ) const {MATH_INFO_MSG("IOptions::Print","it is not implemented");}
virtual void SetRealValue(const char * , double );
virtual void SetIntValue(const char * , int );
virtual void SetNamedValue(const char * , const char * );

virtual void Print(std::ostream & = std::cout ) const;

private:

Expand Down
185 changes: 20 additions & 165 deletions math/mathcore/inc/Math/Minimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@
#define ROOT_Math_Minimizer

#include "Math/IFunction.h"

#include "Math/MinimizerOptions.h"

#include "Math/Util.h"

#include "Math/Error.h"


#include <string>
#include <limits>
#include <cmath>
Expand Down Expand Up @@ -200,25 +194,10 @@ class Minimizer {
virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper ) {
return SetLimitedVariable(ivar, name, val, step, - std::numeric_limits<double>::infinity(), upper );
}
/// set a new upper/lower limited variable (override if minimizer supports them ) otherwise as default set an unlimited variable
virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step ,
double lower , double upper ) {
MATH_WARN_MSG("Minimizer::SetLimitedVariable","Setting of limited variable not implemented - set as unlimited");
MATH_UNUSED(lower); MATH_UNUSED(upper);
return SetVariable(ivar, name, val, step);
}
/// set a new fixed variable (override if minimizer supports them )
virtual bool SetFixedVariable(unsigned int ivar , const std::string & name , double val ) {
MATH_ERROR_MSG("Minimizer::SetFixedVariable","Setting of fixed variable not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(name); MATH_UNUSED(val);
return false;
}
/// set the value of an already existing variable
virtual bool SetVariableValue(unsigned int ivar , double value) {
MATH_ERROR_MSG("Minimizer::SetVariableValue","Set of a variable value not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(value);
return false;
}
double lower , double upper );
virtual bool SetFixedVariable(unsigned int ivar , const std::string & name , double val );
virtual bool SetVariableValue(unsigned int ivar , double value);
/// set the values of all existing variables (array must be dimensioned to the size of the existing parameters)
virtual bool SetVariableValues(const double * x) {
bool ret = true;
Expand All @@ -228,54 +207,17 @@ class Minimizer {
}
return ret;
}
/// set the step size of an already existing variable
virtual bool SetVariableStepSize(unsigned int ivar, double value ) {
MATH_ERROR_MSG("Minimizer::SetVariableStepSize","Setting an existing variable step size not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(value);
return false;
}
/// set the lower-limit of an already existing variable
virtual bool SetVariableLowerLimit(unsigned int ivar, double lower) {
MATH_ERROR_MSG("Minimizer::SetVariableLowerLimit","Setting an existing variable limit not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(lower);
return false;
}
/// set the upper-limit of an already existing variable
virtual bool SetVariableUpperLimit(unsigned int ivar, double upper) {
MATH_ERROR_MSG("Minimizer::SetVariableUpperLimit","Setting an existing variable limit not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(upper);
return false;
}
virtual bool SetVariableStepSize(unsigned int ivar, double value );
virtual bool SetVariableLowerLimit(unsigned int ivar, double lower);
virtual bool SetVariableUpperLimit(unsigned int ivar, double upper);
/// set the limits of an already existing variable
virtual bool SetVariableLimits(unsigned int ivar, double lower, double upper) {
return SetVariableLowerLimit(ivar,lower) && SetVariableUpperLimit(ivar,upper);
}
/// fix an existing variable
virtual bool FixVariable(unsigned int ivar) {
MATH_ERROR_MSG("Minimizer::FixVariable","Fixing an existing variable not implemented");
MATH_UNUSED(ivar);
return false;
}
/// release an existing variable
virtual bool ReleaseVariable(unsigned int ivar) {
MATH_ERROR_MSG("Minimizer::ReleaseVariable","Releasing an existing variable not implemented");
MATH_UNUSED(ivar);
return false;
}
/// query if an existing variable is fixed (i.e. considered constant in the minimization)
/// note that by default all variables are not fixed
virtual bool IsFixedVariable(unsigned int ivar) const {
MATH_ERROR_MSG("Minimizer::IsFixedVariable","Querying an existing variable not implemented");
MATH_UNUSED(ivar);
return false;
}
/// get variable settings in a variable object (like ROOT::Fit::ParamsSettings)
virtual bool GetVariableSettings(unsigned int ivar, ROOT::Fit::ParameterSettings & pars) const {
MATH_ERROR_MSG("Minimizer::GetVariableSettings","Querying an existing variable not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(pars);
return false;
}

virtual bool FixVariable(unsigned int ivar);
virtual bool ReleaseVariable(unsigned int ivar);
virtual bool IsFixedVariable(unsigned int ivar) const;
virtual bool GetVariableSettings(unsigned int ivar, ROOT::Fit::ParameterSettings & pars) const;

/// set the initial range of an existing variable
virtual bool SetVariableInitialRange(unsigned int /* ivar */, double /* mininitial */, double /* maxinitial */) {
Expand Down Expand Up @@ -318,39 +260,9 @@ class Minimizer {
/// return errors at the minimum
virtual const double * Errors() const { return nullptr; }

/** return covariance matrices element for variables ivar,jvar
if the variable is fixed the return value is zero
The ordering of the variables is the same as in the parameter and errors vectors
*/
virtual double CovMatrix(unsigned int ivar , unsigned int jvar ) const {
MATH_UNUSED(ivar); MATH_UNUSED(jvar);
return 0;
}

/**
Fill the passed array with the covariance matrix elements
if the variable is fixed or const the value is zero.
The array will be filled as cov[i *ndim + j]
The ordering of the variables is the same as in errors and parameter value.
This is different from the direct interface of Minuit2 or TMinuit where the
values were obtained only to variable parameters
*/
virtual bool GetCovMatrix(double * covMat) const {
MATH_UNUSED(covMat);
return false;
}

/**
Fill the passed array with the Hessian matrix elements
The Hessian matrix is the matrix of the second derivatives
and is the inverse of the covariance matrix
If the variable is fixed or const the values for that variables are zero.
The array will be filled as h[i *ndim + j]
*/
virtual bool GetHessianMatrix(double * hMat) const {
MATH_UNUSED(hMat);
return false;
}
virtual double CovMatrix(unsigned int ivar , unsigned int jvar ) const;
virtual bool GetCovMatrix(double * covMat) const;
virtual bool GetHessianMatrix(double * hMat) const;


///return status of covariance matrix
Expand All @@ -369,81 +281,24 @@ class Minimizer {
return ( tmp < 0) ? 0 : CovMatrix(i,j) / std::sqrt( tmp );
}

/**
return global correlation coefficient for variable i
This is a number between zero and one which gives
the correlation between the i-th parameter and that linear combination of all
other parameters which is most strongly correlated with i.
Minimizer must overload method if implemented
*/
virtual double GlobalCC(unsigned int ivar) const {
MATH_UNUSED(ivar);
return -1;
}

/**
minos error for variable i, return false if Minos failed or not supported
and the lower and upper errors are returned in errLow and errUp
An extra flag specifies if only the lower (option=-1) or the upper (option=+1) error calculation is run
*/
virtual bool GetMinosError(unsigned int ivar , double & errLow, double & errUp, int option = 0) {
MATH_ERROR_MSG("Minimizer::GetMinosError","Minos Error not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(errLow); MATH_UNUSED(errUp); MATH_UNUSED(option);
return false;
}
virtual double GlobalCC(unsigned int ivar) const;

/**
perform a full calculation of the Hessian matrix for error calculation
*/
virtual bool Hesse() {
MATH_ERROR_MSG("Minimizer::Hesse","Hesse not implemented");
return false;
}

/**
scan function minimum for variable i. Variable and function must be set before using Scan
Return false if an error or if minimizer does not support this functionality
*/
virtual bool GetMinosError(unsigned int ivar , double & errLow, double & errUp, int option = 0);
virtual bool Hesse();
virtual bool Scan(unsigned int ivar , unsigned int & nstep , double * x , double * y ,
double xmin = 0, double xmax = 0) {
MATH_ERROR_MSG("Minimizer::Scan","Scan not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(nstep); MATH_UNUSED(x); MATH_UNUSED(y);
MATH_UNUSED(xmin); MATH_UNUSED(xmax);
return false;
}

/**
find the contour points (xi, xj) of the function for parameter ivar and jvar around the minimum
The contour will be find for value of the function = Min + ErrorUp();
*/
double xmin = 0, double xmax = 0);
virtual bool Contour(unsigned int ivar , unsigned int jvar, unsigned int & npoints,
double * xi , double * xj ) {
MATH_ERROR_MSG("Minimizer::Contour","Contour not implemented");
MATH_UNUSED(ivar); MATH_UNUSED(jvar); MATH_UNUSED(npoints);
MATH_UNUSED(xi); MATH_UNUSED(xj);
return false;
}
double * xi , double * xj );

/// return reference to the objective function
///virtual const ROOT::Math::IGenFunction & Function() const = 0;

/// print the result according to set level (implemented for TMinuit for maintaining Minuit-style printing)
virtual void PrintResults() {}

/// get name of variables (override if minimizer support storing of variable names)
/// return an empty string if variable is not found
virtual std::string VariableName(unsigned int ivar) const {
MATH_UNUSED(ivar);
return std::string(); // return empty string
}
virtual std::string VariableName(unsigned int ivar) const;

/// get index of variable given a variable given a name
/// return -1 if variable is not found
virtual int VariableIndex(const std::string & name) const {
MATH_ERROR_MSG("Minimizer::VariableIndex","Getting variable index from name not implemented");
MATH_UNUSED(name);
return -1;
}
virtual int VariableIndex(const std::string & name) const;

/** minimizer configuration parameters **/

Expand Down
Loading

0 comments on commit 160d396

Please sign in to comment.