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 "RooMsgService.h"
28#include "RooMinimizer.h"
29#include "RooNaNPacker.h"
30
31#include "TMatrixDSym.h"
32
33#include <fstream>
34#include <iomanip>
35
36using namespace std;
37
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 RooArgSet out;
46 funct.getParameters(nullptr, out);
47 return out;
48}
49
50} // namespace
51
52
54 bool verbose) :
55 RooAbsMinimizerFcn(getParameters(*funct), context, verbose), _funct(funct)
56{}
57
58
59
61 _funct(other._funct)
62{}
63
64
66{}
67
68
70{
71 return new RooMinimizerFcn(*this) ;
72}
73
75{
76 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
77}
78
79/// Evaluate function given the parameters in `x`.
80double RooMinimizerFcn::DoEval(const double *x) const {
81
82 // Set the parameter values for this iteration
83 for (unsigned index = 0; index < _nDim; index++) {
84 if (_logfile) (*_logfile) << x[index] << " " ;
86 }
87
88 // Calculate the function for these parameters
90 double fvalue = _funct->getVal();
92
93 if (!std::isfinite(fvalue) || RooAbsReal::numEvalErrors() > 0 || fvalue > 1e30) {
96 _numBadNLL++ ;
97
98 if (_doEvalErrorWall) {
99 const double badness = RooNaNPacker::unpackNaN(fvalue);
100 fvalue = (std::isfinite(_maxFCN) ? _maxFCN : 0.) + _recoverFromNaNStrength * badness;
101 }
102 } else {
103 if (_evalCounter > 0 && _evalCounter == _numBadNLL) {
104 // This is the first time we get a valid function value; while before, the
105 // function was always invalid. For invalid cases, we returned values > 0.
106 // Now, we offset valid values such that they are < 0.
107 _funcOffset = -fvalue;
108 }
109 fvalue += _funcOffset;
110 _maxFCN = std::max(fvalue, _maxFCN);
111 }
112
113 // Optional logging
114 if (_logfile)
115 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << endl;
116 if (_verbose) {
117 cout << "\nprevFCN" << (_funct->isOffsetting()?"-offset":"") << " = " << setprecision(10)
118 << fvalue << setprecision(4) << " " ;
119 cout.flush() ;
120 }
121
122 finishDoEval();
123
124 return fvalue;
125}
126
128{
129 return _funct->GetName();
130}
131
133{
134 return _funct->GetTitle();
135}
136
138{
140}
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 doAlsoTrackingOpt=true)
Interface function signaling a request to perform constant term optimization.
Definition: RooAbsArg.cxx:1853
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:541
void printEvalErrors() const
Print information about why evaluation failed.
std::ofstream * _logfile
bool SetPdfParamVal(int index, double value) const
Set value of parameter i.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:62
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
virtual bool isOffsetting() const
Definition: RooAbsReal.h:369
virtual void enableOffsetting(bool)
Definition: RooAbsReal.h:368
static void setHideOffset(bool flag)
Definition: RooAbsReal.cxx:116
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:56
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 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.
void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) override
This function must be overridden in the derived class to pass on constant term optimization configura...
double DoEval(const double *x) const override
Evaluate function given the parameters in x.
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:43
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