Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:09:41

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2021 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 http://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     GbtsConnection *pC = new 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       delete pC;
0050       continue;
0051     }
0052 
0053     vol_id = dst / 1000;
0054 
0055     if (vol_id == 13 || vol_id == 12 || vol_id == 14) {
0056       delete pC;
0057       continue;
0058     }
0059 
0060     std::map<int, std::vector<GbtsConnection *>>::iterator it =
0061         m_connMap.find(stage);
0062 
0063     if (it == m_connMap.end()) {
0064       std::vector<GbtsConnection *> v(1, pC);
0065       m_connMap.insert(std::make_pair(stage, v));
0066     } else {
0067       (*it).second.push_back(pC);
0068     }
0069   }
0070 
0071   // re-arrange the connection stages
0072 
0073   std::list<const GbtsConnection *> lConns;
0074 
0075   std::map<int, std::vector<const GbtsConnection *>> newConnMap;
0076 
0077   for (const auto &conn : m_connMap) {
0078     std::copy(conn.second.begin(), conn.second.end(),
0079               std::back_inserter(lConns));
0080   }
0081 
0082   int stageCounter = 0;
0083 
0084   while (!lConns.empty()) {
0085     std::unordered_map<unsigned int, std::pair<int, int>>
0086         mCounter;  // layerKey, nDst, nSrc
0087 
0088     for (const auto &conn : lConns) {
0089       auto entryIt = mCounter.find(conn->m_dst);
0090       if (entryIt != mCounter.end()) {
0091         (*entryIt).second.first++;
0092       } else {
0093         int nDst = 1;
0094         int nSrc = 0;
0095         mCounter.insert(
0096             std::make_pair(conn->m_dst, std::make_pair(nDst, nSrc)));
0097       }
0098 
0099       entryIt = mCounter.find(conn->m_src);
0100       if (entryIt != mCounter.end()) {
0101         (*entryIt).second.second++;
0102       } else {
0103         int nDst = 0;
0104         int nSrc = 1;
0105         mCounter.insert(
0106             std::make_pair(conn->m_src, std::make_pair(nDst, nSrc)));
0107       }
0108     }
0109 
0110     // find layers with nSrc = 0
0111 
0112     std::set<unsigned int> zeroLayers;
0113 
0114     for (const auto &layerCounts : mCounter) {
0115       if (layerCounts.second.second != 0) {
0116         continue;
0117       }
0118 
0119       zeroLayers.insert(layerCounts.first);
0120     }
0121 
0122     // remove connections which use zeroLayer as destination
0123 
0124     std::vector<const GbtsConnection *> theStage;
0125 
0126     std::list<const GbtsConnection *>::iterator cIt = lConns.begin();
0127 
0128     while (cIt != lConns.end()) {
0129       if (zeroLayers.find((*cIt)->m_dst) !=
0130           zeroLayers.end()) {  // check if contains
0131         theStage.push_back(*cIt);
0132         cIt = lConns.erase(cIt);
0133         continue;
0134       }
0135       cIt++;
0136     }
0137     newConnMap.insert(std::make_pair(stageCounter, theStage));
0138     stageCounter++;
0139   }
0140 
0141   // create layer groups
0142 
0143   int currentStage = 0;
0144 
0145   // the doublet making is done using "outside-in" approach hence the reverse
0146   // iterations
0147 
0148   for (std::map<int, std::vector<const GbtsConnection *>>::reverse_iterator it =
0149            newConnMap.rbegin();
0150        it != newConnMap.rend(); ++it, currentStage++) {
0151     const std::vector<const GbtsConnection *> &vConn = (*it).second;
0152 
0153     // loop over links, extract all connections for the stage, group sources by
0154     // L1 (dst) index
0155 
0156     std::map<unsigned int, std::vector<const GbtsConnection *>> l1ConnMap;
0157 
0158     for (const auto *conn : vConn) {
0159       unsigned int dst = conn->m_dst;
0160 
0161       std::map<unsigned int, std::vector<const GbtsConnection *>>::iterator
0162           l1MapIt = l1ConnMap.find(dst);
0163       if (l1MapIt != l1ConnMap.end()) {
0164         (*l1MapIt).second.push_back(conn);
0165       } else {
0166         std::vector<const GbtsConnection *> v = {conn};
0167         l1ConnMap.insert(std::make_pair(dst, v));
0168       }
0169     }
0170 
0171     std::vector<LayerGroup> lgv;
0172 
0173     lgv.reserve(l1ConnMap.size());
0174 
0175     for (const auto &l1Group : l1ConnMap) {
0176       lgv.push_back(LayerGroup(l1Group.first, l1Group.second));
0177     }
0178 
0179     m_layerGroups.insert(std::make_pair(currentStage, lgv));
0180   }
0181 
0182   newConnMap.clear();
0183 }
0184 
0185 GbtsConnector::~GbtsConnector() {
0186   m_layerGroups.clear();
0187   for (std::map<int, std::vector<GbtsConnection *>>::iterator it =
0188            m_connMap.begin();
0189        it != m_connMap.end(); ++it) {
0190     for (std::vector<GbtsConnection *>::iterator cIt = (*it).second.begin();
0191          cIt != (*it).second.end(); ++cIt) {
0192       delete (*cIt);
0193     }
0194   }
0195 }
0196 }  // namespace Acts