Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
MinuitFcnGrad.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#include "MinuitFcnGrad.h"
14
15#include "RooMinimizer.h"
16#include "RooMsgService.h"
17#include "RooAbsPdf.h"
18
19#include <iomanip> // std::setprecision
20
21namespace RooFit {
22namespace TestStatistics {
23
24/** \class MinuitFcnGrad
25 *
26 * \brief Minuit-RooMinimizer interface which synchronizes parameter data and coordinates evaluation of likelihood
27 * (gradient) values
28 *
29 * This class provides an interface between RooFit and Minuit. It synchronizes parameter values from Minuit, calls
30 * calculator classes to evaluate likelihood and likelihood gradient values and returns them to Minuit. The Wrapper
31 * objects do the actual calculations. These are constructed inside the MinuitFcnGrad constructor using the RooAbsL
32 * likelihood passed in to the constructor, usually directly from RooMinimizer, with which this class is intimately
33 * coupled, being a RooAbsMinimizerFcn implementation. MinuitFcnGrad inherits from ROOT::Math::IMultiGradFunction as
34 * well, which allows it to be used as the FCN and GRAD parameters Minuit expects.
35 *
36 * \note The class is not intended for use by end-users. We recommend to either use RooMinimizer with a RooAbsL derived
37 * likelihood object, or to use a higher level entry point like RooAbsPdf::fitTo() or RooAbsPdf::createNLL().
38 */
39
40/// \param[in] context RooMinimizer that creates and owns this class.
41/// \param[in] parameters The vector of ParameterSettings objects that describe the parameters used in the Minuit
42/// Fitter. Note that these must match the set used in the Fitter used by \p context! It can be passed in from
43/// RooMinimizer with fitter()->Config().ParamsSettings().
44MinuitFcnGrad::MinuitFcnGrad(const std::shared_ptr<RooFit::TestStatistics::RooAbsL> &_likelihood, RooMinimizer *context,
45 std::vector<ROOT::Fit::ParameterSettings> &parameters, LikelihoodMode likelihoodMode,
46 LikelihoodGradientMode likelihoodGradientMode, bool verbose)
47 : RooAbsMinimizerFcn(RooArgList(*_likelihood->getParameters()), context, verbose), minuit_internal_x_(NDim(), 0),
48 minuit_external_x_(NDim(), 0)
49{
50 synchronizeParameterSettings(parameters, kTRUE, verbose);
51
52 calculation_is_clean = std::make_shared<WrapperCalculationCleanFlags>();
53
54 likelihood = LikelihoodWrapper::create(likelihoodMode, _likelihood, calculation_is_clean);
55 gradient =
56 LikelihoodGradientWrapper::create(likelihoodGradientMode, _likelihood, calculation_is_clean, getNDim(), _context);
57
58 likelihood->synchronizeParameterSettings(parameters);
59 gradient->synchronizeParameterSettings(this, parameters);
60
61 // Note: can be different than RooGradMinimizerFcn, where default options are passed
62 // (ROOT::Math::MinimizerOptions::DefaultStrategy() and ROOT::Math::MinimizerOptions::DefaultErrorDef())
63 likelihood->synchronizeWithMinimizer(ROOT::Math::MinimizerOptions());
64 gradient->synchronizeWithMinimizer(ROOT::Math::MinimizerOptions());
65}
66
67double MinuitFcnGrad::DoEval(const double *x) const
68{
69 Bool_t parameters_changed = syncParameterValuesFromMinuitCalls(x, false);
70
71 // Calculate the function for these parameters
72 // RooAbsReal::setHideOffset(kFALSE);
73 likelihood->evaluate();
74 double fvalue = likelihood->getResult().Sum();
75 calculation_is_clean->likelihood = true;
76 // RooAbsReal::setHideOffset(kTRUE);
77
78 if (!parameters_changed) {
79 return fvalue;
80 }
81
82 if (!std::isfinite(fvalue) || RooAbsReal::numEvalErrors() > 0 || fvalue > 1e30) {
83
84 if (_printEvalErrors >= 0) {
85
86 if (_doEvalErrorWall) {
87 oocoutW(static_cast<RooAbsArg *>(nullptr), Eval)
88 << "RooGradMinimizerFcn: Minimized function has error status." << std::endl
89 << "Returning maximum FCN so far (" << _maxFCN
90 << ") to force MIGRAD to back out of this region. Error log follows" << std::endl;
91 } else {
92 oocoutW(static_cast<RooAbsArg *>(nullptr), Eval)
93 << "RooGradMinimizerFcn: Minimized function has error status but is ignored" << std::endl;
94 }
95
97 ooccoutW(static_cast<RooAbsArg *>(nullptr), Eval) << "Parameter values: ";
98 for (const auto rooAbsArg : *_floatParamList) {
99 auto var = static_cast<const RooRealVar *>(rooAbsArg);
100 if (first) {
101 first = kFALSE;
102 } else {
103 ooccoutW(static_cast<RooAbsArg *>(nullptr), Eval) << ", ";
104 }
105 ooccoutW(static_cast<RooAbsArg *>(nullptr), Eval) << var->GetName() << "=" << var->getVal();
106 }
107 ooccoutW(static_cast<RooAbsArg *>(nullptr), Eval) << std::endl;
108
110 ooccoutW(static_cast<RooAbsArg *>(nullptr), Eval) << std::endl;
111 }
112
113 if (_doEvalErrorWall) {
114 fvalue = _maxFCN + 1;
115 }
116
118 _numBadNLL++;
119 } else if (fvalue > _maxFCN) {
120 _maxFCN = fvalue;
121 }
122
123 // Optional logging
124 if (_verbose) {
125 std::cout << "\nprevFCN" << (likelihood->isOffsetting() ? "-offset" : "") << " = " << std::setprecision(10)
126 << fvalue << std::setprecision(4) << " ";
127 std::cout.flush();
128 }
129
130 _evalCounter++;
131 return fvalue;
132}
133
134/// Minuit calls (via FcnAdapters etc) DoEval or Gradient with a set of parameters x.
135/// This function syncs these values to the proper places in RooFit.
136///
137/// The first twist, and reason this function is more complicated than one may imagine, is that Minuit internally uses a
138/// transformed parameter space to account for parameter boundaries. Whether we receive these Minuit "internal"
139/// parameter values or "regular"/untransformed RooFit parameter space values depends on the situation.
140/// - The values that arrive here via DoEval are always "normal" parameter values, since Minuit transforms these
141/// back into regular space before passing to DoEval (see MnUserFcn::operator() which wraps the Fcn(Gradient)Base
142/// in ModularFunctionMinimizer::Minimize and is used for direct function calls from that point on in the minimizer).
143/// These can thus always be safely synced with this function's RooFit parameters using SetPdfParamVal.
144/// - The values that arrive here via Gradient will be in internal coordinates if that is
145/// what this class expects, and indeed this is the case for MinuitFcnGrad's current implementation. This is
146/// communicated to Minuit via MinuitFcnGrad::returnsInMinuit2ParameterSpace. Inside Minuit, that function determines
147/// whether this class's gradient calculator is wrapped inside a AnalyticalGradientCalculator, to which Minuit passes
148/// "external" parameter values, or as an ExternalInternalGradientCalculator, which gets "internal" parameter values.
149/// Long story short: when MinuitFcnGrad::returnsInMinuit2ParameterSpace() returns true, Minuit will pass "internal"
150/// values to Gradient. These cannot be synced with this function's RooFit parameters using
151/// SetPdfParamVal, unless a manual transformation step is performed in advance. However, they do need to be passed
152/// on to the gradient calculator, since indeed we expect values there to be in "internal" space. However, this is
153/// calculator dependent. Note that in the current MinuitFcnGrad implementation we do not actually allow for
154/// calculators in "external" (i.e. regular RooFit parameter space) values, since
155/// MinuitFcnGrad::returnsInMinuit2ParameterSpace is hardcoded to true. This should in a future version be changed so
156/// that the calculator (the wrapper) is queried for this information.
157/// Because some gradient calculators may also use the regular RooFit parameters (e.g. for calculating the likelihood's
158/// value itself), this information is also passed on to the gradient wrapper. Vice versa, when updated "internal"
159/// parameters are passed to Gradient, the likelihood may be affected as well. Even though a
160/// transformation from internal to "external" may be necessary before the values can be used, the likelihood can at
161/// least log that its parameter values are possibly no longer in sync with those of the gradient.
162///
163/// The second twist is that the Minuit external parameters may still be different from the ones used in RooFit. This
164/// happens when Minuit tries out values that lay outside the RooFit parameter's range(s). RooFit's setVal (called
165/// inside SetPdfParamVal) then clips the RooAbsArg's value to one of the range limits, instead of setting it to the
166/// value Minuit intended. When this happens, i.e. syncParameterValuesFromMinuitCalls is called with
167/// minuit_internal = false and the values do not match the previous values stored in minuit_internal_x_ *but* the
168/// values after SetPdfParamVal did not get set to the intended value, the minuit_internal_roofit_x_mismatch_ flag is
169/// set. This information can be used by calculators, if desired, for instance when a calculator does not want to make
170/// use of the range information in the RooAbsArg parameters.
171bool MinuitFcnGrad::syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const
172{
173 bool a_parameter_has_been_updated = false;
174 if (minuit_internal) {
176 throw std::logic_error("Updating Minuit-internal parameters only makes sense for (gradient) calculators that "
177 "are defined in Minuit-internal parameter space.");
178 }
179
180 for (std::size_t ix = 0; ix < NDim(); ++ix) {
181 bool parameter_changed = (x[ix] != minuit_internal_x_[ix]);
182 if (parameter_changed) {
183 minuit_internal_x_[ix] = x[ix];
184 }
185 a_parameter_has_been_updated |= parameter_changed;
186 }
187
188 if (a_parameter_has_been_updated) {
189 calculation_is_clean->set_all(false);
190 likelihood->updateMinuitInternalParameterValues(minuit_internal_x_);
191 gradient->updateMinuitInternalParameterValues(minuit_internal_x_);
192 }
193 } else {
194 bool a_parameter_is_mismatched = false;
195
196 for (std::size_t ix = 0; ix < NDim(); ++ix) {
197 // Note: the return value of SetPdfParamVal does not always mean that the parameter's value in the RooAbsReal
198 // changed since last time! If the value was out of range bin, setVal was still called, but the value was not
199 // updated.
200 SetPdfParamVal(ix, x[ix]);
201 minuit_external_x_[ix] = x[ix];
202 // The above is why we need minuit_external_x_. The minuit_external_x_ vector can also be passed to
203 // LikelihoodWrappers, if needed, but typically they will make use of the RooFit parameters directly. However,
204 // we log in the flag below whether they are different so that calculators can use this information.
205 bool parameter_changed = (x[ix] != minuit_external_x_[ix]);
206 a_parameter_has_been_updated |= parameter_changed;
207 a_parameter_is_mismatched |= (((RooRealVar *)_floatParamList->at(ix))->getVal() != minuit_external_x_[ix]);
208 }
209
210 minuit_internal_roofit_x_mismatch_ = a_parameter_is_mismatched;
211
212 if (a_parameter_has_been_updated) {
213 calculation_is_clean->set_all(false);
214 likelihood->updateMinuitExternalParameterValues(minuit_external_x_);
215 gradient->updateMinuitExternalParameterValues(minuit_external_x_);
216 }
217 }
218 return a_parameter_has_been_updated;
219}
220
221void MinuitFcnGrad::Gradient(const double *x, double *grad) const
222{
224 gradient->fillGradient(grad);
225}
226
227void MinuitFcnGrad::GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2,
228 double *previous_gstep) const
229{
231 gradient->fillGradientWithPrevResult(grad, previous_grad, previous_g2, previous_gstep);
232}
233
234double MinuitFcnGrad::DoDerivative(const double * /*x*/, unsigned int /*icoord*/) const
235{
236 throw std::runtime_error("MinuitFcnGrad::DoDerivative is not implemented, please use Gradient instead.");
237}
238
239Bool_t
240MinuitFcnGrad::Synchronize(std::vector<ROOT::Fit::ParameterSettings> &parameters, Bool_t optConst, Bool_t verbose)
241{
242 Bool_t returnee = synchronizeParameterSettings(parameters, optConst, verbose);
243 likelihood->synchronizeParameterSettings(parameters);
244 gradient->synchronizeParameterSettings(parameters);
245
246 likelihood->synchronizeWithMinimizer(_context->fitter()->Config().MinimizerOptions());
247 gradient->synchronizeWithMinimizer(_context->fitter()->Config().MinimizerOptions());
248 return returnee;
249}
250
251} // namespace TestStatistics
252} // namespace RooFit
#define oocoutW(o, a)
#define ooccoutW(o, a)
const Bool_t kFALSE
Definition RtypesCore.h:101
bool Bool_t
Definition RtypesCore.h:63
const Bool_t kTRUE
Definition RtypesCore.h:100
ROOT::Math::MinimizerOptions & MinimizerOptions()
access to the minimizer control parameter (non const method)
Definition FitConfig.h:167
const FitConfig & Config() const
access to the fit configuration (const method)
Definition Fitter.h:412
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:69
Bool_t SetPdfParamVal(int index, double value) const
Set value of parameter i.
std::unique_ptr< RooArgList > _floatParamList
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.
unsigned int getNDim() const
static Int_t numEvalErrors()
Return the number of logged evaluation errors since the last clearing.
static void printEvalErrors(std::ostream &os=std::cout, Int_t maxPerNode=10000000)
Print all outstanding logged evaluation error on the given ostream.
static void clearEvalErrorLog()
Clear the stack of evaluation error messages.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
static std::unique_ptr< LikelihoodGradientWrapper > create(LikelihoodGradientMode likelihoodGradientMode, std::shared_ptr< RooAbsL > likelihood, std::shared_ptr< WrapperCalculationCleanFlags > calculationIsClean, std::size_t nDim, RooMinimizer *minimizer)
Factory method.
static std::unique_ptr< LikelihoodWrapper > create(LikelihoodMode likelihoodMode, std::shared_ptr< RooAbsL > likelihood, std::shared_ptr< WrapperCalculationCleanFlags > calculationIsClean)
Factory method.
std::vector< double > minuit_internal_x_
double DoDerivative(const double *x, unsigned int icoord) const override
This override should not be used in this class, so it throws.
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.
double DoEval(const double *x) const override
IMultiGradFunction override necessary for Minuit.
bool syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const
Minuit calls (via FcnAdapters etc) DoEval or Gradient with a set of parameters x.
std::shared_ptr< WrapperCalculationCleanFlags > calculation_is_clean
bool returnsInMinuit2ParameterSpace() const override
void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2, double *previous_gstep) const override
void Gradient(const double *x, double *grad) const override
IMultiGradFunction overrides necessary for Minuit.
std::shared_ptr< LikelihoodWrapper > likelihood
std::vector< double > minuit_external_x_
std::shared_ptr< LikelihoodGradientWrapper > gradient
unsigned int NDim() const override
Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit.
MinuitFcnGrad(const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &_likelihood, RooMinimizer *context, std::vector< ROOT::Fit::ParameterSettings > &parameters, LikelihoodMode likelihoodMode, LikelihoodGradientMode likelihoodGradientMode, bool verbose=false)
RooMinimizer is a wrapper class around ROOT::Fit:Fitter that provides a seamless interface between th...
ROOT::Fit::Fitter * fitter()
Return underlying ROOT fitter object.
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition Common.h:18
Definition first.py:1