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