10 #include "GaudiKernel/ISvcLocator.h"
19 static const std::set<uint8_t> special_cards{50, 60, 61, 71};
27 ISvcLocator* svcLocator = Gaudi::svcLocator();
30 if (
sc != StatusCode::SUCCESS) {
31 throw std::runtime_error(
"Could not find the detctor store");
34 if (
sc != StatusCode::SUCCESS) {
35 throw std::runtime_error(
"Could not retrieve the MdtIdHelper");
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(
180 [&](
const MezzCardPtr& card) { return card->id() == mezzCardId; });
193 <<
"Mezzanine Type: " <<
static_cast<int>(map_data.
mezzanine_type)
194 <<
" not found in the list !" <<
endmsg;
197 !mezzaType->checkConsistency(
log))
200 std::unique_ptr<MdtTdcMap> newTdc =
201 std::make_unique<MdtTdcMap>(mezzaType, map_data);
207 offModule.
cards.emplace(newTdc.get());
208 if (!offModule.
csm[0])
209 offModule.
csm[0] = map_data;
210 else if (offModule.
csm[0] != map_data) {
211 if (!offModule.
csm[1]) {
212 offModule.
csm[1] = map_data;
217 }
else if (offModule.
csm[1] != map_data) {
218 log << MSG::ERROR <<
"The mulit layer " << map_data
219 <<
" has already associated the CSMs " << std::endl
220 <<
" *** " << offModule.
csm[0] << std::endl
221 <<
" *** " << offModule.
csm[1] << std::endl
222 <<
", while this one is a third one and not supported"
228 if (attachedTdcs.size() <= map_data.
tdcId)
229 attachedTdcs.resize(map_data.
tdcId + 1);
231 m_tdcs.push_back(std::move(newTdc));
240 MsgStream&
log)
const {
241 OnlToOffMap::const_iterator module_itr =
m_toOfflineConv.find(cabling_map);
244 <<
"Could not find a cabling module to recieve offline Id for "
251 if (!module_itr->second.zero_module) {
252 log << MSG::WARNING <<
" No tdc with channel zero found for "
253 << module_itr->first <<
endmsg;
256 if (!module_itr->second.zero_module->offlineId(cabling_map,
log)) {
257 log << MSG::WARNING <<
"MdtTdMap::getOfflineId() -- Channel: "
258 <<
static_cast<unsigned>(cabling_map.
channelId)
259 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId)
265 const TdcOnlSet& attachedTdcs = module_itr->second.all_modules;
266 if (attachedTdcs.size() < cabling_map.
tdcId) {
267 log << MSG::WARNING <<
"getOfflineId() -- Tdc: "
268 <<
static_cast<unsigned>(cabling_map.
tdcId)
269 <<
" is not part of " << module_itr->first <<
". Maximally "
270 << attachedTdcs.size() <<
" Tdcs were attached. " <<
endmsg;
275 log << MSG::WARNING <<
"getOfflineId() -- Tdc: "
276 <<
static_cast<unsigned>(cabling_map.
tdcId) <<
" not found in "
281 log << MSG::WARNING <<
"MdtTdMap::getOfflineId() -- channel: "
282 <<
static_cast<unsigned>(cabling_map.
channelId)
283 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId)
291 <<
"Channel: " <<
static_cast<unsigned>(cabling_map.
channelId)
292 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId) <<
" "
297 <<
" layer: " << cabling_map.
layer <<
" tube: " << cabling_map.
tube
305 MsgStream&
log)
const {
306 OffToOnlMap::const_iterator module_itr =
m_toOnlineConv.find(cabling_map);
309 <<
"getOnlineId() --- Could not find a cabling CSM set recieve "
314 const TdcOffSet& attachedTdcs = module_itr->second.cards;
315 TdcOffSet::const_iterator tdc_itr = attachedTdcs.find(cabling_map);
316 if (tdc_itr == attachedTdcs.end()) {
317 log << MSG::WARNING <<
"No matching Tdc channel was found for "
319 }
else if ((*tdc_itr)->onlineId(cabling_map,
log))
322 TdcOffSet::const_iterator control_itr =
323 std::find_if(attachedTdcs.begin(), attachedTdcs.end(),
325 return tdc->onlineId(cabling_map, log);
327 if (control_itr == attachedTdcs.end()) {
329 <<
"Second trial to find a valid cabling channel for "
330 << cabling_map <<
" failed as well. " <<
endmsg;
350 log << MSG::ERROR <<
"Could not found hashId for station: " << map_data
356 <<
"Could not found hashId for multi layer: " << map_data <<
endmsg;
365 <<
" and subdetector+rod ID: " << hardId <<
endmsg;
378 const bool robInitialized =
381 if (!robInitialized) {
391 MsgStream&
log)
const {
393 OffToOnlMap::const_iterator off_itr =
m_toOnlineConv.find(map_data);
395 log << MSG::ERROR <<
"csmNumOnChamber() -- Nothing is saved under "
399 return (1 * (off_itr->second.csm[0] == map_data)) +
400 (2 * (off_itr->second.csm[1] == map_data));
404 MsgStream&
log)
const {
409 <<
"getstationCode() -- Could not find HashId for module: "
422 MsgStream&
log)
const {
426 <<
"getMultiLayerCode() -- Could not determine the detector layer "
432 map_data.
phi, ml, 1, 1);
438 <<
"getMultiLayerCode() -- Could not find HashId for module: "
448 <<
"getMultiLayerCode() -- Could not find HashId for module: "
456 MsgStream&
log)
const {
461 log << MSG::WARNING <<
"ROB ID " << stationCode <<
" not found !" <<
endmsg;
467 const std::vector<IdentifierHash>& mdtHashVector, MsgStream&
log)
const {
468 std::vector<uint32_t> robVector;
470 for (
unsigned int i = 0;
i < mdtHashVector.size(); ++
i) {
474 <<
"ROB id not found for Hash Id: " << mdtHashVector[
i]
480 robVector.push_back(robId);
493 return Rob_it->second;
496 log << MSG::WARNING <<
"Rod ID not found !" <<
endmsg;
497 static const std::vector<IdentifierHash> emptyIdHashVec{};
498 return emptyIdHashVec;
502 const std::vector<uint32_t>& ROBId_list, MsgStream&
log)
const {
503 std::vector<IdentifierHash> HashVec;
504 for (
unsigned int i = 0;
i < ROBId_list.size(); ++
i) {
505 ROBToChamberMap::const_iterator Rob_it =
508 log << MSG::WARNING <<
"Rod ID " << ROBId_list[
i]
509 <<
" not found, continuing with the rest of the ROBId"
513 HashVec.insert(HashVec.end(), Rob_it->second.begin(),
514 Rob_it->second.end());
525 log << MSG::ERROR <<
"No tdc maps were loaded " <<
endmsg;
529 if (!card->checkConsistency(
log))
533 const unsigned int offToOnlChan =
535 [](
unsigned int N,
const auto& map) {
536 return N + map.second.cards.size();
540 [](
unsigned int N,
const auto& map) {
541 return N + std::accumulate(map.second.begin(), map.second.end(), 0,
542 [](unsigned int M, const auto& tdc) {
547 if (offToOnlChan != onlToOffChan || onlToOffChan != m_tdcs.size()) {
549 <<
"Offline <-> online conversion channels were lost. Expect "
551 <<
" cabling channels. Counted Offline -> online channels "
552 << offToOnlChan <<
". Conted Online -> offline channels "
553 << onlToOffChan <<
endmsg;
556 for (
auto& toOff : m_toOfflineConv) {
557 MdtTdcModule&
mod = toOff.second;
558 TdcOnlSet::const_iterator itr = std::find_if(
560 return sorter && sorter->tdcZero() == 0;
562 if (itr ==
mod.end()) {
563 log << MSG::ERROR <<
"There is no tdc with channel 0 in "
565 <<
". That is important to decode the station names later"
569 mod.zero_module = (*itr);
572 log << MSG::INFO <<
"MdtCabling successfully loaded. Found in total "
573 << m_tdcs.size() <<
" channels." <<
endmsg;
577 std::unique_ptr<MdtMezzanineCard> card, MsgStream&
log) {
580 MezzCardList::const_iterator itr =
583 return existing->id() == card->id();
586 log << MSG::ERROR <<
"Mezzanine card " << std::endl
587 << (*card) <<
" has already been added " << std::endl
588 << (**itr) << std::endl
589 <<
" please check. " <<
endmsg;
596 MsgStream&
msg)
const {
600 const int mezzType =
cabling.mezzanine_type;
601 const int chanZero =
cabling.channelId;
602 const int layerZero =
cabling.layer;
603 const int tubeZero =
cabling.tube;
608 if (special_cards.count(mezzType)) {
612 <<
"legacyHedgehogCard() -- Special layout given " << mezzType
618 if (mezzType == 71) {
619 tube_chan = 5 - tube_chan;
640 if (mezzType == 50 || mezzType == 60 || mezzType == 61) {
642 if (layerZero == 1) {
649 if ((tubeZero - 1) % 6 == 0) {
650 tube = tubeZero + tube_chan;
652 tube = tubeZero - tube_chan;
662 else if (mezzType == 71) {
665 tube = tubeZero - tube_chan;
673 return std::make_unique<MdtMezzanineCard>(chMap, 4, mezzType);
682 <<
"to connect " <<
cabling <<
" tubeZero: " << tubeZero <<
endmsg;
689 if (layerZero == 1) {
690 layer =
chan / (card->numTubesPerLayer()) + 1;
692 layer = layerZero -
chan / (card->numTubesPerLayer());
696 uint8_t localchan =
chan % (card->numTubesPerLayer());
700 ((card->id() > 40 && localchan % 2 == 0) ||
701 (card->id() < 40 && localchan < 3)))
705 OfflineCh locTubeLay = card->offlineTube(
706 localchan + (
layer - 1) * card->numTubesPerLayer(),
msg);
707 OfflineCh zeroTubeLay = card->offlineTube(
708 chanZero + (
layer - 1) * card->numTubesPerLayer(),
msg);
709 if (!locTubeLay.isValid || !zeroTubeLay.isValid)
712 tube = (
static_cast<int>(locTubeLay.tube) -
713 static_cast<int>(zeroTubeLay.tube) + tubeZero);
717 <<
" mapped to " <<
layer <<
", " <<
tube
718 <<
" locTube: " <<
static_cast<int>(locTubeLay.tube)
719 <<
" zeroTubeLay: " <<
static_cast<int>(zeroTubeLay.tube)
727 std::unique_ptr<MdtMezzanineCard> to_ret =
728 std::make_unique<MdtMezzanineCard>(chMap, card->numTubeLayers(),
737 if (!tube_lay.isValid)
739 uint8_t tubeNumber = tube_lay.tube + tubeZeroOff + 1;
740 chMap[
chan] = card->tubeNumber(tube_lay.layer, tubeNumber);
745 return std::make_unique<MdtMezzanineCard>(chMap, card->numTubeLayers(),