CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/Alignment/CommonAlignmentAlgorithm/src/AlignmentCorrelationsStore.cc

Go to the documentation of this file.
00001 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentCorrelationsStore.h"
00002 
00003 #include "Alignment/CommonAlignment/interface/Alignable.h"
00004 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 
00007 
00008 AlignmentCorrelationsStore::AlignmentCorrelationsStore( void )
00009 {
00010   edm::LogInfo("Alignment") << "@SUB=AlignmentCorrelationsStore::AlignmentCorrelationsStore "
00011                             << "\nCreated.";
00012 }
00013 
00014 
00015 void AlignmentCorrelationsStore::correlations( Alignable* ap1, Alignable* ap2,
00016                                                AlgebraicSymMatrix& cov, int row, int col ) const
00017 {
00018   static Alignable* previousAlignable = 0;
00019   static CorrelationsTable* previousCorrelations;
00020 
00021   // Needed by 'resetCorrelations()' to reset the static pointer:
00022   if ( ap1 == 0 ) { previousAlignable = 0; return; }
00023 
00024   bool transpose = ( ap2 > ap1 );
00025   if ( transpose ) std::swap( ap1, ap2 ); 
00026 
00027   if ( ap1 == previousAlignable )
00028   {
00029     CorrelationsTable::const_iterator itC2 = previousCorrelations->find( ap2 );
00030     if ( itC2 != previousCorrelations->end() )
00031     {
00032       transpose ?
00033         fillCovarianceT( ap1, ap2, (*itC2).second, cov, row, col ) :
00034         fillCovariance( ap1, ap2, (*itC2).second, cov, row, col );
00035     }
00036   }
00037   else
00038   {
00039     Correlations::const_iterator itC1 = theCorrelations.find( ap1 );
00040     if ( itC1 != theCorrelations.end() )
00041     {
00042       previousAlignable = ap1;
00043       previousCorrelations = (*itC1).second;
00044 
00045       CorrelationsTable::const_iterator itC2 = (*itC1).second->find( ap2 );
00046       if ( itC2 != (*itC1).second->end() )
00047       {
00048         transpose ?
00049           fillCovarianceT( ap1, ap2, (*itC2).second, cov, row, col ) :
00050           fillCovariance( ap1, ap2, (*itC2).second, cov, row, col );
00051       }
00052     }
00053   }
00054 
00055   // don't fill anything into the covariance if there's no entry
00056   return;
00057 }
00058 
00059 
00060 void AlignmentCorrelationsStore::setCorrelations( Alignable* ap1, Alignable* ap2,
00061                                                   const AlgebraicSymMatrix& cov, int row, int col )
00062 {
00063   static Alignable* previousAlignable = 0;
00064   static CorrelationsTable* previousCorrelations;
00065 
00066   // Needed by 'resetCorrelations()' to reset the static pointer:
00067   if ( ap1 == 0 ) { previousAlignable = 0; return; }
00068 
00069   bool transpose = ( ap2 > ap1 );
00070   if ( transpose ) std::swap( ap1, ap2 );
00071 
00072   if ( ap1 == previousAlignable )
00073   {
00074     fillCorrelationsTable( ap1, ap2, previousCorrelations, cov, row, col, transpose );
00075   }
00076   else
00077   {
00078     Correlations::iterator itC = theCorrelations.find( ap1 );
00079     if ( itC != theCorrelations.end() )
00080     {
00081       fillCorrelationsTable( ap1, ap2, itC->second, cov, row, col, transpose );
00082       previousAlignable = ap1;
00083       previousCorrelations = itC->second;
00084     }
00085     else
00086     {
00087       CorrelationsTable* newTable = new CorrelationsTable;
00088       fillCorrelationsTable( ap1, ap2, newTable, cov, row, col, transpose );
00089 
00090       theCorrelations[ap1] = newTable;
00091       previousAlignable = ap1;
00092       previousCorrelations = newTable;
00093     }
00094   }
00095 }
00096 
00097 
00098 void AlignmentCorrelationsStore::setCorrelations( Alignable* ap1, Alignable* ap2, AlgebraicMatrix& mat )
00099 {
00100   bool transpose = ( ap2 > ap1 );
00101   if ( transpose ) std::swap( ap1, ap2 );
00102 
00103   Correlations::iterator itC1 = theCorrelations.find( ap1 );
00104   if ( itC1 != theCorrelations.end() )
00105   {
00106     (*itC1->second)[ap2] = transpose ? mat.T() : mat;
00107   }
00108   else
00109   {
00110     CorrelationsTable* newTable = new CorrelationsTable;
00111     (*newTable)[ap2] = transpose ? mat.T() : mat;
00112     theCorrelations[ap1] = newTable;
00113   }
00114 }
00115 
00116 
00117 bool AlignmentCorrelationsStore::correlationsAvailable( Alignable* ap1, Alignable* ap2 ) const
00118 {
00119   bool transpose = ( ap2 > ap1 );
00120   if ( transpose ) std::swap( ap1, ap2 );
00121 
00122   Correlations::const_iterator itC1 = theCorrelations.find( ap1 );
00123   if ( itC1 != theCorrelations.end() )
00124   {
00125     CorrelationsTable::const_iterator itC2 = itC1->second->find( ap2 );
00126     if ( itC2 != itC1->second->end() ) return true;
00127   }
00128   return false;
00129 }
00130 
00131 
00132 void AlignmentCorrelationsStore::resetCorrelations( void )
00133 {
00134   Correlations::iterator itC;
00135   for ( itC = theCorrelations.begin(); itC != theCorrelations.end(); ++itC ) delete (*itC).second;
00136   theCorrelations.erase( theCorrelations.begin(), theCorrelations.end() );
00137 
00138   // Reset the static pointers to the 'previous alignables'
00139   AlgebraicSymMatrix dummy;
00140   correlations( 0, 0, dummy, 0, 0 );
00141   setCorrelations( 0, 0, dummy, 0, 0 );
00142 }
00143 
00144 
00145 unsigned int AlignmentCorrelationsStore::size( void ) const
00146 {
00147   unsigned int size = 0;
00148   Correlations::const_iterator itC;
00149   for ( itC = theCorrelations.begin(); itC != theCorrelations.end(); ++itC )
00150     size += itC->second->size();
00151 
00152   return size;
00153 }
00154 
00155 
00156 void
00157 AlignmentCorrelationsStore::fillCorrelationsTable( Alignable* ap1, Alignable* ap2, CorrelationsTable* table,
00158                                                    const AlgebraicSymMatrix& cov, int row, int col, bool transpose )
00159 {
00160   CorrelationsTable::iterator itC = table->find( ap2 );
00161 
00162   if ( itC != table->end() )
00163   {
00164     transpose ?
00165       readFromCovarianceT( ap1, ap2, itC->second, cov, row, col ) :
00166       readFromCovariance( ap1, ap2, itC->second, cov, row, col );
00167   }
00168   else
00169   {
00170     int nRow = ap1->alignmentParameters()->numSelected();
00171     int nCol = ap2->alignmentParameters()->numSelected();
00172     AlgebraicMatrix newEntry( nRow, nCol );
00173 
00174     transpose ?
00175       readFromCovarianceT( ap1, ap2, newEntry, cov, row, col ) :
00176       readFromCovariance( ap1, ap2, newEntry, cov, row, col );
00177 
00178     (*table)[ap2] = newEntry;
00179   }
00180 }
00181 
00182 
00183 void
00184 AlignmentCorrelationsStore::fillCovariance( Alignable* ap1, Alignable* ap2, const AlgebraicMatrix& entry,
00185                                             AlgebraicSymMatrix& cov, int row, int col ) const
00186 {
00187   int nRow = entry.num_row();
00188   int nCol = entry.num_col();
00189 
00190   for ( int iRow = 0; iRow < nRow; ++iRow )
00191     for ( int jCol = 0; jCol < nCol; ++jCol )
00192       cov[row+iRow][col+jCol] = entry[iRow][jCol];
00193 }
00194 
00195 
00196 void
00197 AlignmentCorrelationsStore::fillCovarianceT( Alignable* ap1, Alignable* ap2, const AlgebraicMatrix& entry,
00198                                              AlgebraicSymMatrix& cov, int row, int col ) const
00199 {
00200   int nRow = entry.num_row();
00201   int nCol = entry.num_col();
00202 
00203   for ( int iRow = 0; iRow < nRow; ++iRow )
00204     for ( int jCol = 0; jCol < nCol; ++jCol )
00205       cov[row+jCol][col+iRow] = entry[iRow][jCol];
00206 }
00207 
00208 
00209 void
00210 AlignmentCorrelationsStore::readFromCovariance( Alignable* ap1, Alignable* ap2, AlgebraicMatrix& entry,
00211                                                 const AlgebraicSymMatrix& cov, int row, int col )
00212 {
00213   int nRow = entry.num_row();
00214   int nCol = entry.num_col();
00215 
00216   for ( int iRow = 0; iRow < nRow; ++iRow )
00217     for ( int jCol = 0; jCol < nCol; ++jCol )
00218       entry[iRow][jCol] = cov[row+iRow][col+jCol];
00219 }
00220 
00221 
00222 void
00223 AlignmentCorrelationsStore::readFromCovarianceT( Alignable* ap1, Alignable* ap2, AlgebraicMatrix& entry,
00224                                                  const AlgebraicSymMatrix& cov, int row, int col )
00225 {
00226   int nRow = entry.num_row();
00227   int nCol = entry.num_col();
00228 
00229   for ( int iRow = 0; iRow < nRow; ++iRow )
00230     for ( int jCol = 0; jCol < nCol; ++jCol )
00231       entry[iRow][jCol] = cov[row+jCol][col+iRow];
00232 }