Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooMinimizerFcn.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*****************************************************************************
4 * Project: RooFit *
5 * Package: RooFitCore *
6 * @(#)root/roofitcore:$Id$
7 * Authors: *
8 * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it *
9 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl *
10 * *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17//////////////////////////////////////////////////////////////////////////////
18/// \class RooMinimizerFcn
19/// RooMinimizerFcn is an interface to the ROOT::Math::IBaseFunctionMultiDim,
20/// a function that ROOT's minimisers use to carry out minimisations.
21///
22
23#include "RooMinimizerFcn.h"
24
25#include "RooAbsArg.h"
26#include "RooAbsPdf.h"
27#include "RooArgSet.h"
28#include "RooRealVar.h"
29#include "RooMsgService.h"
30#include "RooMinimizer.h"
31#include "RooNaNPacker.h"
32#include "RooCategory.h"
33
34#include "Math/Functor.h"
35#include "TMatrixDSym.h"
36
37#include <fstream>
38#include <iomanip>
39
40using std::setprecision;
41
42namespace {
43
44// Helper function that wraps RooAbsArg::getParameters and directly returns the
45// output RooArgSet. To be used in the initializer list of the RooMinimizerFcn
46// constructor. In the case of figuring out all parameters for the minimizer,
47// we don't want to strip disconnected parameters, becuase which parameters are
48// disconnected can change between minimization runs.
50{
51 RooArgSet out;
52 funct.getParameters(nullptr, out, /*stripDisconnected*/ false);
53 return out;
54}
55
56} // namespace
57
58// use reference wrapper for the Functor, such that the functor points to this RooMinimizerFcn by reference.
59RooMinimizerFcn::RooMinimizerFcn(RooAbsReal *funct, RooMinimizer *context)
61{
62 unsigned int nDim = getNDim();
63
64 if (context->_cfg.useGradient && funct->hasGradient()) {
65 _gradientOutput.resize(_allParams.size());
66 _multiGenFcn = std::make_unique<ROOT::Math::GradFunctor>(this, &RooMinimizerFcn::operator(),
67 &RooMinimizerFcn::evaluateGradient, nDim);
68 } else {
69 _multiGenFcn = std::make_unique<ROOT::Math::Functor>(std::cref(*this), getNDim());
70 }
71 if (context->_cfg.useHessian) {
72 _hessianOutput.resize(_allParams.size() * _allParams.size());
73 }
74}
75
76void RooMinimizerFcn::setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt)
77{
78 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
79}
80
81/// Evaluate function given the parameters in `x`.
82double RooMinimizerFcn::operator()(const double *x) const
83{
84 // Set the parameter values for this iteration
85 for (unsigned index = 0; index < getNDim(); index++) {
86 if (_logfile)
87 (*_logfile) << x[index] << " ";
89 }
90
91 // Calculate the function for these parameters
93 double fvalue = _funct->getVal();
95
97
98 // Optional logging
99 if (_logfile)
100 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << std::endl;
101 if (cfg().verbose) {
102 std::cout << "\nprevFCN" << (_funct->isOffsetting() ? "-offset" : "") << " = " << setprecision(10) << fvalue
103 << setprecision(4) << " ";
104 std::cout.flush();
105 }
106
107 finishDoEval();
108
109 return fvalue;
110}
111
112void RooMinimizerFcn::evaluateGradient(const double *x, double *out) const
113{
114 // Set the parameter values for this iteration
115 for (unsigned index = 0; index < getNDim(); index++) {
116 if (_logfile)
117 (*_logfile) << x[index] << " ";
119 }
120
121 _funct->gradient(_gradientOutput.data());
122
123 std::size_t iAll = 0;
124 std::size_t iFloating = 0;
125 for (RooAbsArg *param : _allParamsInit) {
126 if (!treatAsConstant(*param)) {
128 ++iFloating;
129 }
130 ++iAll;
131 }
132
133 // Optional logging
134 if (cfg().verbose) {
135 std::cout << "\n gradient = ";
136 for (std::size_t i = 0; i < getNDim(); ++i) {
137 std::cout << out[i] << ", ";
138 }
139 }
140}
141
142std::string RooMinimizerFcn::getFunctionName() const
143{
144 return _funct->GetName();
145}
146
147std::string RooMinimizerFcn::getFunctionTitle() const
148{
149 return _funct->GetTitle();
150}
151
152void RooMinimizerFcn::setOffsetting(bool flag)
153{
154 _funct->enableOffsetting(flag);
155}
156
157RooArgSet RooMinimizerFcn::freezeDisconnectedParameters() const
158{
159
162
163 _funct->getParameters(nullptr, paramsDisconnected, /*stripDisconnected*/ false);
164 _funct->getParameters(nullptr, paramsConnected, /*stripDisconnected*/ true);
165
166 paramsDisconnected.remove(paramsConnected, true, true);
167
169
171 auto *v = dynamic_cast<RooRealVar *>(a);
172 auto *cv = dynamic_cast<RooCategory *>(a);
173 if (v && !v->isConstant()) {
174 v->setConstant();
175 changedSet.add(*v);
176 } else if (cv && !cv->isConstant()) {
177 cv->setConstant();
178 changedSet.add(*cv);
179 }
180 }
181
182 return changedSet;
183}
184
185bool RooMinimizerFcn::evaluateHessian(std::span<const double> x, double *out) const
186{
187 // Set the parameter values for this iteration
188 for (unsigned index = 0; index < getNDim(); index++) {
189 if (_logfile)
190 (*_logfile) << x[index] << " ";
192 }
193
194 _funct->hessian(_hessianOutput.data());
195
196 std::size_t m = _allParamsInit.size();
197 std::size_t n = getNDim();
198 std::size_t iAll = 0;
199 std::size_t iFloating = 0;
201 if (!treatAsConstant(*param_i)) {
202 std::size_t jAll = 0;
203 std::size_t jFloating = 0;
205 if (!treatAsConstant(*param_j)) {
207 ++jFloating;
208 }
209 ++jAll;
210 }
211 ++iFloating;
212 }
213 ++iAll;
214 }
215
216 // Optional logging
217 if (cfg().verbose) {
218 std::cout << "\n hessian = " << std::endl;
219 for (std::size_t i = 0; i < getNDim(); ++i) {
220 for (std::size_t j = 0; j < getNDim(); ++j) {
221 std::cout << out[i * n + j] << ", ";
222 }
223 std::cout << std::endl;
224 }
225 }
226 return true;
227}
228
229void RooMinimizerFcn::initMinimizer(ROOT::Math::Minimizer &minim, RooMinimizer *context)
230{
231 minim.SetFunction(*_multiGenFcn);
232 if (context->_cfg.useHessian && _funct->hasHessian()) {
233 minim.SetHessianFunction(
234 std::bind(&RooMinimizerFcn::evaluateHessian, this, std::placeholders::_1, std::placeholders::_2));
235 }
236}
237
238/// \endcond
#define a(i)
Definition RSha256.hxx:99
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition Minimizer.h:124
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
void setConstant(bool value=true)
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
static void setHideOffset(bool flag)
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Object to represent discrete states.
Definition RooCategory.h:28
Wrapper class around ROOT::Math::Minimizer that provides a seamless interface between the minimizer f...
RooMinimizer::Config _cfg
Variable that can be changed from the outside.
Definition RooRealVar.h:37
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TMarker m
Definition textangle.C:8