Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-28 07:45:42

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