Logo ROOT  
Reference Guide
RooFit::TestStatistics::MinuitFcnGrad Class Reference

Minuit-RooMinimizer interface which synchronizes parameter data and coordinates evaluation of likelihood (gradient) values.

This class provides an interface between RooFit and Minuit. It synchronizes parameter values from Minuit, calls calculator classes to evaluate likelihood and likelihood gradient values and returns them to Minuit. The Wrapper objects do the actual calculations. These are constructed inside the MinuitFcnGrad constructor using the RooAbsL likelihood passed in to the constructor, usually directly from RooMinimizer, with which this class is intimately coupled, being a RooAbsMinimizerFcn implementation. MinuitFcnGrad inherits from ROOT::Math::IMultiGradFunction as well, which allows it to be used as the FCN and GRAD parameters Minuit expects.

Note
The class is not intended for use by end-users. We recommend to either use RooMinimizer with a RooAbsL derived likelihood object, or to use a higher level entry point like RooAbsPdf::fitTo() or RooAbsPdf::createNLL().

Definition at line 56 of file MinuitFcnGrad.h.

Public Member Functions

ROOT::Math::IMultiGradFunctionClone () const override
 Clone a function. More...
 
std::string getFunctionName () const override
 RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class. More...
 
std::string getFunctionTitle () const override
 RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class. More...
 
void Gradient (const double *x, double *grad) const override
 IMultiGradFunction override necessary for Minuit. More...
 
unsigned int NDim () const override
 Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit. More...
 
bool returnsInMinuit2ParameterSpace () const override
 
void setOffsetting (Bool_t flag) override
 Enable or disable offsetting on the function to be minimized, which enhances numerical precision. More...
 
void setOptimizeConstOnFunction (RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt) override
 This function must be overridden in the derived class to pass on constant term optimization configuration to the function to be minimized. More...
 
Bool_t Synchronize (std::vector< ROOT::Fit::ParameterSettings > &parameter_settings, Bool_t optConst, Bool_t verbose=kFALSE) override
 Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization. More...
 
- Public Member Functions inherited from ROOT::Math::IGradientFunctionMultiDimTempl< T >
virtual ~IGradientFunctionMultiDimTempl ()
 Virtual Destructor (no operations) More...
 
virtual void FdF (const T *x, T &f, T *df) const
 Optimized method to evaluate at the same time the function value and derivative at a point x. More...
 
virtual void Gradient (const T *x, T *grad) const
 Evaluate all the vector of function derivatives (gradient) at a point x. More...
 
virtual void GradientWithPrevResult (const T *x, T *grad, T *previous_grad, T *previous_g2, T *previous_gstep) const
 In some cases, the gradient algorithm will use information from the previous step, these can be passed in with this overload. More...
 
virtual unsigned int NDim () const=0
 Retrieve the dimension of the function. More...
 
virtual bool returnsInMinuit2ParameterSpace () const
 
- Public Member Functions inherited from ROOT::Math::IBaseFunctionMultiDimTempl< T >
 IBaseFunctionMultiDimTempl ()
 
virtual ~IBaseFunctionMultiDimTempl ()
 virtual destructor More...
 
virtual IBaseFunctionMultiDimTempl< T > * Clone () const =0
 Clone a function. More...
 
virtual unsigned int NDim () const =0
 Retrieve the dimension of the function. More...
 
operator() (const T *x) const
 Evaluate the function at a point x[]. More...
 
- Public Member Functions inherited from ROOT::Math::IGradientMultiDimTempl< T >
virtual ~IGradientMultiDimTempl ()
 virual destructor More...
 
Derivative (const T *x, unsigned int icoord, T *previous_grad, T *previous_g2, T *previous_gstep) const
 In some cases, the derivative algorithm will use information from the previous step, these can be passed in with this overload. More...
 
Derivative (const T *x, unsigned int icoord=0) const
 Return the partial derivative with respect to the passed coordinate. More...
 
virtual void FdF (const T *x, T &f, T *df) const =0
 Optimized method to evaluate at the same time the function value and derivative at a point x. More...
 
