Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:32:58

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 <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 } // namespace RooFit
0047 
0048 class RooMinimizer : public TObject {
0049 public:
0050    // Internal struct for the temporary fit result.
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;                       ///< flag for indicating valid fit
0068       int fStatus = -1;                          ///< minimizer status code
0069       int fCovStatus = -1;                       ///< covariance matrix status code
0070       double fVal = 0;                           ///< minimum function value
0071       double fEdm = -1;                          ///< expected distance from minimum
0072       std::map<unsigned int, bool> fFixedParams; ///< list of fixed parameters
0073       std::vector<double> fParams;               ///< parameter values. Size is total number of parameters
0074       std::vector<double> fErrors;               ///< errors
0075       std::vector<double> fCovMatrix; ///< covariance matrix (size is npar*(npar+1)/2) where npar is total parameters
0076       std::vector<double> fGlobalCC;  ///< global Correlation coefficient
0077       std::map<unsigned int, std::pair<double, double>> fMinosErrors; ///< map contains the two Minos errors
0078       std::string fMinimType;                                         ///< string indicating type of minimizer
0079    };
0080 
0081    /// Config argument to RooMinimizer constructor.
0082    struct Config {
0083 
0084       Config() {}
0085 
0086       bool useGradient = true; // Use the gradient provided by the RooAbsReal, if there is one.
0087 
0088       double recoverFromNaN = 10.; // RooAbsMinimizerFcn config
0089       int printEvalErrors = 10;    // RooAbsMinimizerFcn config
0090       int doEEWall = 1;            // RooAbsMinimizerFcn config
0091       int offsetting = -1;         // RooAbsMinimizerFcn config
0092       const char *logf = nullptr;  // RooAbsMinimizerFcn config
0093 
0094       // RooAbsMinimizerFcn config that can only be set in constructor, 0 means no parallelization (default),
0095       // -1 is parallelization with the number of workers controlled by RooFit::MultiProcess which
0096       // defaults to the number of available processors, n means parallelization with n CPU's
0097       int parallelize = 0;
0098 
0099       // Experimental: RooAbsMinimizerFcn config that can only be set in constructor
0100       // argument is ignored when parallelize is 0
0101       bool enableParallelGradient = true;
0102 
0103       // Experimental: RooAbsMinimizerFcn config that can only be set in constructor
0104       // argument is ignored when parallelize is 0
0105       bool enableParallelDescent = false;
0106 
0107       bool verbose = false;        // local config
0108       bool profile = false;        // local config
0109       bool timingAnalysis = false; // local config
0110       std::string minimizerType;   // local config
0111 
0112       bool setInitialCovariance = false; // Use covariance matrix provided by user
0113    };
0114 
0115    // For backwards compatibility with when the RooMinimizer used the ROOT::Math::Fitter.
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    // Setters on _theFitter
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    // Setters on _fcn
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    /// Clears the Minuit status history.
0183    void clearStatusHistory() { _statusHistory.clear(); }
0184 
0185    int evalCounter() const;
0186    void zeroEvalCount();
0187 
0188    /// Return underlying ROOT fitter object
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    // constructor helper functions
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;                      ///< fitter configuration (options and parameter settings)
0237    std::unique_ptr<FitResult> _result;                ///<! pointer to the object containing the result of the fit
0238    std::unique_ptr<ROOT::Math::Minimizer> _minimizer; ///<! pointer to used 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; // local config object
0247 
0248    ClassDefOverride(RooMinimizer, 0) // RooFit interface to ROOT::Math::Minimizer
0249 };
0250 
0251 #endif