Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 31 of file MinuitFcnGrad.h.

Public Member Functions

 MinuitFcnGrad (const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &absL, RooMinimizer *context, std::vector< ROOT::Fit::ParameterSettings > &parameters, LikelihoodMode likelihoodMode, LikelihoodGradientMode likelihoodGradientMode)
 
std::string getFunctionName () const override
 RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
 
std::string getFunctionTitle () const override
 RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class.
 
ROOT::Math::IMultiGenFunctiongetMultiGenFcn () override
 
void Gradient (const double *x, double *grad) const
 IMultiGradFunction overrides necessary for Minuit.
 
void GradientWithPrevResult (const double *x, double *grad, double *previous_grad, double *previous_g2, double *previous_gstep) const
 
double operator() (const double *x) const
 
bool returnsInMinuit2ParameterSpace () const
 
void setOffsetting (bool flag) override
 Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
 
void setOptimizeConstOnFunction (RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) override
 This function must be overridden in the derived class to pass on constant term optimization configuration to the function to be minimized.
 
bool Synchronize (std::vector< ROOT::Fit::ParameterSettings > &parameter_settings) override
 Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.
 
- Public Member Functions inherited from RooAbsMinimizerFcn
 RooAbsMinimizerFcn (const RooAbsMinimizerFcn &other)
 
 RooAbsMinimizerFcn (RooArgList paramList, RooMinimizer *context)
 
virtual ~RooAbsMinimizerFcn ()=default
 
void ApplyCovarianceMatrix (TMatrixDSym &V)
 Set different external covariance matrix.
 
void BackProp (const ROOT::Fit::FitResult &results)
 Put Minuit results back into RooFit objects.
 
RooMinimizer::Config const & cfg () const
 
Int_t evalCounter () const
 
RooArgListGetConstParamList ()
 
RooArgListGetFloatParamList ()
 
RooArgListGetInitConstParamList ()
 
RooArgListGetInitFloatParamList ()
 
std::ofstream * GetLogFile ()
 
doubleGetMaxFCN ()
 
unsigned int getNDim () const
 
Int_t GetNumInvalidNLL () const
 
doublegetOffset () const
 Return a possible offset that's applied to the function to separate invalid function values from valid ones.
 
std::vector< doublegetParameterValues () const
 
unsigned int NDim () const
 
bool SetLogFile (const char *inLogfile)
 Change the file name for logging of a RooMinimizer of all MINUIT steppings through the parameter space.
 
void setOptimizeConst (Int_t flag)
 
bool SetPdfParamVal (int index, double value) const
 Set value of parameter i.
 
bool synchronizeParameterSettings (std::vector< ROOT::Fit::ParameterSettings > &parameters, bool optConst)
 Informs Minuit through its parameter_settings vector of RooFit parameter properties.
 
void zeroEvalCount ()
 

Private Member Functions

template<class Func >
void applyToLikelihood (Func &&func) const
 
void syncOffsets () const
 Make sure the offsets are up to date.
 
bool syncParameterValuesFromMinuitCalls (const double *x, bool minuit_internal) const
 Minuit calls (via FcnAdapters etc) DoEval or Gradient with a set of parameters x.
 

Private Attributes

bool _calculatingGradient = false
 
std::shared_ptr< WrapperCalculationCleanFlags_calculationIsClean
 
std::unique_ptr< LikelihoodGradientWrapper_gradient
 
std::shared_ptr< LikelihoodWrapper_likelihood
 
std::shared_ptr< LikelihoodWrapper_likelihoodInGradient
 
std::vector< double_minuitExternalX
 
bool _minuitInternalRooFitXMismatch = false
 
std::vector< double_minuitInternalX
 
std::unique_ptr< ROOT::Math::IMultiGradFunction_multiGenFcn
 
bool offsets_reset_ = false
 

Additional Inherited Members

- Protected Member Functions inherited from RooAbsMinimizerFcn
double applyEvalErrorHandling (double fvalue) const
 Apply corrections on the fvalue if errors were signaled.
 
void ClearPdfParamAsymErr (Int_t index)
 Modify PDF parameter error by ordinal index (needed by MINUIT)
 
void finishDoEval () const
 
void optimizeConstantTerms (bool constStatChange, bool constValChange)
 
void printEvalErrors () const
 Print information about why evaluation failed.
 
void SetPdfParamErr (Int_t index, double loVal, double hiVal)
 Modify PDF parameter error by ordinal index (needed by MINUIT)
 
void SetPdfParamErr (Int_t index, double value)
 Modify PDF parameter error by ordinal index (needed by MINUIT)
 
- Protected Attributes inherited from RooAbsMinimizerFcn
std::unique_ptr< RooArgList_constParamList
 
RooMinimizer_context
 
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 _optConst = false
 

#include </home/sftnight/build/workspace/root-makedoc-v632/rootspi/rdoc/src/v6-32-00-patches/roofit/roofitcore/src/TestStatistics/MinuitFcnGrad.h>

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

Constructor & Destructor Documentation

◆ MinuitFcnGrad()

