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