test
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 = 0xffffffff;
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::LogWarning("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  tm->recHit()->det()!=nullptr // do not consider outliers constraints and other special "hits"
142  ) {
143  hasoutliers = true;
144  cut = estimate;
145  outlierId = tm->recHit()->geographicalId().rawId();
146  outlierDet = tm->recHit()->det();
147  }
148  // --- here the block of code about generic chi2-based Outlier Rejection ends ---
149 
150 
151  // --- here is the block of code about PXL Outlier Rejection ---
152  if(log_pixel_prob_cut > log_pixel_probability_lower_limit){
153  // TO BE FIXED: the following code should really be moved into an external class or
154  // at least in a separate function. Current solution is ugly!
155  // The KFFittingSmoother shouldn't handle the details of
156  // Outliers identification and rejection. It shoudl just fit tracks.
157 
158  // ggiurgiu@fnal.gov: Get pixel hit probability here
160  unsigned int testSubDetID = ( hit->geographicalId().subdetId() );
161 
162  if ( hit->isValid() &&
163  hit->geographicalId().det() == DetId::Tracker &&
164  ( testSubDetID == PixelSubdetector::PixelBarrel ||
165  testSubDetID == PixelSubdetector::PixelEndcap )
166  ){
167  // get the enclosed persistent hit
168  const TrackingRecHit* persistentHit = hit->hit();
169 
170  // check if it's not null, and if it's a valid pixel hit
171  if ( !persistentHit == 0 &&
172  typeid(*persistentHit) == typeid(SiPixelRecHit) )
173  {
174 
175  // tell the C++ compiler that the hit is a pixel hit
176  const SiPixelRecHit* pixhit = dynamic_cast<const SiPixelRecHit*>( hit->hit() );
177 
178  double pixel_hit_probability = (float)pixhit->clusterProbability(0);
179 
180  if ( pixel_hit_probability < 0.0 )
181  LogDebug("From KFFittingSmoother.cc")
182  << "Wraning : Negative pixel hit probability !!!! Will set the probability to 10^{-15} !!!" << endl;
183 
184  if ( pixel_hit_probability <= 0.0 || log10( pixel_hit_probability ) < log_pixel_probability_lower_limit )
185  log_pixel_hit_probability = log_pixel_probability_lower_limit;
186  else
187  log_pixel_hit_probability = log10( pixel_hit_probability );
188 
189  int qbin = (int)pixhit->qBin();
190 
191  if ( ( log_pixel_hit_probability < log_pixel_prob_cut ) &&
192  ( qbin != 0 ) )
193  {
194  has_low_pixel_prob = true;
195  log_pixel_prob_cut = log_pixel_hit_probability;
196  low_pixel_prob_Id = tm->recHit()->geographicalId().rawId();
197  low_pixel_prob_Det = tm->recHit()->det();
198  }
199 
200  } // if ( !persistentHit == 0 && ... )
201 
202  } // if ( hit->isValid() && ... )
203  }
204  // --- here the block of code about PXL Outlier Rejection ends ---
205 
206  } // for (std::vector<TrajectoryMeasurement>::const_iterator tm=vtm.begin(); tm!=vtm.end(); tm++)
207 
208 
209  if ( hasoutliers || has_low_pixel_prob ) { // Reject outliers and pixel hits with low probability
210 
211  // Replace outlier hit or low probability pixel hit with invalid hit
212  for ( unsigned int j=0; j<myHits.size(); ++j ) {
213  if ( hasoutliers && outlierId == myHits[j]->geographicalId().rawId() )
214  {
215  LogTrace("TrackFitters") << "Rejecting outlier hit with estimate " << cut << " at position "
216  << j << " with rawId=" << myHits[j]->geographicalId().rawId();
217  LogTrace("TrackFitters") << "The fit will be repeated without the outlier";
218  myHits[j] = std::make_shared<InvalidTrackingRecHit>(*outlierDet, TrackingRecHit::missing);
219  }
220  else if ( has_low_pixel_prob && low_pixel_prob_Id == myHits[j]->geographicalId().rawId() ){
221  LogTrace("TrackFitters") << "Rejecting low proability pixel hit with log_pixel_prob_cut = "
222  << log_pixel_prob_cut << " at position "
223  << j << " with rawId =" << myHits[j]->geographicalId().rawId();
224  LogTrace("TrackFitters") << "The fit will be repeated without the outlier";
225  myHits[j] = std::make_shared<InvalidTrackingRecHit>(*low_pixel_prob_Det, TrackingRecHit::missing);
226  }
227 
228  } // for ( unsigned int j=0; j<myHits.size(); ++j)
229 
230  // Look if there are two consecutive invalid hits
231  if ( breakTrajWith2ConsecutiveMissing ) {
232  unsigned int firstinvalid = myHits.size();
233  for ( unsigned int j=0; j<myHits.size()-1; ++j )
234  {
235  if ( ((myHits[j ]->type() == TrackingRecHit::missing) && (myHits[j ]->geographicalId().rawId() != 0)) &&
236  ((myHits[j+1]->type() == TrackingRecHit::missing) && (myHits[j+1]->geographicalId().rawId() != 0)) )
237  {
238  firstinvalid = j;
239  LogTrace("TrackFitters") << "Found two consecutive missing hits. First invalid: " << firstinvalid;
240  break;
241  }
242  }
243 
244  //reject all the hits after the last valid before two consecutive invalid (missing) hits
245  //hits are sorted in the same order as in the track candidate FIXME??????
246  if (firstinvalid != myHits.size()) myHits.erase(myHits.begin()+firstinvalid,myHits.end());
247 
248  }
249 
250  } // if ( hasoutliers || has_low_pixel_prob )
251 
252  } // if ( theEstimateCut > 0 ... )
253 
254  if ( ( hasoutliers || // otherwise there won't be a 'next' loop where tmp_first could be useful
255  has_low_pixel_prob ) && // ggiurgiu@fnal.gov
256  !rejectTracksFlag && // othewrise we won't ever need tmp_first
257  firstTry ) { // only at first step
258  std::swap(smoothed,tmp_first); firstTry=false;
259  }
260 
261  } // do
262  while ( hasoutliers || has_low_pixel_prob ); // ggiurgiu@fnal.gov
263 
264  if ( smoothed.isValid() ) {
265  if ( noInvalidHitsBeginEnd
266  && !smoothed.empty() //should we send a warning ?
267  ) {
268  // discard latest dummy measurements
269  if (!smoothed.empty() && !smoothed.lastMeasurement().recHitR().isValid() )
270  LogTrace("TrackFitters") << "Last measurement is invalid";
271 
272  while (!smoothed.empty() && !smoothed.lastMeasurement().recHitR().isValid() )
273  smoothed.pop();
274 
275  //remove the invalid hits at the begin of the trajectory
276  if (!smoothed.empty() && !smoothed.firstMeasurement().recHitR().isValid() ) {
277  LogTrace("TrackFitters") << "First measurement is in`valid";
278  Trajectory tmpTraj(smoothed.seed(),smoothed.direction());
279  Trajectory::DataContainer & meas = smoothed.measurements();
280  auto it = meas.begin();
281  for ( ; it!=meas.end(); ++it )
282  if ( it->recHitR().isValid() ) break;
283  tmpTraj.push(std::move(*it),smoothed.chiSquared());//push the first valid measurement and set the same global chi2
284 
285  for (auto itt=it+1; itt!=meas.end();++itt)
286  tmpTraj.push(std::move(*itt),0);//add all the other measurements
287 
288  std::swap(smoothed,tmpTraj);
289 
290  } // if ( !smoothed[0].firstMeasurement().recHit()->isValid() )
291 
292  } // if ( noInvalidHitsBeginEnd )
293 
294  LogTrace("TrackFitters") << "end: returning smoothed trajectory with chi2="
295  << smoothed.chiSquared() ;
296 
297  //LogTrace("TrackFitters") << "dump hits before return";
298  //Trajectory::DataContainer meas = smoothed[0].measurements();
299  //for (Trajectory::DataContainer::iterator it=meas.begin();it!=meas.end();++it) {
300  //LogTrace("TrackFitters") << "hit #" << meas.end()-it-1 << " validity=" << it->recHit()->isValid()
301  //<< " det=" << it->recHit()->geographicalId().rawId();
302  //}
303 
304  }
305 
306  return smoothed;
307 
308 }
309 
310 
312  const RecHitContainer& hits,fitType type) const{
313 
314  throw cms::Exception("TrackFitters",
315  "KFFittingSmoother::fit(TrajectorySeed, <TransientTrackingRecHit>) not implemented");
316 
317  return Trajectory();
318 }
#define LogDebug(id)
bool empty() const
True if trajectory has no measurements.
Definition: Trajectory.h:244
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
def move
Definition: eostools.py:510
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