ATLAS Offline Software
Loading...
Searching...
No Matches
TGCCableASDToPP.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <fstream>
8#include <sstream>
9
10#include "GaudiKernel/StatusCode.h"
14
15namespace MuonTGC_Cabling {
16
17TGCCableASDToPP::TGCCableASDToPP(const std::string& filename,
18 const std::string& diffFile)
20 initialize(filename, diffFile);
21}
22
24
25void TGCCableASDToPP::initialize(const std::string& filename,
26 const std::string& diffFile) {
28 std::make_shared<TGCDatabaseASDToPP>(filename, "FWD");
30 std::make_shared<TGCDatabaseASDToPP>(filename, "FSD");
32 std::make_shared<TGCDatabaseASDToPP>(filename, "FWT");
34 std::make_shared<TGCDatabaseASDToPP>(filename, "FST");
35
37 std::make_shared<TGCDatabaseASDToPP>(filename, "EWD");
39 std::make_shared<TGCDatabaseASDToPP>(filename, "ESD");
41 std::make_shared<TGCDatabaseASDToPP>(filename, "EWT");
43 std::make_shared<TGCDatabaseASDToPP>(filename, "EST");
44
46 std::make_shared<TGCDatabaseASDToPP>(filename, "FWI");
48 std::make_shared<TGCDatabaseASDToPP>(filename, "FSI");
50 std::make_shared<TGCDatabaseASDToPP>(filename, "EWI");
52 std::make_shared<TGCDatabaseASDToPP>(filename, "ESI");
53
54 for (int side = 0; side < +TGCId::SideType::MaxSideType; side++) {
55 for (int sector = 0; sector < TGCId::NUM_FORWARD_SECTOR; sector++) {
60 }
61 for (int sector = 0; sector < TGCId::NUM_ENDCAP_SECTOR; sector++) {
66 }
67 for (int sector = 0; sector < TGCId::NUM_INNER_SECTOR; sector++) {
72 }
73 }
74 updateDatabase(diffFile);
75}
76
77void TGCCableASDToPP::updateDatabase(const std::string& diffFile) {
78
79 std::vector<std::string> fileContent{};
80 std::ifstream inASDToPP;
81 inASDToPP.open(diffFile);
82 if (inASDToPP.bad()) {
83 throw std::runtime_error("Failed to open " + diffFile);
84 return;
85 }
86
87 std::string buf{};
88 // Copy database into m_ASD2PP_DIFF_12
89 while (getline(inASDToPP, buf)) {
90 char letter = buf.at(0);
91 // Truncation saves initialization CPU time of about 30 ms.
92 if (letter == '/' || letter == '*') {
93 continue;
94 }
95
96 fileContent.push_back(buf);
97 }
98
99 for (int side = 0; side < +TGCId::SideType::MaxSideType; side++) {
100 for (int sector = 0; sector < TGCId::NUM_FORWARD_SECTOR; sector++) {
101 updateIndividualDatabase(side, sector, fileContent, "FWD",
102 m_FWDdb[side][sector]);
103 updateIndividualDatabase(side, sector, fileContent, "FSD",
104 m_FSDdb[side][sector]);
105 updateIndividualDatabase(side, sector, fileContent, "FWT",
106 m_FWTdb[side][sector]);
107 updateIndividualDatabase(side, sector, fileContent, "FST",
108 m_FSTdb[side][sector]);
109 }
110 for (int sector = 0; sector < TGCId::NUM_ENDCAP_SECTOR; sector++) {
111 updateIndividualDatabase(side, sector, fileContent, "EWD",
112 m_EWDdb[side][sector]);
113 updateIndividualDatabase(side, sector, fileContent, "ESD",
114 m_ESDdb[side][sector]);
115 updateIndividualDatabase(side, sector, fileContent, "EWT",
116 m_EWTdb[side][sector]);
117 updateIndividualDatabase(side, sector, fileContent, "EST",
118 m_ESTdb[side][sector]);
119 }
120 for (int sector = 0; sector < TGCId::NUM_INNER_SECTOR; sector++) {
121 updateIndividualDatabase(side, sector, fileContent, "EWI",
122 m_EWIdb[side][sector]);
123 updateIndividualDatabase(side, sector, fileContent, "ESI",
124 m_ESIdb[side][sector]);
125 updateIndividualDatabase(side, sector, fileContent, "FWI",
126 m_FWIdb[side][sector]);
127 updateIndividualDatabase(side, sector, fileContent, "FSI",
128 m_FSIdb[side][sector]);
129 }
130 }
131}
132
133std::vector<std::vector<int>> TGCCableASDToPP::getUpdateInfo(
134 const int side, const int sector, const std::vector<std::string>& diffFile,
135 std::string_view blockname) {
136 // clear info
137 std::vector<std::vector<int>> info{};
138
139 std::vector<std::string>::const_iterator it = diffFile.begin();
140 std::vector<std::string>::const_iterator it_e = diffFile.end();
141 int size = 0;
142
143 // search block name
144 while (it != it_e) {
145 const std::string& buf = (*it);
146 ++it;
147 char firstl = buf.at(0);
148 if (firstl == '/' || firstl == '*') {
149 continue;
150 }
151 if (std::string_view{buf}.starts_with(blockname)) {
152 std::istringstream line(buf);
153 std::string temp;
154 line >> temp >> size;
155 break;
156 }
157 }
158
159 // loop over entries of specified block
160 while (it != it_e) {
161 const std::string& buf = (*it);
162 ++it;
163 char firstl = buf.at(0);
164 if (firstl == '/' || firstl == '*') {
165 continue;
166 }
167 if (firstl == 'E' || firstl == 'F') {
168 break;
169 }
170 std::istringstream line(buf);
171 std::vector<int> entry;
172 int t_side{}, t_sector{};
173 line >> t_side;
174 line >> t_sector;
175 bool isOK = false;
176 if ((t_side == side) && (t_sector == sector)) {
177 for (int i = 2; i < 8; i++) {
178 int temp = -1;
179 if (line >> temp) {
180 entry.push_back(temp);
181 } else {
182 break;
183 }
184 isOK = (i == 7);
185 }
186 if (isOK) {
187 info.push_back(std::move(entry));
188 }
189 }
190 }
191 return info;
192}
193
195 const TGCId::RegionType region,
196 const int sector,
197 const TGCId::ModuleType module) const {
198 if (side >= TGCId::SideType::MaxSideType) {
199 return nullptr;
200 }
201 if (sector < 0) {
202 return nullptr;
203 }
204
205 TGCDatabaseASDToPP* db = nullptr;
206 if (region == TGCId::RegionType::Endcap) {
207 switch (module) {
209 if (sector < TGCId::NUM_ENDCAP_SECTOR) {
210 db = m_EWDdb[+side][sector].get();
211 }
212 break;
214 if (sector < TGCId::NUM_ENDCAP_SECTOR) {
215 db = m_ESDdb[+side][sector].get();
216 }
217 break;
219 if (sector < TGCId::NUM_ENDCAP_SECTOR) {
220 db = m_EWTdb[+side][sector].get();
221 }
222 break;
224 if (sector < TGCId::NUM_ENDCAP_SECTOR) {
225 db = m_ESTdb[+side][sector].get();
226 }
227 break;
229 if (sector < TGCId::NUM_INNER_SECTOR) {
230 db = m_EWIdb[+side][sector].get();
231 }
232 break;
234 if (sector < TGCId::NUM_INNER_SECTOR) {
235 db = m_ESIdb[+side][sector].get();
236 }
237 break;
238 default:
239 break;
240 }
241 } else if (region == TGCId::RegionType::Forward) {
242 switch (module) {
244 if (sector < TGCId::NUM_FORWARD_SECTOR) {
245 db = m_FWDdb[+side][sector].get();
246 }
247 break;
249 if (sector < TGCId::NUM_FORWARD_SECTOR) {
250 db = m_FSDdb[+side][sector].get();
251 }
252 break;
254 if (sector < TGCId::NUM_FORWARD_SECTOR) {
255 db = m_FWTdb[+side][sector].get();
256 }
257 break;
259 if (sector < TGCId::NUM_FORWARD_SECTOR) {
260 db = m_FSTdb[+side][sector].get();
261 }
262 break;
264 if (sector < TGCId::NUM_INNER_SECTOR) {
265 db = m_FWIdb[+side][sector].get();
266 }
267 break;
269 if (sector < TGCId::NUM_INNER_SECTOR) {
270 db = m_FSIdb[+side][sector].get();
271 }
272 break;
273 default:
274 break;
275 }
276 }
277 return db;
278}
279
280std::unique_ptr<TGCChannelId> TGCCableASDToPP::getChannel(
281 const TGCChannelId& channelId, bool orChannel) const {
282 if (channelId.getChannelIdType() == TGCChannelId::ChannelIdType::ASDOut) {
283 return getChannelOut(channelId, orChannel);
284 }
285 if (channelId.getChannelIdType() == TGCChannelId::ChannelIdType::PPIn) {
286 return getChannelIn(channelId, orChannel);
287 }
288
289 return nullptr;
290}
291
292std::unique_ptr<TGCChannelId> TGCCableASDToPP::getChannelIn(
293 const TGCChannelId& ppin, bool orChannel) const {
294 if (orChannel || !ppin.isValid()) {
295 return nullptr;
296 }
297
298 TGCDatabaseASDToPP* databaseP =
299 getDatabase(ppin.getSideType(), ppin.getRegionType(), ppin.getSector(),
300 ppin.getModuleType());
301
302 if (databaseP == nullptr) {
303 return nullptr;
304 }
305
306 int indexOut[TGCDatabaseASDToPP::NIndexOut] = {
307 ppin.getId(), ppin.getBlock(), ppin.getChannel()};
308 int i = databaseP->getIndexDBOut(indexOut);
309 if (i < 0) {
310 return nullptr;
311 }
312
313 // ASD2PP.db is Backward connection
314 int layer = databaseP->getEntry(i, 0);
315 if (ppin.isStrip()) {
316 if (!ppin.isBackward()) {
317 layer = s_stripForward[layer];
318 }
319 }
320 int offset = (ppin.isWire()) ? 4 : 0;
321 int channel = databaseP->getEntry(i, 2 + offset);
322
323 // Endcap Triplet chamberId start from 1 in ASDOut
324 int chamber = databaseP->getEntry(i, 1);
325 if (ppin.isEndcap() && ppin.isTriplet()) {
326 chamber = chamber + 1;
327 }
328 return std::make_unique<TGCChannelASDOut>(
329 ppin.getSideType(), ppin.getSignalType(), ppin.getRegionType(),
330 ppin.getSector(), layer, chamber, channel);
331}
332
333std::unique_ptr<TGCChannelId> TGCCableASDToPP::getChannelOut(
334 const TGCChannelId& asdout, bool orChannel) const {
335 if (orChannel || !asdout.isValid()) {
336 return nullptr;
337 }
338
339 const bool asdoutisStrip = asdout.isStrip();
340 const bool asdoutisBackward = asdout.isBackward();
341 const bool asdoutisEndcap = asdout.isEndcap();
342 const bool asdoutisM1 = (asdout.getStation() == TGCId::StationType::M1);
343 const int asdoutLayer = asdout.getLayer();
344 const int asdoutChamber = asdout.getChamber();
345 const int asdoutChannel = asdout.getChannel();
346
347 TGCDatabaseASDToPP* databaseP =
348 getDatabase(asdout.getSideType(), asdout.getRegionType(),
349 asdout.getSector(), asdout.getModuleType());
350
351 if (databaseP == nullptr) {
352 return nullptr;
353 }
354
355 const int MaxEntry = databaseP->getMaxEntry();
356 for (int i = 0; i < MaxEntry; i++) {
357 // ASD2PP.db is Backward connection
358 int layer = asdoutLayer;
359 if (asdoutisStrip && !asdoutisBackward) {
360 layer = s_stripForward[layer];
361 }
362
363 int elecChannel = asdoutChannel;
364
365 // Endcap Triplet chamberId start from 1 in ASDOut
366 int chamber = asdoutChamber;
367 if (asdoutisEndcap && asdoutisM1) {
368 chamber = chamber - 1;
369 }
370 int offset = (asdout.isWire()) ? 4 : 0;
371 if (databaseP->getEntry(i, 0) == layer &&
372 databaseP->getEntry(i, 1) == chamber &&
373 databaseP->getEntry(i, 2 + offset) == elecChannel) {
374 int id = databaseP->getEntry(i, 3);
375 int block = databaseP->getEntry(i, 4);
376 int channel = databaseP->getEntry(i, 5);
377
378 return std::make_unique<TGCChannelPPIn>(
379 asdout.getSideType(), asdout.getStation(), asdout.getModuleType(),
380 asdout.getRegionType(), asdout.getSector(), id, block, channel);
381 }
382 }
383 return nullptr;
384}
385
387 const int side, const int sector, const std::vector<std::string>& diffFile,
388 std::string_view blockname,
389 std::shared_ptr<TGCDatabaseASDToPP>& database) {
390 if (!database) {
391 return;
392 }
393 std::vector<std::vector<int>> info =
394 getUpdateInfo(side, sector, diffFile, blockname);
395
396 if (info.empty()) {
397 return;
398 }
399
400 if (database->isCommon()) {
401 database = std::make_unique<TGCDatabaseASDToPP>(
402 *database,
403 false); // false means this database is not commonly used.
404 }
405
406 for (auto& i : info) {
407 database->update(i);
408 }
409}
410
411} // namespace MuonTGC_Cabling
size_t size() const
Number of registered mappings.
TGCDatabaseASDToPP * getDatabase(const TGCId::SideType side, const TGCId::RegionType region, const int sector, const TGCId::ModuleType module) const
void initialize(const std::string &filename, const std::string &diffFile)
void updateDatabase(const std::string &diffFile)
std::unique_ptr< TGCChannelId > getChannel(const TGCChannelId &channelId, bool orChannel=false) const
std::unique_ptr< TGCChannelId > getChannelOut(const TGCChannelId &asdout, bool orChannel=false) const
void updateIndividualDatabase(const int side, const int sector, const std::vector< std::string > &diffFile, std::string_view blockname, std::shared_ptr< TGCDatabaseASDToPP > &database)
std::vector< std::vector< int > > getUpdateInfo(const int side, const int sector, const std::vector< std::string > &diffFile, std::string_view blockname)
std::unique_ptr< TGCChannelId > getChannelIn(const TGCChannelId &ppin, bool orChannel=false) const
TGCCableASDToPP(const std::string &fileName, const std::string &diffFile)
static constexpr std::array< int, 10 > s_stripForward
TGCCable(CableType type=NoCableType)
Definition TGCCable.h:31
virtual bool isValid() const
virtual int getIndexDBOut(int *indexOut) const override
Get IndexDBOut (position in the databse between 0 and database.size()-1) from indexOut which is NInde...
virtual int getMaxEntry() const
virtual int getEntry(int entry, int column) const
StationType getStation() const
Definition TGCId.h:128
bool isEndcap() const
Definition TGCId.h:178
bool isTriplet() const
Definition TGCId.h:166
static constexpr int NUM_INNER_SECTOR
Definition TGCId.h:45
ModuleType getModuleType() const
Definition TGCId.h:131
static constexpr int NUM_ENDCAP_SECTOR
Definition TGCId.h:43
int getChamber() const
Definition TGCId.h:147
bool isStrip() const
Definition TGCId.h:160
RegionType getRegionType() const
Definition TGCId.h:137
bool isWire() const
Definition TGCId.h:163
virtual int getSector() const
Definition TGCId.h:144
SideType getSideType() const
Definition TGCId.h:125
SignalType getSignalType() const
Definition TGCId.h:134
int getId() const
Definition TGCId.h:150
static constexpr int NUM_FORWARD_SECTOR
Definition TGCId.h:44
void initialize()