virtual void Gradient (const T *x, T *grad) const =0
 Evaluate all the vector of function derivatives (gradient) at a point x. More...
 
- Public Member Functions inherited from RooAbsMinimizerFcn
 RooAbsMinimizerFcn (const RooAbsMinimizerFcn &other)
 
 RooAbsMinimizerFcn (RooArgList paramList, RooMinimizer *context, bool verbose=false)
 
virtual ~RooAbsMinimizerFcn ()=default
 
void ApplyCovarianceMatrix (TMatrixDSym &V)
 Set different external covariance matrix. More...
 
void BackProp (const ROOT::Fit::FitResult &results)
 Put Minuit results back into RooFit objects. More...
 
Int_t evalCounter () const
 
RooArgListGetConstParamList ()
 
RooArgListGetFloatParamList ()
 Logistics. More...
 
virtual std::string getFunctionName () const =0
 RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class. More...
 
virtual std::string getFunctionTitle () const =0
 RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class. More...
 
RooArgListGetInitConstParamList ()
 
RooArgListGetInitFloatParamList ()
 
std::ofstream * GetLogFile ()
 
Double_tGetMaxFCN ()
 
unsigned int getNDim () const
 
Int_t GetNumInvalidNLL () const
 
double getOffset () const
 Return a possible offset that's applied to the function to separate invalid function values from valid ones. More...
 
bool getOptConst ()
 
std::vector< doublegetParameterValues () const
 
void SetEvalErrorWall (Bool_t flag)
 
Bool_t SetLogFile (const char *inLogfile)
 Change the file name for logging of a RooMinimizer of all MINUIT steppings through the parameter space. More...
 
virtual void setOffsetting (Bool_t flag)=0
 Enable or disable offsetting on the function to be minimized, which enhances numerical precision. More...
 
void setOptimizeConst (Int_t flag)
 
Bool_t SetPdfParamVal (int index, double value) const
 Set value of parameter i. More...
 
void SetPrintEvalErrors (Int_t numEvalErrors)
 
void SetRecoverFromNaNStrength (double strength)
 Try to recover from invalid function values. More...
 
void SetVerbose (Bool_t flag=kTRUE)
 
virtual Bool_t Synchronize (std::vector< ROOT::Fit::ParameterSettings > &parameters, Bool_t optConst, Bool_t verbose)
 Like synchronizeParameterSettings, Synchronize informs Minuit through its parameter_settings vector of RooFit parameter properties, but Synchronize can be overridden to e.g. More...
 
Bool_t synchronizeParameterSettings (std::vector< ROOT::Fit::ParameterSettings > &parameters, Bool_t optConst, Bool_t verbose)
 Informs Minuit through its parameter_settings vector of RooFit parameter properties. More...
 
void zeroEvalCount ()
 

Static Public Member Functions

template<typename LikelihoodWrapperT , typename LikelihoodGradientWrapperT >
static MinuitFcnGradcreate (const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &likelihood, RooMinimizer *context, std::vector< ROOT::Fit::ParameterSettings > &parameters, bool verbose=false)
 

Public Attributes

std::shared_ptr< WrapperCalculationCleanFlagscalculation_is_clean
 
bool minuit_internal_roofit_x_mismatch_ = false
 

Private Member Functions

template<typename LikelihoodWrapperT , typename LikelihoodGradientWrapperT >
 MinuitFcnGrad (const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &_likelihood, RooMinimizer *context, std::vector< ROOT::Fit::ParameterSettings > &parameters, bool verbose, LikelihoodWrapperT *=static_cast< LikelihoodWrapperT * >(nullptr), LikelihoodGradientWrapperT *=static_cast< LikelihoodGradientWrapperT * >(nullptr))
 
double DoDerivative (const double *x, unsigned int icoord) const override
 This override should not be used in this class, so it throws. More...
 
double DoEval (const double *x) const override
 IMultiGradFunction override necessary for Minuit. More...
 
bool syncParameterValuesFromMinuitCalls (const double *x, bool minuit_internal) const
 Minuit calls (via FcnAdapters etc) DoEval or Gradient with a set of parameters x. More...
 

