Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:11

0001 /*! \brief   Implementation of methods of TTClusterAlgorithm_official
0002  *  \details Here, in the source file, the methods which do depend
0003  *           on the specific type <T> that can fit the template.
0004  *
0005  *  \author Nicola Pozzobon
0006  *  \date   2013, Jul 12
0007  *
0008  */
0009 
0010 #include "L1Trigger/TrackTrigger/interface/TTClusterAlgorithm_official.h"
0011 
0012 /// Function to compare clusters and sort them by row
0013 template <>
0014 bool TTClusterAlgorithm_official<Ref_Phase2TrackerDigi_>::CompareClusters(const Ref_Phase2TrackerDigi_& a,
0015                                                                           const Ref_Phase2TrackerDigi_& b) {
0016   return (a->row() < b->row());
0017 }
0018 
0019 /// Clustering operations
0020 template <>
0021 void TTClusterAlgorithm_official<Ref_Phase2TrackerDigi_>::Cluster(
0022     std::vector<std::vector<Ref_Phase2TrackerDigi_> >& output,
0023     const std::vector<Ref_Phase2TrackerDigi_>& input,
0024     bool isPS) const {
0025   /// Prepare the output
0026   output.clear();
0027 
0028   /// Prepare a proper hit container
0029   std::map<unsigned int, std::vector<Ref_Phase2TrackerDigi_> > mapHitsByColumn;
0030 
0031   /// Map all the hits by column index
0032   typename std::vector<Ref_Phase2TrackerDigi_>::const_iterator inputIterator;
0033   inputIterator = input.begin();
0034   while (inputIterator != input.end()) {
0035     mapHitsByColumn[(**inputIterator).column()].push_back(*inputIterator);
0036     ++inputIterator;
0037   }
0038 
0039   /// 1D Clusters must be stored properly <column, first row index>
0040   std::map<std::pair<unsigned int, unsigned int>, std::vector<Ref_Phase2TrackerDigi_> > map1DCluByColRow;
0041 
0042   /// Loop over the mapped hits
0043   typename std::map<unsigned int, std::vector<Ref_Phase2TrackerDigi_> >::iterator mapIterHbC;
0044   mapIterHbC = mapHitsByColumn.begin();
0045   while (mapIterHbC != mapHitsByColumn.end()) {
0046     /// Collect hits sharing column index and
0047     /// differing by 1 in row index
0048     typename std::vector<Ref_Phase2TrackerDigi_>::iterator inputIterator;
0049     inputIterator = mapIterHbC->second.begin();
0050 
0051     /// Loop over single column
0052     while (inputIterator != mapIterHbC->second.end()) {
0053       std::vector<Ref_Phase2TrackerDigi_> temp;
0054       temp.push_back(*inputIterator);
0055       inputIterator = mapIterHbC->second.erase(inputIterator);
0056       typename std::vector<Ref_Phase2TrackerDigi_>::iterator inputIterator2;
0057       inputIterator2 = inputIterator;
0058 
0059       /// Nested loop
0060       while (inputIterator2 != mapIterHbC->second.end()) {
0061         /// Check col/row and add to the cluster
0062         if ((temp.back()->column() == (**inputIterator2).column()) &&
0063             ((**inputIterator2).row() - temp.back()->row() == 1)) {
0064           temp.push_back(*inputIterator2);
0065           inputIterator2 = mapIterHbC->second.erase(inputIterator2);
0066         } else
0067           break;
0068 
0069       }  /// End of nested loop
0070 
0071       /// Sort the vector elements by row index
0072       std::sort(temp.begin(), temp.end(), CompareClusters);
0073 
0074       /// Put the cluster in the map
0075       map1DCluByColRow.insert(std::make_pair(std::make_pair(mapIterHbC->first, temp.at(0)->row()), temp));
0076 
0077       inputIterator = inputIterator2;
0078 
0079     }  /// End of loop over single column
0080     ++mapIterHbC;
0081 
0082   }  /// End of loop over mapped hits
0083 
0084   /// Cluster over the second dimension
0085   /// only in PS modules!
0086   typename std::map<std::pair<unsigned int, unsigned int>, std::vector<Ref_Phase2TrackerDigi_> >::iterator
0087       mapIter1DCbCR0;
0088   typename std::map<std::pair<unsigned int, unsigned int>, std::vector<Ref_Phase2TrackerDigi_> >::iterator
0089       mapIter1DCbCR1;
0090   mapIter1DCbCR0 = map1DCluByColRow.begin();
0091   unsigned int lastCol = mapIter1DCbCR0->first.first;
0092 
0093   while (mapIter1DCbCR0 != map1DCluByColRow.end()) {
0094     /// Add the hits
0095     std::vector<Ref_Phase2TrackerDigi_> candCluster;
0096     candCluster.insert(candCluster.end(), mapIter1DCbCR0->second.begin(), mapIter1DCbCR0->second.end());
0097 
0098     if (isPS) {
0099       /// Loop over the other elements of the map
0100       mapIter1DCbCR1 = map1DCluByColRow.begin();
0101 
0102       while (mapIter1DCbCR1 != map1DCluByColRow.end()) {
0103         /// Skip same element
0104         if (mapIter1DCbCR1 == mapIter1DCbCR0) {
0105           ++mapIter1DCbCR1;
0106           continue;
0107         }
0108 
0109         /// Skip non-contiguous column
0110         if (std::abs((int)(mapIter1DCbCR1->first.first) - (int)lastCol) != 1) {
0111           ++mapIter1DCbCR1;
0112           continue;
0113         }
0114 
0115         /// Column is contiguous
0116         /// Update the "last column index"
0117         /// This should be safe as maps are sorted structures by construction
0118         lastCol = mapIter1DCbCR1->first.first;
0119 
0120         /// Check that the cluster is good to be clustered
0121         /// Get first row
0122         unsigned int iRow0 = mapIter1DCbCR0->first.second;
0123         unsigned int iRow1 = mapIter1DCbCR1->first.second;
0124 
0125         /// Get the max row in the cluster
0126         unsigned int jRow0 = mapIter1DCbCR0->second.back()->row();
0127         unsigned int jRow1 = mapIter1DCbCR1->second.back()->row();
0128 
0129         /// Check if they overlap
0130         if ((iRow1 >= iRow0 && iRow1 <= jRow0) || (jRow1 >= iRow0 && jRow1 <= jRow0)) {
0131           /// If so, add the hits to the cluster!
0132           candCluster.insert(candCluster.end(), mapIter1DCbCR1->second.begin(), mapIter1DCbCR1->second.end());
0133           map1DCluByColRow.erase(mapIter1DCbCR1++);
0134         } else {
0135           ++mapIter1DCbCR1;
0136         }
0137       }  /// End of nested loop
0138 
0139       map1DCluByColRow.erase(mapIter1DCbCR0++);
0140 
0141       /// Check output
0142       /// Sort the vector by row index
0143       std::sort(candCluster.begin(), candCluster.end(), CompareClusters);
0144       /*
0145       std::cout << candCluster.at(0)->row() - candCluster.back()->row() << " / " 
0146         << static_cast<int>(candCluster.at(0)->row() - candCluster.back()->row()) << " / " 
0147         << abs( candCluster.at(0)->row() - candCluster.back()->row() ) << " / " 
0148         << std::abs( candCluster.at(0)->row() - candCluster.back()->row() ) << " / " 
0149         << mWidthCut << std::endl;
0150       */
0151       if (std::abs(static_cast<int>(candCluster.at(0)->row() - candCluster.back()->row())) <
0152               mWidthCut ||  /// one should add 1 to use <=
0153           mWidthCut < 1) {
0154         output.push_back(candCluster);
0155       }
0156     }  /// End of isPS
0157     else {
0158       map1DCluByColRow.erase(mapIter1DCbCR0++);
0159 
0160       /// Check output
0161       /// Sort the vector by row index
0162       std::sort(candCluster.begin(), candCluster.end(), CompareClusters);
0163 
0164       if (std::abs(static_cast<int>(candCluster.at(0)->row() - candCluster.back()->row())) <
0165               mWidthCut ||  /// one should add 1 to use <=
0166           mWidthCut < 1) {
0167         output.push_back(candCluster);
0168       }
0169     }  /// End of non-PS case
0170   }    /// End of loop over mapped 1D Clusters
0171 }