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}
72
73void RooMinimizerFcn::setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt)
74{
75 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
76}
77
78/// Evaluate function given the parameters in `x`.
79double RooMinimizerFcn::operator()(const double *x) const
80{
81 // Set the parameter values for this iteration
82 for (unsigned index = 0; index < getNDim(); index++) {
83 if (_logfile)
84 (*_logfile) << x[index] << " ";
86 }
87
88 // Calculate the function for these parameters
90 double fvalue = _funct->getVal();
92
94
95 // Optional logging
96 if (_logfile)
97 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << std::endl;
98 if (cfg().verbose) {
99 std::cout << "\nprevFCN" << (_funct->isOffsetting() ? "-offset" : "") << " = " << setprecision(10) << fvalue
100 << setprecision(4) << " ";
101 std::cout.flush();
102 }
103
104 finishDoEval();
105
106 return fvalue;
107}
108
109void RooMinimizerFcn::evaluateGradient(const double *x, double *out) const
110{
111 // Set the parameter values for this iteration
112 for (unsigned index = 0; index < getNDim(); index++) {
113 if (_logfile)
114 (*_logfile) << x[index] << " ";
116 }
117
118 _funct->gradient(_gradientOutput.data());
119
120 std::size_t iAll = 0;
121 std::size_t iFloating = 0;
122 for (RooAbsArg *param : _allParamsInit) {
123 if (!treatAsConstant(*param)) {
125 ++iFloating;
126 }
127 ++iAll;
128 }
129
130 // Optional logging
131 if (cfg().verbose) {
132 std::cout << "\n gradient = ";
133 for (std::size_t i = 0; i < getNDim(); ++i) {
134 std::cout << out[i] << ", ";
135 }
136 }
137}
138
139std::string RooMinimizerFcn::getFunctionName() const
140{
141 return _funct->GetName();
142}
143
144std::string RooMinimizerFcn::getFunctionTitle() const
145{
146 return _funct->GetTitle();
147}
148
149void RooMinimizerFcn::setOffsetting(bool flag)
150{
151 _funct->enableOffsetting(flag);
152}
153
154RooArgSet RooMinimizerFcn::freezeDisconnectedParameters() const
155{
156
159
160 _funct->getParameters(nullptr, paramsDisconnected, /*stripDisconnected*/ false);
161 _funct->getParameters(nullptr, paramsConnected, /*stripDisconnected*/ true);
162
163 paramsDisconnected.remove(paramsConnected, true, true);
164
166
168 auto *v = dynamic_cast<RooRealVar *>(a);
169 auto *cv = dynamic_cast<RooCategory *>(a);
170 if (v && !v->isConstant()) {
171 v->setConstant();
172 changedSet.add(*v);
173 } else if (cv && !cv->isConstant()) {
174 cv->setConstant();
175 changedSet.add(*cv);
176 }
177 }
178
179 return changedSet;
180}
181
182/// \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
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