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