@@ -84,6 +84,27 @@ struct two_filters_smoother {
8484 const matrix_type<e_bound_size, 1 >& predicted_vec =
8585 bound_params.vector ();
8686
87+ matrix_type<D, e_bound_size> H =
88+ measurements.at (trk_state.measurement_index ())
89+ .subs .template projector <D>();
90+ if (dim == 1 ) {
91+ getter::element (H, 1u , 0u ) = 0 .f ;
92+ getter::element (H, 1u , 1u ) = 0 .f ;
93+ }
94+
95+ // Spatial resolution (Measurement covariance)
96+ matrix_type<D, D> V;
97+ edm::get_measurement_covariance<algebra_t >(
98+ measurements.at (trk_state.measurement_index ()), V);
99+ if (dim == 1 ) {
100+ getter::element (V, 1u , 1u ) = 1 .f ;
101+ }
102+
103+ TRACCC_DEBUG_HOST (" Measurement position: " << meas_local);
104+ TRACCC_DEBUG_HOST (" Measurement variance:\n " << V);
105+ TRACCC_DEBUG_HOST (" Predicted residual: " << meas_local -
106+ H * predicted_vec);
107+
87108 // Predicted covaraince of bound track parameters
88109 const matrix_type<e_bound_size, e_bound_size>& predicted_cov =
89110 bound_params.covariance ();
@@ -134,29 +155,8 @@ struct two_filters_smoother {
134155 // Wrap the phi and theta angles in their valid ranges
135156 normalize_angles (trk_state.smoothed_params ());
136157
137- matrix_type<D, e_bound_size> H =
138- measurements.at (trk_state.measurement_index ())
139- .subs .template projector <D>();
140- if (dim == 1 ) {
141- getter::element (H, 1u , 0u ) = 0 .f ;
142- getter::element (H, 1u , 1u ) = 0 .f ;
143- }
144-
145158 const matrix_type<D, 1 > residual_smt = meas_local - H * smoothed_vec;
146159
147- // Spatial resolution (Measurement covariance)
148- matrix_type<D, D> V;
149- edm::get_measurement_covariance<algebra_t >(
150- measurements.at (trk_state.measurement_index ()), V);
151- if (dim == 1 ) {
152- getter::element (V, 1u , 1u ) = 1 .f ;
153- }
154-
155- TRACCC_DEBUG_HOST (" Measurement position: " << meas_local);
156- TRACCC_DEBUG_HOST (" Measurement variance:\n " << V);
157- TRACCC_DEBUG_HOST (" Predicted residual: " << meas_local -
158- H * predicted_vec);
159-
160160 // Eq (3.39) of "Pattern Recognition, Tracking and Vertex
161161 // Reconstruction in Particle Detectors"
162162 const matrix_type<D, D> R_smt =
@@ -204,7 +204,6 @@ struct two_filters_smoother {
204204
205205 const auto I66 =
206206 matrix::identity<matrix_type<e_bound_size, e_bound_size>>();
207- const auto I_m = matrix::identity<matrix_type<D, D>>();
208207
209208 const matrix_type<e_bound_size, D> projected_cov =
210209 algebra::matrix::transposed_product<false , true >(predicted_cov, H);
@@ -252,39 +251,38 @@ struct two_filters_smoother {
252251
253252 bound_params.set_covariance (filtered_cov);
254253
255- // Residual between measurement and (projected) filtered vector
256- const matrix_type<D, 1 > residual = meas_local - H * filtered_vec;
257-
258- // Calculate backward chi2
259- const matrix_type<D, D> R = (I_m - H * K) * V;
260- // assert(matrix::determinant(R) != 0.f); // @TODO: This fails
261- assert (std::isfinite (matrix::determinant (R)));
262- const matrix_type<1 , 1 > chi2 =
263- algebra::matrix::transposed_product<true , false >(
264- residual, matrix::inverse (R)) *
265- residual;
266-
267- const scalar chi2_val{getter::element (chi2, 0 , 0 )};
268-
269- TRACCC_VERBOSE_HOST (" Filtered residual: " << residual);
270- TRACCC_DEBUG_HOST (" R:\n " << R);
271- TRACCC_DEBUG_HOST_DEVICE (" det(R): %f" , matrix::determinant (R));
272- TRACCC_DEBUG_HOST (" R_inv:\n " << matrix::inverse (R));
273- TRACCC_VERBOSE_HOST_DEVICE (" Chi2: %f" , chi2_val);
274-
275- if (chi2_val < 0 .f ) {
276- TRACCC_ERROR_HOST_DEVICE (" Chi2 negative: %f" , chi2_val);
277- return kalman_fitter_status::ERROR_SMOOTHER_CHI2_NEGATIVE;
278- }
279-
280- if (!std::isfinite (chi2_val)) {
281- TRACCC_ERROR_HOST_DEVICE (" Chi2 infinite" );
282- return kalman_fitter_status::ERROR_SMOOTHER_CHI2_NOT_FINITE;
254+ {
255+ // Calculate the chi square
256+ const matrix_type<D, D> R =
257+ V - (H * predicted_cov * algebra::matrix::transpose (H));
258+ // Residual between measurement and predicted vector
259+ const matrix_type<D, 1 > residual = meas_local - H * predicted_vec;
260+ const matrix_type<1 , 1 > chi2_mat =
261+ algebra::matrix::transposed_product<true , false >(
262+ residual, matrix::inverse (R)) *
263+ residual;
264+ const scalar chi2_val = getter::element (chi2_mat, 0 , 0 );
265+
266+ TRACCC_VERBOSE_HOST (" Predicted residual: " << residual);
267+ TRACCC_DEBUG_HOST (" R:\n " << R);
268+ TRACCC_DEBUG_HOST_DEVICE (" det(R): %f" , matrix::determinant (R));
269+ TRACCC_DEBUG_HOST (" R_inv:\n " << matrix::inverse (R));
270+ TRACCC_VERBOSE_HOST_DEVICE (" Chi2: %f" , chi2_val);
271+
272+ if (chi2_val < 0 .f ) {
273+ TRACCC_ERROR_HOST_DEVICE (" Chi2 negative: %f" , chi2_val);
274+ return kalman_fitter_status::ERROR_SMOOTHER_CHI2_NEGATIVE;
275+ }
276+
277+ if (!std::isfinite (chi2_val)) {
278+ TRACCC_ERROR_HOST_DEVICE (" Chi2 infinite" );
279+ return kalman_fitter_status::ERROR_SMOOTHER_CHI2_NOT_FINITE;
280+ }
281+
282+ // Set backward chi2
283+ trk_state.backward_chi2 () = chi2_val;
283284 }
284285
285- // Set backward chi2
286- trk_state.backward_chi2 () = chi2_val;
287-
288286 // Wrap the phi and theta angles in their valid ranges
289287 normalize_angles (bound_params);
290288
0 commit comments