CMS 3D CMS Logo

CRackTrajectoryBuilder.cc
Go to the documentation of this file.
1 //
2 // Package: RecoTracker/SingleTrackPattern
3 // Class: CRackTrajectoryBuilder
4 // Original Author: Michele Pioppi-INFN perugia
5 //
6 // Package: RecoTracker/SingleTrackPattern
7 // Class: CRackTrajectoryBuilder
8 // Original Author: Michele Pioppi-INFN perugia
9 
10 #include <vector>
11 #include <iostream>
12 #include <cmath>
13 
25 
26 //#include "RecoTracker/CkfPattern/interface/TransientInitialStateEstimator.h"
27 
28 using namespace std;
29 
31  //minimum number of hits per tracks
32 
33  theMinHits = conf.getParameter<int>("MinHits");
34  //cut on chi2
35  chi2cut = conf.getParameter<double>("Chi2Cut");
36  edm::LogInfo("CosmicTrackFinder") << "Minimum number of hits " << theMinHits << " Cut on Chi2= " << chi2cut;
37 
38  debug_info = conf.getUntrackedParameter<bool>("debug", false);
39  fastPropagation = conf.getUntrackedParameter<bool>("fastPropagation", false);
40  useMatchedHits = conf.getUntrackedParameter<bool>("useMatchedHits", true);
41 
42  geometry = conf.getUntrackedParameter<std::string>("GeometricStructure", "STANDARD");
43  theBuilderName = conf.getParameter<std::string>("TTRHBuilder");
44 }
45 
47  // delete theInitialState;
48 }
49 
50 void CRackTrajectoryBuilder::init(const edm::EventSetup& es, bool seedplus) {
51  // edm::ParameterSet tise_params = conf_.getParameter<edm::ParameterSet>("TransientInitialStateEstimatorParameters") ;
52  // theInitialState = new TransientInitialStateEstimator( es,tise_params);
53 
54  //services
57 
58  if (seedplus) {
59  seed_plus = true;
60  thePropagator = new PropagatorWithMaterial(alongMomentum, 0.1057, &(*magfield));
61  thePropagatorOp = new PropagatorWithMaterial(oppositeToMomentum, 0.1057, &(*magfield));
62  } else {
63  seed_plus = false;
64  thePropagator = new PropagatorWithMaterial(oppositeToMomentum, 0.1057, &(*magfield));
65  thePropagatorOp = new PropagatorWithMaterial(alongMomentum, 0.1057, &(*magfield));
66  }
67 
68  theUpdator = new KFUpdator();
69  // theUpdator= new KFStripUpdator();
70  theEstimator = new Chi2MeasurementEstimator(chi2cut);
71 
73  es.get<TransientRecHitRecord>().get(theBuilderName, theBuilder);
74 
75  RHBuilder = theBuilder.product();
76 
77  theFitter = new KFTrajectoryFitter(*thePropagator, *theUpdator, *theEstimator);
78 
79  theSmoother = new KFTrajectorySmoother(*thePropagatorOp, *theUpdator, *theEstimator);
80 }
81 
83  const SiStripRecHit2DCollection& collstereo,
84  const SiStripRecHit2DCollection& collrphi,
85  const SiStripMatchedRecHit2DCollection& collmatched,
86  const SiPixelRecHitCollection& collpixel,
87  const edm::EventSetup& es,
88  edm::Event& e,
89  vector<Trajectory>& trajoutput) {
90  std::vector<Trajectory> trajSmooth;
91  std::vector<Trajectory>::iterator trajIter;
92 
93  TrajectorySeedCollection::const_iterator iseed;
94  unsigned int IS = 0;
95  for (iseed = collseed.begin(); iseed != collseed.end(); iseed++) {
96  bool seedplus = ((*iseed).direction() == alongMomentum);
97  init(es, seedplus);
98  hits.clear();
99  trajFit.clear();
100  trajSmooth.clear();
101 
102  Trajectory startingTraj = createStartingTrajectory(*iseed);
103  Trajectory trajTmp; // used for the opposite direction
104  Trajectory traj = startingTraj;
105 
106  //first propagate track in opposite direction ...
107  seed_plus = !seed_plus;
108  vector<const TrackingRecHit*> allHitsOppsite = SortHits(collstereo, collrphi, collmatched, collpixel, *iseed, true);
109  seed_plus = !seed_plus;
110  if (!allHitsOppsite.empty()) {
111  //there are hits which are above the seed,
112  //cout << "Number of hits higher than seed " <<allHitsOppsite.size() << endl;
113 
114  AddHit(traj, allHitsOppsite, thePropagatorOp);
115 
116  if (debug_info) {
117  cout << "Hits in opposite direction..." << endl;
118  TransientTrackingRecHit::RecHitContainer::const_iterator iHit;
119  for (iHit = hits.begin(); iHit != hits.end(); iHit++) {
120  cout << (**iHit).globalPosition() << endl;
121  }
122  }
123 
124  // now add hist opposite to seed
125  //now crate new starting trajectory
126  reverse(hits.begin(), hits.end());
127  if (thePropagatorOp
128  ->propagate(traj.firstMeasurement().updatedState(),
129  tracker->idToDet((hits.front())->geographicalId())->surface())
130  .isValid()) {
131  TSOS startingStateNew = //TrajectoryStateWithArbitraryError()//
132  (thePropagatorOp->propagate(traj.firstMeasurement().updatedState(),
133  tracker->idToDet((hits.front())->geographicalId())->surface()));
134 
135  if (debug_info) {
136  cout << "Hits in opposite direction reversed..." << endl;
137  TransientTrackingRecHit::RecHitContainer::const_iterator iHit;
138  for (iHit = hits.begin(); iHit != hits.end(); iHit++) {
139  cout << (**iHit).globalPosition() << endl;
140  }
141  }
142 
143  const TrajectorySeed& tmpseed = traj.seed();
144  trajTmp = theFitter->fit(tmpseed, hits, startingStateNew).front();
145 
146  if (debug_info) {
147  cout << "Debugging show fitted hits" << endl;
148  auto hitsFit = trajTmp.recHits();
149  for (auto hit = hitsFit.begin(); hit != hitsFit.end(); hit++) {
150  cout << RHBuilder->build(&(*(*hit)->hit()))->globalPosition() << endl;
151  }
152  }
153  }
154  } else {
155  if (debug_info)
156  cout << "There are no hits in opposite direction ..." << endl;
157  }
158 
159  vector<const TrackingRecHit*> allHits;
160  if (trajTmp.foundHits()) {
161  traj = trajTmp;
162  allHits = SortHits(collstereo, collrphi, collmatched, collpixel, *iseed, false);
163  } else {
164  traj = startingTraj;
165  hits.clear();
166  allHits = SortHits(collstereo, collrphi, collmatched, collpixel, *iseed, true);
167  }
168 
169  AddHit(traj, allHits, thePropagator);
170 
171  if (debug_info) {
172  cout << "Debugging show All fitted hits" << endl;
173  auto hits = traj.recHits();
174  for (auto hit = hits.begin(); hit != hits.end(); hit++) {
175  cout << (*hit)->globalPosition() << endl;
176  }
177 
178  cout << qualityFilter(traj) << " <- quality filter good?" << endl;
179  }
180 
181  if (debug_info)
182  cout << "now do quality checks" << endl;
183  if (qualityFilter(traj)) {
184  const TrajectorySeed& tmpseed = traj.seed();
185  std::pair<TrajectoryStateOnSurface, const GeomDet*> initState =
186  innerState(traj); //theInitialState->innerState(traj);
187  if (initState.first.isValid())
188  trajFit = theFitter->fit(tmpseed, hits, initState.first);
189  }
190 
191  for (trajIter = trajFit.begin(); trajIter != trajFit.end(); trajIter++) {
192  trajSmooth = theSmoother->trajectories((*trajIter));
193  }
194  for (trajIter = trajSmooth.begin(); trajIter != trajSmooth.end(); trajIter++) {
195  if ((*trajIter).isValid()) {
196  if (debug_info)
197  cout << "adding track ... " << endl;
198  trajoutput.push_back((*trajIter));
199  }
200  }
201  delete theUpdator;
202  delete theEstimator;
203  delete thePropagator;
204  delete thePropagatorOp;
205  delete theFitter;
206  delete theSmoother;
207  //Only the first 30 seeds are considered
208  if (IS > 30)
209  return;
210  IS++;
211  }
212 }
213 
215  Trajectory result(seed, seed.direction());
216  std::vector<TM>&& seedMeas = seedMeasurements(seed);
217  for (auto i : seedMeas)
218  result.push(std::move(i));
219  return result;
220 }
221 
222 std::vector<TrajectoryMeasurement> CRackTrajectoryBuilder::seedMeasurements(const TrajectorySeed& seed) const {
223  std::vector<TrajectoryMeasurement> result;
224  TrajectorySeed::range hitRange = seed.recHits();
225  for (TrajectorySeed::const_iterator ihit = hitRange.first; ihit != hitRange.second; ihit++) {
226  //RC TransientTrackingRecHit* recHit = RHBuilder->build(&(*ihit));
227  TransientTrackingRecHit::RecHitPointer recHit = RHBuilder->build(&(*ihit));
228  const GeomDet* hitGeomDet = (&(*tracker))->idToDet(ihit->geographicalId());
229  TSOS invalidState(new BasicSingleTrajectoryState(hitGeomDet->surface()));
230 
231  if (ihit == hitRange.second - 1) {
232  TSOS updatedState = startingTSOS(seed);
233  result.emplace_back(invalidState, updatedState, recHit);
234 
235  } else {
236  result.emplace_back(invalidState, recHit);
237  }
238  }
239 
240  return result;
241 }
242 
243 vector<const TrackingRecHit*> CRackTrajectoryBuilder::SortHits(const SiStripRecHit2DCollection& collstereo,
244  const SiStripRecHit2DCollection& collrphi,
245  const SiStripMatchedRecHit2DCollection& collmatched,
246  const SiPixelRecHitCollection& collpixel,
247  const TrajectorySeed& seed,
248  const bool bAddSeedHits) {
249  //The Hits with global y more than the seed are discarded
250  //The Hits correspondign to the seed are discarded
251  //At the end all the hits are sorted in y
252  vector<const TrackingRecHit*> allHits;
253 
255  TrajectorySeed::range hRange = seed.recHits();
257  float yref = 0.;
258 
259  if (debug_info)
260  cout << "SEED " << startingTSOS(seed) << endl;
261  if (debug_info)
262  cout << "seed hits size " << seed.nHits() << endl;
263 
264  //seed debugging:
265  // GeomDet *seedDet = tracker->idToDet(seed.startingState().detId());
266 
267  // edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "SEED " << seed.startingState();
268 
269  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "SEED " << startingTSOS(seed);
270  // edm::LogInfo("CRackTrajectoryBuilder::SortHits" << "seed hits size " << seed.nHits();
271 
272  // if (seed.nHits()<2)
273  // return allHits;
274 
275  float_t yMin = 0.;
276  float_t yMax = 0.;
277 
278  int seedHitSize = hRange.second - hRange.first;
279 
280  vector<int> detIDSeedMatched(seedHitSize);
281  vector<int> detIDSeedRphi(seedHitSize);
282  vector<int> detIDSeedStereo(seedHitSize);
283 
284  for (ihit = hRange.first; ihit != hRange.second; ihit++) {
285  // need to find track with lowest (seed_plus)/ highest y (seed_minus)
286  // split matched hits ...
287  const SiStripMatchedRecHit2D* matchedhit = dynamic_cast<const SiStripMatchedRecHit2D*>(&(*ihit));
288 
289  yref = RHBuilder->build(&(*ihit))->globalPosition().y();
290  if (ihit == hRange.first) {
291  yMin = yref;
292  yMax = yref;
293  }
294 
295  if (matchedhit) {
296  auto m = matchedhit->monoHit();
297  auto s = matchedhit->stereoHit();
298  float_t yGlobRPhi = RHBuilder->build(&m)->globalPosition().y();
299  float_t yGlobStereo = RHBuilder->build(&s)->globalPosition().y();
300 
301  if (debug_info)
302  cout << "Rphi ..." << yGlobRPhi << endl;
303  if (debug_info)
304  cout << "Stereo ..." << yGlobStereo << endl;
305 
306  if (yGlobStereo < yMin)
307  yMin = yGlobStereo;
308  if (yGlobRPhi < yMin)
309  yMin = yGlobRPhi;
310 
311  if (yGlobStereo > yMax)
312  yMax = yGlobStereo;
313  if (yGlobRPhi > yMax)
314  yMax = yGlobRPhi;
315 
316  detIDSeedMatched.push_back(matchedhit->geographicalId().rawId());
317  detIDSeedRphi.push_back(m.geographicalId().rawId());
318  detIDSeedStereo.push_back(s.geographicalId().rawId());
319 
320  if (bAddSeedHits) {
321  if (useMatchedHits) {
322  hits.push_back((RHBuilder->build(&(*ihit))));
323  } else {
324  if (((yGlobRPhi > yGlobStereo) && seed_plus) || ((yGlobRPhi < yGlobStereo) && !seed_plus)) {
325  hits.push_back((RHBuilder->build(&m)));
326  hits.push_back((RHBuilder->build(&s)));
327  } else {
328  hits.push_back((RHBuilder->build(&s)));
329  hits.push_back((RHBuilder->build(&m)));
330  }
331  }
332  }
333  } else if (bAddSeedHits) {
334  hits.push_back((RHBuilder->build(&(*ihit))));
335  detIDSeedRphi.push_back(ihit->geographicalId().rawId());
336  detIDSeedMatched.push_back(-1);
337  detIDSeedStereo.push_back(-1);
338  }
339 
340  if (yref < yMin)
341  yMin = yref;
342  if (yref > yMax)
343  yMax = yref;
344 
345  // if (bAddSeedHits)
346  // hits.push_back((RHBuilder->build(&(*ihit))));
347 
348  LogDebug("CosmicTrackFinder") << "SEED HITS" << RHBuilder->build(&(*ihit))->globalPosition();
349  if (debug_info)
350  cout << "SEED HITS" << RHBuilder->build(&(*ihit))->globalPosition() << endl;
351  // if (debug_info) cout <<"SEED HITS"<< seed.startingState().parameters() << endl;
352  }
353 
354  yref = (seed_plus) ? yMin : yMax;
355 
357  for (ipix = collpixel.data().begin(); ipix != collpixel.data().end(); ipix++) {
358  float ych = RHBuilder->build(&(*ipix))->globalPosition().y();
359  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
360  allHits.push_back(&(*ipix));
361  }
362 
363  if (useMatchedHits) // use matched
364  {
365  //add the matched hits ...
367 
368  for (istripm = collmatched.data().begin(); istripm != collmatched.data().end(); istripm++) {
369  float ych = RHBuilder->build(&(*istripm))->globalPosition().y();
370 
371  int cDetId = istripm->geographicalId().rawId();
372  bool noSeedDet = (detIDSeedMatched.end() == find(detIDSeedMatched.begin(), detIDSeedMatched.end(), cDetId));
373 
374  if (noSeedDet)
375  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
376  allHits.push_back(&(*istripm));
377  }
378 
379  //add the rpi hits, but only accept hits that are not matched hits
380  for (istrip = collrphi.data().begin(); istrip != collrphi.data().end(); istrip++) {
381  float ych = RHBuilder->build(&(*istrip))->globalPosition().y();
382  StripSubdetector monoDetId(istrip->geographicalId());
383  if (monoDetId.partnerDetId()) {
384  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "this det belongs to a glued det " << ych << endl;
385  continue;
386  }
387  int cDetId = istrip->geographicalId().rawId();
388  bool noSeedDet = (detIDSeedRphi.end() == find(detIDSeedRphi.begin(), detIDSeedRphi.end(), cDetId));
389  if (noSeedDet)
390  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref))) {
391  bool hitIsUnique = true;
392  //now
393  for (istripm = collmatched.data().begin(); istripm != collmatched.data().end(); istripm++) {
394  // if ( isDifferentStripReHit2D ( *istrip, (istripm->stereoHit() ) ) == false)
395  if (isDifferentStripReHit2D(*istrip, (istripm->monoHit())) == false) {
396  hitIsUnique = false;
397  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "rphi hit is in matched hits; y: " << ych << endl;
398  break;
399  }
400  } //end loop over all matched
401  if (hitIsUnique) {
402  // if (debug_info) cout << "adding rphi hit " << &(*istrip) << endl;
403  allHits.push_back(&(*istrip));
404  }
405  }
406  }
407 
408  } else // dont use matched ...
409  {
410  for (istrip = collrphi.data().begin(); istrip != collrphi.data().end(); istrip++) {
411  float ych = RHBuilder->build(&(*istrip))->globalPosition().y();
412  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
413  allHits.push_back(&(*istrip));
414  }
415 
416  for (istrip = collstereo.data().begin(); istrip != collstereo.data().end(); istrip++) {
417  float ych = RHBuilder->build(&(*istrip))->globalPosition().y();
418  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
419  allHits.push_back(&(*istrip));
420  }
421  }
422 
423  if (seed_plus)
424  stable_sort(allHits.begin(), allHits.end(), CompareHitY_plus(*tracker));
425  else
426  stable_sort(allHits.begin(), allHits.end(), CompareHitY(*tracker));
427 
428  if (debug_info) {
429  if (debug_info)
430  cout << "all hits" << endl;
431 
432  //starting trajectory
433  Trajectory startingTraj = createStartingTrajectory(seed);
434 
435  if (debug_info)
436  cout << "START " << startingTraj.lastMeasurement().updatedState() << endl;
437  if (debug_info)
438  cout << "START Err" << startingTraj.lastMeasurement().updatedState().localError().matrix() << endl;
439 
440  vector<const TrackingRecHit*>::iterator iHit;
441  for (iHit = allHits.begin(); iHit < allHits.end(); iHit++) {
442  GlobalPoint gphit = RHBuilder->build(*iHit)->globalPosition();
443  if (debug_info)
444  cout << "GH " << gphit << endl;
445 
446  // tracker->idToDet((*iHit)->geographicalId())->surface();
447 
448  TSOS prSt = thePropagator->propagate(startingTraj.lastMeasurement().updatedState(),
449  tracker->idToDet((*iHit)->geographicalId())->surface());
450 
451  if (prSt.isValid()) {
452  if (debug_info)
453  cout << "PR " << prSt.globalPosition() << endl;
454  //if (debug_info) cout << "PR Err" << prSt.localError().matrix() << endl;
455 
456  } else {
457  if (debug_info)
458  cout << "not valid" << endl;
459  }
460  }
461  if (debug_info)
462  cout << "all hits end" << endl;
463  }
464 
465  return allHits;
466 }
467 
469  PTrajectoryStateOnDet pState(seed.startingState());
470  const GeomDet* gdet = (&(*tracker))->idToDet(DetId(pState.detId()));
471  TSOS State = trajectoryStateTransform::transientState(pState, &(gdet->surface()), &(*magfield));
472  return State;
473 }
474 
476  const vector<const TrackingRecHit*>& _Hits,
477  Propagator* currPropagator) {
478  vector<const TrackingRecHit*> Hits = _Hits;
479  if (Hits.empty())
480  return;
481 
482  if (debug_info)
483  cout << "CRackTrajectoryBuilder::AddHit" << endl;
484  if (debug_info)
485  cout << "START " << traj.lastMeasurement().updatedState() << endl;
486 
487  vector<TrackingRecHitRange> hitRangeByDet;
488  TrackingRecHitIterator prevDet;
489 
490  prevDet = Hits.begin();
491  for (TrackingRecHitIterator iHit = Hits.begin(); iHit != Hits.end(); iHit++) {
492  if ((*prevDet)->geographicalId() == (*iHit)->geographicalId())
493  continue;
494 
495  hitRangeByDet.push_back(make_pair(prevDet, iHit));
496  prevDet = iHit;
497  }
498  hitRangeByDet.push_back(make_pair(prevDet, Hits.end()));
499 
501 
502  if (fastPropagation) {
503  for (TrackingRecHitRangeIterator iHitRange = hitRangeByDet.begin(); iHitRange != hitRangeByDet.end(); iHitRange++) {
504  const TrackingRecHit* currHit = *(iHitRange->first);
505  DetId currDet = currHit->geographicalId();
506 
507  TSOS prSt =
508  currPropagator->propagate(traj.lastMeasurement().updatedState(), tracker->idToDet(currDet)->surface());
509 
510  if (!prSt.isValid()) {
511  if (debug_info)
512  cout << "Not Valid: PRST" << prSt.globalPosition();
513  // if (debug_info) cout << "Not Valid: HIT" << *currHit;
514 
515  continue;
516  }
517 
518  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build(currHit);
519  double chi2min = theEstimator->estimate(prSt, *bestHit).second;
520 
521  if (debug_info)
522  cout << "Size " << iHitRange->first - (*iHitRange).second << endl;
523  for (TrackingRecHitIterator iHit = (*iHitRange).first + 1; iHit != iHitRange->second; iHit++) {
524  if (debug_info)
525  cout << "loop3 "
526  << " " << Hits.end() - iHit << endl;
527 
528  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build(*iHit);
529  double currChi2 = theEstimator->estimate(prSt, *tmpHit).second;
530  if (currChi2 < chi2min) {
531  chi2min = currChi2;
532  bestHit = tmpHit;
533  }
534  }
535  //now we have check if the track can be added to the trajectory
536  if (debug_info)
537  cout << chi2min << endl;
538  if (chi2min < chi2cut) {
539  if (debug_info)
540  cout << "chi2 fine : " << chi2min << endl;
541  TSOS UpdatedState = theUpdator->update(prSt, *bestHit);
542  if (UpdatedState.isValid()) {
543  hits.push_back(bestHit);
544  traj.push(TM(prSt, UpdatedState, bestHit, chi2min));
545  if (debug_info)
546  edm::LogInfo("CosmicTrackFinder") << "STATE UPDATED WITH HIT AT POSITION "
547 
548  << bestHit->globalPosition() << UpdatedState << " " << traj.chiSquared();
549  if (debug_info)
550  cout << "STATE UPDATED WITH HIT AT POSITION "
551 
552  << bestHit->globalPosition() << UpdatedState << " " << traj.chiSquared();
553  if (debug_info)
554  cout << "State is valid ..." << endl;
555  break; // now we need to
556  } else {
557  edm::LogWarning("CosmicTrackFinder") << " State can not be updated with hit at position " << endl;
558  TSOS UpdatedState = theUpdator->update(prSt, *bestHit);
559  if (UpdatedState.isValid()) {
560  cout << "NOT! UPDATED WITH HIT AT POSITION "
561 
562  << bestHit->globalPosition() << UpdatedState << " " << traj.chiSquared();
563  }
564  }
565  }
566  }
567  } //simple version end
568  else {
569  //first sort the dets in the order they are traversed by the trajectory
570  // we need three loops:
571  // 1: loop as long as there can be an new hit added to the trajectory
572  // 2: loop over all dets that might be hit
573  // 3: loop over all hits on a certain det
574 
575  std::vector<std::pair<TrackingRecHitRangeIterator, TSOS> > trackHitCandidates;
576  std::vector<std::pair<TrackingRecHitRangeIterator, TSOS> >::iterator iHitRange;
577  std::vector<uint32_t> processedDets;
578  do {
579  //create vector of possibly hit detectors...
580  trackHitCandidates.clear();
581  DetId currDet;
582  for (TrackingRecHitRangeIterator iHit = hitRangeByDet.begin(); iHit != hitRangeByDet.end(); iHit++) {
583  const TrackingRecHit* currHit = *(iHit->first);
584  currDet = currHit->geographicalId();
585 
586  if (find(processedDets.begin(), processedDets.end(), currDet.rawId()) != processedDets.end())
587  continue;
588 
589  TSOS prSt =
590  currPropagator->propagate(traj.lastMeasurement().updatedState(), tracker->idToDet(currDet)->surface());
591  if ((!prSt.isValid()) ||
592  (theEstimator->Chi2MeasurementEstimatorBase::estimate(prSt, tracker->idToDet(currDet)->surface()) == false))
593  // if ( ( !prSt.isValid() ) || (theEstimator->estimate(prSt,tracker->idToDet(currDet)->surface() ) == false) )
594  continue;
595 
596  trackHitCandidates.push_back(make_pair(iHit, prSt));
597  }
598 
599  if (trackHitCandidates.empty())
600  break;
601 
602  if (debug_info)
603  cout << Hits.size() << " (int) trackHitCandidates.begin() " << trackHitCandidates.size() << endl;
604  if (debug_info)
605  cout << "Before sorting ... " << endl;
606 
607  if (debug_info)
608  for (iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++) {
609  if (debug_info)
610  cout << (tracker->idToDet((*(iHitRange->first->first))->geographicalId()))->position();
611  }
612  if (debug_info)
613  cout << endl;
614 
615  stable_sort(trackHitCandidates.begin(),
616  trackHitCandidates.end(),
618 
619  if (debug_info)
620  cout << "After sorting ... " << endl;
621  if (debug_info) {
622  for (iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++) {
623  if (debug_info)
624  cout << (tracker->idToDet((*(iHitRange->first->first))->geographicalId()))->position();
625  }
626  cout << endl;
627  }
628 
629  for (iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++) //loop over dets
630  {
631  //now find the best hit of the detector
632  if (debug_info)
633  cout << "loop2 " << trackHitCandidates.size() << " " << trackHitCandidates.end() - iHitRange << endl;
634  const TrackingRecHit* currHit = *(iHitRange->first->first);
635 
636  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build(currHit);
637  TSOS currPrSt = (*iHitRange).second;
638 
639  if (debug_info)
640  cout << "curr position" << bestHit->globalPosition();
641  for (TrackingRecHitIterator iHit = (*iHitRange).first->first + 1; iHit != iHitRange->first->second; iHit++) {
642  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build(*iHit);
643  if (debug_info)
644  cout << "curr position" << tmpHit->globalPosition();
645  }
646  }
647  if (debug_info)
648  cout << "Cross check end ..." << endl;
649 
650  //just a simple test if the same hit can be added twice ...
651  // for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ ) //loop over all hits
652 
653  // break;
654 
655  for (iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end();
656  iHitRange++) //loop over detsall hits
657  {
658  //now find the best hit of the detector
659  if (debug_info)
660  cout << "loop2 " << trackHitCandidates.size() << " " << trackHitCandidates.end() - iHitRange << endl;
661 
662  const TrackingRecHit* currHit = *(iHitRange->first->first);
663 
664  processedDets.push_back(currHit->geographicalId().rawId());
665 
666  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build(currHit);
667 
668  if (debug_info)
669  cout << "curr position A" << bestHit->globalPosition() << endl;
670  TSOS currPrSt = (*iHitRange).second;
671  double chi2min = theEstimator->estimate(currPrSt, *bestHit).second;
672 
673  if (debug_info)
674  cout << "Size " << iHitRange->first->second - (*iHitRange).first->first << endl;
675  for (TrackingRecHitIterator iHit = (*iHitRange).first->first + 1; iHit != iHitRange->first->second; iHit++) {
676  if (debug_info)
677  cout << "loop3 "
678  << " " << Hits.end() - iHit << endl;
679 
680  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build(*iHit);
681  if (debug_info)
682  cout << "curr position B" << tmpHit->globalPosition() << endl;
683  double currChi2 = theEstimator->estimate(currPrSt, *tmpHit).second;
684  if (currChi2 < chi2min) {
685  if (debug_info)
686  cout << "Is best hit" << endl;
687  chi2min = currChi2;
688  bestHit = tmpHit;
689  }
690  }
691  //now we have checked the det and can remove the entry from the vector...
692 
693  //if (debug_info) cout << "before erase ..." << endl;
694  //this is to slow ...
695  // hitRangeByDet.erase( (*iHitRange).first,(*iHitRange).first+1 );
696  //if (debug_info) cout << "after erase ..." << endl;
697 
698  if (debug_info)
699  cout << chi2min << endl;
700  //if the track can be added to the trajectory
701  if (chi2min < chi2cut) {
702  if (debug_info)
703  cout << "chi2 fine : " << chi2min << endl;
704 
705  // if (debug_info) cout << "previaous state " << traj.lastMeasurement().updatedState() <<endl;
706  TSOS UpdatedState = theUpdator->update(currPrSt, *bestHit);
707  if (UpdatedState.isValid()) {
708  hits.push_back(bestHit);
709  traj.push(TM(currPrSt, UpdatedState, bestHit, chi2min));
710  if (debug_info)
711  edm::LogInfo("CosmicTrackFinder") << "STATE UPDATED WITH HIT AT POSITION "
712  // <<tmphitbestdet->globalPosition()
713  << UpdatedState << " " << traj.chiSquared();
714  if (debug_info)
715  cout << "Added Hit" << bestHit->globalPosition() << endl;
716  if (debug_info)
717  cout << "State is valid ..." << UpdatedState << endl;
718  //cout << "updated state " << traj.lastMeasurement().updatedState() <<endl;
719 
720  // return; //break;
721  //
722  // TSOS prSt= currPropagator->propagate(traj.lastMeasurement().updatedState(),
723  // tracker->idToDet( bestHit->geographicalId() )->surface());
724  //
725  // if ( prSt.isValid())
726  // cout << "the same hit can be added twice ..." << endl;
727  //
728 
729  break;
730  } else {
731  if (debug_info)
732  edm::LogWarning("CosmicTrackFinder")
733  << " State can not be updated with hit at position " << bestHit->globalPosition();
734  // cout << "State can not be updated with hit at " << bestHit->globalPosition() << endl;
735  }
736  continue;
737  } else {
738  // cout << "chi2 to big : " << chi2min << endl;
739  }
740  if (debug_info)
741  cout << " continue 1 " << endl;
742  }
743  //now we remove all already processed dets from the list ...
744  // hitRangeByDet.erase( (*trackHitCandidates.begin()).first,(*iHitRange).first+1 );
745 
746  if (debug_info)
747  cout << " continue 2 " << endl;
748  }
749  //if this was the last exit
750  while (iHitRange != trackHitCandidates.end());
751  }
752 }
753 
755  int ngoodhits = 0;
756  if (geometry == "MTCC") {
757  auto hits = traj.recHits();
758  for (auto hit = hits.begin(); hit != hits.end(); hit++) {
759  unsigned int iid = (*hit)->hit()->geographicalId().rawId();
760  //CHECK FOR 3 hits r-phi
761  if (((iid >> 0) & 0x3) != 1)
762  ngoodhits++;
763  }
764  } else
765  ngoodhits = traj.foundHits();
766 
767  if (ngoodhits >= theMinHits) {
768  return true;
769  } else {
770  return false;
771  }
772 }
773 
774 //----------------------------------------------------------------------------------------------------------
775 // little helper function that returns false if both hits conatin the same information
776 
778  if (hitA.geographicalId() != hitB.geographicalId())
779  return true;
780  if (hitA.localPosition().x() != hitB.localPosition().x())
781  return true;
782  if (hitA.localPosition().y() != hitB.localPosition().y())
783  return true;
784  if (hitA.localPosition().z() != hitB.localPosition().z())
785  return true;
786 
787  // if (debug_info) cout << hitA.localPosition() << endl;
788  // if (debug_info) cout << hitB << endl;
789 
790  return false;
791 }
792 
793 //----backfitting
794 std::pair<TrajectoryStateOnSurface, const GeomDet*> CRackTrajectoryBuilder::innerState(const Trajectory& traj) const {
795  int lastFitted = 999;
796  int nhits = traj.foundHits();
797  if (nhits < lastFitted + 1)
798  lastFitted = nhits - 1;
799 
800  std::vector<TrajectoryMeasurement> measvec = traj.measurements();
802 
803  bool foundLast = false;
804  int actualLast = -99;
805  for (int i = lastFitted; i >= 0; i--) {
806  if (measvec[i].recHit()->isValid()) {
807  if (!foundLast) {
808  actualLast = i;
809  foundLast = true;
810  }
811  firstHits.push_back(measvec[i].recHit());
812  }
813  }
814  TSOS unscaledState = measvec[actualLast].updatedState();
815  AlgebraicSymMatrix55 C = ROOT::Math::SMatrixIdentity();
816  // C *= 100.;
817 
818  TSOS startingState(unscaledState.localParameters(),
820  unscaledState.surface(),
821  thePropagator->magneticField());
822 
823  // cout << endl << "FitTester starts with state " << startingState << endl;
824 
825  KFTrajectoryFitter backFitter(*thePropagator, KFUpdator(), Chi2MeasurementEstimator(100., 3));
826 
828 
829  // only direction matters in this contest
831 
832  vector<Trajectory> fitres = backFitter.fit(fakeSeed, firstHits, startingState);
833 
834  if (fitres.size() != 1) {
835  // cout << "FitTester: first hits fit failed!" << endl;
836  return std::pair<TrajectoryStateOnSurface, const GeomDet*>();
837  }
838 
839  TrajectoryMeasurement firstMeas = fitres[0].lastMeasurement();
840  const TSOS& firstState = firstMeas.updatedState();
841 
842  // cout << "FitTester: Fitted first state " << firstState << endl;
843  //cout << "FitTester: chi2 = " << fitres[0].chiSquared() << endl;
844 
845  TSOS initialState(
846  firstState.localParameters(), LocalTrajectoryError(C), firstState.surface(), thePropagator->magneticField());
847 
848  return std::pair<TrajectoryStateOnSurface, const GeomDet*>(initialState, firstMeas.recHit()->det());
849 }
#define LogDebug(id)
PropagationDirection direction() const
T getParameter(std::string const &) const
int foundHits() const
Definition: Trajectory.h:206
T getUntrackedParameter(std::string const &, T const &) const
std::vector< TrackingRecHitRange >::iterator TrackingRecHitRangeIterator
std::pair< const_iterator, const_iterator > range
Trajectory createStartingTrajectory(const TrajectorySeed &seed) const
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:263
const LocalTrajectoryParameters & localParameters() const
int init
Definition: HydjetWrapper.h:64
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
T y() const
Definition: PV3DBase.h:60
GlobalPoint globalPosition() const
ConstRecHitContainer recHits() const
Definition: Trajectory.h:186
PropagationDirection
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
std::vector< const TrackingRecHit * >::iterator TrackingRecHitIterator
PropagationDirection const & direction() const
Definition: Trajectory.cc:133
std::pair< TrajectoryStateOnSurface, const GeomDet * > innerState(const Trajectory &traj) const
DataContainer const & measurements() const
Definition: Trajectory.h:178
bool qualityFilter(const Trajectory &traj)
const SurfaceType & surface() const
std::vector< TrajectorySeed > TrajectorySeedCollection
void run(const TrajectorySeedCollection &collseed, const SiStripRecHit2DCollection &collstereo, const SiStripRecHit2DCollection &collrphi, const SiStripMatchedRecHit2DCollection &collmatched, const SiPixelRecHitCollection &collpixel, const edm::EventSetup &es, edm::Event &e, std::vector< Trajectory > &trajoutput)
Runs the algorithm.
recHitContainer::const_iterator const_iterator
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:150
T z() const
Definition: PV3DBase.h:61
void update(const LocalTrajectoryParameters &p, const SurfaceType &aSurface, const MagneticField *field, SurfaceSide side=SurfaceSideDefinition::atCenterOfSurface)
data_type const * data(size_t cell) const
const AlgebraicSymMatrix55 & matrix() const
std::vector< TrajectoryMeasurement > seedMeasurements(const TrajectorySeed &seed) const
const LocalTrajectoryError & localError() const
TSOS startingTSOS(const TrajectorySeed &seed) const
std::shared_ptr< TrackingRecHit const > RecHitPointer
std::vector< ConstRecHitPointer > ConstRecHitContainer
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
Definition: DetId.h:17
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:166
PTrajectoryStateOnDet const & startingState() const
SiStripRecHit2D stereoHit() const
int iseed
Definition: AMPTWrapper.h:134
CRackTrajectoryBuilder(const edm::ParameterSet &conf)
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
bool isDifferentStripReHit2D(const SiStripRecHit2D &hitA, const SiStripRecHit2D &hitB)
range recHits() const
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
void init(const edm::EventSetup &es, bool)
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:50
float chiSquared() const
Definition: Trajectory.h:241
SiStripRecHit2D monoHit() const
unsigned int nHits() const
T get() const
Definition: EventSetup.h:73
std::vector< const TrackingRecHit * > SortHits(const SiStripRecHit2DCollection &collstereo, const SiStripRecHit2DCollection &collrphi, const SiStripMatchedRecHit2DCollection &collmatched, const SiPixelRecHitCollection &collpixel, const TrajectorySeed &seed, const bool bAddSeedHits)
LocalPoint localPosition() const final
GlobalPoint globalPosition() const final
TrajectoryStateOnSurface const & updatedState() const
DetId geographicalId() const
T x() const
Definition: PV3DBase.h:59
void AddHit(Trajectory &traj, const std::vector< const TrackingRecHit * > &Hits, Propagator *currPropagator)
T const * product() const
Definition: ESHandle.h:86
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:50
def move(src, dest)
Definition: eostools.py:511