Logo ROOT  
Reference Guide
RooAbsMinimizerFcn.h
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#ifndef ROO_ABS_MINIMIZER_FCN
16#define ROO_ABS_MINIMIZER_FCN
17
18#include "Math/IFunction.h"
20#include "Fit/FitResult.h"
21
22#include "TMatrixDSym.h"
23
24#include "RooAbsReal.h"
25#include "RooArgList.h"
26#include "RooRealVar.h"
27
28#include <Fit/Fitter.h>
29
30#include <iostream>
31#include <fstream>
32#include <string>
33#include <memory> // unique_ptr
34
35// forward declaration
36class RooMinimizer;
37
39
40public:
41 RooAbsMinimizerFcn(RooArgList paramList, RooMinimizer *context, bool verbose = false);
43 virtual ~RooAbsMinimizerFcn() = default;
44
45 /// Informs Minuit through its parameter_settings vector of RooFit parameter properties.
46 bool synchronizeParameterSettings(std::vector<ROOT::Fit::ParameterSettings> &parameters, bool optConst, bool verbose);
47 /// Like synchronizeParameterSettings, Synchronize informs Minuit through its parameter_settings vector of RooFit parameter properties,
48 /// but Synchronize can be overridden to e.g. also include gradient strategy synchronization in subclasses.
49 virtual bool Synchronize(std::vector<ROOT::Fit::ParameterSettings> &parameters, bool optConst, bool verbose);
50
55 Int_t GetNumInvalidNLL() const { return _numBadNLL; }
56
57 // need access from Minimizer:
58 void SetEvalErrorWall(bool flag) { _doEvalErrorWall = flag; }
59 /// Try to recover from invalid function values. When invalid function values are encountered,
60 /// a penalty term is returned to the minimiser to make it back off. This sets the strength of this penalty.
61 /// \note A strength of zero is equivalent to a constant penalty (= the gradient vanishes, ROOT < 6.24).
62 /// Positive values lead to a gradient pointing away from the undefined regions. Use ~10 to force the minimiser
63 /// away from invalid function values.
64 void SetRecoverFromNaNStrength(double strength) { _recoverFromNaNStrength = strength; }
65 void SetPrintEvalErrors(Int_t numEvalErrors) { _printEvalErrors = numEvalErrors; }
66 double &GetMaxFCN() { return _maxFCN; }
67 Int_t evalCounter() const {return _evalCounter; }
69 /// Return a possible offset that's applied to the function to separate invalid function values from valid ones.
70 double getOffset() const { return _funcOffset; }
71 void SetVerbose(bool flag = true) { _verbose = flag; }
72
73 /// Put Minuit results back into RooFit objects.
74 void BackProp(const ROOT::Fit::FitResult &results);
75
76 /// RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
77 virtual std::string getFunctionName() const = 0;
78 /// RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class.
79 virtual std::string getFunctionTitle() const = 0;
80
81 /// Set different external covariance matrix
83
84 bool SetLogFile(const char *inLogfile);
85 std::ofstream *GetLogFile() { return _logfile; }
86
87 unsigned int getNDim() const { return _nDim; }
88
89 // In the past, the `getNDim` function was called just `NDim`. The funciton
90 // was renamed to match the code convention (lower case for funciton names),
91 // but we have to keep an overload with the old name to not break existing
92 // user code.
93 inline unsigned int NDim() const { return getNDim(); }
94
95 void setOptimizeConst(Int_t flag);
96
97 bool getOptConst() { return _optConst; }
98 std::vector<double> getParameterValues() const;
99
100 bool SetPdfParamVal(int index, double value) const;
101
102 /// Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
103 virtual void setOffsetting(bool flag) = 0;
104 virtual bool fit(ROOT::Fit::Fitter&) const = 0;
106
107protected:
108 void optimizeConstantTerms(bool constStatChange, bool constValChange);
109 /// This function must be overridden in the derived class to pass on constant term optimization configuration
110 /// to the function to be minimized. For a RooAbsArg, this would be RooAbsArg::constOptimizeTestStatistic.
111 virtual void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) = 0;
112
113 // used in BackProp (Minuit results -> RooFit) and ApplyCovarianceMatrix
114 void SetPdfParamErr(Int_t index, double value);
116 void SetPdfParamErr(Int_t index, double loVal, double hiVal);
117
118 void printEvalErrors() const;
119
120 void finishDoEval() const;
121
122 // members
124
125 // the following four are mutable because DoEval is const (in child classes)
126 // Reset the *largest* negative log-likelihood value we have seen so far:
127 mutable double _maxFCN = -std::numeric_limits<double>::infinity();
128 mutable double _funcOffset{0.};
130 mutable int _numBadNLL = 0;
131 mutable int _printEvalErrors = 10;
132 mutable int _evalCounter{0};
133
134 unsigned int _nDim = 0;
135
136 bool _optConst = false;
137
138 std::unique_ptr<RooArgList> _floatParamList;
139 std::unique_ptr<RooArgList> _constParamList;
140 std::unique_ptr<RooArgList> _initFloatParamList;
141 std::unique_ptr<RooArgList> _initConstParamList;
142
143 std::ofstream *_logfile = nullptr;
146};
147
148#endif
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
class containing the result of the fit and all the related information (fitted parameter values,...
Definition: FitResult.h:47
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
RooArgList * GetInitFloatParamList()
void SetVerbose(bool flag=true)
std::ofstream * GetLogFile()
void setOptimizeConst(Int_t flag)
RooMinimizer * _context
std::unique_ptr< RooArgList > _floatParamList
bool SetLogFile(const char *inLogfile)
Change the file name for logging of a RooMinimizer of all MINUIT steppings through the parameter spac...
void SetPrintEvalErrors(Int_t numEvalErrors)
virtual bool Synchronize(std::vector< ROOT::Fit::ParameterSettings > &parameters, bool optConst, bool verbose)
Like synchronizeParameterSettings, Synchronize informs Minuit through its parameter_settings vector o...
Int_t GetNumInvalidNLL() const
RooArgList * GetConstParamList()
virtual void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt)=0
This function must be overridden in the derived class to pass on constant term optimization configura...
virtual ~RooAbsMinimizerFcn()=default
void BackProp(const ROOT::Fit::FitResult &results)
Put Minuit results back into RooFit objects.
virtual std::string getFunctionName() const =0
RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
void optimizeConstantTerms(bool constStatChange, bool constValChange)
void printEvalErrors() const
Print information about why evaluation failed.
void ClearPdfParamAsymErr(Int_t index)
Modify PDF parameter error by ordinal index (needed by MINUIT)
virtual std::string getFunctionTitle() const =0
RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class...
std::vector< double > getParameterValues() const
std::ofstream * _logfile
virtual ROOT::Math::IMultiGenFunction * getMultiGenFcn()=0
void SetPdfParamErr(Int_t index, double value)
Modify PDF parameter error by ordinal index (needed by MINUIT)
void ApplyCovarianceMatrix(TMatrixDSym &V)
Set different external covariance matrix.
std::unique_ptr< RooArgList > _initConstParamList
double getOffset() const
Return a possible offset that's applied to the function to separate invalid function values from vali...
void SetEvalErrorWall(bool flag)
RooArgList * GetFloatParamList()
unsigned int NDim() const
std::unique_ptr< RooArgList > _constParamList
RooAbsMinimizerFcn(RooArgList paramList, RooMinimizer *context, bool verbose=false)
bool synchronizeParameterSettings(std::vector< ROOT::Fit::ParameterSettings > &parameters, bool optConst, bool verbose)
Informs Minuit through its parameter_settings vector of RooFit parameter properties.
virtual bool fit(ROOT::Fit::Fitter &) const =0
virtual void setOffsetting(bool flag)=0
Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
bool SetPdfParamVal(int index, double value) const
Set value of parameter i.
RooArgList * GetInitConstParamList()
unsigned int getNDim() const
std::unique_ptr< RooArgList > _initFloatParamList
void SetRecoverFromNaNStrength(double strength)
Try to recover from invalid function values.
Int_t evalCounter() const
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:22
RooMinimizer is a wrapper class around ROOT::Fit:Fitter that provides a seamless interface between th...
Definition: RooMinimizer.h:43