Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-14 09:23:25

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id$
0005  * Authors:                                                                  *
0006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
0007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
0008  *   AL, Alfio Lazzaro,   INFN Milan,        alfio.lazzaro@mi.infn.it        *
0009  *   PB, Patrick Bos,     NL eScience Center, p.bos@esciencecenter.nl        *
0010  *                                                                           *
0011  *                                                                           *
0012  * Redistribution and use in source and binary forms,                        *
0013  * with or without modification, are permitted according to the terms        *
0014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
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 } // namespace RooFit
0045 
0046 class RooMinimizer : public TObject {
0047 public:
0048    // Internal struct for the temporary fit result.
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;                       ///< flag for indicating valid fit
0066       int fStatus = -1;                          ///< minimizer status code
0067       int fCovStatus = -1;                       ///< covariance matrix status code
0068       double fVal = 0;                           ///< minimum function value
0069       double fEdm = -1;                          ///< expected distance from minimum
0070       std::map<unsigned int, bool> fFixedParams; ///< list of fixed parameters
0071       std::vector<double> fParams;               ///< parameter values. Size is total number of parameters
0072       std::vector<double> fErrors;               ///< errors
0073       std::vector<double> fCovMatrix; ///< covariance matrix (size is npar*(npar+1)/2) where npar is total parameters
0074       std::vector<double> fGlobalCC;  ///< global Correlation coefficient
0075       std::map<unsigned int, std::pair<double, double>> fMinosErrors; ///< map contains the two Minos errors
0076       std::string fMinimType;                                         ///< string indicating type of minimizer
0077    };
0078 
0079    /// Config argument to RooMinimizer constructor.
0080    struct Config {
0081 
0082       Config() {}
0083 
0084       bool useGradient = true; // Use the gradient provided by the RooAbsReal, if there is one.
0085       bool useHessian = false; // Use the Hessian provided by the RooAbsReal, if there is one.
0086 
0087       double recoverFromNaN = 10.; // RooAbsMinimizerFcn config
0088       int printEvalErrors = 10;    // RooAbsMinimizerFcn config
0089       int doEEWall = 1;            // RooAbsMinimizerFcn config
0090       int offsetting = -1;         // RooAbsMinimizerFcn config
0091       const char *logf = nullptr;  // RooAbsMinimizerFcn config
0092 
0093       // RooAbsMinimizerFcn config that can only be set in constructor, 0 means no parallelization (default),
0094       // -1 is parallelization with the number of workers controlled by RooFit::MultiProcess which
0095       // defaults to the number of available processors, n means parallelization with n CPU's
0096       int parallelize = 0;
0097 
0098       // Experimental: RooAbsMinimizerFcn config that can only be set in constructor
0099       // argument is ignored when parallelize is 0
0100       bool enableParallelGradient = true;
0101 
0102       // Experimental: RooAbsMinimizerFcn config that can only be set in constructor
0103       // argument is ignored when parallelize is 0
0104       bool enableParallelDescent = false;
0105 
0106       bool verbose = false;        // local config
0107       bool profile = false;        // local config
0108       bool timingAnalysis = false; // local config
0109       std::string minimizerType;   // local config
0110 
0111       bool setInitialCovariance = false; // Use covariance matrix provided by user
0112    };
0113 
0114    // For backwards compatibility with when the RooMinimizer used the ROOT::Math::Fitter.
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    // Setters on _theFitter
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    // Setters on _fcn
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    /// Clears the Minuit status history.
0186    void clearStatusHistory() { _statusHistory.clear(); }
0187 
0188    int evalCounter() const;
0189    void zeroEvalCount();
0190 
0191    /// Return underlying ROOT fitter object
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    // constructor helper functions
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;                      ///< fitter configuration (options and parameter settings)
0239    std::unique_ptr<FitResult> _result;                ///<! pointer to the object containing the result of the fit
0240    std::unique_ptr<ROOT::Math::Minimizer> _minimizer; ///<! pointer to used 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; // local config object
0249 
0250    ClassDefOverride(RooMinimizer, 0) // RooFit interface to ROOT::Math::Minimizer
0251 };
0252 
0253 #endif