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