Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:39

0001 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentCorrelationsStore.h"
0002 
0003 #include "Alignment/CommonAlignment/interface/Alignable.h"
0004 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 
0007 AlignmentCorrelationsStore::AlignmentCorrelationsStore(void) {
0008   edm::LogInfo("Alignment") << "@SUB=AlignmentCorrelationsStore::AlignmentCorrelationsStore "
0009                             << "\nCreated.";
0010 }
0011 
0012 void AlignmentCorrelationsStore::correlations(
0013     Alignable* ap1, Alignable* ap2, AlgebraicSymMatrix& cov, int row, int col) const {
0014   static Alignable* previousAlignable = nullptr;
0015   static CorrelationsTable* previousCorrelations;
0016 
0017   // Needed by 'resetCorrelations()' to reset the static pointer:
0018   if (ap1 == nullptr) {
0019     previousAlignable = nullptr;
0020     return;
0021   }
0022 
0023   bool transpose = (ap2 > ap1);
0024   if (transpose)
0025     std::swap(ap1, ap2);
0026 
0027   if (ap1 == previousAlignable) {
0028     CorrelationsTable::const_iterator itC2 = previousCorrelations->find(ap2);
0029     if (itC2 != previousCorrelations->end()) {
0030       transpose ? fillCovarianceT(ap1, ap2, (*itC2).second, cov, row, col)
0031                 : fillCovariance(ap1, ap2, (*itC2).second, cov, row, col);
0032     }
0033   } else {
0034     Correlations::const_iterator itC1 = theCorrelations.find(ap1);
0035     if (itC1 != theCorrelations.end()) {
0036       previousAlignable = ap1;
0037       previousCorrelations = (*itC1).second;
0038 
0039       CorrelationsTable::const_iterator itC2 = (*itC1).second->find(ap2);
0040       if (itC2 != (*itC1).second->end()) {
0041         transpose ? fillCovarianceT(ap1, ap2, (*itC2).second, cov, row, col)
0042                   : fillCovariance(ap1, ap2, (*itC2).second, cov, row, col);
0043       }
0044     }
0045   }
0046 
0047   // don't fill anything into the covariance if there's no entry
0048   return;
0049 }
0050 
0051 void AlignmentCorrelationsStore::setCorrelations(
0052     Alignable* ap1, Alignable* ap2, const AlgebraicSymMatrix& cov, int row, int col) {
0053   static Alignable* previousAlignable = nullptr;
0054   static CorrelationsTable* previousCorrelations;
0055 
0056   // Needed by 'resetCorrelations()' to reset the static pointer:
0057   if (ap1 == nullptr) {
0058     previousAlignable = nullptr;
0059     return;
0060   }
0061 
0062   bool transpose = (ap2 > ap1);
0063   if (transpose)
0064     std::swap(ap1, ap2);
0065 
0066   if (ap1 == previousAlignable) {
0067     fillCorrelationsTable(ap1, ap2, previousCorrelations, cov, row, col, transpose);
0068   } else {
0069     Correlations::iterator itC = theCorrelations.find(ap1);
0070     if (itC != theCorrelations.end()) {
0071       fillCorrelationsTable(ap1, ap2, itC->second, cov, row, col, transpose);
0072       previousAlignable = ap1;
0073       previousCorrelations = itC->second;
0074     } else {
0075       CorrelationsTable* newTable = new CorrelationsTable;
0076       fillCorrelationsTable(ap1, ap2, newTable, cov, row, col, transpose);
0077 
0078       theCorrelations[ap1] = newTable;
0079       previousAlignable = ap1;
0080       previousCorrelations = newTable;
0081     }
0082   }
0083 }
0084 
0085 void AlignmentCorrelationsStore::setCorrelations(Alignable* ap1, Alignable* ap2, AlgebraicMatrix& mat) {
0086   bool transpose = (ap2 > ap1);
0087   if (transpose)
0088     std::swap(ap1, ap2);
0089 
0090   Correlations::iterator itC1 = theCorrelations.find(ap1);
0091   if (itC1 != theCorrelations.end()) {
0092     (*itC1->second)[ap2] = transpose ? mat.T() : mat;
0093   } else {
0094     CorrelationsTable* newTable = new CorrelationsTable;
0095     (*newTable)[ap2] = transpose ? mat.T() : mat;
0096     theCorrelations[ap1] = newTable;
0097   }
0098 }
0099 
0100 bool AlignmentCorrelationsStore::correlationsAvailable(Alignable* ap1, Alignable* ap2) const {
0101   bool transpose = (ap2 > ap1);
0102   if (transpose)
0103     std::swap(ap1, ap2);
0104 
0105   Correlations::const_iterator itC1 = theCorrelations.find(ap1);
0106   if (itC1 != theCorrelations.end()) {
0107     CorrelationsTable::const_iterator itC2 = itC1->second->find(ap2);
0108     if (itC2 != itC1->second->end())
0109       return true;
0110   }
0111   return false;
0112 }
0113 
0114 void AlignmentCorrelationsStore::resetCorrelations(void) {
0115   Correlations::iterator itC;
0116   for (itC = theCorrelations.begin(); itC != theCorrelations.end(); ++itC)
0117     delete (*itC).second;
0118   theCorrelations.erase(theCorrelations.begin(), theCorrelations.end());
0119 
0120   // Reset the static pointers to the 'previous alignables'
0121   AlgebraicSymMatrix dummy;
0122   correlations(nullptr, nullptr, dummy, 0, 0);
0123   setCorrelations(nullptr, nullptr, dummy, 0, 0);
0124 }
0125 
0126 unsigned int AlignmentCorrelationsStore::size(void) const {
0127   unsigned int size = 0;
0128   Correlations::const_iterator itC;
0129   for (itC = theCorrelations.begin(); itC != theCorrelations.end(); ++itC)
0130     size += itC->second->size();
0131 
0132   return size;
0133 }
0134 
0135 void AlignmentCorrelationsStore::fillCorrelationsTable(Alignable* ap1,
0136                                                        Alignable* ap2,
0137                                                        CorrelationsTable* table,
0138                                                        const AlgebraicSymMatrix& cov,
0139                                                        int row,
0140                                                        int col,
0141                                                        bool transpose) {
0142   CorrelationsTable::iterator itC = table->find(ap2);
0143 
0144   if (itC != table->end()) {
0145     transpose ? readFromCovarianceT(ap1, ap2, itC->second, cov, row, col)
0146               : readFromCovariance(ap1, ap2, itC->second, cov, row, col);
0147   } else {
0148     int nRow = ap1->alignmentParameters()->numSelected();
0149     int nCol = ap2->alignmentParameters()->numSelected();
0150     AlgebraicMatrix newEntry(nRow, nCol);
0151 
0152     transpose ? readFromCovarianceT(ap1, ap2, newEntry, cov, row, col)
0153               : readFromCovariance(ap1, ap2, newEntry, cov, row, col);
0154 
0155     (*table)[ap2] = newEntry;
0156   }
0157 }
0158 
0159 void AlignmentCorrelationsStore::fillCovariance(
0160     Alignable* ap1, Alignable* ap2, const AlgebraicMatrix& entry, AlgebraicSymMatrix& cov, int row, int col) const {
0161   int nRow = entry.num_row();
0162   int nCol = entry.num_col();
0163 
0164   for (int iRow = 0; iRow < nRow; ++iRow)
0165     for (int jCol = 0; jCol < nCol; ++jCol)
0166       cov[row + iRow][col + jCol] = entry[iRow][jCol];
0167 }
0168 
0169 void AlignmentCorrelationsStore::fillCovarianceT(
0170     Alignable* ap1, Alignable* ap2, const AlgebraicMatrix& entry, AlgebraicSymMatrix& cov, int row, int col) const {
0171   int nRow = entry.num_row();
0172   int nCol = entry.num_col();
0173 
0174   for (int iRow = 0; iRow < nRow; ++iRow)
0175     for (int jCol = 0; jCol < nCol; ++jCol)
0176       cov[row + jCol][col + iRow] = entry[iRow][jCol];
0177 }
0178 
0179 void AlignmentCorrelationsStore::readFromCovariance(
0180     Alignable* ap1, Alignable* ap2, AlgebraicMatrix& entry, const AlgebraicSymMatrix& cov, int row, int col) {
0181   int nRow = entry.num_row();
0182   int nCol = entry.num_col();
0183 
0184   for (int iRow = 0; iRow < nRow; ++iRow)
0185     for (int jCol = 0; jCol < nCol; ++jCol)
0186       entry[iRow][jCol] = cov[row + iRow][col + jCol];
0187 }
0188 
0189 void AlignmentCorrelationsStore::readFromCovarianceT(
0190     Alignable* ap1, Alignable* ap2, AlgebraicMatrix& entry, const AlgebraicSymMatrix& cov, int row, int col) {
0191   int nRow = entry.num_row();
0192   int nCol = entry.num_col();
0193 
0194   for (int iRow = 0; iRow < nRow; ++iRow)
0195     for (int jCol = 0; jCol < nCol; ++jCol)
0196       entry[iRow][jCol] = cov[row + jCol][col + iRow];
0197 }