Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-03 00:12:41

0001 #include "SimTracker/TrackTriggerAssociation/interface/StubAssociation.h"
0002 
0003 #include <map>
0004 #include <vector>
0005 #include <utility>
0006 #include <numeric>
0007 
0008 namespace tt {
0009 
0010   StubAssociation::StubAssociation(const Config& iConfig, const Setup* setup)
0011       : setup_(setup),
0012         minLayersGood_(iConfig.minLayersGood_),
0013         minLayersGoodPS_(iConfig.minLayersGoodPS_),
0014         maxLayersBad_(iConfig.maxLayersBad_),
0015         maxLayersBadPS_(iConfig.maxLayersBadPS_) {}
0016 
0017   // insert a TPPtr and its associated collection of TTstubRefs into the underlayering maps
0018   void StubAssociation::insert(const TPPtr& tpPtr, const std::vector<TTStubRef>& ttSTubRefs) {
0019     mapTPPtrsTTStubRefs_.insert({tpPtr, ttSTubRefs});
0020     for (const TTStubRef& ttSTubRef : ttSTubRefs)
0021       mapTTStubRefsTPPtrs_[ttSTubRef].push_back(tpPtr);
0022   }
0023 
0024   // returns collection of TPPtrs associated to given TTstubRef
0025   std::vector<TPPtr> StubAssociation::findTrackingParticlePtrs(const TTStubRef& ttStubRef) const {
0026     const auto it = mapTTStubRefsTPPtrs_.find(ttStubRef);
0027     const std::vector<TPPtr> res = it != mapTTStubRefsTPPtrs_.end() ? it->second : emptyTPPtrs_;
0028     return res;
0029   }
0030 
0031   // returns collection of TTStubRefs associated to given TPPtr
0032   std::vector<TTStubRef> StubAssociation::findTTStubRefs(const TPPtr& tpPtr) const {
0033     const auto it = mapTPPtrsTTStubRefs_.find(tpPtr);
0034     return it != mapTPPtrsTTStubRefs_.end() ? it->second : emptyTTStubRefs_;
0035   }
0036 
0037   // Get all TPs that are matched to these stubs in at least 'tpMinLayers' layers and 'tpMinLayersPS' ps layers
0038   std::vector<TPPtr> StubAssociation::associate(const std::vector<TTStubRef>& ttStubRefs) const {
0039     // count associated layer for each TP
0040     std::map<TPPtr, std::pair<std::set<int>, std::set<int>>> m;
0041     for (const TTStubRef& ttStubRef : ttStubRefs) {
0042       for (const TPPtr& tpPtr : findTrackingParticlePtrs(ttStubRef)) {
0043         const int layerId = setup_->layerId(ttStubRef);
0044         m[tpPtr].first.insert(layerId);
0045         if (setup_->psModule(ttStubRef))
0046           m[tpPtr].second.insert(layerId);
0047       }
0048     }
0049     // count matched TPs
0050     auto acc = [this](int sum, const std::pair<TPPtr, std::pair<std::set<int>, std::set<int>>>& p) {
0051       return sum + (static_cast<int>(p.second.first.size()) < minLayersGood_ ||
0052                             static_cast<int>(p.second.second.size()) < minLayersGoodPS_
0053                         ? 0
0054                         : 1);
0055     };
0056     const int nTPs = std::accumulate(m.begin(), m.end(), 0, acc);
0057     std::vector<TPPtr> tpPtrs;
0058     tpPtrs.reserve(nTPs);
0059     // fill and return matched TPs
0060     for (const auto& p : m)
0061       if (static_cast<int>(p.second.first.size()) >= minLayersGood_ &&
0062           static_cast<int>(p.second.second.size()) >= minLayersGoodPS_)
0063         tpPtrs.push_back(p.first);
0064     return tpPtrs;
0065   }
0066 
0067   // Get all TPs that are matched to these stubs in at least 'tpMinLayers' layers and 'tpMinLayersPS' ps layers with not more then 'tpMaxBadStubs2S' not associated 2S stubs and not more then 'tpMaxBadStubsPS' associated PS stubs
0068   std::vector<TPPtr> StubAssociation::associateFinal(const std::vector<TTStubRef>& ttStubRefs) const {
0069     // Get all TPs that are matched to these stubs in at least 'tpMinLayers' layers and 'tpMinLayersPS' ps layers
0070     std::vector<TPPtr> tpPtrs = associate(ttStubRefs);
0071     // remove TPs with more then 'tpMaxBadStubs2S' not associated 2S stubs and more then 'tpMaxBadStubsPS' not associated PS stubs
0072     auto check = [this, &ttStubRefs](const TPPtr& tpPtr) {
0073       int bad2S(0);
0074       int badPS(0);
0075       for (const TTStubRef& ttStubRef : ttStubRefs) {
0076         const std::vector<TPPtr>& tpPtrs = findTrackingParticlePtrs(ttStubRef);
0077         if (std::find(tpPtrs.begin(), tpPtrs.end(), tpPtr) == tpPtrs.end())
0078           setup_->psModule(ttStubRef) ? badPS++ : bad2S++;
0079       }
0080       return (badPS > maxLayersBadPS_ || badPS + bad2S > maxLayersBad_);
0081     };
0082     tpPtrs.erase(std::remove_if(tpPtrs.begin(), tpPtrs.end(), check), tpPtrs.end());
0083     return tpPtrs;
0084   }
0085 
0086 }  // namespace tt