Logo ROOT  
Reference Guide
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 "RooAbsRealLValue.h"
28#include "RooMsgService.h"
29#include "RooMinimizer.h"
30#include "RooNaNPacker.h"
31
32#include "TClass.h"
33#include "TMatrixDSym.h"
34
35#include <fstream>
36#include <iomanip>
37
38using namespace std;
39
40
41namespace {
42
43// Helper function that wraps RooAbsArg::getParameters and directly returns the
44// output RooArgSet. To be used in the initializer list of the RooMinimizerFcn
45// constructor.
46RooArgSet getParameters(RooAbsReal const& funct) {
47 RooArgSet out;
48 funct.getParameters(nullptr, out);
49 return out;
50}
51
52} // namespace
53
54
56 bool verbose) :
57 RooAbsMinimizerFcn(getParameters(*funct), context, verbose), _funct(funct)
58{}
59
60
61
63 _funct(other._funct)
64{}
65
66
68{}
69
70
72{
73 return new RooMinimizerFcn(*this) ;
74}
75
77{
78 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
79}
80
81/// Evaluate function given the parameters in `x`.
82double RooMinimizerFcn::DoEval(const double *x) const {
83
84 // Set the parameter values for this iteration
85 for (unsigned index = 0; index < _nDim; index++) {
86 if (_logfile) (*_logfile) << x[index] << " " ;
88 }
89
90 // Calculate the function for these parameters
92 double fvalue = _funct->getVal();
94
95 if (!std::isfinite(fvalue) || RooAbsReal::numEvalErrors() > 0 || fvalue > 1e30) {
98 _numBadNLL++ ;
99
100 if (_doEvalErrorWall) {
101 const double badness = RooNaNPacker::unpackNaN(fvalue);
102 fvalue = (std::isfinite(_maxFCN) ? _maxFCN : 0.) + _recoverFromNaNStrength * badness;
103 }
104 } else {
105 if (_evalCounter > 0 && _evalCounter == _numBadNLL) {
106 // This is the first time we get a valid function value; while before, the
107 // function was always invalid. For invalid cases, we returned values > 0.
108 // Now, we offset valid values such that they are < 0.
109 _funcOffset = -fvalue;
110 }
111 fvalue += _funcOffset;
112 _maxFCN = std::max(fvalue, _maxFCN);
113 }
114
115 // Optional logging
116 if (_logfile)
117 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << endl;
118 if (_verbose) {
119 cout << "\nprevFCN" << (_funct->isOffsetting()?"-offset":"") << " = " << setprecision(10)
120 << fvalue << setprecision(4) << " " ;
121 cout.flush() ;
122 }
123
124 _evalCounter++ ;
125
126 return fvalue;
127}
128
130{
131 return _funct->GetName();
132}
133
135{
136 return _funct->GetTitle();
137}
138
140{
142}
const Bool_t kFALSE
Definition: RtypesCore.h:101
const Bool_t kTRUE
Definition: RtypesCore.h:100
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
virtual void constOptimizeTestStatistic(ConstOpCode opcode, Bool_t doAlsoTrackingOpt=kTRUE)
Interface function signaling a request to perform constant term optimization.
Definition: RooAbsArg.cxx:1863
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...
Definition: RooAbsArg.cxx:569
Bool_t SetPdfParamVal(int index, double value) const
Set value of parameter i.
void printEvalErrors() const
Print information about why evaluation failed.
std::ofstream * _logfile
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:63
virtual Bool_t isOffsetting() const
Definition: RooAbsReal.h:371
static void setHideOffset(Bool_t flag)
Definition: RooAbsReal.cxx:121
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:93
virtual void enableOffsetting(Bool_t)
Definition: RooAbsReal.h:370
static Int_t numEvalErrors()
Return the number of logged evaluation errors since the last clearing.
static void clearEvalErrorLog()
Clear the stack of evaluation error messages.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:57
RooMinimizerFcn is an interface to the ROOT::Math::IBaseFunctionMultiDim, a function that ROOT's mini...
std::string getFunctionName() const override
RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
~RooMinimizerFcn() override
void setOffsetting(Bool_t flag) override
Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
ROOT::Math::IBaseFunctionMultiDim * Clone() const override
Clone a function.
double DoEval(const double *x) const override
Evaluate function given the parameters in x.
void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt) override
This function must be overridden in the derived class to pass on constant term optimization configura...
RooMinimizerFcn(RooAbsReal *funct, RooMinimizer *context, bool verbose=false)
RooAbsReal * _funct
std::string getFunctionTitle() const override
RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class...
RooMinimizer is a wrapper class around ROOT::Fit:Fitter that provides a seamless interface between th...
Definition: RooMinimizer.h:42
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
Namespace for new Math classes and functions.
IBaseFunctionMultiDimTempl< double > IBaseFunctionMultiDim
Definition: IFunctionfwd.h:31
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
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...
Definition: RooNaNPacker.h:122