ATLAS Offline Software
FPGATrackSimSectorBank.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2024 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  makeInverseMap();
42 }
43 
44 
45 // Retrieve the number of sectors and layers, resize m_s2mMap
47 {
48  bool ok = true;
49  std::string line;
50  int nSectors;
51 
52  ok = ok && std::getline(fin, line);
54 
55  std::istringstream sline(line);
56  ok = ok && (sline >> nSectors >> m_nLayers);
57 
58  if (!ok) throw "Error reading header";
59 
60  m_s2mMap.resize(nSectors, std::vector<module_t>(m_nLayers));
61 }
62 
63 
65 {
66  std::string line;
67  bool ok = true;
68  int sector = 0; // make sure sectors are numbered in order
69  int sector_file, UNUSED, coverage;
70 
71  while (std::getline(fin, line))
72  {
73  if (line.empty() || line[0] == '!') continue;
75  std::istringstream sline(line);
76 
77  ok = ok && (sline >> sector_file);
78  ok = ok && (sector_file == sector);
79 
80  for (unsigned i = 0; i < m_nLayers; i++)
81  ok = ok && (sline >> m_s2mMap[sector][i]);
82 
83  ok = ok && (sline >> UNUSED >> coverage); // both these are not stored
84 
85  if (!ok) break;
86  sector++;
87  }
88 
89  if (!ok) ANA_MSG_ERROR("Error reading file on line: " << line);
90 }
91 
92 
93 
94 // Inverts m_s2mMap, a list of module ids for each sector, into a mapping
95 // from module ids to sectors, m_m2sMap.
97 {
98  m_m2sMap.resize(m_nLayers);
99  for (size_t layer = 0; layer < m_nLayers; layer++)
100  {
101  for (sector_t sector = 0; (size_t)sector < m_s2mMap.size(); sector++)
102  {
103  module_t moduleID = m_s2mMap[sector][layer];
104  m_m2sMap[layer].insert(std::make_pair(moduleID, sector));
105  }
106  }
107 }
108 
109 
110 std::vector<sector_t> FPGATrackSimSectorBank::getSectors(unsigned layer, module_t module) const
111 {
112  std::vector<sector_t> out;
113  auto range = m_m2sMap[layer].equal_range(module);
114  for (auto it_m2s = range.first; it_m2s != range.second; it_m2s++)
115  out.push_back(it_m2s->second);
116 
117  return out;
118 }
119 
120 
121 sector_t FPGATrackSimSectorBank::findSector(std::vector<module_t> const & modules) const
122 {
123  assert(modules.size() == m_nLayers);
124 
125  std::set<sector_t> sectors_good; // List of matching sectors so far.
126  for (size_t layer = 0; layer < m_nLayers; layer++)
127  {
128  std::set<sector_t> sectors_new;
129  auto range = m_m2sMap[layer].equal_range(modules[layer]);
130 
131  for (auto it_m2s = range.first; it_m2s != range.second; it_m2s++)
132  if (layer == 0 || sectors_good.count(it_m2s->second))
133  sectors_new.insert(it_m2s->second);
134 
135  if (sectors_new.empty()) return -1;
136  sectors_good = sectors_new;
137  }
138 
139  assert(sectors_good.size() == 1);
140  return *(sectors_good.begin());
141 }
142 
143 
144 // Returns the sector matching 'hits' exactly (WC must be matched with WC), or -1 if none.
145 // If multiple hits have different hashIDs, will prefer most popular, then random.
146 sector_t FPGATrackSimSectorBank::findSector(const std::vector<std::vector<std::shared_ptr<const FPGATrackSimHit>>> & hits) const
147 {
148  std::vector<module_t> modules(hits.size());
149 
150  for (size_t i = 0; i < hits.size(); i++)
151  {
152  if (hits[i].empty())
153  {
154  modules[i] = MODULE_BADMODULE;
155  }
156  else if (hits[i].size() == 1)
157  {
158  modules[i] = hits[i][0]->getIdentifierHash();
159  }
160  else
161  {
162  std::unordered_map<unsigned, unsigned> hashCount;
163  for (auto const &h : hits[i]) hashCount[h->getIdentifierHash()]++;
164  modules[i] = std::max_element(hashCount.begin(), hashCount.end())->first;
165  }
166  }
167 
168  return findSector(modules);
169 }
170 
171 
172 // Helper function. Store the q/pt binning information for this set of sectors here.
173 void FPGATrackSimSectorBank::storeQOverPtBinning(const std::vector<double>& qOverPtBins, bool absBinning)
174 {
175  m_absQOverPtBinning = absBinning;
176 
177  // Retrieve q/pt binning information. Fallback to no binning if not set.
178  // Note that this behavior on whether or not we are using |q/pt|.
179  m_qOverPtBins = qOverPtBins;
180  if (m_qOverPtBins.size() == 0) {
181  ANA_MSG_FATAL("q/pt bin information not set in bank tag!");
182  }
183 }
184 
FPGATrackSimSectorBank::readSectors
void readSectors(std::ifstream &fin)
Definition: FPGATrackSimSectorBank.cxx:64
checkFileSG.line
line
Definition: checkFileSG.py:75
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
FPGATrackSimSectorBank::readHeader
void readHeader(std::ifstream &fin)
Definition: FPGATrackSimSectorBank.cxx:46
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
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:121
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:173
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:195
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:110
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:96
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