11#include "CoralBase/Attribute.h"
12#include "CoralBase/AttributeListSpecification.h"
18#include <CoralBase/Blob.h>
28 const bool initCondKey =
m_mezzJSON.value().empty() &&
34 return StatusCode::SUCCESS;
43 <<
". In theory this should not be called, but may happen"
44 <<
" if multiple concurrent events are being processed out of order.");
45 return StatusCode::SUCCESS;
49 std::unique_ptr<MuonMDT_CablingMap> writeCdo{std::make_unique<MuonMDT_CablingMap>()};
53 if (!writeCdo->finalize_init(msgStream()))
return StatusCode::FAILURE;
56 ATH_MSG_INFO(
"recorded new " << writeHandle.
key() <<
" with range " << writeHandle.
getRange() <<
" into Conditions Store");
57 return StatusCode::SUCCESS;
62 itr != readHandle->end(); ++itr) {
63 const coral::AttributeList& atr = itr->second;
65 if (atr[
"data"].specification().
type() ==
typeid(coral::Blob)) {
70 return StatusCode::FAILURE;
73 data = *(
static_cast<const std::string*
>((atr[
"data"]).addressOfData()));
75 json = nlohmann::json::parse(data);
77 return StatusCode::SUCCESS;
85 if (!in_json.good()) {
87 return StatusCode::FAILURE;
89 nlohmann::json payload{};
92 return StatusCode::SUCCESS;
97 return StatusCode::FAILURE;
102 <<
" readCdoMap->size()= " << readHandleMap->size());
104 ATH_MSG_VERBOSE(
"Collection CondAttrListCollection CLID " << readHandleMap->clID());
109 return StatusCode::SUCCESS;
113 for (itrMap = readHandleMap->begin(); itrMap != readHandleMap->end(); ++itrMap) {
114 const coral::AttributeList& atr = itrMap->second;
119 constexpr std::string_view
delimiter{
","};
120 const std::string
map = *(
static_cast<const std::string*
>((atr[
"Map"]).addressOfData()));
127 if (!cabling_map.
addMezzanine(map_data, DataSource::LegacyCOOL, msgStream())) {
129 return StatusCode::FAILURE;
136 ATH_MSG_INFO(
"Found a round 3 setup. Will add the cabling for BIS78 manually");
137 const int BIS_stat =
m_idHelperSvc->mdtIdHelper().stationNameIndex(
"BIS");
138 constexpr int eta_index = 7;
139 const std::string zeroStr{
"0"};
140 const std::string emptyStr{
""};
141 for (
unsigned int sector = 1; sector <= 8; ++sector) {
142 int is_biy{0}, tdcId{0};
143 auto increment_tdc = [&is_biy, &tdcId]() {
145 constexpr int tdcMax = 17;
146 if (tdcId > tdcMax) {
151 constexpr int tubesPerCsm = 54;
152 bool added_tdc =
true;
153 std::vector<std::string> db_coding{std::to_string(tdcId), zeroStr, emptyStr};
156 for (
int ml = 1; ml <= 2; ++ml) {
158 const int tubes_per_layer =
m_idHelperSvc->mdtIdHelper().tubeMax(layer_id);
160 const int first_tube = 6 * (1 + tdcId / 2) + tubesPerCsm * is_biy;
161 const bool valid_tube = (first_tube <= tubes_per_layer);
162 added_tdc |= valid_tube;
171 bis78_data.
eta = eta_index;
172 bis78_data.
phi = sector;
175 bis78_data.
mrod = (is_biy ? -1 : 7) + sector;
176 bis78_data.
csm = is_biy ? 5 : 0;
182 <<
m_idHelperSvc->mdtIdHelper().tubeMax(layer_id) <<
" tdcId: "
183 << tdcId <<
" is_Biy: " << is_biy <<
" first_tube: " << first_tube);
185 db_coding[2] = std::to_string(tube_coding);
189 if (!cabling_map.
addMezzanine(bis78_data, DataSource::LegacyCOOL, msgStream())) {
190 ATH_MSG_ERROR(
"Failed to add mezzanine card " << bis78_data);
191 return StatusCode::FAILURE;
194 if (bis78_data.
tube != first_tube) {
195 ATH_MSG_ERROR(
"BIS78 data tube encoding failed. Expected " << first_tube <<
" got " << bis78_data.
tube <<
".");
196 return StatusCode::FAILURE;
199 return StatusCode::FAILURE;
206 return StatusCode::SUCCESS;
210 map_data.
eta = *(
static_cast<const int*
>((atr[
"Eta"]).addressOfData()));
211 map_data.
phi = *(
static_cast<const int*
>((atr[
"Phi"]).addressOfData()));
212 map_data.
mrod = *(
static_cast<const int*
>((atr[
"MROD"]).addressOfData()));
213 map_data.
csm = *(
static_cast<const int*
>((atr[
"CSM"]).addressOfData()));
214 map_data.
channelId = *(
static_cast<const int*
>((atr[
"Chan"]).addressOfData()));
215 map_data.
mezzanine_type = *(
static_cast<const int*
>((atr[
"Mezzanine_Type"]).addressOfData()));
217 const std::string chamber_name = *(
static_cast<const std::string*
>((atr[
"Chamber_Name"]).addressOfData()));
218 const std::string subdetector_id = *(
static_cast<const std::string*
>((atr[
"SubDet_Id"]).addressOfData()));
221 std::string stationNameString = chamber_name.substr(0, 3);
223 if (stationNameString ==
"BOE") { stationNameString =
"BOL"; }
225 if (stationNameString ==
"BIX" || stationNameString ==
"BIY") {
226 stationNameString =
"BIS";
227 static std::atomic<bool> sInfoPrinted{
false};
229 ATH_MSG_INFO(
"Found an entry concerning the new BIS78 chambers. Will increase the number of tube per layer to "
238 if ((stationNameString ==
"EIS" && (!
m_idHelperSvc->hasCSC() || chamber_name.find(
'A') != std::string::npos)) ||
239 chamber_name.find(
"BIS7A") != std::string::npos) {
240 ATH_MSG_VERBOSE(
"It's sooo sad but the chamber " << chamber_name <<
" is no longer with us");
249 ATH_MSG_VERBOSE(
"Data load is chamber_Name = " << chamber_name <<
" translated to "<<map_data<<
" FINISHED HERE ");
252 static std::atomic<bool> stWarningPrinted{
false};
253 if (!stWarningPrinted) {
255 <<
" which is not reasonable, maybe related to ATLASRECTS-5961, continuing...");
256 stWarningPrinted =
true;
265 constexpr unsigned int coding_block_size = 3;
266 const unsigned int n = info_map.size() >= coding_block_size ? coding_block_size : info_map.size();
267 bool decoded_full_block{
false};
268 for (
unsigned int i = 0; i < n; ++i) {
273 map_data.
tdcId = info;
285 decoded_full_block =
true;
290 if (decoded_full_block) info_map.erase(info_map.begin(), info_map.begin() + coding_block_size);
291 return decoded_full_block;
298 if (!in_json.good()) {
300 return StatusCode::FAILURE;
302 nlohmann::json payload{};
305 return StatusCode::SUCCESS;
310 if (!readHandleMez.
isValid()) {
312 return StatusCode::FAILURE;
317 <<
" readCdoMez->size()= " << readHandleMez->size());
323 return StatusCode::SUCCESS;
326 for (itrMez = readHandleMez->begin(); itrMez != readHandleMez->end(); ++itrMez) {
327 const coral::AttributeList& atr = itrMez->second;
328 int sequence{0}, layer{0}, mezzanine_type{0};
330 mezzanine_type = *(
static_cast<const int*
>((atr[
"Mezzanine_Type"]).addressOfData()));
331 layer = *(
static_cast<const int*
>((atr[
"Layer"]).addressOfData()));
332 sequence = *(
static_cast<const int*
>((atr[
"Sequence"]).addressOfData()));
333 ATH_MSG_VERBOSE(
"Sequence load is " << sequence <<
" for the mezzanine type = " << mezzanine_type
334 <<
" for the layer number = " << layer);
337 if (!cabling_map.
addMezzanineLine(mezzanine_type, layer, sequence, msgStream())) {
338 ATH_MSG_ERROR(
"Could not add the mezzanine sequence to the map ");
339 return StatusCode::FAILURE;
344 return StatusCode::SUCCESS;
348 for (
const auto &cabl_chan : payload.items()) {
349 nlohmann::json mezz_payload = cabl_chan.value();
350 const uint8_t
id = mezz_payload[
"mezzId"];
351 const uint8_t nLay = mezz_payload[
"nTubeLayer"];
352 const MezzMapping mapping = mezz_payload[
"tdcToTubeMap"];
353 if(!cabling_map.
addMezanineLayout(std::make_unique<MdtMezzanineCard>(mapping, nLay,
id),
354 msgStream()))
return StatusCode::FAILURE;
356 return StatusCode::SUCCESS;
360 for (
const auto &db_channel : payload.items()) {
361 nlohmann::json ms_payload = db_channel.value();
364 ms_channel.
eta = ms_payload[
"eta"];
365 ms_channel.
phi = ms_payload[
"phi"];
369 ms_channel.
csm = ms_payload[
"csm"];
370 ms_channel.
mrod = ms_payload[
"mrod"];
373 ms_channel.
tdcId = ms_payload[
"tdcId"];
374 ms_channel.
tube = ms_payload[
"tubeZero"];
376 ms_channel.
layer = 1;
378 ms_channel.
channelId = hedgeHogCard ? (*std::min_element(
381 if (!cabling_map.
addMezzanine(std::move(ms_channel), DataSource::JSON, msgStream()))
return StatusCode::FAILURE;
383 return StatusCode::SUCCESS;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(x,...)
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
MuonMDT_CablingMap::DataSource DataSource
ChanAttrListMap::const_iterator const_iterator
static EventIDRange infiniteRunLB()
Produces an EventIDRange that is infinite in RunLumi and invalid in Time.
static constexpr int maxNTubesPerLayer
The maxNTubesPerLayer represents the absolute maximum of tubes which are built into a single multilay...
const Mapping & tubeToTdcMap() const
Returns the underlying Tube -> Tdc conversion map.
std::shared_ptr< const MdtMezzanineCard > MezzCardPtr
std::array< uint8_t, 24 > Mapping
int stationNameIndex(const std::string &name) const
Gaudi::Property< bool > m_useJSONFormat
StatusCode loadCablingSchema(const EventContext &ctx, SG::WriteCondHandle< MuonMDT_CablingMap > &writeHandle, MuonMDT_CablingMap &cabling_map) const
Load the cabling schema of the tubes.
StatusCode dbPayloadToJson(SG::ReadCondHandle< CondAttrListCollection > &readHandle, nlohmann::json &json) const
StatusCode loadCablingSchemaFromJSON(nlohmann::json &&payload, MuonMDT_CablingMap &cabling_map) const
SG::WriteCondHandleKey< MuonMDT_CablingMap > m_writeKey
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
virtual StatusCode execute(const EventContext &ctx) const override
StatusCode loadMezzanineSchema(const EventContext &ctx, SG::WriteCondHandle< MuonMDT_CablingMap > &writeHandle, MuonMDT_CablingMap &cabling_map) const
Load the mezzanine schema into the cabling.
Gaudi::Property< bool > m_isRun3
bool extractStationInfo(const coral::AttributeList &atr, CablingData &map_data) const
Retrieves the general MDT station info from the coral attribute.
SG::ReadCondHandleKey< CondAttrListCollection > m_readKeyMap
Gaudi::Property< std::string > m_chambJSON
StatusCode loadMezzanineFromJSON(nlohmann::json &&payload, MuonMDT_CablingMap &cabling_map) const
bool extractLayerInfo(std::vector< std::string > &, CablingData &map_data) const
Retrieves the channel info from the coral attribute.
SG::ReadCondHandleKey< CondAttrListCollection > m_readKeyMez
MuonMDT_CablingMap::CablingData CablingData
Gaudi::Property< std::string > m_mezzJSON
virtual StatusCode initialize() override
MezzCardPtr getHedgeHogMapping(uint8_t mezzCardId) const
bool addMezanineLayout(std::unique_ptr< MdtMezzanineCard > card, MsgStream &log)
Adds a new mezzanine card mapping.
bool addMezzanine(CablingData cabling_data, DataSource source, MsgStream &log)
Add a new fully configured mezzanine card.
bool addMezzanineLine(const int type, const int layer, const int sequence, MsgStream &log)
Add a new line describing a mezzanine type.
const DataObjID & fullKey() const
const std::string & key() const
void addDependency(const EventIDRange &range)
const EventIDRange & getRange() const
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
const DataObjID & fullKey() const
static const std::string delimiter("/")
bool readBlobAsString(const coral::Blob &, std::string &)
std::vector< std::string > tokenize(std::string_view the_str, std::string_view delimiters)
Splits the string into smaller substrings.
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
uint8_t tdcId
Mezzanine type.
int tubes_per_layer
Helper constants to extract tube,layer, multilayer information.
uint8_t mezzanine_type
Tube number in the layer.
int tube
Layer inside the multilayer.
uint8_t channelId
Identifier of the corresponding tdc.
int8_t & multilayer
Phi sector of the MDT station.
int8_t & eta
Station of the chamber (i.e, BIL,BIS,etc.).
int8_t & phi
Eta of the MDT station.
uint8_t & subdetectorId
CSM number.
uint8_t & csm
MROD number.