22 #include "Identifier/Identifier.h"
33 static const std::string rodFolderName{
"/SCT/DAQ/Configuration/ROD"};
34 static const std::string rodMurFolderName{
"/SCT/DAQ/Configuration/RODMUR"};
35 static const std::string murFolderName{
"/SCT/DAQ/Configuration/MUR"};
36 static const std::string geoFolderName{
"/SCT/DAQ/Configuration/Geog"};
38 static const std::string rodFolderName2{
"/SCT/DAQ/Config/ROD"};
39 static const std::string rodMurFolderName2{
"/SCT/DAQ/Config/RODMUR"};
40 static const std::string murFolderName2{
"/SCT/DAQ/Config/MUR"};
41 static const std::string geoFolderName2{
"/SCT/DAQ/Config/Geog"};
48 static const int disabledFibre{255};
49 static const int defaultLink{128};
53 static const int slotsPerCrate{16};
54 static const int fibresPerMur{12};
55 static const int fibresPerRod{96};
56 static const int mursPerRod{8};
60 static const int HO1[]{5,4,3,2,1,0};
61 static const int HO2[]{12,11,10,9,8,7};
62 static const int HM1[]{6,16,15,14,13,-1};
63 static const int HM2[]{22,21,20,19,18,17};
64 static const int HI1[]{28,30,32,29,31,-1};
65 static const int HI2[]{26,27,25,23,24,-1};
68 static const int qc[]{-1,3,2,1,0};
69 static const int qa[]{-1,2,1,0,3};
76 bool isOdd(
int value) {
77 return ((
value % 2) ==1);
81 int positionNumber(
const unsigned int murPosition,
const unsigned int indx) {
84 case 0:
number = HO1[indx];
break;
85 case 1:
number = HO2[indx];
break;
86 case 2:
number = HM1[indx];
break;
87 case 3:
number = HM2[indx];
break;
88 case 4:
number = HI1[indx];
break;
89 case 5:
number = HI2[indx];
break;
95 bool specialQuadrant(
const unsigned int disk,
const unsigned int quadrant) {
96 return ((disk==8) and (quadrant==4));
100 int reassignSpecialQuadrant(
const int bec,
const unsigned int module) {
113 convertToAthenaCoords(
const int bec,
const unsigned int disk,
const unsigned int quadrant,
const int harnessPosition,
const unsigned int module) {
117 int eta{-1}, internalPhi{-1};
118 const std::pair<int, int> errorValue{std::make_pair(
eta,
phi)};
119 if (specialQuadrant(disk, quadrant)) {
121 if (-1==
q)
return errorValue;
135 internalPhi=
q * 10 +
number -13;
138 internalPhi=
q * 10 +
number -23;
142 phi = (internalPhi +14) % 52;
144 phi = (64-internalPhi) % 52;
146 phi = (49-internalPhi) % 40;
150 phi=(64 - internalPhi) % 52;
152 phi = (52 + 14 +internalPhi) % 52;
154 phi = (10 + 40 +internalPhi) % 40;
156 phi = (internalPhi + 40 + 11) % 40;
159 return std::make_pair(
eta,
phi);
163 validLinkNumber(
const int link) {
164 return ((link>-1) and (link<96)) or (link==defaultLink) or (link==disabledFibre);
180 return StatusCode::FAILURE;
184 return StatusCode::FAILURE;
188 return StatusCode::FAILURE;
192 return StatusCode::FAILURE;
207 return StatusCode::SUCCESS;
213 return StatusCode::SUCCESS;
221 if (writeHandle.isValid()) {
222 ATH_MSG_DEBUG(
"CondHandle " << writeHandle.fullKey() <<
" is already valid."
223 <<
". In theory this should not be called, but may happen"
224 <<
" if multiple concurrent events are being processed out of order.");
225 return StatusCode::SUCCESS;
236 return StatusCode::FAILURE;
241 using IntMap = std::map<int, int>;
247 using S = std::set<int>;
249 for (; rodIt != last_rod; ++rodIt) {
252 slots.insert(
int(rodIt->second[
"slot"].data<
unsigned char>()));
254 slots.insert(
int(rodIt->second[
"slot"].data<
short>()));
258 for (S::const_iterator
i{slots.begin()};
i != slots.end();++
i) {
263 IntMap crateSlot2RobMap;
264 bool allInsertsSucceeded{
true};
267 std::set<int> tempRobSet;
268 for (; rodIt != last_rod; ++rodIt) {
270 int rob{rodAttributes[
"ROB"].data<
int>()};
271 if (not tempRobSet.insert(rob).second)
ATH_MSG_WARNING(
"Duplicate rob? :" << std::hex << rob << std::dec);
272 int crate{
isRun2 ?
static_cast<int>(rodAttributes[
"crate"].data<
unsigned char>()) : (rodAttributes[
"crate"].data<
int>())};
273 int crateSlot{
isRun2 ?
static_cast<int>(rodAttributes[
"slot"].data<
unsigned char>()) :
static_cast<int>(rodAttributes[
"slot"].data<
short>())};
275 IntMap::const_iterator pSlot{slotMap.find(crateSlot)};
276 int slot{(pSlot==slotMap.end()) ? -1 : pSlot->second};
277 if (slot==-1)
ATH_MSG_ERROR(
"Failed to find a crate slot in the crate map");
278 int rodPosition{(crate * slotsPerCrate) +slot};
280 bool thisInsertSucceeded{crateSlot2RobMap.insert(IntMap::value_type(rodPosition, rob)).
second};
281 if (not thisInsertSucceeded) {
282 ATH_MSG_WARNING(
"Insert (rodPosition, rob) " << rodPosition <<
", " << rob <<
" failed.");
283 ATH_MSG_INFO(
"map(rod position) is already " << crateSlot2RobMap[rodPosition]);
284 ATH_MSG_INFO(
"crate, slot, slots per crate: " << crate <<
", " << slot <<
", " << slotsPerCrate);
286 allInsertsSucceeded = thisInsertSucceeded and allInsertsSucceeded;
289 ATH_MSG_INFO(nrods <<
" rods entered, of which " << tempRobSet.size() <<
" are unique.");
291 if (not allInsertsSucceeded)
ATH_MSG_WARNING(
"Some Rod-Rob map inserts failed.");
302 return StatusCode::FAILURE;
306 for (;geoIt != last_geo;++geoIt) {
308 int mur{
isRun2 ?
static_cast<int>(geoAttributes[
"MUR"].data<
unsigned int>()) : (geoAttributes[
"MUR"].data<
int>())};
309 int position{
isRun2 ?
static_cast<int>(geoAttributes[
"position"].data<
short>()) : (geoAttributes[
"position"].data<
int>())};
310 if (mur > 10000) geoMurMap[mur]=position;
317 IntMap murPositionMap;
320 if (pRodMur==
nullptr) {
322 return StatusCode::FAILURE;
326 allInsertsSucceeded =
true;
327 std::set<int> tempRobSet2;
328 for (; rodMurIt!=last_rodMur; ++rodMurIt) {
330 int mur{
isRun2 ?
static_cast<int>(rodMurAttributes[
"MUR"].data<
unsigned int>()) : (rodMurAttributes[
"MUR"].data<
int>())};
331 int crate{
isRun2 ?
static_cast<int>(rodMurAttributes[
"crate"].data<
unsigned char>()) : (rodMurAttributes[
"crate"].data<
int>())};
332 int crateSlot{
isRun2 ?
static_cast<int>(rodMurAttributes[
"rod"].data<
unsigned char>()) : (rodMurAttributes[
"rod"].data<
int>())};
334 IntMap::const_iterator pSlot{slotMap.find(crateSlot)};
335 int slot{(pSlot==slotMap.end()) ? -1 : pSlot->second};
336 if (slot==-1)
ATH_MSG_ERROR(
"Failed to find a crate slot in the crate map");
338 int order{
isRun2 ?
static_cast<int>(rodMurAttributes[
"position"].data<
unsigned char>()) : (rodMurAttributes[
"position"].data<
int>())};
339 int fibreOrder{((((crate * slotsPerCrate) + slot ) * mursPerRod) +
order) * fibresPerMur};
340 bool thisInsertSucceeded{murPositionMap.insert(IntMap::value_type(mur, fibreOrder)).
second};
341 if (not thisInsertSucceeded)
ATH_MSG_WARNING(
"Insert (mur, fibre) " << mur <<
", " << fibreOrder <<
" failed.");
342 allInsertsSucceeded = thisInsertSucceeded and allInsertsSucceeded;
344 if (not allInsertsSucceeded)
ATH_MSG_WARNING(
"Some MUR-position map inserts failed.");
351 return StatusCode::FAILURE;
355 writeHandle.addDependency(readHandleRod);
356 writeHandle.addDependency(readHandleRodMur);
357 writeHandle.addDependency(readHandleMur);
358 writeHandle.addDependency(readHandleGeo);
361 std::unique_ptr<SCT_CablingData> writeCdo{std::make_unique<SCT_CablingData>()};
367 std::set<int> onlineIdSet, robSet;
368 std::set<Identifier> offlineIdSet;
369 long long lastSerialNumber{0};
370 for (; murIt != last_mur; ++murIt) {
372 int mur{
isRun2 ?
static_cast<int>(murAttributes[
"MUR"].data<
unsigned int>()) : (murAttributes[
"MUR"].data<
int>())};
373 bool nullMur{murAttributes[
"moduleID"].isNull() or murAttributes[
"module"].isNull()};
374 if (9999 == mur or nullMur)
continue;
375 int fibreInMur{ (
isRun2 ?
static_cast<int>(murAttributes[
"module"].data<
unsigned char>()) : (murAttributes[
"module"].data<
int>()) ) - 1};
376 long long sn{murAttributes[
"moduleID"].data<
long long>()};
377 if (lastSerialNumber==sn) {
382 IntMap::const_iterator pFibre{murPositionMap.find(mur)};
384 if (pFibre==murPositionMap.end()) {
387 fibreOffset = pFibre->second;
389 int encodedCrateSlot{fibreOffset / (fibresPerMur * mursPerRod)};
391 IntMap::const_iterator pCrate{crateSlot2RobMap.find(encodedCrateSlot)};
392 if (pCrate == crateSlot2RobMap.end()) {
393 ATH_MSG_WARNING(
"Failed to find a crate slot in the cabling, slot " << encodedCrateSlot);
396 tempRobSet2.insert(rob);
401 std::pair<int, int> etaPhiPair;
402 int harnessPosition{-1};
404 layer = (mur/10000) - 1;
406 int quadrant{(mur/100) % 10};
409 harnessPosition=geoMurMap[mur];
410 etaPhiPair= convertToAthenaCoords(
bec,
layer, quadrant, harnessPosition, fibreInMur);
411 eta=etaPhiPair.first;
412 phi=etaPhiPair.second;
415 layer= (mur /1000) - 3;
416 phi= (mur % 100) - 1;
417 eta = (((mur / 100) % 10) *2 -1) * (fibreInMur+1);
421 rxLink[0]=murAttributes[
"rx0Fibre"].data<
unsigned char>();rxLink[1]= murAttributes[
"rx1Fibre"].data<
unsigned char>();
423 rxLink[0]=murAttributes[
"rx0Fibre"].data<
int>();rxLink[1]=murAttributes[
"rx1Fibre"].data<
int>();
425 if (not (validLinkNumber(rxLink[0]) and validLinkNumber(rxLink[1]))) {
426 ATH_MSG_WARNING(
"Invalid link number in database in one of these db entries: rx0Fibre=" << rxLink[0] <<
", rx1Fibre=" << rxLink[1]);
430 bool normalOrdering{
true};
431 if (rxLink[0] != disabledFibre) {
432 normalOrdering=(rxLink[0] % 2)==0;
434 normalOrdering=(rxLink[1] % 2)==1;
437 int possibleLinks[2]{0,0};
438 if (normalOrdering) {
439 possibleLinks[0]=(rxLink[0]!=disabledFibre) ? rxLink[0] : (rxLink[1]-1);
440 possibleLinks[1]=(rxLink[1]!=disabledFibre) ? rxLink[1] : (rxLink[0]+1);
442 possibleLinks[0]=(rxLink[0]!=disabledFibre) ? rxLink[0] : (rxLink[1]+1);
443 possibleLinks[1]=(rxLink[1]!=disabledFibre) ? rxLink[1] : (rxLink[0]-1);
446 if ((-1==
phi) and (-1==
eta)) {
447 ATH_MSG_WARNING(
"Invalid eta, phi..skipping insertion to map for module " << snString <<
" (may be already present in map)");
451 int link{rxLink[
side]};
452 if (defaultLink==link) {
453 link = (fibreOffset % fibresPerRod) + fibreInMur*2 +
side;
455 if (disabledFibre==link) {
456 int otherLink{rxLink[1-
side]};
457 if (otherLink==possibleLinks[0]) link=possibleLinks[1];
458 if (otherLink==possibleLinks[1]) link=possibleLinks[0];
460 bool flippedModule{isOdd(
side)!=isOdd(link)};
462 link = (link==possibleLinks[0]) ? possibleLinks[1] : possibleLinks[0];
464 int onlineId{(rob & 0xFFFFFF)|(link << 24)};
466 if (not onlineIdSet.insert(onlineId).second)
ATH_MSG_WARNING(
"Insert of online Id : " << onlineId <<
" failed.");
467 if (not offlineIdSet.insert(offlineId).second) {
470 ATH_MSG_INFO(
"MUR, position " << mur <<
", " << harnessPosition);
478 if (writeHandle.record(std::move(writeCdo)).isFailure()) {
479 ATH_MSG_FATAL(
"Could not record SCT_CablingData " << writeHandle.key()
480 <<
" with EventRange " << writeHandle.getRange()
481 <<
" into Conditions Store");
482 return StatusCode::FAILURE;
484 ATH_MSG_INFO(
"recorded new CDO " << writeHandle.key() <<
" with range " << writeHandle.getRange() <<
" into Conditions Store");
486 const int robLo{*(tempRobSet2.cbegin())};
487 const int robHi{*(tempRobSet2.crbegin())};
488 ATH_MSG_INFO(numEntries <<
" entries were made to the identifier map.");
489 ATH_MSG_INFO(tempRobSet2.size() <<
" unique rob ids were used, spanning 0x" << std::hex << robLo <<
" to 0x" << robHi << std::dec);
490 if (tempRobSet.size() != tempRobSet2.size()) {
492 std::cout << std::hex;
493 std::set_difference(tempRobSet.cbegin(), tempRobSet.cend(),
494 tempRobSet2.cbegin(), tempRobSet2.cend(),
495 std::ostream_iterator<int>(std::cout,
", "));
496 std::cout << std::dec << std::endl;
499 return (numEntries==0) ? (StatusCode::FAILURE) : (StatusCode::SUCCESS);
509 if (not
hash.is_valid()) {
515 ATH_MSG_FATAL(
"Pointer of derived conditions object is null");
519 if (not
data->setHashForOnlineId(
hash, onlineId))
return false;
520 if (not
data->setOnlineIdForHash(onlineId,
hash))
return false;
522 bool successfulInsert{
data->setHashForSerialNumber(
hash, sn)};
523 successfulInsert &=
data->setSerialNumberForHash(sn,
hash);
525 if (successfulInsert) {