File indexing completed on 2026-09-14 09:23:25
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 <RooAbsReal.h>
0021
0022 #include <TStopwatch.h>
0023 #include <TMatrixDSymfwd.h>
0024
0025 #include <Fit/FitConfig.h>
0026
0027 #include <fstream>
0028 #include <map>
0029 #include <memory>
0030 #include <string>
0031 #include <utility>
0032 #include <vector>
0033
0034 class RooAbsMinimizerFcn;
0035 class RooFitResult;
0036 class RooArgList;
0037 class RooRealVar;
0038 class RooArgSet;
0039 class RooPlot;
0040 namespace RooFit {
0041 namespace TestStatistics {
0042 class LikelihoodGradientJob;
0043 }
0044 }
0045
0046 class RooMinimizer : public TObject {
0047 public:
0048
0049 struct FitResult {
0050
0051 FitResult() = default;
0052 FitResult(const ROOT::Fit::FitConfig &fconfig);
0053
0054 double error(unsigned int i) const { return (i < fErrors.size()) ? fErrors[i] : 0; }
0055 double lowerError(unsigned int i) const;
0056 double upperError(unsigned int i) const;
0057
0058 double Edm() const { return fEdm; }
0059 bool IsValid() const { return fValid; }
0060 int Status() const { return fStatus; }
0061 void GetCovarianceMatrix(TMatrixDSym &cov) const;
0062
0063 bool isParameterFixed(unsigned int ipar) const;
0064
0065 bool fValid = false;
0066 int fStatus = -1;
0067 int fCovStatus = -1;
0068 double fVal = 0;
0069 double fEdm = -1;
0070 std::map<unsigned int, bool> fFixedParams;
0071 std::vector<double> fParams;
0072 std::vector<double> fErrors;
0073 std::vector<double> fCovMatrix;
0074 std::vector<double> fGlobalCC;
0075 std::map<unsigned int, std::pair<double, double>> fMinosErrors;
0076 std::string fMinimType;
0077 };
0078
0079
0080 struct Config {
0081
0082 Config() {}
0083
0084 bool useGradient = true;
0085 bool useHessian = false;
0086
0087 double recoverFromNaN = 10.;
0088 int printEvalErrors = 10;
0089 int doEEWall = 1;
0090 int offsetting = -1;
0091 const char *logf = nullptr;
0092
0093
0094
0095
0096 int parallelize = 0;
0097
0098
0099
0100 bool enableParallelGradient = true;
0101
0102
0103
0104 bool enableParallelDescent = false;
0105
0106 bool verbose = false;
0107 bool profile = false;
0108 bool timingAnalysis = false;
0109 std::string minimizerType;
0110
0111 bool setInitialCovariance = false;
0112 };
0113
0114
0115 class FitterInterface {
0116 public:
0117 FitterInterface(ROOT::Fit::FitConfig *config, ROOT::Math::Minimizer *minimizer, FitResult const *result)
0118 : _config{config}, _minimizer{minimizer}, _result{result}
0119 {
0120 }
0121
0122 ROOT::Fit::FitConfig &Config() const { return *_config; }
0123 ROOT::Math::Minimizer *GetMinimizer() const { return _minimizer; }
0124 const FitResult &Result() const { return *_result; }
0125
0126 private:
0127 ROOT::Fit::FitConfig *_config = nullptr;
0128 ROOT::Math::Minimizer *_minimizer = nullptr;
0129 FitResult const *_result = nullptr;
0130 };
0131
0132 explicit RooMinimizer(RooAbsReal &function, Config const &cfg = {});
0133
0134 ~RooMinimizer() override;
0135
0136 enum Strategy { Speed = 0, Balance = 1, Robustness = 2 };
0137 enum PrintLevel { None = -1, Reduced = 0, Normal = 1, ExtraForProblem = 2, Maximum = 3 };
0138
0139
0140 void setStrategy(int istrat);
0141 void setErrorLevel(double level);
0142 void setEps(double eps);
0143 void setMaxIterations(int n);
0144 void setMaxFunctionCalls(int n);
0145 void setPrintLevel(int newLevel);
0146
0147
0148 void optimizeConst(int flag)
0149 #ifndef ROOFIT_BUILDS_ITSELF
0150 R__DEPRECATED(6, 42, "Please use the default \"cpu\" likelihood evaluation backend if you want all optimizations.")
0151 #endif
0152 ;
0153 void setEvalErrorWall(bool flag) { _cfg.doEEWall = flag; }
0154 void setRecoverFromNaNStrength(double strength);
0155 void setOffsetting(bool flag);
0156 void setPrintEvalErrors(int numEvalErrors) { _cfg.printEvalErrors = numEvalErrors; }
0157 void setVerbose(bool flag = true) { _cfg.verbose = flag; }
0158 bool setLogFile(const char *logf = nullptr);
0159
0160 int migrad();
0161 int hesse();
0162 int minos();
0163 int minos(const RooArgSet &minosParamList);
0164 int seek();
0165 int simplex();
0166 int improve();
0167
0168 int minimize(const char *type, const char *alg = nullptr);
0169
0170 RooFit::OwningPtr<RooFitResult> save(const char *name = nullptr, const char *title = nullptr);
0171 RooPlot *contour(RooRealVar &var1, RooRealVar &var2, double n1 = 1.0, double n2 = 2.0, double n3 = 0.0,
0172 double n4 = 0.0, double n5 = 0.0, double n6 = 0.0, unsigned int npoints = 50);
0173
0174 void setProfile(bool flag = true) { _cfg.profile = flag; }
0175
0176 int getPrintLevel();
0177
0178 void setMinimizerType(std::string const &type);
0179 std::string const &minimizerType() const { return _cfg.minimizerType; }
0180
0181 RooFit::OwningPtr<RooFitResult> lastMinuitFit();
0182
0183 void saveStatus(const char *label, int status) { _statusHistory.emplace_back(label, status); }
0184
0185
0186 void clearStatusHistory() { _statusHistory.clear(); }
0187
0188 int evalCounter() const;
0189 void zeroEvalCount();
0190
0191
0192 inline auto fitter() { return std::make_unique<FitterInterface>(&_config, _minimizer.get(), _result.get()); }
0193
0194 int getNPar() const;
0195
0196 void applyCovarianceMatrix(TMatrixDSym const &V);
0197
0198 private:
0199 friend class RooAbsMinimizerFcn;
0200 friend class RooMinimizerFcn;
0201 friend class RooFit::TestStatistics::LikelihoodGradientJob;
0202
0203 std::unique_ptr<RooAbsReal::EvalErrorContext> makeEvalErrorContext() const;
0204
0205 void addParamsToProcessTimer();
0206
0207 void profileStart();
0208 void profileStop();
0209
0210 std::ofstream *logfile();
0211 double &maxFCN();
0212 double &fcnOffset() const;
0213
0214
0215 void initMinimizerFirstPart();
0216 void initMinimizerFcnDependentPart(double defaultErrorLevel);
0217
0218 void determineStatus(bool fitterReturnValue);
0219
0220 int exec(std::string const &algoName, std::string const &statusName);
0221
0222 bool fitFCN();
0223
0224 bool calculateHessErrors();
0225 bool calculateMinosErrors();
0226
0227 void initMinimizer();
0228 void updateFitConfig();
0229 bool updateMinimizerOptions(bool canDifferentMinim = true);
0230
0231 void fillResult(bool isValid);
0232 bool update(bool isValid);
0233
0234 void fillCorrMatrix(RooFitResult &fitRes);
0235 void updateErrors();
0236
0237 RooAbsReal &_function;
0238 ROOT::Fit::FitConfig _config;
0239 std::unique_ptr<FitResult> _result;
0240 std::unique_ptr<ROOT::Math::Minimizer> _minimizer;
0241 int _status = -99;
0242 bool _profileStart = false;
0243 TStopwatch _timer;
0244 TStopwatch _cumulTimer;
0245 std::unique_ptr<TMatrixDSym> _extV;
0246 std::unique_ptr<RooAbsMinimizerFcn> _fcn;
0247 std::vector<std::pair<std::string, int>> _statusHistory;
0248 RooMinimizer::Config _cfg;
0249
0250 ClassDefOverride(RooMinimizer, 0)
0251 };
0252
0253 #endif