Private Attributes

std::shared_ptr< LikelihoodGradientWrappergradient
 
std::shared_ptr< LikelihoodWrapperlikelihood
 
std::vector< doubleminuit_external_x_
 
std::vector< doubleminuit_internal_x_
 

Additional Inherited Members

- Public Types inherited from ROOT::Math::IGradientFunctionMultiDimTempl< T >
typedef IBaseFunctionMultiDimTempl< T > BaseFunc
 
typedef IGradientMultiDimTempl< T > BaseGrad
 
- Public Types inherited from ROOT::Math::IBaseFunctionMultiDimTempl< T >
typedef T BackendType
 
typedef IBaseFunctionMultiDimTempl< T > BaseFunc
 
- Protected Member Functions inherited from RooAbsMinimizerFcn
void ClearPdfParamAsymErr (Int_t index)
 Modify PDF parameter error by ordinal index (needed by MINUIT) More...
 
void optimizeConstantTerms (bool constStatChange, bool constValChange)
 
void printEvalErrors () const
 Print information about why evaluation failed. More...
 
virtual void setOptimizeConstOnFunction (RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt)=0
 This function must be overridden in the derived class to pass on constant term optimization configuration to the function to be minimized. More...
 
void SetPdfParamErr (Int_t index, Double_t loVal, Double_t hiVal)
 Modify PDF parameter error by ordinal index (needed by MINUIT) More...
 
void SetPdfParamErr (Int_t index, Double_t value)
 Modify PDF parameter error by ordinal index (needed by MINUIT) More...
 
- Protected Attributes inherited from RooAbsMinimizerFcn
std::unique_ptr< RooArgList_constParamList
 
RooMinimizer_context
 
bool _doEvalErrorWall {true}
 
int _evalCounter {0}
 
std::unique_ptr< RooArgList_floatParamList
 
double _funcOffset {0.}
 
std::unique_ptr< RooArgList_initConstParamList
 
std::unique_ptr< RooArgList_initFloatParamList
 
std::ofstream * _logfile = nullptr
 
double _maxFCN = -std::numeric_limits<double>::infinity()
 
unsigned int _nDim = 0
 
int _numBadNLL = 0
 
Bool_t _optConst = kFALSE
 
int _printEvalErrors = 10
 
double _recoverFromNaNStrength {10.}
 
bool _verbose
 

#include <TestStatistics/MinuitFcnGrad.h>

Inheritance diagram for RooFit::TestStatistics::MinuitFcnGrad:
[legend]

Constructor & Destructor Documentation

◆ MinuitFcnGrad()

template<typename LikelihoodWrapperT , typename LikelihoodGradientWrapperT >
RooFit::TestStatistics::MinuitFcnGrad::MinuitFcnGrad ( const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &  _likelihood,
RooMinimizer context,
std::vector< ROOT::Fit::ParameterSettings > &  parameters,
bool  verbose,
LikelihoodWrapperT *  = static_cast<LikelihoodWrapperT *>(nullptr),
LikelihoodGradientWrapperT *  = static_cast<LikelihoodGradientWrapperT *>(nullptr) 
)
private
Parameters
[in]contextRooMinimizer that creates and owns this class.
[in]parametersThe vector of ParameterSettings objects that describe the parameters used in the Minuit Fitter. Note that these must match the set used in the Fitter used by context! It can be passed in from RooMinimizer with fitter()->Config().ParamsSettings().

Definition at line 132 of file MinuitFcnGrad.h.

Member Function Documentation

◆ Clone()

ROOT::Math::IMultiGradFunction * RooFit::TestStatistics::MinuitFcnGrad::Clone ( ) const
inlineoverridevirtual

Clone a function.

Each derived class must implement their version of the Clone method

Implements ROOT::Math::IBaseFunctionMultiDimTempl< T >.

Definition at line 65 of file MinuitFcnGrad.h.

◆ create()

template<typename LikelihoodWrapperT , typename LikelihoodGradientWrapperT >
MinuitFcnGrad * RooFit::TestStatistics::MinuitFcnGrad::create ( const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &  likelihood,
RooMinimizer context,
std::vector< ROOT::Fit::ParameterSettings > &  parameters,
bool  verbose = false 
)
static

