CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Alignment/CommonAlignmentParametrization/src/CompositeAlignmentParameters.cc

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 #include "Alignment/CommonAlignmentParametrization/interface/CompositeAlignmentParameters.h"
00006 
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 
00009 #include "Alignment/CommonAlignment/interface/Alignable.h"
00010 #include "Alignment/CommonAlignment/interface/AlignableDetOrUnitPtr.h"
00011 #include "Alignment/CommonAlignmentParametrization/interface/CompositeAlignmentDerivativesExtractor.h"
00012 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00013 
00014 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
00015 
00016 
00017 //__________________________________________________________________________________________________
00018 CompositeAlignmentParameters::
00019 CompositeAlignmentParameters(const AlgebraicVector& par, const AlgebraicSymMatrix& cov,
00020                              const Components& comp) :
00021   theData(DataContainer(new AlignmentParametersData(par, cov))),
00022   theComponents(comp) 
00023 {}
00024 
00025 
00026 //__________________________________________________________________________________________________
00027 CompositeAlignmentParameters::
00028 CompositeAlignmentParameters(const AlgebraicVector& par, const AlgebraicSymMatrix& cov,
00029                              const Components& comp, const AlignableDetToAlignableMap& alimap,
00030                              const Aliposmap& aliposmap, const Alilenmap& alilenmap) :
00031   theData(DataContainer(new AlignmentParametersData(par, cov))),
00032   theComponents(comp) ,
00033   theAlignableDetToAlignableMap(alimap),
00034   theAliposmap(aliposmap),
00035   theAlilenmap(alilenmap)
00036 {}
00037 
00038 
00039 //__________________________________________________________________________________________________
00040 CompositeAlignmentParameters::
00041 CompositeAlignmentParameters(const DataContainer& data,
00042                              const Components& comp, const AlignableDetToAlignableMap& alimap,
00043                              const Aliposmap& aliposmap, const Alilenmap& alilenmap) :
00044   theData(data),
00045   theComponents(comp) ,
00046   theAlignableDetToAlignableMap(alimap),
00047   theAliposmap(aliposmap),
00048   theAlilenmap(alilenmap)
00049 {}
00050 
00051 
00052 //__________________________________________________________________________________________________
00053 CompositeAlignmentParameters::~CompositeAlignmentParameters() 
00054 {}
00055 
00056 
00057 //__________________________________________________________________________________________________
00058 CompositeAlignmentParameters* 
00059 CompositeAlignmentParameters::clone( const AlgebraicVector& par, 
00060                                      const AlgebraicSymMatrix& cov) const
00061 {
00062   CompositeAlignmentParameters* cap = 
00063     new CompositeAlignmentParameters(par,cov,components());
00064 
00065   return cap;
00066 }
00067 
00068 //__________________________________________________________________________________________________
00069 CompositeAlignmentParameters* 
00070 CompositeAlignmentParameters::clone( const AlgebraicVector& par, 
00071                                      const AlgebraicSymMatrix& cov,
00072                                      const AlignableDetToAlignableMap& alimap, 
00073                                      const Aliposmap& aliposmap,
00074                                      const Alilenmap& alilenmap ) const
00075 {
00076   CompositeAlignmentParameters* cap = 
00077     new CompositeAlignmentParameters(par,cov,components(),alimap,aliposmap,alilenmap);
00078 
00079   return cap;
00080 }
00081 
00082 //__________________________________________________________________________________________________
00083 CompositeAlignmentParameters::Components 
00084 CompositeAlignmentParameters::components() const
00085 { 
00086   return theComponents;
00087 }
00088 
00089 
00090 //__________________________________________________________________________________________________
00091 // full derivatives for a composed object
00092 AlgebraicMatrix
00093 CompositeAlignmentParameters::derivatives( const std::vector<TrajectoryStateOnSurface>& tsosvec,
00094                                            const std::vector<AlignableDet*>& alidetvec ) const
00095 {
00096   std::vector<AlignableDetOrUnitPtr> detOrUnits;
00097   this->convert(alidetvec, detOrUnits);
00098 
00099   return this->derivatives(tsosvec, detOrUnits);
00100 }
00101 
00102 AlgebraicMatrix
00103 CompositeAlignmentParameters::derivatives( const std::vector<TrajectoryStateOnSurface>& tsosvec,
00104                                            const std::vector<AlignableDetOrUnitPtr>& alidetvec ) const
00105 {
00106   std::vector<Alignable*> alivec;
00107   for (std::vector<AlignableDetOrUnitPtr>::const_iterator it=alidetvec.begin(); it!=alidetvec.end(); ++it)
00108     alivec.push_back(alignableFromAlignableDet(*it));
00109   
00110   CompositeAlignmentDerivativesExtractor extractor(alivec,alidetvec,tsosvec);
00111   return extractor.derivatives();
00112 }
00113 
00114 //__________________________________________________________________________________________________
00115 AlgebraicVector 
00116 CompositeAlignmentParameters::correctionTerm( const std::vector<TrajectoryStateOnSurface>& tsosvec,
00117                                               const std::vector<AlignableDet*>& alidetvec) const
00118 {
00119   std::vector<AlignableDetOrUnitPtr> detOrUnits;
00120   this->convert(alidetvec, detOrUnits);
00121 
00122   return this->correctionTerm(tsosvec, detOrUnits);
00123 }
00124 
00125 //__________________________________________________________________________________________________
00126 AlgebraicVector 
00127 CompositeAlignmentParameters::correctionTerm( const std::vector<TrajectoryStateOnSurface>& tsosvec,
00128                                               const std::vector<AlignableDetOrUnitPtr>& alidetvec) const
00129 {
00130   std::vector<Alignable*> alivec;
00131   for (std::vector<AlignableDetOrUnitPtr>::const_iterator it=alidetvec.begin(); it!=alidetvec.end(); ++it )
00132     alivec.push_back(alignableFromAlignableDet(*it));
00133   
00134   CompositeAlignmentDerivativesExtractor extractor(alivec,alidetvec,tsosvec);
00135   return extractor.correctionTerm();
00136 }
00137          
00138 //__________________________________________________________________________________________________     
00139 // assume all are selected
00140 AlgebraicMatrix
00141 CompositeAlignmentParameters::selectedDerivatives( const std::vector<TrajectoryStateOnSurface>& tsosvec,
00142                                                    const std::vector<AlignableDet*>& alidetvec) const
00143 { 
00144   return derivatives(tsosvec,alidetvec);
00145 }
00146 //__________________________________________________________________________________________________     
00147 // assume all are selected
00148 AlgebraicMatrix
00149 CompositeAlignmentParameters::selectedDerivatives( const std::vector<TrajectoryStateOnSurface>& tsosvec,
00150                                                    const std::vector<AlignableDetOrUnitPtr>& alidetvec) const
00151 { 
00152   return derivatives(tsosvec,alidetvec);
00153 }
00154 
00155 //__________________________________________________________________________________________________     
00156 // only one (tsos,AlignableDet) as argument [for compatibility with base class]
00157 AlgebraicMatrix 
00158 CompositeAlignmentParameters::derivatives( const TrajectoryStateOnSurface &tsos, 
00159                                            const AlignableDetOrUnitPtr &alidet) const
00160 {
00161   std::vector<TrajectoryStateOnSurface> tsosvec;
00162   std::vector<AlignableDetOrUnitPtr> alidetvec;
00163   tsosvec.push_back(tsos);
00164   alidetvec.push_back(alidet);
00165   return derivatives(tsosvec,alidetvec);
00166 }
00167         
00168 //__________________________________________________________________________________________________ 
00169 // assume all are selected
00170 AlgebraicMatrix 
00171 CompositeAlignmentParameters::selectedDerivatives( const TrajectoryStateOnSurface &tsos, 
00172                                                    const AlignableDetOrUnitPtr &alidet ) const
00173 { 
00174   return derivatives(tsos,alidet);
00175 }
00176          
00177 
00178 // Derivatives ----------------------------------------------------------------
00179 // legacy methods
00180 // full derivatives for a composed object
00181 AlgebraicMatrix
00182 CompositeAlignmentParameters::derivativesLegacy( const std::vector<TrajectoryStateOnSurface> &tsosvec, 
00183                                                  const std::vector<AlignableDet*>& alidetvec ) const
00184 {
00185   // sanity check: length of parameter argument vectors must be equal
00186   if (alidetvec.size() != tsosvec.size())
00187   {
00188     edm::LogError("BadArgument") << " Inconsistent length of argument vectors! ";
00189     AlgebraicMatrix selderiv(1,0);
00190     return selderiv;
00191   }
00192 
00193   std::vector<AlgebraicMatrix> vecderiv;
00194   int nparam=0;
00195 
00196   std::vector<TrajectoryStateOnSurface>::const_iterator itsos=tsosvec.begin();
00197   for( std::vector<AlignableDet*>::const_iterator it=alidetvec.begin(); 
00198        it!=alidetvec.end(); ++it, ++itsos ) 
00199   {
00200     AlignableDet* ad = (*it);
00201     Alignable* ali = alignableFromAlignableDet(ad);
00202     AlignmentParameters* ap = ali->alignmentParameters();
00203     AlgebraicMatrix thisselderiv = ap->selectedDerivatives(*itsos,ad);
00204     vecderiv.push_back(thisselderiv);
00205     nparam += thisselderiv.num_row();
00206   }
00207 
00208   int ipos=1;
00209   AlgebraicMatrix selderiv(nparam,2);
00210   for ( std::vector<AlgebraicMatrix>::const_iterator imat=vecderiv.begin();
00211         imat!=vecderiv.end(); ++imat ) 
00212   {
00213     AlgebraicMatrix thisselderiv=(*imat);
00214     int npar=thisselderiv.num_row();
00215     selderiv.sub(ipos,1,thisselderiv);
00216     ipos += npar;
00217   }
00218 
00219   return selderiv;
00220 }
00221 
00222 
00223 //__________________________________________________________________________________________________
00224 // assume all are selected
00225 AlgebraicMatrix
00226 CompositeAlignmentParameters::selectedDerivativesLegacy( const std::vector<TrajectoryStateOnSurface> &tsosvec, 
00227                                                          const std::vector<AlignableDet*>& alidetvec ) const
00228 { 
00229   return derivativesLegacy(tsosvec,alidetvec);
00230 }
00231 
00232 
00233 //__________________________________________________________________________________________________
00234 // only one (tsos,AlignableDet) as argument [for compatibility with base class]
00235 AlgebraicMatrix 
00236 CompositeAlignmentParameters::derivativesLegacy( const TrajectoryStateOnSurface& tsos, 
00237                                                  AlignableDet* alidet ) const
00238 {
00239   std::vector<TrajectoryStateOnSurface> tsosvec;
00240   std::vector<AlignableDet*> alidetvec;
00241   tsosvec.push_back(tsos);
00242   alidetvec.push_back(alidet);
00243   return derivativesLegacy(tsosvec,alidetvec);
00244 
00245 }
00246 
00247 
00248 //__________________________________________________________________________________________________
00249 // assume all are selected
00250 AlgebraicMatrix 
00251 CompositeAlignmentParameters::selectedDerivativesLegacy( const TrajectoryStateOnSurface& tsos, 
00252                                                          AlignableDet* alidet ) const
00253 { 
00254   return derivativesLegacy(tsos,alidet);
00255 }
00256 
00257 
00258 //__________________________________________________________________________________________________
00259 // finds Alignable corresponding to AlignableDet
00260 Alignable* 
00261 CompositeAlignmentParameters::alignableFromAlignableDet(AlignableDetOrUnitPtr adet) const
00262 {
00263 
00264   AlignableDetToAlignableMap::const_iterator iali =
00265     theAlignableDetToAlignableMap.find(adet);
00266   if ( iali!=theAlignableDetToAlignableMap.end() ) return (*iali).second;
00267   else return 0;
00268 
00269 }
00270 
00271 
00272 //__________________________________________________________________________________________________
00273 AlgebraicVector
00274 CompositeAlignmentParameters::parameterSubset( const std::vector<Alignable*>& vec ) const
00275 {
00276   const std::vector< Alignable* > sel = extractAlignables( vec );
00277 
00278   const unsigned int nali = sel.size();
00279   int ndim = 0;
00280 
00281   std::vector<int> posvec;
00282   std::vector<int> lenvec;
00283 
00284   posvec.reserve( nali );
00285   lenvec.reserve( nali );
00286 
00287   // iterate over input vector of alignables to determine size of result vector
00288   if ( !extractPositionAndLength( sel, posvec, lenvec, ndim ) ) return AlgebraicVector();
00289 
00290   // OK, let's do the real work now
00291   AlgebraicVector result( ndim );
00292 
00293   int resi = 0;
00294   for ( unsigned int iali = 0; iali < nali; ++iali )
00295   {
00296     int posi = posvec[iali];
00297     int leni = lenvec[iali];
00298 
00299     for ( int ir = 0; ir < leni; ++ir )
00300       result[resi+ir] = theData->parameters()[posi-1+ir];
00301 
00302     resi += leni;
00303   }
00304 
00305   return result;
00306 }
00307 
00308 
00309 //__________________________________________________________________________________________________
00310 // extract covariance matrix for a subset of alignables
00311 AlgebraicSymMatrix 
00312 CompositeAlignmentParameters::covarianceSubset( const std::vector<Alignable*>& vec ) const
00313 {
00314   const std::vector< Alignable* > sel = extractAlignables( vec );
00315 
00316   const unsigned int nali = sel.size();
00317   int ndim = 0;
00318 
00319   std::vector<int> posvec;
00320   std::vector<int> lenvec;
00321 
00322   posvec.reserve( nali );
00323   lenvec.reserve( nali );
00324 
00325   // iterate over input vectors of alignables
00326   // to determine dimensions of result matrix
00327   if ( !extractPositionAndLength( sel, posvec, lenvec, ndim ) ) return AlgebraicSymMatrix();
00328 
00329   // OK, let's do the real work now
00330   AlgebraicSymMatrix result( ndim );
00331 
00332   int resi = 0;
00333   for ( unsigned int iali = 0; iali < nali; ++iali )
00334   {
00335     int posi = posvec[iali];
00336     int leni = lenvec[iali];
00337 
00338     int resj = 0;
00339     for ( unsigned int jali = 0; jali <= iali; ++jali )
00340     {   
00341       int posj = posvec[jali];
00342       int lenj = lenvec[jali];
00343 
00344       for ( int ir = 0; ir < leni; ++ir )
00345         for ( int ic = 0; ic < lenj; ++ic )
00346           result[resi+ir][resj+ic] = theData->covariance()[posi-1+ir][posj-1+ic];
00347 
00348       resj += lenj;
00349     }
00350     resi += leni;
00351   }
00352 
00353   return result;
00354 }
00355 
00356 
00357 //__________________________________________________________________________________________________
00358 // extract covariance matrix elements between two subsets of alignables
00359 AlgebraicMatrix 
00360 CompositeAlignmentParameters::covarianceSubset( const std::vector<Alignable*>& veci, 
00361                                                 const std::vector<Alignable*>& vecj ) const
00362 {
00363   const std::vector< Alignable* > seli = extractAlignables( veci );
00364   const std::vector< Alignable* > selj = extractAlignables( vecj );
00365 
00366   int ndimi=0;
00367   int ndimj=0;
00368 
00369   std::vector<int> posveci;
00370   std::vector<int> lenveci;
00371   std::vector<int> posvecj;
00372   std::vector<int> lenvecj;
00373 
00374   posveci.reserve( seli.size() );
00375   lenveci.reserve( seli.size() );
00376   posvecj.reserve( selj.size() );
00377   lenvecj.reserve( selj.size() );
00378 
00379   // iterate over input vectors of alignables
00380   // to determine dimensions of result matrix
00381   if ( !extractPositionAndLength( seli, posveci, lenveci, ndimi ) ) return AlgebraicSymMatrix();
00382   // vector vecj
00383   if ( !extractPositionAndLength( selj, posvecj, lenvecj, ndimj ) ) return AlgebraicSymMatrix();
00384 
00385   // OK, let's do the real work now
00386   AlgebraicMatrix result( ndimi, ndimj );
00387 
00388   int resi = 0;
00389   for ( unsigned int iali = 0; iali < seli.size(); ++iali )
00390   {
00391     int posi = posveci[iali];
00392     int leni = lenveci[iali];
00393 
00394     int resj = 0;
00395     for ( unsigned int jali = 0; jali < selj.size(); ++jali )
00396     {   
00397       int posj = posvecj[jali];
00398       int lenj = lenvecj[jali];
00399 
00400       for ( int ir = 0; ir < leni; ++ir )
00401         for ( int ic = 0; ic < lenj; ++ic )
00402           result[resi+ir][resj+ic] = theData->covariance()[posi-1+ir][posj-1+ic];
00403 
00404       resj += lenj;
00405     }
00406     resi += leni;
00407   }
00408 
00409   return result;
00410 }
00411 
00412 
00413 //__________________________________________________________________________________________________
00414 // Extract position and length of parameters for a subset of Alignables.
00415 bool
00416 CompositeAlignmentParameters::extractPositionAndLength( const std::vector<Alignable*>& alignables,
00417                                                         std::vector<int>& posvec,
00418                                                         std::vector<int>& lenvec,
00419                                                         int& length ) const
00420 {
00421   length = 0;
00422 
00423   for ( std::vector<Alignable*>::const_iterator it = alignables.begin(); it != alignables.end(); ++it ) 
00424   {
00425     // check if in components 
00426     if ( std::find( theComponents.begin(), theComponents.end(), *it ) == theComponents.end() ) 
00427     {
00428       edm::LogError( "NotFound" ) << "@SUB=CompositeAlignmentParameters::extractPositionAndLength"
00429                                   << "Alignable not found in components!";
00430       return false;
00431     }
00432 
00433     // get pos/length
00434     Aliposmap::const_iterator iposmap = theAliposmap.find( *it );
00435     Alilenmap::const_iterator ilenmap = theAlilenmap.find( *it );
00436     if ( iposmap == theAliposmap.end() || ilenmap == theAlilenmap.end() ) 
00437     {
00438       edm::LogError( "NotFound" ) << "@SUB=CompositeAlignmentParameters::extractPositionAndLength"
00439                                   << "position/length not found for Alignable in maps!";
00440       return false;
00441     }
00442     posvec.push_back( iposmap->second );
00443     lenvec.push_back( ilenmap->second );
00444     length += ilenmap->second;
00445   }
00446 
00447   return true;
00448 }
00449 
00450 
00451 //__________________________________________________________________________________________________
00452 std::vector< Alignable* >
00453 CompositeAlignmentParameters::extractAlignables( const std::vector< Alignable* >& alignables ) const
00454 {
00455   std::vector< Alignable* > result;
00456 
00457   std::vector< Alignable* >::const_iterator itA, itEnd;
00458   for ( itA = alignables.begin(), itEnd = alignables.end(); itA != itEnd; ++itA )
00459   {
00460     if ( std::find( result.begin(), result.end(), *itA ) == result.end() ) result.push_back( *itA );
00461   }
00462 
00463   return result;
00464 }
00465 
00466 
00467 //__________________________________________________________________________________________________
00468 void CompositeAlignmentParameters::convert(const std::vector<AlignableDet*> &input,
00469                                            std::vector<AlignableDetOrUnitPtr> &output) const
00470 {
00471   output.clear();
00472   output.reserve(input.size());
00473 
00474   std::vector<AlignableDet*>::const_iterator it, itEnd;
00475   for (it = input.begin(), itEnd = input.end(); it != itEnd; ++it)
00476     output.push_back(AlignableDetOrUnitPtr(*it));
00477 }