Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 09:08:01

0001 // @(#)root/mathcore:$Id$
0002 // Author: L. Moneta Thu Sep 21 16:21:29 2006
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // Header file for class FitConfig
0012 
0013 #ifndef ROOT_Fit_FitConfig
0014 #define ROOT_Fit_FitConfig
0015 
0016 
0017 #include "Fit/ParameterSettings.h"
0018 
0019 #include "Math/MinimizerOptions.h"
0020 
0021 #include "Math/IParamFunctionfwd.h"
0022 
0023 #include "TMath.h"
0024 
0025 #include <vector>
0026 #include <string>
0027 
0028 namespace ROOT {
0029 
0030    namespace Math {
0031 
0032       class Minimizer;
0033       class MinimizerOptions;
0034    }
0035 
0036    namespace Fit {
0037 
0038       class FitResult;
0039 
0040 //___________________________________________________________________________________
0041 /**
0042    Class describing the configuration of the fit, options and parameter settings
0043    using the ROOT::Fit::ParameterSettings class
0044 
0045    @see ROOT::Math::MinimizerOptions::SetDefaultMinimizer
0046 
0047    @ingroup FitMain
0048 */
0049 class FitConfig {
0050 
0051 public:
0052 
0053    /**
0054       Default constructor
0055    */
0056    FitConfig (unsigned int npar = 0);
0057 
0058 
0059    /*
0060      Copy constructor
0061     */
0062    FitConfig(const FitConfig & rhs);
0063 
0064    /**
0065       Destructor
0066    */
0067    ~FitConfig ();
0068 
0069    /*
0070      Assignment operator
0071    */
0072    FitConfig & operator= (const FitConfig & rhs);
0073 
0074 
0075    /**
0076       get the parameter settings for the i-th parameter (const method)
0077    */
0078    const ParameterSettings & ParSettings(unsigned int i) const { return fSettings.at(i); }
0079 
0080    /**
0081       get the parameter settings for the i-th parameter (non-const method)
0082    */
0083    ParameterSettings & ParSettings(unsigned int i) { return fSettings.at(i); }
0084 
0085    /**
0086       get the vector of parameter settings  (const method)
0087    */
0088    const std::vector<ROOT::Fit::ParameterSettings> & ParamsSettings() const { return fSettings; }
0089 
0090    /**
0091       get the vector of parameter settings  (non-const method)
0092    */
0093    std::vector<ROOT::Fit::ParameterSettings> & ParamsSettings() { return fSettings; }
0094 
0095    /**
0096       number of parameters settings
0097     */
0098    unsigned int NPar() const { return fSettings.size(); }
0099 
0100    /**
0101       return a vector of stored parameter values (i.e initial fit parameters)
0102     */
0103    std::vector<double> ParamsValues() const;
0104 
0105 
0106    /**
0107       set the parameter settings from a model function.
0108       Create always new parameter setting list from a given model function
0109    */
0110    template <class T>
0111    void CreateParamsSettings(const ROOT::Math::IParamMultiFunctionTempl<T> &func) {
0112       // initialize from model function
0113       // set the parameters values from the function
0114       unsigned int npar = func.NPar();
0115       const double *begin = func.Parameters();
0116       if (!begin) {
0117          fSettings = std::vector<ParameterSettings>(npar);
0118          return;
0119       }
0120 
0121       fSettings.clear();
0122       fSettings.reserve(npar);
0123       const double *end = begin + npar;
0124       unsigned int i = 0;
0125       for (const double *ipar = begin; ipar != end; ++ipar) {
0126          double val = *ipar;
0127          double step = 0.3 * fabs(val); // step size is 30% of par value
0128          // double step = 2.0*fabs(val);   // step size is 30% of par value
0129          if (val == 0) step = 0.3;
0130 
0131          fSettings.push_back(ParameterSettings(func.ParameterName(i), val, step));
0132 #ifdef DEBUG
0133          std::cout << "FitConfig: add parameter " << func.ParameterName(i) << " val = " << val << std::endl;
0134 #endif
0135          i++;
0136       }
0137    }
0138 
0139    /**
0140       set the parameter settings from number of parameters and a vector of values and optionally step values. If there are not existing or number of parameters does not match existing one, create a new parameter setting list.
0141    */
0142    void SetParamsSettings(unsigned int npar, const double * params, const double * vstep = nullptr);
0143 
0144    /*
0145      Set the parameter settings from a vector of parameter settings
0146    */
0147    void SetParamsSettings (const std::vector<ROOT::Fit::ParameterSettings>& pars) {
0148       fSettings = pars;
0149    }
0150 
0151 
0152    /*
0153      Set the parameter settings from a fit Result
0154    */
0155    void SetFromFitResult (const FitResult & rhs);
0156 
0157 
0158 
0159    /**
0160       create a new minimizer according to chosen configuration
0161    */
0162    ROOT::Math::Minimizer * CreateMinimizer();
0163 
0164 
0165 
0166    /**
0167       access to the minimizer  control parameter (non const method)
0168    */
0169    ROOT::Math::MinimizerOptions & MinimizerOptions()  { return fMinimizerOpts; }
0170 
0171 
0172    /**
0173       set all the minimizer options using class ROOT::Math::MinimizerOptions
0174       @see ROOT::Math::MinimizerOptions::SetDefaultMinimizer
0175     */
0176    void SetMinimizerOptions(const ROOT::Math::MinimizerOptions & minopt);
0177 
0178 
0179    /**
0180       set minimizer type and algorithm
0181       @see ROOT::Math::MinimizerOptions::SetDefaultMinimizer
0182    */
0183    void SetMinimizer(const char *type, const char *algo = nullptr) {
0184       if (type) fMinimizerOpts.SetMinimizerType(type);
0185       if (algo) fMinimizerOpts.SetMinimizerAlgorithm(algo);
0186    }
0187 
0188    /**
0189       return type of minimizer package
0190    */
0191    const std::string & MinimizerType() const { return fMinimizerOpts.MinimizerType(); }
0192 
0193    /**
0194       return type of minimizer algorithms
0195    */
0196    const std::string & MinimizerAlgoType() const { return fMinimizerOpts.MinimizerAlgorithm(); }
0197 
0198    /**
0199     * return Minimizer full name (type / algorithm)
0200     */
0201    std::string MinimizerName() const;
0202 
0203    /**
0204       flag to check if resulting errors are be normalized according to chi2/ndf
0205    */
0206    bool NormalizeErrors() const { return fNormErrors; }
0207 
0208    ///do analysis for parabolic errors
0209    bool ParabErrors() const { return fParabErrors; }
0210 
0211    ///do minos errors analysis on the  parameters
0212    bool MinosErrors() const { return fMinosErrors; }
0213 
0214    ///Update configuration after a fit using the FitResult
0215    bool UpdateAfterFit() const { return fUpdateAfterFit; }
0216 
0217    ///Apply Weight correction for error matrix computation
0218    bool UseWeightCorrection() const { return fWeightCorr; }
0219 
0220 
0221    /// return vector of parameter indices for which the Minos Error will be computed
0222    const std::vector<unsigned int> & MinosParams() const { return fMinosParams; }
0223 
0224    /**
0225       set the option to normalize the error on the result  according to chi2/ndf
0226    */
0227    void SetNormErrors(bool on = true) { fNormErrors= on; }
0228 
0229    ///set parabolic errors
0230    void SetParabErrors(bool on = true) { fParabErrors = on; }
0231 
0232    ///set Minos errors computation to be performed after fitting
0233    void SetMinosErrors(bool on = true) { fMinosErrors = on; }
0234 
0235    ///apply the weight correction for error matrix computation
0236    void SetWeightCorrection(bool on = true) { fWeightCorr = on; }
0237 
0238    /// set parameter indices for running Minos
0239    /// this can be used for running Minos on a subset of parameters - otherwise is run on all of them
0240    /// if MinosErrors() is set
0241    void SetMinosErrors(const std::vector<unsigned int> & paramInd ) {
0242       fMinosErrors = true;
0243       fMinosParams = paramInd;
0244    }
0245 
0246    ///Update configuration after a fit using the FitResult
0247    void SetUpdateAfterFit(bool on = true) { fUpdateAfterFit = on; }
0248 
0249 
0250    /**
0251       static function to control default minimizer type and algorithm
0252    */
0253    static void SetDefaultMinimizer(const char *type, const char *algo = nullptr);
0254 
0255 
0256 
0257 
0258 protected:
0259 
0260 
0261 private:
0262 
0263    bool fNormErrors;       ///< flag for error normalization
0264    bool fParabErrors;      ///< get correct parabolic errors estimate (call Hesse after minimizing)
0265    bool fMinosErrors;      ///< do full error analysis using Minos
0266    bool fUpdateAfterFit;   ///< update the configuration after a fit using the result
0267    bool fWeightCorr;       ///< apply correction to errors for weights fits
0268 
0269    std::vector<ROOT::Fit::ParameterSettings> fSettings;  ///< vector with the parameter settings
0270    std::vector<unsigned int> fMinosParams;               ///< vector with the parameter indices for running Minos
0271 
0272    ROOT::Math::MinimizerOptions fMinimizerOpts;   ///< minimizer control parameters including name and algo type
0273 
0274 };
0275 
0276    } // end namespace Fit
0277 
0278 } // end namespace ROOT
0279 
0280 
0281 #endif /* ROOT_Fit_FitConfig */