From 28aff209c4e399ce86e8a4802d828c5d520e86fb Mon Sep 17 00:00:00 2001 From: AndrewYongZhenNing Date: Wed, 22 Sep 2021 12:38:54 +0100 Subject: [PATCH] New function fitSample to make XYSampleData::fit more modular: Eg application: In fit scans, one may only want central fit for the uncorr fit to speed up process. --- lib/Statistics/XYSampleData.cpp | 69 +++++++++++++++++++++------------ lib/Statistics/XYSampleData.hpp | 6 ++- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/lib/Statistics/XYSampleData.cpp b/lib/Statistics/XYSampleData.cpp index bdb4f19..0bad9ce 100644 --- a/lib/Statistics/XYSampleData.cpp +++ b/lib/Statistics/XYSampleData.cpp @@ -294,9 +294,40 @@ const XYStatData & XYSampleData::getData(void) } // fit ///////////////////////////////////////////////////////////////////////// + +void XYSampleData::fitSample(std::vector &minimizer, + const std::vector &v, + SampleFitResult &result, + FitResult &sampleResult, + DVec &init, + Index s) +{ + double pValue = 0; + setDataToSample(s); + if (s == central) + { + sampleResult = data_.fit(minimizer, init, v); + init = sampleResult.segment(0, init.size()); + pValue = sampleResult.getPValue(); + checkPValue(pValue); + } + else + { + sampleResult = data_.fit(*(minimizer.back()), init, v); + } + result[s] = sampleResult; + result.chi2_[s] = sampleResult.getChi2(); + for (unsigned int j = 0; j < v.size(); ++j) + { + result.model_[j].resize(nSample_); + result.model_[j][s] = sampleResult.getModel(j); + } +} + SampleFitResult XYSampleData::fit(std::vector &minimizer, const DVec &init, - const std::vector &v) + const std::vector &v, + bool centralSample) { computeVarMat(); @@ -307,29 +338,16 @@ SampleFitResult XYSampleData::fit(std::vector &minimizer, result.resize(nSample_); result.chi2_.resize(nSample_); result.model_.resize(v.size()); - double chi2PerDof; - FOR_STAT_ARRAY(result, s) + if(centralSample) { - setDataToSample(s); - if (s == central) - { - sampleResult = data_.fit(minimizer, initCopy, v); - initCopy = sampleResult.segment(0, initCopy.size()); - chi2PerDof = sampleResult.getChi2PerDof(); - checkChi2PerDof(chi2PerDof); - } - else - { - sampleResult = data_.fit(*(minimizer.back()), initCopy, v); - chi2PerDof = sampleResult.getChi2PerDof(); - } - result[s] = sampleResult; - result.chi2_[s] = sampleResult.getChi2(); - for (unsigned int j = 0; j < v.size(); ++j) - { - result.model_[j].resize(nSample_); - result.model_[j][s] = sampleResult.getModel(j); - } + fitSample(minimizer, v, result, sampleResult, initCopy, central); + } + else + { + FOR_STAT_ARRAY(result, s) + { + fitSample(minimizer, v, result, sampleResult, initCopy, s); + } } result.nPar_ = sampleResult.getNPar(); result.nDof_ = sampleResult.nDof_; @@ -340,11 +358,12 @@ SampleFitResult XYSampleData::fit(std::vector &minimizer, SampleFitResult XYSampleData::fit(Minimizer &minimizer, const DVec &init, - const std::vector &v) + const std::vector &v, + bool centralSample) { vector mv{&minimizer}; - return fit(mv, init, v); + return fit(mv, init, v, centralSample); } // residuals /////////////////////////////////////////////////////////////////// diff --git a/lib/Statistics/XYSampleData.hpp b/lib/Statistics/XYSampleData.hpp index f6bcb5f..ac3a464 100644 --- a/lib/Statistics/XYSampleData.hpp +++ b/lib/Statistics/XYSampleData.hpp @@ -102,10 +102,12 @@ public: // get internal XYStatData const XYStatData & getData(void); // fit + void fitSample(std::vector &minimizer, const std::vector &v, + SampleFitResult &result, FitResult &sampleResult, DVec &init, Index s); SampleFitResult fit(std::vector &minimizer, const DVec &init, - const std::vector &v); + const std::vector &v, bool centralSample = false); SampleFitResult fit(Minimizer &minimizer, const DVec &init, - const std::vector &v); + const std::vector &v, bool centralSample = false); template SampleFitResult fit(std::vector &minimizer, const DVec &init, const DoubleModel &model, const Ts... models);