ATLAS Offline Software
PixelMapping.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 // PixelMapping.cxx
5 // PixelMapping
6 //
7 // Created by sroe on 13/03/2023.
8 //
9 
10 #include "PixelMapping.h"
11 
12 #include <fstream>
13 #include <iostream>
14 #include <regex>
15 #include <stdexcept>
16 namespace pix{
17 
18  PixelMapping::PixelMapping(const std::string & csvFilename){
19  //read in csv file and fill data structures
20  //lines in file are of format 'D2A_B02_S1_M3, 1950, 2, 1, 10, 0'
21  //'#' at start of line denotes a comment or header
22  std::ifstream fs;
23  fs.open(csvFilename.c_str());
24  if (not fs.is_open()){
25  const std::string msg = "Failed to open the mapping file: " + csvFilename;
26  throw std::runtime_error(msg);
27  }
28  std::string line;
29  //string, digits, signed digits, digits, digits, signed digits
30  std::regex re(R"delim(^(.*), (\d+), (-?\d+), (\d+), (\d+), (-?\d+)$)delim");
31  auto dissectMatch=[](const std::smatch & m)->std::pair<std::string, Coordinates>{
32  static constexpr size_t expectedSize(7);
33  static constexpr size_t coordStart(2);
34  std::pair<std::string, PixelMapping::Coordinates> result{};
35  if (m.size()!=expectedSize) return result;
36  const std::string name = m[1];
37  Coordinates coords{};
38 
39  for (size_t i(0); i!=coords.size();++i){
40  try{
41  coords[i] = std::stoi(m.str(i+coordStart));
42  } catch (std::invalid_argument &e){
43  const std::string msg = "PixelMapping: Line of csv file in unexpected format, \n"+ std::string(m[0]);
44  throw std::runtime_error(msg);
45  }
46  }
47  result.first = name;
48  result.second = coords;
49  return result;
50  };
51  while (std::getline(fs, line)){
52  //hash at start indicates comment, do nothing
53  if (line[0] == '#') continue;
54  std::smatch m;
55  bool matched = std::regex_match(line, m, re);
56  if (not matched){
57  std::cout<<"line in unexpected format:\n"<<line<<std::endl;
58  continue;
59  }
60  const auto& [ptr, Ok]=m_internalMap.insert(dissectMatch(m));
61  if (not Ok){
62  std::cout<<"repeated entry:\n"<<line<<std::endl;
63  }
64  }
65  fs.close();
66  }
67 
68 
69  void
70  PixelMapping::mapping(const std::string & geographicalID, int *hashID, int *bec, int *layer, int *phimod, int *etamod) const{
71  auto pName = m_internalMap.find(geographicalID);
72  if (pName == m_internalMap.end()){
73  std::cout<<"id "<<geographicalID<<" not found in mapping"<<std::endl;
74  return;
75  }
76  const auto & coordinates = pName->second;
77  *hashID = coordinates[0];
78  *bec = coordinates[1];
79  *layer = coordinates[2];
80  *phimod = coordinates[3];
81  *etamod = coordinates[4];
82  return;
83  }
84 
85 
86 int PixelMapping::getID(const std::string & geographicalID) const {
87  auto pName = m_internalMap.find(geographicalID);
88  if (pName == m_internalMap.end()) {
89  std::cout<<"id "<<geographicalID<<" not found in mapping"<<std::endl;
90  return -1;
91  }
92  const auto & coordinates = pName->second;
93  return coordinates[0];
94 }
95 
96  bool
97  PixelMapping::contains(const std::string & geographicalID) const{
98  return (m_internalMap.find(geographicalID) != m_internalMap.end());
99  }
100 
101  int
103  return m_internalMap.size();
104  }
105 
106 
107 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
checkFileSG.line
line
Definition: checkFileSG.py:75
get_generator_info.result
result
Definition: get_generator_info.py:21
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
ParticleGun_SamplingFraction.bec
int bec
Definition: ParticleGun_SamplingFraction.py:89
PixelMapping.h
pix::PixelMapping::getID
int getID(const std::string &geographicalID) const
Definition: PixelMapping.cxx:86
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
pix::PixelMapping::nModules
int nModules() const
Definition: PixelMapping.cxx:102
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
CaloSwCorrections.etamod
def etamod(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:206
lumiFormat.i
int i
Definition: lumiFormat.py:85
CaloSwCorrections.phimod
def phimod(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:203
pix::PixelMapping::PixelMapping
PixelMapping(const std::string &csvFilename)
Definition: PixelMapping.cxx:18
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
python.ElectronD3PDObject.matched
matched
Definition: ElectronD3PDObject.py:138
re
const boost::regex re(r_e)
Herwig7_QED_EvtGen_ll.fs
dictionary fs
Definition: Herwig7_QED_EvtGen_ll.py:17
pix::PixelMapping::mapping
void mapping(const std::string &geographicalID, int *hashID, int *bec, int *layer, int *phimod, int *etamod) const
Definition: PixelMapping.cxx:70
pix::PixelMapping::m_internalMap
std::unordered_map< std::string, Coordinates > m_internalMap
Definition: PixelMapping.h:33
pix
Definition: PixelMapping.cxx:16
pix::PixelMapping::contains
bool contains(const std::string &geographicalID) const
Definition: PixelMapping.cxx:97
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
pix::PixelMapping::Coordinates
std::array< int, 5 > Coordinates
Definition: PixelMapping.h:32