Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <iostream>
29#include <fstream>
30#include <string>
31#include <memory> // unique_ptr
32
33// forward declaration
34class RooMinimizer;
35
37
38public:
39 RooAbsMinimizerFcn(RooArgList paramList, RooMinimizer *context, bool verbose = false);
41 virtual ~RooAbsMinimizerFcn() = default;
42
43 /// Informs Minuit through its parameter_settings vector of RooFit parameter properties.
44 Bool_t synchronizeParameterSettings(std::vector<ROOT::Fit::ParameterSettings> &parameters, Bool_t optConst, Bool_t verbose);
45 /// Like synchronizeParameterSettings, Synchronize informs Minuit through its parameter_settings vector of RooFit parameter properties,
46 /// but Synchronize can be overridden to e.g. also include gradient strategy synchronization in subclasses.
47 virtual Bool_t Synchronize(std::vector<ROOT::Fit::ParameterSettings> &parameters, Bool_t optConst, Bool_t verbose);
48
53 Int_t GetNumInvalidNLL() const;
54
55 // need access from Minimizer:
56 void SetEvalErrorWall(Bool_t flag);
57 /// Try to recover from invalid function values. When invalid function values are encountered,
58 /// a penalty term is returned to the minimiser to make it back off. This sets the strength of this penalty.
59 /// \note A strength of zero is equivalent to a constant penalty (= the gradient vanishes, ROOT < 6.24).
60 /// Positive values lead to a gradient pointing away from the undefined regions. Use ~10 to force the minimiser
61 /// away from invalid function values.
62 void SetRecoverFromNaNStrength(double strength) { _recoverFromNaNStrength = strength; }
63 void SetPrintEvalErrors(Int_t numEvalErrors);
65 Int_t evalCounter() const;
66 void zeroEvalCount();
67 /// Return a possible offset that's applied to the function to separate invalid function values from valid ones.
68 double getOffset() const { return _funcOffset; }
69 void SetVerbose(Bool_t flag = kTRUE);
70
71 /// Put Minuit results back into RooFit objects.
72 void BackProp(const ROOT::Fit::FitResult &results);
73
74 /// RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
75 virtual std::string getFunctionName() const = 0;
76 /// RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class.
77 virtual std::string getFunctionTitle() const = 0;
78
79 /// Set different external covariance matrix
81
82 Bool_t SetLogFile(const char *inLogfile);
83 std::ofstream *GetLogFile() { return _logfile; }
84
85 unsigned int getNDim() const { return _nDim; }
86
87 // In the past, the `getNDim` function was called just `NDim`. The funciton
88 // was renamed to match the code convention (lower case for funciton names),
89 // but we have to keep an overload with the old name to not break existing
90 // user code.
91 inline unsigned int NDim() const { return getNDim(); }
92
93 void setOptimizeConst(Int_t flag);
94
95 bool getOptConst();
96 std::vector<double> getParameterValues() const;
97
98 Bool_t SetPdfParamVal(int index, double value) const;
99
100 /// Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
101 virtual void setOffsetting(Bool_t flag) = 0;
102
103protected:
104 void optimizeConstantTerms(bool constStatChange, bool constValChange);
105 /// This function must be overridden in the derived class to pass on constant term optimization configuration
106 /// to the function to be minimized. For a RooAbsArg, this would be RooAbsArg::constOptimizeTestStatistic.
107 virtual void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt) = 0;
108
109 // used in BackProp (Minuit results -> RooFit) and ApplyCovarianceMatrix
110 void SetPdfParamErr(Int_t index, Double_t value);
111 void ClearPdfParamAsymErr(Int_t index);
112 void SetPdfParamErr(Int_t index, Double_t loVal, Double_t hiVal);
113
114 void printEvalErrors() const;
115
116 // members
118
119 // the following four are mutable because DoEval is const (in child classes)
120 // Reset the *largest* negative log-likelihood value we have seen so far:
121 mutable double _maxFCN = -std::numeric_limits<double>::infinity();
122 mutable double _funcOffset{0.};
124 mutable int _numBadNLL = 0;
125 mutable int _printEvalErrors = 10;
126 mutable int _evalCounter{0};
127
128 unsigned int _nDim = 0;
129
131
132 std::unique_ptr<RooArgList> _floatParamList;
133 std::unique_ptr<RooArgList> _constParamList;
134 std::unique_ptr<RooArgList> _initFloatParamList;
135 std::unique_ptr<RooArgList> _initConstParamList;
136
137 std::ofstream *_logfile = nullptr;
140};
141
142#endif
const Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
const Bool_t kTRUE
Definition RtypesCore.h:100
class containg the result of the fit and all the related information (fitted parameter values,...
Definition FitResult.h:47
RooArgList * GetInitFloatParamList()
Bool_t SetPdfParamVal(int index, double value) const
Set value of parameter i.
std::ofstream * GetLogFile()
void setOptimizeConst(Int_t flag)
virtual Bool_t Synchronize(std::vector< ROOT::Fit::ParameterSettings > &parameters, Bool_t optConst, Bool_t verbose)
Like synchronizeParameterSettings, Synchronize informs Minuit through its parameter_settings vector o...
std::unique_ptr< RooArgList > _floatParamList
void SetPrintEvalErrors(Int_t numEvalErrors)
RooArgList * GetConstParamList()
virtual ~RooAbsMinimizerFcn()=default
virtual void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt)=0
This function must be overridden in the derived class to pass on constant term optimization configura...
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
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...
Bool_t synchronizeParameterSettings(std::vector< ROOT::Fit::ParameterSettings > &parameters, Bool_t optConst, Bool_t verbose)
Informs Minuit through its parameter_settings vector of RooFit parameter properties.
RooArgList * GetFloatParamList()
Logistics.
void SetEvalErrorWall(Bool_t flag)
void SetVerbose(Bool_t flag=kTRUE)
unsigned int NDim() const
std::unique_ptr< RooArgList > _constParamList
RooArgList * GetInitConstParamList()
unsigned int getNDim() const
std::unique_ptr< RooArgList > _initFloatParamList
Bool_t SetLogFile(const char *inLogfile)
Change the file name for logging of a RooMinimizer of all MINUIT steppings through the parameter spac...
void SetRecoverFromNaNStrength(double strength)
Try to recover from invalid function values.
virtual void setOffsetting(Bool_t flag)=0
Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
void SetPdfParamErr(Int_t index, Double_t value)
Modify PDF parameter error by ordinal index (needed by MINUIT)
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...