CMS 3D CMS Logo

KFTrajectoryFitter.cc
Go to the documentation of this file.
9 
10 #ifdef EDM_ML_DEBUG
11 
24 
25 namespace {
26  void dump(TrackingRecHit const & hit, int hitcounter) {
27  if (hit.isValid()) {
28  LogTrace("TrackFitters")<< " ----------------- HIT #" << hitcounter << " (VALID)-----------------------\n"
29  << " HIT IS AT R " << hit.globalPosition().perp() << "\n"
30  << " HIT IS AT Z " << hit.globalPosition().z() << "\n"
31  << " HIT IS AT Phi " << hit.globalPosition().phi() << "\n"
32  << " HIT IS AT Loc " << hit.localPosition() << "\n"
33  << " WITH LocError " << hit.localPositionError() << "\n"
34  << " HIT IS AT Glo " << hit.globalPosition() << "\n"
35  << "SURFACE POSITION" << "\n"
36  << hit.surface()->position()<<"\n"
37  << "SURFACE ROTATION" << "\n"
38  << hit.surface()->rotation()
39  << "dimension " << hit.dimension();
40 
41  DetId hitId = hit.geographicalId();
42 
43  LogDebug("TrackFitters") << " hit det=" << hitId.rawId();
44 
45  if(hitId.det() == DetId::Tracker) {
46  if (hitId.subdetId() == StripSubdetector::TIB )
47  LogDebug("TrackFitters") << " I am TIB " << TIBDetId(hitId).layer();
48  else if (hitId.subdetId() == StripSubdetector::TOB )
49  LogDebug("TrackFitters") << " I am TOB " << TOBDetId(hitId).layer();
50  else if (hitId.subdetId() == StripSubdetector::TEC )
51  LogDebug("TrackFitters") << " I am TEC " << TECDetId(hitId).wheel();
52  else if (hitId.subdetId() == StripSubdetector::TID )
53  LogDebug("TrackFitters") << " I am TID " << TIDDetId(hitId).wheel();
54  else if (hitId.subdetId() == (int) PixelSubdetector::PixelBarrel )
55  LogDebug("TrackFitters") << " I am PixBar " << PXBDetId(hitId).layer();
56  else if (hitId.subdetId() == (int) PixelSubdetector::PixelEndcap )
57  LogDebug("TrackFitters") << " I am PixFwd " << PXFDetId(hitId).disk();
58  else
59  LogDebug("TrackFitters") << " UNKNOWN TRACKER HIT TYPE ";
60  }
61  else if(hitId.det() == DetId::Muon) {
62  if(hitId.subdetId() == MuonSubdetId::DT)
63  LogDebug("TrackFitters") << " I am DT " << DTWireId(hitId);
64  else if (hitId.subdetId() == MuonSubdetId::CSC )
65  LogDebug("TrackFitters") << " I am CSC " << CSCDetId(hitId);
66  else if (hitId.subdetId() == MuonSubdetId::RPC )
67  LogDebug("TrackFitters") << " I am RPC " << RPCDetId(hitId);
68  else if (hitId.subdetId() == MuonSubdetId::GEM )
69  LogDebug("TrackFitters") << " I am GEM " << GEMDetId(hitId);
70 
71  else if (hitId.subdetId() == MuonSubdetId::ME0 )
72  LogDebug("TrackFitters") << " I am ME0 " << ME0DetId(hitId);
73  else
74  LogDebug("TrackFitters") << " UNKNOWN MUON HIT TYPE ";
75  }
76  else
77  LogDebug("TrackFitters") << " UNKNOWN HIT TYPE ";
78 
79  } else {
80  LogDebug("TrackFitters")
81  << " ----------------- INVALID HIT #" << hitcounter << " -----------------------";
82  }
83  }
84 }
85 #else
86 namespace {
87  inline void dump(TrackingRecHit const &, int) {}
88 }
89 #endif
90 
92 
94 
95  if(aTraj.empty()) return Trajectory();
96 
97  TM firstTM = aTraj.firstMeasurement();
98  TSOS firstTsos = TrajectoryStateWithArbitraryError()(firstTM.updatedState());
99 
100  return fitOne(aTraj.seed(), aTraj.recHits(), firstTsos,type);
101 }
102 
104  const RecHitContainer&, fitType) const{
105 
106  throw cms::Exception("TrackFitters",
107  "KFTrajectoryFitter::fit(TrajectorySeed, <TransientTrackingRecHit>) not implemented");
108 
109  return Trajectory();
110 }
111 
113  const RecHitContainer& hits,
114  const TSOS& firstPredTsos,fitType) const
115 {
116  if(hits.empty()) return Trajectory();
117 
118 
119  if unlikely(aSeed.direction() == anyDirection)
120  throw cms::Exception("KFTrajectoryFitter","TrajectorySeed::direction() requested but not set");
121 
122  std::unique_ptr<Propagator> p_cloned = SetPropagationDirection(*thePropagator,
123  aSeed.direction());
124 
125 #ifdef EDM_ML_DEBUG
126  LogDebug("TrackFitters")
127  <<" +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
128  <<" KFTrajectoryFitter::fit starting with " << hits.size() <<" HITS";
129 
130  for (unsigned int j=0;j<hits.size();j++) {
131  if (hits[j]->det())
132  LogTrace("TrackFitters") << "hit #:" << j+1 << " rawId=" << hits[j]->det()->geographicalId().rawId()
133  << " validity=" << hits[j]->isValid();
134  else
135  LogTrace("TrackFitters") << "hit #:" << j+1 << " Hit with no Det information";
136  }
137  LogTrace("TrackFitters") << " INITIAL STATE "<< firstPredTsos;
138 #endif
139 
140  Trajectory ret(aSeed, p_cloned->propagationDirection());
141  Trajectory & myTraj = ret;
142  myTraj.reserve(hits.size());
143 
144  TSOS predTsos(firstPredTsos);
145  TSOS currTsos;
146 
147  int hitcounter = 0;
148  for(auto ihit : hits) {
149  ++hitcounter;
150 
151  const TransientTrackingRecHit & hit = (*ihit);
152 
153  // if unlikely(hit.det() == nullptr) continue;
154 
155  if unlikely( (!hit.isValid()) && hit.surface() == nullptr) {
156  LogDebug("TrackFitters")<< " Error: invalid hit with no GeomDet attached .... skipping";
157  continue;
158  }
159  // if (hit.det() && hit.geographicalId()<1000U) LogDebug("TrackFitters")<< "Problem 0 det id for " << typeid(hit).name() << ' ' << hit.det()->geographicalId() ;
160  // if (hit.isValid() && hit.geographicalId()<1000U) LogDebug("TrackFitters")<< "Problem 0 det id for " << typeid(hit).name() << ' ' << hit.det()->geographicalId();
161 
162  if ( hitcounter != 1) //no propagation needed for the first hit
163  predTsos = p_cloned->propagate( currTsos, *(hit.surface()) );
164 
165 
166  if unlikely(!predTsos.isValid()) {
167  LogDebug("TrackFitters")
168  << "SOMETHING WRONG !" << "\n"
169  << "KFTrajectoryFitter: predicted tsos not valid!\n"
170  << "current TSOS: " << currTsos << "\n";
171 
172  if(hit.surface()) LogTrace("TrackFitters") << "next Surface: " << hit.surface()->position() << "\n";
173 
174  if( myTraj.foundHits() >= minHits_ ) {
175  LogDebug("TrackFitters") << " breaking trajectory" << "\n";
176  break;
177  } else {
178  LogDebug("TrackFitters") << " killing trajectory" << "\n";
179  return Trajectory();
180  }
181  }
182 
183 
184  if likely(hit.isValid()) {
185  assert( (hit.geographicalId()!=0U) | !hit.canImproveWithTrack() ) ;
186  assert(hit.surface()!=nullptr);
187  //update
188  LogTrace("TrackFitters") << "THE HIT IS VALID: updating hit with predTsos";
189  assert( (!hit.canImproveWithTrack()) | (nullptr!=theHitCloner));
190  assert( (!hit.canImproveWithTrack()) | (nullptr!=dynamic_cast<BaseTrackerRecHit const*>((ihit).get())));
191  auto preciseHit = theHitCloner->makeShared(ihit,predTsos);
192  dump(*preciseHit,hitcounter);
193  assert(preciseHit->isValid());
194  assert( (preciseHit->geographicalId()!=0U) | (!preciseHit->canImproveWithTrack()) );
195  assert(preciseHit->surface()!=nullptr);
196 
197  if unlikely(!preciseHit->isValid()){
198  LogTrace("TrackFitters") << "THE Precise HIT IS NOT VALID: using currTsos = predTsos" << "\n";
199  currTsos = predTsos;
200  myTraj.push(TM(predTsos, ihit,0,theGeometry->idToLayer((ihit)->geographicalId()) ));
201 
202  }else{
203  LogTrace("TrackFitters") << "THE Precise HIT IS VALID: updating currTsos" << "\n";
204  currTsos = updator()->update(predTsos, *preciseHit);
205  //check for valid hits with no det (refitter with constraints)
206  bool badState = (!currTsos.isValid())
207  || (hit.geographicalId().det() == DetId::Tracker
208  &&
209  (std::abs(currTsos.localParameters().qbp())>100
210  || std::abs(currTsos.localParameters().position().y()) > 1000
211  || std::abs(currTsos.localParameters().position().x()) > 1000
212  ) ) || edm::isNotFinite(currTsos.localParameters().qbp());
213  if unlikely(badState){
214  if (!currTsos.isValid()) {
215  edm::LogError("FailedUpdate") <<"updating with the hit failed. Not updating the trajectory with the hit";
216 
217  }
218  else if (edm::isNotFinite(currTsos.localParameters().qbp())) {
219  edm::LogError("TrajectoryNaN")<<"Trajectory has NaN";
220 
221  }
222  else{
223  LogTrace("FailedUpdate")<<"updated state is valid but pretty bad, skipping. currTsos " <<currTsos<<"\n predTsos "<<predTsos;
224  }
225  myTraj.push(TM(predTsos, ihit,0,theGeometry->idToLayer((ihit)->geographicalId()) ));
226  //There is a no-fail policy here. So, it's time to give up
227  //Keep the traj with invalid TSOS so that it's clear what happened
228  if( myTraj.foundHits() >= minHits_ ) {
229  LogDebug("TrackFitters") << " breaking trajectory" << "\n";
230  break;
231  } else {
232  LogDebug("TrackFitters") << " killing trajectory" << "\n";
233  return Trajectory();
234  }
235  } else{
236  if (preciseHit->det()){
237  myTraj.push(TM(predTsos, currTsos, preciseHit,
238  estimator()->estimate(predTsos, *preciseHit).second,
239  theGeometry->idToLayer(preciseHit->geographicalId()) ));
240  }
241  else{
242  myTraj.push(TM(predTsos, currTsos, preciseHit,
243  estimator()->estimate(predTsos, *preciseHit).second));
244  }
245  }
246  }
247  } else { // invalid hit
248  dump(hit,hitcounter);
249  //no update
250  LogDebug("TrackFitters") << "THE HIT IS NOT VALID: using currTsos" << "\n";
251  currTsos = predTsos;
252  assert( ((ihit)->det()==nullptr) || (ihit)->geographicalId()!=0U);
253  if ((ihit)->det()) myTraj.push(TM(predTsos, ihit,0,theGeometry->idToLayer((ihit)->geographicalId()) ));
254  else myTraj.push(TM(predTsos, ihit,0));
255  }
256  LogTrace("TrackFitters")
257  << "predTsos !" << "\n"
258  << predTsos
259  <<" with local position " << predTsos.localPosition()
260  <<"currTsos !" << "\n"
261  << currTsos
262  <<" with local position " << currTsos.localPosition();
263  }
264 
265  LogDebug("TrackFitters") << "Found 1 trajectory with " << myTraj.foundHits() << " valid hits\n";
266 
267  return ret;
268 }
269 
#define LogDebug(id)
PropagationDirection direction() const
bool empty() const
True if trajectory has no measurements.
Definition: Trajectory.h:254
type
Definition: HCALResponse.h:21
virtual const Surface * surface() const
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 LocalTrajectoryParameters & localParameters() const
const TrajectoryStateUpdator * updator() const
LocalPoint position() const
Local x and y position coordinates.
static const int GEM
Definition: MuonSubdetId.h:15
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
const MeasurementEstimator * estimator() const
T y() const
Definition: PV3DBase.h:63
ConstRecHitContainer recHits() const
Definition: Trajectory.h:204
TrajectoryMeasurement TM
TrackingRecHit::ConstRecHitPointer makeShared(TrackingRecHit::ConstRecHitPointer const &hit, TrajectoryStateOnSurface const &tsos) const
Definition: TkCloner.h:23
void reserve(unsigned int n)
Definition: Trajectory.h:150
virtual GlobalPoint globalPosition() const
virtual TrajectoryStateOnSurface update(const TrajectoryStateOnSurface &, const TrackingRecHit &) const =0
unsigned int layer() const
layer id
Definition: PXBDetId.h:35
Trajectory fitOne(const Trajectory &aTraj, fitType) const override
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
#define unlikely(x)
U second(std::pair< T, U > const &p)
#define likely(x)
std::unique_ptr< Propagator > SetPropagationDirection(Propagator const &iprop, PropagationDirection dir)
static const int ME0
Definition: MuonSubdetId.h:16
static const int CSC
Definition: MuonSubdetId.h:13
bool isNotFinite(T x)
Definition: isFinite.h:10
static const DetLayerGeometry dummyGeometry
virtual int dimension() const =0
T z() const
Definition: PV3DBase.h:64
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
virtual LocalPoint localPosition() const =0
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
const DetLayerGeometry * theGeometry
Definition: DetId.h:18
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:187
virtual bool canImproveWithTrack() const
bool isValid() const
unsigned int wheel() const
wheel id
Definition: TECDetId.h:52
unsigned int layer() const
layer id
Definition: TIBDetId.h:41
static const int RPC
Definition: MuonSubdetId.h:14
const Propagator * thePropagator
const RotationType & rotation() const
static const int DT
Definition: MuonSubdetId.h:12
TrajectoryStateOnSurface const & updatedState() const
DetId geographicalId() const
virtual LocalError localPositionError() const =0
virtual const DetLayer * idToLayer(const DetId &detId) const
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
T x() const
Definition: PV3DBase.h:62
const PositionType & position() const
Trajectory::RecHitContainer RecHitContainer
unsigned int wheel() const
wheel id
Definition: TIDDetId.h:50
TkCloner const * theHitCloner