File indexing completed on 2024-04-06 11:59:50
0001 #include "CalibTracker/SiStripDCS/interface/SiStripPsuDetIdMap.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/ServiceRegistry/interface/Service.h"
0007
0008 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
0009 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0010 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0011 #include <cstdlib>
0012 #include <iostream>
0013 #include <iomanip>
0014 #include <sstream>
0015 #include <string>
0016
0017 using namespace sistrip;
0018
0019
0020 SiStripPsuDetIdMap::SiStripPsuDetIdMap() {
0021 LogTrace("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] Constructing ...";
0022 }
0023
0024 SiStripPsuDetIdMap::~SiStripPsuDetIdMap() {
0025 LogTrace("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] Destructing ...";
0026 }
0027
0028
0029 void SiStripPsuDetIdMap::BuildMap(const std::string& mapFile, const bool debug) {
0030 BuildMap(mapFile, debug, LVMap, HVMap, HVUnmapped_Map, HVCrosstalking_Map);
0031 }
0032
0033 void SiStripPsuDetIdMap::BuildMap(const std::string& mapFile, std::vector<std::pair<uint32_t, std::string> >& rawmap) {
0034
0035
0036
0037
0038
0039 edm::FileInPath file(mapFile.c_str());
0040 std::ifstream ifs(file.fullPath().c_str());
0041 string line;
0042 while (getline(ifs, line)) {
0043 if (!line.empty()) {
0044
0045 stringstream ss(line);
0046 string PSUChannel;
0047 uint32_t detId;
0048 ss >> detId;
0049 ss >> PSUChannel;
0050 rawmap.push_back(std::make_pair(detId, PSUChannel));
0051 }
0052 }
0053 }
0054
0055
0056 void SiStripPsuDetIdMap::BuildMap(
0057 const std::string& mapFile,
0058 const bool debug,
0059 PsuDetIdMap& LVmap,
0060 PsuDetIdMap& HVmap,
0061 PsuDetIdMap& HVUnmappedmap,
0062 PsuDetIdMap& HVCrosstalkingmap)
0063 {
0064
0065
0066
0067
0068
0069
0070
0071
0072 edm::FileInPath file(mapFile.c_str());
0073 std::ifstream ifs(file.fullPath().c_str());
0074 string line;
0075 while (getline(ifs, line)) {
0076 if (!line.empty()) {
0077
0078 stringstream ss(line);
0079 string PSUChannel;
0080 uint32_t detId;
0081 ss >> detId;
0082 ss >> PSUChannel;
0083
0084
0085
0086 std::string PSU = PSUChannel.substr(0, PSUChannel.size() - 10);
0087 std::string Channel = PSUChannel.substr(PSUChannel.size() - 10);
0088 LVmap[PSU].push_back(detId);
0089 if (Channel == "channel000") {
0090 HVUnmappedmap[PSU].push_back(
0091 detId);
0092 } else if (Channel == "channel999") {
0093 HVCrosstalkingmap[PSU].push_back(
0094 detId);
0095 } else {
0096 HVmap[PSUChannel].push_back(detId);
0097 }
0098 }
0099 }
0100
0101
0102 for (PsuDetIdMap::iterator psu = LVMap.begin(); psu != LVMap.end(); psu++) {
0103 RemoveDuplicateDetIDs(psu->second);
0104 }
0105 for (PsuDetIdMap::iterator psuchan = HVMap.begin(); psuchan != HVMap.end(); psuchan++) {
0106 RemoveDuplicateDetIDs(psuchan->second);
0107 }
0108 for (PsuDetIdMap::iterator psu = HVUnmapped_Map.begin(); psu != HVUnmapped_Map.end(); psu++) {
0109 RemoveDuplicateDetIDs(psu->second);
0110 }
0111 for (PsuDetIdMap::iterator psu = HVCrosstalking_Map.begin(); psu != HVCrosstalking_Map.end(); psu++) {
0112 RemoveDuplicateDetIDs(psu->second);
0113 }
0114 if (debug) {
0115
0116 std::cout << "Dumping the LV map" << std::endl;
0117 std::cout << "PSU->detids" << std::endl;
0118 for (PsuDetIdMap::iterator psu = LVMap.begin(); psu != LVMap.end(); psu++) {
0119 std::cout << psu->first << " corresponds to following detids" << endl;
0120 for (unsigned int i = 0; i < psu->second.size(); i++) {
0121 std::cout << "\t\t" << psu->second[i] << std::endl;
0122 }
0123 }
0124 std::cout << "Dumping the HV map for HV mapped channels" << std::endl;
0125 std::cout << "PSUChannel->detids" << std::endl;
0126 for (PsuDetIdMap::iterator psuchan = HVMap.begin(); psuchan != HVMap.end(); psuchan++) {
0127 std::cout << psuchan->first << " corresponds to following detids" << endl;
0128 for (unsigned int i = 0; i < psuchan->second.size(); i++) {
0129 std::cout << "\t\t" << psuchan->second[i] << std::endl;
0130 }
0131 }
0132 std::cout << "Dumping the HV map for HV UNmapped channels" << std::endl;
0133 std::cout << "PSU->detids" << std::endl;
0134 for (PsuDetIdMap::iterator psu = HVUnmapped_Map.begin(); psu != HVUnmapped_Map.end(); psu++) {
0135 std::cout << psu->first << " corresponds to following detids" << endl;
0136 for (unsigned int i = 0; i < psu->second.size(); i++) {
0137 std::cout << "\t\t" << psu->second[i] << std::endl;
0138 }
0139 }
0140 std::cout << "Dumping the HV map for HV Crosstalking channels" << std::endl;
0141 std::cout << "PSU->detids" << std::endl;
0142 for (PsuDetIdMap::iterator psu = HVCrosstalking_Map.begin(); psu != HVCrosstalking_Map.end(); psu++) {
0143 std::cout << psu->first << " corresponds to following detids" << endl;
0144 for (unsigned int i = 0; i < psu->second.size(); i++) {
0145 std::cout << "\t\t" << psu->second[i] << std::endl;
0146 }
0147 }
0148
0149
0150 }
0151 }
0152
0153 void SiStripPsuDetIdMap::RemoveDuplicateDetIDs(std::vector<uint32_t>& detids) {
0154
0155 if (!detids.empty()) {
0156 std::sort(detids.begin(), detids.end());
0157 std::vector<uint32_t>::iterator it = std::unique(detids.begin(), detids.end());
0158 detids.resize(it - detids.begin());
0159 }
0160 }
0161
0162 std::vector<uint32_t> SiStripPsuDetIdMap::getLvDetID(std::string PSU) {
0163
0164
0165 if (LVMap.find(PSU) != LVMap.end()) {
0166 return LVMap[PSU];
0167 } else {
0168 std::vector<uint32_t> detids;
0169 return detids;
0170 }
0171 }
0172
0173 void SiStripPsuDetIdMap::getHvDetID(std::string PSUChannel,
0174 std::vector<uint32_t>& ids,
0175 std::vector<uint32_t>& unmapped_ids,
0176 std::vector<uint32_t>& crosstalking_ids) {
0177
0178 if (HVMap.find(PSUChannel) != HVMap.end()) {
0179 ids = HVMap[PSUChannel];
0180 }
0181
0182 std::string PSU = PSUChannel.substr(0, PSUChannel.size() - 10);
0183 if (HVUnmapped_Map.find(PSU) != HVUnmapped_Map.end()) {
0184 unmapped_ids = HVUnmapped_Map[PSU];
0185 }
0186 if (HVCrosstalking_Map.find(PSU) != HVCrosstalking_Map.end()) {
0187 crosstalking_ids = HVCrosstalking_Map[PSU];
0188 }
0189 }
0190
0191
0192
0193
0194
0195 void SiStripPsuDetIdMap::getDetID(std::string PSUChannel,
0196 const bool debug,
0197 std::vector<uint32_t>& detids,
0198 std::vector<uint32_t>& unmapped_detids,
0199 std::vector<uint32_t>& crosstalking_detids) {
0200
0201
0202
0203
0204
0205
0206
0207 const std::string& PSUChannelFromQuery = PSUChannel;
0208
0209
0210 std::string ChannelFromQuery = PSUChannelFromQuery.substr(PSUChannelFromQuery.size() - 10);
0211
0212 std::string PSUFromQuery = PSUChannelFromQuery.substr(0, PSUChannelFromQuery.size() - 10);
0213 if (debug) {
0214
0215
0216 std::cout << "DPNAME from QUERY: " << PSUChannelFromQuery << ", Channel: " << ChannelFromQuery
0217 << "PSU: " << PSUFromQuery << std::endl;
0218 }
0219
0220
0221
0222
0223 if (ChannelFromQuery == "channel000" or ChannelFromQuery == "channel001") {
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238 if (LVMap.find(PSUFromQuery) != LVMap.end()) {
0239 detids = LVMap[PSUFromQuery];
0240 }
0241 }
0242
0243 else if (ChannelFromQuery == "channel002" or ChannelFromQuery == "channel003") {
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275 if (HVMap.find(PSUChannelFromQuery) != HVMap.end()) {
0276 detids = HVMap[PSUChannelFromQuery];
0277 } else if (HVUnmapped_Map.find(PSUFromQuery) != HVUnmapped_Map.end()) {
0278 unmapped_detids = HVUnmapped_Map[PSUFromQuery];
0279 } else if (HVCrosstalking_Map.find(PSUFromQuery) != HVCrosstalking_Map.end()) {
0280 crosstalking_detids = HVCrosstalking_Map[PSUFromQuery];
0281 }
0282 }
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347 }
0348
0349
0350 std::string SiStripPsuDetIdMap::getPSUName(uint32_t detid) {
0351 std::vector<std::pair<uint32_t, std::string> >::iterator iter;
0352 for (iter = pgMap.begin(); iter != pgMap.end(); iter++) {
0353 if (iter->first && iter->first == detid) {
0354 return iter->second;
0355 }
0356 }
0357
0358 return "UNKNOWN";
0359 }
0360
0361 std::string SiStripPsuDetIdMap::getPSUName(uint32_t detid, std::string group) {
0362 std::vector<std::pair<uint32_t, std::string> >::iterator iter;
0363 if (group == "PG") {
0364 for (iter = pgMap.begin(); iter != pgMap.end(); iter++) {
0365 if (iter->first && iter->first == detid) {
0366 return iter->second;
0367 }
0368 }
0369 }
0370 if (group == "CG") {
0371 for (iter = cgMap.begin(); iter != cgMap.end(); iter++) {
0372 if (iter->first && iter->first == detid) {
0373 return iter->second;
0374 }
0375 }
0376 }
0377
0378 return "UNKNOWN";
0379 }
0380
0381
0382 std::string SiStripPsuDetIdMap::getDetectorLocation(uint32_t detid) {
0383 for (unsigned int i = 0; i < pgMap.size(); i++) {
0384 if (pgMap[i].first == detid) {
0385 return detectorLocations[i];
0386 }
0387 }
0388 return "UNKNOWN";
0389 }
0390
0391
0392 std::string SiStripPsuDetIdMap::getDetectorLocation(uint32_t detid, std::string group) {
0393 if (group == "PG") {
0394 for (unsigned int i = 0; i < pgMap.size(); i++) {
0395 if (pgMap[i].first == detid) {
0396 return detectorLocations[i];
0397 }
0398 }
0399 }
0400 if (group == "CG") {
0401 for (unsigned int i = 0; i < cgMap.size(); i++) {
0402 if (cgMap[i].first == detid) {
0403 return controlLocations[i];
0404 }
0405 }
0406 }
0407 return "UNKNOWN";
0408 }
0409
0410
0411 std::string SiStripPsuDetIdMap::getDetectorLocation(std::string PSUChannel) {
0412 for (unsigned int i = 0; i < pgMap.size(); i++) {
0413 if (pgMap[i].second == PSUChannel) {
0414 return detectorLocations[i];
0415 }
0416 }
0417 for (unsigned int i = 0; i < cgMap.size(); i++) {
0418 if (cgMap[i].second == PSUChannel) {
0419 return controlLocations[i];
0420 }
0421 }
0422 return "UNKNOWN";
0423 }
0424
0425
0426 uint32_t SiStripPsuDetIdMap::getDcuId(std::string PSUChannel) {
0427 for (unsigned int i = 0; i < pgMap.size(); i++) {
0428 if (pgMap[i].second == PSUChannel) {
0429 return dcuIds[i];
0430 }
0431 }
0432 for (unsigned int i = 0; i < cgMap.size(); i++) {
0433 if (cgMap[i].second == PSUChannel) {
0434 return cgDcuIds[i];
0435 }
0436 }
0437 return 0;
0438 }
0439
0440 uint32_t SiStripPsuDetIdMap::getDcuId(uint32_t detid) {
0441 for (unsigned int i = 0; i < pgMap.size(); i++) {
0442 if (pgMap[i].first == detid) {
0443 return dcuIds[i];
0444 }
0445 }
0446 return 0;
0447 }
0448
0449
0450 int SiStripPsuDetIdMap::IsHVChannel(std::string PSUChannel) {
0451
0452 int isHV = 0;
0453 std::string::size_type loc = PSUChannel.find("channel", 0);
0454 if (loc != std::string::npos) {
0455 std::string chNumber = PSUChannel.substr(loc + 7, 3);
0456 if (chNumber == "002" || chNumber == "003") {
0457 isHV = 1;
0458 } else if (chNumber == "000" || chNumber == "001") {
0459 isHV = 0;
0460 } else {
0461 edm::LogWarning("SiStripPsuDetIdMap")
0462 << "[SiStripPsuDetIdMap::" << __func__ << "] channel number of unexpected format, setting error flag!";
0463 isHV = -1;
0464 }
0465 } else {
0466 edm::LogWarning("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__
0467 << "] channel number not located in PSU channel name, setting error flag!";
0468 isHV = -1;
0469 }
0470 return isHV;
0471 }
0472
0473 void SiStripPsuDetIdMap::clone(DcuPsuVector& input, DcuPsuVector& output) {
0474 output.clear();
0475 for (unsigned int i = 0; i < input.size(); i++) {
0476 output.push_back(new TkDcuPsuMap(*(input[i])));
0477 }
0478 }
0479
0480 void SiStripPsuDetIdMap::printMap() {
0481 stringstream pg;
0482 pg << "Map of power supplies to DET IDs: " << std::endl << "-- PSU name -- -- Det Id --" << std::endl;
0483 for (unsigned int p = 0; p < pgMap.size(); p++) {
0484 pg << pgMap[p].first << " " << pgMap[p].second << std::endl;
0485 }
0486 edm::LogInfo("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] " << pg.str();
0487 }
0488
0489 void SiStripPsuDetIdMap::printControlMap() {
0490 stringstream cg;
0491 cg << "Map of control power supplies to DET IDs: " << std::endl
0492 << "-- PSU name -- -- Det Id --" << std::endl;
0493 for (unsigned int p = 0; p < cgMap.size(); p++) {
0494 cg << cgMap[p].first << " " << cgMap[p].second << std::endl;
0495 }
0496 edm::LogInfo("SiStripPsuDetIdMap") << "[SiStripPsuDetIdMap::" << __func__ << "] " << cg.str();
0497 }
0498
0499 std::vector<std::pair<uint32_t, std::string> > SiStripPsuDetIdMap::getDcuPsuMap() {
0500 if (!pgMap.empty()) {
0501 return pgMap;
0502 }
0503 std::vector<std::pair<uint32_t, std::string> > emptyVec;
0504 return emptyVec;
0505 }
0506
0507 void SiStripPsuDetIdMap::checkMapInputValues(const SiStripConfigDb::DcuDetIdsV& dcuDetIds_,
0508 const DcuPsuVector& dcuPsus_) {
0509 std::cout << "Number of entries in DCU-PSU map: " << dcuPsus_.size() << std::endl;
0510 std::cout << "Number of entries in DCU-DETID map: " << dcuDetIds_.size() << std::endl;
0511 std::cout << std::endl;
0512
0513 std::vector<bool> ddUsed(dcuDetIds_.size(), false);
0514 std::vector<bool> dpUsed(dcuPsus_.size(), false);
0515
0516 for (unsigned int dp = 0; dp < dcuPsus_.size(); dp++) {
0517 for (unsigned int dd = 0; dd < dcuDetIds_.size(); dd++) {
0518 if (dcuPsus_[dp]->getDcuHardId() == dcuDetIds_[dd].second->getDcuHardId()) {
0519 dpUsed[dp] = true;
0520 ddUsed[dd] = true;
0521 }
0522 }
0523 }
0524 unsigned int numDpUsed = 0, numDpNotUsed = 0;
0525 for (unsigned int dp = 0; dp < dpUsed.size(); dp++) {
0526 if (dpUsed[dp]) {
0527 numDpUsed++;
0528 } else {
0529 numDpNotUsed++;
0530 }
0531 }
0532
0533 std::cout << "Number of used DCU-PSU entries: " << numDpUsed << std::endl;
0534 std::cout << "Number of unused DCU-PSU entries: " << numDpNotUsed << std::endl;
0535
0536 unsigned int numDdUsed = 0, numDdNotUsed = 0;
0537 for (unsigned int dd = 0; dd < ddUsed.size(); dd++) {
0538 if (ddUsed[dd]) {
0539 numDdUsed++;
0540 } else {
0541 numDdNotUsed++;
0542 }
0543 }
0544
0545 std::cout << "Number of used DCU-DETID entries: " << numDdUsed << std::endl;
0546 std::cout << "Number of unused DCU-DETID entries: " << numDdNotUsed << std::endl;
0547 std::cout << std::endl;
0548 std::cout << "Size of PSU-DETID map: " << pgMap.size() << std::endl;
0549 std::cout << "Size of detectorLocations: " << detectorLocations.size() << std::endl;
0550 }
0551
0552
0553 std::vector<std::pair<std::vector<uint16_t>, std::vector<uint32_t> > > SiStripPsuDetIdMap::retrieveDcuDeviceAddresses(
0554 std::string partition) {
0555
0556 SiStripDbParams dbParams_ = db_->dbParams();
0557 SiStripDbParams::SiStripPartitions::const_iterator iter;
0558
0559 std::vector<std::pair<uint32_t, SiStripConfigDb::DeviceAddress> > resultVec;
0560
0561 SiStripConfigDb::DeviceDescriptionsV dcuDevices_;
0562 SiStripConfigDb::DeviceType device_ = DCU;
0563
0564 for (iter = dbParams_.partitions().begin(); iter != dbParams_.partitions().end(); ++iter) {
0565 if (partition.empty() || partition == iter->second.partitionName()) {
0566 if (iter->second.partitionName() == SiStripPartition::defaultPartitionName_) {
0567 continue;
0568 }
0569 if (iter->second.dcuVersion().first > 0 && iter->second.fecVersion().first > 0) {
0570 SiStripConfigDb::DeviceDescriptionsRange range =
0571 db_->getDeviceDescriptions(device_, iter->second.partitionName());
0572 if (!range.empty()) {
0573 SiStripConfigDb::DeviceDescriptionsV nextVec(range.begin(), range.end());
0574 for (unsigned int i = 0; i < nextVec.size(); i++) {
0575 dcuDescription* desc = dynamic_cast<dcuDescription*>(nextVec[i]);
0576 resultVec.push_back(std::make_pair(desc->getDcuHardId(), db_->deviceAddress(*(nextVec[i]))));
0577 }
0578 }
0579 }
0580 }
0581 }
0582
0583 std::vector<std::pair<std::vector<uint16_t>, std::vector<uint32_t> > > testVec;
0584 std::vector<std::pair<uint32_t, SiStripConfigDb::DeviceAddress> >::iterator reorg_iter = resultVec.begin();
0585
0586 for (; reorg_iter != resultVec.end(); reorg_iter++) {
0587 std::vector<uint16_t> fecInfo(4, 0);
0588 fecInfo[0] = reorg_iter->second.fecCrate_;
0589 fecInfo[1] = reorg_iter->second.fecSlot_;
0590 fecInfo[2] = reorg_iter->second.fecRing_;
0591 fecInfo[3] = reorg_iter->second.ccuAddr_;
0592 std::vector<uint32_t> dcuids;
0593 std::vector<std::pair<uint32_t, SiStripConfigDb::DeviceAddress> >::iterator jter = reorg_iter;
0594 for (; jter != resultVec.end(); jter++) {
0595 if (reorg_iter->second.fecCrate_ == jter->second.fecCrate_ &&
0596 reorg_iter->second.fecSlot_ == jter->second.fecSlot_ &&
0597 reorg_iter->second.fecRing_ == jter->second.fecRing_ &&
0598 reorg_iter->second.ccuAddr_ == jter->second.ccuAddr_) {
0599 dcuids.push_back(jter->first);
0600 }
0601 }
0602
0603 bool isDup = false;
0604 for (unsigned int i = 0; i < testVec.size(); i++) {
0605 if (fecInfo == testVec[i].first) {
0606 isDup = true;
0607 dcuids.insert(dcuids.end(), (testVec[i].second).begin(), (testVec[i].second).end());
0608 std::sort(dcuids.begin(), dcuids.end());
0609 std::vector<uint32_t>::iterator it = std::unique(dcuids.begin(), dcuids.end());
0610 dcuids.resize(it - dcuids.begin());
0611 testVec[i].second = dcuids;
0612 }
0613 }
0614 if (!isDup) {
0615 std::sort(dcuids.begin(), dcuids.end());
0616 std::vector<uint32_t>::iterator it = std::unique(dcuids.begin(), dcuids.end());
0617 dcuids.resize(it - dcuids.begin());
0618 testVec.push_back(std::make_pair(fecInfo, dcuids));
0619 }
0620 }
0621
0622 return testVec;
0623 }
0624
0625 std::vector<uint32_t> SiStripPsuDetIdMap::findDcuIdFromDeviceAddress(uint32_t dcuid_) {
0626 std::vector<std::pair<std::vector<uint16_t>, std::vector<uint32_t> > >::iterator iter =
0627 dcu_device_addr_vector.begin();
0628 std::vector<std::pair<std::vector<uint16_t>, std::vector<uint32_t> > >::iterator res_iter =
0629 dcu_device_addr_vector.end();
0630 std::vector<uint32_t> pgDcu;
0631
0632 for (; iter != dcu_device_addr_vector.end(); iter++) {
0633 std::vector<uint32_t> dcuids = iter->second;
0634 std::vector<uint32_t>::iterator dcu_iter = std::find(dcuids.begin(), dcuids.end(), dcuid_);
0635 bool alreadyFound = false;
0636 if (res_iter != dcu_device_addr_vector.end()) {
0637 alreadyFound = true;
0638 }
0639 if (dcu_iter != dcuids.end()) {
0640 res_iter = iter;
0641 if (!alreadyFound) {
0642 for (unsigned int i = 0; i < dcuids.size(); i++) {
0643 if (dcuids[i] != dcuid_) {
0644 pgDcu.push_back(dcuids[i]);
0645 }
0646 }
0647 } else {
0648 std::cout << "Oh oh ... we have a duplicate :-(" << std::endl;
0649 }
0650 }
0651 }
0652 return pgDcu;
0653 }