CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
KFFittingSmoother.cc
Go to the documentation of this file.
2 // #include "TrackingTools/MaterialEffects/interface/PropagatorWithMaterial.h"
9 
10 // ggiurgiu@fnal.gov: Add headers needed to cut on pixel hit probability
14 #include <cmath>
15 
16 using namespace std;
17 
19 {
20  delete theSmoother;
21  delete theFitter;
22 }
23 
24 vector<Trajectory> KFFittingSmoother::fit(const Trajectory& t) const
25 {
26  vector<Trajectory> smoothed;
27  if ( t.isValid() )
28  {
29  vector<Trajectory> fitted = fitter()->fit(t);
30  smoothingStep(fitted, smoothed);
31  }
32  return smoothed;
33 }
34 
35 bool KFFittingSmoother::checkForNans(const Trajectory & theTraj) const
36 {
37  if (std::isnan(theTraj.chiSquared ())) return false;
38  const std::vector<TrajectoryMeasurement> & vtm = theTraj.measurements();
39  for (std::vector<TrajectoryMeasurement>::const_iterator tm=vtm.begin(); tm!=vtm.end();tm++) {
40  if (std::isnan(tm->estimate())) return false;
41  AlgebraicVector5 v = tm->updatedState ().localParameters ().vector();
42  for (int i=0;i<5;++i) if (std::isnan(v[i])) return false;
43  const AlgebraicSymMatrix55 & m = tm->updatedState ().curvilinearError ().matrix();
44  for (int i=0;i<5;++i)
45  for (int j=0;j<(i+1);++j) if (std::isnan(m(i,j))) return false;
46  }
47  return true;
48 }
49 
50 vector<Trajectory> KFFittingSmoother::fit(const TrajectorySeed& aSeed,
51  const RecHitContainer& hits,
52  const TrajectoryStateOnSurface& firstPredTsos) const
53 {
54  LogDebug("TrackFitters") << "In KFFittingSmoother::fit";
55 
56  //if(hits.empty()) return vector<Trajectory>(); // gio: moved later to optimize return value
57 
58  bool hasoutliers;
59  bool has_low_pixel_prob; // ggiurgiu@fnal.gov: Add flag for pixel hits with low template probability
60 
61  // ggiurgiu@fnal.gov: If log(Prob) < -15.0 or if Prob <= 0.0 then set log(Prob) = -15.0
62  double log_pixel_probability_lower_limit = -15.0;
63 
64  RecHitContainer myHits = hits;
65  vector<Trajectory> smoothed;
66  vector<Trajectory> tmp_first;
67 
68  do
69  {
70  if ( hits.empty() )
71  {
72  smoothed.clear();
73  break;
74  }
75 
76  //if no outliers the fit is done only once
77  //for (unsigned int j=0;j<myHits.size();j++) {
78  //if (myHits[j]->det())
79  //LogTrace("TrackFitters") << "hit #:" << j+1 << " rawId=" << myHits[j]->det()->geographicalId().rawId()
80  //<< " validity=" << myHits[j]->isValid();
81  //else
82  //LogTrace("TrackFitters") << "hit #:" << j+1 << " Hit with no Det information";
83  //}
84 
85  hasoutliers = false;
86  has_low_pixel_prob = false; // ggiurgiu@fnal.gov
87 
88  double cut = theEstimateCut;
89 
90  double log_pixel_prob_cut = theLogPixelProbabilityCut; // ggiurgiu@fnal.gov
91 
92 
93  unsigned int outlierId = 0;
94  const GeomDet* outlierDet = 0;
95 
96  unsigned int low_pixel_prob_Id = 0; // ggiurgiu@fnal.gov
97  const GeomDet* low_pixel_prob_Det = 0; // ggiurgiu@fnal.gov
98 
99  //call the fitter
100  vector<Trajectory> fitted = fitter()->fit(aSeed, myHits, firstPredTsos);
101  //call the smoother
102  smoothed.clear();
103  smoothingStep(fitted, smoothed);
104 
105  //if (tmp_first.size()==0) tmp_first = smoothed; moved later
106 
107  if ( smoothed.empty() )
108  {
109  if ( rejectTracksFlag )
110  {
111  LogTrace("TrackFitters") << "smoothed.size()==0 => trajectory rejected";
112  //return vector<Trajectory>(); // break is enough to get this
113  }
114  else
115  {
116  LogTrace("TrackFitters") << "smoothed.size()==0 => returning orignal trajectory" ;
117  smoothed.swap(tmp_first); // if first attempt, tmp_first would be empty anyway
118  }
119  break;
120  }
121  //else {
122  //LogTrace("TrackFitters") << "dump hits after smoothing";
123  //Trajectory::DataContainer meas = smoothed[0].measurements();
124  //for (Trajectory::DataContainer::iterator it=meas.begin();it!=meas.end();++it) {
125  //LogTrace("TrackFitters") << "hit #" << meas.end()-it-1 << " validity=" << it->recHit()->isValid()
126  //<< " det=" << it->recHit()->geographicalId().rawId();
127  //}
128  //}
129  if (!checkForNans(smoothed[0])) {
130  edm::LogError("TrackNaN")<<"Track has NaN";
131  smoothed.clear();
132  break;
133  }
134 
135  if ( theEstimateCut > 0 || log_pixel_prob_cut > log_pixel_probability_lower_limit )
136  {
137  if ( smoothed[0].foundHits() < theMinNumberOfHits )
138  {
139  if ( rejectTracksFlag )
140  {
141  LogTrace("TrackFitters") << "smoothed[0].foundHits()<theMinNumberOfHits => trajectory rejected";
142  smoothed.clear();
143  //return vector<Trajectory>(); // break is enough
144  }
145  else
146  {
147  // it might be it's the first step
148  if ( !tmp_first.empty() )
149  {
150  tmp_first.swap(smoothed);
151  }
152 
153  LogTrace("TrackFitters")
154  << "smoothed[0].foundHits()<theMinNumberOfHits => returning orignal trajectory with chi2="
155  << smoothed[0].chiSquared() ;
156  }
157  break;
158  }
159 
160  // Check if there are outliers or low probability pixel rec hits
161  const std::vector<TrajectoryMeasurement> & vtm = smoothed[0].measurements();
162 
163  double log_pixel_hit_probability = -999999.9;
164 
165  for (std::vector<TrajectoryMeasurement>::const_iterator tm=vtm.begin(); tm!=vtm.end();tm++)
166  {
167  double estimate = tm->estimate();
168 
169  // --- here is the block of code about generic chi2-based Outlier Rejection ---
170  if ( estimate > cut && theEstimateCut > 0 )
171  {
172  hasoutliers = true;
173  cut = estimate;
174  outlierId = tm->recHit()->geographicalId().rawId();
175  outlierDet = tm->recHit()->det();
176  }
177  // --- here the block of code about generic chi2-based Outlier Rejection ends ---
178 
179 
180  // --- here is the block of code about PXL Outlier Rejection ---
181  if(log_pixel_prob_cut > log_pixel_probability_lower_limit){
182  // TO BE FIXED: the following code should really be moved into an external class or
183  // at least in a separate function. Current solution is ugly!
184  // The KFFittingSmoother shouldn't handle the details of
185  // Outliers identification and rejection. It shoudl just fit tracks.
186 
187  // ggiurgiu@fnal.gov: Get pixel hit probability here
189  unsigned int testSubDetID = ( hit->geographicalId().subdetId() );
190 
191  if ( hit->isValid() &&
192  hit->geographicalId().det() == DetId::Tracker &&
193  ( testSubDetID == PixelSubdetector::PixelBarrel ||
194  testSubDetID == PixelSubdetector::PixelEndcap )
195  )
196  {
197  // get the enclosed persistent hit
198  const TrackingRecHit* persistentHit = hit->hit();
199 
200  // check if it's not null, and if it's a valid pixel hit
201  if ( !persistentHit == 0 &&
202  typeid(*persistentHit) == typeid(SiPixelRecHit) )
203  {
204 
205  // tell the C++ compiler that the hit is a pixel hit
206  const SiPixelRecHit* pixhit = dynamic_cast<const SiPixelRecHit*>( hit->hit() );
207 
208  double pixel_hit_probability = (float)pixhit->clusterProbability(0);
209 
210  if ( pixel_hit_probability < 0.0 )
211  LogDebug("From KFFittingSmoother.cc")
212  << "Wraning : Negative pixel hit probability !!!! Will set the probability to 10^{-15} !!!" << endl;
213 
214  if ( pixel_hit_probability <= 0.0 || log10( pixel_hit_probability ) < log_pixel_probability_lower_limit )
215  log_pixel_hit_probability = log_pixel_probability_lower_limit;
216  else
217  log_pixel_hit_probability = log10( pixel_hit_probability );
218 
219  int qbin = (int)pixhit->qBin();
220 
221  if ( ( log_pixel_hit_probability < log_pixel_prob_cut ) &&
222  ( qbin != 0 ) )
223  {
224  has_low_pixel_prob = true;
225  log_pixel_prob_cut = log_pixel_hit_probability;
226  low_pixel_prob_Id = tm->recHit()->geographicalId().rawId();
227  low_pixel_prob_Det = tm->recHit()->det();
228  }
229 
230  } // if ( !persistentHit == 0 && ... )
231 
232  } // if ( hit->isValid() && ... )
233  }
234  // --- here the block of code about PXL Outlier Rejection ends ---
235 
236 
237  } // for (std::vector<TrajectoryMeasurement>::const_iterator tm=vtm.begin(); tm!=vtm.end(); tm++)
238 
239 
240  if ( hasoutliers || has_low_pixel_prob )
241  { // Reject outliers and pixel hits with low probability
242 
243  // Replace outlier hit or low probability pixel hit with invalid hit
244  for ( unsigned int j=0; j<myHits.size(); ++j )
245  {
246  if ( hasoutliers && outlierId == myHits[j]->geographicalId().rawId() )
247  {
248  LogTrace("TrackFitters") << "Rejecting outlier hit with estimate " << cut << " at position "
249  << j << " with rawId=" << myHits[j]->geographicalId().rawId();
250  LogTrace("TrackFitters") << "The fit will be repeated without the outlier";
252  }
253  else if ( has_low_pixel_prob && low_pixel_prob_Id == myHits[j]->geographicalId().rawId() )
254  {
255  LogTrace("TrackFitters") << "Rejecting low proability pixel hit with log_pixel_prob_cut = "
256  << log_pixel_prob_cut << " at position "
257  << j << " with rawId =" << myHits[j]->geographicalId().rawId();
258  LogTrace("TrackFitters") << "The fit will be repeated without the outlier";
259  myHits[j] = InvalidTransientRecHit::build(low_pixel_prob_Det, TrackingRecHit::missing);
260  }
261 
262  } // for ( unsigned int j=0; j<myHits.size(); ++j)
263 
264  // Look if there are two consecutive invalid hits
265  if ( breakTrajWith2ConsecutiveMissing )
266  {
267  unsigned int firstinvalid = myHits.size()-1;
268  for ( unsigned int j=0; j<myHits.size()-1; ++j )
269  {
270  if ( ((myHits[j ]->type() == TrackingRecHit::missing) && (myHits[j ]->geographicalId().rawId() != 0)) &&
271  ((myHits[j+1]->type() == TrackingRecHit::missing) && (myHits[j+1]->geographicalId().rawId() != 0)) )
272  {
273  firstinvalid = j;
274  LogTrace("TrackFitters") << "Found two consecutive missing hits. First invalid: " << firstinvalid;
275  break;
276  }
277  }
278 
279  //reject all the hits after the last valid before two consecutive invalid (missing) hits
280  //hits are sorted in the same order as in the track candidate FIXME??????
281  myHits.erase(myHits.begin()+firstinvalid,myHits.end());
282 
283  }
284 
285  } // if ( hasoutliers || has_low_pixel_prob )
286 
287  } // if ( theEstimateCut > 0 ... )
288 
289  if ( ( hasoutliers || // otherwise there won't be a 'next' loop where tmp_first could be useful
290  has_low_pixel_prob ) && // ggiurgiu@fnal.gov
291  !rejectTracksFlag && // othewrise we won't ever need tmp_first
292  tmp_first.empty() )
293  { // only at first step
294  smoothed.swap(tmp_first);
295  }
296 
297  } // do
298  while ( hasoutliers || has_low_pixel_prob ); // ggiurgiu@fnal.gov
299 
300  if ( !smoothed.empty() )
301  {
302  if ( noInvalidHitsBeginEnd )
303  {
304  // discard latest dummy measurements
305  if ( !smoothed[0].empty() &&
306  !smoothed[0].lastMeasurement().recHit()->isValid() )
307  LogTrace("TrackFitters") << "Last measurement is invalid";
308 
309  while ( !smoothed[0].empty() &&
310  !smoothed[0].lastMeasurement().recHit()->isValid() )
311  smoothed[0].pop();
312 
313  //remove the invalid hits at the begin of the trajectory
314  if ( !smoothed[0].firstMeasurement().recHit()->isValid() )
315  {
316  LogTrace("TrackFitters") << "First measurement is invalid";
317  Trajectory tmpTraj(smoothed[0].seed(),smoothed[0].direction());
318  Trajectory::DataContainer meas = smoothed[0].measurements();
319 
320  Trajectory::DataContainer::iterator it;//first valid hit
321  for ( it=meas.begin(); it!=meas.end(); ++it )
322  {
323  if ( !it->recHit()->isValid() )
324  continue;
325  else break;
326  }
327  tmpTraj.push(*it,smoothed[0].chiSquared());//push the first valid measurement and set the same global chi2
328 
329  for (Trajectory::DataContainer::iterator itt=it+1; itt!=meas.end();++itt)
330  {
331  tmpTraj.push(*itt,0);//add all the other measurements
332  }
333 
334  smoothed.clear();
335  smoothed.push_back(tmpTraj);
336 
337  } // if ( !smoothed[0].firstMeasurement().recHit()->isValid() )
338 
339  } // if ( noInvalidHitsBeginEnd )
340 
341  LogTrace("TrackFitters") << "end: returning smoothed trajectory with chi2="
342  << smoothed[0].chiSquared() ;
343 
344  //LogTrace("TrackFitters") << "dump hits before return";
345  //Trajectory::DataContainer meas = smoothed[0].measurements();
346  //for (Trajectory::DataContainer::iterator it=meas.begin();it!=meas.end();++it) {
347  //LogTrace("TrackFitters") << "hit #" << meas.end()-it-1 << " validity=" << it->recHit()->isValid()
348  //<< " det=" << it->recHit()->geographicalId().rawId();
349  //}
350 
351  }
352 
353  return smoothed;
354 
355 }
356 
357 
358 void
359 KFFittingSmoother::smoothingStep(vector<Trajectory>& fitted, vector<Trajectory> &smoothed) const
360 {
361 
362  for(vector<Trajectory>::iterator it = fitted.begin(); it != fitted.end();
363  it++) {
364  vector<Trajectory> mysmoothed = smoother()->trajectories(*it);
365  smoothed.insert(smoothed.end(), mysmoothed.begin(), mysmoothed.end());
366  }
367  LogDebug("TrackFitters") << "In KFFittingSmoother::smoothingStep "<<smoothed.size();
368 }
369 
370 vector<Trajectory> KFFittingSmoother::fit(const TrajectorySeed& aSeed,
371  const RecHitContainer& hits) const{
372 
373  throw cms::Exception("TrackFitters",
374  "KFFittingSmoother::fit(TrajectorySeed, <TransientTrackingRecHit>) not implemented");
375 
376  return vector<Trajectory>();
377 }
#define LogDebug(id)
type
Definition: HCALResponse.h:22
int i
Definition: DBlmapReader.cc:9
float clusterProbability(unsigned int flags=0) const
static RecHitPointer build(const GeomDet *geom, Type type=TrackingRecHit::missing, const DetLayer *layer=0)
virtual std::vector< Trajectory > fit(const Trajectory &t) const
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
bool checkForNans(const Trajectory &theTraj) const
Method to check that the trajectory has no NaN in the states and chi2.
DataContainer const & measurements() const
Definition: Trajectory.h:203
std::vector< TrajectoryMeasurement > DataContainer
Definition: Trajectory.h:42
bool isnan(float x)
Definition: math.h:13
int j
Definition: DBlmapReader.cc:9
void smoothingStep(std::vector< Trajectory > &fitted, std::vector< Trajectory > &smoothed) const
virtual ~KFFittingSmoother()
#define LogTrace(id)
int qBin() const
Definition: SiPixelRecHit.h:96
bool isValid() const
Definition: Trajectory.h:259
ROOT::Math::SVector< double, 5 > AlgebraicVector5
Trajectory::RecHitContainer RecHitContainer
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:35
mathSSE::Vec4< T > v
double chiSquared() const
Definition: Trajectory.h:242
Our base class.
Definition: SiPixelRecHit.h:22