CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AlignmentExtendedCorrelationsStore.cc
Go to the documentation of this file.
2 
10 
11 
13 {
14  theMaxUpdates = config.getParameter<int>( "MaxUpdates" );
15  theCut = config.getParameter<double>( "CutValue" );
16  theWeight = config.getParameter<double>( "Weight" );
17 
18  edm::LogInfo("Alignment") << "@SUB=AlignmentExtendedCorrelationsStore::AlignmentExtendedCorrelationsStore"
19  << "Created.";
20 }
21 
22 
24  AlgebraicSymMatrix& cov, int row, int col ) const
25 {
26  static Alignable* previousAlignable = 0;
27  static ExtendedCorrelationsTable* previousCorrelations;
28 
29  // Needed by 'resetCorrelations()' to reset the static pointer:
30  if ( ap1 == 0 ) { previousAlignable = 0; return; }
31 
32  bool transpose = ( ap2 > ap1 );
33  if ( transpose ) std::swap( ap1, ap2 );
34 
35  if ( ap1 == previousAlignable )
36  {
37  ExtendedCorrelationsTable::const_iterator itC2 = previousCorrelations->find( ap2 );
38  if ( itC2 != previousCorrelations->end() )
39  {
40  transpose ?
41  fillCovarianceT( ap1, ap2, (*itC2).second, cov, row, col ) :
42  fillCovariance( ap1, ap2, (*itC2).second, cov, row, col );
43  }
44  }
45  else
46  {
47  ExtendedCorrelations::const_iterator itC1 = theCorrelations.find( ap1 );
48  if ( itC1 != theCorrelations.end() )
49  {
50  previousAlignable = ap1;
51  previousCorrelations = (*itC1).second;
52 
53  ExtendedCorrelationsTable::const_iterator itC2 = (*itC1).second->find( ap2 );
54  if ( itC2 != (*itC1).second->end() )
55  {
56  transpose ?
57  fillCovarianceT( ap1, ap2, (*itC2).second, cov, row, col ) :
58  fillCovariance( ap1, ap2, (*itC2).second, cov, row, col );
59  }
60  }
61  }
62 
63  // don't fill anything into the covariance if there's no entry
64  return;
65 }
66 
67 
69  const AlgebraicSymMatrix& cov, int row, int col )
70 {
71  static Alignable* previousAlignable = 0;
72  static ExtendedCorrelationsTable* previousCorrelations;
73 
74  // Needed by 'resetCorrelations()' to reset the static pointer:
75  if ( ap1 == 0 ) { previousAlignable = 0; return; }
76 
77  bool transpose = ( ap2 > ap1 );
78  if ( transpose ) std::swap( ap1, ap2 );
79 
80  if ( ap1 == previousAlignable )
81  {
82  fillCorrelationsTable( ap1, ap2, previousCorrelations, cov, row, col, transpose );
83  }
84  else
85  {
86  ExtendedCorrelations::iterator itC = theCorrelations.find( ap1 );
87  if ( itC != theCorrelations.end() )
88  {
89  fillCorrelationsTable( ap1, ap2, itC->second, cov, row, col, transpose );
90  previousAlignable = ap1;
91  previousCorrelations = itC->second;
92  }
93  else
94  {
95  // make new entry
97  fillCorrelationsTable( ap1, ap2, newTable, cov, row, col, transpose );
98 
99  theCorrelations[ap1] = newTable;
100 
101  previousAlignable = ap1;
102  previousCorrelations = newTable;
103  }
104  }
105 }
106 
107 
109 {
110  bool transpose = ( ap2 > ap1 );
111  if ( transpose ) std::swap( ap1, ap2 );
112 
113  ExtendedCorrelations::iterator itC1 = theCorrelations.find( ap1 );
114  if ( itC1 != theCorrelations.end() )
115  {
116  ExtendedCorrelationsTable::iterator itC2 = itC1->second->find( ap1 );
117  if ( itC2 != itC1->second->end() )
118  {
119  itC2->second = transpose ? ExtendedCorrelationsEntry( mat.T() ) : ExtendedCorrelationsEntry( mat );
120  }
121  else
122  {
123  (*itC1->second)[ap2] = transpose ? ExtendedCorrelationsEntry( mat.T() ) : ExtendedCorrelationsEntry( mat );
124  }
125  }
126  else
127  {
129  (*newTable)[ap2] = transpose ? ExtendedCorrelationsEntry( mat.T() ) : ExtendedCorrelationsEntry( mat );
130  theCorrelations[ap1] = newTable;
131  }
132 }
133 
134 
136 {
137  bool transpose = ( ap2 > ap1 );
138  if ( transpose ) std::swap( ap1, ap2 );
139 
140  ExtendedCorrelations::const_iterator itC1 = theCorrelations.find( ap1 );
141  if ( itC1 != theCorrelations.end() )
142  {
143  ExtendedCorrelationsTable::const_iterator itC2 = itC1->second->find( ap2 );
144  if ( itC2 != itC1->second->end() )
145  {
146  mat = transpose ? itC2->second.matrix().T() : itC2->second.matrix();
147  return;
148  }
149  }
150 
151  mat = AlgebraicMatrix();
152 }
153 
154 
156 {
157  bool transpose = ( ap2 > ap1 );
158  if ( transpose ) std::swap( ap1, ap2 );
159 
160  ExtendedCorrelations::const_iterator itC1 = theCorrelations.find( ap1 );
161  if ( itC1 != theCorrelations.end() )
162  {
163  ExtendedCorrelationsTable::const_iterator itC2 = itC1->second->find( ap2 );
164  if ( itC2 != itC1->second->end() ) return true;
165  }
166  return false;
167 }
168 
169 
171 {
172  ExtendedCorrelations::iterator itC;
173  for ( itC = theCorrelations.begin(); itC != theCorrelations.end(); ++itC ) delete (*itC).second;
174  theCorrelations.erase( theCorrelations.begin(), theCorrelations.end() );
175 
176  // Reset the static pointers to the 'previous alignables'
177  AlgebraicSymMatrix dummy;
178  correlations( 0, 0, dummy, 0, 0 );
179  setCorrelations( 0, 0, dummy, 0, 0 );
180 }
181 
182 
184 {
185  unsigned int size = 0;
186  ExtendedCorrelations::const_iterator itC;
187  for ( itC = theCorrelations.begin(); itC != theCorrelations.end(); ++itC )
188  size += itC->second->size();
189 
190  return size;
191 }
192 
193 
194 void
197  const AlgebraicSymMatrix& cov,
198  int row, int col, bool transpose )
199 {
200  ExtendedCorrelationsTable::iterator itC = table->find( ap2 );
201 
202  if ( itC != table->end() )
203  {
204  //if ( itC->second.counter() > theMaxUpdates ) return;
205 
206  transpose ?
207  readFromCovarianceT( ap1, ap2, itC->second, cov, row, col ) :
208  readFromCovariance( ap1, ap2, itC->second, cov, row, col );
209 
210  //itC->second.incrementCounter();
211  }
212  else
213  {
214  int nRow = ap1->alignmentParameters()->numSelected();
215  int nCol = ap2->alignmentParameters()->numSelected();
216  ExtendedCorrelationsEntry newEntry( nRow, nCol );
217 
218  transpose ?
219  readFromCovarianceT( ap1, ap2, newEntry, cov, row, col ) :
220  readFromCovariance( ap1, ap2, newEntry, cov, row, col );
221 
222  (*table)[ap2] = newEntry;
223  }
224 }
225 
226 
227 void
229  AlgebraicSymMatrix& cov, int row, int col ) const
230 {
231  int nRow = entry.numRow();
232  int nCol = entry.numCol();
233 
234  for ( int iRow = 0; iRow < nRow; ++iRow )
235  {
236  double factor = sqrt(cov[row+iRow][row+iRow]);
237  if ( edm::isNotFinite(factor) ) throw cms::Exception("LogicError") << "[AlignmentExtendedCorrelationsStore::fillCovariance] "
238  << "NaN-factor: sqrt(" << cov[row+iRow][row+iRow] << ")";
239 
240  for ( int jCol = 0; jCol < nCol; ++jCol )
241  cov[row+iRow][col+jCol] = entry( iRow, jCol )*factor;
242  }
243 
244  for ( int jCol = 0; jCol < nCol; ++jCol )
245  {
246  double factor = sqrt(cov[col+jCol][col+jCol]);
247  if ( edm::isNotFinite(factor) ) throw cms::Exception("LogicError") << "[AlignmentExtendedCorrelationsStore::fillCovariance] "
248  << "NaN-factor: sqrt(" << cov[col+jCol][col+jCol] << ")";
249 
250  for ( int iRow = 0; iRow < nRow; ++iRow )
251  cov[row+iRow][col+jCol] *= factor;
252  }
253 }
254 
255 
256 void
258  AlgebraicSymMatrix& cov, int row, int col ) const
259 {
260  int nRow = entry.numRow();
261  int nCol = entry.numCol();
262 
263  for ( int iRow = 0; iRow < nRow; ++iRow )
264  {
265  double factor = sqrt(cov[col+iRow][col+iRow]);
266  if ( edm::isNotFinite(factor) ) throw cms::Exception("LogicError") << "[AlignmentExtendedCorrelationsStore::fillCovarianceT] "
267  << "NaN-factor: sqrt(" << cov[col+iRow][col+iRow] << ")";
268  for ( int jCol = 0; jCol < nCol; ++jCol )
269  cov[row+jCol][col+iRow] = entry( iRow, jCol )*factor;
270  }
271 
272  for ( int jCol = 0; jCol < nCol; ++jCol )
273  {
274  double factor = sqrt(cov[row+jCol][row+jCol]);
275  if ( edm::isNotFinite(factor) ) throw cms::Exception("LogicError") << "[AlignmentExtendedCorrelationsStore::fillCovarianceT] "
276  << "NaN-factor: sqrt(" << cov[row+jCol][row+jCol] << ")";
277  for ( int iRow = 0; iRow < nRow; ++iRow )
278  cov[row+jCol][col+iRow] *= factor;
279  }
280 
281 }
282 
283 
284 void
286  const AlgebraicSymMatrix& cov, int row, int col )
287 {
288  int nRow = entry.numRow();
289  int nCol = entry.numCol();
290 
291  for ( int iRow = 0; iRow < nRow; ++iRow )
292  {
293  double factor = sqrt(cov[row+iRow][row+iRow]);
294  for ( int jCol = 0; jCol < nCol; ++jCol )
295  entry( iRow, jCol ) = cov[row+iRow][col+jCol]/factor;
296  }
297 
298  double maxCorr = 0;
299 
300  for ( int jCol = 0; jCol < nCol; ++jCol )
301  {
302  double factor = sqrt(cov[col+jCol][col+jCol]);
303  for ( int iRow = 0; iRow < nRow; ++iRow )
304  {
305  entry( iRow, jCol ) /= factor;
306  if ( fabs( entry( iRow, jCol ) ) > maxCorr ) maxCorr = fabs( entry( iRow, jCol ) );
307  }
308  }
309 
310  resizeCorruptCorrelations( entry, maxCorr );
311 }
312 
313 
314 void
316  const AlgebraicSymMatrix& cov, int row, int col )
317 {
318  int nRow = entry.numRow();
319  int nCol = entry.numCol();
320 
321  for ( int iRow = 0; iRow < nRow; ++iRow )
322  {
323  double factor = sqrt(cov[col+iRow][col+iRow]);
324  for ( int jCol = 0; jCol < nCol; ++jCol )
325  entry( iRow, jCol ) = cov[row+jCol][col+iRow]/factor;
326  }
327 
328  double maxCorr = 0;
329 
330  for ( int jCol = 0; jCol < nCol; ++jCol )
331  {
332  double factor = sqrt(cov[row+jCol][row+jCol]);
333  for ( int iRow = 0; iRow < nRow; ++iRow )
334  {
335  entry( iRow, jCol ) /= factor;
336  if ( fabs( entry( iRow, jCol ) ) > maxCorr ) maxCorr = fabs( entry( iRow, jCol ) );
337  }
338  }
339 
340  resizeCorruptCorrelations( entry, maxCorr );
341 }
342 
343 
344 void
346  double maxCorr )
347 {
348  if ( maxCorr > 1. )
349  {
350  entry *= theWeight/maxCorr;
351  }
352  else if ( maxCorr > theCut )
353  {
354  entry *= 1. - ( maxCorr - theCut )/( 1. - theCut )*( 1. - theWeight );
355  }
356 }
T getParameter(std::string const &) const
virtual void resetCorrelations(void)
Reset correlations.
std::map< Alignable *, ExtendedCorrelationsEntry > ExtendedCorrelationsTable
virtual void fillCorrelationsTable(Alignable *ap1, Alignable *ap2, ExtendedCorrelationsTable *table, const AlgebraicSymMatrix &cov, int row, int col, bool transpose)
virtual void fillCovariance(Alignable *ap1, Alignable *ap2, const ExtendedCorrelationsEntry &entry, AlgebraicSymMatrix &cov, int row, int col) const
virtual unsigned int size(void) const
Get number of stored correlations.
virtual bool correlationsAvailable(Alignable *ap1, Alignable *ap2) const
Check whether correlations are stored for a given pair of alignables.
AlignmentExtendedCorrelationsEntry ExtendedCorrelationsEntry
virtual void correlations(Alignable *ap1, Alignable *ap2, AlgebraicSymMatrix &cov, int row, int col) const
const short unsigned int numRow(void) const
Get the number of rows of the correlation matrix.
AlignmentParameters * alignmentParameters() const
Get the AlignmentParameters.
Definition: Alignable.h:57
const short unsigned int numCol(void) const
Get the number of columns of the correlation matrix.
virtual void setCorrelations(Alignable *ap1, Alignable *ap2, const AlgebraicSymMatrix &cov, int row, int col)
bool isNotFinite(T x)
Definition: isFinite.h:10
CLHEP::HepMatrix AlgebraicMatrix
virtual void fillCovarianceT(Alignable *ap1, Alignable *ap2, const ExtendedCorrelationsEntry &entry, AlgebraicSymMatrix &cov, int row, int col) const
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
T sqrt(T t)
Definition: SSEVec.h:48
void resizeCorruptCorrelations(ExtendedCorrelationsEntry &entry, double maxCorr)
virtual void readFromCovariance(Alignable *ap1, Alignable *ap2, ExtendedCorrelationsEntry &entry, const AlgebraicSymMatrix &cov, int row, int col)
int numSelected(void) const
Get number of selected parameters.
#define table(NAME)
Definition: DbCore.h:49
virtual void readFromCovarianceT(Alignable *ap1, Alignable *ap2, ExtendedCorrelationsEntry &entry, const AlgebraicSymMatrix &cov, int row, int col)
AlignmentExtendedCorrelationsStore(const edm::ParameterSet &config)
CLHEP::HepSymMatrix AlgebraicSymMatrix
int col
Definition: cuy.py:1008
virtual void getCorrelations(Alignable *ap1, Alignable *ap2, AlgebraicMatrix &mat) const
Get correlations.