CMS 3D CMS Logo

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