Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RooAbsMinimizerFcn.h
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*****************************************************************************
4 * Project: RooFit *
5 * Package: RooFitCore *
6 * @(#)root/roofitcore:$Id$
7 * Authors: *
8 * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it *
9 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl *
10 * *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17#ifndef ROO_ABS_MINIMIZER_FCN
18#define ROO_ABS_MINIMIZER_FCN
19
20#include "Math/IFunction.h"
21
22#include "TMatrixDSym.h"
23
24#include "RooAbsReal.h"
25#include "RooArgList.h"
26#include "RooMinimizer.h"
27#include "RooRealVar.h"
28
29#include <Math/Minimizer.h>
30
31#include <iostream>
32#include <fstream>
33#include <string>
34#include <memory> // unique_ptr
35
37
38public:
39 RooAbsMinimizerFcn(RooArgList paramList, RooMinimizer *context);
40 virtual ~RooAbsMinimizerFcn() = default;
41
42 virtual void initMinimizer(ROOT::Math::Minimizer &, RooMinimizer *context) = 0;
43
44 /// Informs Minuit through its parameter_settings vector of RooFit parameter properties.
45 bool synchronizeParameterSettings(std::vector<ROOT::Fit::ParameterSettings> &parameters, bool optConst);
46 /// Like synchronizeParameterSettings, Synchronize informs Minuit through
47 /// its parameter_settings vector of RooFit parameter properties, but
48 /// Synchronize can be overridden to e.g. also include gradient strategy
49 /// synchronization in subclasses.
50 virtual bool Synchronize(std::vector<ROOT::Fit::ParameterSettings> &parameters);
51
52 RooArgList const &allParams() const { return _allParams; }
53 RooArgList floatParams() const;
54 RooArgList constParams() const;
55 RooArgList initFloatParams() const;
56 Int_t GetNumInvalidNLL() const { return _numBadNLL; }
57
58 double &GetMaxFCN() { return _maxFCN; }
59 Int_t evalCounter() const { return _evalCounter; }
60 void zeroEvalCount() { _evalCounter = 0; }
61 /// Return a possible offset that's applied to the function to separate invalid function values from valid ones.
62 double &getOffset() const { return _funcOffset; }
63
64 /// Put Minuit results back into RooFit objects.
65 void BackProp();
66
67 /// RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
68 virtual std::string getFunctionName() const = 0;
69 /// RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class.
70 virtual std::string getFunctionTitle() const = 0;
71
72 /// Set different external covariance matrix
73 void ApplyCovarianceMatrix(TMatrixDSym &V);
74
75 bool SetLogFile(const char *inLogfile);
76 std::ofstream *GetLogFile() { return _logfile; }
77
78 unsigned int getNDim() const { return _floatableParamIndices.size(); }
79
80 void setOptimizeConst(Int_t flag);
81
82 bool SetPdfParamVal(int index, double value) const;
83
84 /// Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
85 virtual void setOffsetting(bool flag) = 0;
86
87 RooMinimizer::Config const &cfg() const { return _context->_cfg; }
88
89 inline RooRealVar &floatableParam(std::size_t i) const
90 {
91 return static_cast<RooRealVar &>(_allParams[_floatableParamIndices[i]]);
92 }
93
94 virtual RooArgSet freezeDisconnectedParameters() const { return {}; }
95
96protected:
97 void optimizeConstantTerms(bool constStatChange, bool constValChange);
98 /// This function must be overridden in the derived class to pass on constant term optimization configuration
99 /// to the function to be minimized. For a RooAbsArg, this would be RooAbsArg::constOptimizeTestStatistic.
100 virtual void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) = 0;
101
102 void printEvalErrors() const;
103
104 double applyEvalErrorHandling(double fvalue) const;
105 void finishDoEval() const;
106
107 inline static bool canBeFloating(RooAbsArg const &arg) { return dynamic_cast<RooRealVar const *>(&arg); }
108
109 // Figure out whether we have to treat this parameter as a constant.
110 inline static bool treatAsConstant(RooAbsArg const &arg) { return arg.isConstant() || !canBeFloating(arg); }
111
112 // members
113 RooMinimizer *_context = nullptr;
114
115 // the following four are mutable because DoEval is const (in child classes)
116 // Reset the *largest* negative log-likelihood value we have seen so far:
117 mutable double _maxFCN = -std::numeric_limits<double>::infinity();
118 mutable double _funcOffset{0.};
119 mutable int _numBadNLL = 0;
120 mutable int _evalCounter{0};
121 // PB: these mutables signal a suboptimal design. A separate error handling
122 // object containing all this would clean up this class. It would allow const
123 // functions to be actually const (even though state still changes in the
124 // error handling object).
125
126 bool _optConst = false;
127
128 RooArgList _allParams;
129 RooArgList _allParamsInit;
130
131 std::vector<std::size_t> _floatableParamIndices;
132
133 std::ofstream *_logfile = nullptr;
134};
135
136#endif
137
138/// \endcond
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
TMatrixTSym< Double_t > TMatrixDSym
bool isConstant() const
Check if the "Constant" attribute is set.
Definition RooAbsArg.h:283