CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/TrackingTools/TrajectoryCleaning/src/TrajectoryCleanerBySharedHits.cc

Go to the documentation of this file.
00001 #include "TrackingTools/TrajectoryCleaning/interface/TrajectoryCleanerBySharedHits.h"
00002 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"
00003 #include "TrackingTools/TransientTrackingRecHit/interface/RecHitComparatorByPosition.h"
00004 #include <map>
00005 #include <vector>
00006 #include <boost/unordered_map.hpp>
00007 
00008 #include "TrackingTools/TrajectoryCleaning/src/OtherHashMaps.h"
00009 
00010 
00011 //#define DEBUG_PRINT(X) X
00012 #define DEBUG_PRINT(X) 
00013 
00014 // Define when two rechits are equals
00015 struct EqualsBySharesInput { 
00016     bool operator()(const TransientTrackingRecHit *h1, const TransientTrackingRecHit *h2) const {
00017         return (h1 == h2) || ((h1->geographicalId() == h2->geographicalId()) && (h1->hit()->sharesInput(h2->hit(), TrackingRecHit::some)));
00018     }
00019 };
00020 // Define a hash, i.e. a number that must be equal if hits are equal, and should be different if they're not
00021 struct HashByDetId : std::unary_function<const TransientTrackingRecHit *, std::size_t> {
00022     std::size_t operator()(const TransientTrackingRecHit *hit) const { 
00023         boost::hash<uint32_t> hasher; 
00024         return hasher(hit->geographicalId().rawId());
00025     }
00026 };
00027 
00028 using namespace std;
00029 
00030 void TrajectoryCleanerBySharedHits::clean( TrajectoryPointerContainer & tc) const
00031 {
00032   if (tc.size() <= 1) return; // nothing to clean
00033 
00034   //typedef boost::unordered_map<const TransientTrackingRecHit*, TIs, HashByDetId, EqualsBySharesInput> RecHitMap;
00035   typedef cmsutil::SimpleAllocHashMultiMap<const TransientTrackingRecHit*, Trajectory *, HashByDetId, EqualsBySharesInput> RecHitMap;
00036 
00037   //typedef boost::unordered_map<Trajectory*, int> TrajMap;  // for each Trajectory it stores the number of shared hits
00038   typedef cmsutil::UnsortedDumbVectorMap<Trajectory*, int> TrajMap;
00039 
00040   static RecHitMap theRecHitMap(128,256,1024);// allocate 128 buckets, one row for 256 keys and one row for 512 values
00041   theRecHitMap.clear(10*tc.size());           // set 10*tc.size() active buckets
00042                                               // numbers are not optimized
00043 
00044   DEBUG_PRINT(std::cout << "Filling RecHit map" << std::endl);
00045   for (TrajectoryPointerContainer::iterator
00046          it = tc.begin(); it != tc.end(); ++it) {
00047     DEBUG_PRINT(std::cout << "  Processing trajectory " << *it << " (" << (*it)->foundHits() << " valid hits)" << std::endl);
00048     const Trajectory::DataContainer & pd = (*it)->measurements();
00049     for (Trajectory::DataContainer::const_iterator im = pd.begin();
00050          im != pd.end(); im++) {
00051       const TransientTrackingRecHit* theRecHit = &(*(*im).recHit());
00052       if (theRecHit->isValid()) {
00053         DEBUG_PRINT(std::cout << "    Added hit " << theRecHit << " for trajectory " << *it << std::endl);
00054         theRecHitMap.insert(theRecHit, *it);
00055       }
00056     }
00057   }
00058   DEBUG_PRINT(theRecHitMap.dump());
00059 
00060   DEBUG_PRINT(std::cout << "Using RecHit map" << std::endl);
00061   // for each trajectory fill theTrajMap
00062   static TrajMap theTrajMap; 
00063   for (TrajectoryCleaner::TrajectoryPointerIterator
00064          itt = tc.begin(); itt != tc.end(); ++itt) {
00065     if((*itt)->isValid()){  
00066       DEBUG_PRINT(std::cout << "  Processing trajectory " << *itt << " (" << (*itt)->foundHits() << " valid hits)" << std::endl);
00067       theTrajMap.clear();
00068       const Trajectory::DataContainer & pd = (*itt)->measurements();
00069       for (Trajectory::DataContainer::const_iterator im = pd.begin();
00070            im != pd.end(); ++im) {
00071         //RC const TransientTrackingRecHit* theRecHit = ((*im).recHit());
00072         const TransientTrackingRecHit* theRecHit = &(*(*im).recHit());
00073         if (theRecHit->isValid()) {
00074           DEBUG_PRINT(std::cout << "    Searching for overlaps on hit " << theRecHit << " for trajectory " << *itt << std::endl);
00075           for (RecHitMap::value_iterator ivec = theRecHitMap.values(theRecHit);
00076                 ivec.good(); ++ivec) {
00077               if (*ivec != *itt){
00078                 if ((*ivec)->isValid()){
00079                     theTrajMap[*ivec]++;
00080                 }
00081               }
00082           }
00083         }
00084       }
00085       //end filling theTrajMap
00086 
00087       // check for duplicated tracks
00088       if(!theTrajMap.empty() > 0){
00089         for(TrajMap::iterator imapp = theTrajMap.begin(); 
00090             imapp != theTrajMap.end(); ++imapp){
00091           if((*imapp).second > 0 ){
00092             int innerHit = 0;
00093             if ( allowSharedFirstHit ) {
00094               const TrajectoryMeasurement & innerMeasure1 = ( (*itt)->direction() == alongMomentum ) ? 
00095                 (*itt)->firstMeasurement() : (*itt)->lastMeasurement();
00096               const TransientTrackingRecHit* h1 = &(*(innerMeasure1).recHit());
00097               const TrajectoryMeasurement & innerMeasure2 = ( (*imapp).first->direction() == alongMomentum ) ? 
00098                 (*imapp).first->firstMeasurement() : (*imapp).first->lastMeasurement();
00099               const TransientTrackingRecHit* h2 = &(*(innerMeasure2).recHit());
00100               if ( (h1 == h2) || ((h1->geographicalId() == h2->geographicalId()) && 
00101                                   (h1->hit()->sharesInput(h2->hit(), TrackingRecHit::some))) ) {
00102                 innerHit = 1;
00103               }
00104             }
00105             int nhit1 = (*itt)->foundHits();
00106             int nhit2 = (*imapp).first->foundHits();
00107             if( ((*imapp).second - innerHit) >= ( (min(nhit1, nhit2)-innerHit) * theFraction) ){
00108               Trajectory* badtraj;
00109               double score1 = validHitBonus_*nhit1 - missingHitPenalty_*(*itt)->lostHits() - (*itt)->chiSquared();
00110               double score2 = validHitBonus_*nhit2 - missingHitPenalty_*(*imapp).first->lostHits() - (*imapp).first->chiSquared();
00111               badtraj = (score1 > score2) ? (*imapp).first : *itt;
00112               badtraj->invalidate();  // invalidate this trajectory
00113             }
00114           }
00115         }
00116       } 
00117     }
00118   }
00119 }