Go to the documentation of this file.00001 #include "TrackingTools/GsfTracking/interface/PosteriorWeightsCalculator.h"
00002
00003 #include "TrackingTools/PatternTools/interface/MeasurementExtractor.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include "DataFormats/TrackingRecHit/interface/KfComponentsHolder.h"
00006
00007 #include <cfloat>
00008
00009 std::vector<double> PosteriorWeightsCalculator::weights(const TransientTrackingRecHit& recHit) const {
00010 switch (recHit.dimension()) {
00011 case 1: return weights<1>(recHit);
00012 case 2: return weights<2>(recHit);
00013 case 3: return weights<3>(recHit);
00014 case 4: return weights<4>(recHit);
00015 case 5: return weights<5>(recHit);
00016 }
00017 throw cms::Exception("Error: rechit of size not 1,2,3,4,5");
00018 }
00019
00020 template<unsigned int D>
00021 std::vector<double> PosteriorWeightsCalculator::weights(const TransientTrackingRecHit& recHit) const {
00022 typedef typename AlgebraicROOTObject<D,5>::Matrix MatD5;
00023 typedef typename AlgebraicROOTObject<5,D>::Matrix Mat5D;
00024 typedef typename AlgebraicROOTObject<D,D>::SymMatrix SMatDD;
00025 typedef typename AlgebraicROOTObject<D>::Vector VecD;
00026
00027 std::vector<double> weights;
00028 if ( predictedComponents.empty() ) {
00029 edm::LogError("EmptyPredictedComponents")<<"a multi state is empty. cannot compute any weight.";
00030 return weights;
00031 }
00032 weights.reserve(predictedComponents.size());
00033
00034 std::vector<double> detRs;
00035 detRs.reserve(predictedComponents.size());
00036 std::vector<double> chi2s;
00037 chi2s.reserve(predictedComponents.size());
00038
00039 MatD5 H;
00040 VecD r, rMeas;
00041 SMatDD V, R;
00042 AlgebraicVector5 x;
00043
00044
00045
00046
00047 double chi2Min(DBL_MAX);
00048 for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 KfComponentsHolder holder;
00059 x = predictedComponents[i].localParameters().vector();
00060 holder.template setup<D>(&r, &V, &H, &rMeas, &R,
00061 x, predictedComponents[i].localError().matrix());
00062 recHit.getKfComponents(holder);
00063
00064 r -= rMeas;
00065 R += V;
00066
00067 double detR;
00068 if (! R.Det2(detR) ) {
00069 edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: determinant failed";
00070 return std::vector<double>();
00071 }
00072 detRs.push_back(detR);
00073
00074 int ierr = ! R.Invert();
00075 if ( ierr!=0 ) {
00076 edm::LogError("PosteriorWeightsCalculator")
00077 << "PosteriorWeightsCalculator: inversion failed, ierr = " << ierr;
00078 return std::vector<double>();
00079 }
00080 double chi2 = ROOT::Math::Similarity(r,R);
00081 chi2s.push_back(chi2);
00082 if ( chi2<chi2Min ) chi2Min = chi2;
00083 }
00084 if ( detRs.size()!=predictedComponents.size() ||
00085 chi2s.size()!=predictedComponents.size() ) {
00086 edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes";
00087 return std::vector<double>();
00088 }
00089
00090
00091
00092
00093
00094 double sumWeights(0.);
00095 for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
00096 double priorWeight = predictedComponents[i].weight();
00097
00098 double chi2 = chi2s[i] - chi2Min;
00099
00100 double tempWeight(0.);
00101 if ( detRs[i]>FLT_MIN ) {
00102
00103
00104
00105
00106 tempWeight = priorWeight * sqrt(1./detRs[i]) * exp(-0.5 * chi2);
00107 }
00108
00109
00110
00111 weights.push_back(tempWeight);
00112 sumWeights += tempWeight;
00113 }
00114 if ( sumWeights<DBL_MIN ) {
00115 edm::LogInfo("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
00116 edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
00117 return std::vector<double>();
00118 }
00119
00120 if ( weights.size()!=predictedComponents.size() ) {
00121 edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes (2)";
00122 return std::vector<double>();
00123 }
00124 for (std::vector<double>::iterator iter = weights.begin();
00125 iter != weights.end(); iter++) {
00126 (*iter) /= sumWeights;
00127 }
00128
00129 return weights;
00130 }