CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SingleTrajectoryUpdator.cc
Go to the documentation of this file.
1 
2 //#include "Alignment/KalmanAlignmentAlgorithm/plugins/SingleTrajectoryUpdator.h"
4 
6 
8 
10 
12 
13 #include <algorithm>
14 
15 
16 using namespace std;
17 
18 
20  KalmanAlignmentUpdator( config )
21 {
22  theMinNumberOfHits = config.getParameter< unsigned int >( "MinNumberOfHits" );
23  theExtraWeight = config.getParameter< double >( "ExtraWeight" );
24  theExternalPredictionWeight = config.getParameter< double >( "ExternalPredictionWeight" );
25  theCovCheckFlag = config.getParameter< bool >( "CheckCovariance" );
26 
27  theNumberOfPreAlignmentEvts = config.getParameter< unsigned int >( "NumberOfPreAlignmentEvts" );
29 
30  std::cout << "[SingleTrajectoryUpdator] Use " << theNumberOfPreAlignmentEvts << "events for pre-alignment" << std::endl;
31 }
32 
33 
35 
36 
39  AlignableNavigator* navigator,
41  const MagneticField* magField )
42 {
43  if ( !( *trajectory ).isValid() ) return;
44 
45 // std::cout << "[SingleTrajectoryUpdator::process] START" << std::endl;
46 
47  vector< AlignableDetOrUnitPtr > currentAlignableDets = navigator->alignablesFromHits( ( *trajectory ).recHits() );
48  vector< Alignable* > currentAlignables = alignablesFromAlignableDets( currentAlignableDets, store );
49 
50  if ( nDifferentAlignables( currentAlignables ) < 2 ) return;
51  if ( currentAlignables.size() < theMinNumberOfHits ) return;
52 
54  bool includeCorrelations = ( theNumberOfPreAlignmentEvts < theNumberOfProcessedEvts );
55 
56  metrics->update( currentAlignables );
57 
58  vector< Alignable* > additionalAlignables;
59  if ( includeCorrelations ) additionalAlignables = metrics->additionalAlignables( currentAlignables );
60 
61  vector< Alignable* > allAlignables;
62  allAlignables.reserve( currentAlignables.size() + additionalAlignables.size() );
63  allAlignables.insert( allAlignables.end(), currentAlignables.begin(), currentAlignables.end() );
64  allAlignables.insert( allAlignables.end(), additionalAlignables.begin(), additionalAlignables.end() );
65 
66  CompositeAlignmentParameters alignmentParameters = store->selectParameters( allAlignables );
67 
68  const AlgebraicVector& allAlignmentParameters = alignmentParameters.parameters();
69  AlgebraicSymMatrix currentAlignmentCov = alignmentParameters.covarianceSubset( currentAlignables );
70  AlgebraicSymMatrix additionalAlignmentCov = alignmentParameters.covarianceSubset( additionalAlignables );
71  AlgebraicMatrix mixedAlignmentCov = alignmentParameters.covarianceSubset( additionalAlignables, currentAlignables );
72  AlgebraicMatrix alignmentCovSubset = alignmentParameters.covarianceSubset( alignmentParameters.components(), currentAlignables );
73 
74  CompositeAlignmentDerivativesExtractor extractor( currentAlignables, currentAlignableDets, trajectory->trajectoryStates() );
75  AlgebraicVector correctionTerm = extractor.correctionTerm();
76  AlgebraicMatrix alignmentDeriv = extractor.derivatives();
77  AlgebraicSymMatrix alignmentCov = currentAlignmentCov.similarity( alignmentDeriv );
78 
79  AlgebraicVector allMeasurements = trajectory->measurements();
80  AlgebraicSymMatrix measurementCov = trajectory->measurementErrors();
81  AlgebraicVector referenceTrajectory = trajectory->trajectoryPositions();
82  AlgebraicMatrix derivatives = trajectory->derivatives();
83 
84  measurementCov += theExtraWeight*AlgebraicSymMatrix( measurementCov.num_row(), 1 );
85 
86  AlgebraicSymMatrix misalignedCov = measurementCov + alignmentCov;
87 
88  int checkInversion = 0;
89 
90  AlgebraicSymMatrix weightMatrix;
91  AlgebraicVector residuals;
92 
93  if ( trajectory->parameterErrorsAvailable() ) // Make an update using an external prediction for the track parameters.
94  {
95  const AlgebraicSymMatrix& externalParamCov = trajectory->parameterErrors();
96  AlgebraicSymMatrix externalTrackCov = theExternalPredictionWeight*externalParamCov.similarity( derivatives );
97  AlgebraicSymMatrix fullCov = misalignedCov + externalTrackCov;
98  measurementCov += externalTrackCov;
99 
100  weightMatrix = fullCov.inverse( checkInversion );
101  if ( checkInversion != 0 )
102  {
103  cout << "[KalmanAlignment] WARNING: 'AlgebraicSymMatrix fullCov' not invertible." << endl;
104  return;
105  }
106 
107  //const AlgebraicVector& trackParameters = trajectory->parameters();
108  //const AlgebraicVector& externalTrackParameters = trajectory->externalPrediction();
109  //AlgebraicVector trackCorrectionTerm = derivatives*( externalTrackParameters - trackParameters );
110  residuals = allMeasurements - referenceTrajectory - correctionTerm;// - trackCorrectionTerm;
111  }
112  else // No external prediction for the track parameters available --> give the track parameters weight 0.
113  {
114  AlgebraicSymMatrix invMisalignedCov = misalignedCov.inverse( checkInversion );
115  if ( checkInversion != 0 )
116  {
117  cout << "[KalmanAlignment] WARNING: 'AlgebraicSymMatrix misalignedCov' not invertible." << endl;
118  return;
119  }
120  AlgebraicSymMatrix weightMatrix1 = ( invMisalignedCov.similarityT( derivatives ) ).inverse( checkInversion );
121  if ( checkInversion != 0 )
122  {
123  cout << "[KalmanAlignment] WARNING: 'AlgebraicSymMatrix weightMatrix1' not computed." << endl;
124  return;
125  }
126  AlgebraicSymMatrix weightMatrix2 = weightMatrix1.similarity( invMisalignedCov*derivatives );
127 
128  weightMatrix = invMisalignedCov - weightMatrix2;
129  residuals = allMeasurements - referenceTrajectory - correctionTerm;
130  }
131 
132 // AlgebraicVector deltaR = allMeasurements - referenceTrajectory;
133 // for ( int i = 0; i < deltaR.num_row()/2; ++i )
134 // KalmanAlignmentDataCollector::fillHistogram( "DeltaR_", i, deltaR[2*i] );
135 // return;
136 
137  AlgebraicMatrix fullCovTimesDeriv = alignmentCovSubset*alignmentDeriv.T();
138  AlgebraicMatrix fullGainMatrix = fullCovTimesDeriv*weightMatrix;
139  AlgebraicMatrix covTimesDeriv = currentAlignmentCov*alignmentDeriv.T();
140  AlgebraicMatrix gainMatrix = covTimesDeriv*weightMatrix;
141 
142  // make updates for the kalman-filter
143  // update of parameters
144  AlgebraicVector updatedAlignmentParameters = allAlignmentParameters + fullGainMatrix*residuals;
145 
146  // update of covariance
147  int nCRow = currentAlignmentCov.num_row();
148  int nARow = additionalAlignmentCov.num_row();
149 
150  AlgebraicSymMatrix updatedAlignmentCov( nCRow + nARow );
151 
152  AlgebraicMatrix gTimesDeriv = weightMatrix*alignmentDeriv;
153  AlgebraicMatrix simMat = AlgebraicMatrix( nCRow, nCRow, 1 ) - covTimesDeriv*gTimesDeriv;
154  AlgebraicSymMatrix updatedCurrentAlignmentCov = currentAlignmentCov.similarity( simMat ) + measurementCov.similarity( gainMatrix );
155 
156  AlgebraicMatrix mixedUpdateMat = simMat.T()*simMat.T() + measurementCov.similarity( gTimesDeriv.T() )*currentAlignmentCov;
157  AlgebraicMatrix updatedMixedAlignmentCov = mixedAlignmentCov*mixedUpdateMat;
158 
159  AlgebraicSymMatrix additionalUpdateMat = misalignedCov.similarity( gTimesDeriv.T() ) - 2.*weightMatrix.similarity( alignmentDeriv.T() );
160  AlgebraicSymMatrix updatedAdditionalAlignmentCov = additionalAlignmentCov + additionalUpdateMat.similarity( mixedAlignmentCov );
161 
162  for ( int nRow=0; nRow<nCRow; nRow++ )
163  {
164  for ( int nCol=0; nCol<=nRow; nCol++ ) updatedAlignmentCov[nRow][nCol] = updatedCurrentAlignmentCov[nRow][nCol];
165  }
166 
167  for ( int nRow=0; nRow<nARow; nRow++ )
168  {
169  for ( int nCol=0; nCol<=nRow; nCol++ ) updatedAlignmentCov[nRow+nCRow][nCol+nCRow] = updatedAdditionalAlignmentCov[nRow][nCol];
170  }
171 
172  for ( int nRow=0; nRow<nARow; nRow++ )
173  {
174  for ( int nCol=0; nCol<nCRow; nCol++ ) updatedAlignmentCov[nRow+nCRow][nCol] = updatedMixedAlignmentCov[nRow][nCol];
175  }
176 
177 
178  // update in alignment-interface
179  CompositeAlignmentParameters* updatedParameters;
180  updatedParameters = alignmentParameters.clone( updatedAlignmentParameters, updatedAlignmentCov );
181 
182  if ( !checkCovariance( updatedAlignmentCov ) )
183  {
184  if ( includeCorrelations ) throw cms::Exception( "BadCovariance" );
185 
186  delete updatedParameters;
187  return;
188  }
189 
190  store->updateParameters( *updatedParameters, includeCorrelations );
191  delete updatedParameters;
192 
193 
194  // update user variables for debugging
195  //updateUserVariables( alignmentParameters.components() );
196 
197  //std::cout << "update user variables now" << std::endl;
198  updateUserVariables( currentAlignables );
199  //std::cout << "done." << std::endl;
200 
201  static int i = 0;
202  if ( i%100 == 0 ) KalmanAlignmentDataCollector::fillGraph( "correlation_entries", i, store->numCorrelations() );
203  ++i;
204 
205  //std::cout << "[SingleTrajectoryUpdator::process] DONE" << std::endl;
206 
207  return;
208 }
209 
210 
212 {
213  for ( int i = 0; i < cov.num_row(); ++i )
214  {
215  if ( cov[i][i] < 0. ) return false;
216  }
217 
218  return true;
219 }
220 
221 
T getParameter(std::string const &) const
virtual const std::vector< Alignable * > alignablesFromAlignableDets(std::vector< AlignableDetOrUnitPtr > &alignableDets, AlignmentParameterStore *store) const
int i
Definition: DBlmapReader.cc:9
virtual void process(const ReferenceTrajectoryPtr &trajectory, AlignmentParameterStore *store, AlignableNavigator *navigator, KalmanAlignmentMetricsUpdator *metrics, const MagneticField *magField=0)
Calculate the improved estimate.
void updateUserVariables(const std::vector< Alignable * > &alignables) const
Update the AlignmentUserVariables, given that the Alignables hold KalmanAlignmentUserVariables.
virtual const std::vector< Alignable * > additionalAlignables(const std::vector< Alignable * > &alignables)=0
virtual void update(const std::vector< Alignable * > &alignables)=0
void updateParameters(const CompositeAlignmentParameters &aap, bool updateCorrelations=true)
update parameters
const AlgebraicVector & parameters() const
Get alignment parameters.
AlgebraicSymMatrix covarianceSubset(const std::vector< Alignable * > &vec) const
Extract covariance matrix for subset of alignables.
CLHEP::HepMatrix AlgebraicMatrix
Components components() const
Get vector of alignable components.
SingleTrajectoryUpdator(const edm::ParameterSet &config)
bool checkCovariance(const AlgebraicSymMatrix &cov) const
const unsigned int numCorrelations(void) const
get number of correlations between alignables
unsigned int nDifferentAlignables(const std::vector< Alignable * > &ali) const
CLHEP::HepVector AlgebraicVector
CompositeAlignmentParameters selectParameters(const std::vector< AlignableDet * > &alignabledets) const
std::vector< AlignableDetOrUnitPtr > alignablesFromHits(const std::vector< const TransientTrackingRecHit * > &hitvec)
Returns vector AlignableDetOrUnitPtr for given vector of Hits.
CLHEP::HepSymMatrix AlgebraicSymMatrix
tuple cout
Definition: gather_cfg.py:121
#define DEFINE_EDM_PLUGIN(factory, type, name)
static void fillGraph(std::string graph_name, float x_data, float y_data)
CompositeAlignmentParameters * clone(const AlgebraicVector &par, const AlgebraicSymMatrix &cov) const
Clone parameters.