Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooMinimizerFcn.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it *
7 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl *
8 * *
9 * *
10 * Redistribution and use in source and binary forms, *
11 * with or without modification, are permitted according to the terms *
12 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
13 *****************************************************************************/
14
15//////////////////////////////////////////////////////////////////////////////
16/// \class RooMinimizerFcn
17/// RooMinimizerFcn is an interface to the ROOT::Math::IBaseFunctionMultiDim,
18/// a function that ROOT's minimisers use to carry out minimisations.
19///
20
21#include "RooMinimizerFcn.h"
22
23#include "RooAbsArg.h"
24#include "RooAbsPdf.h"
25#include "RooArgSet.h"
26#include "RooRealVar.h"
27#include "RooMsgService.h"
28#include "RooMinimizer.h"
29#include "RooNaNPacker.h"
30
31#include "Math/Functor.h"
32#include "TMatrixDSym.h"
33
34#include <fstream>
35#include <iomanip>
36
37using std::cout, std::endl, std::setprecision;
38
39namespace {
40
41// Helper function that wraps RooAbsArg::getParameters and directly returns the
42// output RooArgSet. To be used in the initializer list of the RooMinimizerFcn
43// constructor.
44RooArgSet getParameters(RooAbsReal const &funct)
45{
47 funct.getParameters(nullptr, out);
48 return out;
49}
50
51} // namespace
52
53// use reference wrapper for the Functor, such that the functor points to this RooMinimizerFcn by reference.
55 : RooAbsMinimizerFcn(getParameters(*funct), context), _funct(funct)
56{
57 if (context->_cfg.useGradient && funct->hasGradient()) {
58 _multiGenFcn = std::make_unique<ROOT::Math::GradFunctor>(this, &RooMinimizerFcn::operator(),
60 } else {
61 _multiGenFcn = std::make_unique<ROOT::Math::Functor>(std::cref(*this), getNDim());
62 }
63}
64
66{
67 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
68}
69
70/// Evaluate function given the parameters in `x`.
71double RooMinimizerFcn::operator()(const double *x) const
72{
73 // Set the parameter values for this iteration
74 for (unsigned index = 0; index < _nDim; index++) {
75 if (_logfile)
76 (*_logfile) << x[index] << " ";
78 }
79
80 // Calculate the function for these parameters
82 double fvalue = _funct->getVal();
84
85 if (!std::isfinite(fvalue) || RooAbsReal::numEvalErrors() > 0 || fvalue > 1e30) {
88 _numBadNLL++;
89
90 if (cfg().doEEWall) {
91 const double badness = RooNaNPacker::unpackNaN(fvalue);
92 fvalue = (std::isfinite(_maxFCN) ? _maxFCN : 0.) + cfg().recoverFromNaN * badness;
93 }
94 } else {
95 if (_evalCounter > 0 && _evalCounter == _numBadNLL) {
96 // This is the first time we get a valid function value; while before, the
97 // function was always invalid. For invalid cases, we returned values > 0.
98 // Now, we offset valid values such that they are < 0.
99 _funcOffset = -fvalue;
100 }
101 fvalue += _funcOffset;
102 _maxFCN = std::max(fvalue, _maxFCN);
103 }
104
105 // Optional logging
106 if (_logfile)
107 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << endl;
108 if (cfg().verbose) {
109 cout << "\nprevFCN" << (_funct->isOffsetting() ? "-offset" : "") << " = " << setprecision(10) << fvalue
110 << setprecision(4) << " ";
111 cout.flush();
112 }
113
114 finishDoEval();
115
116 return fvalue;
117}
118
119void RooMinimizerFcn::evaluateGradient(const double *x, double *out) const
120{
121 // Set the parameter values for this iteration
122 for (unsigned index = 0; index < _nDim; index++) {
123 if (_logfile)
124 (*_logfile) << x[index] << " ";
126 }
127
128 _funct->gradient(out);
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
140{
141 return _funct->GetName();
142}
143
145{
146 return _funct->GetTitle();
147}
148
150{
152}
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
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
virtual void constOptimizeTestStatistic(ConstOpCode opcode, bool doAlsoTrackingOpt=true)
Interface function signaling a request to perform constant term optimization.
RooMinimizer::Config const & cfg() const
void printEvalErrors() const
Print information about why evaluation failed.
std::ofstream * _logfile
bool SetPdfParamVal(int index, double value) const
Set value of parameter i.
unsigned int getNDim() const
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
virtual bool isOffsetting() const
Definition RooAbsReal.h:377
virtual bool hasGradient() const
Definition RooAbsReal.h:394
virtual void gradient(double *) const
Definition RooAbsReal.h:395
static void setHideOffset(bool flag)
static Int_t numEvalErrors()
Return the number of logged evaluation errors since the last clearing.
virtual void enableOffsetting(bool)
static void clearEvalErrorLog()
Clear the stack of evaluation error messages.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
void evaluateGradient(const double *x, double *out) const
std::string getFunctionName() const override
RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
void setOffsetting(bool flag) override
Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) override
This function must be overridden in the derived class to pass on constant term optimization configura...
std::unique_ptr< ROOT::Math::IBaseFunctionMultiDim > _multiGenFcn
RooAbsReal * _funct
double operator()(const double *x) const
Evaluate function given the parameters in x.
RooMinimizerFcn(RooAbsReal *funct, RooMinimizer *context)
std::string getFunctionTitle() const override
RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class...
Wrapper class around ROOT::Fit:Fitter that provides a seamless interface between the minimizer functi...
RooMinimizer::Config _cfg
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
Double_t x[n]
Definition legend1.C:17
static float unpackNaN(double val)
If val is NaN and a this NaN has been tagged as containing a payload, unpack the float from the manti...