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  if (hit->det() && hit->geographicalId()<1000U) std::cout << "Problem 0 det id for " << typeid(*hit).name() << ' ' << hit->det()->geographicalId() << std::endl;
86  if (hit->isValid() && hit->geographicalId()<1000U) std::cout << "Problem 0 det id for " << typeid(*hit).name() << ' ' << hit->det()->geographicalId() << std::endl;
87 
88 
89  if (hitcounter != avtm.size())//no propagation needed for first smoothed (==last fitted) hit
90  predTsos = usePropagator->propagate( currTsos, *(hit->surface()) );
91 
92  if unlikely(!predTsos.isValid()) {
93  LogDebug("TrackFitters") << "KFTrajectorySmoother: predicted tsos not valid!";
94  if( myTraj.foundHits() >= minHits_ ) {
95  LogDebug("TrackFitters") << " breaking trajectory" << "\n";
96  } else {
97  LogDebug("TrackFitters") << " killing trajectory" << "\n";
98  return Trajectory();
99  }
100  break;
101  }
102 
103  if(hit->isValid()) {
104 
105 #ifdef EDM_ML_DEBUG
106  LogDebug("TrackFitters")
107  << "----------------- HIT #" << hitcounter << " (VALID)-----------------------\n"
108  << "HIT IS AT R " << hit->globalPosition().perp() << "\n"
109  << "HIT IS AT Z " << hit->globalPosition().z() << "\n"
110  << "HIT IS AT Phi " << hit->globalPosition().phi() << "\n"
111  << "HIT IS AT Loc " << hit->localPosition() << "\n"
112  << "WITH LocError " << hit->localPositionError() << "\n"
113  << "HIT IS AT Glo " << hit->globalPosition() << "\n"
114  << "SURFACE POSITION: " << hit->surface()->position() << "\n"
115  << "SURFACE ROTATION: " << hit->surface()->rotation() << "\n"
116  << "hit geographicalId=" << hit->geographicalId().rawId();
117 
118  DetId hitId = hit->geographicalId();
119 
120  if(hitId.det() == DetId::Tracker) {
121  if (hitId.subdetId() == StripSubdetector::TIB )
122  LogTrace("TrackFitters") << " I am TIB " << TIBDetId(hitId).layer();
123  else if (hitId.subdetId() == StripSubdetector::TOB )
124  LogTrace("TrackFitters") << " I am TOB " << TOBDetId(hitId).layer();
125  else if (hitId.subdetId() == StripSubdetector::TEC )
126  LogTrace("TrackFitters") << " I am TEC " << TECDetId(hitId).wheel();
127  else if (hitId.subdetId() == StripSubdetector::TID )
128  LogTrace("TrackFitters") << " I am TID " << TIDDetId(hitId).wheel();
129  else if (hitId.subdetId() == (int) PixelSubdetector::PixelBarrel )
130  LogTrace("TrackFitters") << " I am PixBar " << PXBDetId(hitId).layer();
131  else if (hitId.subdetId() == (int) PixelSubdetector::PixelEndcap )
132  LogTrace("TrackFitters") << " I am PixFwd " << PXFDetId(hitId).disk();
133  else
134  LogTrace("TrackFitters") << " UNKNOWN TRACKER HIT TYPE ";
135  }
136  else if(hitId.det() == DetId::Muon) {
137  if(hitId.subdetId() == MuonSubdetId::DT)
138  LogTrace("TrackFitters") << " I am DT " << DTWireId(hitId);
139  else if (hitId.subdetId() == MuonSubdetId::CSC )
140  LogTrace("TrackFitters") << " I am CSC " << CSCDetId(hitId);
141  else if (hitId.subdetId() == MuonSubdetId::RPC )
142  LogTrace("TrackFitters") << " I am RPC " << RPCDetId(hitId);
143  else
144  LogTrace("TrackFitters") << " UNKNOWN MUON HIT TYPE ";
145  }
146  else
147  LogTrace("TrackFitters") << " UNKNOWN HIT TYPE ";
148 #endif //EDM_ML_DEBUG
149 
150 
151 
152  TSOS combTsos,smooTsos;
153 
154  //3 different possibilities to calculate smoothed state:
155  //1: update combined predictions with hit
156  //2: combine fwd-prediction with bwd-filter
157  //3: combine bwd-prediction with fwd-filter
158 
159  //combTsos is the predicted state with N-1 hits information. this means:
160  //forward predicted state for first smoothed (last fitted) hit
161  //backward predicted state for last smoothed (first fitted) hit
162  //combination of forward and backward predictions for other hits
163  if (hitcounter == avtm.size()) combTsos = itm->forwardPredictedState();
164  else if (hitcounter == 1) combTsos = predTsos;
165  else combTsos = combiner(predTsos, itm->forwardPredictedState());
166 
167  if unlikely(!combTsos.isValid()) {
168  LogDebug("TrackFitters") <<
169  "KFTrajectorySmoother: combined tsos not valid!\n" <<
170  "pred Tsos pos: " << predTsos.globalPosition() << "\n" <<
171  "pred Tsos mom: " << predTsos.globalMomentum() << "\n" <<
172  "TrackingRecHit: " << hit->surface()->toGlobal(hit->localPosition()) << "\n" ;
173  if( myTraj.foundHits() >= minHits_ ) {
174  LogDebug("TrackFitters") << " breaking trajectory" << "\n";
175  } else {
176  LogDebug("TrackFitters") << " killing trajectory" << "\n";
177  return Trajectory();
178  }
179  break;
180  }
181 
182  assert(hit->geographicalId()!=0U);
183  assert(hit->surface()!=nullptr);
184  assert( (!(hit)->canImproveWithTrack()) | (nullptr!=theHitCloner));
185  assert( (!(hit)->canImproveWithTrack()) | (nullptr!=dynamic_cast<BaseTrackerRecHit const*>(hit.get())));
186  auto preciseHit = theHitCloner->makeShared(hit,combTsos);
187  assert(preciseHit->isValid());
188  assert(preciseHit->geographicalId()!=0U);
189  assert(preciseHit->surface()!=nullptr);
190 
191  if unlikely(!preciseHit->isValid()){
192  LogTrace("TrackFitters") << "THE Precise HIT IS NOT VALID: using currTsos = predTsos" << "\n";
193  currTsos = predTsos;
194  myTraj.push(TM(predTsos, hit, 0, theGeometry->idToLayer(hit->geographicalId()) ));
195  }else{
196  LogTrace("TrackFitters") << "THE Precise HIT IS VALID: updating currTsos" << "\n";
197 
198  //update backward predicted tsos with the hit
199  currTsos = updator()->update(predTsos, *preciseHit);
200  if unlikely(!currTsos.isValid()) {
201  currTsos = predTsos;
202  edm::LogWarning("KFSmoother_UpdateFailed") <<
203  "Failed updating state with hit. Rolling back to non-updated state.\n" <<
204  "State: " << predTsos <<
205  "Hit local pos: " << hit->localPosition() << "\n" <<
206  "Hit local err: " << hit->localPositionError() << "\n" <<
207  "Hit global pos: " << hit->globalPosition() << "\n" <<
208  "Hit global err: " << hit->globalPositionError().matrix() <<
209  "\n";
210  }
211 
212  //smooTsos updates the N-1 hits prediction with the hit
213  if (hitcounter == avtm.size()) smooTsos = itm->updatedState();
214  else if (hitcounter == 1) smooTsos = currTsos;
215  else smooTsos = combiner(itm->forwardPredictedState(), currTsos);
216 
217  if unlikely(!smooTsos.isValid()) {
218  LogDebug("TrackFitters") << "KFTrajectorySmoother: smoothed tsos not valid!";
219  if( myTraj.foundHits() >= minHits_ ) {
220  LogDebug("TrackFitters") << " breaking trajectory" << "\n";
221  } else {
222  LogDebug("TrackFitters") << " killing trajectory" << "\n";
223  return Trajectory();
224  }
225  break;
226  }
227 
228  double estimate;
229  if (hitcounter != avtm.size()) estimate = estimator()->estimate(combTsos, *preciseHit ).second;//correct?
230  else estimate = itm->estimate();
231 
232  LogTrace("TrackFitters")
233  << "predTsos !" << "\n"
234  << predTsos << "\n"
235  << "currTsos !" << "\n"
236  << currTsos << "\n"
237  << "smooTsos !" << "\n"
238  << smooTsos << "\n"
239  << "smoothing estimate (with combTSOS)=" << estimate << "\n"
240  << "filtering estimate=" << itm->estimate() << "\n";
241 
242  //check for valid hits with no det (refitter with constraints)
243  if (preciseHit->det()) myTraj.push(TM(itm->forwardPredictedState(),
244  predTsos,
245  smooTsos,
246  preciseHit,
247  estimate,
248  theGeometry->idToLayer(preciseHit->geographicalId()) ),
249  estimator()->estimate(predTsos,*preciseHit).second);
250  else myTraj.push(TM(itm->forwardPredictedState(),
251  predTsos,
252  smooTsos,
253  preciseHit,
254  estimate),
255  estimator()->estimate(predTsos,*preciseHit).second);
256  //itm->estimate());
257  }
258  } else {
259  LogDebug("TrackFitters")
260  << "----------------- HIT #" << hitcounter << " (INVALID)-----------------------";
261 
262  //no update
263  currTsos = predTsos;
264  TSOS combTsos;
265  if (hitcounter == avtm.size()) combTsos = itm->forwardPredictedState();
266  else if (hitcounter == 1) combTsos = predTsos;
267  else combTsos = combiner(predTsos, itm->forwardPredictedState());
268 
269  if unlikely(!combTsos.isValid()) {
270  LogDebug("TrackFitters") <<
271  "KFTrajectorySmoother: combined tsos not valid!";
272  return Trajectory();
273  }
274  assert( (hit->det()==nullptr) || hit->geographicalId()!=0U);
275  if (hit->det())
276  myTraj.push(TM(itm->forwardPredictedState(),
277  predTsos,
278  combTsos,
279  hit,
280  0,
281  theGeometry->idToLayer(hit->geographicalId()) ));
282  else myTraj.push(TM(itm->forwardPredictedState(),
283  predTsos,
284  combTsos,
285  hit,
286  0));
287 
288  }
289  } // for loop
290 
291  return ret;
292 
293 }
#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:20
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
tuple cout
Definition: gather_cfg.py:121
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