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 
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
64  es.get<IdealMagneticFieldRecord>().get(magfield);
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.empty())
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  for(istripm=collmatched.data().begin();istripm!=collmatched.data().end();istripm++)
456  {
457  // if ( isDifferentStripReHit2D ( *istrip, (istripm->stereoHit() ) ) == false)
458  if ( isDifferentStripReHit2D ( *istrip, (istripm->monoHit() ) ) == false)
459  {
460  hitIsUnique = false;
461  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "rphi hit is in matched hits; y: " << ych << endl;
462  break;
463  }
464  } //end loop over all matched
465  if (hitIsUnique)
466  {
467  // if (debug_info) cout << "adding rphi hit " << &(*istrip) << endl;
468  allHits.push_back(&(*istrip));
469  }
470  }
471  }
472 
473 
474  }
475  else // dont use matched ...
476  {
477 
478  for(istrip=collrphi.data().begin();istrip!=collrphi.data().end();istrip++){
479  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
480  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
481  allHits.push_back(&(*istrip));
482  }
483 
484  for(istrip=collstereo.data().begin();istrip!=collstereo.data().end();istrip++){
485  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
486  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
487  allHits.push_back(&(*istrip));
488  }
489  }
490 
491 
492 
493  if (seed_plus)
494  stable_sort(allHits.begin(),allHits.end(),CompareHitY_plus(*tracker));
495  else
496  stable_sort(allHits.begin(),allHits.end(),CompareHitY(*tracker));
497 
498  if (debug_info)
499  {
500  if (debug_info) cout << "all hits" << endl;
501 
502  //starting trajectory
503  Trajectory startingTraj = createStartingTrajectory(seed);
504 
505  if (debug_info) cout << "START " << startingTraj.lastMeasurement().updatedState() << endl;
506  if (debug_info) cout << "START Err" << startingTraj.lastMeasurement().updatedState().localError().matrix() << endl;
507 
508 
509  vector<const TrackingRecHit*>::iterator iHit;
510  for (iHit=allHits.begin(); iHit<allHits.end(); iHit++)
511  {
512  GlobalPoint gphit=RHBuilder->build(*iHit)->globalPosition();
513  if (debug_info) cout << "GH " << gphit << endl;
514 
515  // tracker->idToDet((*iHit)->geographicalId())->surface();
516 
517  TSOS prSt = thePropagator->propagate(startingTraj.lastMeasurement().updatedState(),
518  tracker->idToDet((*iHit)->geographicalId())->surface());
519 
520  if(prSt.isValid())
521  {
522  if (debug_info) cout << "PR " << prSt.globalPosition() << endl;
523  //if (debug_info) cout << "PR Err" << prSt.localError().matrix() << endl;
524 
525  }
526  else
527  {
528  if (debug_info) cout << "not valid" << endl;
529  }
530  }
531  if (debug_info) cout << "all hits end" << endl;
532  }
533 
534 
535  return allHits;
536 }
537 
540 {
541  PTrajectoryStateOnDet pState( seed.startingState());
542  const GeomDet* gdet = (&(*tracker))->idToDet(DetId(pState.detId()));
543  TSOS State= trajectoryStateTransform::transientState( pState, &(gdet->surface()),
544  &(*magfield));
545  return State;
546 
547 }
548 
550  const vector<const TrackingRecHit*>& _Hits, Propagator *currPropagator){
551  vector<const TrackingRecHit*> Hits = _Hits;
552  if ( Hits.empty() )
553  return;
554 
555  if (debug_info) cout << "CRackTrajectoryBuilder::AddHit" << endl;
556  if (debug_info) cout << "START " << traj.lastMeasurement().updatedState() << endl;
557 
558  vector <TrackingRecHitRange> hitRangeByDet;
559  TrackingRecHitIterator prevDet;
560 
561  prevDet = Hits.begin();
562  for( TrackingRecHitIterator iHit = Hits.begin(); iHit != Hits.end(); iHit++ )
563  {
564  if ( (*prevDet)->geographicalId() == (*iHit)->geographicalId() )
565  continue;
566 
567  hitRangeByDet.push_back( make_pair( prevDet, iHit ) );
568  prevDet = iHit;
569  }
570  hitRangeByDet.push_back( make_pair( prevDet, Hits.end() ) );
571 
573 
574  if (fastPropagation) {
575  for( TrackingRecHitRangeIterator iHitRange = hitRangeByDet.begin(); iHitRange != hitRangeByDet.end(); iHitRange++ )
576  {
577  const TrackingRecHit *currHit = *(iHitRange->first);
578  DetId currDet = currHit->geographicalId();
579 
580  TSOS prSt= currPropagator->propagate(traj.lastMeasurement().updatedState(),
581  tracker->idToDet(currDet)->surface());
582 
583  if ( !prSt.isValid())
584  {
585  if (debug_info) cout << "Not Valid: PRST" << prSt.globalPosition();
586  // if (debug_info) cout << "Not Valid: HIT" << *currHit;
587 
588 
589  continue;
590  }
591 
592  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build( currHit );
593  double chi2min = theEstimator->estimate( prSt, *bestHit).second;
594 
595  if (debug_info) cout << "Size " << iHitRange->first - (*iHitRange).second << endl;
596  for( TrackingRecHitIterator iHit = (*iHitRange).first+1; iHit != iHitRange->second; iHit++ )
597  {
598  if (debug_info) cout << "loop3 " <<" "<< Hits.end() - iHit << endl;
599 
600  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build( *iHit );
601  double currChi2 = theEstimator->estimate(prSt, *tmpHit).second;
602  if ( currChi2 < chi2min )
603  {
604  chi2min = currChi2;
605  bestHit = tmpHit;
606  }
607  }
608  //now we have check if the track can be added to the trajectory
609  if (debug_info) cout << chi2min << endl;
610  if (chi2min < chi2cut)
611  {
612  if (debug_info) cout << "chi2 fine : " << chi2min << endl;
613  TSOS UpdatedState= theUpdator->update( prSt, *bestHit );
614  if (UpdatedState.isValid()){
615  hits.push_back(bestHit);
616  traj.push( TM(prSt,UpdatedState, bestHit, chi2min) );
617  if (debug_info) edm::LogInfo("CosmicTrackFinder") <<
618  "STATE UPDATED WITH HIT AT POSITION "
619 
620  << bestHit->globalPosition()
621  <<UpdatedState<<" "
622  <<traj.chiSquared();
623  if (debug_info) cout <<
624  "STATE UPDATED WITH HIT AT POSITION "
625 
626  << bestHit->globalPosition()
627  <<UpdatedState<<" "
628  <<traj.chiSquared();
629  if (debug_info) cout << "State is valid ..." << endl;
630  break; // now we need to
631  }
632  else
633  {
634  edm::LogWarning("CosmicTrackFinder")<<" State can not be updated with hit at position " << endl;
635  TSOS UpdatedState= theUpdator->update( prSt, *bestHit );
636  if (UpdatedState.isValid()){
637  cout <<
638  "NOT! UPDATED WITH HIT AT POSITION "
639 
640  << bestHit->globalPosition()
641  <<UpdatedState<<" "
642  <<traj.chiSquared();
643 
644  }
645  }
646  }
647  }
648  } //simple version end
649  else
650  {
651  //first sort the dets in the order they are traversed by the trajectory
652  // we need three loops:
653  // 1: loop as long as there can be an new hit added to the trajectory
654  // 2: loop over all dets that might be hit
655  // 3: loop over all hits on a certain det
656 
657 
658  std::vector < std::pair<TrackingRecHitRangeIterator, TSOS> > trackHitCandidates;
659  std::vector <std::pair<TrackingRecHitRangeIterator, TSOS> >::iterator iHitRange;
660  std::vector <uint32_t> processedDets;
661  do
662  {
663 
664  //create vector of possibly hit detectors...
665  trackHitCandidates.clear();
666  DetId currDet;
667  for( TrackingRecHitRangeIterator iHit = hitRangeByDet.begin(); iHit != hitRangeByDet.end(); iHit++ )
668  {
669  const TrackingRecHit *currHit = *(iHit->first);
670  currDet = currHit->geographicalId();
671 
672  if ( find(processedDets.begin(), processedDets.end(), currDet.rawId()) != processedDets.end() )
673  continue;
674 
675  TSOS prSt= currPropagator->propagate(traj.lastMeasurement().updatedState(),
676  tracker->idToDet(currDet)->surface());
677  if ( ( !prSt.isValid() ) || (theEstimator->Chi2MeasurementEstimatorBase::estimate(prSt,tracker->idToDet(currDet)->surface() ) == false) )
678  // if ( ( !prSt.isValid() ) || (theEstimator->estimate(prSt,tracker->idToDet(currDet)->surface() ) == false) )
679  continue;
680 
681  trackHitCandidates.push_back( make_pair(iHit, prSt) );
682  }
683 
684  if (trackHitCandidates.empty())
685  break;
686 
687  if (debug_info) cout << Hits.size() << " (int) trackHitCandidates.begin() " << trackHitCandidates.size() << endl;
688  if (debug_info) cout << "Before sorting ... " << endl;
689 
690  if (debug_info)
691  for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ )
692  {
693  if (debug_info) cout << (tracker->idToDet((*(iHitRange->first->first))->geographicalId()))->position();
694  }
695  if (debug_info) cout << endl;
696 
697 
698  stable_sort( trackHitCandidates.begin(), trackHitCandidates.end(), CompareDetByTraj(traj.lastMeasurement().updatedState()) );
699 
700  if (debug_info) cout << "After sorting ... " << endl;
701  if (debug_info)
702  {
703  for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ )
704  {
705  if (debug_info) cout << (tracker->idToDet((*(iHitRange->first->first))->geographicalId()))->position();
706  }
707  cout << endl;
708  }
709 
710  for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ ) //loop over dets
711  {
712 
713  //now find the best hit of the detector
714  if (debug_info) cout << "loop2 " << trackHitCandidates.size() <<" " << trackHitCandidates.end() - iHitRange << endl;
715  const TrackingRecHit *currHit = *(iHitRange->first->first);
716 
717  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build( currHit );
718  TSOS currPrSt = (*iHitRange).second;
719 
720  if (debug_info) cout << "curr position" << bestHit->globalPosition();
721  for( TrackingRecHitIterator iHit = (*iHitRange).first->first+1; iHit != iHitRange->first->second; iHit++ )
722  {
723  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build( *iHit );
724  if (debug_info) cout << "curr position" << tmpHit->globalPosition() ;
725 
726  }
727  }
728  if (debug_info) cout << "Cross check end ..." << endl;
729 
730 
731  //just a simple test if the same hit can be added twice ...
732  // for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ ) //loop over all hits
733 
734  // break;
735 
736  for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ ) //loop over detsall hits
737  {
738 
739  //now find the best hit of the detector
740  if (debug_info) cout << "loop2 " << trackHitCandidates.size() <<" " << trackHitCandidates.end() - iHitRange << endl;
741 
742  const TrackingRecHit *currHit = *(iHitRange->first->first);
743 
744  processedDets.push_back(currHit->geographicalId().rawId());
745 
746 
747  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build( currHit );
748 
749  if (debug_info) cout << "curr position A" << bestHit->globalPosition() << endl;
750  TSOS currPrSt = (*iHitRange).second;
751  double chi2min = theEstimator->estimate( currPrSt, *bestHit).second;
752 
753  if (debug_info) cout << "Size " << iHitRange->first->second - (*iHitRange).first->first << endl;
754  for( TrackingRecHitIterator iHit = (*iHitRange).first->first+1; iHit != iHitRange->first->second; iHit++ )
755  {
756  if (debug_info) cout << "loop3 " <<" "<< Hits.end() - iHit << endl;
757 
758  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build( *iHit );
759  if (debug_info) cout << "curr position B" << tmpHit->globalPosition() << endl;
760  double currChi2 = theEstimator->estimate(currPrSt, *tmpHit).second;
761  if ( currChi2 < chi2min )
762  {
763  if (debug_info) cout << "Is best hit" << endl;
764  chi2min = currChi2;
765  bestHit = tmpHit;
766  }
767  }
768  //now we have checked the det and can remove the entry from the vector...
769 
770  //if (debug_info) cout << "before erase ..." << endl;
771  //this is to slow ...
772  // hitRangeByDet.erase( (*iHitRange).first,(*iHitRange).first+1 );
773  //if (debug_info) cout << "after erase ..." << endl;
774 
775  if (debug_info) cout << chi2min << endl;
776  //if the track can be added to the trajectory
777  if (chi2min < chi2cut)
778  {
779  if (debug_info) cout << "chi2 fine : " << chi2min << endl;
780 
781  // if (debug_info) cout << "previaous state " << traj.lastMeasurement().updatedState() <<endl;
782  TSOS UpdatedState= theUpdator->update( currPrSt, *bestHit );
783  if (UpdatedState.isValid()){
784 
785  hits.push_back(bestHit);
786  traj.push( TM(currPrSt,UpdatedState, bestHit, chi2min) );
787  if (debug_info) edm::LogInfo("CosmicTrackFinder") <<
788  "STATE UPDATED WITH HIT AT POSITION "
789  // <<tmphitbestdet->globalPosition()
790  <<UpdatedState<<" "
791  <<traj.chiSquared();
792  if (debug_info) cout << "Added Hit" << bestHit->globalPosition() << endl;
793  if (debug_info) cout << "State is valid ..." << UpdatedState << endl;
794  //cout << "updated state " << traj.lastMeasurement().updatedState() <<endl;
795 
796  // return; //break;
797  //
798  // TSOS prSt= currPropagator->propagate(traj.lastMeasurement().updatedState(),
799  // tracker->idToDet( bestHit->geographicalId() )->surface());
800  //
801  // if ( prSt.isValid())
802  // cout << "the same hit can be added twice ..." << endl;
803  //
804 
805  break;
806  }
807  else
808  {
809  if (debug_info) edm::LogWarning("CosmicTrackFinder")<<" State can not be updated with hit at position "
810  << bestHit->globalPosition();
811  // cout << "State can not be updated with hit at " << bestHit->globalPosition() << endl;
812  }
813  continue;
814  }
815  else
816  {
817  // cout << "chi2 to big : " << chi2min << endl;
818  }
819  if (debug_info) cout << " continue 1 " << endl;
820  }
821  //now we remove all already processed dets from the list ...
822  // hitRangeByDet.erase( (*trackHitCandidates.begin()).first,(*iHitRange).first+1 );
823 
824  if (debug_info) cout << " continue 2 " << endl;
825  }
826  //if this was the last exit
827  while ( iHitRange != trackHitCandidates.end() );
828  }
829 
830 
831 }
832 
833 
834 bool
836  int ngoodhits=0;
837  if(geometry=="MTCC"){
838  auto hits= traj.recHits();
839  for(auto hit=hits.begin();hit!=hits.end();hit++){
840  unsigned int iid=(*hit)->hit()->geographicalId().rawId();
841  //CHECK FOR 3 hits r-phi
842  if(((iid>>0)&0x3)!=1) ngoodhits++;
843  }
844  }
845  else ngoodhits=traj.foundHits();
846 
847  if ( ngoodhits >= theMinHits) {
848  return true;
849  }
850  else {
851  return false;
852  }
853 }
854 
855 //----------------------------------------------------------------------------------------------------------
856 // little helper function that returns false if both hits conatin the same information
857 
859 {
860  if ( hitA.geographicalId() != hitB.geographicalId() )
861  return true;
862  if ( hitA.localPosition().x() != hitB.localPosition().x() )
863  return true;
864  if ( hitA.localPosition().y() != hitB.localPosition().y() )
865  return true;
866  if ( hitA.localPosition().z() != hitB.localPosition().z() )
867  return true;
868 
869  // if (debug_info) cout << hitA.localPosition() << endl;
870  // if (debug_info) cout << hitB << endl;
871 
872  return false;
873 }
874 
875 
876 
877 //----backfitting
878 std::pair<TrajectoryStateOnSurface, const GeomDet*>
880 {
881  int lastFitted = 999;
882  int nhits = traj.foundHits();
883  if (nhits < lastFitted+1) lastFitted = nhits-1;
884 
885  std::vector<TrajectoryMeasurement> measvec = traj.measurements();
887 
888  bool foundLast = false;
889  int actualLast = -99;
890  for (int i=lastFitted; i >= 0; i--) {
891  if(measvec[i].recHit()->isValid()){
892  if(!foundLast){
893  actualLast = i;
894  foundLast = true;
895  }
896  firstHits.push_back( measvec[i].recHit());
897  }
898  }
899  TSOS unscaledState = measvec[actualLast].updatedState();
900  AlgebraicSymMatrix55 C=ROOT::Math::SMatrixIdentity();
901  // C *= 100.;
902 
903  TSOS startingState( unscaledState.localParameters(), LocalTrajectoryError(C),
904  unscaledState.surface(),
905  thePropagator->magneticField());
906 
907  // cout << endl << "FitTester starts with state " << startingState << endl;
908 
909  KFTrajectoryFitter backFitter( *thePropagator,
910  KFUpdator(),
911  Chi2MeasurementEstimator( 100., 3));
912 
914 
915  // only direction matters in this contest
918  backFitDirection);
919 
920  vector<Trajectory> fitres = backFitter.fit( fakeSeed, firstHits, startingState);
921 
922  if (fitres.size() != 1) {
923  // cout << "FitTester: first hits fit failed!" << endl;
924  return std::pair<TrajectoryStateOnSurface, const GeomDet*>();
925  }
926 
927  TrajectoryMeasurement firstMeas = fitres[0].lastMeasurement();
928  const TSOS& firstState = firstMeas.updatedState();
929 
930  // cout << "FitTester: Fitted first state " << firstState << endl;
931  //cout << "FitTester: chi2 = " << fitres[0].chiSquared() << endl;
932 
933  TSOS initialState( firstState.localParameters(), LocalTrajectoryError(C),
934  firstState.surface(),
935  thePropagator->magneticField());
936 
937  return std::pair<TrajectoryStateOnSurface, const GeomDet*>( initialState,
938  firstMeas.recHit()->det());
939 }
#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
Trajectory createStartingTrajectory(const TrajectorySeed &seed) const
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:285
const LocalTrajectoryParameters & localParameters() const
int init
Definition: HydjetWrapper.h:67
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:20
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:42
std::vector< const TrackingRecHit * >::iterator TrackingRecHitIterator
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)
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:55
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
unsigned int nHits() const
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
State
Definition: hltDiff.cc:288
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)
T const * product() const
Definition: ESHandle.h:86
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:50
def move(src, dest)
Definition: eostools.py:510