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