CMS 3D CMS Logo

KFTrajectorySmoother.cc
Go to the documentation of this file.
7 
10 
12 
14  delete theAlongPropagator;
15  delete theOppositePropagator;
16  delete theUpdator;
17  delete theEstimator;
18 }
19 
21  if (aTraj.empty())
22  return Trajectory();
23 
24  const Propagator* usePropagator = theAlongPropagator;
25  if (aTraj.direction() == alongMomentum) {
26  usePropagator = theOppositePropagator;
27  }
28 
29  const std::vector<TM>& avtm = aTraj.measurements();
30 
31 #ifdef EDM_ML_DEBUG
32  LogDebug("TrackFitters") << "KFTrajectorySmoother::trajectories starting with " << avtm.size() << " HITS\n";
33  for (unsigned int j = 0; j < avtm.size(); j++) {
34  if (avtm[j].recHit()->det())
35  LogTrace("TrackFitters") << "hit #:" << j + 1 << " rawId=" << avtm[j].recHit()->det()->geographicalId().rawId()
36  << " validity=" << avtm[j].recHit()->isValid();
37  else
38  LogTrace("TrackFitters") << "hit #:" << j + 1 << " Hit with no Det information";
39  }
40 #endif // EDM_ML_DEBUG
41 
43  bool retry = false;
44  auto start = avtm.rbegin();
45 
46  do {
47  auto hitSize = avtm.rend() - start;
48  if
49  UNLIKELY(hitSize < minHits_) {
50  LogDebug("TrackFitters") << " killing trajectory"
51  << "\n";
52  return Trajectory();
53  }
54  Trajectory ret(aTraj.seed(), usePropagator->propagationDirection());
55  Trajectory& myTraj = ret;
56  myTraj.reserve(hitSize);
57  retry = false;
58 
59  TSOS predTsos = (*start).forwardPredictedState();
61  TSOS currTsos;
62 
63  auto hitCounter = hitSize;
64  for (std::vector<TM>::const_reverse_iterator itm = start; itm != (avtm.rend()); ++itm, --hitCounter) {
66 
67  //check surface just for safety: should never be ==0 because they are skipped in the fitter
68  // if UNLIKELY(hit->det() == nullptr) continue;
69  if
70  UNLIKELY(hit->surface() == nullptr) {
71  LogDebug("TrackFitters") << " Error: invalid hit with no GeomDet attached .... skipping";
72  continue;
73  }
74 
75  if (itm != start) //no propagation needed for first smoothed (==last fitted) hit
76  predTsos = usePropagator->propagate(currTsos, *(hit->surface()));
77 
78  if
79  UNLIKELY(!predTsos.isValid()) {
80  LogDebug("TrackFitters") << "KFTrajectorySmoother: predicted tsos not valid!";
81  LogDebug("TrackFitters") << " retry with last hit removed"
82  << "\n";
83  LogDebug("TrackFitters")
84  // std::cout
85  << "tsos not valid " << currTsos.globalMomentum().perp() << ' ' << hitSize << ' ' << hitCounter << ' '
86  << int(hit->geographicalId()) << ' ' << hit->surface()->position().perp() << ' ' << hit->surface()->eta()
87  << ' ' << hit->surface()->phi() << std::endl;
88  start++;
89  retry = true;
90  break;
91  }
92 
93  if (hit->isValid()) {
94  TSOS combTsos, smooTsos;
95 
96  //3 different possibilities to calculate smoothed state:
97  //1: update combined predictions with hit
98  //2: combine fwd-prediction with bwd-filter
99  //3: combine bwd-prediction with fwd-filter
100 
101  //combTsos is the predicted state with N-1 hits information. this means:
102  //forward predicted state for first smoothed (last fitted) hit
103  //backward predicted state for last smoothed (first fitted) hit
104  //combination of forward and backward predictions for other hits
105  if (itm == start)
106  combTsos = itm->forwardPredictedState();
107  else if (hitCounter == 1)
108  combTsos = predTsos;
109  else
110  combTsos = combiner(predTsos, itm->forwardPredictedState());
111 
112  if
113  UNLIKELY(!combTsos.isValid()) {
114  LogDebug("TrackFitters") << "KFTrajectorySmoother: combined tsos not valid!\n"
115  << "pred Tsos pos: " << predTsos.globalPosition() << "\n"
116  << "pred Tsos mom: " << predTsos.globalMomentum() << "\n"
117  << "TrackingRecHit: " << hit->surface()->toGlobal(hit->localPosition()) << "\n";
118  start++;
119  retry = true;
120  break;
121  }
122 
123  assert((hit->geographicalId() != 0U) | (!hit->canImproveWithTrack()));
124  assert(hit->surface() != nullptr);
125  assert((!(hit)->canImproveWithTrack()) | (nullptr != theHitCloner));
126  assert((!(hit)->canImproveWithTrack()) | (nullptr != dynamic_cast<BaseTrackerRecHit const*>(hit.get())));
127  auto preciseHit = theHitCloner->makeShared(hit, combTsos);
128  assert(preciseHit->isValid());
129  assert((preciseHit->geographicalId() != 0U) | (!preciseHit->canImproveWithTrack()));
130  assert(preciseHit->surface() != nullptr);
131 
132  dump(*hit, hitCounter, "TrackFitters");
133 
134  if
135  UNLIKELY(!preciseHit->isValid()) {
136  LogTrace("TrackFitters") << "THE Precise HIT IS NOT VALID: using currTsos = predTsos"
137  << "\n";
138  currTsos = predTsos;
139  myTraj.push(TM(predTsos, hit, 0, theGeometry->idToLayer(hit->geographicalId())));
140  }
141  else {
142  LogTrace("TrackFitters") << "THE Precise HIT IS VALID: updating currTsos"
143  << "\n";
144 
145  //update backward predicted tsos with the hit
146  currTsos = updator()->update(predTsos, *preciseHit);
147  if
148  UNLIKELY(!currTsos.isValid()) {
149  currTsos = predTsos;
150  edm::LogWarning("KFSmoother_UpdateFailed")
151  << "Failed updating state with hit. Rolling back to non-updated state.\n"
152  << "State: " << predTsos << "Hit local pos: " << hit->localPosition() << "\n"
153  << "Hit local err: " << hit->localPositionError() << "\n"
154  << "Hit global pos: " << hit->globalPosition() << "\n"
155  << "Hit global err: " << hit->globalPositionError().matrix() << "\n";
156  }
157 
158  //smooTsos updates the N-1 hits prediction with the hit
159  if (itm == start)
160  smooTsos = itm->updatedState();
161  else if (hitCounter == 1)
162  smooTsos = currTsos;
163  else
164  smooTsos = combiner(itm->forwardPredictedState(), currTsos);
165 
166  if
167  UNLIKELY(!smooTsos.isValid()) {
168  LogDebug("TrackFitters") << "KFTrajectorySmoother: smoothed tsos not valid!";
169  start++;
170  retry = true;
171  break;
172  }
173 
174  double estimate;
175  if (itm != start)
176  estimate = estimator()->estimate(combTsos, *preciseHit).second; //correct?
177  else
178  estimate = itm->estimate();
179 
180  LogTrace("TrackFitters") << "predTsos !"
181  << "\n"
182  << predTsos << " with local position " << predTsos.localPosition() << "\n\n"
183  << "currTsos !"
184  << "\n"
185  << currTsos << "\n"
186  << " with local position " << currTsos.localPosition() << "\n\n"
187  << "smooTsos !"
188  << "\n"
189  << smooTsos << " with local position " << smooTsos.localPosition() << "\n\n"
190  << "smoothing estimate (with combTSOS)=" << estimate << "\n"
191  << "filtering estimate=" << itm->estimate() << "\n";
192 
193  //check for valid hits with no det (refitter with constraints)
194  if (preciseHit->det())
195  myTraj.push(TM(itm->forwardPredictedState(),
196  predTsos,
197  smooTsos,
198  preciseHit,
199  estimate,
200  theGeometry->idToLayer(preciseHit->geographicalId())),
201  estimator()->estimate(predTsos, *preciseHit).second);
202  else
203  myTraj.push(TM(itm->forwardPredictedState(), predTsos, smooTsos, preciseHit, estimate),
204  estimator()->estimate(predTsos, *preciseHit).second);
205  //itm->estimate());
206  }
207  } else {
208  LogDebug("TrackFitters") << "----------------- HIT #" << hitCounter << " (INVALID)-----------------------";
209 
210  //no update
211  currTsos = predTsos;
212  TSOS combTsos;
213  if (itm == start)
214  combTsos = itm->forwardPredictedState();
215  else if (hitCounter == 1)
216  combTsos = predTsos;
217  else
218  combTsos = combiner(predTsos, itm->forwardPredictedState());
219 
220  if
221  UNLIKELY(!combTsos.isValid()) {
222  LogDebug("TrackFitters") << "KFTrajectorySmoother: combined tsos not valid!";
223  return Trajectory();
224  }
225  assert((hit->det() == nullptr) || hit->geographicalId() != 0U);
226  if (hit->det())
227  myTraj.push(TM(
228  itm->forwardPredictedState(), predTsos, combTsos, hit, 0, theGeometry->idToLayer(hit->geographicalId())));
229  else
230  myTraj.push(TM(itm->forwardPredictedState(), predTsos, combTsos, hit, 0));
231  }
232  } // for loop
233 
234  if (!retry)
235  return ret;
236  } while (true);
237 
238  return Trajectory();
239 }
#define LogDebug(id)
Definition: start.py:1
bool empty() const
True if trajectory has no measurements.
Definition: Trajectory.h:233
T perp() const
Definition: PV3DBase.h:69
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:263
ret
prodAgent to be discontinued
const DetLayerGeometry * theGeometry
GlobalPoint globalPosition() const
TrackingRecHit::ConstRecHitPointer makeShared(TrackingRecHit::ConstRecHitPointer const &hit, TrajectoryStateOnSurface const &tsos) const
Definition: TkCloner.h:23
virtual TrajectoryStateOnSurface update(const TrajectoryStateOnSurface &, const TrackingRecHit &) const =0
PropagationDirection const & direction() const
Definition: Trajectory.cc:133
DataContainer const & measurements() const
Definition: Trajectory.h:178
virtual PropagationDirection propagationDirection() const final
Definition: Propagator.h:139
std::shared_ptr< TrackingRecHit const > ConstRecHitPointer
const MeasurementEstimator * estimator() const
Trajectory trajectory(const Trajectory &aTraj) const override
const MeasurementEstimator * theEstimator
const TrajectoryStateUpdator * theUpdator
TrajectoryMeasurement TM
#define LogTrace(id)
const TrajectoryStateUpdator * updator() const
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:50
GlobalVector globalMomentum() const
virtual HitReturnType estimate(const TrajectoryStateOnSurface &ts, const TrackingRecHit &hit) const =0
const Propagator * theAlongPropagator
const Propagator * theOppositePropagator
#define UNLIKELY(x)
Definition: Likely.h:21
virtual const DetLayer * idToLayer(const DetId &detId) const
TkCloner const * theHitCloner