9 #include "GaudiKernel/ISvcLocator.h"
13 #include "Identifier/Identifier.h"
14 #include "GeoModelKernel/throwExcept.h"
20 static const std::set<uint8_t> special_cards{50, 60, 61, 71};
28 ISvcLocator* svcLocator = Gaudi::svcLocator();
29 SmartIF<StoreGateSvc>
detStore{svcLocator->service(
"DetectorStore")};
34 if (
sc != StatusCode::SUCCESS) {
44 bool valid{!check_valid};
70 const int sequence, MsgStream&
log) {
72 if (special_cards.count(
type)) {
75 <<
" breaks the legacy database format. No need to add the "
76 "card if it's hardcoded in C++"
81 int nOfLayers{0}, ntubes{0},
number{sequence};
82 std::array<int, 8> newseq{};
89 log << MSG::ERROR <<
"More than 8 tubes in a layer, not possible !"
95 <<
" to the layer " <<
layer <<
" of mezzanine type " <<
type
99 newseq[ntubes] =
tube;
106 if (ntubes != 8 && ntubes != 6) {
107 log << MSG::ERROR <<
"in type " <<
type
108 <<
": number of tubes per mezzanine layer can be only 6 or 8 ! "
109 "what are you doing ???"
113 nOfLayers = 24 / ntubes;
114 if (
layer > nOfLayers) {
116 <<
"The maximum number of layers for this mezzanine is: "
117 << nOfLayers <<
" so you can't initialize layer: " <<
layer
126 <<
" layers mezzanine - OK, OK..." <<
endmsg;
130 std::array<uint8_t, 8> newLayer{};
131 for (
int i = 0;
i < ntubes; ++
i) {
132 newLayer[
i] = newseq[ntubes -
i - 1];
139 MezzMapping new_map =
141 ? (**itr).tdcToTubeMap()
146 for (
int i = 0;
i < ntubes; ++
i) {
148 const int chStart = ntubes * (
layer - 1);
151 for (
int lay = 1; lay <= nOfLayers; ++lay) {
152 const int chStart = ntubes * (lay - 1);
153 new_map[chStart +
i] = dummy_card.
tubeNumber(lay, newLayer[
i]);
160 log << MSG::ERROR <<
"The mezzanine type " <<
type
161 <<
"has been already initialized" <<
endmsg;
164 (*itr) = std::make_unique<MdtMezzanineCard>(new_map, nOfLayers,
type);
169 std::make_unique<MdtMezzanineCard>(new_map, nOfLayers,
type));
178 MezzCardList::const_iterator
it = std::find_if(
193 <<
"Mezzanine Type: " <<
static_cast<int>(map_data.
mezzanine_type)
194 <<
" not found in the list !" <<
endmsg;
199 auto newTdc = std::make_unique<MdtTdcMap>(mezzaType, map_data);
204 offModule.
cards.emplace(newTdc.get());
205 if (!offModule.
csm[0]) {
206 offModule.
csm[0] = map_data;
215 if (!secondModule.csm[0]){
216 secondModule.csm[0] = map_data;
217 }
else if (secondModule.csm[0] != map_data && !secondModule.
csm[1]) {
218 secondModule.
csm[1] = map_data;
219 std::swap(secondModule.csm[1], secondModule.csm[0]);
222 }
else if (offModule.
csm[0] != map_data) {
223 if (!offModule.
csm[1]) {
224 offModule.
csm[1] = map_data;
228 }
else if (offModule.
csm[1] != map_data) {
229 log << MSG::ERROR <<
"The mulit layer " << map_data
230 <<
" has already associated the CSMs " << std::endl
231 <<
" *** " << offModule.
csm[0] << std::endl
232 <<
" *** " << offModule.
csm[1] << std::endl
233 <<
", while this one is a third one and not supported"
239 if (attachedTdcs.size() <= map_data.
tdcId)
240 attachedTdcs.resize(map_data.
tdcId + 1);
242 m_tdcs.push_back(std::move(newTdc));
251 MsgStream&
log)
const {
252 OnlToOffMap::const_iterator module_itr =
m_toOfflineConv.find(cabling_map);
255 <<
"Could not find a cabling module to recieve offline Id for "
262 if (!module_itr->second.zero_module) {
263 log << MSG::WARNING <<
" No tdc with channel zero found for "
264 << module_itr->first <<
endmsg;
267 if (!module_itr->second.zero_module->offlineId(cabling_map,
log)) {
268 log << MSG::WARNING <<
"MdtTdMap::getOfflineId() -- Channel: "
269 <<
static_cast<unsigned>(cabling_map.
channelId)
270 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId)
276 const TdcOnlSet& attachedTdcs = module_itr->second.all_modules;
277 if (attachedTdcs.size() <= cabling_map.
tdcId) {
278 log << MSG::WARNING <<
"getOfflineId() -- Tdc: "
279 <<
static_cast<unsigned>(cabling_map.
tdcId)
280 <<
" is not part of " << module_itr->first <<
". Maximally "
281 << attachedTdcs.size() <<
" Tdcs were attached. " <<
endmsg;
286 log << MSG::WARNING <<
"getOfflineId() -- Tdc: "
287 <<
static_cast<unsigned>(cabling_map.
tdcId) <<
" not found in "
292 log << MSG::WARNING <<
"MdtTdMap::getOfflineId() -- channel: "
293 <<
static_cast<unsigned>(cabling_map.
channelId)
294 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId)
302 <<
"Channel: " <<
static_cast<unsigned>(cabling_map.
channelId)
303 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId) <<
" "
308 <<
" layer: " << cabling_map.
layer <<
" tube: " << cabling_map.
tube
316 MsgStream&
log)
const {
317 OffToOnlMap::const_iterator module_itr =
m_toOnlineConv.find(cabling_map);
320 <<
"getOnlineId() --- Could not find a cabling CSM set recieve "
325 const TdcOffSet& attachedTdcs = module_itr->second.cards;
326 TdcOffSet::const_iterator tdc_itr = attachedTdcs.find(cabling_map);
327 if (tdc_itr == attachedTdcs.end()) {
328 log << MSG::WARNING <<
"No matching Tdc channel was found for "
330 }
else if ((*tdc_itr)->onlineId(cabling_map,
log))
333 TdcOffSet::const_iterator control_itr =
334 std::find_if(attachedTdcs.begin(), attachedTdcs.end(),
336 return tdc->onlineId(cabling_map, log);
338 if (control_itr == attachedTdcs.end()) {
340 <<
"Second trial to find a valid cabling channel for "
341 << cabling_map <<
" failed as well. " <<
endmsg;
361 log << MSG::ERROR <<
"Could not find hashId for station: " << map_data
366 log << MSG::ERROR <<
"Could not find hashId for multi layer: " << map_data <<
endmsg;
375 <<
" and subdetector+rod ID: " << hardId <<
endmsg;
388 const bool robInitialized =
391 if (!robInitialized) {
401 MsgStream&
log)
const {
403 OffToOnlMap::const_iterator off_itr =
m_toOnlineConv.find(map_data);
405 log << MSG::ERROR <<
"csmNumOnChamber() -- Nothing is saved under "
409 return (1 * (off_itr->second.csm[0] == map_data)) +
410 (2 * (off_itr->second.csm[1] == map_data));
414 MsgStream&
log)
const {
419 <<
"getstationCode() -- Could not find HashId for module: "
432 MsgStream&
log)
const {
436 <<
"getMultiLayerCode() -- Could not determine the detector layer "
442 map_data.
phi, ml, 1, 1);
448 <<
"getMultiLayerCode() -- Could not find HashId for module: "
458 <<
"getMultiLayerCode() -- Could not find HashId for module: "
466 MsgStream&
log)
const {
471 log << MSG::WARNING <<
"ROB ID " << stationCode <<
" not found !" <<
endmsg;
477 const std::vector<IdentifierHash>& mdtHashVector, MsgStream&
log)
const {
478 std::vector<uint32_t> robVector;
480 for (
unsigned int i = 0;
i < mdtHashVector.size(); ++
i) {
484 <<
"ROB id not found for Hash Id: " << mdtHashVector[
i]
490 robVector.push_back(robId);
503 return Rob_it->second;
506 log << MSG::WARNING <<
"Rod ID not found !" <<
endmsg;
507 static const std::vector<IdentifierHash> emptyIdHashVec{};
508 return emptyIdHashVec;
512 const std::vector<uint32_t>& ROBId_list, MsgStream&
log)
const {
513 std::vector<IdentifierHash> HashVec;
514 for (
unsigned int i = 0;
i < ROBId_list.size(); ++
i) {
515 ROBToChamberMap::const_iterator Rob_it =
518 log << MSG::WARNING <<
"Rod ID " << ROBId_list[
i]
519 <<
" not found, continuing with the rest of the ROBId"
523 HashVec.insert(HashVec.end(), Rob_it->second.begin(),
524 Rob_it->second.end());
535 log << MSG::ERROR <<
"No tdc maps were loaded " <<
endmsg;
539 if (!
card->checkConsistency(
log))
543 const unsigned int offToOnlChan =
545 [](
unsigned int N,
const auto& map) {
546 return N + map.second.cards.size();
550 [](
unsigned int N,
const auto& map) {
551 return N + std::accumulate(map.second.begin(), map.second.end(), 0,
552 [](unsigned int M, const auto& tdc) {
557 if (offToOnlChan != onlToOffChan || onlToOffChan != m_tdcs.size()) {
559 <<
"Offline <-> online conversion channels were lost. Expect "
561 <<
" cabling channels. Counted Offline -> online channels "
562 << offToOnlChan <<
". Conted Online -> offline channels "
563 << onlToOffChan <<
endmsg;
566 for (
auto& toOff : m_toOfflineConv) {
567 MdtTdcModule&
mod = toOff.second;
568 TdcOnlSet::const_iterator itr = std::find_if(
570 return sorter && sorter->tdcZero() == 0;
572 if (itr ==
mod.end()) {
573 log << MSG::ERROR <<
"There is no tdc with channel 0 in "
575 <<
". That is important to decode the station names later"
579 mod.zero_module = (*itr);
582 log << MSG::INFO <<
"MdtCabling successfully loaded. Found in total "
583 << m_tdcs.size() <<
" channels." <<
endmsg;
587 std::unique_ptr<MdtMezzanineCard>
card, MsgStream&
log) {
588 if (!
card->checkConsistency(
log))
590 MezzCardList::const_iterator itr =
593 return existing->id() == card->id();
596 log << MSG::ERROR <<
"Mezzanine card " << std::endl
597 << (*card) <<
" has already been added " << std::endl
598 << (**itr) << std::endl
599 <<
" please check. " <<
endmsg;
606 MsgStream&
msg)
const {
610 const int mezzType =
cabling.mezzanine_type;
611 const int chanZero =
cabling.channelId;
612 const int layerZero =
cabling.layer;
613 const int tubeZero =
cabling.tube;
618 if (special_cards.count(mezzType)) {
622 <<
"legacyHedgehogCard() -- Special layout given " << mezzType
628 if (mezzType == 71) {
629 tube_chan = 5 - tube_chan;
650 if (mezzType == 50 || mezzType == 60 || mezzType == 61) {
652 if (layerZero == 1) {
659 if ((tubeZero - 1) % 6 == 0) {
660 tube = tubeZero + tube_chan;
662 tube = tubeZero - tube_chan;
672 else if (mezzType == 71) {
675 tube = tubeZero - tube_chan;
683 return std::make_unique<MdtMezzanineCard>(chMap, 4, mezzType);
692 <<
"to connect " <<
cabling <<
" tubeZero: " << tubeZero <<
endmsg;
699 if (layerZero == 1) {
710 ((
card->id() > 40 && localchan % 2 == 0) ||
711 (
card->id() < 40 && localchan < 3)))
715 OfflineCh locTubeLay =
card->offlineTube(
716 localchan + (
layer - 1) *
card->numTubesPerLayer(),
msg);
717 OfflineCh zeroTubeLay =
card->offlineTube(
718 chanZero + (
layer - 1) *
card->numTubesPerLayer(),
msg);
719 if (!locTubeLay.isValid || !zeroTubeLay.isValid)
722 tube = (
static_cast<int>(locTubeLay.tube) -
723 static_cast<int>(zeroTubeLay.tube) + tubeZero);
727 <<
" mapped to " <<
layer <<
", " <<
tube
728 <<
" locTube: " <<
static_cast<int>(locTubeLay.tube)
729 <<
" zeroTubeLay: " <<
static_cast<int>(zeroTubeLay.tube)
737 std::unique_ptr<MdtMezzanineCard> to_ret =
738 std::make_unique<MdtMezzanineCard>(chMap,
card->numTubeLayers(),
747 if (!tube_lay.isValid)
749 uint8_t tubeNumber = tube_lay.tube + tubeZeroOff + 1;
750 chMap[
chan] =
card->tubeNumber(tube_lay.layer, tubeNumber);
755 return std::make_unique<MdtMezzanineCard>(chMap,
card->numTubeLayers(),