Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:18:58

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 #include "Acts/Seeding/GbtsLayerConnection.hpp"
0010 
0011 #include <cstring>
0012 #include <fstream>
0013 #include <iostream>
0014 #include <list>
0015 #include <memory>
0016 #include <ranges>
0017 #include <set>
0018 #include <unordered_map>
0019 
0020 namespace Acts::Experimental {
0021 
0022 GbtsLayerConnectionMap GbtsLayerConnectionMap::fromStream(
0023     std::istream& inStream, bool lrtMode) {
0024   GbtsLayerConnectionMap connectionMap;
0025 
0026   std::uint32_t nLinks{};
0027 
0028   inStream >> nLinks >> connectionMap.etaBinWidth;
0029 
0030   for (std::uint32_t l = 0; l < nLinks; l++) {
0031     std::uint32_t stage{};
0032     std::uint32_t lIdx{};
0033     std::uint32_t src{};
0034     std::uint32_t dst{};
0035     std::uint32_t nEntries{};
0036     std::uint32_t height{};
0037     std::uint32_t width{};
0038 
0039     inStream >> lIdx >> stage >> src >> dst >> height >> width >> nEntries;
0040 
0041     auto pC = std::make_unique<GbtsLayerConnection>(src, dst);
0042 
0043     std::uint32_t dummy{};
0044 
0045     for (std::uint32_t i = 0; i < height; ++i) {
0046       for (std::uint32_t j = 0; j < width; ++j) {
0047         inStream >> dummy;
0048       }
0049     }
0050 
0051     std::uint32_t srcvol_id = src / 1000;
0052     std::uint32_t dstvol_id = dst / 1000;
0053 
0054     bool srcIsStrip = (srcvol_id == 13 || srcvol_id == 12 || srcvol_id == 14);
0055     bool dstIsStrip = (dstvol_id == 13 || dstvol_id == 12 || dstvol_id == 14);
0056     if (lrtMode) {
0057       if (!srcIsStrip || !dstIsStrip) {
0058         continue;
0059       }
0060     } else {
0061       if (srcIsStrip || dstIsStrip) {
0062         continue;
0063       }
0064     }
0065 
0066     if (const auto it = connectionMap.connectionMap.find(stage);
0067         it == connectionMap.connectionMap.end()) {
0068       std::vector<std::unique_ptr<GbtsLayerConnection>> v;
0069       v.push_back(std::move(pC));  // move the unique_ptr in
0070       connectionMap.connectionMap.emplace(
0071           stage,
0072           std::move(v));  // move the vector into the map
0073     } else {
0074       it->second.push_back(std::move(pC));  // move into existing vector
0075     }
0076   }
0077 
0078   // re-arrange the connection stages
0079 
0080   std::list<const GbtsLayerConnection*> lConns;
0081 
0082   std::map<std::int32_t, std::vector<const GbtsLayerConnection*>> newConnMap;
0083 
0084   for (const auto& conn : connectionMap.connectionMap) {
0085     for (const auto& up : conn.second) {
0086       lConns.push_back(up.get());
0087     }
0088   }
0089 
0090   std::uint32_t stageCounter = 0;
0091 
0092   while (!lConns.empty()) {
0093     std::unordered_map<std::uint32_t, std::pair<std::int32_t, std::int32_t>>
0094         mCounter;  // layerKey, nDst, nSrc
0095 
0096     for (const auto& conn : lConns) {
0097       auto entryIt = mCounter.find(conn->dst);
0098       if (entryIt != mCounter.end()) {
0099         (*entryIt).second.first++;
0100       } else {
0101         std::uint32_t nDst = 1;
0102         std::uint32_t nSrc = 0;
0103         mCounter.insert(std::make_pair(conn->dst, std::make_pair(nDst, nSrc)));
0104       }
0105 
0106       entryIt = mCounter.find(conn->src);
0107       if (entryIt != mCounter.end()) {
0108         (*entryIt).second.second++;
0109       } else {
0110         std::uint32_t nDst = 0;
0111         std::uint32_t nSrc = 1;
0112         mCounter.insert(std::make_pair(conn->src, std::make_pair(nDst, nSrc)));
0113       }
0114     }
0115 
0116     // find layers with nSrc = 0
0117 
0118     std::set<std::uint32_t> zeroLayers;
0119 
0120     for (const auto& layerCounts : mCounter) {
0121       if (layerCounts.second.second != 0) {
0122         continue;
0123       }
0124 
0125       zeroLayers.insert(layerCounts.first);
0126     }
0127 
0128     // remove connections which use zeroLayer as destination
0129 
0130     std::vector<const GbtsLayerConnection*> theStage;
0131 
0132     std::list<const GbtsLayerConnection*>::iterator cIt = lConns.begin();
0133 
0134     while (cIt != lConns.end()) {
0135       if (zeroLayers.find((*cIt)->dst) !=
0136           zeroLayers.end()) {  // check if contains
0137         theStage.push_back(*cIt);
0138         cIt = lConns.erase(cIt);
0139         continue;
0140       }
0141       ++cIt;
0142     }
0143     newConnMap.insert(std::make_pair(stageCounter, theStage));
0144     stageCounter++;
0145   }
0146 
0147   // create layer groups
0148 
0149   std::uint32_t currentStage = 0;
0150 
0151   // the doublet making is done using "outside-in" approach hence the reverse
0152   // iterations
0153 
0154   for (const auto& it : std::views::reverse(newConnMap)) {
0155     const std::vector<const GbtsLayerConnection*>& vConn = it.second;
0156 
0157     // loop over links, extract all connections for the stage, group sources by
0158     // L1 (dst) index
0159 
0160     std::map<std::uint32_t, std::vector<const GbtsLayerConnection*>> l1ConnMap;
0161 
0162     for (const auto* conn : vConn) {
0163       std::uint32_t dst = conn->dst;
0164 
0165       std::map<std::uint32_t, std::vector<const GbtsLayerConnection*>>::iterator
0166           l1MapIt = l1ConnMap.find(dst);
0167       if (l1MapIt != l1ConnMap.end()) {
0168         (*l1MapIt).second.push_back(conn);
0169       } else {
0170         std::vector<const GbtsLayerConnection*> v = {conn};
0171         l1ConnMap.insert(std::make_pair(dst, v));
0172       }
0173     }
0174 
0175     std::vector<LayerGroup> lgv;
0176 
0177     lgv.reserve(l1ConnMap.size());
0178 
0179     for (const auto& l1Group : l1ConnMap) {
0180       lgv.push_back(LayerGroup(l1Group.first, l1Group.second));
0181     }
0182 
0183     connectionMap.layerGroups.insert(std::make_pair(currentStage, lgv));
0184 
0185     currentStage++;
0186   }
0187 
0188   return connectionMap;
0189 }
0190 
0191 GbtsLayerConnectionMap GbtsLayerConnectionMap::fromFile(std::string& inFile,
0192                                                         bool lrtMode) {
0193   std::ifstream inputStream(inFile.c_str());
0194   return fromStream(inputStream, lrtMode);
0195 }
0196 
0197 }  // namespace Acts::Experimental