CMS 3D CMS Logo

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