ATLAS Offline Software
FPGATrackSimRegionMap.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
14 
15 #include <cstdlib>
16 #include <string>
17 #include <iostream>
18 #include <vector>
19 
20 using namespace std;
21 using namespace asg::msgUserCode;
22 
23 
25 // Constructor/Desctructor
27 
28 
29 FPGATrackSimRegionMap::FPGATrackSimRegionMap(const FPGATrackSimPlaneMap *pmap, std::string const & filepath) :
30  m_pmap(pmap)
31 {
32  // Open the file
33  ANA_MSG_INFO("Reading " << filepath);
34  ifstream fin(filepath);
35  if (!fin.is_open())
36  {
37  ANA_MSG_FATAL("Couldn't open " << filepath);
38  throw ("FPGATrackSimRegionMap Couldn't open " + filepath);
39  }
40 
41  // Reads the header of the file to resize all the vector members
43 
44  // Read all the region data
45  for (int region = 0; region < m_nregions; region++) readRegion(fin, region);
46 }
47 
48 
49 // Reads the header of the file to resize all the vector members
51 {
52  string line, towerKey;
53  bool ok = true;
54 
55  ok = ok && getline(fin, line);
56  ANA_MSG_DEBUG(line << " < " << ok);
57 
58  istringstream sline(line);
59  ok = ok && (sline >> towerKey >> m_nregions);
60  ok = ok && (towerKey == "towers");
61 
62 
63  if (!ok) ANA_MSG_FATAL("Error reading header");
64 
65  m_map.resize(m_nregions);
66  for (auto & vv : m_map)
67  {
68  vv.resize(m_pmap->getNLogiLayers());
69  for (size_t l = 0; l < vv.size(); l++) vv[l].resize(m_pmap->getNSections(l));
70  }
71 }
72 
73 
74 // Reads one region from file.
75 void FPGATrackSimRegionMap::readRegion(ifstream & fin, int expected_region)
76 {
77  string line, dummy;
78  bool ok = true;
79  int region = -1;
80  uint32_t linesRead = 0; // detLayer lines read
81 
82  while (getline(fin, line))
83  {
84  if (line.empty() || line[0] == '#') continue;
85  istringstream sline(line);
86 
87  if (region < 0) // Find the starting header of the next region
88  {
89  ok = ok && (sline >> region);// should check this is a sensible number
90  ok = ok && !(sline >> dummy); // No keyword to check that we're not reading a detector line, so make sure rest of string is empty
91  ok = ok && (region == expected_region);
92  if (!ok) break;
93  }
94  else // Detector layer line
95  {
96  int isPix{}, BEC{}, physLayer{}, phi_min{}, phi_max{}, phi_tot{}, eta_min{}, eta_max{}, eta_tot{};
97  //should check these are within sensible limits after they are read
98  ok = ok && (sline >> isPix >> BEC >> physLayer >> phi_min >> phi_max >> phi_tot >> eta_min >> eta_max >> eta_tot);
99  if (!ok) break;
100 
101  int logiLayer = m_pmap->getLayerSection(static_cast<SiliconTech>(isPix), static_cast<DetectorZone>(BEC), physLayer).layer;
102  int section = m_pmap->getLayerSection(static_cast<SiliconTech>(isPix), static_cast<DetectorZone>(BEC), physLayer).section;
103 
104  if (logiLayer > -1)
105  m_map[region][logiLayer][section] = { phi_min, phi_max, eta_min, eta_max };
106 
107  if (++linesRead == m_pmap->getNDetLayers()) break;
108  }
109  }
110 
111  if (!ok)
112  {
113  ANA_MSG_FATAL("Found error reading file at line: " << line);
114  throw "FPGATrackSimRegionMap read error";
115  }
116 }
117 
118 
119 // Read module id LUT (defining global -> tower-local module IDs)
120 void FPGATrackSimRegionMap::loadModuleIDLUT(std::string const & filepath)
121 {
122  ANA_MSG_INFO("Reading module LUT" << filepath);
123  ifstream fin(filepath);
124  if (!fin.is_open())
125  {
126  ANA_MSG_ERROR("Couldn't open " << filepath);
127  throw ("FPGATrackSimRegionMap Couldn't open " + filepath);
128  }
129 
130  m_global_local_map.clear();
131  m_global_local_map.resize(m_nregions, vector<map<uint32_t, uint32_t>>(m_pmap->getNLogiLayers()));
132 
133  string line;
134  while (getline(fin, line))
135  {
136  uint32_t region, layer, globalID, localID;
137  istringstream sline(line);
138 
139  if (!(sline >> region >> layer >> globalID >> localID))
140  ANA_MSG_WARNING("Error reading module LUT");
141  else if (region >= m_global_local_map.size() || layer >= m_pmap->getNLogiLayers())
142  ANA_MSG_WARNING("loadModuleIDLUT() bad region=" << region << " or layer=" << layer);
143  else
144  m_global_local_map[region][layer][globalID] = localID;
145  }
146 }
147 
148 // Copied from the 1D Hough bitstream tool.
149 void FPGATrackSimRegionMap::loadRadiiFile(std::string const & filepath)
150 {
151 
152  // Resize the radius structure appropriately.
153  m_radii_map.clear();
154  m_radii_map.resize(m_nregions, std::vector<double>(m_pmap->getNLogiLayers()));
155 
156  // Open the file
157  std::ifstream fin(filepath);
158  if (!fin.is_open())
159  {
160  ANA_MSG_FATAL("Couldn't open radius file " << filepath);
161  }
162 
163  // Variables to fill
164  std::string line;
165  bool ok = true;
166  double r = 0.0;
167 
168  // Parse the file
169  while (getline(fin, line))
170  {
171  if (line.empty() || line[0] == '#') continue;
172  std::istringstream sline(line);
173  std::vector<int> shifts;
174 
175  int subregion{-1};
176  ok = ok && (sline >> subregion);
177 
178  // The radii file contains an "inclusive" line and then one for each subregion.
179  // If we only have one region (because we are the rmap or because we are a subrmap
180  // with one z-slice) then we only want to read the inclusive line.
181  // Otherwise we want to read everything BUT the inclusive line.
182  if (m_nregions == 1 && subregion != -1) {
183  continue;
184  }
185  if (m_nregions > 1 && subregion == -1) {
186  continue;
187  }
188 
189  for (unsigned layer = 0; layer < m_pmap->getNLogiLayers(); layer++) {
190  ok = ok && (sline >> r);
191  if (!ok) break;
192  if (r<=0) {
193  ANA_MSG_WARNING("Radius in radiiFile is "<< r <<" for layer: " << layer << " setting to dummy value!");
194  r = 500.0; // dummy value that won't cause a crash, but won't work anywhere.
195  }
196  if (subregion == -1) {
197  m_radii_map[0][layer] = r;
198  } else {
199  m_radii_map[subregion][layer] = r;
200  }
201  }
202 
203  if (!ok) break;
204  }
205 
206  if (!ok)
207  {
208  ANA_MSG_FATAL("Found error reading file at line: " << line);
209  }
210 }
211 
212 
214 // Interface Functions
216 
218 {
219  // To avoid confusion and double-counting, by convention, always use the coordinates of the inner hit
220  // when testing if a spacepoint is in a (sub)region.
221  if (hit.getHitType() == HitType::spacepoint) {
222  return isInRegion(region, hit.getPairedLayer(), hit.getPairedSection(), hit.getPairedEtaModule(), hit.getPairedPhiModule());
223  } else {
224  return isInRegion(region, hit.getLayer(), hit.getSection(), hit.getEtaModule(), hit.getPhiModule());
225  }
226 }
227 
228 
230 {
231  if ( region >= m_map.size()
232  || layer >= m_map[region].size()
233  || section >= m_map[region][layer].size() )
234  {
235  return false;
236  }
237 
238  int eta_min = m_map[region][layer][section].eta_min;
239  int eta_max = m_map[region][layer][section].eta_max;
240 
241  if (eta < eta_min || eta > eta_max) return false;
242 
243  int phi_min = m_map[region][layer][section].phi_min;
244  int phi_max = m_map[region][layer][section].phi_max;
245 
246  // Need special cases for phi because it can go from 2pi to 0.
247  if (phi_min <= phi_max) // Region does not cross phi = 0
248  {
249  if (phi < phi_min || phi > phi_max) return false;
250  }
251  else // Region crosses phi = 0
252  {
253  if (phi < phi_min && phi > phi_max) return false;
254  }
255 
256  return true;
257 }
258 
259 std::vector<uint32_t> FPGATrackSimRegionMap::getRegions(const FPGATrackSimHit &hit) const
260 {
261  std::vector<uint32_t> regions;
262  for (uint32_t region = 0; region < m_map.size(); region++)
263  if (isInRegion(region, hit))
264  regions.push_back(region);
265  return regions;
266 }
267 
269 {
270  /*
271  Todo: Does this handle EC hits correctly?
272 
273  error code key:
274  6 digit number. 1 = ok. 2 = not ok
275 
276  1st digit - Endcap Check
277  2nd - region
278  3rd - layer
279  4th - section
280  5th - eta
281  6th - phi
282  */
283 
284  uint32_t layer = hit.getLayer();
285  uint32_t section = hit.getSection();
286  int eta = hit.getEtaModule();
287  int phi = hit.getPhiModule();
288 
289  int anyerr = 0;
290  int err[] = {1,1,1,1,1,1};
291 
292  if (region >= m_map.size()) anyerr = err[1] = 2;
293 
294  if (!anyerr && layer >= m_map[region].size()) anyerr = err[2] = 2;
295 
296  if (!anyerr && section >= m_map[region][layer].size()) anyerr = err[3] = 2;
297 
298  if (!anyerr) {
299  int eta_min = m_map[region][layer][section].eta_min;
300  int eta_max = m_map[region][layer][section].eta_max;
301 
302  if (eta < eta_min) err[4] = 3;
303  if (eta > eta_max) err[4] = 2;
304 
305  int phi_min = m_map[region][layer][section].phi_min;
306  int phi_max = m_map[region][layer][section].phi_max;
307 
308  // Need special cases for phi berrause it can go from 2pi to 0.
309  if (phi_min <= phi_max) // Region does not cross phi = 0
310  {
311  if (phi < phi_min || phi > phi_max) err[5] = 2;
312  }
313  else // Region crosses phi = 0
314  {
315  if (phi < phi_min && phi > phi_max) err[5] = 3;
316  }
317  }
318 
319  int error_code = 100000*err[0] + 10000*err[1] + 1000*err[2] + 100*err[3] + 10*err[4] + err[5];
320 
321  return error_code;
322 }
323 
324 
326 {
327  // TEMPORARY UNTIL WE HAVE A MODULE LUT
328  (void) region;
329  (void) layer;
330  return globalModuleID & 0x3ff;
331 }
332 
333 
335 {
336  if (region >= m_global_local_map.size() || layer >= m_pmap->getNLogiLayers())
337  {
338  ANA_MSG_ERROR("getGlobalID() bad region=" << region << " or layer=" << layer);
339  return -1;
340  }
341 
342  for (auto const & g_l : m_global_local_map[region][layer])
343  if (g_l.second == localModuleID) return g_l.first;
344 
345  ANA_MSG_ERROR("getGlobalID() Did not find global id for region " << region << ", layer " << layer << ", localID " << localModuleID);
346  return -1;
347 }
348 
349 double FPGATrackSimRegionMap::getAvgRadius(unsigned region, unsigned layer) const {
350 
351  if (region >= m_radii_map.size() || layer >= m_pmap->getNLogiLayers())
352  {
353  ANA_MSG_ERROR("getAvgRadius() bad region=" << region << " or layer=" << layer);
354  return -1;
355  }
356 
357  // Return the radius we loaded for this region.
358  return m_radii_map[region][layer];
359 }
FPGATrackSimHit::getSection
unsigned getSection() const
Definition: FPGATrackSimHit.cxx:80
beamspotman.r
def r
Definition: beamspotman.py:676
FPGATrackSimHit::getPhiModule
unsigned getPhiModule() const
Definition: FPGATrackSimHit.h:88
FPGATrackSimRegionMap::m_global_local_map
std::vector< std::vector< std::map< uint32_t, uint32_t > > > m_global_local_map
Definition: FPGATrackSimRegionMap.h:113
FPGATrackSimPlaneMap::getNSections
uint32_t getNSections(size_t logiLayer) const
Definition: FPGATrackSimPlaneMap.h:80
checkFileSG.line
line
Definition: checkFileSG.py:75
FPGATrackSimPlaneMap::getNLogiLayers
uint32_t getNLogiLayers() const
Definition: FPGATrackSimPlaneMap.h:78
LArNewCalib_Delay_OFC_Cali.BEC
BEC
Definition: LArNewCalib_Delay_OFC_Cali.py:118
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
FPGATrackSimHit::getPairedSection
unsigned getPairedSection() const
Definition: FPGATrackSimHit.h:123
FPGATrackSimHit::getLayer
unsigned getLayer() const
Definition: FPGATrackSimHit.cxx:74
FPGATrackSimHit::getPairedLayer
unsigned getPairedLayer() const
Definition: FPGATrackSimHit.h:124
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
HitType::spacepoint
@ spacepoint
FPGATrackSimHit::getEtaModule
int getEtaModule() const
Definition: FPGATrackSimHit.h:87
FPGATrackSimPlaneMap::getLayerSection
const LayerSection & getLayerSection(SiliconTech siTech, DetectorZone zone, uint32_t physLayer) const
Definition: FPGATrackSimPlaneMap.h:120
FPGATrackSimRegionMap::readRegion
void readRegion(std::ifstream &fin, int expected_region)
Definition: FPGATrackSimRegionMap.cxx:75
FPGATrackSimHit
Definition: FPGATrackSimHit.h:41
FPGATrackSimRegionMap.h
Maps ITK module indices to FPGATrackSim regions.
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
FPGATrackSimHit::getPairedEtaModule
int getPairedEtaModule() const
Definition: FPGATrackSimHit.h:121
FPGATrackSimRegionMap::FPGATrackSimRegionMap
FPGATrackSimRegionMap(FPGATrackSimPlaneMap const *pmap, std::string const &filepath)
Definition: FPGATrackSimRegionMap.cxx:29
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:182
FPGATrackSimRegionMap::getGlobalID
uint32_t getGlobalID(uint32_t region, uint32_t layer, uint32_t localModuleID) const
Definition: FPGATrackSimRegionMap.cxx:334
SiliconTech
SiliconTech
Definition: FPGATrackSimTypes.h:25
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
FPGATrackSimRegionMap::getLocalID
uint32_t getLocalID(uint32_t region, uint32_t layer, uint32_t globalModuleID) const
Definition: FPGATrackSimRegionMap.cxx:325
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
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
FPGATrackSimHit::getPairedPhiModule
unsigned getPairedPhiModule() const
Definition: FPGATrackSimHit.h:122
python.xAODType.dummy
dummy
Definition: xAODType.py:4
FPGATrackSimRegionMap::isInRegion
bool isInRegion(uint32_t region, const FPGATrackSimHit &hit) const
Definition: FPGATrackSimRegionMap.cxx:217
FPGATrackSimRegionMap::getRegions
std::vector< uint32_t > getRegions(const FPGATrackSimHit &hit) const
Definition: FPGATrackSimRegionMap.cxx:259
FPGATrackSimRegionMap::m_pmap
const FPGATrackSimPlaneMap * m_pmap
Definition: FPGATrackSimRegionMap.h:107
FPGATrackSimRegionMap::loadModuleIDLUT
void loadModuleIDLUT(std::string const &filepath)
Definition: FPGATrackSimRegionMap.cxx:120
ANA_MSG_FATAL
#define ANA_MSG_FATAL(xmsg)
Macro printing fatal messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:296
FPGATrackSimPlaneMap
Definition: FPGATrackSimPlaneMap.h:62
keylayer_zslicemap.isPix
isPix
Definition: keylayer_zslicemap.py:127
LayerSection::section
uint32_t section
Definition: FPGATrackSimPlaneMap.h:42
FPGATrackSimRegionMap::m_map
std::vector< std::vector< std::vector< FPGATrackSimRegionBoundaries > > > m_map
Definition: FPGATrackSimRegionMap.h:110
FPGATrackSimRegionMap::getUnmappedID
uint32_t getUnmappedID(uint32_t region, const FPGATrackSimHit &hit) const
Definition: FPGATrackSimRegionMap.cxx:268
DetectorZone
DetectorZone
Definition: FPGATrackSimTypes.h:28
LayerSection::layer
int layer
Definition: FPGATrackSimPlaneMap.h:41
FPGATrackSimRegionMap::m_radii_map
std::vector< std::vector< double > > m_radii_map
Definition: FPGATrackSimRegionMap.h:116
FPGATrackSimPlaneMap::getNDetLayers
uint32_t getNDetLayers() const
Definition: FPGATrackSimPlaneMap.h:77
LArHVGainsPredictor.error_code
error_code
Definition: LArHVGainsPredictor.py:229
compute_lumi.fin
fin
Definition: compute_lumi.py:19
FPGATrackSimRegionMap::allocateMap
void allocateMap(std::ifstream &fin)
Definition: FPGATrackSimRegionMap.cxx:50
FPGATrackSimRegionMap::m_nregions
int m_nregions
Definition: FPGATrackSimRegionMap.h:108
section
void section(const std::string &sec)
Definition: TestTriggerMenuAccess.cxx:22
PlotCalibFromCool.vv
vv
Definition: PlotCalibFromCool.py:716
FPGATrackSimRegionMap::loadRadiiFile
void loadRadiiFile(std::string const &radii_file)
Definition: FPGATrackSimRegionMap.cxx:149
FPGATrackSimHit::getHitType
HitType getHitType() const
Definition: FPGATrackSimHit.h:57
FPGATrackSimRegionMap::getAvgRadius
double getAvgRadius(unsigned region, unsigned layer) const
Definition: FPGATrackSimRegionMap.cxx:349
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288