ATLAS Offline Software
Loading...
Searching...
No Matches
PixelMapping.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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>
16namespace 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 return {name, coords};
48 };
49 while (std::getline(fs, line)){
50 //hash at start indicates comment, do nothing
51 if (line[0] == '#') continue;
52 std::smatch m;
53 bool matched = std::regex_match(line, m, re);
54 if (not matched){
55 std::cout<<"line in unexpected format:\n"<<line<<std::endl;
56 continue;
57 }
58 const auto& [ptr, Ok]=m_internalMap.insert(dissectMatch(m));
59 if (not Ok){
60 std::cout<<"repeated entry:\n"<<line<<std::endl;
61 }
62 }
63 fs.close();
64 }
65
66
67 void
68 PixelMapping::mapping(const std::string & geographicalID, int *hashID, int *bec, int *layer, int *phimod, int *etamod) const{
69 auto pName = m_internalMap.find(geographicalID);
70 if (pName == m_internalMap.end()){
71 std::cout<<"id "<<geographicalID<<" not found in mapping"<<std::endl;
72 return;
73 }
74 const auto & coordinates = pName->second;
75 *hashID = coordinates[0];
76 *bec = coordinates[1];
77 *layer = coordinates[2];
78 *phimod = coordinates[3];
79 *etamod = coordinates[4];
80 return;
81 }
82
83
84int PixelMapping::getID(const std::string & geographicalID) const {
85 auto pName = m_internalMap.find(geographicalID);
86 if (pName == m_internalMap.end()) {
87 std::cout<<"id "<<geographicalID<<" not found in mapping"<<std::endl;
88 return -1;
89 }
90 const auto & coordinates = pName->second;
91 return coordinates[0];
92}
93
94 bool
95 PixelMapping::contains(const std::string & geographicalID) const{
96 return (m_internalMap.find(geographicalID) != m_internalMap.end());
97 }
98
99 int
101 return m_internalMap.size();
102 }
103
104
105}
const boost::regex re(r_e)
static Double_t fs
bool contains(const std::string &geographicalID) const
int getID(const std::string &geographicalID) const
std::unordered_map< std::string, Coordinates > m_internalMap
PixelMapping(const std::string &csvFilename)
std::array< int, 5 > Coordinates
void mapping(const std::string &geographicalID, int *hashID, int *bec, int *layer, int *phimod, int *etamod) const
MsgStream & msg
Definition testRead.cxx:32