CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackCandidateProducer.cc
Go to the documentation of this file.
1 #include <memory>
2 
7 
16 
19 
21 //#include "FastSimulation/Tracking/interface/TrackerRecHitSplit.h"
22 
24 
25 #include <vector>
26 #include <map>
27 
32 
38 
39 //Propagator withMaterial
41 
42 
43 //
44 
45 //for debug only
46 //#define FAMOS_DEBUG
47 
49 {
50 #ifdef FAMOS_DEBUG
51  std::cout << "TrackCandidateProducer created" << std::endl;
52 #endif
53 
54  // The main product is a track candidate collection.
55  produces<TrackCandidateCollection>();
56 
57  // These products contain tracks already reconstructed at this level
58  // (No need to reconstruct them twice!)
59  produces<reco::TrackCollection>();
60  produces<TrackingRecHitCollection>();
61  produces<reco::TrackExtraCollection>();
62  produces<std::vector<Trajectory> >();
63  produces<TrajTrackAssociationCollection>();
64 
65  // The name of the seed producer
66  seedProducer = conf.getParameter<edm::InputTag>("SeedProducer");
67 
68  // The name of the recHit producer
69  hitProducer = conf.getParameter<edm::InputTag>("HitProducer");
70 
71  // The name of the track producer (tracks already produced need not be produced again!)
72  // trackProducer = conf.getParameter<edm::InputTag>("TrackProducer");
73  trackProducers = conf.getParameter<std::vector<edm::InputTag> >("TrackProducers");
74 
75  // Copy (or not) the tracks already produced in a new collection
76  keepFittedTracks = conf.getParameter<bool>("KeepFittedTracks");
77 
78  // The minimum number of crossed layers
79  minNumberOfCrossedLayers = conf.getParameter<unsigned int>("MinNumberOfCrossedLayers");
80 
81  // The maximum number of crossed layers
82  maxNumberOfCrossedLayers = conf.getParameter<unsigned int>("MaxNumberOfCrossedLayers");
83 
84  // Reject overlapping hits?
85  rejectOverlaps = conf.getParameter<bool>("OverlapCleaning");
86 
87  // Split hits ?
88  splitHits = conf.getParameter<bool>("SplitHits");
89 
90  // Reject tracks with several seeds ?
91  // Typically don't do that at HLT for electrons, but do it otherwise
92  seedCleaning = conf.getParameter<bool>("SeedCleaning");
93 
94  simTracks_ = conf.getParameter<edm::InputTag>("SimTracks");
95  estimatorCut_= conf.getParameter<double>("EstimatorCut");
96 
97  // consumes
98  seedToken = consumes<edm::View<TrajectorySeed> >(seedProducer);
99  recHitToken = consumes<SiTrackerGSMatchedRecHit2DCollection>(hitProducer);
100  edm::InputTag _label("famosSimHits");
101  simVertexToken = consumes<edm::SimVertexContainer>(_label);
102  simTrackToken = consumes<edm::SimTrackContainer>(_label);
103  for(unsigned tprod=0; tprod < trackProducers.size(); ++tprod){
104  trackTokens.push_back(consumes<reco::TrackCollection>(trackProducers[tprod]));
105  trajectoryTokens.push_back(consumes<std::vector<Trajectory> >(trackProducers[tprod]));
106  assoMapTokens.push_back(consumes<TrajTrackAssociationCollection>(trackProducers[tprod]));
107  }
108 }
109 
110 
111 // Virtual destructor needed.
113 
114  if(thePropagator) delete thePropagator;
115 
116  // do nothing
117 #ifdef FAMOS_DEBUG
118  std::cout << "TrackCandidateProducer destructed" << std::endl;
119 #endif
120 
121 
122 }
123 
124 void
126 
127  //services
128 
131 
132  es.get<IdealMagneticFieldRecord>().get(magField);
133  es.get<TrackerDigiGeometryRecord>().get(geometry);
134 
135  theMagField = &(*magField);
136  theGeometry = &(*geometry);
137 
139 }
140 
141  // Functions that gets called by framework every event
142 void
144 
145 #ifdef FAMOS_DEBUG
146  std::cout << "################################################################" << std::endl;
147  std::cout << " TrackCandidateProducer produce init " << std::endl;
148 #endif
149 
150  // Useful typedef's to avoid retyping
151  typedef std::pair<reco::TrackRef,edm::Ref<std::vector<Trajectory> > > TrackPair;
152  typedef std::map<unsigned,TrackPair> TrackMap;
153 
154  // The produced objects
155  std::auto_ptr<TrackCandidateCollection> output(new TrackCandidateCollection);
156  std::auto_ptr<reco::TrackCollection> recoTracks(new reco::TrackCollection);
157  std::auto_ptr<TrackingRecHitCollection> recoHits(new TrackingRecHitCollection);
158  std::auto_ptr<reco::TrackExtraCollection> recoTrackExtras(new reco::TrackExtraCollection);
159  std::auto_ptr<std::vector<Trajectory> > recoTrajectories(new std::vector<Trajectory> );
160  std::auto_ptr<TrajTrackAssociationCollection> recoTrajTrackMap( new TrajTrackAssociationCollection() );
161 
162  // Get the seeds
163  // edm::Handle<TrajectorySeedCollection> theSeeds;
165  e.getByToken(seedToken,theSeeds);
166 
167  //Retrieve tracker topology from geometry
169  es.get<IdealGeometryRecord>().get(tTopoHand);
170  const TrackerTopology *tTopo=tTopoHand.product();
171 
172 
173  // No seed -> output an empty track collection
174  if(theSeeds->size() == 0) {
175  e.put(output);
176  e.put(recoTracks);
177  e.put(recoHits);
178  e.put(recoTrackExtras);
179  e.put(recoTrajectories);
180  e.put(recoTrajTrackMap);
181  return;
182  }
183 
184  // Get the GS RecHits
185  // edm::Handle<SiTrackerGSRecHit2DCollection> theGSRecHits;
187  e.getByToken(recHitToken, theGSRecHits);
188 
189  //get other general things
190  const std::vector<unsigned> theSimTrackIds = theGSRecHits->ids();
191  // SimTracks and SimVertices
192 
194  e.getByToken(simVertexToken,theSimVtx);
196  e.getByToken(simTrackToken,theSTC);
197 
198  const edm::SimTrackContainer* theSimTracks = &(*theSTC);
199  LogDebug("FastTracking")<<"looking at: "<< theSimTrackIds.size()<<" simtracks.";
200 
201 
202 
203 
204  // The input track collection + extra's
205  /*
206  edm::Handle<reco::TrackCollection> theTrackCollection;
207  edm:: Handle<std::vector<Trajectory> > theTrajectoryCollection;
208  edm::Handle<TrajTrackAssociationCollection> theAssoMap;
209  bool isTrackCollection = e.getByLabel(trackProducer,theTrackCollection);
210  */
211  std::vector<edm::Handle<reco::TrackCollection> > theTrackCollections;
212  std::vector<edm:: Handle<std::vector<Trajectory> > > theTrajectoryCollections;
213  std::vector<edm::Handle<TrajTrackAssociationCollection> > theAssoMaps;
214  std::vector<bool> isTrackCollections;
217  TrackMap theTrackMap;
218  unsigned nCollections = trackProducers.size();
219  unsigned nRecoHits = 0;
220 
221  if ( nCollections ) {
222  theTrackCollections.resize(nCollections);
223  theTrajectoryCollections.resize(nCollections);
224  theAssoMaps.resize(nCollections);
225  isTrackCollections.resize(nCollections);
226  for ( unsigned tprod=0; tprod < nCollections; ++tprod ) {
227  isTrackCollections[tprod] = e.getByToken(trackTokens[tprod],theTrackCollections[tprod]);
228 
229  if ( isTrackCollections[tprod] ) {
230  // The track collection
231  reco::TrackCollection::const_iterator aTrack = theTrackCollections[tprod]->begin();
232  reco::TrackCollection::const_iterator lastTrack = theTrackCollections[tprod]->end();
233  // The numbers of hits
234  for ( ; aTrack!=lastTrack; ++aTrack ) nRecoHits+= aTrack->recHitsSize();
235  e.getByToken(trajectoryTokens[tprod],theTrajectoryCollections[tprod]);
236  e.getByToken(assoMapTokens[tprod],theAssoMaps[tprod]);
237  // The association between trajectories and tracks
238  anAssociation = theAssoMaps[tprod]->begin();
239  lastAssociation = theAssoMaps[tprod]->end();
240 #ifdef FAMOS_DEBUG
241  std::cout << "Input Track Producer " << tprod << " : " << trackProducers[tprod] << std::endl;
242  std::cout << "List of tracks already reconstructed " << std::endl;
243 #endif
244  // Build the map of correspondance between reco tracks and sim tracks
245  for ( ; anAssociation != lastAssociation; ++anAssociation ) {
246  edm::Ref<std::vector<Trajectory> > aTrajectoryRef = anAssociation->key;
247  reco::TrackRef aTrackRef = anAssociation->val;
248  // Find the simtrack id of the reconstructed track
249  int recoTrackId = findId(*aTrackRef);
250  if ( recoTrackId < 0 ) continue;
251 #ifdef FAMOS_DEBUG
252  std::cout << recoTrackId << " ";
253 #endif
254  // And store it.
255  theTrackMap[recoTrackId] = TrackPair(aTrackRef,aTrajectoryRef);
256  }
257 #ifdef FAMOS_DEBUG
258  std::cout << std::endl;
259 #endif
260  }
261  }
262  // This is to save some time at push_back.
263  recoHits->reserve(nRecoHits);
264  }
265 
266 
267  // Loop over the seeds
268  int currentTrackId = -1;
269  /*
270  TrajectorySeedCollection::const_iterator aSeed = theSeeds->begin();
271  TrajectorySeedCollection::const_iterator lastSeed = theSeeds->end();
272  for ( ; aSeed!=lastSeed; ++aSeed ) {
273  // The first hit of the seed and its simtrack id
274  */
275  /* */
276 #ifdef FAMOS_DEBUG
277  std::cout << "Input seed Producer : " << seedProducer << std::endl;
278  std::cout << "Number of seeds : " << theSeeds->size() << std::endl;
279 #endif
280  unsigned seed_size = theSeeds->size();
281  for (unsigned seednr = 0; seednr < seed_size; ++seednr){
282 
283  LogDebug("FastTracking")<<"looking at seed #:"<<seednr;
284 
285  // The seed
286  const BasicTrajectorySeed* aSeed = &((*theSeeds)[seednr]);
287 
288  std::vector<int> simTrackIds;
289  std::map<int,TrajectoryStateOnSurface> seedStates;
290  std::map<int,TrajectoryStateOnSurface> simtkStates;
291 
292  TrackerRecHit theFirstSeedingTrackerRecHit;
293  if (theSeeds->at(seednr).nHits()==0){
294  //new stuff for no hits on seed
295 
296  LogDebug("FastTracking")<<" seed with no hits to be considered.";
297 
298  //moved out of the loop
299  //edm::ESHandle<MagneticField> field;
300  //es.get<IdealMagneticFieldRecord>().get(field);
301 
302  PTrajectoryStateOnDet ptod =theSeeds->at(seednr).startingState();
303  DetId id(ptod.detId());
304  const GeomDet * g = theGeometry->idToDet(id);
305  const Surface * surface=&g->surface();
306 
308 
309  edm::ESHandle<Propagator> propagator;
310  es.get<TrackingComponentsRecord>().get("AnyDirectionAnalyticalPropagator",propagator);
311 
312  //moved out of the loop
313  // const std::vector<unsigned> theSimTrackIds = theGSRecHits->ids();
314  // edm::Handle<edm::SimTrackContainer> theSTC;
315  // e.getByLabel(simTracks_,theSTC);
316  // const edm::SimTrackContainer* theSimTracks = &(*theSTC);
317 
318  double minimunEst=1000000;
319  LogDebug("FastTracking")<<"looking at: "<< theSimTrackIds.size()<<" simtracks.";
320  for ( unsigned tkId=0; tkId != theSimTrackIds.size(); ++tkId ) {
321 
322  const SimTrack & simtrack = (*theSimTracks)[theSimTrackIds[tkId]];
323 
325  simtrack.trackerSurfacePosition().y(),
326  simtrack.trackerSurfacePosition().z());
327 
328  GlobalVector momentum(simtrack.trackerSurfaceMomentum().x(),
329  simtrack.trackerSurfaceMomentum().y(),
330  simtrack.trackerSurfaceMomentum().z());
331 
332  if (position.basicVector().dot( momentum.basicVector() ) * seedState.globalPosition().basicVector().dot(seedState.globalMomentum().basicVector()) <0. ){
333  LogDebug("FastTracking")<<"not on the same direction.";
334  continue;
335  }
336 
337  //no charge mis-identification ... FIXME
338  int charge = (int) simtrack.charge();
339  GlobalTrajectoryParameters glb_parameters(position,
340  momentum,
341  charge,
342  theMagField);
343  FreeTrajectoryState simtrack_trackerstate(glb_parameters);
344 
345  TrajectoryStateOnSurface simtrack_comparestate = propagator->propagate(simtrack_trackerstate,*surface);
346 
347 
348  if (!simtrack_comparestate.isValid()){
349  LogDebug("FastTracking")<<" ok this is a state-based seed. simtrack state does not propagate to the seed surface. skipping.";
350  continue;}
351 
352  if (simtrack_comparestate.globalPosition().basicVector().dot(simtrack_comparestate.globalMomentum().basicVector()) * seedState.globalPosition().basicVector().dot(seedState.globalMomentum().basicVector()) <0. ){
353  LogDebug("FastTracking")<<"not on the same direction.";
354  continue;
355  }
356 
357 
358  AlgebraicVector5 v(seedState.localParameters().vector() - simtrack_comparestate.localParameters().vector());
359  AlgebraicSymMatrix55 m(seedState.localError().matrix());
360  bool ierr = !m.Invert();
361  if ( ierr ){
362  edm::LogWarning("FastTracking") <<" Candidate Producer cannot invert the error matrix! - Skipping...";
363  continue;
364  }
365  double est = ROOT::Math::Similarity(v,m);
366  LogDebug("FastTracking")<<"comparing two state on the seed surface:\n"
367  <<"seed: "<<seedState
368  <<"sim: "<<simtrack_comparestate
369  <<"\n estimator is: "<<est;
370 
371  if (est<minimunEst) minimunEst=est;
372  if (est<estimatorCut_){
373  simTrackIds.push_back(theSimTrackIds[tkId]);
374  //making a state with exactly the sim track parameters
375  //the initial errors are set to unity just for kicks
376  // AlgebraicSymMatrix C(5,1);// C*=50;
377  //new attempt!!!!
379  C *= 0.0000001;
380 
381  seedStates[theSimTrackIds[tkId]] = TrajectoryStateOnSurface(simtrack_comparestate.globalParameters(),
383  seedState.surface());
384  LogDebug("FastTracking")<<"the compatibility estimator is: "<<est<<" for track id: "<<simTrackIds.back();
385  }
386  }//SimTrack loop
387  if (simTrackIds.size()==0) LogDebug("FastTracking")<<"could not find any simtrack within errors, closest was at: "<<minimunEst;
388  }//seed has 0 hit.
389  else{
390  //same old stuff
391  // Find the first hit of the Seed
392  TrajectorySeed::range theSeedingRecHitRange = aSeed->recHits();
393  const SiTrackerGSMatchedRecHit2D * theFirstSeedingRecHit = (const SiTrackerGSMatchedRecHit2D*) (&(*(theSeedingRecHitRange.first)));
394  theFirstSeedingTrackerRecHit = TrackerRecHit(theFirstSeedingRecHit,theGeometry,tTopo);
395  // The SimTrack id associated to that recHit
396  simTrackIds.push_back( theFirstSeedingRecHit->simtrackId() );
397  }
398 
399  //from then on, only the simtrack IDs are usefull.
400  //now loop over all possible trackid for this seed.
401  //an actual seed can be shared by two tracks in dense envirronement, and also for hit-less seeds.
402  for (unsigned int iToMake=0;iToMake!=simTrackIds.size();++iToMake){
403  int simTrackId = simTrackIds[iToMake];
404 
405  // Don't consider seeds belonging to a track already considered
406  // (Equivalent to seed cleaning)
407  if ( seedCleaning && currentTrackId == simTrackId ) continue;
408  currentTrackId = simTrackId;
409 
410  // A vector of TrackerRecHits belonging to the track and the number of crossed layers
411  std::vector<TrackerRecHit> theTrackerRecHits;
412  unsigned theNumberOfCrossedLayers = 0;
413 
414  // The track has indeed been reconstructed already -> Save the pertaining info
415  TrackMap::const_iterator theTrackIt = theTrackMap.find(simTrackId);
416  if ( nCollections && theTrackIt != theTrackMap.end() ) {
417 
418  if ( keepFittedTracks ) {
419  LogDebug("FastTracking") << "Track " << simTrackId << " already reconstructed -> copy it";
420  // The track and trajectroy references
421  reco::TrackRef aTrackRef = theTrackIt->second.first;
422  edm::Ref<std::vector<Trajectory> > aTrajectoryRef = theTrackIt->second.second;
423 
424  // A copy of the track
425  reco::Track aRecoTrack(*aTrackRef);
426  recoTracks->push_back(aRecoTrack);
427 
428  // A copy of the hits
429  unsigned nh = aRecoTrack.recHitsSize();
430  for ( unsigned ih=0; ih<nh; ++ih ) {
431  TrackingRecHit *hit = aRecoTrack.recHit(ih)->clone();
432  recoHits->push_back(hit);
433  }
434 
435  // A copy of the trajectories
436  recoTrajectories->push_back(*aTrajectoryRef);
437 
438  }// keepFitterTracks
439  else {
440  LogDebug("FastTracking") << "Track " << simTrackId << " already reconstructed -> ignore it";
441  }
442 
443  // The track was not saved -> create a track candidate.
444 
445  } //already existing collection of tracks
446  else{//no collection of tracks already exists
447 
448  LogDebug("FastTracking")<<"Track " << simTrackId << " is considered to return a track candidate" ;
449 
450  // Get all the rechits associated to this track
451  SiTrackerGSMatchedRecHit2DCollection::range theRecHitRange = theGSRecHits->get(simTrackId);
452  SiTrackerGSMatchedRecHit2DCollection::const_iterator theRecHitRangeIteratorBegin = theRecHitRange.first;
453  SiTrackerGSMatchedRecHit2DCollection::const_iterator theRecHitRangeIteratorEnd = theRecHitRange.second;
455 
456  LogDebug("FastTracking")<<"counting: "<<theRecHitRangeIteratorEnd-theRecHitRangeIteratorBegin<<" hits to be considered.";
457 
458  bool firstRecHit = true;
459  TrackerRecHit theCurrentRecHit, thePreviousRecHit;
460  TrackerRecHit theFirstHitComp, theSecondHitComp;
461 
462  for ( iterRecHit = theRecHitRangeIteratorBegin;
463  iterRecHit != theRecHitRangeIteratorEnd;
464  ++iterRecHit) {
465 
466  // Check the number of crossed layers
467  if ( theNumberOfCrossedLayers >= maxNumberOfCrossedLayers ) continue;
468 
469  // Get current and previous rechits
470  if(!firstRecHit) thePreviousRecHit = theCurrentRecHit;
471  theCurrentRecHit = TrackerRecHit(&(*iterRecHit),theGeometry,tTopo);
472 
473  //>>>>>>>>>BACKBUILDING CHANGE: DO NOT STAT FROM THE FIRST HIT OF THE SEED
474 
475  // NOTE: checking the direction --> specific for OIHit only
476  // if( aSeed->direction()!=oppositeToMomentum ) {
477  // // Check that the first rechit is indeed the first seeding hit
478  // if ( firstRecHit && theCurrentRecHit != theFirstSeedingTrackerRecHit && theSeeds->at(seednr).nHits()!=0 ) continue;
479  // }
480  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
481 
482  // Count the number of crossed layers
483  if ( !theCurrentRecHit.isOnTheSameLayer(thePreviousRecHit) )
484  ++theNumberOfCrossedLayers;
485 
486  // Add all rechits (Grouped Trajectory Builder) from this hit onwards
487  // Always add the first seeding rechit anyway
488  if ( !rejectOverlaps || firstRecHit ) {
489  // Split matched hits (if requested / possible )
490  if ( splitHits && theCurrentRecHit.matchedHit()->isMatched() ) addSplitHits(theCurrentRecHit,theTrackerRecHits);
491  else theTrackerRecHits.push_back(theCurrentRecHit); // No splitting
492  firstRecHit = false;
493 
494  // And now treat the following RecHits if hits in the same layer
495  // have to be rejected - The split option is not
496  } else {
497 
498  // Not the same layer : Add the current hit
499  if ( theCurrentRecHit.subDetId() != thePreviousRecHit.subDetId() ||
500  theCurrentRecHit.layerNumber() != thePreviousRecHit.layerNumber() ) {
501 
502  // Split matched hits (if requested / possible )
503  if ( splitHits && theCurrentRecHit.matchedHit()->isMatched() ) addSplitHits(theCurrentRecHit,theTrackerRecHits);
504  else theTrackerRecHits.push_back(theCurrentRecHit); // No splitting
505 
506  // Same layer : keep the current hit if better, and drop the other - otherwise do nothing
507  } else if ( theCurrentRecHit.localError() < thePreviousRecHit.localError() ) {
508 
509  // Split matched hits (if requested / possible )
510  if( splitHits && theCurrentRecHit.matchedHit()->isMatched() ){
511  // Remove the previous hit(s)
512  theTrackerRecHits.pop_back();
513  if ( thePreviousRecHit.matchedHit()->isMatched() ) theTrackerRecHits.pop_back();
514 
515  // Replace by the new hits
516  addSplitHits(theCurrentRecHit,theTrackerRecHits);
517  }
518  // No splitting
519  else {
520  theTrackerRecHits.back() = theCurrentRecHit; // Replace the previous hit by the current hit
521  }
522 
523  } else {
524 
525  //keep the older rechit as a reference if the error of the new one is worse
526  theCurrentRecHit = thePreviousRecHit;
527  }
528  }
529  }
530 
531  // End of loop over the track rechits
532  }//no collection of track already existed. adding the hits by hand.
533 
534  LogDebug("FastTracking")<<" number of hits: " << theTrackerRecHits.size()<<" after counting overlaps and splitting.";
535 
536  // 1) Create the OwnWector of TrackingRecHits
538  unsigned nTrackerHits = theTrackerRecHits.size();
539  recHits.reserve(nTrackerHits); // To save some time at push_back
540 
541  if (aSeed->direction()==oppositeToMomentum){
542  LogDebug("FastTracking")<<"reversing the order of the hits";
543  std::reverse(theTrackerRecHits.begin(),theTrackerRecHits.end());
544  }
545 
546  for ( unsigned ih=0; ih<nTrackerHits; ++ih ) {
547  TrackingRecHit* aTrackingRecHit = theTrackerRecHits[ih].hit()->clone();
548  recHits.push_back(aTrackingRecHit);
549 
550  const DetId& detId = theTrackerRecHits[ih].hit()->geographicalId();
551  LogDebug("FastTracking")
552  << "Added RecHit from detid " << detId.rawId()
553  << " subdet = " << theTrackerRecHits[ih].subDetId()
554  << " layer = " << theTrackerRecHits[ih].layerNumber()
555  << " ring = " << theTrackerRecHits[ih].ringNumber()
556  << " error = " << theTrackerRecHits[ih].localError()
557  << std::endl
558 
559  << "Track/z/r : "
560  << simTrackId << " "
561  << theTrackerRecHits[ih].globalPosition().z() << " "
562  << theTrackerRecHits[ih].globalPosition().perp() << std::endl;
563  if ( theTrackerRecHits[ih].matchedHit() && theTrackerRecHits[ih].matchedHit()->isMatched() )
564  LogTrace("FastTracking") << "Matched : " << theTrackerRecHits[ih].matchedHit()->isMatched()
565  << "Rphi Hit = " << theTrackerRecHits[ih].matchedHit()->monoHit()->simhitId()
566  << "Stereo Hit = " << theTrackerRecHits[ih].matchedHit()->stereoHit()->simhitId()
567  <<std::endl;
568  }//loop over the rechits
569 
570  // Check the number of crossed layers
571  if ( theNumberOfCrossedLayers < minNumberOfCrossedLayers ) {
572  LogDebug("FastTracking")<<"not enough layer crossed ("<<theNumberOfCrossedLayers<<")";
573  continue;
574  }
575 
576 
577  //>>>>>>>>>BACKBUILDING CHANGE: REPLACE THE STARTING STATE
578 
579  // Create a track Candidate (now with the reference to the seed!) .
580  //PTrajectoryStateOnDet PTSOD = aSeed->startingState();
581  PTrajectoryStateOnDet PTSOD;
582 
583  if (aSeed->nHits()==0){
584  //stabilize the fit with the true simtrack state
585  //in case of zero hits
586 
587  PTSOD = trajectoryStateTransform::persistentState(seedStates[simTrackId],aSeed->startingState().detId());
588 
589  } else {
590  //create the initial state from the SimTrack
591  int vertexIndex = (*theSimTracks)[currentTrackId].vertIndex();
592  // a) origin vertex
593  GlobalPoint position((*theSimVtx)[vertexIndex].position().x(),
594  (*theSimVtx)[vertexIndex].position().y(),
595  (*theSimVtx)[vertexIndex].position().z());
596 
597  // b) initial momentum
598  GlobalVector momentum( (*theSimTracks)[currentTrackId].momentum().x() ,
599  (*theSimTracks)[currentTrackId].momentum().y() ,
600  (*theSimTracks)[currentTrackId].momentum().z() );
601  // c) electric charge
602  float charge = (*theSimTracks)[simTrackId].charge();
603  // -> inital parameters
604  GlobalTrajectoryParameters initialParams(position,momentum,(int)charge,theMagField);
605  // -> large initial errors
606  AlgebraicSymMatrix55 errorMatrix= AlgebraicMatrixID();
607  CurvilinearTrajectoryError initialError(errorMatrix);
608  // -> initial state
609  FreeTrajectoryState initialFTS(initialParams, initialError);
610 #ifdef FAMOS_DEBUG
611  std::cout << "TrajectorySeedProducer: FTS momentum " << initialFTS.momentum() << std::endl;
612 #endif
613  const GeomDet* initialLayer = theGeometry->idToDet(recHits.front().geographicalId());
614  //this is wrong because the FTS is defined at vertex, and it need to be properly propagated to the first rechit
615  // const TrajectoryStateOnSurface initialTSOS(initialFTS, initialLayer->surface());
616  const TrajectoryStateOnSurface initialTSOS = thePropagator->propagate(initialFTS,initialLayer->surface()) ;
617  if (!initialTSOS.isValid()) continue;
618 
619 
620  PTSOD = trajectoryStateTransform::persistentState(initialTSOS,recHits.front().geographicalId().rawId());
621  }
622 
624  newTrackCandidate(recHits,
625  *aSeed,
626  PTSOD,
627  edm::RefToBase<TrajectorySeed>(theSeeds,seednr));
628 
629  LogDebug("FastTracking")<< "\tSeed Information " << std::endl
630  << "\tSeed Direction = " << aSeed->direction() << std::endl
631  << "\tSeed StartingDet = " << aSeed->startingState().detId() << std::endl
632  << "\tTrajectory Parameters " << std::endl
633  << "\t\t detId = " << newTrackCandidate.trajectoryStateOnDet().detId() << std::endl
634  << "\t\t loc.px = "
635  << newTrackCandidate.trajectoryStateOnDet().parameters().momentum().x()
636  << std::endl
637  << "\t\t loc.py = "
638  << newTrackCandidate.trajectoryStateOnDet().parameters().momentum().y()
639  << std::endl
640  << "\t\t loc.pz = "
641  << newTrackCandidate.trajectoryStateOnDet().parameters().momentum().z()
642  << std::endl
643  << "\t\t error = ";
644 
645  bool newTrackCandidateIsDuplicate = isDuplicateCandidate(*output,newTrackCandidate);
646  if (!newTrackCandidateIsDuplicate) output->push_back(newTrackCandidate);
647  LogDebug("FastTracking")<<"filling a track candidate into the collection, now having: "<<output->size();
648 
649  }//loop over possible simtrack associated.
650  }//loop over all possible seeds.
651 
652  // Save the track candidates in the event
653  LogDebug("FastTracking") << "Saving "
654  << output->size() << " track candidates and "
655  << recoTracks->size() << " reco::Tracks ";
656  // Save the track candidates
657  e.put(output);
658 
659 
660 
661  // Save the tracking recHits
662 
663  edm::OrphanHandle <TrackingRecHitCollection> theRecoHits = e.put(recoHits );
664 
665  // Create the track extras and add the references to the rechits
666  unsigned hits=0;
667  unsigned nTracks = recoTracks->size();
668  recoTrackExtras->reserve(nTracks); // To save some time at push_back
669  for ( unsigned index = 0; index < nTracks; ++index ) {
670  //reco::TrackExtra aTrackExtra;
671  reco::Track& aTrack = recoTracks->at(index);
672  reco::TrackExtra aTrackExtra(aTrack.outerPosition(),
673  aTrack.outerMomentum(),
674  aTrack.outerOk(),
675  aTrack.innerPosition(),
676  aTrack.innerMomentum(),
677  aTrack.innerOk(),
678  aTrack.outerStateCovariance(),
679  aTrack.outerDetId(),
680  aTrack.innerStateCovariance(),
681  aTrack.innerDetId(),
682  aTrack.seedDirection(),
683  aTrack.seedRef());
684 
685  unsigned nHits = aTrack.recHitsSize();
686  for ( unsigned int ih=0; ih<nHits; ++ih) {
687  aTrackExtra.add(TrackingRecHitRef(theRecoHits,hits++));
688  }
689  recoTrackExtras->push_back(aTrackExtra);
690  }
691 
692 
693  // Save the track extras
694  edm::OrphanHandle<reco::TrackExtraCollection> theRecoTrackExtras = e.put(recoTrackExtras);
695 
696  // Add the reference to the track extra in the tracks
697  for ( unsigned index = 0; index<nTracks; ++index ) {
698  const reco::TrackExtraRef theTrackExtraRef(theRecoTrackExtras,index);
699  (recoTracks->at(index)).setExtra(theTrackExtraRef);
700  }
701 
702  // Save the tracks
703  edm::OrphanHandle<reco::TrackCollection> theRecoTracks = e.put(recoTracks);
704 
705  // Save the trajectories
706  edm::OrphanHandle<std::vector<Trajectory> > theRecoTrajectories = e.put(recoTrajectories);
707 
708  // Create and set the trajectory/track association map
709  for ( unsigned index = 0; index<nTracks; ++index ) {
710  edm::Ref<std::vector<Trajectory> > trajRef( theRecoTrajectories, index );
711  edm::Ref<reco::TrackCollection> tkRef( theRecoTracks, index );
712  recoTrajTrackMap->insert(trajRef,tkRef);
713  }
714 
715 
716  // Save the association map.
717  e.put(recoTrajTrackMap);
718 
719 }
720 
721 int
723  int trackId = -1;
724  trackingRecHit_iterator aHit = aTrack.recHitsBegin();
725  trackingRecHit_iterator lastHit = aTrack.recHitsEnd();
726  for ( ; aHit!=lastHit; ++aHit ) {
727  if ( !aHit->get()->isValid() ) continue;
728  // const SiTrackerGSRecHit2D * rechit = (const SiTrackerGSRecHit2D*) (aHit->get());
729  const SiTrackerGSMatchedRecHit2D * rechit = (const SiTrackerGSMatchedRecHit2D*) (aHit->get());
730  trackId = rechit->simtrackId();
731  break;
732  }
733  return trackId;
734 }
735 
736 void
738  std::vector<TrackerRecHit>& theTrackerRecHits) {
739 
740  const SiTrackerGSRecHit2D* mHit = theCurrentRecHit.matchedHit()->monoHit();
741  const SiTrackerGSRecHit2D* sHit = theCurrentRecHit.matchedHit()->stereoHit();
742 
743  // Add the new hits
744  if( mHit->simhitId() < sHit->simhitId() ) {
745 
746  theTrackerRecHits.push_back(TrackerRecHit(mHit,theCurrentRecHit));
747  theTrackerRecHits.push_back(TrackerRecHit(sHit,theCurrentRecHit));
748 
749  } else {
750 
751  theTrackerRecHits.push_back(TrackerRecHit(sHit,theCurrentRecHit));
752  theTrackerRecHits.push_back(TrackerRecHit(mHit,theCurrentRecHit));
753 
754  }
755 
756 }
757 
759  typedef TrackCandidateCollection::const_iterator TCCI;
760 
761  TCCI candsEnd = candidates.end();
762  double newQbp = newCand.trajectoryStateOnDet().parameters().qbp();
763  double newDxdz = newCand.trajectoryStateOnDet().parameters().dxdz();
764  TrackCandidate::range newHits = newCand.recHits();
765 
766  for (TCCI iCand = candidates.begin(); iCand!= candsEnd; ++iCand){
767  //pick some first traits of duplication: qbp and dxdz
768  double iQbp = iCand->trajectoryStateOnDet().parameters().qbp();
769  double iDxdz = iCand->trajectoryStateOnDet().parameters().dxdz();
770  if (newQbp == iQbp && newDxdz == iDxdz){
771  LogDebug("isDuplicateCandidate")<<"Probably a duplicate "<<iQbp <<" : "<<iDxdz;
772  TrackCandidate::range iHits = iCand->recHits();
773  unsigned int nHits = 0;
774  unsigned int nShared = 0;
775  unsigned int nnewHits = 0;
776  for (TrackCandidate::const_iterator iiHit = iHits.first; iiHit != iHits.second; ++iiHit){
777  nHits++;
778  for (TrackCandidate::const_iterator inewHit = newHits.first; inewHit != newHits.second; ++inewHit){
779  if (iiHit == iHits.first) nnewHits++;
780  if (sameLocalParameters(&*iiHit,&*inewHit)) nShared++;
781  }
782  }
783  LogDebug("isDuplicateCandidate") <<"nHits "<<nHits<<" nnewHits "<< nnewHits<< " nShared "<<nShared;
784  if (nHits == nShared && nHits == nnewHits) return true;
785  }
786  }
787  return false;
788 }
789 
791  bool localPointSame = aH->localPosition() == bH->localPosition();
792  bool localErrorSame = aH->localPositionError().xx() == bH->localPositionError().xx();
793  localErrorSame &= aH->localPositionError().yy() == bH->localPositionError().yy();
794  localErrorSame &= aH->localPositionError().xy() == bH->localPositionError().xy();
795  return localPointSame && localErrorSame;
796 }
#define LogDebug(id)
PropagationDirection direction() const
const math::XYZVectorD & trackerSurfacePosition() const
Definition: SimTrack.h:36
T getParameter(std::string const &) const
float xx() const
Definition: LocalError.h:24
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
const int & simhitId() const
std::vector< edm::EDGetTokenT< reco::TrackCollection > > trackTokens
const LocalTrajectoryParameters & localParameters() const
range recHits() const
edm::EDGetTokenT< edm::View< TrajectorySeed > > seedToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
edm::EDGetTokenT< edm::SimVertexContainer > simVertexToken
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
Definition: Track.h:69
edm::EDGetTokenT< edm::SimTrackContainer > simTrackToken
ROOT::Math::SMatrixIdentity AlgebraicMatrixID
TrackCandidateProducer(const edm::ParameterSet &conf)
int findId(const reco::Track &aTrack) const
const CurvilinearTrajectoryError & curvilinearError() const
std::vector< TrackCandidate > TrackCandidateCollection
size_type size() const
Definition: OwnVector.h:248
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:10
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
T y() const
Definition: PV3DBase.h:63
GlobalPoint globalPosition() const
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:40
const SiTrackerGSRecHit2D * stereoHit() const
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
std::pair< const_iterator, const_iterator > range
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:47
float charge() const
charge
Definition: CoreSimTrack.cc:18
const MagneticField * theMagField
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:35
double charge(const std::vector< uint8_t > &Ampls)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
float float float z
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
bool isOnTheSameLayer(const TrackerRecHit &other) const
Check if two hits are on the same layer of the same subdetector.
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
virtual void produce(edm::Event &e, const edm::EventSetup &es) override
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:42
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
PTrajectoryStateOnDet const & trajectoryStateOnDet() const
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
AlgebraicVector5 vector() const
void push_back(D *&d)
Definition: OwnVector.h:274
float xy() const
Definition: LocalError.h:25
const SurfaceType & surface() const
const SiTrackerGSMatchedRecHit2D * matchedHit() const
The Hit itself.
Definition: TrackerRecHit.h:73
const SiTrackerGSRecHit2D * monoHit() const
float yy() const
Definition: LocalError.h:26
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
PropagatorWithMaterial * thePropagator
edm::Ref< TrackingRecHitCollection > TrackingRecHitRef
persistent reference to a TrackingRecHit
edm::EDGetTokenT< SiTrackerGSMatchedRecHit2DCollection > recHitToken
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:51
T z() const
Definition: PV3DBase.h:64
std::vector< edm::InputTag > trackProducers
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:59
std::pair< const_iterator, const_iterator > range
bool isMatched(TrackingRecHit const &hit)
unsigned int detId() const
const AlgebraicSymMatrix55 & matrix() const
LocalVector momentum() const
Momentum vector in the local frame.
const LocalTrajectoryError & localError() const
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:63
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
virtual const GeomDet * idToDet(DetId) const
GlobalVector momentum() const
#define LogTrace(id)
tuple conf
Definition: dbtoconf.py:185
virtual TrackingRecHit * clone() const =0
Definition: DetId.h:18
virtual TrackingRecHit const * hit() const
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:9
edm::RefToBase< TrajectorySeed > seedRef() const
Definition: Track.h:112
PTrajectoryStateOnDet const & startingState() const
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:49
TrajectoryStateOnSurface transientState(const PTrajectoryStateOnDet &ts, const Surface *surface, const MagneticField *field)
ROOT::Math::SVector< double, 5 > AlgebraicVector5
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:38
const GlobalTrajectoryParameters & globalParameters() const
virtual LocalError localPositionError() const =0
const math::XYZTLorentzVectorD & trackerSurfaceMomentum() const
Definition: SimTrack.h:38
virtual void beginRun(edm::Run const &run, const edm::EventSetup &es) override
const T & get() const
Definition: EventSetup.h:55
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:53
key_type key() const
Accessor for product key.
Definition: Ref.h:266
T const * product() const
Definition: ESHandle.h:62
range recHits() const
unsigned int layerNumber() const
The Layer Number.
Definition: TrackerRecHit.h:84
std::vector< edm::EDGetTokenT< std::vector< Trajectory > > > trajectoryTokens
ESHandle< TrackerGeometry > geometry
unsigned int nHits() const
const AlgebraicSymMatrix55 & matrix() const
GlobalVector globalMomentum() const
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:45
TrackingRecHitRef recHit(size_t i) const
Get i-th hit on the track.
Definition: Track.h:67
unsigned int subDetId() const
The subdet Id.
Definition: TrackerRecHit.h:81
double localError()
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:105
tuple cout
Definition: gather_cfg.py:121
const TrackerGeometry * theGeometry
DetId geographicalId() const
Definition: DDAxes.h:10
bool sameLocalParameters(const TrackingRecHit *aH, const TrackingRecHit *bH) const
bool isDuplicateCandidate(const TrackCandidateCollection &candidates, const TrackCandidate &newCand) const
std::vector< edm::EDGetTokenT< TrajTrackAssociationCollection > > assoMapTokens
RecHitContainer::const_iterator const_iterator
T x() const
Definition: PV3DBase.h:62
virtual LocalPoint localPosition() const =0
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:61
std::vector< SimTrack > SimTrackContainer
reference front()
Definition: OwnVector.h:349
void reserve(size_t)
Definition: OwnVector.h:268
const LocalTrajectoryParameters & parameters() const
Definition: Run.h:41
void addSplitHits(const TrackerRecHit &, std::vector< TrackerRecHit > &)
T dot(const Basic3DVector &rh) const
Scalar product, or &quot;dot&quot; product, with a vector of same type.
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:65