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