File indexing completed on 2024-04-06 12:31:30
0001 #include "TrackingTools/GsfTools/interface/MultiGaussianStateCombiner1D.h"
0002
0003 #include "FWCore/Utilities/interface/Exception.h"
0004
0005 #include <cfloat>
0006
0007 SingleGaussianState1D MultiGaussianStateCombiner1D::combine(const MultiGaussianState1D& theState) const {
0008 return combine(theState.components());
0009 }
0010
0011 SingleGaussianState1D MultiGaussianStateCombiner1D::combine(const VSC& theComponents) const {
0012 if (theComponents.empty()) {
0013 throw cms::Exception("LogicError") << "MultiGaussianStateCombiner1D:: state container to collapse is empty";
0014
0015
0016
0017 return SingleGaussianState1D();
0018 }
0019
0020 const SingleGaussianState1D& firstState(theComponents.front());
0021 if (theComponents.size() == 1)
0022 return firstState;
0023
0024
0025 double meanMean(0.);
0026 double weightSum(0.);
0027
0028 double measCovar1(0.);
0029 double measCovar2(0.);
0030 for (VSC::const_iterator mixtureIter1 = theComponents.begin(); mixtureIter1 != theComponents.end(); mixtureIter1++) {
0031 double weight = mixtureIter1->weight();
0032 weightSum += weight;
0033
0034 double mean1 = mixtureIter1->mean();
0035 meanMean += weight * mean1;
0036 measCovar1 += weight * mixtureIter1->variance();
0037
0038 for (VSC::const_iterator mixtureIter2 = mixtureIter1 + 1; mixtureIter2 != theComponents.end(); mixtureIter2++) {
0039 double posDiff = mean1 - mixtureIter2->mean();
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 double covGen = posDiff * posDiff;
0051
0052 measCovar2 += weight * mixtureIter2->weight() * covGen;
0053 }
0054 }
0055
0056 double measCovar;
0057 if (weightSum < DBL_MIN) {
0058 std::cout << "MultiGaussianStateCombiner1D:: New state has total weight of 0." << std::endl;
0059
0060 meanMean = 0.;
0061 measCovar = 0.;
0062 weightSum = 0.;
0063 } else {
0064 weightSum = 1. / weightSum;
0065 meanMean *= weightSum;
0066 measCovar1 *= weightSum;
0067 measCovar2 *= weightSum * weightSum;
0068 measCovar = measCovar1 + measCovar2;
0069
0070 }
0071
0072 return SingleGaussianState1D(meanMean, measCovar, weightSum);
0073 }