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