ATLAS Offline Software
Loading...
Searching...
No Matches
FPGATrackSimSectorBank.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
9
10
14
16#include <set>
17#include <cassert>
18#include <unordered_map>
19#include <algorithm>
20
21using namespace asg::msgUserCode;
22
24{
25 // Open the file for reading
26 ANA_MSG_INFO("Reading " << filepath);
27 std::ifstream fin(filepath);
28 if (!fin.is_open())
29 {
30 ANA_MSG_ERROR("Couldn't open " << filepath);
31 throw ("FPGATrackSimSectorBank couldn't open " + filepath);
32 }
33
34 // Retrieve the number of sectors and layers, resize m_s2mMap
35 readHeader(fin);
36
37 // Read the modules from each sector
38 readSectors(fin);
39
40 // Make m2sMap
41 //coverity[tainted_data]
43}
44
45
46// Retrieve the number of sectors and layers, resize m_s2mMap
47void FPGATrackSimSectorBank::readHeader(std::ifstream & fin)
48{
49 bool ok = true;
50 std::string line;
51 int nSectors;
52
53 ok = ok && std::getline(fin, line);
54 ANA_MSG_DEBUG(line);
55
56 std::istringstream sline(line);
57 ok = ok && (sline >> nSectors >> m_nLayers);
58
59 if (!ok) throw "Error reading header";
60 // coverity [tainted_data]
61 m_s2mMap.resize(nSectors, std::vector<module_t>(m_nLayers));
62}
63
64
65void FPGATrackSimSectorBank::readSectors(std::ifstream & fin)
66{
67 std::string line;
68 bool ok = true;
69 int sector = 0; // make sure sectors are numbered in order
70 int sector_file, UNUSED, coverage;
71
72 while (std::getline(fin, line))
73 {
74 if (line.empty() || line[0] == '!') continue;
75 ANA_MSG_DEBUG(line);
76 std::istringstream sline(line);
77
78 ok = ok && (sline >> sector_file);
79 ok = ok && (sector_file == sector);
80
81 for (unsigned i = 0; i < m_nLayers; i++)
82 ok = ok && (sline >> m_s2mMap[sector][i]);
83
84 ok = ok && (sline >> UNUSED >> coverage); // both these are not stored
85
86 if (!ok) break;
87 sector++;
88 }
89
90 if (!ok) ANA_MSG_ERROR("Error reading file on line: " << line);
91}
92
93
94
95// Inverts m_s2mMap, a list of module ids for each sector, into a mapping
96// from module ids to sectors, m_m2sMap.
98{
99 m_m2sMap.resize(m_nLayers);
100 for (size_t layer = 0; layer < m_nLayers; layer++)
101 {
102 for (sector_t sector = 0; (size_t)sector < m_s2mMap.size(); sector++)
103 {
104 module_t moduleID = m_s2mMap[sector][layer];
105 m_m2sMap[layer].insert(std::make_pair(moduleID, sector));
106 }
107 }
108}
109
110
111std::vector<sector_t> FPGATrackSimSectorBank::getSectors(unsigned layer, module_t module) const
112{
113 std::vector<sector_t> out;
114 auto range = m_m2sMap[layer].equal_range(module);
115 for (auto it_m2s = range.first; it_m2s != range.second; it_m2s++)
116 out.push_back(it_m2s->second);
117
118 return out;
119}
120
121
122sector_t FPGATrackSimSectorBank::findSector(std::vector<module_t> const & modules) const
123{
124 assert(modules.size() == m_nLayers);
125
126 std::set<sector_t> sectors_good; // List of matching sectors so far.
127 for (size_t layer = 0; layer < m_nLayers; layer++)
128 {
129 std::set<sector_t> sectors_new;
130 auto range = m_m2sMap[layer].equal_range(modules[layer]);
131
132 for (auto it_m2s = range.first; it_m2s != range.second; it_m2s++)
133 if (layer == 0 || sectors_good.count(it_m2s->second))
134 sectors_new.insert(it_m2s->second);
135
136 if (sectors_new.empty()) return -1;
137 sectors_good = std::move(sectors_new);
138 }
139
140 assert(sectors_good.size() == 1);
141 return *(sectors_good.begin());
142}
143
144
145// Returns the sector matching 'hits' exactly (WC must be matched with WC), or -1 if none.
146// If multiple hits have different hashIDs, will prefer most popular, then random.
147sector_t FPGATrackSimSectorBank::findSector(const std::vector<std::vector<std::shared_ptr<const FPGATrackSimHit>>> & hits) const
148{
149 std::vector<module_t> modules(hits.size());
150
151 for (size_t i = 0; i < hits.size(); i++)
152 {
153 if (hits[i].empty())
154 {
155 modules[i] = MODULE_BADMODULE;
156 }
157 else if (hits[i].size() == 1)
158 {
159 modules[i] = hits[i][0]->getIdentifierHash();
160 }
161 else
162 {
163 std::unordered_map<unsigned, unsigned> hashCount;
164 for (auto const &h : hits[i]) hashCount[h->getIdentifierHash()]++;
165 modules[i] = std::max_element(hashCount.begin(), hashCount.end())->first;
166 }
167 }
168
169 return findSector(modules);
170}
171
172
173// Helper function. Store the q/pt binning information for this set of sectors here.
174void FPGATrackSimSectorBank::storeQOverPtBinning(const std::vector<double>& qOverPtBins, bool absBinning)
175{
176 m_absQOverPtBinning = absBinning;
177
178 // Retrieve q/pt binning information. Fallback to no binning if not set.
179 // Note that this behavior on whether or not we are using |q/pt|.
180 m_qOverPtBins = qOverPtBins;
181 if (m_qOverPtBins.size() == 0) {
182 ANA_MSG_FATAL("q/pt bin information not set in bank tag!");
183 }
184}
185
macros for messaging and checking status codes
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
#define ANA_MSG_FATAL(xmsg)
Macro printing fatal messages.
: FPGATrackSim-specific class to represent an hit in the detector.
This file declares a class that stores the module IDs of the sectors.
int32_t module_t
int32_t sector_t
#define MODULE_BADMODULE
static const Attributes_t empty
void storeQOverPtBinning(const std::vector< double > &qOverPtBins, bool absBinning)
std::vector< std::vector< module_t > > m_s2mMap
std::vector< sector_t > getSectors(unsigned layer, module_t module) const
void readHeader(std::ifstream &fin)
sector_t findSector(std::vector< module_t > const &modules) const
std::vector< double > m_qOverPtBins
std::vector< std::multimap< module_t, sector_t > > m_m2sMap
FPGATrackSimSectorBank(std::string const &filepath)
void readSectors(std::ifstream &fin)