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 "GaudiKernel/StatusCode.h"
11
12#include <fstream>
13#include <sstream>
14
15namespace MuonTGC_Cabling {
16
17TGCCableASDToPP::TGCCableASDToPP(const std::string& filename) :
19 m_tgcCablingDbTool("TGCCablingDbTool") {
20 initialize(filename);
21}
22
25
26
27void TGCCableASDToPP::initialize(const std::string& filename) {
28 m_commonDb[TGCId::Forward][TGCId::WD] = std::make_shared<TGCDatabaseASDToPP>(filename, "FWD");
29 m_commonDb[TGCId::Forward][TGCId::SD] = std::make_shared<TGCDatabaseASDToPP>(filename, "FSD");
30 m_commonDb[TGCId::Forward][TGCId::WT] = std::make_shared<TGCDatabaseASDToPP>(filename, "FWT");
31 m_commonDb[TGCId::Forward][TGCId::ST] = std::make_shared<TGCDatabaseASDToPP>(filename, "FST");
32
33 m_commonDb[TGCId::Endcap][TGCId::WD] = std::make_shared<TGCDatabaseASDToPP>(filename, "EWD");
34 m_commonDb[TGCId::Endcap][TGCId::SD] = std::make_shared<TGCDatabaseASDToPP>(filename, "ESD");
35 m_commonDb[TGCId::Endcap][TGCId::WT] = std::make_shared<TGCDatabaseASDToPP>(filename, "EWT");
36 m_commonDb[TGCId::Endcap][TGCId::ST] = std::make_shared<TGCDatabaseASDToPP>(filename, "EST");
37
38 m_commonDb[TGCId::Forward][TGCId::WI] = std::make_shared<TGCDatabaseASDToPP>(filename, "FWI");
39 m_commonDb[TGCId::Forward][TGCId::SI] = std::make_shared<TGCDatabaseASDToPP>(filename, "FSI");
40 m_commonDb[TGCId::Endcap][TGCId::WI] = std::make_shared<TGCDatabaseASDToPP>(filename, "EWI");
41 m_commonDb[TGCId::Endcap][TGCId::SI] = std::make_shared<TGCDatabaseASDToPP>(filename, "ESI");
42
43 for(int side=0; side < TGCId::MaxSideType; side++) {
44 for(int sector=0; sector < TGCId::NUM_FORWARD_SECTOR; sector++) {
49 }
50 for(int sector=0; sector < TGCId::NUM_ENDCAP_SECTOR; sector++) {
51 m_EWDdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::WD];
52 m_ESDdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::SD];
53 m_EWTdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::WT];
54 m_ESTdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::ST];
55 }
56 for(int sector=0; sector < TGCId::NUM_INNER_SECTOR; sector++) {
59 m_EWIdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::WI];
60 m_ESIdb[side][sector] = m_commonDb[TGCId::Endcap][TGCId::SI];
61 }
62 }
63}
64
65
67 if(m_tgcCablingDbTool.retrieve().isFailure()) return StatusCode::FAILURE;
68
69 StatusCode sc = m_tgcCablingDbTool->readASD2PP_DIFF_12FromText();
70 if(!sc.isSuccess()) {
71 return StatusCode::SUCCESS;
72 }
73
74 std::vector<std::string> *tmp_ASD2PP_DIFF_12 = m_tgcCablingDbTool->giveASD2PP_DIFF_12();
75 if(!tmp_ASD2PP_DIFF_12) return StatusCode::FAILURE;
76
77 m_ASD2PP_DIFF_12.clear();
78 // Truncation saves initialization CPU time of about 30 ms.
79 for (const std::string& s : *tmp_ASD2PP_DIFF_12) {
80 char letter = s.at(0);
81 if(letter=='/'||letter=='*') continue;
82 m_ASD2PP_DIFF_12.push_back(s);
83 }
84 delete tmp_ASD2PP_DIFF_12;
85 tmp_ASD2PP_DIFF_12 = nullptr;
86
87 for(int side=0; side<TGCId::MaxSideType; side++) {
88 for(int sector=0; sector<TGCId::NUM_FORWARD_SECTOR; sector++) {
89 StatusCode sc = updateIndividualDatabase(side, sector, "FWD", m_FWDdb[side][sector]);
90 if(!sc.isSuccess()) return sc;
91
92 sc = updateIndividualDatabase(side, sector, "FSD", m_FSDdb[side][sector]);
93 if(!sc.isSuccess()) return sc;
94 sc = updateIndividualDatabase(side, sector, "FWT", m_FWTdb[side][sector]);
95 if(!sc.isSuccess()) return sc;
96 sc = updateIndividualDatabase(side, sector, "FST", m_FSTdb[side][sector]);
97 if(!sc.isSuccess()) return sc;
98 }
99 for(int sector=0; sector<TGCId::NUM_ENDCAP_SECTOR; sector++) {
100 StatusCode sc = updateIndividualDatabase(side, sector, "EWD", m_EWDdb[side][sector]);
101 if(!sc.isSuccess()) return sc;
102 sc = updateIndividualDatabase(side, sector, "ESD", m_ESDdb[side][sector]);
103 if(!sc.isSuccess()) return sc;
104 sc = updateIndividualDatabase(side, sector, "EWT", m_EWTdb[side][sector]);
105 if(!sc.isSuccess()) return sc;
106 sc = updateIndividualDatabase(side, sector, "EST", m_ESTdb[side][sector]);
107 if(!sc.isSuccess()) return sc;
108 }
109 for(int sector=0; sector<TGCId::NUM_INNER_SECTOR; sector++) {
110 StatusCode sc = updateIndividualDatabase(side, sector, "EWI", m_EWIdb[side][sector]);
111 if(!sc.isSuccess()) return sc;
112 sc = updateIndividualDatabase(side, sector, "ESI", m_ESIdb[side][sector]);
113 if(!sc.isSuccess()) return sc;
114 sc = updateIndividualDatabase(side, sector, "FWI", m_FWIdb[side][sector]);
115 if(!sc.isSuccess()) return sc;
116 sc = updateIndividualDatabase(side, sector, "FSI", m_FSIdb[side][sector]);
117 if(!sc.isSuccess()) return sc;
118 }
119 }
120
121 return StatusCode::SUCCESS;
122}
123
124StatusCode TGCCableASDToPP::getUpdateInfo(const int side,
125 const int sector,
126 const std::string& blockname,
127 std::vector<std::vector<int> >& info)
128{
129 // clear info
130 info.clear();
131
132 std::vector<std::string>::const_iterator it = m_ASD2PP_DIFF_12.begin();
133 std::vector<std::string>::const_iterator it_e = m_ASD2PP_DIFF_12.end();
134 int size = 0;
135
136 // search block name
137 while(it!=it_e) {
138 const std::string &buf = (*it);
139 ++it;
140 char firstl = buf.at(0);
141 if(firstl=='/'||firstl=='*') continue;
142 if(buf.compare(0,blockname.size(),blockname)==0) {
143 std::istringstream line(buf);
144 std::string temp;
145 line >> temp >> size;
146 break;
147 }
148 }
149
150 // loop over entries of specified block
151 while(it!=it_e) {
152 const std::string &buf = (*it);
153 ++it;
154 char firstl = buf.at(0);
155 if(firstl=='/'||firstl=='*') continue;
156 if(firstl=='E'||firstl=='F') break;
157 std::istringstream line(buf);
158 std::vector<int> entry;
159 int t_side, t_sector;
160 line >> t_side;
161 line >> t_sector;
162 bool isOK = false;
163 if((t_side==side) && (t_sector==sector)) {
164 for(int i=2; i<8; i++) {
165 int temp=-1;
166 if(line >> temp) {
167 entry.push_back(temp);
168 } else {
169 break;
170 }
171 isOK = (i==7);
172 }
173 if(isOK) {
174 info.push_back(std::move(entry));
175 }
176 }
177 }
178
179 return StatusCode::SUCCESS;
180}
181
182
183
185 const int region,
186 const int sector,
187 const int module) const
188{
189 if(side<0 || side>=TGCId::MaxSideType) return nullptr;
190 if(sector<0) return nullptr;
191
192 TGCDatabaseASDToPP* db=nullptr;
193 if(region==TGCId::Endcap) {
194 switch(module) {
195 case TGCId::WD :
196 if(sector<TGCId::NUM_ENDCAP_SECTOR) db = m_EWDdb[side][sector].get();
197 break;
198 case TGCId::SD :
199 if(sector<TGCId::NUM_ENDCAP_SECTOR) db = m_ESDdb[side][sector].get();
200 break;
201 case TGCId::WT :
202 if(sector<TGCId::NUM_ENDCAP_SECTOR) db = m_EWTdb[side][sector].get();
203 break;
204 case TGCId::ST :
205 if(sector<TGCId::NUM_ENDCAP_SECTOR) db = m_ESTdb[side][sector].get();
206 break;
207 case TGCId::WI :
208 if(sector<TGCId::NUM_INNER_SECTOR) db = m_EWIdb[side][sector].get();
209 break;
210 case TGCId::SI :
211 if(sector<TGCId::NUM_INNER_SECTOR) db = m_ESIdb[side][sector].get();
212 break;
213 default:
214 break;
215 }
216 } else if(region==TGCId::Forward) {
217 switch(module) {
218 case TGCId::WD :
219 if(sector<TGCId::NUM_FORWARD_SECTOR) db = m_FWDdb[side][sector].get();
220 break;
221 case TGCId::SD :
222 if(sector<TGCId::NUM_FORWARD_SECTOR) db = m_FSDdb[side][sector].get();
223 break;
224 case TGCId::WT :
225 if(sector<TGCId::NUM_FORWARD_SECTOR) db = m_FWTdb[side][sector].get();
226 break;
227 case TGCId::ST :
228 if(sector<TGCId::NUM_FORWARD_SECTOR) db = m_FSTdb[side][sector].get();
229 break;
230 case TGCId::WI :
231 if(sector<TGCId::NUM_INNER_SECTOR) db = m_FWIdb[side][sector].get();
232 break;
233 case TGCId::SI :
234 if(sector<TGCId::NUM_INNER_SECTOR) db = m_FSIdb[side][sector].get();
235 break;
236 default:
237 break;
238 }
239 }
240 return db;
241}
242
243// reverse layers in Forward sector
244const int TGCCableASDToPP::s_stripForward[] = {2,1,0,4,3,6,5,8,7};
245
246
248 bool orChannel) const {
249 if(channelId) {
250 if(channelId->getChannelIdType()==TGCChannelId::ChannelIdType::ASDOut)
251 return getChannelOut(channelId,orChannel);
252 if(channelId->getChannelIdType()==TGCChannelId::ChannelIdType::PPIn)
253 return getChannelIn(channelId,orChannel);
254 }
255 return nullptr;
256}
257
259 bool orChannel) const {
260 if(orChannel) return nullptr;
261 if(ppin->isValid()==false) return nullptr;
262
263 TGCDatabaseASDToPP* databaseP =
264 getDatabase(ppin->getSideType(),
265 ppin->getRegionType(),
266 ppin->getSector(),
267 ppin->getModuleType());
268
269 if(databaseP==nullptr) return nullptr;
270
271 int indexOut[TGCDatabaseASDToPP::NIndexOut] =
272 {ppin->getId(), ppin->getBlock(), ppin->getChannel()};
273 int i = databaseP->getIndexDBOut(indexOut);
274 if(i<0) return nullptr;
275
276 // ASD2PP.db is Backward connection
277 int layer = databaseP->getEntry(i,0);
278 if(ppin->isStrip()) {
279 if(!(ppin->isBackward())) {
280 layer = s_stripForward[layer];
281 }
282 }
283 int offset = (ppin->isWire()) ? 4 : 0;
284 int channel = databaseP->getEntry(i,2+offset);
285
286 // Endcap Triplet chamberId start from 1 in ASDOut
287 int chamber = databaseP->getEntry(i,1);
288 if(ppin->isEndcap() && ppin->isTriplet()) {
289 chamber = chamber+1;
290 }
291 TGCChannelASDOut *asdout = new TGCChannelASDOut(ppin->getSideType(),
292 ppin->getSignalType(),
293 ppin->getRegionType(),
294 ppin->getSector(),
295 layer,
296 chamber,
297 channel);
298
299 return asdout;
300}
301
303 bool orChannel) const {
304 if(orChannel) return nullptr;
305 if(asdout->isValid()==false) return nullptr;
306
307 const bool asdoutisStrip = asdout->isStrip();
308 const bool asdoutisBackward = asdout->isBackward();
309 const bool asdoutisEndcap = asdout->isEndcap();
310 const bool asdoutisTriplet = asdout->isTriplet();
311 const int asdoutLayer = asdout->getLayer();
312 const int asdoutChamber = asdout->getChamber();
313 const int asdoutChannel = asdout->getChannel();
314
315 TGCDatabaseASDToPP* databaseP =
316 getDatabase(asdout->getSideType(),
317 asdout->getRegionType(),
318 asdout->getSector(),
319 asdout->getModuleType());
320
321 if(databaseP==nullptr) return nullptr;
322
323 TGCChannelPPIn* ppin = nullptr;
324 const int MaxEntry = databaseP->getMaxEntry();
325 for(int i=0; i<MaxEntry; i++) {
326 // ASD2PP.db is Backward connection
327 int layer = asdoutLayer;
328 if(asdoutisStrip) {
329 if(!asdoutisBackward) {
330 layer = s_stripForward[layer];
331 }
332 }
333
334 int elecChannel = asdoutChannel;
335
336 // Endcap Triplet chamberId start from 1 in ASDOut
337 int chamber = asdoutChamber;
338 if(asdoutisEndcap&&asdoutisTriplet)
339 chamber = chamber-1;
340 int offset = (asdout->isWire())? 4 : 0;
341 if(databaseP->getEntry(i,0)==layer&&
342 databaseP->getEntry(i,1)==chamber&&
343 databaseP->getEntry(i,2+offset)==elecChannel)
344 {
345 int id = databaseP->getEntry(i,3);
346 int block = databaseP->getEntry(i,4);
347 int channel = databaseP->getEntry(i,5);
348
349 ppin = new TGCChannelPPIn(asdout->getSideType(),
350 asdout->getModuleType(),
351 asdout->getRegionType(),
352 asdout->getSector(),
353 id,
354 block,
355 channel);
356 break;
357 }
358 }
359 return ppin;
360}
361
363 const int sector,
364 const std::string& blockname,
365 std::shared_ptr<TGCDatabaseASDToPP>& database) {
366 if (!database) return StatusCode::FAILURE;
367 std::vector<std::vector<int>> info;
368 StatusCode sc = getUpdateInfo(side, sector, blockname, info);
369 if(!sc.isSuccess()) return sc;
370
371 size_t info_size = info.size();
372 if(!info_size) return StatusCode::SUCCESS;
373
374 if(database->isCommon()) {
375 database.reset(new TGCDatabaseASDToPP(*database, false)); // false means this database is not commonly used.
376 if(!database) return StatusCode::FAILURE;
377 }
378
379 for(size_t i=0; i<info_size; i++) {
380 database->update(info[i]);
381 }
382
383 return StatusCode::SUCCESS;
384}
385
386} //end of namespace
static Double_t sc
ToolHandle< ITGCCablingDbTool > m_tgcCablingDbTool
StatusCode updateIndividualDatabase(const int side, const int sector, const std::string &blockname, std::shared_ptr< TGCDatabaseASDToPP > &database)
void initialize(const std::string &filename)
std::vector< std::string > m_ASD2PP_DIFF_12
StatusCode getUpdateInfo(const int side, const int sector, const std::string &blockname, std::vector< std::vector< int > > &info)
virtual TGCChannelId * getChannelOut(const TGCChannelId *asdout, bool orChannel=false) const
TGCDatabaseASDToPP * getDatabase(const int side, const int region, const int sector, const int module) const
virtual TGCChannelId * getChannelIn(const TGCChannelId *ppin, bool orChannel=false) const
virtual TGCChannelId * getChannel(const TGCChannelId *channelId, bool orChannel=false) const
TGCCable(CableType type=NoCableType)
Definition TGCCable.h:24
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(void) const
virtual int getEntry(int entry, int column) const
bool isEndcap() const
Definition TGCId.h:141
bool isTriplet() const
Definition TGCId.h:137
static constexpr int NUM_INNER_SECTOR
Definition TGCId.h:42
ModuleType getModuleType(void) const
Definition TGCId.h:122
static constexpr int NUM_ENDCAP_SECTOR
Definition TGCId.h:40
int getChamber() const
Definition TGCId.h:130
RegionType getRegionType(void) const
Definition TGCId.h:125
bool isStrip() const
Definition TGCId.h:135
SignalType getSignalType(void) const
Definition TGCId.h:123
bool isWire() const
Definition TGCId.h:136
virtual int getSector() const
Definition TGCId.h:129
SideType getSideType(void) const
Definition TGCId.h:121
int getId() const
Definition TGCId.h:131
static constexpr int NUM_FORWARD_SECTOR
Definition TGCId.h:41
void initialize()