Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:24

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 <Fit/Fitter.h>
0025 #include <TStopwatch.h>
0026 #include <TMatrixDSymfwd.h>
0027 
0028 #include <fstream>
0029 #include <memory> // shared_ptr, unique_ptr
0030 #include <string>
0031 #include <utility>
0032 #include <vector>
0033 
0034 class RooAbsMinimizerFcn;
0035 class RooAbsReal;
0036 class RooFitResult;
0037 class RooArgList;
0038 class RooRealVar;
0039 class RooArgSet;
0040 class RooPlot;
0041 class RooDataSet;
0042 namespace RooFit {
0043 namespace TestStatistics {
0044 class LikelihoodGradientJob;
0045 }
0046 } // namespace RooFit
0047 
0048 class RooMinimizer : public TObject {
0049 public:
0050    /// Config argument to RooMinimizer constructor.
0051    struct Config {
0052 
0053       Config() {}
0054 
0055       bool useGradient = true; // Use the gradient provided by the RooAbsReal, if there is one.
0056 
0057       double recoverFromNaN = 10.; // RooAbsMinimizerFcn config
0058       int printEvalErrors = 10;    // RooAbsMinimizerFcn config
0059       int doEEWall = 1;            // RooAbsMinimizerFcn config
0060       int offsetting = -1;         // RooAbsMinimizerFcn config
0061       const char *logf = nullptr;  // RooAbsMinimizerFcn config
0062 
0063       // RooAbsMinimizerFcn config that can only be set in constructor, 0 means no parallelization (default),
0064       // -1 is parallelization with the number of workers controlled by RooFit::MultiProcess which
0065       // defaults to the number of available processors, n means parallelization with n CPU's
0066       int parallelize = 0;
0067 
0068       // Experimental: RooAbsMinimizerFcn config that can only be set in constructor
0069       // argument is ignored when parallelize is 0
0070       bool enableParallelGradient = true;
0071 
0072       // Experimental: RooAbsMinimizerFcn config that can only be set in constructor
0073       // argument is ignored when parallelize is 0
0074       bool enableParallelDescent = false;
0075 
0076       bool verbose = false;        // local config
0077       bool profile = false;        // local config
0078       bool timingAnalysis = false; // local config
0079       std::string minimizerType;   // local config
0080    private:
0081       int getDefaultWorkers();
0082    };
0083 
0084    explicit RooMinimizer(RooAbsReal &function, Config const &cfg = {});
0085 
0086    ~RooMinimizer() override;
0087 
0088    enum Strategy { Speed = 0, Balance = 1, Robustness = 2 };
0089    enum PrintLevel { None = -1, Reduced = 0, Normal = 1, ExtraForProblem = 2, Maximum = 3 };
0090 
0091    // Setters on _theFitter
0092    void setStrategy(int istrat);
0093    void setErrorLevel(double level);
0094    void setEps(double eps);
0095    void setMaxIterations(int n);
0096    void setMaxFunctionCalls(int n);
0097    void setPrintLevel(int newLevel);
0098 
0099    // Setters on _fcn
0100    void optimizeConst(int flag);
0101    void setEvalErrorWall(bool flag) { _cfg.doEEWall = flag; }
0102    void setRecoverFromNaNStrength(double strength);
0103    void setOffsetting(bool flag);
0104    void setPrintEvalErrors(int numEvalErrors) { _cfg.printEvalErrors = numEvalErrors; }
0105    void setVerbose(bool flag = true) { _cfg.verbose = flag; }
0106    bool setLogFile(const char *logf = nullptr);
0107 
0108    int migrad();
0109    int hesse();
0110    int minos();
0111    int minos(const RooArgSet &minosParamList);
0112    int seek();
0113    int simplex();
0114    int improve();
0115 
0116    int minimize(const char *type, const char *alg = nullptr);
0117 
0118    RooFit::OwningPtr<RooFitResult> save(const char *name = nullptr, const char *title = nullptr);
0119    RooPlot *contour(RooRealVar &var1, RooRealVar &var2, double n1 = 1.0, double n2 = 2.0, double n3 = 0.0,
0120                     double n4 = 0.0, double n5 = 0.0, double n6 = 0.0, unsigned int npoints = 50);
0121 
0122    void setProfile(bool flag = true) { _cfg.profile = flag; }
0123    /// Enable or disable the logging of function evaluations to a RooDataSet.
0124    /// \see RooMinimizer::getLogDataSet().
0125    /// param[in] flag Boolean flag to disable or enable the functionality.
0126    void setLoggingToDataSet(bool flag = true) { _loggingToDataSet = flag; }
0127 
0128    /// If logging of function evaluations to a RooDataSet is enabled, returns a
0129    /// pointer to a dataset with one row per evaluation of the RooAbsReal passed
0130    /// to the minimizer. As columns, there are all floating parameters and the
0131    /// values they had for that evaluation.
0132    /// \see RooMinimizer::setLoggingToDataSet(bool).
0133    RooDataSet *getLogDataSet() const { return _logDataSet.get(); }
0134 
0135    static int getPrintLevel();
0136 
0137    void setMinimizerType(std::string const &type);
0138    std::string const &minimizerType() const { return _cfg.minimizerType; }
0139 
0140    static void cleanup();
0141    static RooFit::OwningPtr<RooFitResult> lastMinuitFit();
0142    static RooFit::OwningPtr<RooFitResult> lastMinuitFit(const RooArgList &varList);
0143 
0144    void saveStatus(const char *label, int status) { _statusHistory.emplace_back(label, status); }
0145 
0146    /// Clears the Minuit status history.
0147    void clearStatusHistory() { _statusHistory.clear(); }
0148 
0149    int evalCounter() const;
0150    void zeroEvalCount();
0151 
0152    ROOT::Fit::Fitter *fitter();
0153    const ROOT::Fit::Fitter *fitter() const;
0154 
0155    ROOT::Math::IMultiGenFunction *getMultiGenFcn() const;
0156 
0157    int getNPar() const;
0158 
0159    void applyCovarianceMatrix(TMatrixDSym const &V);
0160 
0161 private:
0162    friend class RooAbsMinimizerFcn;
0163    friend class RooMinimizerFcn;
0164    friend class RooFit::TestStatistics::LikelihoodGradientJob;
0165 
0166    std::unique_ptr<RooAbsReal::EvalErrorContext> makeEvalErrorContext() const;
0167 
0168    void addParamsToProcessTimer();
0169 
0170    void profileStart();
0171    void profileStop();
0172 
0173    std::ofstream *logfile();
0174    double &maxFCN();
0175    double &fcnOffset() const;
0176 
0177    bool fitFcn() const;
0178 
0179    // constructor helper functions
0180    void initMinimizerFirstPart();
0181    void initMinimizerFcnDependentPart(double defaultErrorLevel);
0182 
0183    void determineStatus(bool fitterReturnValue);
0184 
0185    int _status = -99;
0186    bool _profileStart = false;
0187    bool _loggingToDataSet = false;
0188 
0189    TStopwatch _timer;
0190    TStopwatch _cumulTimer;
0191 
0192    std::unique_ptr<TMatrixDSym> _extV;
0193 
0194    std::unique_ptr<RooAbsMinimizerFcn> _fcn;
0195 
0196    static std::unique_ptr<ROOT::Fit::Fitter> _theFitter;
0197 
0198    std::vector<std::pair<std::string, int>> _statusHistory;
0199 
0200    std::unique_ptr<RooDataSet> _logDataSet;
0201 
0202    RooMinimizer::Config _cfg; // local config object
0203 
0204    ClassDefOverride(RooMinimizer, 0) // RooFit interface to ROOT::Fit::Fitter
0205 };
0206 
0207 #endif