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 "TMatrixDSym.h"
32
33#include <fstream>
34#include <iomanip>
35
36using namespace std;
37
38namespace {
39
40// Helper function that wraps RooAbsArg::getParameters and directly returns the
41// output RooArgSet. To be used in the initializer list of the RooMinimizerFcn
42// constructor.
43RooArgSet getParameters(RooAbsReal const &funct)
44{
45 RooArgSet out;
46 funct.getParameters(nullptr, out);
47 return out;
48}
49
50} // namespace
51
53 : RooAbsMinimizerFcn(getParameters(*funct), context), _funct(funct)
54{
55}
56
58 : RooAbsMinimizerFcn(other), ROOT::Math::IBaseFunctionMultiDim(other), _funct(other._funct)
59{
60}
61
63
65{
66 return new RooMinimizerFcn(*this);
67}
68
70{
71 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
72}
73
74/// Evaluate function given the parameters in `x`.
75double RooMinimizerFcn::DoEval(const double *x) const
76{
77
78 // Set the parameter values for this iteration
79 for (unsigned index = 0; index < _nDim; index++) {
80 if (_logfile)
81 (*_logfile) << x[index] << " ";
83 }
84
85 // Calculate the function for these parameters
87 double fvalue = _funct->getVal();
89
90 if (!std::isfinite(fvalue) || RooAbsReal::numEvalErrors() > 0 || fvalue > 1e30) {
93 _numBadNLL++;
94
95 if (cfg().doEEWall) {
96 const double badness = RooNaNPacker::unpackNaN(fvalue);
97 fvalue = (std::isfinite(_maxFCN) ? _maxFCN : 0.) + cfg().recoverFromNaN * badness;
98 }
99 } else {
100 if (_evalCounter > 0 && _evalCounter == _numBadNLL) {
101 // This is the first time we get a valid function value; while before, the
102 // function was always invalid. For invalid cases, we returned values > 0.
103 // Now, we offset valid values such that they are < 0.
104 _funcOffset = -fvalue;
105 }
106 fvalue += _funcOffset;
107 _maxFCN = std::max(fvalue, _maxFCN);
108 }
109
110 // Optional logging
111 if (_logfile)
112 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << endl;
113 if (cfg().verbose) {
114 cout << "\nprevFCN" << (_funct->isOffsetting() ? "-offset" : "") << " = " << setprecision(10) << fvalue
115 << setprecision(4) << " ";
116 cout.flush();
117 }
118
119 finishDoEval();
120
121 return fvalue;
122}
123
125{
126 return _funct->GetName();
127}
128
130{
131 return _funct->GetTitle();
132}
133
135{
137}
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.
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:385
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
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.
RooAbsReal * _funct
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...
RooMinimizer is a wrapper class around ROOT::Fit:Fitter that provides a seamless interface between th...
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.
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...