CMS 3D CMS Logo

KFTrajectorySmoother.cc
Go to the documentation of this file.
6 
9 
10 #ifdef EDM_ML_DEBUG
21 
22 namespace {
24  LogDebug("TrackFitters")
25  << "----------------- HIT #" << hitCounter << " (VALID)-----------------------\n"
26  << "HIT IS AT R " << hit->globalPosition().perp() << "\n"
27  << "HIT IS AT Z " << hit->globalPosition().z() << "\n"
28  << "HIT IS AT Phi " << hit->globalPosition().phi() << "\n"
29  << "HIT IS AT Loc " << hit->localPosition() << "\n"
30  << "WITH LocError " << hit->localPositionError() << "\n"
31  << "HIT IS AT Glo " << hit->globalPosition() << "\n"
32  << "SURFACE POSITION: " << hit->surface()->position() << "\n"
33  << "SURFACE ROTATION: " << hit->surface()->rotation() << "\n"
34  << "hit geographicalId=" << hit->geographicalId().rawId();
35 
36  DetId hitId = hit->geographicalId();
37 
38  if(hitId.det() == DetId::Tracker) {
39  if (hitId.subdetId() == StripSubdetector::TIB )
40  LogTrace("TrackFitters") << " I am TIB " << TIBDetId(hitId).layer();
41  else if (hitId.subdetId() == StripSubdetector::TOB )
42  LogTrace("TrackFitters") << " I am TOB " << TOBDetId(hitId).layer();
43  else if (hitId.subdetId() == StripSubdetector::TEC )
44  LogTrace("TrackFitters") << " I am TEC " << TECDetId(hitId).wheel();
45  else if (hitId.subdetId() == StripSubdetector::TID )
46  LogTrace("TrackFitters") << " I am TID " << TIDDetId(hitId).wheel();
47  else if (hitId.subdetId() == (int) PixelSubdetector::PixelBarrel )
48  LogTrace("TrackFitters") << " I am PixBar " << PXBDetId(hitId).layer();
49  else if (hitId.subdetId() == (int) PixelSubdetector::PixelEndcap )
50  LogTrace("TrackFitters") << " I am PixFwd " << PXFDetId(hitId).disk();
51  else
52  LogTrace("TrackFitters") << " UNKNOWN TRACKER HIT TYPE ";
53  }
54  else if(hitId.det() == DetId::Muon) {
55  if(hitId.subdetId() == MuonSubdetId::DT)
56  LogTrace("TrackFitters") << " I am DT " << DTWireId(hitId);
57  else if (hitId.subdetId() == MuonSubdetId::CSC )
58  LogTrace("TrackFitters") << " I am CSC " << CSCDetId(hitId);
59  else if (hitId.subdetId() == MuonSubdetId::RPC )
60  LogTrace("TrackFitters") << " I am RPC " << RPCDetId(hitId);
61  else
62  LogTrace("TrackFitters") << " UNKNOWN MUON HIT TYPE ";
63  }
64  else
65  LogTrace("TrackFitters") << " UNKNOWN HIT TYPE ";
66  }
67 
68 }
69 #else
70 namespace {
72 }
73 #endif
74 
76 
77 
79 
80  delete theAlongPropagator;
81  delete theOppositePropagator;
82  delete theUpdator;
83  delete theEstimator;
84 
85 }
86 
89 
90  if(aTraj.empty()) return Trajectory();
91 
92  const Propagator* usePropagator = theAlongPropagator;
93  if(aTraj.direction() == alongMomentum) {
94  usePropagator = theOppositePropagator;
95  }
96 
97  const std::vector<TM> & avtm = aTraj.measurements();
98 
99 
100 
101 
102 #ifdef EDM_ML_DEBUG
103  LogDebug("TrackFitters") << "KFTrajectorySmoother::trajectories starting with " << avtm.size() << " HITS\n";
104  for (unsigned int j=0;j<avtm.size();j++) {
105  if (avtm[j].recHit()->det())
106  LogTrace("TrackFitters") << "hit #:" << j+1 << " rawId=" << avtm[j].recHit()->det()->geographicalId().rawId()
107  << " validity=" << avtm[j].recHit()->isValid();
108  else
109  LogTrace("TrackFitters") << "hit #:" << j+1 << " Hit with no Det information";
110  }
111 #endif // EDM_ML_DEBUG
112 
113 
114 
116  bool retry=false;
117  auto start = avtm.rbegin();
118 
119  do {
120  auto hitSize = avtm.rend()-start;
121  if unlikely( hitSize < minHits_ ) {
122  LogDebug("TrackFitters") << " killing trajectory" << "\n";
123  return Trajectory();
124  }
125  Trajectory ret(aTraj.seed(), usePropagator->propagationDirection());
126  Trajectory & myTraj = ret;
127  myTraj.reserve(hitSize);
128  retry=false;
129 
130  TSOS predTsos = (*start).forwardPredictedState();
132  TSOS currTsos;
133 
134  auto hitCounter = hitSize;
135  for(std::vector<TM>::const_reverse_iterator itm = start; itm != (avtm.rend()); ++itm,--hitCounter) {
136 
138 
139  //check surface just for safety: should never be ==0 because they are skipped in the fitter
140  // if unlikely(hit->det() == nullptr) continue;
141  if unlikely( hit->surface()==nullptr ) {
142  LogDebug("TrackFitters") << " Error: invalid hit with no GeomDet attached .... skipping";
143  continue;
144  }
145 
146 
147  if (itm != start)//no propagation needed for first smoothed (==last fitted) hit
148  predTsos = usePropagator->propagate( currTsos, *(hit->surface()) );
149 
150  if unlikely(!predTsos.isValid()) {
151  LogDebug("TrackFitters") << "KFTrajectorySmoother: predicted tsos not valid!";
152  LogDebug("TrackFitters") << " retry with last hit removed" << "\n";
153  LogDebug("TrackFitters")
154  // std::cout
155  << "tsos not valid " << currTsos.globalMomentum().perp() << ' '
156  << hitSize << ' ' << hitCounter << ' ' << int(hit->geographicalId()) << ' '
157  << hit->surface()->position().perp() << ' ' << hit->surface()->eta() << ' ' << hit->surface()->phi() << std::endl;
158  start++;
159  retry = true;
160  break;
161  }
162 
163  if(hit->isValid()) {
164 
165  TSOS combTsos,smooTsos;
166 
167  //3 different possibilities to calculate smoothed state:
168  //1: update combined predictions with hit
169  //2: combine fwd-prediction with bwd-filter
170  //3: combine bwd-prediction with fwd-filter
171 
172  //combTsos is the predicted state with N-1 hits information. this means:
173  //forward predicted state for first smoothed (last fitted) hit
174  //backward predicted state for last smoothed (first fitted) hit
175  //combination of forward and backward predictions for other hits
176  if (itm == start) combTsos = itm->forwardPredictedState();
177  else if (hitCounter == 1) combTsos = predTsos;
178  else combTsos = combiner(predTsos, itm->forwardPredictedState());
179 
180  if unlikely(!combTsos.isValid()) {
181  LogDebug("TrackFitters") <<
182  "KFTrajectorySmoother: combined tsos not valid!\n" <<
183  "pred Tsos pos: " << predTsos.globalPosition() << "\n" <<
184  "pred Tsos mom: " << predTsos.globalMomentum() << "\n" <<
185  "TrackingRecHit: " << hit->surface()->toGlobal(hit->localPosition()) << "\n" ;
186  start++;
187  retry = true;
188  break;
189  }
190 
191  assert( (hit->geographicalId()!=0U) | (!hit->canImproveWithTrack()) );
192  assert(hit->surface()!=nullptr);
193  assert( (!(hit)->canImproveWithTrack()) | (nullptr!=theHitCloner));
194  assert( (!(hit)->canImproveWithTrack()) | (nullptr!=dynamic_cast<BaseTrackerRecHit const*>(hit.get())));
195  auto preciseHit = theHitCloner->makeShared(hit,combTsos);
196  assert(preciseHit->isValid());
197  assert( (preciseHit->geographicalId()!=0U) | (!preciseHit->canImproveWithTrack()) );
198  assert(preciseHit->surface()!=nullptr);
199 
200  dump(hit,hitCounter);
201 
202  if unlikely(!preciseHit->isValid()){
203  LogTrace("TrackFitters") << "THE Precise HIT IS NOT VALID: using currTsos = predTsos" << "\n";
204  currTsos = predTsos;
205  myTraj.push(TM(predTsos, hit, 0, theGeometry->idToLayer(hit->geographicalId()) ));
206  }else{
207  LogTrace("TrackFitters") << "THE Precise HIT IS VALID: updating currTsos" << "\n";
208 
209  //update backward predicted tsos with the hit
210  currTsos = updator()->update(predTsos, *preciseHit);
211  if unlikely(!currTsos.isValid()) {
212  currTsos = predTsos;
213  edm::LogWarning("KFSmoother_UpdateFailed") <<
214  "Failed updating state with hit. Rolling back to non-updated state.\n" <<
215  "State: " << predTsos <<
216  "Hit local pos: " << hit->localPosition() << "\n" <<
217  "Hit local err: " << hit->localPositionError() << "\n" <<
218  "Hit global pos: " << hit->globalPosition() << "\n" <<
219  "Hit global err: " << hit->globalPositionError().matrix() <<
220  "\n";
221  }
222 
223  //smooTsos updates the N-1 hits prediction with the hit
224  if (itm == start) smooTsos = itm->updatedState();
225  else if (hitCounter == 1) smooTsos = currTsos;
226  else smooTsos = combiner(itm->forwardPredictedState(), currTsos);
227 
228  if unlikely(!smooTsos.isValid()) {
229  LogDebug("TrackFitters") << "KFTrajectorySmoother: smoothed tsos not valid!";
230  start++;
231  retry = true;
232  break;
233  }
234 
235  double estimate;
236  if (itm != start) estimate = estimator()->estimate(combTsos, *preciseHit ).second;//correct?
237  else estimate = itm->estimate();
238 
239  LogTrace("TrackFitters")
240  << "predTsos !" << "\n"
241  << predTsos
242  <<" with local position " << predTsos.localPosition() << "\n\n"
243  << "currTsos !" << "\n"
244  << currTsos << "\n"
245  <<" with local position " << currTsos.localPosition() << "\n\n"
246  << "smooTsos !" << "\n"
247  << smooTsos
248  <<" with local position " << smooTsos.localPosition() << "\n\n"
249  << "smoothing estimate (with combTSOS)=" << estimate << "\n"
250  << "filtering estimate=" << itm->estimate() << "\n";
251 
252  //check for valid hits with no det (refitter with constraints)
253  if (preciseHit->det()) myTraj.push(TM(itm->forwardPredictedState(),
254  predTsos,
255  smooTsos,
256  preciseHit,
257  estimate,
258  theGeometry->idToLayer(preciseHit->geographicalId()) ),
259  estimator()->estimate(predTsos,*preciseHit).second);
260  else myTraj.push(TM(itm->forwardPredictedState(),
261  predTsos,
262  smooTsos,
263  preciseHit,
264  estimate),
265  estimator()->estimate(predTsos,*preciseHit).second);
266  //itm->estimate());
267  }
268  } else {
269  LogDebug("TrackFitters")
270  << "----------------- HIT #" << hitCounter << " (INVALID)-----------------------";
271 
272  //no update
273  currTsos = predTsos;
274  TSOS combTsos;
275  if (itm == start) combTsos = itm->forwardPredictedState();
276  else if (hitCounter == 1) combTsos = predTsos;
277  else combTsos = combiner(predTsos, itm->forwardPredictedState());
278 
279  if unlikely(!combTsos.isValid()) {
280  LogDebug("TrackFitters") <<
281  "KFTrajectorySmoother: combined tsos not valid!";
282  return Trajectory();
283  }
284  assert( (hit->det()==nullptr) || hit->geographicalId()!=0U);
285  if (hit->det())
286  myTraj.push(TM(itm->forwardPredictedState(),
287  predTsos,
288  combTsos,
289  hit,
290  0,
291  theGeometry->idToLayer(hit->geographicalId()) ));
292  else myTraj.push(TM(itm->forwardPredictedState(),
293  predTsos,
294  combTsos,
295  hit,
296  0));
297 
298  }
299  } // for loop
300 
301  if (!retry) return ret;
302  } while(true);
303 
304  return Trajectory();
305 
306 }
#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
unsigned int layer() const
layer id
Definition: TOBDetId.h:39
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
unsigned int layer() const
layer id
Definition: PXBDetId.h:35
PropagationDirection const & direction() const
Definition: Trajectory.cc:140
#define unlikely(x)
DataContainer const & measurements() const
Definition: Trajectory.h:196
static const int CSC
Definition: MuonSubdetId.h:13
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
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
#define LogTrace(id)
unsigned int disk() const
disk id
Definition: PXFDetId.h:43
Definition: DetId.h:18
const TrajectoryStateUpdator * updator() const
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:53
unsigned int wheel() const
wheel id
Definition: TECDetId.h:52
unsigned int layer() const
layer id
Definition: TIBDetId.h:41
GlobalVector globalMomentum() const
static const int RPC
Definition: MuonSubdetId.h:14
virtual HitReturnType estimate(const TrajectoryStateOnSurface &ts, const TrackingRecHit &hit) const =0
const Propagator * theAlongPropagator
static const int DT
Definition: MuonSubdetId.h:12
const Propagator * theOppositePropagator
virtual const DetLayer * idToLayer(const DetId &detId) const
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
TkCloner const * theHitCloner
unsigned int wheel() const
wheel id
Definition: TIDDetId.h:50