Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:30

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 // TODO: update to C++17 style
0010 #include "Acts/TrackFinding/GbtsConnector.hpp"
0011 
0012 #include <fstream>
0013 #include <iostream>
0014 #include <list>
0015 #include <set>
0016 #include <unordered_map>
0017 
0018 namespace Acts {
0019 
0020 GbtsConnection::GbtsConnection(unsigned int s, unsigned int d)
0021     : m_src(s), m_dst(d) {}
0022 
0023 GbtsConnector::GbtsConnector(std::ifstream &inFile) {
0024   m_layerGroups.clear();
0025 
0026   int nLinks{};
0027 
0028   inFile >> nLinks >> m_etaBin;
0029 
0030   for (int l = 0; l < nLinks; l++) {
0031     unsigned int stage{}, lIdx{}, src{}, dst{}, nEntries{};
0032     int height{}, width{};
0033 
0034     inFile >> lIdx >> stage >> src >> dst >> height >> width >> nEntries;
0035 
0036     auto pC = std::make_unique<GbtsConnection>(src, dst);
0037 
0038     int dummy{};
0039 
0040     for (int i = 0; i < height; i++) {
0041       for (int j = 0; j < width; j++) {
0042         inFile >> dummy;  // pC->m_binTable[j+i*width];
0043       }
0044     }
0045 
0046     int vol_id = src / 1000;
0047 
0048     if (vol_id == 13 || vol_id == 12 || vol_id == 14) {
0049       continue;
0050     }
0051 
0052     vol_id = dst / 1000;
0053 
0054     if (vol_id == 13 || vol_id == 12 || vol_id == 14) {
0055       continue;
0056     }
0057 
0058     auto &connections = m_connMap[stage];
0059     connections.push_back(std::move(pC));
0060   }
0061 
0062   // re-arrange the connection stages
0063 
0064   std::list<const GbtsConnection *> lConns;
0065 
0066   std::map<int, std::vector<const GbtsConnection *>> newConnMap;
0067 
0068   for (const auto &[_, value] : m_connMap) {
0069     for (const auto &conn : value) {
0070       lConns.push_back(conn.get());
0071     }
0072   }
0073 
0074   int stageCounter = 0;
0075 
0076   while (!lConns.empty()) {
0077     std::unordered_map<unsigned int, std::pair<int, int>>
0078         mCounter;  // layerKey, nDst, nSrc
0079 
0080     for (const auto &conn : lConns) {
0081       auto entryIt = mCounter.find(conn->m_dst);
0082       if (entryIt != mCounter.end()) {
0083         (*entryIt).second.first++;
0084       } else {
0085         int nDst = 1;
0086         int nSrc = 0;
0087         mCounter.insert(
0088             std::make_pair(conn->m_dst, std::make_pair(nDst, nSrc)));
0089       }
0090 
0091       entryIt = mCounter.find(conn->m_src);
0092       if (entryIt != mCounter.end()) {
0093         (*entryIt).second.second++;
0094       } else {
0095         int nDst = 0;
0096         int nSrc = 1;
0097         mCounter.insert(
0098             std::make_pair(conn->m_src, std::make_pair(nDst, nSrc)));
0099       }
0100     }
0101 
0102     // find layers with nSrc = 0
0103 
0104     std::set<unsigned int> zeroLayers;
0105 
0106     for (const auto &[key, value] : mCounter) {
0107       if (value.second != 0) {
0108         continue;
0109       }
0110 
0111       zeroLayers.insert(key);
0112     }
0113 
0114     // remove connections which use zeroLayer as destination
0115 
0116     std::vector<const GbtsConnection *> theStage;
0117 
0118     auto cIt = lConns.begin();
0119 
0120     while (cIt != lConns.end()) {
0121       if (zeroLayers.contains((*cIt)->m_dst)) {
0122         theStage.push_back(*cIt);
0123         cIt = lConns.erase(cIt);
0124         continue;
0125       }
0126       cIt++;
0127     }
0128     newConnMap.insert(std::make_pair(stageCounter, theStage));
0129     stageCounter++;
0130   }
0131 
0132   // create layer groups
0133 
0134   int currentStage = 0;
0135 
0136   // the doublet making is done using "outside-in" approach hence the reverse
0137   // iterations
0138 
0139   for (auto it = newConnMap.rbegin(); it != newConnMap.rend();
0140        it++, currentStage++) {
0141     const auto &[_, vConn] = *it;
0142 
0143     // loop over links, extract all connections for the stage, group sources by
0144     // L1 (dst) index
0145 
0146     std::map<unsigned int, std::vector<const GbtsConnection *>> l1ConnMap;
0147 
0148     for (const auto *conn : vConn) {
0149       unsigned int dst = conn->m_dst;
0150 
0151       auto l1MapIt = l1ConnMap.find(dst);
0152       if (l1MapIt != l1ConnMap.end()) {
0153         (*l1MapIt).second.push_back(conn);
0154       } else {
0155         std::vector<const GbtsConnection *> v = {conn};
0156         l1ConnMap.insert(std::make_pair(dst, v));
0157       }
0158     }
0159 
0160     std::vector<LayerGroup> lgv;
0161 
0162     lgv.reserve(l1ConnMap.size());
0163 
0164     for (const auto &[key, value] : l1ConnMap) {
0165       lgv.emplace_back(LayerGroup(key, value));
0166     }
0167 
0168     m_layerGroups.insert(std::make_pair(currentStage, lgv));
0169   }
0170 
0171   newConnMap.clear();
0172 }
0173 
0174 }  // namespace Acts