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