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 
415  if ((&collpixel)!=0){
417  for(ipix=collpixel.data().begin();ipix!=collpixel.data().end();ipix++){
418  float ych= RHBuilder->build(&(*ipix))->globalPosition().y();
419  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
420  allHits.push_back(&(*ipix));
421  }
422  }
423 
424 
425  if (useMatchedHits) // use matched
426  {
427  //add the matched hits ...
429 
430  if ((&collmatched)!=0){
431  for(istripm=collmatched.data().begin();istripm!=collmatched.data().end();istripm++){
432  float ych= RHBuilder->build(&(*istripm))->globalPosition().y();
433 
434  int cDetId=istripm->geographicalId().rawId();
435  bool noSeedDet = ( detIDSeedMatched.end() == find (detIDSeedMatched.begin(), detIDSeedMatched.end(), cDetId ) ) ;
436 
437  if ( noSeedDet )
438  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
439  {
440  //if (debug_info) cout << "adding matched hit " << &(*istripm) << endl;
441  allHits.push_back(&(*istripm));
442  }
443  }
444  }
445 
446  //add the rpi hits, but only accept hits that are not matched hits
447  if ((&collrphi)!=0){
448  for(istrip=collrphi.data().begin();istrip!=collrphi.data().end();istrip++){
449  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
450  StripSubdetector monoDetId(istrip->geographicalId());
451  if (monoDetId.partnerDetId())
452  {
453  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "this det belongs to a glued det " << ych << endl;
454  continue;
455  }
456  int cDetId=istrip->geographicalId().rawId();
457  bool noSeedDet = ( detIDSeedRphi.end()== find (detIDSeedRphi.begin(), detIDSeedRphi.end(), cDetId ) ) ;
458  if (noSeedDet)
459  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
460  {
461 
462  bool hitIsUnique = true;
463  //now
464  if ((&collmatched)!=0)
465  for(istripm=collmatched.data().begin();istripm!=collmatched.data().end();istripm++)
466  {
467  // if ( isDifferentStripReHit2D ( *istrip, (istripm->stereoHit() ) ) == false)
468  if ( isDifferentStripReHit2D ( *istrip, (istripm->monoHit() ) ) == false)
469  {
470  hitIsUnique = false;
471  edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "rphi hit is in matched hits; y: " << ych << endl;
472  break;
473  }
474  } //end loop over all matched
475  if (hitIsUnique)
476  {
477  // if (debug_info) cout << "adding rphi hit " << &(*istrip) << endl;
478  allHits.push_back(&(*istrip));
479  }
480  }
481  }
482  }
483 
484 
485  //add the stereo hits except the hits that are in the matched collection
486  //update do not use unmatched rphi hist due to limitation of alignment framework
487  //if (!useMatchedHits)
488  //if ((&collstereo)!=0){
489  // for(istrip=collstereo.data().begin();istrip!=collstereo.data().end();istrip++){
490  // float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
491  //
492  //
493  // int cDetId = istrip->geographicalId().rawId();
494  // bool noSeedDet = ( detIDSeedStereo.end()== find (detIDSeedStereo.begin(), detIDSeedStereo.end(), cDetId ) ) ;
495  //
496  // if (noSeedDet)
497  // if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
498  // {
499  //
500  // bool hitIsUnique = true;
501  // //now
502  // if ((&collmatched)!=0)
503  // for(istripm=collmatched.data().begin();istripm!=collmatched.data().end();istripm++)
504  // {
505  // if ( isDifferentStripReHit2D ( *istrip, * (istripm->stereoHit() ) ) == false)
506  // {
507  // hitIsUnique = false;
508  // edm::LogInfo("CRackTrajectoryBuilder::SortHits") << "stereo hit already in matched hits; y: " << ych << endl;
509  // break;
510  // }
511  // } //end loop over all stereo
512  // if (hitIsUnique)
513  // {
514  //
515  // // if (debug_info) cout << "now I am adding a stero hit, either noise or not in overlap ...!!!!" << endl;
516  // allHits.push_back(&(*istrip));
517  // }
518  // }
519  // }
520  //}
521  }
522  else // dont use matched ...
523  {
524 
525  if ((&collrphi)!=0){
526  for(istrip=collrphi.data().begin();istrip!=collrphi.data().end();istrip++){
527  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
528  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
529  allHits.push_back(&(*istrip));
530  }
531  }
532 
533 
534  if ((&collstereo)!=0){
535  for(istrip=collstereo.data().begin();istrip!=collstereo.data().end();istrip++){
536  float ych= RHBuilder->build(&(*istrip))->globalPosition().y();
537  if ((seed_plus && (ych<yref)) || (!(seed_plus) && (ych>yref)))
538  allHits.push_back(&(*istrip));
539  }
540  }
541 
542  }
543 
544 
545 // if (seed_plus){
546 // stable_sort(allHits.begin(),allHits.end(),CompareDetY_plus(*tracker));
547 // }
548 // else {
549 // stable_sort(allHits.begin(),allHits.end(),CompareDetY_minus(*tracker));
550 // }
551 
552 
553  if (seed_plus){
554  stable_sort(allHits.begin(),allHits.end(),CompareHitY_plus(*tracker));
555  }
556  else {
557  stable_sort(allHits.begin(),allHits.end(),CompareHitY(*tracker));
558  }
559 
560 
561 
562  if (debug_info)
563  {
564  if (debug_info) cout << "all hits" << endl;
565 
566  //starting trajectory
567  Trajectory startingTraj = createStartingTrajectory(seed);
568 
569  if (debug_info) cout << "START " << startingTraj.lastMeasurement().updatedState() << endl;
570  if (debug_info) cout << "START Err" << startingTraj.lastMeasurement().updatedState().localError().matrix() << endl;
571 
572 
573  vector<const TrackingRecHit*>::iterator iHit;
574  for (iHit=allHits.begin(); iHit<allHits.end(); iHit++)
575  {
576  GlobalPoint gphit=RHBuilder->build(*iHit)->globalPosition();
577  if (debug_info) cout << "GH " << gphit << endl;
578 
579  // tracker->idToDet((*iHit)->geographicalId())->surface();
580 
581  TSOS prSt = thePropagator->propagate(startingTraj.lastMeasurement().updatedState(),
582  tracker->idToDet((*iHit)->geographicalId())->surface());
583 
584  if(prSt.isValid())
585  {
586  if (debug_info) cout << "PR " << prSt.globalPosition() << endl;
587  //if (debug_info) cout << "PR Err" << prSt.localError().matrix() << endl;
588 
589  }
590  else
591  {
592  if (debug_info) cout << "not valid" << endl;
593  }
594  }
595  if (debug_info) cout << "all hits end" << endl;
596  }
597 
598 
599  return allHits;
600 }
601 
604 {
605  PTrajectoryStateOnDet pState( seed.startingState());
606  const GeomDet* gdet = (&(*tracker))->idToDet(DetId(pState.detId()));
607  TSOS State= trajectoryStateTransform::transientState( pState, &(gdet->surface()),
608  &(*magfield));
609  return State;
610 
611 }
612 
614  const vector<const TrackingRecHit*>& _Hits, Propagator *currPropagator){
615  vector<const TrackingRecHit*> Hits = _Hits;
616  if ( Hits.size() == 0 )
617  return;
618 
619  if (debug_info) cout << "CRackTrajectoryBuilder::AddHit" << endl;
620  if (debug_info) cout << "START " << traj.lastMeasurement().updatedState() << endl;
621 
622  vector <TrackingRecHitRange> hitRangeByDet;
623  TrackingRecHitIterator prevDet;
624 
625  prevDet = Hits.begin();
626  for( TrackingRecHitIterator iHit = Hits.begin(); iHit != Hits.end(); iHit++ )
627  {
628  if ( (*prevDet)->geographicalId() == (*iHit)->geographicalId() )
629  continue;
630 
631  hitRangeByDet.push_back( make_pair( prevDet, iHit ) );
632  prevDet = iHit;
633  }
634  hitRangeByDet.push_back( make_pair( prevDet, Hits.end() ) );
635 
637 
638  if (fastPropagation) {
639  for( TrackingRecHitRangeIterator iHitRange = hitRangeByDet.begin(); iHitRange != hitRangeByDet.end(); iHitRange++ )
640  {
641  const TrackingRecHit *currHit = *(iHitRange->first);
642  DetId currDet = currHit->geographicalId();
643 
644  TSOS prSt= currPropagator->propagate(traj.lastMeasurement().updatedState(),
645  tracker->idToDet(currDet)->surface());
646 
647  if ( !prSt.isValid())
648  {
649  if (debug_info) cout << "Not Valid: PRST" << prSt.globalPosition();
650  // if (debug_info) cout << "Not Valid: HIT" << *currHit;
651 
652 
653  continue;
654  }
655 
656  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build( currHit );
657  double chi2min = theEstimator->estimate( prSt, *bestHit).second;
658 
659  if (debug_info) cout << "Size " << iHitRange->first - (*iHitRange).second << endl;
660  for( TrackingRecHitIterator iHit = (*iHitRange).first+1; iHit != iHitRange->second; iHit++ )
661  {
662  if (debug_info) cout << "loop3 " <<" "<< Hits.end() - iHit << endl;
663 
664  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build( *iHit );
665  double currChi2 = theEstimator->estimate(prSt, *tmpHit).second;
666  if ( currChi2 < chi2min )
667  {
668  chi2min = currChi2;
669  bestHit = tmpHit;
670  }
671  }
672  //now we have check if the track can be added to the trajectory
673  if (debug_info) cout << chi2min << endl;
674  if (chi2min < chi2cut)
675  {
676  if (debug_info) cout << "chi2 fine : " << chi2min << endl;
677  TSOS UpdatedState= theUpdator->update( prSt, *bestHit );
678  if (UpdatedState.isValid()){
679  hits.push_back(bestHit);
680  traj.push( TM(prSt,UpdatedState, bestHit, chi2min) );
681  if (debug_info) edm::LogInfo("CosmicTrackFinder") <<
682  "STATE UPDATED WITH HIT AT POSITION "
683 
684  << bestHit->globalPosition()
685  <<UpdatedState<<" "
686  <<traj.chiSquared();
687  if (debug_info) cout <<
688  "STATE UPDATED WITH HIT AT POSITION "
689 
690  << bestHit->globalPosition()
691  <<UpdatedState<<" "
692  <<traj.chiSquared();
693  if (debug_info) cout << "State is valid ..." << endl;
694  break; // now we need to
695  }
696  else
697  {
698  edm::LogWarning("CosmicTrackFinder")<<" State can not be updated with hit at position " << endl;
699  TSOS UpdatedState= theUpdator->update( prSt, *bestHit );
700  if (UpdatedState.isValid()){
701  cout <<
702  "NOT! UPDATED WITH HIT AT POSITION "
703 
704  << bestHit->globalPosition()
705  <<UpdatedState<<" "
706  <<traj.chiSquared();
707 
708  }
709  }
710  }
711  }
712  } //simple version end
713  else
714  {
715  //first sort the dets in the order they are traversed by the trajectory
716  // we need three loops:
717  // 1: loop as long as there can be an new hit added to the trajectory
718  // 2: loop over all dets that might be hit
719  // 3: loop over all hits on a certain det
720 
721 
722  std::vector < std::pair<TrackingRecHitRangeIterator, TSOS> > trackHitCandidates;
723  std::vector <std::pair<TrackingRecHitRangeIterator, TSOS> >::iterator iHitRange;
724  std::vector <uint32_t> processedDets;
725  do
726  {
727 
728  //create vector of possibly hit detectors...
729  trackHitCandidates.clear();
730  DetId currDet;
731  for( TrackingRecHitRangeIterator iHit = hitRangeByDet.begin(); iHit != hitRangeByDet.end(); iHit++ )
732  {
733  const TrackingRecHit *currHit = *(iHit->first);
734  currDet = currHit->geographicalId();
735 
736  if ( find(processedDets.begin(), processedDets.end(), currDet.rawId()) != processedDets.end() )
737  continue;
738 
739  TSOS prSt= currPropagator->propagate(traj.lastMeasurement().updatedState(),
740  tracker->idToDet(currDet)->surface());
741  if ( ( !prSt.isValid() ) || (theEstimator->Chi2MeasurementEstimatorBase::estimate(prSt,tracker->idToDet(currDet)->surface() ) == false) )
742  // if ( ( !prSt.isValid() ) || (theEstimator->estimate(prSt,tracker->idToDet(currDet)->surface() ) == false) )
743  continue;
744 
745  trackHitCandidates.push_back( make_pair(iHit, prSt) );
746  }
747 
748  if (!trackHitCandidates.size())
749  break;
750 
751  if (debug_info) cout << Hits.size() << " (int) trackHitCandidates.begin() " << trackHitCandidates.size() << endl;
752  if (debug_info) cout << "Before sorting ... " << endl;
753 
754  if (debug_info)
755  for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ )
756  {
757  if (debug_info) cout << (tracker->idToDet((*(iHitRange->first->first))->geographicalId()))->position();
758  }
759  if (debug_info) cout << endl;
760 
761 
762  stable_sort( trackHitCandidates.begin(), trackHitCandidates.end(), CompareDetByTraj(traj.lastMeasurement().updatedState()) );
763 
764  if (debug_info) cout << "After sorting ... " << endl;
765  if (debug_info)
766  {
767  for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ )
768  {
769  if (debug_info) cout << (tracker->idToDet((*(iHitRange->first->first))->geographicalId()))->position();
770  }
771  cout << endl;
772  }
773 
774  for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ ) //loop over dets
775  {
776 
777  //now find the best hit of the detector
778  if (debug_info) cout << "loop2 " << trackHitCandidates.size() <<" " << trackHitCandidates.end() - iHitRange << endl;
779  const TrackingRecHit *currHit = *(iHitRange->first->first);
780 
781  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build( currHit );
782  TSOS currPrSt = (*iHitRange).second;
783 
784  if (debug_info) cout << "curr position" << bestHit->globalPosition();
785  for( TrackingRecHitIterator iHit = (*iHitRange).first->first+1; iHit != iHitRange->first->second; iHit++ )
786  {
787  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build( *iHit );
788  if (debug_info) cout << "curr position" << tmpHit->globalPosition() ;
789 
790  }
791  }
792  if (debug_info) cout << "Cross check end ..." << endl;
793 
794 
795  //just a simple test if the same hit can be added twice ...
796  // for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ ) //loop over all hits
797 
798  // break;
799 
800  for( iHitRange = trackHitCandidates.begin(); iHitRange != trackHitCandidates.end(); iHitRange++ ) //loop over detsall hits
801  {
802 
803  //now find the best hit of the detector
804  if (debug_info) cout << "loop2 " << trackHitCandidates.size() <<" " << trackHitCandidates.end() - iHitRange << endl;
805 
806  const TrackingRecHit *currHit = *(iHitRange->first->first);
807 
808  processedDets.push_back(currHit->geographicalId().rawId());
809 
810 
811  TransientTrackingRecHit::RecHitPointer bestHit = RHBuilder->build( currHit );
812 
813  if (debug_info) cout << "curr position A" << bestHit->globalPosition() << endl;
814  TSOS currPrSt = (*iHitRange).second;
815  double chi2min = theEstimator->estimate( currPrSt, *bestHit).second;
816 
817  if (debug_info) cout << "Size " << iHitRange->first->second - (*iHitRange).first->first << endl;
818  for( TrackingRecHitIterator iHit = (*iHitRange).first->first+1; iHit != iHitRange->first->second; iHit++ )
819  {
820  if (debug_info) cout << "loop3 " <<" "<< Hits.end() - iHit << endl;
821 
822  TransientTrackingRecHit::RecHitPointer tmpHit = RHBuilder->build( *iHit );
823  if (debug_info) cout << "curr position B" << tmpHit->globalPosition() << endl;
824  double currChi2 = theEstimator->estimate(currPrSt, *tmpHit).second;
825  if ( currChi2 < chi2min )
826  {
827  if (debug_info) cout << "Is best hit" << endl;
828  chi2min = currChi2;
829  bestHit = tmpHit;
830  }
831  }
832  //now we have checked the det and can remove the entry from the vector...
833 
834  //if (debug_info) cout << "before erase ..." << endl;
835  //this is to slow ...
836  // hitRangeByDet.erase( (*iHitRange).first,(*iHitRange).first+1 );
837  //if (debug_info) cout << "after erase ..." << endl;
838 
839  if (debug_info) cout << chi2min << endl;
840  //if the track can be added to the trajectory
841  if (chi2min < chi2cut)
842  {
843  if (debug_info) cout << "chi2 fine : " << chi2min << endl;
844 
845  // if (debug_info) cout << "previaous state " << traj.lastMeasurement().updatedState() <<endl;
846  TSOS UpdatedState= theUpdator->update( currPrSt, *bestHit );
847  if (UpdatedState.isValid()){
848 
849  hits.push_back(bestHit);
850  traj.push( TM(currPrSt,UpdatedState, bestHit, chi2min) );
851  if (debug_info) edm::LogInfo("CosmicTrackFinder") <<
852  "STATE UPDATED WITH HIT AT POSITION "
853  // <<tmphitbestdet->globalPosition()
854  <<UpdatedState<<" "
855  <<traj.chiSquared();
856  if (debug_info) cout << "Added Hit" << bestHit->globalPosition() << endl;
857  if (debug_info) cout << "State is valid ..." << UpdatedState << endl;
858  //cout << "updated state " << traj.lastMeasurement().updatedState() <<endl;
859 
860  // return; //break;
861  //
862  // TSOS prSt= currPropagator->propagate(traj.lastMeasurement().updatedState(),
863  // tracker->idToDet( bestHit->geographicalId() )->surface());
864  //
865  // if ( prSt.isValid())
866  // cout << "the same hit can be added twice ..." << endl;
867  //
868 
869  break;
870  }
871  else
872  {
873  if (debug_info) edm::LogWarning("CosmicTrackFinder")<<" State can not be updated with hit at position "
874  << bestHit->globalPosition();
875  // cout << "State can not be updated with hit at " << bestHit->globalPosition() << endl;
876  }
877  continue;
878  }
879  else
880  {
881  // cout << "chi2 to big : " << chi2min << endl;
882  }
883  if (debug_info) cout << " continue 1 " << endl;
884  }
885  //now we remove all already processed dets from the list ...
886  // hitRangeByDet.erase( (*trackHitCandidates.begin()).first,(*iHitRange).first+1 );
887 
888  if (debug_info) cout << " continue 2 " << endl;
889  }
890  //if this was the last exit
891  while ( iHitRange != trackHitCandidates.end() );
892  }
893 
894 
895 }
896 
897 
898 bool
900  int ngoodhits=0;
901  if(geometry=="MTCC"){
902  auto hits= traj.recHits();
903  for(auto hit=hits.begin();hit!=hits.end();hit++){
904  unsigned int iid=(*hit)->hit()->geographicalId().rawId();
905  //CHECK FOR 3 hits r-phi
906  if(((iid>>0)&0x3)!=1) ngoodhits++;
907  }
908  }
909  else ngoodhits=traj.foundHits();
910 
911  if ( ngoodhits >= theMinHits) {
912  return true;
913  }
914  else {
915  return false;
916  }
917 }
918 
919 //----------------------------------------------------------------------------------------------------------
920 // little helper function that returns false if both hits conatin the same information
921 
923 {
924  if ( hitA.geographicalId() != hitB.geographicalId() )
925  return true;
926  if ( hitA.localPosition().x() != hitB.localPosition().x() )
927  return true;
928  if ( hitA.localPosition().y() != hitB.localPosition().y() )
929  return true;
930  if ( hitA.localPosition().z() != hitB.localPosition().z() )
931  return true;
932 
933  // if (debug_info) cout << hitA.localPosition() << endl;
934  // if (debug_info) cout << hitB << endl;
935 
936  return false;
937 }
938 
939 
940 
941 //----backfitting
942 std::pair<TrajectoryStateOnSurface, const GeomDet*>
944 {
945  int lastFitted = 999;
946  int nhits = traj.foundHits();
947  if (nhits < lastFitted+1) lastFitted = nhits-1;
948 
949  std::vector<TrajectoryMeasurement> measvec = traj.measurements();
951 
952  bool foundLast = false;
953  int actualLast = -99;
954  for (int i=lastFitted; i >= 0; i--) {
955  if(measvec[i].recHit()->isValid()){
956  if(!foundLast){
957  actualLast = i;
958  foundLast = true;
959  }
960  firstHits.push_back( measvec[i].recHit());
961  }
962  }
963  TSOS unscaledState = measvec[actualLast].updatedState();
964  AlgebraicSymMatrix55 C=ROOT::Math::SMatrixIdentity();
965  // C *= 100.;
966 
967  TSOS startingState( unscaledState.localParameters(), LocalTrajectoryError(C),
968  unscaledState.surface(),
969  thePropagator->magneticField());
970 
971  // cout << endl << "FitTester starts with state " << startingState << endl;
972 
973  KFTrajectoryFitter backFitter( *thePropagator,
974  KFUpdator(),
975  Chi2MeasurementEstimator( 100., 3));
976 
978 
979  // only direction matters in this contest
982  backFitDirection);
983 
984  vector<Trajectory> fitres = backFitter.fit( fakeSeed, firstHits, startingState);
985 
986  if (fitres.size() != 1) {
987  // cout << "FitTester: first hits fit failed!" << endl;
988  return std::pair<TrajectoryStateOnSurface, const GeomDet*>();
989  }
990 
991  TrajectoryMeasurement firstMeas = fitres[0].lastMeasurement();
992  TSOS firstState = firstMeas.updatedState();
993 
994  // cout << "FitTester: Fitted first state " << firstState << endl;
995  //cout << "FitTester: chi2 = " << fitres[0].chiSquared() << endl;
996 
997  TSOS initialState( firstState.localParameters(), LocalTrajectoryError(C),
998  firstState.surface(),
999  thePropagator->magneticField());
1000 
1001  return std::pair<TrajectoryStateOnSurface, const GeomDet*>( initialState,
1002  firstMeas.recHit()->det());
1003 }
#define LogDebug(id)
PropagationDirection direction() const
T getParameter(std::string const &) const
int foundHits() const
Definition: Trajectory.h:279
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:330
const LocalTrajectoryParameters & localParameters() const
int init
Definition: HydjetWrapper.h:67
tuple magfield
Definition: HLT_ES_cff.py:2311
T y() const
Definition: PV3DBase.h:63
GlobalPoint globalPosition() const
ConstRecHitContainer recHits() const
Definition: Trajectory.h:258
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:125
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:250
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:228
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:241
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:307
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:286
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:35
virtual LocalPoint localPosition() const final