File indexing completed on 2025-01-18 10:11:24
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef ROO_MINIMIZER
0018 #define ROO_MINIMIZER
0019
0020 #include <RooFit/TestStatistics/RooAbsL.h>
0021 #include <RooFit/TestStatistics/LikelihoodWrapper.h>
0022 #include <RooFit/TestStatistics/LikelihoodGradientWrapper.h>
0023
0024 #include <Fit/Fitter.h>
0025 #include <TStopwatch.h>
0026 #include <TMatrixDSymfwd.h>
0027
0028 #include <fstream>
0029 #include <memory> // shared_ptr, unique_ptr
0030 #include <string>
0031 #include <utility>
0032 #include <vector>
0033
0034 class RooAbsMinimizerFcn;
0035 class RooAbsReal;
0036 class RooFitResult;
0037 class RooArgList;
0038 class RooRealVar;
0039 class RooArgSet;
0040 class RooPlot;
0041 class RooDataSet;
0042 namespace RooFit {
0043 namespace TestStatistics {
0044 class LikelihoodGradientJob;
0045 }
0046 }
0047
0048 class RooMinimizer : public TObject {
0049 public:
0050
0051 struct Config {
0052
0053 Config() {}
0054
0055 bool useGradient = true;
0056
0057 double recoverFromNaN = 10.;
0058 int printEvalErrors = 10;
0059 int doEEWall = 1;
0060 int offsetting = -1;
0061 const char *logf = nullptr;
0062
0063
0064
0065
0066 int parallelize = 0;
0067
0068
0069
0070 bool enableParallelGradient = true;
0071
0072
0073
0074 bool enableParallelDescent = false;
0075
0076 bool verbose = false;
0077 bool profile = false;
0078 bool timingAnalysis = false;
0079 std::string minimizerType;
0080 private:
0081 int getDefaultWorkers();
0082 };
0083
0084 explicit RooMinimizer(RooAbsReal &function, Config const &cfg = {});
0085
0086 ~RooMinimizer() override;
0087
0088 enum Strategy { Speed = 0, Balance = 1, Robustness = 2 };
0089 enum PrintLevel { None = -1, Reduced = 0, Normal = 1, ExtraForProblem = 2, Maximum = 3 };
0090
0091
0092 void setStrategy(int istrat);
0093 void setErrorLevel(double level);
0094 void setEps(double eps);
0095 void setMaxIterations(int n);
0096 void setMaxFunctionCalls(int n);
0097 void setPrintLevel(int newLevel);
0098
0099
0100 void optimizeConst(int flag);
0101 void setEvalErrorWall(bool flag) { _cfg.doEEWall = flag; }
0102 void setRecoverFromNaNStrength(double strength);
0103 void setOffsetting(bool flag);
0104 void setPrintEvalErrors(int numEvalErrors) { _cfg.printEvalErrors = numEvalErrors; }
0105 void setVerbose(bool flag = true) { _cfg.verbose = flag; }
0106 bool setLogFile(const char *logf = nullptr);
0107
0108 int migrad();
0109 int hesse();
0110 int minos();
0111 int minos(const RooArgSet &minosParamList);
0112 int seek();
0113 int simplex();
0114 int improve();
0115
0116 int minimize(const char *type, const char *alg = nullptr);
0117
0118 RooFit::OwningPtr<RooFitResult> save(const char *name = nullptr, const char *title = nullptr);
0119 RooPlot *contour(RooRealVar &var1, RooRealVar &var2, double n1 = 1.0, double n2 = 2.0, double n3 = 0.0,
0120 double n4 = 0.0, double n5 = 0.0, double n6 = 0.0, unsigned int npoints = 50);
0121
0122 void setProfile(bool flag = true) { _cfg.profile = flag; }
0123
0124
0125
0126 void setLoggingToDataSet(bool flag = true) { _loggingToDataSet = flag; }
0127
0128
0129
0130
0131
0132
0133 RooDataSet *getLogDataSet() const { return _logDataSet.get(); }
0134
0135 static int getPrintLevel();
0136
0137 void setMinimizerType(std::string const &type);
0138 std::string const &minimizerType() const { return _cfg.minimizerType; }
0139
0140 static void cleanup();
0141 static RooFit::OwningPtr<RooFitResult> lastMinuitFit();
0142 static RooFit::OwningPtr<RooFitResult> lastMinuitFit(const RooArgList &varList);
0143
0144 void saveStatus(const char *label, int status) { _statusHistory.emplace_back(label, status); }
0145
0146
0147 void clearStatusHistory() { _statusHistory.clear(); }
0148
0149 int evalCounter() const;
0150 void zeroEvalCount();
0151
0152 ROOT::Fit::Fitter *fitter();
0153 const ROOT::Fit::Fitter *fitter() const;
0154
0155 ROOT::Math::IMultiGenFunction *getMultiGenFcn() const;
0156
0157 int getNPar() const;
0158
0159 void applyCovarianceMatrix(TMatrixDSym const &V);
0160
0161 private:
0162 friend class RooAbsMinimizerFcn;
0163 friend class RooMinimizerFcn;
0164 friend class RooFit::TestStatistics::LikelihoodGradientJob;
0165
0166 std::unique_ptr<RooAbsReal::EvalErrorContext> makeEvalErrorContext() const;
0167
0168 void addParamsToProcessTimer();
0169
0170 void profileStart();
0171 void profileStop();
0172
0173 std::ofstream *logfile();
0174 double &maxFCN();
0175 double &fcnOffset() const;
0176
0177 bool fitFcn() const;
0178
0179
0180 void initMinimizerFirstPart();
0181 void initMinimizerFcnDependentPart(double defaultErrorLevel);
0182
0183 void determineStatus(bool fitterReturnValue);
0184
0185 int _status = -99;
0186 bool _profileStart = false;
0187 bool _loggingToDataSet = false;
0188
0189 TStopwatch _timer;
0190 TStopwatch _cumulTimer;
0191
0192 std::unique_ptr<TMatrixDSym> _extV;
0193
0194 std::unique_ptr<RooAbsMinimizerFcn> _fcn;
0195
0196 static std::unique_ptr<ROOT::Fit::Fitter> _theFitter;
0197
0198 std::vector<std::pair<std::string, int>> _statusHistory;
0199
0200 std::unique_ptr<RooDataSet> _logDataSet;
0201
0202 RooMinimizer::Config _cfg;
0203
0204 ClassDefOverride(RooMinimizer, 0)
0205 };
0206
0207 #endif