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