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  auto const& hitRange = seed.recHits();
225  for (auto ihit = hitRange.begin(); ihit != hitRange.end(); 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.end() - 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  float yref = 0.;
256 
257  if (debug_info)
258  cout << "SEED " << startingTSOS(seed) << endl;
259  if (debug_info)
260  cout << "seed hits size " << seed.nHits() << endl;
261 
262  //seed debugging:
263  // GeomDet *seedDet = tracker->idToDet(seed.startingState().detId());
264 
265  // edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "SEED " << seed.startingState();
266 
267  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "SEED " << startingTSOS(seed);
268  // edm::LogInfo("CRackTrajectoryBuilder::SortHits" << "seed hits size " << seed.nHits();
269 
270  // if (seed.nHits()<2)
271  // return allHits;
272 
273  float_t yMin = 0.;
274  float_t yMax = 0.;
275 
276  int seedHitSize = seed.nHits();
277 
278  vector<int> detIDSeedMatched(seedHitSize);
279  vector<int> detIDSeedRphi(seedHitSize);
280  vector<int> detIDSeedStereo(seedHitSize);
281 
282  auto const& hRange = seed.recHits();
283  for (auto ihit = hRange.begin(); ihit != hRange.end(); ihit++) {
284  // need to find track with lowest (seed_plus)/ highest y (seed_minus)
285  // split matched hits ...
286  const SiStripMatchedRecHit2D* matchedhit = dynamic_cast<const SiStripMatchedRecHit2D*>(&(*ihit));
287 
288  yref = RHBuilder->build(&(*ihit))->globalPosition().y();
289  if (ihit == hRange.begin()) {
290  yMin = yref;
291  yMax = yref;
292  }
293 
294  if (matchedhit) {
295  auto m = matchedhit->monoHit();
296  auto s = matchedhit->stereoHit();
297  float_t yGlobRPhi = RHBuilder->build(&m)->globalPosition().y();
298  float_t yGlobStereo = RHBuilder->build(&s)->globalPosition().y();
299 
300  if (debug_info)
301  cout << "Rphi ..." << yGlobRPhi << endl;
302  if (debug_info)
303  cout << "Stereo ..." << yGlobStereo << endl;
304 
305  if (yGlobStereo < yMin)
306  yMin = yGlobStereo;
307  if (yGlobRPhi < yMin)
308  yMin = yGlobRPhi;
309 
310  if (yGlobStereo > yMax)
311  yMax = yGlobStereo;
312  if (yGlobRPhi > yMax)
313  yMax = yGlobRPhi;
314 
315  detIDSeedMatched.push_back(matchedhit->geographicalId().rawId());
316  detIDSeedRphi.push_back(m.geographicalId().rawId());
317  detIDSeedStereo.push_back(s.geographicalId().rawId());
318 
319  if (bAddSeedHits) {
320  if (useMatchedHits) {
321  hits.push_back((RHBuilder->build(&(*ihit))));
322  } else {
323  if (((yGlobRPhi > yGlobStereo) && seed_plus) || ((yGlobRPhi < yGlobStereo) && !seed_plus)) {
324  hits.push_back((RHBuilder->build(&m)));
325  hits.push_back((RHBuilder->build(&s)));
326  } else {
327  hits.push_back((RHBuilder->build(&s)));
328  hits.push_back((RHBuilder->build(&m)));
329  }
330  }
331  }
332  } else if (bAddSeedHits) {
333  hits.push_back((RHBuilder->build(&(*ihit))));
334  detIDSeedRphi.push_back(ihit->geographicalId().rawId());
335  detIDSeedMatched.push_back(-1);
336  detIDSeedStereo.push_back(-1);
337  }
338 
339  if (yref < yMin)
340  yMin = yref;
341  if (yref > yMax)
342  yMax = yref;
343 
344  // if (bAddSeedHits)
345  // hits.push_back((RHBuilder->build(&(*ihit))));
346 
347  LogDebug("CosmicTrackFinder") << "SEED HITS" << RHBuilder->build(&(*ihit))->globalPosition();
348  if (debug_info)
349  cout << "SEED HITS" << RHBuilder->build(&(*ihit))->globalPosition() << endl;
350  // if (debug_info) cout <<"SEED HITS"<< seed.startingState().parameters() << endl;
351  }
352 
353  yref = (seed_plus) ? yMin : yMax;
354 
356  for (ipix = collpixel.data().begin(); ipix != collpixel.data().end(); ipix++) {
357  float ych = RHBuilder->build(&(*ipix))->globalPosition().y();
358  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
359  allHits.push_back(&(*ipix));
360  }
361 
362  if (useMatchedHits) // use matched
363  {
364  //add the matched hits ...
366 
367  for (istripm = collmatched.data().begin(); istripm != collmatched.data().end(); istripm++) {
368  float ych = RHBuilder->build(&(*istripm))->globalPosition().y();
369 
370  int cDetId = istripm->geographicalId().rawId();
371  bool noSeedDet = (detIDSeedMatched.end() == find(detIDSeedMatched.begin(), detIDSeedMatched.end(), cDetId));
372 
373  if (noSeedDet)
374  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
375  allHits.push_back(&(*istripm));
376  }
377 
378  //add the rpi hits, but only accept hits that are not matched hits
379  for (istrip = collrphi.data().begin(); istrip != collrphi.data().end(); istrip++) {
380  float ych = RHBuilder->build(&(*istrip))->globalPosition().y();
381  StripSubdetector monoDetId(istrip->geographicalId());
382  if (monoDetId.partnerDetId()) {
383  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "this det belongs to a glued det " << ych << endl;
384  continue;
385  }
386  int cDetId = istrip->geographicalId().rawId();
387  bool noSeedDet = (detIDSeedRphi.end() == find(detIDSeedRphi.begin(), detIDSeedRphi.end(), cDetId));
388  if (noSeedDet)
389  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref))) {
390  bool hitIsUnique = true;
391  //now
392  for (istripm = collmatched.data().begin(); istripm != collmatched.data().end(); istripm++) {
393  // if ( isDifferentStripReHit2D ( *istrip, (istripm->stereoHit() ) ) == false)
394  if (isDifferentStripReHit2D(*istrip, (istripm->monoHit())) == false) {
395  hitIsUnique = false;
396  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "rphi hit is in matched hits; y: " << ych << endl;
397  break;
398  }
399  } //end loop over all matched
400  if (hitIsUnique) {
401  // if (debug_info) cout << "adding rphi hit " << &(*istrip) << endl;
402  allHits.push_back(&(*istrip));
403  }
404  }
405  }
406 
407  } else // dont use matched ...
408  {
409  for (istrip = collrphi.data().begin(); istrip != collrphi.data().end(); istrip++) {
410  float ych = RHBuilder->build(&(*istrip))->globalPosition().y();
411  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
412  allHits.push_back(&(*istrip));
413  }
414 
415  for (istrip = collstereo.data().begin(); istrip != collstereo.data().end(); istrip++) {
416  float ych = RHBuilder->build(&(*istrip))->globalPosition().y();
417  if ((seed_plus && (ych < yref)) || (!(seed_plus) && (ych > yref)))
418  allHits.push_back(&(*istrip));
419  }
420  }
421 
422  if (seed_plus)
423  stable_sort(allHits.begin(), allHits.end(), CompareHitY_plus(*tracker));
424  else
425  stable_sort(allHits.begin(), allHits.end(), CompareHitY(*tracker));
426 
427  if (debug_info) {
428  if (debug_info)
429  cout << "all hits" << endl;
430 
431  //starting trajectory
432  Trajectory startingTraj = createStartingTrajectory(seed);
433 
434  if (debug_info)
435  cout << "START " << startingTraj.lastMeasurement().updatedState() << endl;
436  if (debug_info)
437  cout << "START Err" << startingTraj.lastMeasurement().updatedState().localError().matrix() << endl;
438 
439  vector<const TrackingRecHit*>::iterator iHit;
440  for (iHit = allHits.begin(); iHit < allHits.end(); iHit++) {
441  GlobalPoint gphit = RHBuilder->build(*iHit)->globalPosition();
442  if (debug_info)
443  cout << "GH " << gphit << endl;
444 
445  // tracker->idToDet((*iHit)->geographicalId())->surface();
446 
447  TSOS prSt = thePropagator->propagate(startingTraj.lastMeasurement().updatedState(),
448  tracker->idToDet((*iHit)->geographicalId())->surface());
449 
450  if (prSt.isValid()) {
451  if (debug_info)
452  cout << "PR " << prSt.globalPosition() << endl;
453  //if (debug_info) cout << "PR Err" << prSt.localError().matrix() << endl;
454 
455  } else {
456  if (debug_info)
457  cout << "not valid" << endl;
458  }
459  }
460  if (debug_info)
461  cout << "all hits end" << endl;
462  }
463 
464  return allHits;
465 }
466 
468  PTrajectoryStateOnDet pState(seed.startingState());
469  const GeomDet* gdet = (&(*tracker))->idToDet(DetId(pState.detId()));
470  TSOS State = trajectoryStateTransform::transientState(pState, &(gdet->surface()), &(*magfield));
471  return State;
472 }
473 
475  const vector<const TrackingRecHit*>& _Hits,
476  Propagator* currPropagator) {
477  vector<const TrackingRecHit*> Hits = _Hits;
478  if (Hits.empty())
479  return;
480 
481  if (debug_info)
482  cout << "CRackTrajectoryBuilder::AddHit" << endl;
483  if (debug_info)
484  cout << "START " << traj.lastMeasurement().updatedState() << endl;
485 
486  vector<TrackingRecHitRange> hitRangeByDet;
487  TrackingRecHitIterator prevDet;
488 
489  prevDet = Hits.begin();
490  for (TrackingRecHitIterator iHit = Hits.begin(); iHit != Hits.end(); iHit++) {
491  if ((*prevDet)->geographicalId() == (*iHit)->geographicalId())
492  continue;
493 
494  hitRangeByDet.push_back(make_pair(prevDet, iHit));
495  prevDet = iHit;
496  }
497  hitRangeByDet.push_back(make_pair(prevDet, Hits.end()));
498 
500 
501  if (fastPropagation) {
502  for (TrackingRecHitRangeIterator iHitRange = hitRangeByDet.begin(); iHitRange != hitRangeByDet.end(); iHitRange++) {
503  const TrackingRecHit* currHit = *(iHitRange->first);
504  DetId currDet = currHit->geographicalId();
505 
506  TSOS prSt =
507  currPropagator->propagate(traj.lastMeasurement().updatedState(), tracker->idToDet(currDet)->surface());
508 
509  if (!prSt.isValid()) {
510  if (debug_info)
511  cout << "Not Valid: PRST" << prSt.globalPosition();
512  // if (debug_info) cout << "Not Valid: HIT" << *currHit;
513 
514  continue;
515  }
516 
517  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build(currHit);
518  double chi2min = theEstimator->estimate(prSt, *bestHit).second;
519 
520  if (debug_info)
521  cout << "Size " << iHitRange->first - (*iHitRange).second << endl;
522  for (TrackingRecHitIterator iHit = (*iHitRange).first + 1; iHit != iHitRange->second; iHit++) {
523  if (debug_info)
524  cout << "loop3 "
525  << " " << Hits.end() - iHit << endl;
526 
527  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build(*iHit);
528  double currChi2 = theEstimator->estimate(prSt, *tmpHit).second;
529  if (currChi2 < chi2min) {
530  chi2min = currChi2;
531  bestHit = tmpHit;
532  }
533  }
534  //now we have check if the track can be added to the trajectory
535  if (debug_info)
536  cout << chi2min << endl;
537  if (chi2min < chi2cut) {
538  if (debug_info)
539  cout << "chi2 fine : " << chi2min << endl;
540  TSOS UpdatedState = theUpdator->update(prSt, *bestHit);
541  if (UpdatedState.isValid()) {
542  hits.push_back(bestHit);
543  traj.push(TM(prSt, UpdatedState, bestHit, chi2min));
544  if (debug_info)
545  edm::LogInfo("CosmicTrackFinder") << "STATE UPDATED WITH HIT AT POSITION "
546 
547  << bestHit->globalPosition() << UpdatedState << " " << traj.chiSquared();
548  if (debug_info)
549  cout << "STATE UPDATED WITH HIT AT POSITION "
550 
551  << bestHit->globalPosition() << UpdatedState << " " << traj.chiSquared();
552  if (debug_info)
553  cout << "State is valid ..." << endl;
554  break; // now we need to
555  } else {
556  edm::LogWarning("CosmicTrackFinder") << " State can not be updated with hit at position " << endl;
557  TSOS UpdatedState = theUpdator->update(prSt, *bestHit);
558  if (UpdatedState.isValid()) {
559  cout << "NOT! UPDATED WITH HIT AT POSITION "
560 
561  << bestHit->globalPosition() << UpdatedState << " " << traj.chiSquared();
562  }
563  }
564  }
565  }
566  } //simple version end
567  else {
568  //first sort the dets in the order they are traversed by the trajectory
569  // we need three loops:
570  // 1: loop as long as there can be an new hit added to the trajectory
571  // 2: loop over all dets that might be hit
572  // 3: loop over all hits on a certain det
573 
574  std::vector<std::pair<TrackingRecHitRangeIterator, TSOS> > trackHitCandidates;
575  std::vector<std::pair<TrackingRecHitRangeIterator, TSOS> >::iterator iHitRange;
576  std::vector<uint32_t> processedDets;
577  do {
578  //create vector of possibly hit detectors...
579  trackHitCandidates.clear();
580  DetId currDet;
581  for (TrackingRecHitRangeIterator iHit = hitRangeByDet.begin(); iHit != hitRangeByDet.end(); iHit++) {
582  const TrackingRecHit* currHit = *(iHit->first);
583  currDet = currHit->geographicalId();
584 
585  if (find(processedDets.begin(), processedDets.end(), currDet.rawId()) != processedDets.end())
586  continue;
587 
588  TSOS prSt =
589  currPropagator->propagate(traj.lastMeasurement().updatedState(), tracker->idToDet(currDet)->surface());
590  if ((!prSt.isValid()) ||
591  (theEstimator->Chi2MeasurementEstimatorBase::estimate(prSt, tracker->idToDet(currDet)->surface()) == false))
592  // if ( ( !prSt.isValid() ) || (theEstimator->estimate(prSt,tracker->idToDet(currDet)->surface() ) == false) )
593  continue;
594 
595  trackHitCandidates.push_back(make_pair(iHit, prSt));
596  }
597 
598  if (trackHitCandidates.empty())
599  break;
600 
601  if (debug_info)
602  cout << Hits.size() << " (int) trackHitCandidates.begin() " << trackHitCandidates.size() << endl;
603  if (debug_info)
604  cout << "Before sorting ... " << endl;
605 
606  if (debug_info)
607  for (iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++) {
608  if (debug_info)
609  cout << (tracker->idToDet((*(iHitRange->first->first))->geographicalId()))->position();
610  }
611  if (debug_info)
612  cout << endl;
613 
614  stable_sort(trackHitCandidates.begin(),
615  trackHitCandidates.end(),
617 
618  if (debug_info)
619  cout << "After sorting ... " << endl;
620  if (debug_info) {
621  for (iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++) {
622  if (debug_info)
623  cout << (tracker->idToDet((*(iHitRange->first->first))->geographicalId()))->position();
624  }
625  cout << endl;
626  }
627 
628  for (iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++) //loop over dets
629  {
630  //now find the best hit of the detector
631  if (debug_info)
632  cout << "loop2 " << trackHitCandidates.size() << " " << trackHitCandidates.end() - iHitRange << endl;
633  const TrackingRecHit* currHit = *(iHitRange->first->first);
634 
635  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build(currHit);
636  TSOS currPrSt = (*iHitRange).second;
637 
638  if (debug_info)
639  cout << "curr position" << bestHit->globalPosition();
640  for (TrackingRecHitIterator iHit = (*iHitRange).first->first + 1; iHit != iHitRange->first->second; iHit++) {
641  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build(*iHit);
642  if (debug_info)
643  cout << "curr position" << tmpHit->globalPosition();
644  }
645  }
646  if (debug_info)
647  cout << "Cross check end ..." << endl;
648 
649  //just a simple test if the same hit can be added twice ...
650  // for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ ) //loop over all hits
651 
652  // break;
653 
654  for (iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end();
655  iHitRange++) //loop over detsall hits
656  {
657  //now find the best hit of the detector
658  if (debug_info)
659  cout << "loop2 " << trackHitCandidates.size() << " " << trackHitCandidates.end() - iHitRange << endl;
660 
661  const TrackingRecHit* currHit = *(iHitRange->first->first);
662 
663  processedDets.push_back(currHit->geographicalId().rawId());
664 
665  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build(currHit);
666 
667  if (debug_info)
668  cout << "curr position A" << bestHit->globalPosition() << endl;
669  TSOS currPrSt = (*iHitRange).second;
670  double chi2min = theEstimator->estimate(currPrSt, *bestHit).second;
671 
672  if (debug_info)
673  cout << "Size " << iHitRange->first->second - (*iHitRange).first->first << endl;
674  for (TrackingRecHitIterator iHit = (*iHitRange).first->first + 1; iHit != iHitRange->first->second; iHit++) {
675  if (debug_info)
676  cout << "loop3 "
677  << " " << Hits.end() - iHit << endl;
678 
679  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build(*iHit);
680  if (debug_info)
681  cout << "curr position B" << tmpHit->globalPosition() << endl;
682  double currChi2 = theEstimator->estimate(currPrSt, *tmpHit).second;
683  if (currChi2 < chi2min) {
684  if (debug_info)
685  cout << "Is best hit" << endl;
686  chi2min = currChi2;
687  bestHit = tmpHit;
688  }
689  }
690  //now we have checked the det and can remove the entry from the vector...
691 
692  //if (debug_info) cout << "before erase ..." << endl;
693  //this is to slow ...
694  // hitRangeByDet.erase( (*iHitRange).first,(*iHitRange).first+1 );
695  //if (debug_info) cout << "after erase ..." << endl;
696 
697  if (debug_info)
698  cout << chi2min << endl;
699  //if the track can be added to the trajectory
700  if (chi2min < chi2cut) {
701  if (debug_info)
702  cout << "chi2 fine : " << chi2min << endl;
703 
704  // if (debug_info) cout << "previaous state " << traj.lastMeasurement().updatedState() <<endl;
705  TSOS UpdatedState = theUpdator->update(currPrSt, *bestHit);
706  if (UpdatedState.isValid()) {
707  hits.push_back(bestHit);
708  traj.push(TM(currPrSt, UpdatedState, bestHit, chi2min));
709  if (debug_info)
710  edm::LogInfo("CosmicTrackFinder") << "STATE UPDATED WITH HIT AT POSITION "
711  // <<tmphitbestdet->globalPosition()
712  << UpdatedState << " " << traj.chiSquared();
713  if (debug_info)
714  cout << "Added Hit" << bestHit->globalPosition() << endl;
715  if (debug_info)
716  cout << "State is valid ..." << UpdatedState << endl;
717  //cout << "updated state " << traj.lastMeasurement().updatedState() <<endl;
718 
719  // return; //break;
720  //
721  // TSOS prSt= currPropagator->propagate(traj.lastMeasurement().updatedState(),
722  // tracker->idToDet( bestHit->geographicalId() )->surface());
723  //
724  // if ( prSt.isValid())
725  // cout << "the same hit can be added twice ..." << endl;
726  //
727 
728  break;
729  } else {
730  if (debug_info)
731  edm::LogWarning("CosmicTrackFinder")
732  << " State can not be updated with hit at position " << bestHit->globalPosition();
733  // cout << "State can not be updated with hit at " << bestHit->globalPosition() << endl;
734  }
735  continue;
736  } else {
737  // cout << "chi2 to big : " << chi2min << endl;
738  }
739  if (debug_info)
740  cout << " continue 1 " << endl;
741  }
742  //now we remove all already processed dets from the list ...
743  // hitRangeByDet.erase( (*trackHitCandidates.begin()).first,(*iHitRange).first+1 );
744 
745  if (debug_info)
746  cout << " continue 2 " << endl;
747  }
748  //if this was the last exit
749  while (iHitRange != trackHitCandidates.end());
750  }
751 }
752 
754  int ngoodhits = 0;
755  if (geometry == "MTCC") {
756  auto hits = traj.recHits();
757  for (auto hit = hits.begin(); hit != hits.end(); hit++) {
758  unsigned int iid = (*hit)->hit()->geographicalId().rawId();
759  //CHECK FOR 3 hits r-phi
760  if (((iid >> 0) & 0x3) != 1)
761  ngoodhits++;
762  }
763  } else
764  ngoodhits = traj.foundHits();
765 
766  if (ngoodhits >= theMinHits) {
767  return true;
768  } else {
769  return false;
770  }
771 }
772 
773 //----------------------------------------------------------------------------------------------------------
774 // little helper function that returns false if both hits conatin the same information
775 
777  if (hitA.geographicalId() != hitB.geographicalId())
778  return true;
779  if (hitA.localPosition().x() != hitB.localPosition().x())
780  return true;
781  if (hitA.localPosition().y() != hitB.localPosition().y())
782  return true;
783  if (hitA.localPosition().z() != hitB.localPosition().z())
784  return true;
785 
786  // if (debug_info) cout << hitA.localPosition() << endl;
787  // if (debug_info) cout << hitB << endl;
788 
789  return false;
790 }
791 
792 //----backfitting
793 std::pair<TrajectoryStateOnSurface, const GeomDet*> CRackTrajectoryBuilder::innerState(const Trajectory& traj) const {
794  int lastFitted = 999;
795  int nhits = traj.foundHits();
796  if (nhits < lastFitted + 1)
797  lastFitted = nhits - 1;
798 
799  std::vector<TrajectoryMeasurement> measvec = traj.measurements();
801 
802  bool foundLast = false;
803  int actualLast = -99;
804  for (int i = lastFitted; i >= 0; i--) {
805  if (measvec[i].recHit()->isValid()) {
806  if (!foundLast) {
807  actualLast = i;
808  foundLast = true;
809  }
810  firstHits.push_back(measvec[i].recHit());
811  }
812  }
813  TSOS unscaledState = measvec[actualLast].updatedState();
814  AlgebraicSymMatrix55 C = ROOT::Math::SMatrixIdentity();
815  // C *= 100.;
816 
817  TSOS startingState(unscaledState.localParameters(),
819  unscaledState.surface(),
820  thePropagator->magneticField());
821 
822  // cout << endl << "FitTester starts with state " << startingState << endl;
823 
824  KFTrajectoryFitter backFitter(*thePropagator, KFUpdator(), Chi2MeasurementEstimator(100., 3));
825 
827 
828  // only direction matters in this contest
830 
831  vector<Trajectory> fitres = backFitter.fit(fakeSeed, firstHits, startingState);
832 
833  if (fitres.size() != 1) {
834  // cout << "FitTester: first hits fit failed!" << endl;
835  return std::pair<TrajectoryStateOnSurface, const GeomDet*>();
836  }
837 
838  TrajectoryMeasurement firstMeas = fitres[0].lastMeasurement();
839  const TSOS& firstState = firstMeas.updatedState();
840 
841  // cout << "FitTester: Fitted first state " << firstState << endl;
842  //cout << "FitTester: chi2 = " << fitres[0].chiSquared() << endl;
843 
844  TSOS initialState(
845  firstState.localParameters(), LocalTrajectoryError(C), firstState.surface(), thePropagator->magneticField());
846 
847  return std::pair<TrajectoryStateOnSurface, const GeomDet*>(initialState, firstMeas.recHit()->det());
848 }
edm::ESHandle::product
T const * product() const
Definition: ESHandle.h:86
init
int init
Definition: HydjetWrapper.h:64
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
GeomDet
Definition: GeomDet.h:27
CRackTrajectoryBuilder::run
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.
Definition: CRackTrajectoryBuilder.cc:82
TrajectorySeedCollection
std::vector< TrajectorySeed > TrajectorySeedCollection
Definition: TrajectorySeedCollection.h:6
hfClusterShapes_cfi.hits
hits
Definition: hfClusterShapes_cfi.py:5
hcaldqm::flag::State
State
Definition: Flag.h:13
Trajectory::chiSquared
float chiSquared() const
Definition: Trajectory.h:241
PV3DBase::x
T x() const
Definition: PV3DBase.h:59
Trajectory::direction
PropagationDirection const & direction() const
Definition: Trajectory.cc:133
trackAssociatorByChi2_cfi.chi2cut
chi2cut
Definition: trackAssociatorByChi2_cfi.py:4
CRackTrajectoryBuilder::isDifferentStripReHit2D
bool isDifferentStripReHit2D(const SiStripRecHit2D &hitA, const SiStripRecHit2D &hitB)
Definition: CRackTrajectoryBuilder.cc:776
CRackTrajectoryBuilder::createStartingTrajectory
Trajectory createStartingTrajectory(const TrajectorySeed &seed) const
Definition: CRackTrajectoryBuilder.cc:214
TransientRecHitRecord.h
TrajectoryStateOnSurface::globalPosition
GlobalPoint globalPosition() const
Definition: TrajectoryStateOnSurface.h:65
gather_cfg.cout
cout
Definition: gather_cfg.py:144
geometry
Definition: geometry.py:1
StripSubdetector
Definition: StripSubdetector.h:12
TransientRecHitRecord
Definition: TransientRecHitRecord.h:14
CRackTrajectoryBuilder::startingTSOS
TSOS startingTSOS(const TrajectorySeed &seed) const
Definition: CRackTrajectoryBuilder.cc:467
edmNew::DetSetVector::const_iterator
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
Definition: DetSetVectorNew.h:197
oppositeToMomentum
Definition: PropagationDirection.h:4
SiStripRecHit2D
Definition: SiStripRecHit2D.h:7
TransientTrackingRecHit.h
TrajectoryMeasurement::updatedState
TrajectoryStateOnSurface const & updatedState() const
Definition: TrajectoryMeasurement.h:184
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
photonAnalyzer_cfi.yMin
yMin
Definition: photonAnalyzer_cfi.py:86
LocalTrajectoryError::matrix
const AlgebraicSymMatrix55 & matrix() const
Definition: LocalTrajectoryError.h:60
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
TrackingRecHit::RecHitPointer
std::shared_ptr< TrackingRecHit const > RecHitPointer
Definition: TrackingRecHit.h:24
Trajectory::foundHits
int foundHits() const
Definition: Trajectory.h:206
CompareHitY_plus
Definition: CosmicTrajectoryBuilder.h:58
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
CRackTrajectoryBuilder::seedMeasurements
std::vector< TrajectoryMeasurement > seedMeasurements(const TrajectorySeed &seed) const
Definition: CRackTrajectoryBuilder.cc:222
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
CRackTrajectoryBuilder::init
void init(const edm::EventSetup &es, bool)
Definition: CRackTrajectoryBuilder.cc:50
TrackingRecHit::geographicalId
DetId geographicalId() const
Definition: TrackingRecHit.h:120
rpcPointValidation_cfi.recHit
recHit
Definition: rpcPointValidation_cfi.py:7
groupFilesInBlocks.reverse
reverse
Definition: groupFilesInBlocks.py:131
PV3DBase::z
T z() const
Definition: PV3DBase.h:61
CRackTrajectoryBuilder::CRackTrajectoryBuilder
CRackTrajectoryBuilder(const edm::ParameterSet &conf)
Definition: CRackTrajectoryBuilder.cc:30
fileCollector.seed
seed
Definition: fileCollector.py:127
CRackTrajectoryBuilder::CompareDetByTraj
Definition: CRackTrajectoryBuilder.h:140
IdealMagneticFieldRecord
Definition: IdealMagneticFieldRecord.h:11
Propagator
Definition: Propagator.h:44
DetId
Definition: DetId.h:17
GeomDet::surface
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
TrajectoryStateOnSurface
Definition: TrajectoryStateOnSurface.h:16
alignCSCRings.s
s
Definition: alignCSCRings.py:92
PropagatorWithMaterial
Definition: PropagatorWithMaterial.h:25
edm::EventSetup::get
T get() const
Definition: EventSetup.h:80
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
TrackerDigiGeometryRecord
Definition: TrackerDigiGeometryRecord.h:15
Chi2MeasurementEstimator_cfi.Chi2MeasurementEstimator
Chi2MeasurementEstimator
Definition: Chi2MeasurementEstimator_cfi.py:5
TrajectoryStateWithArbitraryError.h
IdealMagneticFieldRecord.h
edm::ESHandle< TransientTrackingRecHitBuilder >
SiStripMatchedRecHit2D::stereoHit
SiStripRecHit2D stereoHit() const
Definition: SiStripMatchedRecHit2D.h:25
CRackTrajectoryBuilder::AddHit
void AddHit(Trajectory &traj, const std::vector< const TrackingRecHit * > &Hits, Propagator *currPropagator)
Definition: CRackTrajectoryBuilder.cc:474
Point3DBase< float, GlobalTag >
multiplicitycorr_cfi.yMax
yMax
Definition: multiplicitycorr_cfi.py:6
nhits
Definition: HIMultiTrackSelector.h:42
BasicSingleTrajectoryState
Definition: BasicSingleTrajectoryState.h:10
CRackTrajectoryBuilder::SortHits
std::vector< const TrackingRecHit * > SortHits(const SiStripRecHit2DCollection &collstereo, const SiStripRecHit2DCollection &collrphi, const SiStripMatchedRecHit2DCollection &collmatched, const SiPixelRecHitCollection &collpixel, const TrajectorySeed &seed, const bool bAddSeedHits)
Definition: CRackTrajectoryBuilder.cc:243
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TrajectoryStateOnSurface::localParameters
const LocalTrajectoryParameters & localParameters() const
Definition: TrajectoryStateOnSurface.h:73
PbPb_ZMuSkimMuonDPG_cff.tracker
tracker
Definition: PbPb_ZMuSkimMuonDPG_cff.py:60
LocalTrajectoryError
Definition: LocalTrajectoryError.h:20
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:223
edm::ParameterSet
Definition: ParameterSet.h:47
iseed
int iseed
Definition: AMPTWrapper.h:134
TrackingRecHit::ConstRecHitContainer
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: TrackingRecHit.h:32
Trajectory::lastMeasurement
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:150
State
State
Definition: RPixErrorChecker.h:15
BaseTrackerRecHit::globalPosition
GlobalPoint globalPosition() const final
Definition: BaseTrackerRecHit.h:85
SiStripMatchedRecHit2D::monoHit
SiStripRecHit2D monoHit() const
Definition: SiStripMatchedRecHit2D.h:26
PV3DBase::y
T y() const
Definition: PV3DBase.h:60
TrajectoryStateOnSurface::update
void update(const LocalTrajectoryParameters &p, const SurfaceType &aSurface, const MagneticField *field, SurfaceSide side=SurfaceSideDefinition::atCenterOfSurface)
Definition: TrajectoryStateOnSurface.cc:6
trajectoryStateTransform::transientState
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
Definition: TrajectoryStateTransform.cc:35
edm::EventSetup
Definition: EventSetup.h:57
CRackTrajectoryBuilder.h
CRackTrajectoryBuilder::TrackingRecHitRangeIterator
std::vector< TrackingRecHitRange >::iterator TrackingRecHitRangeIterator
Definition: CRackTrajectoryBuilder.h:131
Propagator::propagate
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:50
get
#define get
Trajectory::push
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:50
Trajectory::measurements
DataContainer const & measurements() const
Definition: Trajectory.h:178
BasicSingleTrajectoryState.h
CRackTrajectoryBuilder::innerState
std::pair< TrajectoryStateOnSurface, const GeomDet * > innerState(const Trajectory &traj) const
Definition: CRackTrajectoryBuilder.cc:793
TrackingRecHit
Definition: TrackingRecHit.h:21
TrajectoryMeasurement::recHit
ConstRecHitPointer const & recHit() const
Definition: TrajectoryMeasurement.h:190
Trajectory::recHits
ConstRecHitContainer recHits() const
Definition: Trajectory.h:186
CRackTrajectoryBuilder::~CRackTrajectoryBuilder
~CRackTrajectoryBuilder()
Definition: CRackTrajectoryBuilder.cc:46
edmNew::DetSetVector
Definition: DetSetNew.h:13
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
DetId::rawId
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
BaseTrackerRecHit::localPosition
LocalPoint localPosition() const override
Definition: BaseTrackerRecHit.h:56
Trajectory::firstMeasurement
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:166
CRackTrajectoryBuilder::qualityFilter
bool qualityFilter(const Trajectory &traj)
Definition: CRackTrajectoryBuilder.cc:753
edmNew::DetSetVector::data
data_type const * data(size_t cell) const
Definition: DetSetVectorNew.h:560
gen::C
C
Definition: PomwigHadronizer.cc:78
SiStripMatchedRecHit2D
Definition: SiStripMatchedRecHit2D.h:8
Trajectory
Definition: Trajectory.h:38
KFTrajectorySmoother
Definition: KFTrajectorySmoother.h:20
TrackingComponentsRecord.h
PropagationDirection
PropagationDirection
Definition: PropagationDirection.h:4
Trajectory::seed
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:263
TrajectorySeed
Definition: TrajectorySeed.h:18
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
TrajectoryStateOnSurface::surface
const SurfaceType & surface() const
Definition: TrajectoryStateOnSurface.h:78
mps_fire.result
result
Definition: mps_fire.py:311
CompareHitY
Definition: CosmicTrajectoryBuilder.h:44
PTrajectoryStateOnDet
Definition: PTrajectoryStateOnDet.h:10
ParameterSet.h
OwnVector.h
edm::Event
Definition: Event.h:73
TrajectoryStateOnSurface::localError
const LocalTrajectoryError & localError() const
Definition: TrajectoryStateOnSurface.h:77
AlgebraicSymMatrix55
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
Definition: AlgebraicROOTObjects.h:23
TrajectoryMeasurement
Definition: TrajectoryMeasurement.h:25
volumeBasedMagneticField_160812_cfi.magfield
magfield
Definition: volumeBasedMagneticField_160812_cfi.py:11
GlobalPoint.h
alongMomentum
Definition: PropagationDirection.h:4
KFUpdator
Definition: KFUpdator.h:32
TrajectoryStateOnSurface::isValid
bool isValid() const
Definition: TrajectoryStateOnSurface.h:54
hit
Definition: SiStripHitEffFromCalibTree.cc:88
CRackTrajectoryBuilder::TrackingRecHitIterator
std::vector< const TrackingRecHit * >::iterator TrackingRecHitIterator
Definition: CRackTrajectoryBuilder.h:128
edm::OwnVector< TrackingRecHit >
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37