File indexing completed on 2026-09-22 09:06:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef ROOT_Fit_FitResult
0014 #define ROOT_Fit_FitResult
0015
0016 #include "Math/IFunctionfwd.h"
0017 #include "Math/IParamFunctionfwd.h"
0018
0019 #include <vector>
0020 #include <map>
0021 #include <string>
0022 #include <cmath>
0023 #include <memory>
0024
0025 namespace ROOT::Math {
0026 class Minimizer;
0027 }
0028
0029 namespace ROOT::Fit {
0030
0031 class FitConfig;
0032 class FitData;
0033 class BinData;
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 class FitResult {
0045
0046 public:
0047
0048 typedef ROOT::Math::IParamMultiFunction IModelFunction;
0049
0050
0051
0052
0053 FitResult() = default;
0054
0055
0056
0057
0058
0059 FitResult (const FitConfig & fconfig);
0060
0061
0062
0063 virtual ~FitResult () {}
0064
0065
0066 public:
0067
0068
0069
0070
0071
0072 void FillResult(const std::shared_ptr<ROOT::Math::Minimizer> & min, const FitConfig & fconfig, const std::shared_ptr<IModelFunction> & f,
0073 bool isValid, unsigned int sizeOfData = 0, int fitType = 1, const ROOT::Math::IMultiGenFunction *chi2func = nullptr, unsigned int ncalls = 0);
0074
0075
0076
0077
0078
0079
0080
0081
0082 bool Update(const std::shared_ptr<ROOT::Math::Minimizer> & min, const ROOT::Fit::FitConfig & fconfig, bool isValid, unsigned int ncalls = 0);
0083
0084
0085
0086
0087 const std::string & MinimizerType() const { return fMinimType; }
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099 bool IsValid() const { return fValid; }
0100
0101
0102 bool IsEmpty() const { return (fParams.empty()); }
0103
0104
0105 double MinFcnValue() const { return fVal; }
0106
0107
0108 unsigned int NCalls() const { return fNCalls; }
0109
0110
0111 double Edm() const { return fEdm; }
0112
0113
0114 unsigned int NTotalParameters() const { return fParams.size(); }
0115
0116 unsigned int NPar() const { return NTotalParameters(); }
0117
0118
0119 unsigned int NFreeParameters() const { return fNFree; }
0120
0121
0122 int Status() const { return fStatus; }
0123
0124
0125
0126
0127 int CovMatrixStatus() const { return fCovStatus; }
0128
0129
0130
0131
0132
0133 const IModelFunction * FittedFunction() const {
0134 return fFitFunc.get();
0135 }
0136
0137
0138
0139
0140 const BinData * FittedBinData() const;
0141
0142
0143
0144
0145
0146
0147 double Chi2() const { return fChi2; }
0148
0149
0150 unsigned int Ndf() const { return fNdf; }
0151
0152
0153 double Prob() const;
0154
0155
0156 const std::vector<double> & Errors() const { return fErrors; }
0157
0158 const double * GetErrors() const { return fErrors.empty() ? nullptr : &fErrors.front(); }
0159
0160
0161 const std::vector<double> & Parameters() const { return fParams; }
0162
0163 const double * GetParams() const { return &fParams.front(); }
0164
0165
0166 double Value(unsigned int i) const { return fParams[i]; }
0167
0168 double Parameter(unsigned int i) const { return fParams[i]; }
0169
0170
0171
0172
0173 double Error(unsigned int i) const {
0174 return (i < fErrors.size() ) ? fErrors[i] : 0;
0175 }
0176
0177 double ParError(unsigned int i) const {
0178 return (i < fErrors.size() ) ? fErrors[i] : 0;
0179 }
0180
0181
0182 std::string ParName(unsigned int i) const;
0183
0184
0185 void SetMinosError(unsigned int i, double elow, double eup);
0186
0187
0188
0189
0190
0191 void SetChi2AndNdf(double chi2, unsigned int npoints);
0192
0193
0194 bool HasMinosError(unsigned int i) const;
0195
0196
0197 double LowerError(unsigned int i) const;
0198
0199
0200 double UpperError(unsigned int i) const;
0201
0202
0203 double GlobalCC(unsigned int i) const {
0204 return (i < fGlobalCC.size() ) ? fGlobalCC[i] : -1;
0205 }
0206
0207
0208
0209 double CovMatrix (unsigned int i, unsigned int j) const {
0210 if ( i >= fErrors.size() || j >= fErrors.size() ) return 0;
0211 if (fCovMatrix.empty()) return 0;
0212 if ( j < i )
0213 return fCovMatrix[j + i* (i+1) / 2];
0214 else
0215 return fCovMatrix[i + j* (j+1) / 2];
0216 }
0217
0218
0219 double Correlation(unsigned int i, unsigned int j ) const {
0220 if ( i >= fErrors.size() || j >= fErrors.size() ) return 0;
0221 if (fCovMatrix.empty()) return 0;
0222 double tmp = CovMatrix(i,i)*CovMatrix(j,j);
0223 return ( tmp > 0) ? CovMatrix(i,j)/ std::sqrt(tmp) : 0;
0224 }
0225
0226
0227
0228 template<class Matrix>
0229 void GetCovarianceMatrix(Matrix & mat) const {
0230 unsigned int npar = fErrors.size();
0231 if (fCovMatrix.size() != npar*(npar+1)/2 ) return;
0232 for (unsigned int i = 0; i< npar; ++i) {
0233 for (unsigned int j = 0; j<=i; ++j) {
0234 mat(i,j) = fCovMatrix[j + i*(i+1)/2 ];
0235 if (i != j) mat(j,i) = mat(i,j);
0236 }
0237 }
0238 }
0239
0240
0241
0242 template<class Matrix>
0243 void GetCorrelationMatrix(Matrix & mat) const {
0244 unsigned int npar = fErrors.size();
0245 if (fCovMatrix.size() != npar*(npar+1)/2) return;
0246 for (unsigned int i = 0; i< npar; ++i) {
0247 for (unsigned int j = 0; j<=i; ++j) {
0248 double tmp = fCovMatrix[i * (i +3)/2 ] * fCovMatrix[ j * (j+3)/2 ];
0249 mat(i,j) = (tmp > 0) ? fCovMatrix[j + i*(i+1)/2 ] / std::sqrt(tmp) : 0;
0250 if (i != j) mat(j,i) = mat(i,j);
0251 }
0252 }
0253 }
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268 void GetConfidenceIntervals(unsigned int n, unsigned int stride1, unsigned int stride2, const double * x, double * ci, double cl=0.95, bool norm = false ) const;
0269
0270
0271
0272
0273
0274
0275
0276 void GetConfidenceIntervals(const BinData & data, double * ci, double cl=0.95, bool norm = false ) const;
0277
0278
0279
0280
0281
0282 std::vector<double> GetConfidenceIntervals(double cl=0.95, bool norm = false ) const;
0283
0284
0285
0286
0287 bool Scan(unsigned int ipar, unsigned int &npoints, double *pntsx, double *pntsy, double xmin = 0, double xmax = 0);
0288
0289
0290
0291
0292
0293 bool Contour(unsigned int ipar, unsigned int jpar, unsigned int &npoints, double *pntsx, double *pntsy, double confLevel = 0.683);
0294
0295
0296 int Index(const std::string & name) const;
0297
0298
0299 void NormalizeErrors();
0300
0301
0302 bool NormalizedErrors() const { return fNormalized; }
0303
0304
0305 void Print(std::ostream & os, bool covmat = false) const;
0306
0307
0308 void PrintCovMatrix(std::ostream & os) const;
0309
0310
0311 bool IsParameterBound(unsigned int ipar) const;
0312
0313
0314 bool IsParameterFixed(unsigned int ipar) const;
0315
0316
0317 bool ParameterBounds(unsigned int ipar, double &lower, double &upper) const;
0318
0319
0320
0321 std::string GetParameterName(unsigned int ipar) const {
0322 return ParName(ipar);
0323 }
0324
0325
0326 protected:
0327
0328
0329
0330
0331 std::shared_ptr<IModelFunction> ModelFunction() { return fFitFunc; }
0332 void SetModelFunction(const std::shared_ptr<IModelFunction> & func) { fFitFunc = func; }
0333
0334 friend class Fitter;
0335
0336
0337 bool fValid = false;
0338 bool fNormalized = false;
0339 unsigned int fNFree = 0;
0340 unsigned int fNdf = 0;
0341 unsigned int fNCalls = 0;
0342 int fStatus = -1;
0343 int fCovStatus = 0;
0344 double fVal = 0;
0345 double fEdm = -1;
0346 double fChi2 = -1;
0347 std::shared_ptr<ROOT::Math::Minimizer> fMinimizer;
0348 std::shared_ptr<ROOT::Math::IMultiGenFunction> fObjFunc;
0349 std::shared_ptr<IModelFunction> fFitFunc;
0350 std::shared_ptr<FitData> fFitData;
0351 std::vector<bool> fFixedParams;
0352 std::vector<unsigned int> fBoundParams;
0353 std::vector<std::pair<double,double> > fParamBounds;
0354 std::vector<double> fParams;
0355 std::vector<double> fErrors;
0356 std::vector<double> fCovMatrix;
0357 std::vector<double> fGlobalCC;
0358 std::map<unsigned int, std::pair<double,double> > fMinosErrors;
0359 std::string fMinimType;
0360 std::vector<std::string> fParNames;
0361
0362 };
0363
0364 }
0365
0366 #endif