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