CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Alignment/KalmanAlignmentAlgorithm/src/CurrentAlignmentKFUpdator.cc

Go to the documentation of this file.
00001 #include "Alignment/KalmanAlignmentAlgorithm/interface/CurrentAlignmentKFUpdator.h"
00002 
00003 #include "Alignment/CommonAlignment/interface/Alignable.h"
00004 #include "Alignment/CommonAlignment/interface/AlignableDet.h"
00005 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
00006 
00007 #include "TrackingTools/PatternTools/interface/MeasurementExtractor.h"
00008 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"
00009 
00010 
00011 TrajectoryStateOnSurface CurrentAlignmentKFUpdator::update( const TrajectoryStateOnSurface & tsos,
00012                                                             const TransientTrackingRecHit & aRecHit ) const 
00013 {
00014     switch (aRecHit.dimension()) {
00015         case 1: return update<1>(tsos,aRecHit);
00016         case 2: return update<2>(tsos,aRecHit);
00017         case 3: return update<3>(tsos,aRecHit);
00018         case 4: return update<4>(tsos,aRecHit);
00019         case 5: return update<5>(tsos,aRecHit);
00020     }
00021     throw cms::Exception("Rec hit of invalid dimension (not 1,2,3,4,5)");
00022 }
00023 
00024 
00025 template <unsigned int D>
00026 TrajectoryStateOnSurface CurrentAlignmentKFUpdator::update( const TrajectoryStateOnSurface & tsos,
00027                                                             const TransientTrackingRecHit & aRecHit ) const
00028 {
00029   //std::cout << "[CurrentAlignmentKFUpdator::update] Start Updating." << std::endl;
00030   typedef typename AlgebraicROOTObject<D,5>::Matrix MatD5;
00031   typedef typename AlgebraicROOTObject<5,D>::Matrix Mat5D;
00032   typedef typename AlgebraicROOTObject<D,D>::SymMatrix SMatDD;
00033   typedef typename AlgebraicROOTObject<D>::Vector VecD;
00034 
00035   double pzSign = tsos.localParameters().pzSign();
00036 
00037   MeasurementExtractor me( tsos );
00038 
00039   AlgebraicVector5 vecX( tsos.localParameters().vector() );
00040   AlgebraicSymMatrix55 matC( tsos.localError().matrix() );
00041   // Measurement matrix
00042   MatD5 matH = asSMatrix<D,5>( aRecHit.projectionMatrix() );
00043 
00044   // Residuals of aPredictedState w.r.t. aRecHit, 
00045   VecD vecR = asSVector<D>(aRecHit.parameters()) - me.measuredParameters<D>( aRecHit );
00046 
00047   // and covariance matrix of residuals
00048   SMatDD matV = asSMatrix<D>( aRecHit.parametersError() );
00049 
00050   // add information from current estimate on the misalignment
00051   includeCurrentAlignmentEstimate<D>( aRecHit, tsos, vecR, matV );
00052 
00053    SMatDD  matR( matV + me.measuredError<D>( aRecHit ) );
00054 
00055   int checkInversion = 0;
00056   SMatDD invR = matR.Inverse( checkInversion );
00057   if ( checkInversion != 0 )
00058   {
00059     std::cout << "[CurrentAlignmentKFUpdator::update] Inversion of matrix R failed." << std::endl;
00060     return TrajectoryStateOnSurface();
00061   }
00062 
00063   // Compute Kalman gain matrix
00064   Mat5D matK =  matC*ROOT::Math::Transpose(matH)*invR ;
00065 
00066   // Compute local filtered state vector
00067   AlgebraicVector5 fsv( vecX + matK*vecR );
00068 
00069   // Compute covariance matrix of local filtered state vector
00070   AlgebraicSymMatrix55 matI  = AlgebraicMatrixID();
00071   AlgebraicMatrix55 matM( matI - matK*matH );
00072   AlgebraicSymMatrix55 fse( ROOT::Math::Similarity(matM, matC) + ROOT::Math::Similarity(matK, matV) );
00073 
00074   return TrajectoryStateOnSurface( LocalTrajectoryParameters( fsv, pzSign ), LocalTrajectoryError( fse ),
00075                                    tsos.surface(),&( tsos.globalParameters().magneticField() ) );
00076 }
00077 
00078 
00079 template <unsigned int D>
00080 void CurrentAlignmentKFUpdator::includeCurrentAlignmentEstimate( const TransientTrackingRecHit & aRecHit,
00081                                                                  const TrajectoryStateOnSurface & tsos,
00082                                                                  typename AlgebraicROOTObject<D>::Vector & vecR,
00083                                                                  typename AlgebraicROOTObject<D>::SymMatrix & matV ) const
00084 {
00085   const GeomDet* det = aRecHit.det();
00086   if ( !det ) return;
00087 
00088   AlignableDetOrUnitPtr alignableDet = theAlignableNavigator->alignableFromGeomDet( det );
00089   if ( alignableDet.isNull() )
00090   {
00091     //std::cout << "[CurrentAlignmentKFUpdator::includeCurrentAlignmentEstimate] No AlignableDet associated with RecHit." << std::endl;
00092     return;
00093   }
00094 
00095   AlignmentParameters* alignmentParameters = getAlignmentParameters( alignableDet );
00096 
00097   if ( alignmentParameters )
00098   {
00099     AlgebraicMatrix selectedDerivatives = alignmentParameters->selectedDerivatives( tsos, alignableDet );
00100     AlgebraicVector selectedParameters = alignmentParameters->selectedParameters();
00101     AlgebraicSymMatrix selectedCovariance = alignmentParameters->selectedCovariance();
00102 
00103     AlgebraicSymMatrix deltaV = selectedCovariance.similarityT( selectedDerivatives );
00104     AlgebraicVector deltaR = selectedDerivatives.T()*selectedParameters;
00105 
00106     //AlignmentUserVariables* auv = alignmentParameters->userVariables();
00107     //if ( !auv ) std::cout << "[CurrentAlignmentKFUpdator::includeCurrentAlignmentEstimate] No AlignmentUserVariables associated with AlignableDet." << std::endl;
00108     //if ( theAnnealing ) matV *= (*theAnnealing)( auv );
00109 
00110     if ( deltaR.num_row() == D )
00111     {
00112       vecR += asSVector<D>(deltaR);
00113       matV += asSMatrix<D>(deltaV);
00114     }
00115     else std::cout << "[CurrentAlignmentKFUpdator::includeCurrentAlignmentEstimate] Predicted state and misalignment correction not compatible." << std::endl;
00116   } else std::cout << "[CurrentAlignmentKFUpdator::includeCurrentAlignmentEstimate] No AlignmentParameters associated with AlignableDet." << std::endl;
00117 
00118   return;
00119 }
00120 
00121 
00122 AlignmentParameters* CurrentAlignmentKFUpdator::getAlignmentParameters( const AlignableDetOrUnitPtr alignableDet ) const
00123 {
00124   // Get alignment parameters from AlignableDet ...
00125   AlignmentParameters* alignmentParameters = alignableDet->alignmentParameters();
00126   // ... or any higher level alignable.
00127   if ( !alignmentParameters ) alignmentParameters = getHigherLevelParameters( alignableDet );
00128   return alignmentParameters;
00129 }
00130 
00131 
00132 AlignmentParameters* CurrentAlignmentKFUpdator::getHigherLevelParameters( const Alignable* aAlignable ) const
00133 {
00134   Alignable* higherLevelAlignable = aAlignable->mother();
00135   // Alignable has no mother ... most probably the alignable is already the full tracker.
00136   if ( !higherLevelAlignable ) return 0;
00137   AlignmentParameters* higherLevelParameters = higherLevelAlignable->alignmentParameters();
00138   // Found alignment parameters? If not, go one level higher in the hierarchy.
00139   return higherLevelParameters ? higherLevelParameters : getHigherLevelParameters( higherLevelAlignable );
00140 }