File indexing completed on 2024-04-06 12:29:00
0001 #include "TrajectoryToResiduals.h"
0002 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0003 #include "TrackingTools/TrackFitters/interface/TrajectoryStateCombiner.h"
0004
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006
0007 reco::TrackResiduals trajectoryToResiduals(const Trajectory &trajectory) {
0008 reco::TrackResiduals residuals;
0009 residuals.resize(trajectory.measurements().size());
0010 int i_residual = 0;
0011 Trajectory::DataContainer::const_iterator i_fwd = trajectory.measurements().begin();
0012 Trajectory::DataContainer::const_reverse_iterator i_bwd = trajectory.measurements().rbegin();
0013 Trajectory::DataContainer::const_iterator i_end = trajectory.measurements().end();
0014 Trajectory::DataContainer::const_reverse_iterator i_rend = trajectory.measurements().rend();
0015 bool forward = trajectory.direction() == alongMomentum;
0016 for (; forward ? i_fwd != i_end : i_bwd != i_rend; ++i_fwd, ++i_bwd, ++i_residual) {
0017 const TrajectoryMeasurement *i = forward ? &*i_fwd : &*i_bwd;
0018 if (!i->recHit()->isValid() || i->recHit()->det() == nullptr)
0019 continue;
0020 TrajectoryStateCombiner combine;
0021 if (!i->forwardPredictedState().isValid() || !i->backwardPredictedState().isValid()) {
0022 edm::LogError("InvalideState") << "one of the step is invalid";
0023 continue;
0024 }
0025
0026 TrajectoryStateOnSurface &&combo = combine(i->forwardPredictedState(), i->backwardPredictedState());
0027
0028 if (!combo.isValid()) {
0029 edm::LogError("InvalideState") << "the combined state is invalid";
0030 continue;
0031 }
0032
0033 LocalPoint &&combo_localpos = combo.localPosition();
0034 LocalError &&combo_localerr = combo.localError().positionError();
0035 LocalPoint &&dethit_localpos = i->recHit()->localPosition();
0036 LocalError &&dethit_localerr = i->recHit()->localPositionError();
0037 auto const &error_including_alignment = dethit_localerr;
0038 {
0039 auto x = (dethit_localpos.x() - combo_localpos.x());
0040 auto y = (dethit_localpos.y() - combo_localpos.y());
0041 residuals.setResidualXY(i_residual, x, y);
0042 }
0043 {
0044 auto x =
0045 (dethit_localpos.x() - combo_localpos.x()) / std::sqrt(error_including_alignment.xx() + combo_localerr.xx());
0046 auto y =
0047 (dethit_localpos.y() - combo_localpos.y()) / std::sqrt(error_including_alignment.yy() + combo_localerr.yy());
0048 residuals.setPullXY(i_residual, x, y);
0049 }
0050 }
0051 return residuals;
0052 }