Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:08

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