87 {
88
89
90
91
92 if (vertexToSmooth==nullptr) {
93 ATH_MSG_WARNING(
" Empty pointers then calling fit method update. No fit will be done..." );
94 return;
95 }
96
97 ATH_MSG_DEBUG(
"It's smoothing "<<(isPrimary?
"with":
"without")<<
" primary vertex ");
98 ATH_MSG_DEBUG(
"It's " << (updateTrack?
"with":
"without") <<
" updating the track.");
99 ATH_MSG_DEBUG(
"It's the "<<(doFastUpdate?
"fast":
"normal (weight-formalism)")<<
" smoother");
100
101 const std::vector<VxTrackAtVertex*> & allTracksToSmooth(vertexToSmooth->getTracksAtVertex());
102
103
104
105
106 if (allTracksToSmooth.empty()) {
108 if (!isPrimary)
109 ATH_MSG_WARNING (
"Nothing to smooth and it's not a primary vertex: BUG... ");
110 return;
111 }
112
113 const Amg::VectorX & initialjetdir=linearizationPositions.position();
114
115 int numVertex=vertexToSmooth->getNumVertex();
116
117
118
119 std::pair<Amg::Vector3D,Eigen::Matrix3Xd> PosOnJetAxisAndTransformMatrix =
120 m_Updator->createTransformJacobian(initialjetdir, numVertex, isPrimary,
false);
121
122 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
123 std::cout << " smoother: " << PosOnJetAxisAndTransformMatrix.first << " and transform matrix: " << PosOnJetAxisAndTransformMatrix.second <<
124 std::endl;
125 #endif
126
127
128
129
130
131 RecVertexPositions myPositionWithRemovedTracks=myFinalPosition;
132 RecVertexPositions myPositionInWeightFormalism=myFinalPosition;
133 const Amg::VectorX & final_vrt_pos=myFinalPosition.position();
134 const Amg::MatrixX & final_vrt_covariance = myFinalPosition.covariancePosition();
135
137 if (!doFastUpdate) {
138
139
140
141 Eigen::FullPivLU<Amg::MatrixX> lu_decomp(final_vrt_covariance);
142 if (!lu_decomp.isInvertible()) {
143 ATH_MSG_DEBUG(
"Jet vertex positions matrix not invertible right at start of smoother!" <<
144 " -> stop smoothing.");
145 return;
146 }
147 final_vrt_weightmatrix = final_vrt_covariance.inverse();
148 myPositionInWeightFormalism.setPosition(final_vrt_weightmatrix*
149 myPositionInWeightFormalism.position());
150 myPositionInWeightFormalism.setCovariancePosition(final_vrt_weightmatrix);
151 }
152
153
154 double track_compatibility_chi2(0.);
155 double track_ndf(0.);
156
157
158
159 const std::vector<VxTrackAtVertex*>::const_iterator TracksBegin=allTracksToSmooth.begin();
160 const std::vector<VxTrackAtVertex*>::const_iterator TracksEnd=allTracksToSmooth.end();
161
162 for (std::vector<VxTrackAtVertex*>::const_iterator TracksIter=TracksBegin;TracksIter!=TracksEnd;++TracksIter) {
163
164
165 const LinearizedTrack * trk=(*TracksIter)->linState();
166
167 if (trk==nullptr) {
168 ATH_MSG_WARNING (
" Empty pointers then calling smoothing method update. No smoothing will be performed...");
169 return;
170 }
171
172 double trackWeight = (*TracksIter)->weight();
173
174
175 const AmgMatrix(5,3)& oldA = trk->positionJacobian();
176
177 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
178 std::cout << "the old jacobian xyz d0z0phithetaqoverp vs xyz " << oldA << std::endl;
179 #endif
180
181
182 Eigen::Matrix<double,5,Eigen::Dynamic>
A = oldA*PosOnJetAxisAndTransformMatrix.second;
183
184 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
185 std::cout <<
"the new jacobian " <<
A << std::endl;
186 #endif
187
188 const AmgMatrix(5,3) &
B = trk->momentumJacobian();
189 const AmgVector(5)& trackParameters = trk->expectedParametersAtPCA();
190
191 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
192 std::cout << "expected perigee at pca " << trackParameters << std::endl;
193 std::cout << "expected position at pca " << trk->expectedPositionAtPCA() << std::endl;
194 std::cout << " constant term " << trk->constantTerm() <<
195 " old A " << oldA << " * PosOnJetAxis " << PosOnJetAxisAndTransformMatrix.first <<
196 " A " <<
A <<
" * initialjetdir " << initialjetdir << std::endl;
197 #endif
198
199 AmgVector(5) constantTerm=trk->constantTerm() + oldA*PosOnJetAxisAndTransformMatrix.first
200 -A*initialjetdir;
201 const
AmgSymMatrix(5)& trackParametersWeight = trk->expectedWeightAtPCA();
202 AmgSymMatrix(3) S = B.transpose()*trackParametersWeight*B;
203 if(S.determinant()==0.0) {
204 ATH_MSG_WARNING (
"The S matrix inversion failed during smoothing iteration cycle");
205 continue;
206 }
207 S=
S.inverse().eval();
208
209 if (doFastUpdate) {
210 const Amg::VectorX old_vrt_pos = myPositionWithRemovedTracks.position();
211 const Amg::MatrixX & old_vrt_cov = myPositionWithRemovedTracks.covariancePosition();
212
213 AmgSymMatrix(5) old_residual_cov = -trk->expectedCovarianceAtPCA() +
214 A*old_vrt_cov*A.transpose() + B*m_initial_cov_momentum*B.transpose();
215 if (old_residual_cov.determinant() == 0.0 ) {
216 ATH_MSG_ERROR (
"The old_residual matrix can not be inverted during smoothing interation cycle");
217 continue;
218 }
219 AmgSymMatrix(5) old_residual_cov_inv = old_residual_cov.inverse().eval();
220 Eigen::Matrix<double,Eigen::Dynamic,5> Kk1=old_vrt_cov*A.transpose()*old_residual_cov_inv;
221 AmgVector(5) residual_vector=trackParameters-constantTerm-A*old_vrt_pos;
222 Amg::VectorX new_vrt_pos = old_vrt_pos+Kk1*residual_vector;
223 Amg::MatrixX new_vrt_cov = old_vrt_cov+Kk1*old_residual_cov*Kk1.transpose()
224 - 2.*Kk1*A*old_vrt_cov;
225 myPositionWithRemovedTracks = RecVertexPositions(new_vrt_pos,new_vrt_cov);
226
227 } else {
228
229
230
231 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
232 std::cout << " Old weight Matrix is " << myPositionInWeightFormalism.covariancePosition() << std::endl;
233 #endif
234
235
236 const Amg::MatrixX & old_vrt_weight = myPositionInWeightFormalism.covariancePosition();
237 const Amg::VectorX old_vrt_weight_times_vrt_pos = myPositionInWeightFormalism.position();
238
239
240 AmgSymMatrix(5) gB = trackParametersWeight - trackParametersWeight*(B*(S*B.transpose()))*trackParametersWeight.transpose();
241
242
243 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
244 std::cout<<
"Gain factor obtained: "<<trackParametersWeight*(
B*(
S*
B.transpose()))*trackParametersWeight.transpose()<<std::endl;
245 std::cout<<"Resulting Gain Matrix: "<<gB<<std::endl;
246 #endif
247
248
249 Amg::MatrixX new_vrt_weight = old_vrt_weight - trackWeight *
A.transpose()*gB*A;
250
251 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
252 std::cout<<"New vertex weight obtained: "<<new_vrt_weight<<std::endl;
253 #endif
254
255
256 Amg::VectorX new_weight_matrix_times_vrt_position =old_vrt_weight_times_vrt_pos - trackWeight *
A.transpose() * gB *(trackParameters - constantTerm);
257
258
259
260 myPositionInWeightFormalism =
261 RecVertexPositions(new_weight_matrix_times_vrt_position,new_vrt_weight);
262 }
263
264
265 Amg::Vector3D newTrackMomentum =
S*
B.transpose()*trackParametersWeight*(trackParameters - constantTerm -
A*final_vrt_pos);
266
267 AmgVector(5) refTrackParameters = constantTerm + A * final_vrt_pos + B * newTrackMomentum;
268
270
272 (PosOnJetAxisAndTransformMatrix.second)*final_vrt_covariance*(PosOnJetAxisAndTransformMatrix.second).transpose();
273
274 AmgVector(5) refTrackParametersToStore(refTrackParameters);
275
276
277
278
279
280
281
282 if (refTrackParametersToStore[Trk::
phi0] >
M_PI) {
283
291 }
292
294 {
298 }
299 else if (refTrackParametersToStore[
Trk::theta]<0) {
303 }
304
305 if (refTrackParametersToStore[
Trk::theta] < 0) {
310 }
316 }
317
318
319 AmgMatrix(Eigen::Dynamic,3) partialCovariance = A.transpose() * trackParametersWeight * B *S;
320
321 AmgMatrix(Eigen::Dynamic,3) posMomentumCovariance = -final_vrt_covariance * partialCovariance;
322
323 AmgMatrix(Eigen::Dynamic,3) posMomentumCovariance_xyz = PosOnJetAxisAndTransformMatrix.second * posMomentumCovariance;
324
325
327 S + (partialCovariance.transpose()*final_vrt_covariance*partialCovariance) :
328 S + (posMomentumCovariance.transpose()*final_vrt_weightmatrix*posMomentumCovariance);
329
330
332
333 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
334 std::cout << " posMomentumCovariance " << posMomentumCovariance <<
335 " final_vrt_covariance " << final_vrt_covariance << " momentumCovariance " <<
336 momentumCovariance << std::endl;
337 #endif
338
339 fullTrkCov.block<3,3>(0,3) = posMomentumCovariance_xyz.transpose();
340 fullTrkCov.block<3,3>(3,0) = posMomentumCovariance_xyz;
341 fullTrkCov.block<3,3>(0,0) = final_vrt_covariance_xyz;
342 fullTrkCov.block<3,3>(3,3) = momentumCovariance;
343
345 trJac(4,5) = 1.;
346 trJac(3,4) = 1.;
347 trJac(2,3) = 1.;
348
349
350 trJac(0,0) = - sin(refTrackParametersToStore[Trk::
phi0]);
351 trJac(0,1) = cos(refTrackParametersToStore[Trk::
phi0]);
352
353
354 trJac(1,0) = -cos(refTrackParametersToStore[Trk::
phi0])/tan(refTrackParametersToStore[Trk::
theta]);
355 trJac(1,1) = -sin(refTrackParametersToStore[Trk::
phi0])/tan(refTrackParametersToStore[Trk::
theta]);
356 trJac(1,2) = 1.;
357
359 Trk::PerigeeSurface pSurf(trk->linearizationPoint());
361 refTrackParametersToStore[1],
362 refTrackParametersToStore[2],
363 refTrackParametersToStore[3],
364 refTrackParametersToStore[4],
365 pSurf,
366 std::move(perFullTrkCov));
367
368
369 (*TracksIter)->setPerigeeAtVertex(refittedPerigee);
370 }
371
372
373
374 AmgVector(5) paramDifference = trackParameters - refTrackParameters;
375
376 track_compatibility_chi2 +=
377 (paramDifference.transpose()*trackParametersWeight*paramDifference)(0,0)*trackWeight;
378 track_ndf+=2.;
379 ATH_MSG_DEBUG (
"new chi2 with compatibility of the tracks to the vertices " <<
380 track_compatibility_chi2 << " new ndf: " << track_ndf);
381 }
382
384 track_ndf-=1.;
385 }
386 if (!doFastUpdate) {
387
388 const Amg::VectorX & vrt_removed_tracks_weight_times_vrt_pos =
389 myPositionInWeightFormalism.position();
391 myPositionInWeightFormalism.covariancePosition();
392
393 Amg::MatrixX vrt_removed_tracks_covariance = vrt_removed_tracks_weight;
394
395
396
397 vrt_removed_tracks_covariance=(vrt_removed_tracks_covariance.transpose().eval()+vrt_removed_tracks_covariance)/2.0;
398
399 try
400 {
401 m_Updator->smartInvert(vrt_removed_tracks_covariance);
402 }
403 catch (std::string
a)
404 {
406 vrt_removed_tracks_covariance=vrt_removed_tracks_weight.inverse().eval();
407 }
408
409 const Amg::VectorX vrt_removed_tracks_pos=vrt_removed_tracks_covariance*vrt_removed_tracks_weight_times_vrt_pos;
410
411 #ifdef KalmanVertexOnJetAxisSmoother_DEBUG
412 std::cout << " final_vrt_pos " << final_vrt_pos << " vrt_removed_tracks_pos " << vrt_removed_tracks_pos << std::endl;
413 std::cout << " weight removed tracks " << vrt_removed_tracks_weight << std::endl;
414 #endif
415
416 track_compatibility_chi2 +=
417 ((final_vrt_pos-vrt_removed_tracks_pos).transpose()*vrt_removed_tracks_weight*(final_vrt_pos-vrt_removed_tracks_pos))(0,0);
418
419
420 ATH_MSG_DEBUG (
" new chi2 with the vertex residual" << track_compatibility_chi2 <<
421 " new ndf: " << track_ndf);
422 }
423 vertexToSmooth->setFitQuality(
FitQuality(track_compatibility_chi2,track_ndf));
424#ifdef KalmanVertexOnJetAxisSmoother_DEBUG
425 std::cout << " vertexToSmooth pointer is " << vertexToSmooth << std::endl;
426#endif
427
428 }
#define ATH_MSG_WARNING(x)
#define AmgSymMatrix(dim)
#define AmgMatrix(rows, cols)
AmgSymMatrix(3) m_initial_cov_momentum
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
Eigen::Matrix< double, 3, 1 > Vector3D
Eigen::Matrix< double, Eigen::Dynamic, 1 > VectorX
Dynamic Vector - dynamic allocation.
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
ParametersBase< TrackParametersDim, Charged > TrackParameters