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::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++) {
62 m_EWDdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::WD];
63 m_ESDdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::SD];
64 m_EWTdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::WT];
65 m_ESTdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::ST];
66 }
67 for (int sector = 0; sector < TGCId::NUM_INNER_SECTOR; sector++) {
70 m_EWIdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::WI];
71 m_ESIdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::SI];
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::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
136 const std::string& blockname) {
137 // clear info
138 std::vector<std::vector<int>> info{};
139
140 std::vector<std::string>::const_iterator it = diffFile.begin();
141 std::vector<std::string>::const_iterator it_e = diffFile.end();
142 int size = 0;
143
144 // search block name
145 while (it != it_e) {
146 const std::string& buf = (*it);
147 ++it;
148 char firstl = buf.at(0);
149 if (firstl == '/' || firstl == '*') {
150 continue;
151 }
152 if (buf.compare(0, blockname.size(), blockname) == 0) {
153 std::istringstream line(buf);
154 std::string temp;
155 line >> temp >> size;
156 break;
157 }
158 }
159
160 // loop over entries of specified block
161 while (it != it_e) {
162 const std::string& buf = (*it);
163 ++it;
164 char firstl = buf.at(0);
165 if (firstl == '/' || firstl == '*') {
166 continue;
167 }
168 if (firstl == 'E' || firstl == 'F') {
169 break;
170 }
171 std::istringstream line(buf);
172 std::vector<int> entry;
173 int t_side, t_sector;
174 line >> t_side;
175 line >> t_sector;
176 bool isOK = false;
177 if ((t_side == side) && (t_sector == sector)) {
178 for (int i = 2; i < 8; i++) {
179 int temp = -1;
180 if (line >> temp) {
181 entry.push_back(temp);
182 } else {
183 break;
184 }
185 isOK = (i == 7);
186 }
187 if (isOK) {
188 info.push_back(std::move(entry));
189 }
190 }
191 }
192 return info;
193}
194
196 const int region,
197 const int sector,
198 const int module) const {
199 if (side < 0 || side >= TGCId::MaxSideType) {
200 return nullptr;
201 }
202 if (sector < 0) {
203 return nullptr;
204 }
205
206 TGCDatabaseASDToPP* db = nullptr;
207 if (region == TGCId::Endcap) {
208 switch (module) {
209 case TGCId::WD:
210 if (sector < TGCId::NUM_ENDCAP_SECTOR) {
211 db = m_EWDdb[side][sector].get();
212 }
213 break;
214 case TGCId::SD:
215 if (sector < TGCId::NUM_ENDCAP_SECTOR) {
216 db = m_ESDdb[side][sector].get();
217 }
218 break;
219 case TGCId::WT:
220 if (sector < TGCId::NUM_ENDCAP_SECTOR) {
221 db = m_EWTdb[side][sector].get();
222 }
223 break;
224 case TGCId::ST:
225 if (sector < TGCId::NUM_ENDCAP_SECTOR) {
226 db = m_ESTdb[side][sector].get();
227 }
228 break;
229 case TGCId::WI:
230 if (sector < TGCId::NUM_INNER_SECTOR) {
231 db = m_EWIdb[side][sector].get();
232 }
233 break;
234 case TGCId::SI:
235 if (sector < TGCId::NUM_INNER_SECTOR) {
236 db = m_ESIdb[side][sector].get();
237 }
238 break;
239 default:
240 break;
241 }
242 } else if (region == TGCId::Forward) {
243 switch (module) {
244 case TGCId::WD:
245 if (sector < TGCId::NUM_FORWARD_SECTOR) {
246 db = m_FWDdb[side][sector].get();
247 }
248 break;
249 case TGCId::SD:
250 if (sector < TGCId::NUM_FORWARD_SECTOR) {
251 db = m_FSDdb[side][sector].get();
252 }
253 break;
254 case TGCId::WT:
255 if (sector < TGCId::NUM_FORWARD_SECTOR) {
256 db = m_FWTdb[side][sector].get();
257 }
258 break;
259 case TGCId::ST:
260 if (sector < TGCId::NUM_FORWARD_SECTOR) {
261 db = m_FSTdb[side][sector].get();
262 }
263 break;
264 case TGCId::WI:
265 if (sector < TGCId::NUM_INNER_SECTOR) {
266 db = m_FWIdb[side][sector].get();
267 }
268 break;
269 case TGCId::SI:
270 if (sector < TGCId::NUM_INNER_SECTOR) {
271 db = m_FSIdb[side][sector].get();
272 }
273 break;
274 default:
275 break;
276 }
277 }
278 return db;
279}
280
281std::unique_ptr<TGCChannelId> TGCCableASDToPP::getChannel(
282 const TGCChannelId& channelId, bool orChannel) const {
283 if (channelId.getChannelIdType() == TGCChannelId::ChannelIdType::ASDOut) {
284 return getChannelOut(channelId, orChannel);
285 }
286 if (channelId.getChannelIdType() == TGCChannelId::ChannelIdType::PPIn) {
287 return getChannelIn(channelId, orChannel);
288 }
289
290 return nullptr;
291}
292
293std::unique_ptr<TGCChannelId> TGCCableASDToPP::getChannelIn(
294 const TGCChannelId& ppin, bool orChannel) const {
295 if (orChannel || !ppin.isValid()) {
296 return nullptr;
297 }
298
299 TGCDatabaseASDToPP* databaseP =
300 getDatabase(ppin.getSideType(), ppin.getRegionType(), ppin.getSector(),
301 ppin.getModuleType());
302
303 if (databaseP == nullptr) {
304 return nullptr;
305 }
306
307 int indexOut[TGCDatabaseASDToPP::NIndexOut] = {
308 ppin.getId(), ppin.getBlock(), ppin.getChannel()};
309 int i = databaseP->getIndexDBOut(indexOut);
310 if (i < 0) {
311 return nullptr;
312 }
313
314 // ASD2PP.db is Backward connection
315 int layer = databaseP->getEntry(i, 0);
316 if (ppin.isStrip()) {
317 if (!ppin.isBackward()) {
318 layer = s_stripForward[layer];
319 }
320 }
321 int offset = (ppin.isWire()) ? 4 : 0;
322 int channel = databaseP->getEntry(i, 2 + offset);
323
324 // Endcap Triplet chamberId start from 1 in ASDOut
325 int chamber = databaseP->getEntry(i, 1);
326 if (ppin.isEndcap() && ppin.isTriplet()) {
327 chamber = chamber + 1;
328 }
329 return std::make_unique<TGCChannelASDOut>(
330 ppin.getSideType(), ppin.getSignalType(), ppin.getRegionType(),
331 ppin.getSector(), layer, chamber, channel);
332}
333
334std::unique_ptr<TGCChannelId> TGCCableASDToPP::getChannelOut(
335 const TGCChannelId& asdout, bool orChannel) const {
336 if (orChannel || !asdout.isValid()) {
337 return nullptr;
338 }
339
340 const bool asdoutisStrip = asdout.isStrip();
341 const bool asdoutisBackward = asdout.isBackward();
342 const bool asdoutisEndcap = asdout.isEndcap();
343 const bool asdoutisTriplet = asdout.isTriplet();
344 const int asdoutLayer = asdout.getLayer();
345 const int asdoutChamber = asdout.getChamber();
346 const int asdoutChannel = asdout.getChannel();
347
348 TGCDatabaseASDToPP* databaseP =
349 getDatabase(asdout.getSideType(), asdout.getRegionType(),
350 asdout.getSector(), asdout.getModuleType());
351
352 if (databaseP == nullptr) {
353 return nullptr;
354 }
355
356 const int MaxEntry = databaseP->getMaxEntry();
357 for (int i = 0; i < MaxEntry; i++) {
358 // ASD2PP.db is Backward connection
359 int layer = asdoutLayer;
360 if (asdoutisStrip && !asdoutisBackward) {
361 layer = s_stripForward[layer];
362 }
363
364 int elecChannel = asdoutChannel;
365
366 // Endcap Triplet chamberId start from 1 in ASDOut
367 int chamber = asdoutChamber;
368 if (asdoutisEndcap && asdoutisTriplet) {
369 chamber = chamber - 1;
370 }
371 int offset = (asdout.isWire()) ? 4 : 0;
372 if (databaseP->getEntry(i, 0) == layer &&
373 databaseP->getEntry(i, 1) == chamber &&
374 databaseP->getEntry(i, 2 + offset) == elecChannel) {
375 int id = databaseP->getEntry(i, 3);
376 int block = databaseP->getEntry(i, 4);
377 int channel = databaseP->getEntry(i, 5);
378
379 return std::make_unique<TGCChannelPPIn>(
380 asdout.getSideType(), asdout.getModuleType(),
381 asdout.getRegionType(), asdout.getSector(), id, block, channel);
382 }
383 }
384 return nullptr;
385}
386
388 const int side, const int sector, const std::vector<std::string>& diffFile,
389 const std::string& blockname,
390 std::shared_ptr<TGCDatabaseASDToPP>& database) {
391 if (!database) {
392 return;
393 }
394 std::vector<std::vector<int>> info =
395 getUpdateInfo(side, sector, diffFile, blockname);
396
397 if (info.empty()) {
398 return;
399 }
400
401 if (database->isCommon()) {
402 database = std::make_unique<TGCDatabaseASDToPP>(
403 *database,
404 false); // false means this database is not commonly used.
405 }
406
407 for (auto& i : info) {
408 database->update(i);
409 }
410}
411
412} // namespace MuonTGC_Cabling
static constexpr std::array< int, 9 > s_stripForward
std::vector< std::vector< int > > getUpdateInfo(const int side, const int sector, const std::vector< std::string > &diffFile, const std::string &blockname)
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
void updateIndividualDatabase(const int side, const int sector, const std::vector< std::string > &diffFile, const std::string &blockname, std::shared_ptr< TGCDatabaseASDToPP > &database)
std::unique_ptr< TGCChannelId > getChannelOut(const TGCChannelId &asdout, bool orChannel=false) const
std::unique_ptr< TGCChannelId > getChannelIn(const TGCChannelId &ppin, bool orChannel=false) const
TGCDatabaseASDToPP * getDatabase(const int side, const int region, const int sector, const int module) const
TGCCableASDToPP(const std::string &fileName, const std::string &diffFile)
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
bool isEndcap() const
Definition TGCId.h:190
bool isTriplet() const
Definition TGCId.h:178
static constexpr int NUM_INNER_SECTOR
Definition TGCId.h:43
ModuleType getModuleType() const
Definition TGCId.h:137
static constexpr int NUM_ENDCAP_SECTOR
Definition TGCId.h:41
int getChamber() const
Definition TGCId.h:159
bool isStrip() const
Definition TGCId.h:172
RegionType getRegionType() const
Definition TGCId.h:146
bool isWire() const
Definition TGCId.h:175
virtual int getSector() const
Definition TGCId.h:156
SideType getSideType() const
Definition TGCId.h:134
SignalType getSignalType() const
Definition TGCId.h:140
int getId() const
Definition TGCId.h:162
static constexpr int NUM_FORWARD_SECTOR
Definition TGCId.h:42
void initialize()