CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/TrackingTools/PatternTools/src/CollinearFitAtTM.cc

Go to the documentation of this file.
00001 #include "TrackingTools/PatternTools/interface/CollinearFitAtTM.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 
00004 CollinearFitAtTM2::CollinearFitAtTM2 (const TrajectoryMeasurement& tm) : 
00005   valid_(false),chi2_(-1),ndof_(-1) {
00006   //
00007   // check input
00008   //
00009   if ( !tm.forwardPredictedState().isValid() ||
00010        !tm.backwardPredictedState().isValid() ||
00011        !tm.updatedState().isValid() ) {
00012     edm::LogWarning("CollinearFitAtTM2") << "Invalid state in TrajectoryMeasurement";
00013     return;
00014   }
00015   //
00016   // prepare fit
00017   //
00018   initJacobian();
00019   AlgebraicVector5 fwdPar = tm.forwardPredictedState().localParameters().vector();
00020   AlgebraicSymMatrix55 fwdCov = tm.forwardPredictedState().localError().matrix();
00021   AlgebraicVector5 bwdPar = tm.backwardPredictedState().localParameters().vector();
00022   AlgebraicSymMatrix55 bwdCov = tm.backwardPredictedState().localError().matrix();
00023 
00024   LocalPoint hitPos(0.,0.,0.);
00025   LocalError hitErr(-1.,-1.,-1.);
00026   if ( tm.recHit()->isValid() ) {
00027     hitPos = tm.recHit()->localPosition();
00028     hitErr = tm.recHit()->localPositionError();
00029   }
00030 
00031   fit(fwdPar,fwdCov,bwdPar,bwdCov,hitPos,hitErr);
00032 }
00033 
00034 CollinearFitAtTM2::CollinearFitAtTM2 (const AlgebraicVector5& fwdParameters, 
00035                                     const AlgebraicSymMatrix55& fwdCovariance,
00036                                     const AlgebraicVector5& bwdParameters, 
00037                                     const AlgebraicSymMatrix55& bwdCovariance,
00038                                     const LocalPoint& hitPosition, 
00039                                     const LocalError& hitErrors) : 
00040   valid_(false),chi2_(-1),ndof_(-1) {
00041   //
00042   // prepare fit
00043   //
00044   initJacobian();
00045 
00046   fit(fwdParameters,fwdCovariance,bwdParameters,bwdCovariance,hitPosition,hitErrors);
00047 }
00048 
00049 void
00050 CollinearFitAtTM2::initJacobian ()
00051 {
00052   //
00053   // Jacobian
00054   //
00055   for ( int i=0; i<12; ++i ) {
00056     for ( int j=0; j<6; ++j )  jacobian_(i,j) = 0;
00057   }
00058   for ( int i=1; i<5; ++i ) {
00059     jacobian_(i,ParQpOut+i) = jacobian_(i+5,ParQpOut+i) = 1;
00060   }
00061   jacobian_(0,ParQpIn) = 1.;
00062   jacobian_(5,ParQpOut) = 1.;
00063 }
00064 
00065 bool
00066 CollinearFitAtTM2::fit (const AlgebraicVector5& fwdParameters, 
00067                        const AlgebraicSymMatrix55& fwdCovariance,
00068                        const AlgebraicVector5& bwdParameters, 
00069                        const AlgebraicSymMatrix55& bwdCovariance,
00070                        const LocalPoint& hitPos, const LocalError& hitErr)
00071 {
00072 
00073   if ( hitErr.xx()>0 )
00074     jacobian_(10,ParX) = jacobian_(11,ParY) = 1;
00075   else
00076     jacobian_(10,ParX) = jacobian_(11,ParY) = 0;
00077 
00078   for ( int i=0; i<12; ++i ) {
00079     for ( int j=0; j<12; ++j )  weightMatrix_(i,j) = 0;
00080   }
00081 
00082   for ( int i=0; i<5; ++i )  measurements_(i) = fwdParameters(i);
00083   weightMatrix_.Place_at(fwdCovariance,0,0);
00084   for ( int i=0; i<5; ++i )  measurements_(i+5) = bwdParameters(i);
00085   weightMatrix_.Place_at(bwdCovariance,5,5);
00086   if ( hitErr.xx()>0 ) {
00087     measurements_(10) = hitPos.x();
00088     measurements_(11) = hitPos.y();
00089     weightMatrix_(10,10) = hitErr.xx();
00090     weightMatrix_(10,11) = weightMatrix_(11,10) = hitErr.xy();
00091     weightMatrix_(11,11) = hitErr.yy();
00092   }
00093   else {
00094     measurements_(10) = measurements_(11) = 0.;
00095     weightMatrix_(10,10) = weightMatrix_(11,11) = 1.;
00096     weightMatrix_(10,11) = weightMatrix_(11,10) = 0.;
00097   }
00098   //
00099   // invert covariance matrix
00100   //
00101   if ( !weightMatrix_.Invert() ) {
00102     edm::LogWarning("CollinearFitAtTM2") << "Inversion of input covariance matrix failed";
00103     return false;
00104   }
00105 
00106   //
00107   projectedMeasurements_ = ROOT::Math::Transpose(jacobian_)*(weightMatrix_*measurements_);
00108   //
00109   // Fitted parameters and covariance matrix
00110   // 
00111   covariance_ = ROOT::Math::SimilarityT(jacobian_,weightMatrix_);
00112   if ( !covariance_.Invert() ) {
00113     edm::LogWarning("CollinearFitAtTM2") << "Inversion of resulting weight matrix failed";
00114     return false;
00115   }
00116 
00117   parameters_ = covariance_*projectedMeasurements_;
00118 
00119   //
00120   // chi2
00121   //
00122   chi2_ = ROOT::Math::Similarity(measurements_,weightMatrix_) -
00123     ROOT::Math::Similarity(projectedMeasurements_,covariance_);
00124   ndof_ =  hitErr.xx()>0 ? 6 : 4;
00125 
00126   valid_ = true;
00127   return true;
00128 }
00129 
00130 Measurement1D
00131 CollinearFitAtTM2::deltaP () const {
00132   //
00133   // check validity
00134   //
00135   if ( !valid_ )  return Measurement1D();
00136   //
00137   // deltaP = 1/qpout - 1/qpin ; uncertainty from linear error propagation
00138   //
00139   double qpIn = parameters_(0);
00140   double sig2In = covariance_(0,0);
00141   double qpOut = parameters_(1);
00142   double sig2Out = covariance_(1,1);
00143   double corrInOut = covariance_(0,1);
00144   double pIn = 1./fabs(qpIn);
00145   double pOut = 1./fabs(qpOut);
00146   double sig2DeltaP = pIn/qpIn*pIn/qpIn*sig2In - 2*pIn/qpIn*pOut/qpOut*corrInOut + 
00147     pOut/qpOut*pOut/qpOut*sig2Out;
00148 
00149   return Measurement1D(pOut-pIn,sig2DeltaP?sqrt(sig2DeltaP):0.);
00150 }
00151