CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FastTrackMerger.cc
Go to the documentation of this file.
1 #include <memory>
2 
7 
16 
19 
21 
22 #include <vector>
23 #include <map>
24 //
25 
26 //for debug only
27 //#define FAMOS_DEBUG
28 
30 {
31 #ifdef FAMOS_DEBUG
32  std::cout << "FastTrackMerger created" << std::endl;
33 #endif
34 
35  // The main product is a track collection, and all extras
36  produces<reco::TrackCollection>();
37 
38  // The name of the track producers to merge
39  trackProducers = conf.getParameter<std::vector<edm::InputTag> >("TrackProducers");
40 
41  // The name of the track producers to remove
42  std::vector<edm::InputTag> defaultRemove;
44  conf.getUntrackedParameter<std::vector<edm::InputTag> >("RemoveTrackProducers",defaultRemove);
45 
46  // Only the tracks!
47  tracksOnly = conf.getUntrackedParameter<bool>("SaveTracksOnly",false);
48 
49  // optional pT cut
50  double pTMin = conf.getUntrackedParameter<bool>("pTMin",0.);
51  pTMin2 = pTMin*pTMin;
52 
53  // optional nHit cut
54  minHits = conf.getUntrackedParameter<unsigned>("minHits",0);
55 
56  // optional track quality saving
57  promoteQuality = conf.getUntrackedParameter<bool>("promoteTrackQuality",false);
58  qualityStr = conf.getUntrackedParameter<std::string>("newQuality","");
59 
60  // optional trackAlgo (iter0/1/2/3/4)
61  trackAlgo = conf.getUntrackedParameter<unsigned>("trackAlgo",0);
62 
63  //new parameters for Trajectory filtering
64 
65  // The minimum number of hits
66  theMinimumNumberOfHits = conf.getUntrackedParameter<unsigned>("MinNumberOfTrajHits",0);
67 
68  // The maximum number of Lost Hits
69  theMaxLostHits = conf.getUntrackedParameter<unsigned>("MaxLostTrajHits",99);
70 
71  // the max number of consecutive Lost Hits
72  theMaxConsecutiveLostHits = conf.getUntrackedParameter<unsigned>("MaxConsecutiveLostTrajHits",3);
73 
74  //======================
75 
76  if ( !tracksOnly ) {
77  produces<reco::TrackExtraCollection>();
78  produces<TrackingRecHitCollection>();
79  produces<std::vector<Trajectory> >();
80  produces<TrajTrackAssociationCollection>();
81  } else {
82  produces<reco::TrackExtraCollection>();
83  produces<TrackingRecHitCollection>();
84  }
85 }
86 
87 
88 // Functions that gets called by framework every event
89 void
91 
92 #ifdef FAMOS_DEBUG
93  std::cout << "################################################################" << std::endl;
94  std::cout << " FastTrackMerger produce init " << std::endl;
95 #endif
96 
97  // The track algorithm (only of merging after iterative tracking)
99  switch(trackAlgo) {
100  case 4: algo = reco::TrackBase::iter0; break;
101  case 5: algo = reco::TrackBase::iter1; break;
102  case 6: algo = reco::TrackBase::iter2; break;
103  case 7: algo = reco::TrackBase::iter3; break;
104  case 8: algo = reco::TrackBase::iter4; break;
105  case 9: algo = reco::TrackBase::iter5; break;
106  case 10: algo = reco::TrackBase::iter6; break;
107  default: algo = reco::TrackBase::undefAlgorithm;
108  }
109 
110  // The produced objects
111  std::auto_ptr<reco::TrackCollection> recoTracks(new reco::TrackCollection);
112  std::auto_ptr<reco::TrackExtraCollection> recoTrackExtras(new reco::TrackExtraCollection);
113  std::auto_ptr<TrackingRecHitCollection> recoHits(new TrackingRecHitCollection);
114  std::auto_ptr<std::vector<Trajectory> > recoTrajectories(new std::vector<Trajectory>);
115  std::auto_ptr<TrajTrackAssociationCollection> recoTrajTrackMap(new TrajTrackAssociationCollection());
116 
117  //prepare the ref to copy the extras
120  std::vector<reco::TrackRef> trackRefs;
121 
122  // No seed -> output an empty track collection
123  if(trackProducers.size() == 0) {
124  e.put(recoTracks);
125  if ( !tracksOnly ) {
126  e.put(recoTrackExtras);
127  e.put(recoHits);
128  e.put(recoTrajectories);
129  e.put(recoTrajTrackMap);
130  } else {
131  e.put(recoTrackExtras);
132  e.put(recoHits);
133  }
134  return;
135  }
136 
137  // The quality to be set
138  reco::TrackBase::TrackQuality qualityToSet;
139  if (qualityStr != "")
141  else
142  qualityToSet = reco::TrackBase::undefQuality;
143 
144  // The input track collection handle
145  edm::Handle<reco::TrackCollection> theTrackCollection;
146 
147  // First, the tracks to be removed
148  std::set<unsigned> removeTracks;
149  for ( unsigned aProducer=0; aProducer<removeTrackProducers.size(); ++aProducer ) {
150  bool isTrackCollection = e.getByLabel(removeTrackProducers[aProducer],theTrackCollection);
151  if (!isTrackCollection) continue;
152  reco::TrackCollection::const_iterator aTrack = theTrackCollection->begin();
153  reco::TrackCollection::const_iterator lastTrack = theTrackCollection->end();
154  for ( ; aTrack!=lastTrack; ++aTrack ) {
155  // Get the simtrack Id
156  int recoTrackId = findId(*aTrack);
157  if ( recoTrackId < 0 ) continue;
158  // Remove the track if requested
159  if ( removeTracks.find((unsigned)recoTrackId) != removeTracks.end() ) continue;
160  removeTracks.insert((unsigned)recoTrackId);
161  }
162  }
163 
164  // Then the tracks to be added
165  std::set<unsigned> alreadyAddedTracks;
166 
167  // Loop on the track producers to be merged
168  for ( unsigned aProducer=0; aProducer<trackProducers.size(); ++aProducer ) {
169 
170  bool isTrackCollection = e.getByLabel(trackProducers[aProducer],theTrackCollection);
171  if ( ! isTrackCollection ) {
172 #ifdef FAMOS_DEBUG
173  std::cout << "***FastTrackMerger*** Warning! The track collection "
174  << trackProducers[aProducer].encode()
175  << " does not exist." << std::endl;
176 #endif
177  continue;
178  }
179 
180 #ifdef FAMOS_DEBUG
181  std::cout << "***FastTrackMerger*** of track collection "
182  << trackProducers[aProducer].encode()
183  << " with " << theTrackCollection->size()
184  << " tracks to be copied"
185  << std::endl;
186 #endif
187  reco::TrackCollection::const_iterator aTrack = theTrackCollection->begin();
188  reco::TrackCollection::const_iterator lastTrack = theTrackCollection->end();
189 
190  //Copy Tracks and TracksExtra
191  if ( tracksOnly ) {
192 
193  edm:: Handle<reco::TrackExtraCollection > theTrackExtraCollection;
194  // bool isTrackExtraCollection = e.getByLabel(trackProducers[aProducer],theTrackExtraCollection);
195 
196  bool index = 0;
197  for ( ; aTrack!=lastTrack; ++aTrack,++index ) {
198 
199  // Find the track id
200  int recoTrackId = findId(*aTrack);
201  if ( recoTrackId < 0 ) continue;
202 
203  // Ignore tracks to be removed or tracks already copied
204  std::set<unsigned>::iterator iR = removeTracks.find((unsigned)recoTrackId);
205 #ifdef FAMOS_DEBUG
206  if( iR != removeTracks.end() ) std::cout << recoTrackId << "(REMOVED) ";
207 #endif
208  if( iR != removeTracks.end() ) continue;
209 
210  // Ignore tracks already copied
211  std::set<unsigned>::iterator iA = alreadyAddedTracks.find((unsigned)recoTrackId);
212 #ifdef FAMOS_DEBUG
213  if( iA != alreadyAddedTracks.end() ) std::cout << recoTrackId << "(ALREADY ADDED) ";
214 #endif
215  if( iA != alreadyAddedTracks.end() ) continue;
216  //if it is not there then add it!
217  alreadyAddedTracks.insert(recoTrackId);
218 
219 
220 #ifdef FAMOS_DEBUG
221  std::cout << recoTrackId << " ADDED ";
222 #endif
223 
224  // Ignore tracks with too small a pT
225 #ifdef FAMOS_DEBUG
226  if ( aTrack->innerMomentum().Perp2() < pTMin2 )
227  std::cout << "PTMIN CUT APPLIED = " << aTrack->innerMomentum().Perp2() << std::endl;
228 #endif
229 
230  if ( aTrack->innerMomentum().Perp2() < pTMin2 ) continue;
231 
232  // Ignore tracks with too small a pT
233  if ( aTrack->recHitsSize() < minHits ) continue;
234 
235  // A copy of the track + save the transient reference to the track extra reference
236  reco::Track aRecoTrack(*aTrack);
237  recoTracks->push_back(aRecoTrack);
238 
239 
240  recoTrackExtras->push_back(reco::TrackExtra(aRecoTrack.outerPosition(), aRecoTrack.outerMomentum(), aRecoTrack.outerOk(),
241  aRecoTrack.innerPosition(), aRecoTrack.innerMomentum(), aRecoTrack.innerOk(),
242  aRecoTrack.outerStateCovariance(), aRecoTrack.outerDetId(),
243  aRecoTrack.innerStateCovariance(), aRecoTrack.innerDetId(),
244  aRecoTrack.seedDirection(), aRecoTrack.seedRef() ) );
245 
246 
247  recoTracks->back().setExtra(reco::TrackExtraRef(rTrackExtras,recoTrackExtras->size()-1));
248 
249  reco::TrackExtra & tx = recoTrackExtras->back();
250  tx.setResiduals(aRecoTrack.residuals());
251  //TrackingRecHits
252  for( trackingRecHit_iterator hit = aRecoTrack.recHitsBegin(); hit != aRecoTrack.recHitsEnd(); ++ hit ) {
253  recoHits->push_back( (*hit)->clone() );
254  tx.add( TrackingRecHitRef( rHits , recoHits->size() - 1) );
255  }
256 
257  // Save the quality if requested
258  if (promoteQuality) recoTracks->back().setQuality(qualityToSet);
259  if ( trackAlgo ) recoTracks->back().setAlgorithm(algo);
260 
261  }
262 
263 
264  // All extras are to be copied too -> loop on the Trajectory/Track map association
265  } else {
266 
267  edm:: Handle<std::vector<Trajectory> > theTrajectoryCollection;
269 
270  // Count the number of hits and reserve appropriate memory
271  unsigned nRecoHits = 0;
272  for ( ; aTrack!=lastTrack; ++aTrack ) nRecoHits+= aTrack->recHitsSize();
273  recoHits->reserve(nRecoHits); // This is to save some time at push_back.
274 
275  e.getByLabel(trackProducers[aProducer],theTrajectoryCollection);
276  e.getByLabel(trackProducers[aProducer],theAssoMap);
277 
278  // The track collection iterators.
281  anAssociation = theAssoMap->begin();
282  lastAssociation = theAssoMap->end();
283 #ifdef FAMOS_DEBUG
284  std::cout << "List of tracks to be copied " << std::endl;
285 #endif
286  // Build the map of correspondance between reco tracks and sim tracks
287  for ( ; anAssociation != lastAssociation; ++anAssociation ) {
288  edm::Ref<std::vector<Trajectory> > aTrajectoryRef = anAssociation->key;
289  reco::TrackRef aTrackRef = anAssociation->val;
290  // Find the track id
291  int recoTrackId = findId(*aTrackRef);
292  if ( recoTrackId < 0 ) continue;
293 
294  // Ignore tracks to be removed or tracks already copied
295  std::set<unsigned>::iterator iR = removeTracks.find((unsigned)recoTrackId);
296 #ifdef FAMOS_DEBUG
297  if( iR != removeTracks.end() ) std::cout << recoTrackId << "(REMOVED) ";
298 #endif
299  if( iR != removeTracks.end() ) continue;
300 
301  // Ignore tracks already copied
302  std::set<unsigned>::iterator iA = alreadyAddedTracks.find((unsigned)recoTrackId);
303 #ifdef FAMOS_DEBUG
304  if( iA != alreadyAddedTracks.end() ) std::cout << recoTrackId << "(ALREADY ADDED) ";
305 #endif
306  if( iA != alreadyAddedTracks.end() ) continue;
307 
308 #ifdef FAMOS_DEBUG
309  std::cout << recoTrackId << " Newly Added " << std::endl;
310 #endif
311  //if it is not there then add it!
312  alreadyAddedTracks.insert(recoTrackId);
313 
314  // Ignore tracks with too small a pT
315  if ( aTrackRef->innerMomentum().Perp2() < pTMin2 ) continue;
316 
317  // Ignore tracks with too few hits
318  if ( aTrackRef->recHitsSize() < minHits ) continue;
319 
320  //==== add more cuts on the trajectory (emulate the Trajectory Filter)
321 
322 #ifdef FAMOS_DEBUG
323  if(aTrajectoryRef->lostHits() > theMaxLostHits )
324  std::cout << "\tmaxLostHits= " << aTrajectoryRef->lostHits() << "\tCUT =" << theMaxLostHits << std::endl;
325 #endif
326 
327  if((unsigned)aTrajectoryRef->lostHits() > theMaxLostHits ) continue;
328 
329 #ifdef FAMOS_DEBUG
330  if(aTrajectoryRef->foundHits() < theMinimumNumberOfHits )
331  std::cout << "\tMinimumNumberOfHits = " << aTrajectoryRef->foundHits() << "\tCUT = " <<theMinimumNumberOfHits << std::endl;
332 #endif
333 
334  if((unsigned)aTrajectoryRef->foundHits() < theMinimumNumberOfHits ) continue;
335  //calculate the consecutive Lost Hits
336  unsigned consecLostHits = 0;
337  const std::vector<TrajectoryMeasurement> tms = aTrajectoryRef->measurements();
338  for(int itm= tms.size();itm!=0; --itm){
339  if(tms[itm-1].recHit()->isValid())break;
340  else if (Trajectory::lost(*tms[itm-1].recHit())) consecLostHits++;
341  }
342 
343 #ifdef FAMOS_DEBUG
344  if( consecLostHits > theMaxConsecutiveLostHits )
345  std::cout << "\tconsecLostHits = " << consecLostHits << std::endl;
346 #endif
347 
348  if( consecLostHits > theMaxConsecutiveLostHits ) continue;
349 
350 
351  //=============end new filters
352 
353 
354  // A copy of the track
355  reco::Track aRecoTrack(*aTrackRef);
356  recoTracks->push_back(aRecoTrack);
357  // Save the quality if requested
358  if (promoteQuality) recoTracks->back().setQuality(qualityToSet);
359  if ( trackAlgo ) recoTracks->back().setAlgorithm(algo);
360 
361 
362  // A copy of the hits
363  unsigned nh = aRecoTrack.recHitsSize();
364  for ( unsigned ih=0; ih<nh; ++ih ) {
365  TrackingRecHit *hit = aRecoTrack.recHit(ih)->clone();
366  recoHits->push_back(hit);
367  }
368 
369  // A copy of the trajectories
370  recoTrajectories->push_back(*aTrajectoryRef);
371 
372  }
373 #ifdef FAMOS_DEBUG
374  std::cout << std::endl;
375 #endif
376  }
377 }
378 
379  // Save the track candidates in the event
380 #ifdef FAMOS_DEBUG
381  std::cout << "Saving "
382  << recoTracks->size() << " reco::Tracks " << std::endl;
383 
384 #endif
385 
386  if ( tracksOnly ) {
387  // Save only the tracks (with transient reference to track extras)
388  e.put(recoTracks);
389  e.put(recoTrackExtras);
390  e.put(recoHits);
391 
392 
393  } else {
394  // Save the tracking recHits
395  edm::OrphanHandle <TrackingRecHitCollection> theRecoHits = e.put(recoHits );
396 
397  // Create the track extras and add the references to the rechits
398  unsigned hits=0;
399  unsigned nTracks = recoTracks->size();
400  recoTrackExtras->reserve(nTracks); // To save some time at push_back
401  for ( unsigned index = 0; index < nTracks; ++index ) {
402 
403  //reco::TrackExtra aTrackExtra;
404  reco::Track& aTrack = recoTracks->at(index);
405 
406  // std::cout << "initial TrackAlgo = " << trackAlgo << "\tNAME " << aTrack.algoName() << "\tnumber = " << aTrack.algo() << std::endl;
407 
408  reco::TrackExtra aTrackExtra(aTrack.outerPosition(),
409  aTrack.outerMomentum(),
410  aTrack.outerOk(),
411  aTrack.innerPosition(),
412  aTrack.innerMomentum(),
413  aTrack.innerOk(),
414  aTrack.outerStateCovariance(),
415  aTrack.outerDetId(),
416  aTrack.innerStateCovariance(),
417  aTrack.innerDetId(),
418  aTrack.seedDirection(),
419  aTrack.seedRef());
420 
421  unsigned nHits = aTrack.recHitsSize();
422  for ( unsigned int ih=0; ih<nHits; ++ih) {
423  aTrackExtra.add(TrackingRecHitRef(theRecoHits,hits++));
424  }
425  recoTrackExtras->push_back(aTrackExtra);
426  }
427 
428  // Save the track extras
429  edm::OrphanHandle<reco::TrackExtraCollection> theRecoTrackExtras = e.put(recoTrackExtras);
430 
431  // Add the reference to the track extra in the tracks
432  for ( unsigned index = 0; index<nTracks; ++index ) {
433  const reco::TrackExtraRef theTrackExtraRef(theRecoTrackExtras,index);
434  (recoTracks->at(index)).setExtra(theTrackExtraRef);
435  }
436 
437  // Save the tracks
438  edm::OrphanHandle<reco::TrackCollection> theRecoTracks = e.put(recoTracks);
439 
440  // Save the trajectories
441  edm::OrphanHandle<std::vector<Trajectory> > theRecoTrajectories = e.put(recoTrajectories);
442 
443  // Create and set the trajectory/track association map
444  for ( unsigned index = 0; index<nTracks; ++index ) {
445  edm::Ref<std::vector<Trajectory> > trajRef( theRecoTrajectories, index );
446  edm::Ref<reco::TrackCollection> tkRef( theRecoTracks, index );
447  recoTrajTrackMap->insert(trajRef,tkRef);
448  }
449 
450  // Save the association map.
451  e.put(recoTrajTrackMap);
452 
453  }
454 
455 }
456 
457 int
458 FastTrackMerger::findId(const reco::Track& aTrack) const {
459  int trackId = -1;
460  trackingRecHit_iterator aHit = aTrack.recHitsBegin();
461  trackingRecHit_iterator lastHit = aTrack.recHitsEnd();
462  for ( ; aHit!=lastHit; ++aHit ) {
463  if ( !aHit->get()->isValid() ) continue;
464  // const SiTrackerGSRecHit2D * rechit = (const SiTrackerGSRecHit2D*) (aHit->get());
465  const SiTrackerGSMatchedRecHit2D * rechit = (const SiTrackerGSMatchedRecHit2D*) (aHit->get());
466  trackId = rechit->simtrackId();
467  break;
468  }
469  return trackId;
470 }
471 
472 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
unsigned theMinimumNumberOfHits
static bool lost(const TransientTrackingRecHit &hit)
Definition: Trajectory.cc:206
std::vector< edm::InputTag > removeTrackProducers
int findId(const reco::Track &aTrack) const
FastTrackMerger(const edm::ParameterSet &conf)
TrackQuality
track quality
Definition: TrackBase.h:93
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
Definition: Track.h:68
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:10
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:39
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:46
TrackAlgorithm
track algorithm
Definition: TrackBase.h:80
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:41
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
edm::Ref< TrackingRecHitCollection > TrackingRecHitRef
persistent reference to a TrackingRecHit
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:50
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:58
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:62
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::TrackCollection, unsigned short > > TrajTrackAssociationCollection
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
RefProd< PROD > getRefBeforePut()
Definition: Event.h:128
tuple conf
Definition: dbtoconf.py:185
std::string qualityStr
unsigned theMaxConsecutiveLostHits
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:9
edm::RefToBase< TrajectorySeed > seedRef() const
Definition: Track.h:111
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:46
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:48
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:37
virtual void produce(edm::Event &e, const edm::EventSetup &es)
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:52
key_type key() const
Accessor for product key.
Definition: Ref.h:266
void add(const TrackingRecHitRef &r)
add a reference to a RecHit
std::vector< edm::InputTag > trackProducers
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:44
TrackingRecHitRef recHit(size_t i) const
Get i-th hit on the track.
Definition: Track.h:66
const TrackResiduals & residuals() const
Definition: Track.h:116
PropagationDirection seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:104
unsigned theMaxLostHits
tuple cout
Definition: gather_cfg.py:121
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:60
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:64
void setResiduals(const TrackResiduals &r)
set the residuals
Definition: TrackExtra.h:130