CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/TrackingTools/GsfTracking/src/PosteriorWeightsCalculator.cc

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   // calculate chi2 and determinant / component and find
00045   //   minimum / maximum of chi2
00046   //  
00047   double chi2Min(DBL_MAX);
00048   for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
00049 //     MeasurementExtractor me(predictedComponents[i]);
00050 //     // Residuals of aPredictedState w.r.t. aRecHit, 
00051 //     //!!!     AlgebraicVector r(recHit.parameters(predictedComponents[i]) - me.measuredParameters(recHit));
00052 //     VecD r = asSVector<D>(recHit.parameters()) - me.measuredParameters<D>(recHit);
00053 //     // and covariance matrix of residuals
00054 //     //!!!     AlgebraicSymMatrix V(recHit.parametersError(predictedComponents[i]));
00055 //     SMatDD V = asSMatrix<D>(recHit.parametersError());
00056 //     SMatDD R = V + me.measuredError<D>(recHit);
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(); // if (ierr != 0) throw exception;
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   // calculate weights (extracting a common factor
00091   //   exp(-0.5*chi2Min) to avoid numerical problems
00092   //   during exponentation
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       // Calculation of (non-normalised) weight. Common factors exp(-chi2Norm/2.) and
00104       // 1./sqrt(2*pi*recHit.dimension()) have been omitted
00105       //
00106       tempWeight = priorWeight * sqrt(1./detRs[i]) * exp(-0.5 * chi2); 
00107     }
00108     //      else {
00109     //        edm::LogInfo("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: detR < FLT_MIN !!";
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 }