RooFit::TestStatistics::MinuitFcnGrad::MinuitFcnGrad ( const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &  absL,
RooMinimizer context,
std::vector< ROOT::Fit::ParameterSettings > &  parameters,
LikelihoodMode  likelihoodMode,
LikelihoodGradientMode  likelihoodGradientMode 
)
Parameters
[in]absLThe input likelihood.
[in]contextRooMinimizer that creates and owns this class.
[in]parametersThe vector of ParameterSettings objects that describe the parameters used in the Minuit
[in]likelihoodModeLmode
[in]likelihoodGradientModeLgrad
[in]verbosetrue for verbose output 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 83 of file MinuitFcnGrad.cxx.

Member Function Documentation

◆ applyToLikelihood()

template<class Func >
void RooFit::TestStatistics::MinuitFcnGrad::applyToLikelihood ( Func &&  func) const
inlineprivate

Definition at line 73 of file MinuitFcnGrad.h.

◆ 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 57 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 59 of file MinuitFcnGrad.h.

◆ getMultiGenFcn()

ROOT::Math::IMultiGenFunction * RooFit::TestStatistics::MinuitFcnGrad::getMultiGenFcn ( )
inlineoverridevirtual

Implements RooAbsMinimizerFcn.

Definition at line 48 of file MinuitFcnGrad.h.

◆ Gradient()

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

IMultiGradFunction overrides necessary for Minuit.

Definition at line 248 of file MinuitFcnGrad.cxx.

◆ GradientWithPrevResult()

void RooFit::TestStatistics::MinuitFcnGrad::GradientWithPrevResult ( const double x,
double grad,
double previous_grad,
double previous_g2,
double previous_gstep 
) const

Definition at line 257 of file MinuitFcnGrad.cxx.

◆ operator()()

double RooFit::TestStatistics::MinuitFcnGrad::operator() ( const double x) const

Definition at line 135 of file MinuitFcnGrad.cxx.

◆ returnsInMinuit2ParameterSpace()

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

Definition at line 41 of file MinuitFcnGrad.h.

◆ setOffsetting()

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

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

Implements RooAbsMinimizerFcn.

Definition at line 61 of file MinuitFcnGrad.h.

◆ setOptimizeConstOnFunction()

void RooFit::TestStatistics::MinuitFcnGrad::setOptimizeConstOnFunction ( RooAbsArg::ConstOpCode  opcode,
bool  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 43 of file MinuitFcnGrad.h.

◆ Synchronize()

bool RooFit::TestStatistics::MinuitFcnGrad::Synchronize ( std::vector< ROOT::Fit::ParameterSettings > &  parameter_settings)
overridevirtual

Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.

Reimplemented from RooAbsMinimizerFcn.

Definition at line 267 of file MinuitFcnGrad.cxx.

◆ syncOffsets()

void RooFit::TestStatistics::MinuitFcnGrad::syncOffsets ( ) const
private

Make sure the offsets are up to date.

If the offsets need to be updated, this function triggers a likelihood evaluation. The likelihood will make sure the offset is set correctly in their shared_ptr offsets object, that is also shared with possible other LikelihoodWrapper members of MinuitFcnGrad and also the LikelihoodGradientWrapper member. Other necessary synchronization steps are also performed from the Wrapper child classes (e.g. sending the values to workers from MultiProcess::Jobs).

Definition at line 127 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 _minuitInternalX but the values after SetPdfParamVal did not get set to the intended value, the _minuitInternalRooFitXMismatch 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 197 of file MinuitFcnGrad.cxx.

Member Data Documentation

◆ _calculatingGradient

bool RooFit::TestStatistics::MinuitFcnGrad::_calculatingGradient = false
mutableprivate

Definition at line 86 of file MinuitFcnGrad.h.

◆ _calculationIsClean

std::shared_ptr<WrapperCalculationCleanFlags> RooFit::TestStatistics::MinuitFcnGrad::_calculationIsClean
mutableprivate

Definition at line 88 of file MinuitFcnGrad.h.

◆ _gradient

std::unique_ptr<LikelihoodGradientWrapper> RooFit::TestStatistics::MinuitFcnGrad::_gradient
private

Definition at line 85 of file MinuitFcnGrad.h.

◆ _likelihood

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

Definition at line 83 of file MinuitFcnGrad.h.

◆ _likelihoodInGradient

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

Definition at line 84 of file MinuitFcnGrad.h.

◆ _minuitExternalX

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

Definition at line 91 of file MinuitFcnGrad.h.

◆ _minuitInternalRooFitXMismatch

bool RooFit::TestStatistics::MinuitFcnGrad::_minuitInternalRooFitXMismatch = false
mutableprivate

Definition at line 99 of file MinuitFcnGrad.h.

◆ _minuitInternalX

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

Definition at line 90 of file MinuitFcnGrad.h.

◆ _multiGenFcn

std::unique_ptr<ROOT::Math::IMultiGradFunction> RooFit::TestStatistics::MinuitFcnGrad::_multiGenFcn
private

Definition at line 97 of file MinuitFcnGrad.h.

◆ offsets_reset_

bool RooFit::TestStatistics::MinuitFcnGrad::offsets_reset_ = false
mutableprivate

Definition at line 94 of file MinuitFcnGrad.h.

  • roofit/roofitcore/src/TestStatistics/MinuitFcnGrad.h
  • roofit/roofitcore/src/TestStatistics/MinuitFcnGrad.cxx