ATLAS Offline Software
FPGATrackSimSectorBank.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
14 
16 #include <set>
17 #include <cassert>
18 #include <unordered_map>
19 #include <algorithm>
20 
21 using namespace asg::msgUserCode;
22 
23 FPGATrackSimSectorBank::FPGATrackSimSectorBank(std::string const & filepath)
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]
42  makeInverseMap();
43 }
44 
45 
46 // Retrieve the number of sectors and layers, resize m_s2mMap
48 {
49  bool ok = true;
50  std::string line;
51  int nSectors;
52 
53  ok = ok && std::getline(fin, 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 
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;
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 
111 std::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 
122 sector_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.
147 sector_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.
174 void 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 
FPGATrackSimSectorBank::readSectors
void readSectors(std::ifstream &fin)
Definition: FPGATrackSimSectorBank.cxx:65
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
FPGATrackSimSectorBank::readHeader
void readHeader(std::ifstream &fin)
Definition: FPGATrackSimSectorBank.cxx:47
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:70
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
python.PyAthena.module
module
Definition: PyAthena.py:131
FPGATrackSimSectorBank::findSector
sector_t findSector(std::vector< module_t > const &modules) const
Definition: FPGATrackSimSectorBank.cxx:122
module_t
int32_t module_t
Definition: FPGATrackSimTypes.h:20
lumiFormat.i
int i
Definition: lumiFormat.py:85
h
FPGATrackSimSectorBank::storeQOverPtBinning
void storeQOverPtBinning(const std::vector< double > &qOverPtBins, bool absBinning)
Definition: FPGATrackSimSectorBank.cxx:174
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
ANA_MSG_INFO
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:290
MessageCheck.h
macros for messaging and checking status codes
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
ANA_MSG_FATAL
#define ANA_MSG_FATAL(xmsg)
Macro printing fatal messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:296
sector_t
int32_t sector_t
Definition: FPGATrackSimTypes.h:21
FPGATrackSimSectorBank::getSectors
std::vector< sector_t > getSectors(unsigned layer, module_t module) const
Definition: FPGATrackSimSectorBank.cxx:111
FPGATrackSimHit.h
: FPGATrackSim-specific class to represent an hit in the detector.
FPGATrackSimSectorBank::FPGATrackSimSectorBank
FPGATrackSimSectorBank(std::string const &filepath)
Definition: FPGATrackSimSectorBank.cxx:23
FPGATrackSimSectorBank::makeInverseMap
void makeInverseMap()
Definition: FPGATrackSimSectorBank.cxx:97
compute_lumi.fin
fin
Definition: compute_lumi.py:19
MODULE_BADMODULE
#define MODULE_BADMODULE
Definition: FPGATrackSimTypes.h:13
FPGATrackSimTypes.h
FPGATrackSimSectorBank.h
This file declares a class that stores the module IDs of the sectors.
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288