Definition at line 156 of file MinuitFcnGrad.h.

◆ DoDerivative()

double RooFit::TestStatistics::MinuitFcnGrad::DoDerivative ( const double x,
unsigned int  icoord 
) const
overrideprivate

This override should not be used in this class, so it throws.

Definition at line 202 of file MinuitFcnGrad.cxx.

◆ DoEval()

double RooFit::TestStatistics::MinuitFcnGrad::DoEval ( const double x) const
overrideprivate

IMultiGradFunction override necessary for Minuit.

Definition at line 43 of file MinuitFcnGrad.cxx.

◆ getFunctionName()

std::string RooFit::TestStatistics::MinuitFcnGrad::getFunctionName ( ) const
inlineoverridevirtual

RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.

Implements RooAbsMinimizerFcn.

Definition at line 90 of file MinuitFcnGrad.h.

◆ getFunctionTitle()

std::string RooFit::TestStatistics::MinuitFcnGrad::getFunctionTitle ( ) const
inlineoverridevirtual

RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class.

Implements RooAbsMinimizerFcn.

Definition at line 92 of file MinuitFcnGrad.h.

◆ Gradient()

void RooFit::TestStatistics::MinuitFcnGrad::Gradient ( const double x,
double grad 
) const
override

IMultiGradFunction override necessary for Minuit.

Definition at line 196 of file MinuitFcnGrad.cxx.

◆ NDim()

unsigned int RooFit::TestStatistics::MinuitFcnGrad::NDim ( ) const
inlineoverridevirtual

Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit.

Implements ROOT::Math::IBaseFunctionMultiDimTempl< T >.

Definition at line 88 of file MinuitFcnGrad.h.

◆ returnsInMinuit2ParameterSpace()

bool RooFit::TestStatistics::MinuitFcnGrad::returnsInMinuit2ParameterSpace ( ) const
inlineoverridevirtual

Reimplemented from ROOT::Math::IGradientFunctionMultiDimTempl< T >.

Definition at line 72 of file MinuitFcnGrad.h.

◆ setOffsetting()

void RooFit::TestStatistics::MinuitFcnGrad::setOffsetting ( Bool_t  flag)
inlineoverridevirtual

Enable or disable offsetting on the function to be minimized, which enhances numerical precision.

Implements RooAbsMinimizerFcn.

Definition at line 94 of file MinuitFcnGrad.h.

◆ setOptimizeConstOnFunction()

void RooFit::TestStatistics::MinuitFcnGrad::setOptimizeConstOnFunction ( RooAbsArg::ConstOpCode  opcode,
Bool_t  doAlsoTrackingOpt 
)
inlineoverridevirtual

This function must be overridden in the derived class to pass on constant term optimization configuration to the function to be minimized.

For a RooAbsArg, this would be RooAbsArg::constOptimizeTestStatistic.

Implements RooAbsMinimizerFcn.

Definition at line 74 of file MinuitFcnGrad.h.

◆ Synchronize()

Bool_t RooFit::TestStatistics::MinuitFcnGrad::Synchronize ( std::vector< ROOT::Fit::ParameterSettings > &  parameter_settings,
Bool_t  optConst,
Bool_t  verbose = kFALSE 
)
overridevirtual

Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.

Reimplemented from RooAbsMinimizerFcn.

Definition at line 208 of file MinuitFcnGrad.cxx.

◆ syncParameterValuesFromMinuitCalls()

bool RooFit::TestStatistics::MinuitFcnGrad::syncParameterValuesFromMinuitCalls ( const double x,
bool  minuit_internal 
) const
private

Minuit calls (via FcnAdapters etc) DoEval or Gradient with a set of parameters x.

This function syncs these values to the proper places in RooFit.

