CMS 3D CMS Logo

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