9 #include "GaudiKernel/ISvcLocator.h"
13 #include "Identifier/Identifier.h"
15 #include "GeoModelKernel/throwExcept.h"
22 static const std::set<uint8_t> special_cards{50, 60, 61, 71};
30 ISvcLocator* svcLocator = Gaudi::svcLocator();
31 SmartIF<StoreGateSvc>
detStore{svcLocator->service(
"DetectorStore")};
36 if (
sc != StatusCode::SUCCESS) {
46 bool valid{!check_valid};
72 const int sequence, MsgStream&
log) {
74 if (special_cards.count(
type)) {
77 <<
" breaks the legacy database format. No need to add the "
78 "card if it's hardcoded in C++"
83 int nOfLayers{0}, ntubes{0},
number{sequence};
84 std::array<int, 8> newseq{};
91 log << MSG::ERROR <<
"More than 8 tubes in a layer, not possible !"
97 <<
" to the layer " <<
layer <<
" of mezzanine type " <<
type
101 newseq[ntubes] =
tube;
108 if (ntubes != 8 && ntubes != 6) {
109 log << MSG::ERROR <<
"in type " <<
type
110 <<
": number of tubes per mezzanine layer can be only 6 or 8 ! "
111 "what are you doing ???"
115 nOfLayers = 24 / ntubes;
116 if (
layer > nOfLayers) {
118 <<
"The maximum number of layers for this mezzanine is: "
119 << nOfLayers <<
" so you can't initialize layer: " <<
layer
128 <<
" layers mezzanine - OK, OK..." <<
endmsg;
132 std::array<uint8_t, 8> newLayer{};
133 for (
int i = 0;
i < ntubes; ++
i) {
134 newLayer[
i] = newseq[ntubes -
i - 1];
141 MezzMapping new_map =
143 ? (**itr).tdcToTubeMap()
148 for (
int i = 0;
i < ntubes; ++
i) {
150 const int chStart = ntubes * (
layer - 1);
153 for (
int lay = 1; lay <= nOfLayers; ++lay) {
154 const int chStart = ntubes * (lay - 1);
155 new_map[chStart +
i] = dummy_card.
tubeNumber(lay, newLayer[
i]);
162 log << MSG::ERROR <<
"The mezzanine type " <<
type
163 <<
"has been already initialized" <<
endmsg;
166 (*itr) = std::make_unique<MdtMezzanineCard>(new_map, nOfLayers,
type);
171 std::make_unique<MdtMezzanineCard>(new_map, nOfLayers,
type));
180 MezzCardList::const_iterator
it = std::find_if(
195 <<
"Mezzanine Type: " <<
static_cast<int>(map_data.
mezzanine_type)
196 <<
" not found in the list !" <<
endmsg;
201 auto newTdc = std::make_unique<MdtTdcMap>(mezzaType, map_data);
206 offModule.
cards.emplace(newTdc.get());
207 if (!offModule.
csm[0]) {
208 offModule.
csm[0] = map_data;
217 if (!secondModule.csm[0]){
218 secondModule.csm[0] = map_data;
219 }
else if (secondModule.csm[0] != map_data && !secondModule.
csm[1]) {
220 secondModule.
csm[1] = map_data;
221 std::swap(secondModule.csm[1], secondModule.csm[0]);
224 }
else if (offModule.
csm[0] != map_data) {
225 if (!offModule.
csm[1]) {
226 offModule.
csm[1] = map_data;
230 }
else if (offModule.
csm[1] != map_data) {
231 log << MSG::ERROR <<
"The mulit layer " << map_data
232 <<
" has already associated the CSMs " << std::endl
233 <<
" *** " << offModule.
csm[0] << std::endl
234 <<
" *** " << offModule.
csm[1] << std::endl
235 <<
", while this one is a third one and not supported"
241 if (attachedTdcs.size() <= map_data.
tdcId)
242 attachedTdcs.resize(map_data.
tdcId + 1);
244 m_tdcs.push_back(std::move(newTdc));
253 MsgStream&
log)
const {
254 OnlToOffMap::const_iterator module_itr =
m_toOfflineConv.find(cabling_map);
257 <<
"Could not find a cabling module to recieve offline Id for "
264 if (!module_itr->second.zero_module) {
265 log << MSG::WARNING <<
" No tdc with channel zero found for "
266 << module_itr->first <<
endmsg;
269 if (!module_itr->second.zero_module->offlineId(cabling_map,
log)) {
270 log << MSG::WARNING <<
"MdtTdMap::getOfflineId() -- Channel: "
271 <<
static_cast<unsigned>(cabling_map.
channelId)
272 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId)
278 const TdcOnlSet& attachedTdcs = module_itr->second.all_modules;
279 if (attachedTdcs.size() <= cabling_map.
tdcId) {
280 log << MSG::WARNING <<
"getOfflineId() -- Tdc: "
281 <<
static_cast<unsigned>(cabling_map.
tdcId)
282 <<
" is not part of " << module_itr->first <<
". Maximally "
283 << attachedTdcs.size() <<
" Tdcs were attached. " <<
endmsg;
288 log << MSG::WARNING <<
"getOfflineId() -- Tdc: "
289 <<
static_cast<unsigned>(cabling_map.
tdcId) <<
" not found in "
294 log << MSG::WARNING <<
"MdtTdMap::getOfflineId() -- channel: "
295 <<
static_cast<unsigned>(cabling_map.
channelId)
296 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId)
304 <<
"Channel: " <<
static_cast<unsigned>(cabling_map.
channelId)
305 <<
" Tdc: " <<
static_cast<unsigned>(cabling_map.
tdcId) <<
" "
310 <<
" layer: " << cabling_map.
layer <<
" tube: " << cabling_map.
tube
318 MsgStream&
log)
const {
319 OffToOnlMap::const_iterator module_itr =
m_toOnlineConv.find(cabling_map);
322 <<
"getOnlineId() --- Could not find a cabling CSM set recieve "
327 const TdcOffSet& attachedTdcs = module_itr->second.cards;
328 TdcOffSet::const_iterator tdc_itr = attachedTdcs.find(cabling_map);
329 if (tdc_itr == attachedTdcs.end()) {
330 log << MSG::WARNING <<
"No matching Tdc channel was found for "
332 }
else if ((*tdc_itr)->onlineId(cabling_map,
log))
335 TdcOffSet::const_iterator control_itr =
336 std::find_if(attachedTdcs.begin(), attachedTdcs.end(),
338 return tdc->onlineId(cabling_map, log);
340 if (control_itr == attachedTdcs.end()) {
342 <<
"Second trial to find a valid cabling channel for "
343 << cabling_map <<
" failed as well. " <<
endmsg;
363 log << MSG::ERROR <<
"Could not find hashId for station: " << map_data
368 log << MSG::ERROR <<
"Could not find hashId for multi layer: " << map_data <<
endmsg;
377 <<
" and subdetector+rod ID: " << hardId <<
endmsg;
390 const bool robInitialized =
393 if (!robInitialized) {
403 MsgStream&
log)
const {
405 OffToOnlMap::const_iterator off_itr =
m_toOnlineConv.find(map_data);
407 log << MSG::ERROR <<
"csmNumOnChamber() -- Nothing is saved under "
411 return (1 * (off_itr->second.csm[0] == map_data)) +
412 (2 * (off_itr->second.csm[1] == map_data));
416 MsgStream&
log)
const {
421 <<
"getstationCode() -- Could not find HashId for module: "
434 MsgStream&
log)
const {
438 <<
"getMultiLayerCode() -- Could not determine the detector layer "
444 map_data.
phi, ml, 1, 1);
450 <<
"getMultiLayerCode() -- Could not find HashId for module: "
460 <<
"getMultiLayerCode() -- Could not find HashId for module: "
468 MsgStream&
log)
const {
473 log << MSG::WARNING <<
"ROB ID " << stationCode <<
" not found !" <<
endmsg;
479 const std::vector<IdentifierHash>& mdtHashVector, MsgStream&
log)
const {
480 std::vector<uint32_t> robVector;
482 for (
unsigned int i = 0;
i < mdtHashVector.size(); ++
i) {
486 <<
"ROB id not found for Hash Id: " << mdtHashVector[
i]
492 robVector.push_back(robId);
505 return Rob_it->second;
508 log << MSG::WARNING <<
"Rod ID not found !" <<
endmsg;
509 static const std::vector<IdentifierHash> emptyIdHashVec{};
510 return emptyIdHashVec;
514 const std::vector<uint32_t>& ROBId_list, MsgStream&
log)
const {
515 std::vector<IdentifierHash> HashVec;
516 for (
unsigned int i = 0;
i < ROBId_list.size(); ++
i) {
517 ROBToChamberMap::const_iterator Rob_it =
520 log << MSG::WARNING <<
"Rod ID " << ROBId_list[
i]
521 <<
" not found, continuing with the rest of the ROBId"
525 HashVec.insert(HashVec.end(), Rob_it->second.begin(),
526 Rob_it->second.end());
537 log << MSG::ERROR <<
"No tdc maps were loaded " <<
endmsg;
541 if (!
card->checkConsistency(
log))
545 const unsigned int offToOnlChan =
547 [](
unsigned int N,
const auto& map) {
548 return N + map.second.cards.size();
552 [](
unsigned int N,
const auto& map) {
553 return N + std::accumulate(map.second.begin(), map.second.end(), 0,
554 [](unsigned int M, const auto& tdc) {
559 if (offToOnlChan != onlToOffChan || onlToOffChan != m_tdcs.size()) {
561 <<
"Offline <-> online conversion channels were lost. Expect "
563 <<
" cabling channels. Counted Offline -> online channels "
564 << offToOnlChan <<
". Conted Online -> offline channels "
565 << onlToOffChan <<
endmsg;
568 for (
auto& toOff : m_toOfflineConv) {
569 MdtTdcModule&
mod = toOff.second;
570 TdcOnlSet::const_iterator itr = std::find_if(
572 return sorter && sorter->tdcZero() == 0;
574 if (itr ==
mod.end()) {
575 log << MSG::ERROR <<
"There is no tdc with channel 0 in "
577 <<
". That is important to decode the station names later"
581 mod.zero_module = (*itr);
584 log <<
MSG::INFO <<
"MdtCabling successfully loaded. Found in total "
585 << m_tdcs.size() <<
" channels." <<
endmsg;
589 std::unique_ptr<MdtMezzanineCard>
card, MsgStream&
log) {
590 if (!
card->checkConsistency(
log))
592 MezzCardList::const_iterator itr =
595 return existing->id() == card->id();
598 log << MSG::ERROR <<
"Mezzanine card " << std::endl
599 << (*card) <<
" has already been added " << std::endl
600 << (**itr) << std::endl
601 <<
" please check. " <<
endmsg;
608 MsgStream&
msg)
const {
612 const int mezzType =
cabling.mezzanine_type;
613 const int chanZero =
cabling.channelId;
614 const int layerZero =
cabling.layer;
615 const int tubeZero =
cabling.tube;
620 if (special_cards.count(mezzType)) {
624 <<
"legacyHedgehogCard() -- Special layout given " << mezzType
630 if (mezzType == 71) {
631 tube_chan = 5 - tube_chan;
652 if (mezzType == 50 || mezzType == 60 || mezzType == 61) {
654 if (layerZero == 1) {
661 if ((tubeZero - 1) % 6 == 0) {
662 tube = tubeZero + tube_chan;
664 tube = tubeZero - tube_chan;
674 else if (mezzType == 71) {
677 tube = tubeZero - tube_chan;
685 return std::make_unique<MdtMezzanineCard>(chMap, 4, mezzType);
694 <<
"to connect " <<
cabling <<
" tubeZero: " << tubeZero <<
endmsg;
701 if (layerZero == 1) {
712 ((
card->id() > 40 && localchan % 2 == 0) ||
713 (
card->id() < 40 && localchan < 3)))
717 OfflineCh locTubeLay =
card->offlineTube(
718 localchan + (
layer - 1) *
card->numTubesPerLayer(),
msg);
719 OfflineCh zeroTubeLay =
card->offlineTube(
720 chanZero + (
layer - 1) *
card->numTubesPerLayer(),
msg);
721 if (!locTubeLay.isValid || !zeroTubeLay.isValid)
724 tube = (
static_cast<int>(locTubeLay.tube) -
725 static_cast<int>(zeroTubeLay.tube) + tubeZero);
729 <<
" mapped to " <<
layer <<
", " <<
tube
730 <<
" locTube: " <<
static_cast<int>(locTubeLay.tube)
731 <<
" zeroTubeLay: " <<
static_cast<int>(zeroTubeLay.tube)
739 std::unique_ptr<MdtMezzanineCard> to_ret =
740 std::make_unique<MdtMezzanineCard>(chMap,
card->numTubeLayers(),
749 if (!tube_lay.isValid)
751 uint8_t tubeNumber = tube_lay.tube + tubeZeroOff + 1;
752 chMap[
chan] =
card->tubeNumber(tube_lay.layer, tubeNumber);
757 return std::make_unique<MdtMezzanineCard>(chMap,
card->numTubeLayers(),