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#include "RooNaNPacker.h"
19
20#include <iomanip> // std::setprecision
21
22namespace RooFit {
23namespace TestStatistics {
24
25namespace {
26
27class MinuitGradFunctor : public ROOT::Math::IMultiGradFunction {
28
29public:
30 MinuitGradFunctor(MinuitFcnGrad const &fcn) : _fcn{fcn} {}
31
32 ROOT::Math::IMultiGradFunction *Clone() const override { return new MinuitGradFunctor(_fcn); }
33
34 unsigned int NDim() const override { return _fcn.getNDim(); }
35
36 void Gradient(const double *x, double *grad) const override { return _fcn.Gradient(x, grad); }
37
38 void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2,
39 double *previous_gstep) const override
40 {
41 return _fcn.GradientWithPrevResult(x, grad, previous_grad, previous_g2, previous_gstep);
42 }
43
44 bool returnsInMinuit2ParameterSpace() const override { return _fcn.returnsInMinuit2ParameterSpace(); }
45
46private:
47 double DoEval(const double *x) const override { return _fcn(x); }
48
49 double DoDerivative(double const * /*x*/, unsigned int /*icoord*/) const override
50 {
51 throw std::runtime_error("MinuitGradFunctor::DoDerivative is not implemented, please use Gradient instead.");
52 }
53
54 MinuitFcnGrad const &_fcn;
55};
56
57} // namespace
58
59/** \class MinuitFcnGrad
60 *
61 * \brief Minuit-RooMinimizer interface which synchronizes parameter data and coordinates evaluation of likelihood
62 * (gradient) values
63 *
64 * This class provides an interface between RooFit and Minuit. It synchronizes parameter values from Minuit, calls
65 * calculator classes to evaluate likelihood and likelihood gradient values and returns them to Minuit. The Wrapper
66 * objects do the actual calculations. These are constructed inside the MinuitFcnGrad constructor using the RooAbsL
67 * likelihood passed in to the constructor, usually directly from RooMinimizer, with which this class is intimately
68 * coupled, being a RooAbsMinimizerFcn implementation. MinuitFcnGrad inherits from ROOT::Math::IMultiGradFunction as
69 * well, which allows it to be used as the FCN and GRAD parameters Minuit expects.
70 *
71 * \note The class is not intended for use by end-users. We recommend to either use RooMinimizer with a RooAbsL derived
72 * likelihood object, or to use a higher level entry point like RooAbsPdf::fitTo() or RooAbsPdf::createNLL().
73 */
74
75/// \param[in] absL The input likelihood.
76/// \param[in] context RooMinimizer that creates and owns this class.
77/// \param[in] parameters The vector of ParameterSettings objects that describe the parameters used in the Minuit
78/// \param[in] likelihoodMode Lmode
79/// \param[in] likelihoodGradientMode Lgrad
80/// \param[in] verbose true for verbose output
81/// Fitter. Note that these must match the set used in the Fitter used by \p context! It can be passed in from
82/// RooMinimizer with fitter()->Config().ParamsSettings().
83MinuitFcnGrad::MinuitFcnGrad(const std::shared_ptr<RooFit::TestStatistics::RooAbsL> &absL, RooMinimizer *context,
84 std::vector<ROOT::Fit::ParameterSettings> &parameters, LikelihoodMode likelihoodMode,
85 LikelihoodGradientMode likelihoodGradientMode)
86 : RooAbsMinimizerFcn(*absL->getParameters(), context),
87 _minuitInternalX(getNDim(), 0),
88 _minuitExternalX(getNDim(), 0),
89 _multiGenFcn{std::make_unique<MinuitGradFunctor>(*this)}
90{
91 synchronizeParameterSettings(parameters, true);
92
93 _calculationIsClean = std::make_unique<WrapperCalculationCleanFlags>();
94
95 SharedOffset shared_offset;
96
97 if (likelihoodMode == LikelihoodMode::multiprocess &&
98 likelihoodGradientMode == LikelihoodGradientMode::multiprocess) {
99 _likelihood = LikelihoodWrapper::create(likelihoodMode, absL, _calculationIsClean, shared_offset);
102 } else {
103 _likelihood = LikelihoodWrapper::create(likelihoodMode, absL, _calculationIsClean, shared_offset);
105 }
106
108 shared_offset);
109
110 applyToLikelihood([&](auto &l) { l.synchronizeParameterSettings(parameters); });
111 _gradient->synchronizeParameterSettings(getMultiGenFcn(), parameters);
112
113 // Note: can be different than RooGradMinimizerFcn/LikelihoodGradientSerial, where default options are passed
114 // (ROOT::Math::MinimizerOptions::DefaultStrategy() and ROOT::Math::MinimizerOptions::DefaultErrorDef())
115 applyToLikelihood([&](auto &l) { l.synchronizeWithMinimizer(ROOT::Math::MinimizerOptions()); });
116 _gradient->synchronizeWithMinimizer(ROOT::Math::MinimizerOptions());
117}
118
119/// Make sure the offsets are up to date
120///
121/// If the offsets need to be updated, this function triggers a likelihood evaluation.
122/// The likelihood will make sure the offset is set correctly in their shared_ptr
123/// offsets object, that is also shared with possible other LikelihoodWrapper members
124/// of MinuitFcnGrad and also the LikelihoodGradientWrapper member. Other necessary
125/// synchronization steps are also performed from the Wrapper child classes (e.g.
126/// sending the values to workers from MultiProcess::Jobs).
128{
129 if (_likelihood->isOffsetting() && (_evalCounter == 0 || offsets_reset_)) {
130 _likelihoodInGradient->evaluate();
131 offsets_reset_ = false;
132 }
133}
134
135double MinuitFcnGrad::operator()(const double *x) const
136{
138
139 syncOffsets();
140
141 // Calculate the function for these parameters
142 auto &likelihoodHere(_likelihoodInGradient && _gradient->isCalculating() ? *_likelihoodInGradient : *_likelihood);
143 likelihoodHere.evaluate();
144 double fvalue = likelihoodHere.getResult().Sum();
145 _calculationIsClean->likelihood = true;
146
147 fvalue = applyEvalErrorHandling(fvalue);
148
149 // Optional logging
150 if (cfg().verbose) {
151 std::cout << "\nprevFCN" << (likelihoodHere.isOffsetting() ? "-offset" : "") << " = " << std::setprecision(10)
152 << fvalue << std::setprecision(4) << " ";
153 std::cout.flush();
154 }
155
156 finishDoEval();
157 return fvalue;
158}
159
160/// Minuit calls (via FcnAdapters etc) DoEval or Gradient with a set of parameters x.
161/// This function syncs these values to the proper places in RooFit.
162///
163/// The first twist, and reason this function is more complicated than one may imagine, is that Minuit internally uses a
164/// transformed parameter space to account for parameter boundaries. Whether we receive these Minuit "internal"
165/// parameter values or "regular"/untransformed RooFit parameter space values depends on the situation.
166/// - The values that arrive here via DoEval are always "normal" parameter values, since Minuit transforms these
167/// back into regular space before passing to DoEval (see MnUserFcn::operator() which wraps the Fcn(Gradient)Base
168/// in ModularFunctionMinimizer::Minimize and is used for direct function calls from that point on in the minimizer).
169/// These can thus always be safely synced with this function's RooFit parameters using SetPdfParamVal.
170/// - The values that arrive here via Gradient will be in internal coordinates if that is
171/// what this class expects, and indeed this is the case for MinuitFcnGrad's current implementation. This is
172/// communicated to Minuit via MinuitFcnGrad::returnsInMinuit2ParameterSpace. Inside Minuit, that function determines
173/// whether this class's gradient calculator is wrapped inside a AnalyticalGradientCalculator, to which Minuit passes
174/// "external" parameter values, or as an ExternalInternalGradientCalculator, which gets "internal" parameter values.
175/// Long story short: when MinuitFcnGrad::returnsInMinuit2ParameterSpace() returns true, Minuit will pass "internal"
176/// values to Gradient. These cannot be synced with this function's RooFit parameters using
177/// SetPdfParamVal, unless a manual transformation step is performed in advance. However, they do need to be passed
178/// on to the gradient calculator, since indeed we expect values there to be in "internal" space. However, this is
179/// calculator dependent. Note that in the current MinuitFcnGrad implementation we do not actually allow for
180/// calculators in "external" (i.e. regular RooFit parameter space) values, since
181/// MinuitFcnGrad::returnsInMinuit2ParameterSpace is hardcoded to true. This should in a future version be changed so
182/// that the calculator (the wrapper) is queried for this information.
183/// Because some gradient calculators may also use the regular RooFit parameters (e.g. for calculating the likelihood's
184/// value itself), this information is also passed on to the gradient wrapper. Vice versa, when updated "internal"
185/// parameters are passed to Gradient, the likelihood may be affected as well. Even though a
186/// transformation from internal to "external" may be necessary before the values can be used, the likelihood can at
187/// least log that its parameter values are possibly no longer in sync with those of the gradient.
188///
189/// The second twist is that the Minuit external parameters may still be different from the ones used in RooFit. This
190/// happens when Minuit tries out values that lay outside the RooFit parameter's range(s). RooFit's setVal (called
191/// inside SetPdfParamVal) then clips the RooAbsArg's value to one of the range limits, instead of setting it to the
192/// value Minuit intended. When this happens, i.e. syncParameterValuesFromMinuitCalls is called with
193/// minuit_internal = false and the values do not match the previous values stored in _minuitInternalX *but* the
194/// values after SetPdfParamVal did not get set to the intended value, the _minuitInternalRooFitXMismatch flag is
195/// set. This information can be used by calculators, if desired, for instance when a calculator does not want to make
196/// use of the range information in the RooAbsArg parameters.
197bool MinuitFcnGrad::syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const
198{
199 bool aParamWasUpdated = false;
200 if (minuit_internal) {
202 throw std::logic_error("Updating Minuit-internal parameters only makes sense for (gradient) calculators that "
203 "are defined in Minuit-internal parameter space.");
204 }
205
206 for (std::size_t ix = 0; ix < getNDim(); ++ix) {
207 bool parameter_changed = (x[ix] != _minuitInternalX[ix]);
208 if (parameter_changed) {
209 _minuitInternalX[ix] = x[ix];
210 }
211 aParamWasUpdated |= parameter_changed;
212 }
213
214 if (aParamWasUpdated) {
215 _calculationIsClean->set_all(false);
216 applyToLikelihood([&](auto &l) { l.updateMinuitInternalParameterValues(_minuitInternalX); });
217 _gradient->updateMinuitInternalParameterValues(_minuitInternalX);
218 }
219 } else {
220 bool aParamIsMismatched = false;
221
222 for (std::size_t ix = 0; ix < getNDim(); ++ix) {
223 // Note: the return value of SetPdfParamVal does not always mean that the parameter's value in the RooAbsReal
224 // changed since last time! If the value was out of range bin, setVal was still called, but the value was not
225 // updated.
226 SetPdfParamVal(ix, x[ix]);
227 _minuitExternalX[ix] = x[ix];
228 // The above is why we need _minuitExternalX. The _minuitExternalX vector can also be passed to
229 // LikelihoodWrappers, if needed, but typically they will make use of the RooFit parameters directly. However,
230 // we log in the flag below whether they are different so that calculators can use this information.
231 bool parameter_changed = (x[ix] != _minuitExternalX[ix]);
232 aParamWasUpdated |= parameter_changed;
233 aParamIsMismatched |= (floatableParam(ix).getVal() != _minuitExternalX[ix]);
234 }
235
236 _minuitInternalRooFitXMismatch = aParamIsMismatched;
237
238 if (aParamWasUpdated) {
239 _calculationIsClean->set_all(false);
240 applyToLikelihood([&](auto &l) { l.updateMinuitExternalParameterValues(_minuitExternalX); });
241 _gradient->updateMinuitExternalParameterValues(_minuitExternalX);
242 }
243 }
244 return aParamWasUpdated;
245}
246
247void MinuitFcnGrad::Gradient(const double *x, double *grad) const
248{
251 syncOffsets();
252 _gradient->fillGradient(grad);
253 _calculatingGradient = false;
254}
255
256void MinuitFcnGrad::GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2,
257 double *previous_gstep) const
258{
261 syncOffsets();
262 _gradient->fillGradientWithPrevResult(grad, previous_grad, previous_g2, previous_gstep);
263 _calculatingGradient = false;
264}
265
266bool MinuitFcnGrad::Synchronize(std::vector<ROOT::Fit::ParameterSettings> &parameters)
267{
268 bool returnee = synchronizeParameterSettings(parameters, _optConst);
269 applyToLikelihood([&](auto &l) { l.synchronizeParameterSettings(parameters); });
270 _gradient->synchronizeParameterSettings(parameters);
271
272 applyToLikelihood([&](auto &l) { l.synchronizeWithMinimizer(_context->fitter()->Config().MinimizerOptions()); });
273 _gradient->synchronizeWithMinimizer(_context->fitter()->Config().MinimizerOptions());
274 return returnee;
275}
276
277} // namespace TestStatistics
278} // namespace RooFit
MinuitFcnGrad const & _fcn
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition IFunction.h:168
double applyEvalErrorHandling(double fvalue) const
Apply corrections on the fvalue if errors were signaled.
RooMinimizer::Config const & cfg() const
bool synchronizeParameterSettings(std::vector< ROOT::Fit::ParameterSettings > &parameters, bool optConst)
Informs Minuit through its parameter_settings vector of RooFit parameter properties.
RooRealVar & floatableParam(std::size_t i) const
bool SetPdfParamVal(int index, double value) const
Set value of parameter i.
unsigned int getNDim() const
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
static std::unique_ptr< LikelihoodGradientWrapper > create(LikelihoodGradientMode likelihoodGradientMode, std::shared_ptr< RooAbsL > likelihood, std::shared_ptr< WrapperCalculationCleanFlags > calculationIsClean, std::size_t nDim, RooMinimizer *minimizer, SharedOffset offset)
Factory method.
static std::unique_ptr< LikelihoodWrapper > create(LikelihoodMode likelihoodMode, std::shared_ptr< RooAbsL > likelihood, std::shared_ptr< WrapperCalculationCleanFlags > calculationIsClean, SharedOffset offset)
Factory method.
void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2, double *previous_gstep) const
double operator()(const double *x) const
bool Synchronize(std::vector< ROOT::Fit::ParameterSettings > &parameter_settings) override
Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.
void Gradient(const double *x, double *grad) const
IMultiGradFunction overrides necessary for Minuit.
std::vector< double > _minuitInternalX
std::shared_ptr< LikelihoodWrapper > _likelihood
ROOT::Math::IMultiGenFunction * getMultiGenFcn() override
bool syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const
Minuit calls (via FcnAdapters etc) DoEval or Gradient with a set of parameters x.
std::unique_ptr< LikelihoodGradientWrapper > _gradient
void syncOffsets() const
Make sure the offsets are up to date.
std::shared_ptr< WrapperCalculationCleanFlags > _calculationIsClean
std::vector< double > _minuitExternalX
std::shared_ptr< LikelihoodWrapper > _likelihoodInGradient
void applyToLikelihood(Func &&func) const
MinuitFcnGrad(const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &absL, RooMinimizer *context, std::vector< ROOT::Fit::ParameterSettings > &parameters, LikelihoodMode likelihoodMode, LikelihoodGradientMode likelihoodGradientMode)
Wrapper class around ROOT::Math::Minimizer that provides a seamless interface between the minimizer f...
auto fitter()
Return underlying ROOT fitter object.
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 CodegenImpl.h:64
TLine l
Definition textangle.C:4