CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrajectoryCleanerBySharedHits.cc
Go to the documentation of this file.
4 #include <map>
5 #include <vector>
6 #include <boost/unordered_map.hpp>
7 
9 
10 
11 //#define DEBUG_PRINT(X) X
12 #define DEBUG_PRINT(X)
13 
14 // Define when two rechits are equals
16  bool operator()(const TransientTrackingRecHit *h1, const TransientTrackingRecHit *h2) const {
17  return (h1 == h2) || ((h1->geographicalId() == h2->geographicalId()) && (h1->hit()->sharesInput(h2->hit(), TrackingRecHit::some)));
18  }
19 };
20 // Define a hash, i.e. a number that must be equal if hits are equal, and should be different if they're not
21 struct HashByDetId : std::unary_function<const TransientTrackingRecHit *, std::size_t> {
22  std::size_t operator()(const TransientTrackingRecHit *hit) const {
23  boost::hash<uint32_t> hasher;
24  return hasher(hit->geographicalId().rawId());
25  }
26 };
27 
28 using namespace std;
29 
31 {
32  if (tc.size() <= 1) return; // nothing to clean
33 
34  //typedef boost::unordered_map<const TransientTrackingRecHit*, TIs, HashByDetId, EqualsBySharesInput> RecHitMap;
36 
37  //typedef boost::unordered_map<Trajectory*, int> TrajMap; // for each Trajectory it stores the number of shared hits
39 
40  static RecHitMap theRecHitMap(128,256,1024);// allocate 128 buckets, one row for 256 keys and one row for 512 values
41  theRecHitMap.clear(10*tc.size()); // set 10*tc.size() active buckets
42  // numbers are not optimized
43 
44  DEBUG_PRINT(std::cout << "Filling RecHit map" << std::endl);
45  for (TrajectoryPointerContainer::iterator
46  it = tc.begin(); it != tc.end(); ++it) {
47  DEBUG_PRINT(std::cout << " Processing trajectory " << *it << " (" << (*it)->foundHits() << " valid hits)" << std::endl);
48  const Trajectory::DataContainer & pd = (*it)->measurements();
49  for (Trajectory::DataContainer::const_iterator im = pd.begin();
50  im != pd.end(); im++) {
51  const TransientTrackingRecHit* theRecHit = &(*(*im).recHit());
52  if (theRecHit->isValid()) {
53  DEBUG_PRINT(std::cout << " Added hit " << theRecHit << " for trajectory " << *it << std::endl);
54  theRecHitMap.insert(theRecHit, *it);
55  }
56  }
57  }
58  DEBUG_PRINT(theRecHitMap.dump());
59 
60  DEBUG_PRINT(std::cout << "Using RecHit map" << std::endl);
61  // for each trajectory fill theTrajMap
62  static TrajMap theTrajMap;
64  itt = tc.begin(); itt != tc.end(); ++itt) {
65  if((*itt)->isValid()){
66  DEBUG_PRINT(std::cout << " Processing trajectory " << *itt << " (" << (*itt)->foundHits() << " valid hits)" << std::endl);
67  theTrajMap.clear();
68  const Trajectory::DataContainer & pd = (*itt)->measurements();
69  for (Trajectory::DataContainer::const_iterator im = pd.begin();
70  im != pd.end(); ++im) {
71  //RC const TransientTrackingRecHit* theRecHit = ((*im).recHit());
72  const TransientTrackingRecHit* theRecHit = &(*(*im).recHit());
73  if (theRecHit->isValid()) {
74  DEBUG_PRINT(std::cout << " Searching for overlaps on hit " << theRecHit << " for trajectory " << *itt << std::endl);
75  for (RecHitMap::value_iterator ivec = theRecHitMap.values(theRecHit);
76  ivec.good(); ++ivec) {
77  if (*ivec != *itt){
78  if ((*ivec)->isValid()){
79  theTrajMap[*ivec]++;
80  }
81  }
82  }
83  }
84  }
85  //end filling theTrajMap
86 
87  // check for duplicated tracks
88  if(!theTrajMap.empty() > 0){
89  for(TrajMap::iterator imapp = theTrajMap.begin();
90  imapp != theTrajMap.end(); ++imapp){
91  if((*imapp).second > 0 ){
92  int innerHit = 0;
93  if ( allowSharedFirstHit ) {
94  const TrajectoryMeasurement & innerMeasure1 = ( (*itt)->direction() == alongMomentum ) ?
95  (*itt)->firstMeasurement() : (*itt)->lastMeasurement();
96  const TransientTrackingRecHit* h1 = &(*(innerMeasure1).recHit());
97  const TrajectoryMeasurement & innerMeasure2 = ( (*imapp).first->direction() == alongMomentum ) ?
98  (*imapp).first->firstMeasurement() : (*imapp).first->lastMeasurement();
99  const TransientTrackingRecHit* h2 = &(*(innerMeasure2).recHit());
100  if ( (h1 == h2) || ((h1->geographicalId() == h2->geographicalId()) &&
101  (h1->hit()->sharesInput(h2->hit(), TrackingRecHit::some))) ) {
102  innerHit = 1;
103  }
104  }
105  int nhit1 = (*itt)->foundHits();
106  int nhit2 = (*imapp).first->foundHits();
107  if( ((*imapp).second - innerHit) >= ( (min(nhit1, nhit2)-innerHit) * theFraction) ){
108  Trajectory* badtraj;
109  double score1 = validHitBonus_*nhit1 - missingHitPenalty_*(*itt)->lostHits() - (*itt)->chiSquared();
110  double score2 = validHitBonus_*nhit2 - missingHitPenalty_*(*imapp).first->lostHits() - (*imapp).first->chiSquared();
111  badtraj = (score1 > score2) ? (*imapp).first : *itt;
112  badtraj->invalidate(); // invalidate this trajectory
113  }
114  }
115  }
116  }
117  }
118  }
119 }
int lostHits() const
Definition: Trajectory.h:231
TrajectoryPointerContainer::iterator TrajectoryPointerIterator
virtual void clean(TrajectoryPointerContainer &) const
void invalidate()
Method to invalidate a trajectory. Useful during ambiguity resolution.
Definition: Trajectory.h:262
virtual bool sharesInput(const TrackingRecHit *other, SharedInputType what) const
virtual const TrackingRecHit * hit() const =0
#define min(a, b)
Definition: mlp_lapack.h:161
std::vector< Trajectory * > TrajectoryPointerContainer
uint32_t rawId() const
get the raw id
Definition: DetId.h:45
std::vector< TrajectoryMeasurement > DataContainer
Definition: Trajectory.h:42
#define DEBUG_PRINT(X)
std::size_t operator()(const TransientTrackingRecHit *hit) const
bool isValid() const
tuple cout
Definition: gather_cfg.py:121
DetId geographicalId() const
bool operator()(const TransientTrackingRecHit *h1, const TransientTrackingRecHit *h2) const