File indexing completed on 2025-12-18 10:36:26
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
0086 double recoverFromNaN = 10.;
0087 int printEvalErrors = 10;
0088 int doEEWall = 1;
0089 int offsetting = -1;
0090 const char *logf = nullptr;
0091
0092
0093
0094
0095 int parallelize = 0;
0096
0097
0098
0099 bool enableParallelGradient = true;
0100
0101
0102
0103 bool enableParallelDescent = false;
0104
0105 bool verbose = false;
0106 bool profile = false;
0107 bool timingAnalysis = false;
0108 std::string minimizerType;
0109
0110 bool setInitialCovariance = false;
0111 };
0112
0113
0114 class FitterInterface {
0115 public:
0116 FitterInterface(ROOT::Fit::FitConfig *config, ROOT::Math::Minimizer *minimizer, FitResult const *result)
0117 : _config{config}, _minimizer{minimizer}, _result{result}
0118 {
0119 }
0120
0121 ROOT::Fit::FitConfig &Config() const { return *_config; }
0122 ROOT::Math::Minimizer *GetMinimizer() const { return _minimizer; }
0123 const FitResult &Result() const { return *_result; }
0124
0125 private:
0126 ROOT::Fit::FitConfig *_config = nullptr;
0127 ROOT::Math::Minimizer *_minimizer = nullptr;
0128 FitResult const *_result = nullptr;
0129 };
0130
0131 explicit RooMinimizer(RooAbsReal &function, Config const &cfg = {});
0132
0133 ~RooMinimizer() override;
0134
0135 enum Strategy { Speed = 0, Balance = 1, Robustness = 2 };
0136 enum PrintLevel { None = -1, Reduced = 0, Normal = 1, ExtraForProblem = 2, Maximum = 3 };
0137
0138
0139 void setStrategy(int istrat);
0140 void setErrorLevel(double level);
0141 void setEps(double eps);
0142 void setMaxIterations(int n);
0143 void setMaxFunctionCalls(int n);
0144 void setPrintLevel(int newLevel);
0145
0146
0147 void optimizeConst(int flag);
0148 void setEvalErrorWall(bool flag) { _cfg.doEEWall = flag; }
0149 void setRecoverFromNaNStrength(double strength);
0150 void setOffsetting(bool flag);
0151 void setPrintEvalErrors(int numEvalErrors) { _cfg.printEvalErrors = numEvalErrors; }
0152 void setVerbose(bool flag = true) { _cfg.verbose = flag; }
0153 bool setLogFile(const char *logf = nullptr);
0154
0155 int migrad();
0156 int hesse();
0157 int minos();
0158 int minos(const RooArgSet &minosParamList);
0159 int seek();
0160 int simplex();
0161 int improve();
0162
0163 int minimize(const char *type, const char *alg = nullptr);
0164
0165 RooFit::OwningPtr<RooFitResult> save(const char *name = nullptr, const char *title = nullptr);
0166 RooPlot *contour(RooRealVar &var1, RooRealVar &var2, double n1 = 1.0, double n2 = 2.0, double n3 = 0.0,
0167 double n4 = 0.0, double n5 = 0.0, double n6 = 0.0, unsigned int npoints = 50);
0168
0169 void setProfile(bool flag = true) { _cfg.profile = flag; }
0170
0171 int getPrintLevel();
0172
0173 void setMinimizerType(std::string const &type);
0174 std::string const &minimizerType() const { return _cfg.minimizerType; }
0175
0176 RooFit::OwningPtr<RooFitResult> lastMinuitFit();
0177
0178 void saveStatus(const char *label, int status) { _statusHistory.emplace_back(label, status); }
0179
0180
0181 void clearStatusHistory() { _statusHistory.clear(); }
0182
0183 int evalCounter() const;
0184 void zeroEvalCount();
0185
0186
0187 inline auto fitter() { return std::make_unique<FitterInterface>(&_config, _minimizer.get(), _result.get()); }
0188
0189 int getNPar() const;
0190
0191 void applyCovarianceMatrix(TMatrixDSym const &V);
0192
0193 private:
0194 friend class RooAbsMinimizerFcn;
0195 friend class RooMinimizerFcn;
0196 friend class RooFit::TestStatistics::LikelihoodGradientJob;
0197
0198 std::unique_ptr<RooAbsReal::EvalErrorContext> makeEvalErrorContext() const;
0199
0200 void addParamsToProcessTimer();
0201
0202 void profileStart();
0203 void profileStop();
0204
0205 std::ofstream *logfile();
0206 double &maxFCN();
0207 double &fcnOffset() const;
0208
0209
0210 void initMinimizerFirstPart();
0211 void initMinimizerFcnDependentPart(double defaultErrorLevel);
0212
0213 void determineStatus(bool fitterReturnValue);
0214
0215 int exec(std::string const &algoName, std::string const &statusName);
0216
0217 bool fitFCN();
0218
0219 bool calculateHessErrors();
0220 bool calculateMinosErrors();
0221
0222 void initMinimizer();
0223 void updateFitConfig();
0224 bool updateMinimizerOptions(bool canDifferentMinim = true);
0225
0226 void fillResult(bool isValid);
0227 bool update(bool isValid);
0228
0229 void fillCorrMatrix(RooFitResult &fitRes);
0230 void updateErrors();
0231
0232 ROOT::Fit::FitConfig _config;
0233 std::unique_ptr<FitResult> _result;
0234 std::unique_ptr<ROOT::Math::Minimizer> _minimizer;
0235 int _status = -99;
0236 bool _profileStart = false;
0237 TStopwatch _timer;
0238 TStopwatch _cumulTimer;
0239 std::unique_ptr<TMatrixDSym> _extV;
0240 std::unique_ptr<RooAbsMinimizerFcn> _fcn;
0241 std::vector<std::pair<std::string, int>> _statusHistory;
0242 RooMinimizer::Config _cfg;
0243
0244 ClassDefOverride(RooMinimizer, 0)
0245 };
0246
0247 #endif