CMS 3D CMS Logo

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