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