The first twist, and reason this function is more complicated than one may imagine, is that Minuit internally uses a transformed parameter space to account for parameter boundaries. Whether we receive these Minuit "internal" parameter values or "regular"/untransformed RooFit parameter space values depends on the situation.

  • The values that arrive here via DoEval are always "normal" parameter values, since Minuit transforms these back into regular space before passing to DoEval (see MnUserFcn::operator() which wraps the Fcn(Gradient)Base in ModularFunctionMinimizer::Minimize and is used for direct function calls from that point on in the minimizer). These can thus always be safely synced with this function's RooFit parameters using SetPdfParamVal.
  • The values that arrive here via Gradient will be in internal coordinates if that is what this class expects, and indeed this is the case for MinuitFcnGrad's current implementation. This is communicated to Minuit via MinuitFcnGrad::returnsInMinuit2ParameterSpace. Inside Minuit, that function determines whether this class's gradient calculator is wrapped inside a AnalyticalGradientCalculator, to which Minuit passes "external" parameter values, or as an ExternalInternalGradientCalculator, which gets "internal" parameter values. Long story short: when MinuitFcnGrad::returnsInMinuit2ParameterSpace() returns true, Minuit will pass "internal" values to Gradient. These cannot be synced with this function's RooFit parameters using SetPdfParamVal, unless a manual transformation step is performed in advance. However, they do need to be passed on to the gradient calculator, since indeed we expect values there to be in "internal" space. However, this is calculator dependent. Note that in the current MinuitFcnGrad implementation we do not actually allow for calculators in "external" (i.e. regular RooFit parameter space) values, since MinuitFcnGrad::returnsInMinuit2ParameterSpace is hardcoded to true. This should in a future version be changed so that the calculator (the wrapper) is queried for this information. Because some gradient calculators may also use the regular RooFit parameters (e.g. for calculating the likelihood's value itself), this information is also passed on to the gradient wrapper. Vice versa, when updated "internal" parameters are passed to Gradient, the likelihood may be affected as well. Even though a transformation from internal to "external" may be necessary before the values can be used, the likelihood can at least log that its parameter values are possibly no longer in sync with those of the gradient.

The second twist is that the Minuit external parameters may still be different from the ones used in RooFit. This happens when Minuit tries out values that lay outside the RooFit parameter's range(s). RooFit's setVal (called inside SetPdfParamVal) then clips the RooAbsArg's value to one of the range limits, instead of setting it to the value Minuit intended. When this happens, i.e. syncParameterValuesFromMinuitCalls is called with minuit_internal = false and the values do not match the previous values stored in minuit_internal_x_ but the values after SetPdfParamVal did not get set to the intended value, the minuit_internal_roofit_x_mismatch_ flag is set. This information can be used by calculators, if desired, for instance when a calculator does not want to make use of the range information in the RooAbsArg parameters.

Definition at line 147 of file MinuitFcnGrad.cxx.

Member Data Documentation

◆ calculation_is_clean

std::shared_ptr<WrapperCalculationCleanFlags> RooFit::TestStatistics::MinuitFcnGrad::calculation_is_clean
mutable

Definition at line 116 of file MinuitFcnGrad.h.

◆ gradient

std::shared_ptr<LikelihoodGradientWrapper> RooFit::TestStatistics::MinuitFcnGrad::gradient
private

Definition at line 113 of file MinuitFcnGrad.h.

◆ likelihood

std::shared_ptr<LikelihoodWrapper> RooFit::TestStatistics::MinuitFcnGrad::likelihood
private

Definition at line 112 of file MinuitFcnGrad.h.

◆ minuit_external_x_

std::vector<double> RooFit::TestStatistics::MinuitFcnGrad::minuit_external_x_
mutableprivate

Definition at line 120 of file MinuitFcnGrad.h.

◆ minuit_internal_roofit_x_mismatch_

bool RooFit::TestStatistics::MinuitFcnGrad::minuit_internal_roofit_x_mismatch_ = false
mutable

Definition at line 123 of file MinuitFcnGrad.h.

◆ minuit_internal_x_

std::vector<double> RooFit::TestStatistics::MinuitFcnGrad::minuit_internal_x_
mutableprivate

Definition at line 119 of file MinuitFcnGrad.h.

Libraries for RooFit::TestStatistics::MinuitFcnGrad:
[legend]

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