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