CMS 3D CMS Logo

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