Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:07

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