File indexing completed on 2024-09-07 04:37:16
0001
0002 #include "OnlineDB/SiStripESSources/interface/SiStripFedCablingBuilderFromDb.h"
0003 #include "CalibFormats/SiStripObjects/interface/SiStripFecCabling.h"
0004 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0005 #include "CondFormats/SiStripObjects/interface/FedChannelConnection.h"
0006 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0007 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ServiceRegistry/interface/Service.h"
0010 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
0011 #include <cstdlib>
0012 #include <iostream>
0013 #include <sstream>
0014 #include <iomanip>
0015
0016 using namespace std;
0017 using namespace sistrip;
0018
0019
0020
0021 SiStripFedCablingBuilderFromDb::SiStripFedCablingBuilderFromDb(const edm::ParameterSet& pset)
0022 : SiStripFedCablingESProducer(pset), db_(nullptr), source_(sistrip::UNDEFINED_CABLING_SOURCE) {
0023 findingRecord<SiStripFedCablingRcd>();
0024
0025 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0026 << " Constructing object...";
0027
0028
0029 string source = pset.getUntrackedParameter<string>("CablingSource", "UNDEFINED");
0030 source_ = SiStripEnumsAndStrings::cablingSource(source);
0031
0032 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0033 << " CablingSource configurable set to \"" << source << "\""
0034 << ". CablingSource member data set to: \"" << SiStripEnumsAndStrings::cablingSource(source_)
0035 << "\"";
0036 }
0037
0038
0039
0040 SiStripFedCablingBuilderFromDb::~SiStripFedCablingBuilderFromDb() {
0041 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0042 << " Destructing object...";
0043 }
0044
0045
0046
0047 SiStripFedCabling* SiStripFedCablingBuilderFromDb::make(const SiStripFedCablingRcd&) {
0048 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0049 << " Constructing FED cabling...";
0050
0051
0052 SiStripFedCabling* fed_cabling = new SiStripFedCabling();
0053
0054
0055 db_ = edm::Service<SiStripConfigDb>().operator->();
0056
0057
0058 if (db_) {
0059 edm::LogVerbatim(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0060 << " Pointer to SiStripConfigDb: 0x" << std::setw(8) << std::setfill('0') << std::hex
0061 << db_ << std::dec;
0062 } else {
0063 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0064 << " NULL pointer to SiStripConfigDb returned by DB \"service\"!"
0065 << " Cannot build FED cabling object!";
0066 return fed_cabling;
0067 }
0068
0069
0070 if (db_->deviceFactory() || db_->databaseCache()) {
0071
0072 SiStripFecCabling fec_cabling;
0073 buildFecCabling(db_, fec_cabling, source_);
0074
0075
0076 getFedCabling(fec_cabling, *fed_cabling);
0077
0078
0079 writeFedCablingToCondDb(*fed_cabling);
0080
0081
0082
0083
0084
0085
0086
0087
0088 } else {
0089 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0090 << " NULL pointers to DeviceFactory and DatabaseCache returned by SiStripConfigDb!"
0091 << " Cannot build FED cabling object!";
0092 }
0093
0094 return fed_cabling;
0095 }
0096
0097
0098
0099 void SiStripFedCablingBuilderFromDb::buildFecCabling(SiStripConfigDb* const db,
0100 SiStripFecCabling& fec_cabling,
0101 const sistrip::CablingSource& source) {
0102 if (source == sistrip::CABLING_FROM_CONNS) {
0103 buildFecCablingFromFedConnections(db, fec_cabling);
0104 } else if (source == sistrip::CABLING_FROM_DEVICES) {
0105 buildFecCablingFromDevices(db, fec_cabling);
0106 } else if (source == sistrip::CABLING_FROM_DETIDS) {
0107 buildFecCablingFromDetIds(db, fec_cabling);
0108 } else if (source == sistrip::UNDEFINED_CABLING_SOURCE) {
0109 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0110 << " Unexpected value for CablingSource: \"" << SiStripEnumsAndStrings::cablingSource(source)
0111 << "\" Querying DB in order to build cabling from one of connections, devices or DetIds...";
0112 buildFecCabling(db, fec_cabling);
0113 return;
0114
0115 } else {
0116 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0117 << " Cannot build SiStripFecCabling object!"
0118 << " sistrip::CablingSource has value: " << SiStripEnumsAndStrings::cablingSource(source);
0119 return;
0120 }
0121
0122
0123 const NumberOfDevices& devs = fec_cabling.countDevices();
0124 std::stringstream ss;
0125 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0126 << " Built SiStripFecCabling object with following devices:" << endl
0127 << devs;
0128 edm::LogVerbatim(mlCabling_) << ss.str() << endl;
0129 }
0130
0131
0132
0133 void SiStripFedCablingBuilderFromDb::buildFecCabling(SiStripConfigDb* const db, SiStripFecCabling& fec_cabling) {
0134 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0135 << " Building cabling object...";
0136
0137 if (!db->getFedConnections().empty()) {
0138 buildFecCablingFromFedConnections(db, fec_cabling);
0139 } else if (!db->getDeviceDescriptions().empty()) {
0140 buildFecCablingFromDevices(db, fec_cabling);
0141 } else if (!db->getDcuDetIds().empty()) {
0142 buildFecCablingFromDetIds(db, fec_cabling);
0143 } else {
0144 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0145 << " Cannot build SiStripFecCabling object!"
0146 << " FedConnections, DeviceDescriptions and DcuDetIds vectors are all empty!";
0147 return;
0148 }
0149
0150
0151 const NumberOfDevices& devices = fec_cabling.countDevices();
0152 std::stringstream ss;
0153 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0154 << " Built SiStripFecCabling object with following devices:" << std::endl
0155 << devices;
0156 edm::LogVerbatim(mlCabling_) << ss.str() << endl;
0157 }
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 void SiStripFedCablingBuilderFromDb::buildFecCablingFromFedConnections(SiStripConfigDb* const db,
0196 SiStripFecCabling& fec_cabling) {
0197 edm::LogVerbatim(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0198 << " Building FEC cabling from FED connections descriptions...";
0199
0200
0201
0202
0203
0204
0205
0206 SiStripConfigDb::FedConnectionsRange conns = db->getFedConnections();
0207 if (conns.empty()) {
0208 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0209 << " Unable to build FEC cabling!"
0210 << " No entries in FedConnections vector!";
0211 return;
0212 }
0213
0214
0215
0216 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0217 << " Retrieving DCU-DetId vector from database...";
0218 SiStripConfigDb::DcuDetIdsRange range = db->getDcuDetIds();
0219 const SiStripConfigDb::DcuDetIdsV dcu_detid_vector(range.begin(), range.end());
0220 if (!dcu_detid_vector.empty()) {
0221 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0222 << " Found " << dcu_detid_vector.size()
0223 << " entries in DCU-DetId vector retrieved from database!";
0224 } else {
0225 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0226 << " No entries in DCU-DetId vector retrieved from database!";
0227 }
0228
0229
0230
0231 SiStripConfigDb::FedConnectionsV::const_iterator ifed = conns.begin();
0232 SiStripConfigDb::FedConnectionsV::const_iterator jfed = conns.end();
0233 for (; ifed != jfed; ++ifed) {
0234 if (!(*ifed)) {
0235 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0236 << " NULL pointer to FedConnection!";
0237 continue;
0238 }
0239
0240
0241 uint16_t fec_crate =
0242 static_cast<uint16_t>((*ifed)->getFecCrateId() + sistrip::FEC_CRATE_OFFSET);
0243 uint16_t fec_slot = static_cast<uint16_t>((*ifed)->getFecSlot());
0244 uint16_t fec_ring =
0245 static_cast<uint16_t>((*ifed)->getRingSlot() + sistrip::FEC_RING_OFFSET);
0246 uint16_t ccu_addr = static_cast<uint16_t>((*ifed)->getCcuAddress());
0247 uint16_t ccu_chan = static_cast<uint16_t>((*ifed)->getI2cChannel());
0248 uint16_t apv0 = static_cast<uint16_t>((*ifed)->getApvAddress());
0249 uint16_t apv1 = apv0 + 1;
0250 uint32_t dcu_id = static_cast<uint32_t>((*ifed)->getDcuHardId());
0251 uint32_t det_id = 0;
0252 uint16_t npairs = 0;
0253 uint16_t fed_id = static_cast<uint16_t>((*ifed)->getFedId());
0254 uint16_t fed_ch = static_cast<uint16_t>((*ifed)->getFedChannel());
0255 uint16_t length = 0;
0256
0257 FedChannelConnection conn(
0258 fec_crate, fec_slot, fec_ring, ccu_addr, ccu_chan, apv0, apv1, dcu_id, det_id, npairs, fed_id, fed_ch, length);
0259
0260 uint16_t fed_crate = sistrip::invalid_;
0261 uint16_t fed_slot = sistrip::invalid_;
0262 fed_crate = static_cast<uint16_t>((*ifed)->getFedCrateId());
0263 fed_slot = static_cast<uint16_t>((*ifed)->getFedSlot());
0264 conn.fedCrate(fed_crate);
0265 conn.fedSlot(fed_slot);
0266
0267 fec_cabling.addDevices(conn);
0268 }
0269
0270
0271
0272 assignDcuAndDetIds(fec_cabling, dcu_detid_vector);
0273 }
0274
0275
0276
0277
0278
0279
0280
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 void SiStripFedCablingBuilderFromDb::buildFecCablingFromDevices(SiStripConfigDb* const db,
0319 SiStripFecCabling& fec_cabling) {
0320 edm::LogVerbatim(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0321 << " Building FEC cabling object from device descriptions...";
0322
0323
0324
0325
0326
0327
0328
0329 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0330 << " Retrieving APV descriptions from database...";
0331 SiStripConfigDb::DeviceDescriptionsRange apv_desc = db->getDeviceDescriptions(APV25);
0332 if (!apv_desc.empty()) {
0333 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0334 << " Retrieved " << apv_desc.size() << " APV descriptions from database!";
0335 } else {
0336 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0337 << " Unable to build FEC cabling!"
0338 << " No APV descriptions found!";
0339 return;
0340 }
0341
0342
0343
0344 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0345 << " Retrieving DCU descriptions from database...";
0346 SiStripConfigDb::DeviceDescriptionsRange dcu_desc = db->getDeviceDescriptions(DCU);
0347
0348 if (!dcu_desc.empty()) {
0349 uint16_t feh = 0;
0350 uint16_t ccu = 0;
0351 SiStripConfigDb::DeviceDescriptionsV::const_iterator idcu;
0352 for (idcu = dcu_desc.begin(); idcu != dcu_desc.end(); idcu++) {
0353 dcuDescription* dcu = dynamic_cast<dcuDescription*>(*idcu);
0354 if (!dcu) {
0355 continue;
0356 }
0357 if (dcu->getDcuType() == "FEH") {
0358 feh++;
0359 } else {
0360 ccu++;
0361 }
0362 }
0363
0364 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0365 << " Retrieved " << feh << " DCU-FEH descriptions from database!"
0366 << " (and a further " << ccu << " DCUs for CCU modules, etc...)";
0367
0368 } else {
0369 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0370 << " No DCU descriptions found!";
0371 }
0372
0373
0374
0375 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0376 << " Retrieving DCU-DetId vector from database...";
0377 SiStripConfigDb::DcuDetIdsRange range = db->getDcuDetIds();
0378 const SiStripConfigDb::DcuDetIdsV dcu_detid_vector(range.begin(), range.end());
0379 if (!dcu_detid_vector.empty()) {
0380 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0381 << " Found " << dcu_detid_vector.size()
0382 << " entries in DCU-DetId vector retrieved from database!";
0383 } else {
0384 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0385 << " No entries in DCU-DetId vector retrieved from database!";
0386 }
0387
0388
0389
0390 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0391 << " Retrieving FED ids from database...";
0392 SiStripConfigDb::FedIdsRange fed_ids = db->getFedIds();
0393
0394 if (!fed_ids.empty()) {
0395 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0396 << " Retrieved " << fed_ids.size() << " FED ids from database!";
0397 } else {
0398 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0399 << " No FED ids found!";
0400 }
0401
0402
0403
0404 edm::LogVerbatim(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0405 << " Building FEC cabling object from APV and DCU descriptions...";
0406
0407 SiStripConfigDb::DeviceDescriptionsV::const_iterator iapv;
0408 for (iapv = apv_desc.begin(); iapv != apv_desc.end(); iapv++) {
0409 if (!(*iapv)) {
0410 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0411 << " NULL pointer to DeviceDescription (of type APV25)!";
0412 continue;
0413 }
0414
0415 SiStripConfigDb::DeviceAddress addr = db->deviceAddress(**iapv);
0416 FedChannelConnection conn(addr.fecCrate_ + sistrip::FEC_CRATE_OFFSET,
0417 addr.fecSlot_,
0418 addr.fecRing_ + sistrip::FEC_RING_OFFSET,
0419 addr.ccuAddr_,
0420 addr.ccuChan_,
0421 addr.i2cAddr_);
0422 fec_cabling.addDevices(conn);
0423 }
0424
0425 SiStripConfigDb::DeviceDescriptionsV::const_iterator idcu;
0426 for (idcu = dcu_desc.begin(); idcu != dcu_desc.end(); idcu++) {
0427 if (!(*idcu)) {
0428 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0429 << " NULL pointer to DeviceDescription (of type DCU)!";
0430 continue;
0431 }
0432
0433 SiStripConfigDb::DeviceAddress addr = db->deviceAddress(**idcu);
0434 dcuDescription* dcu = dynamic_cast<dcuDescription*>(*idcu);
0435 if (!dcu) {
0436 continue;
0437 }
0438 if (dcu->getDcuType() != "FEH") {
0439 continue;
0440 }
0441 FedChannelConnection conn(addr.fecCrate_ + sistrip::FEC_CRATE_OFFSET,
0442 addr.fecSlot_,
0443 addr.fecRing_ + sistrip::FEC_RING_OFFSET,
0444 addr.ccuAddr_,
0445 addr.ccuChan_,
0446 0,
0447 0,
0448 dcu->getDcuHardId());
0449 fec_cabling.dcuId(conn);
0450 }
0451
0452 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0453 << " Finished building FEC cabling object from APV and DCU descriptions!";
0454
0455 NumberOfDevices devs1 = fec_cabling.countDevices();
0456 std::stringstream ss1;
0457 ss1 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0458 << " Number of devices in FEC cabling object:" << std::endl;
0459 devs1.print(ss1);
0460 LogTrace(mlCabling_) << ss1.str();
0461
0462
0463
0464 std::vector<uint16_t>::const_iterator ifed = fed_ids.begin();
0465 uint16_t fed_ch = 0;
0466
0467
0468
0469 std::vector<uint32_t> used_keys;
0470
0471 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0472 << " Randomly assigning FED ids/channels to APV pairs in front-end modules...";
0473
0474 if (fed_ids.empty()) {
0475 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0476 << " No FED ids retrieved from database! Unable to cable system!";
0477 } else {
0478 bool complete = false;
0479 std::vector<SiStripFecCrate>::const_iterator icrate = fec_cabling.crates().begin();
0480 std::vector<SiStripFecCrate>::const_iterator jcrate = fec_cabling.crates().end();
0481 while (!complete && icrate != jcrate) {
0482 std::vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin();
0483 std::vector<SiStripFec>::const_iterator jfec = icrate->fecs().end();
0484 while (!complete && ifec != jfec) {
0485 std::vector<SiStripRing>::const_iterator iring = ifec->rings().begin();
0486 std::vector<SiStripRing>::const_iterator jring = ifec->rings().end();
0487 while (!complete && iring != jring) {
0488 std::vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin();
0489 std::vector<SiStripCcu>::const_iterator jccu = iring->ccus().end();
0490 while (!complete && iccu != jccu) {
0491 std::vector<SiStripModule>::const_iterator imod = iccu->modules().begin();
0492 std::vector<SiStripModule>::const_iterator jmod = iccu->modules().end();
0493 while (!complete && imod != jmod) {
0494
0495 const_cast<SiStripModule&>(*imod).nApvPairs(0);
0496
0497 used_keys.push_back(
0498 SiStripFecKey(imod->fecCrate(), imod->fecSlot(), imod->fecRing(), imod->ccuAddr(), imod->ccuChan())
0499 .key());
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516 for (uint16_t ipair = 0; ipair < imod->nApvPairs(); ipair++) {
0517
0518 if (ifed == fed_ids.end()) {
0519 fed_ch++;
0520 ifed = fed_ids.begin();
0521 }
0522 if (fed_ch == 96) {
0523 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0524 << " Insufficient FED channels to cable all devices in control system!";
0525 complete = true;
0526 break;
0527 }
0528
0529
0530 pair<uint16_t, uint16_t> addr = imod->activeApvPair(imod->lldChannel(ipair));
0531 SiStripModule::FedChannel fed_channel((*ifed) / 16 + 1,
0532 (*ifed) % 16 + 2,
0533 *ifed,
0534 fed_ch);
0535 const_cast<SiStripModule&>(*imod).fedCh(addr.first, fed_channel);
0536 ifed++;
0537 }
0538
0539 imod++;
0540 }
0541 iccu++;
0542 }
0543 iring++;
0544 }
0545 ifec++;
0546 }
0547 icrate++;
0548 }
0549 }
0550
0551 std::sort(used_keys.begin(), used_keys.end());
0552
0553 NumberOfDevices devs2 = fec_cabling.countDevices();
0554 std::stringstream ss2;
0555 ss2 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0556 << " Number of devices in FEC cabling object:" << std::endl;
0557 devs2.print(ss2);
0558 LogTrace(mlCabling_) << ss2.str();
0559
0560 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0561 << " Finished randomly assigning FED ids/channels to APV pairs in front-end modules...";
0562
0563
0564
0565 LogTrace(mlCabling_)
0566 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0567 << " Assigning APV pairs in dummy front-end modules to any remaining \"uncabled\" FED ids/channels...";
0568
0569 if (fed_ids.empty()) {
0570 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0571 << " No FED ids retrieved from database! Unable to cable system!";
0572 } else {
0573 uint16_t module = 0;
0574 bool complete = false;
0575 while (!complete) {
0576 for (uint16_t lld = sistrip::LLD_CHAN_MIN; lld < sistrip::LLD_CHAN_MAX + 1; lld++) {
0577
0578 if (ifed == fed_ids.end()) {
0579 fed_ch++;
0580 ifed = fed_ids.begin();
0581 }
0582 if (fed_ch == 96) {
0583 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0584 << " All FED channels are now cabled!";
0585 complete = true;
0586 break;
0587 }
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598 FedChannelConnection temp(fecCrate(module),
0599 fecSlot(module),
0600 fecRing(module),
0601 ccuAddr(module),
0602 ccuChan(module),
0603 SiStripFecKey::i2cAddr(lld, true),
0604 SiStripFecKey::i2cAddr(lld, false),
0605 sistrip::invalid32_,
0606 sistrip::invalid32_,
0607 3,
0608 *ifed,
0609 fed_ch);
0610 uint16_t fed_crate = (*ifed) / 16 + 1;
0611 uint16_t fed_slot = (*ifed) % 16 + 2;
0612 temp.fedCrate(fed_crate);
0613 temp.fedSlot(fed_slot);
0614 fec_cabling.addDevices(temp);
0615 ifed++;
0616 }
0617 module++;
0618 }
0619 }
0620
0621 LogTrace(mlCabling_)
0622 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0623 << " Finished assigning APV pairs in dummy front-end modules to any remaining \"uncabled\" FED ids/channels...";
0624
0625 NumberOfDevices devs3 = fec_cabling.countDevices();
0626 std::stringstream ss3;
0627 ss3 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0628 << " Number of devices in FEC cabling object:" << std::endl;
0629 devs3.print(ss3);
0630 LogTrace(mlCabling_) << ss3.str();
0631
0632
0633
0634 assignDcuAndDetIds(fec_cabling, dcu_detid_vector);
0635 }
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675 void SiStripFedCablingBuilderFromDb::buildFecCablingFromDetIds(SiStripConfigDb* const db,
0676 SiStripFecCabling& fec_cabling) {
0677 edm::LogVerbatim(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0678 << " Building FEC cabling object from DetIds...";
0679
0680
0681
0682
0683
0684
0685 uint32_t chans_per_ccu = 10;
0686 uint32_t ccus_per_ring = 10;
0687 uint32_t rings_per_fec = 8;
0688 uint32_t fecs_per_crate = 11;
0689
0690
0691
0692 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0693 << " Retrieving DCU-DetId vector from database...";
0694 SiStripConfigDb::DcuDetIdsRange range = db->getDcuDetIds();
0695 const SiStripConfigDb::DcuDetIdsV dcu_detid_vector(range.begin(), range.end());
0696 if (!dcu_detid_vector.empty()) {
0697 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0698 << " Found " << dcu_detid_vector.size()
0699 << " entries in DCU-DetId vector retrieved from database!";
0700 } else {
0701 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0702 << " No entries in DCU-DetId vector retrieved from database!"
0703 << " Unable to build FEC cabling!";
0704 return;
0705 }
0706
0707
0708
0709 uint32_t imodule = 0;
0710 SiStripConfigDb::DcuDetIdsV::const_iterator iter;
0711 for (iter = dcu_detid_vector.begin(); iter != dcu_detid_vector.end(); iter++) {
0712 if (!(iter->second)) {
0713 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0714 << " NULL pointer to TkDcuInfo!";
0715 continue;
0716 }
0717
0718 uint16_t fec_crate = (imodule / (chans_per_ccu * ccus_per_ring * rings_per_fec * fecs_per_crate)) + 1;
0719 uint16_t fec_slot = (imodule / (chans_per_ccu * ccus_per_ring * rings_per_fec)) % fecs_per_crate + 2;
0720 uint16_t fec_ring = (imodule / (chans_per_ccu * ccus_per_ring)) % rings_per_fec + 1;
0721 uint16_t ccu_addr = (imodule / (chans_per_ccu)) % ccus_per_ring + 1;
0722 uint16_t ccu_chan = (imodule) % chans_per_ccu + 16;
0723
0724 uint32_t dcu_id = iter->second->getDcuHardId();
0725 uint32_t det_id = iter->second->getDetId();
0726 uint16_t npairs = iter->second->getApvNumber() / 2;
0727 uint16_t length = (uint16_t)iter->second->getFibreLength();
0728
0729
0730
0731 if (!dcu_id) {
0732 dcu_id = SiStripFecKey(fec_crate, fec_slot, fec_ring, ccu_addr, ccu_chan).key();
0733 }
0734 if (!det_id) {
0735 det_id = 0xFFFF + imodule;
0736 }
0737 if (!npairs) {
0738 npairs = rand() / 2 ? 2 : 3;
0739 }
0740
0741
0742
0743 for (uint16_t ipair = 0; ipair < npairs; ipair++) {
0744 uint16_t iapv = (ipair == 1 && npairs == 2 ? 36 : 32 + 2 * ipair);
0745 FedChannelConnection conn(fec_crate,
0746 fec_slot,
0747 fec_ring,
0748 ccu_addr,
0749 ccu_chan,
0750 iapv,
0751 iapv + 1,
0752 dcu_id,
0753 det_id,
0754 npairs,
0755 0,
0756 0,
0757 length,
0758 true,
0759 true,
0760 true,
0761 true);
0762 fec_cabling.addDevices(conn);
0763 }
0764
0765 imodule++;
0766 }
0767
0768
0769
0770 uint32_t fed_id = 50;
0771 uint32_t fed_ch = 0;
0772 for (vector<SiStripFecCrate>::const_iterator icrate = fec_cabling.crates().begin();
0773 icrate != fec_cabling.crates().end();
0774 icrate++) {
0775 for (vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin(); ifec != icrate->fecs().end(); ifec++) {
0776 for (vector<SiStripRing>::const_iterator iring = ifec->rings().begin(); iring != ifec->rings().end(); iring++) {
0777 for (vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin(); iccu != iring->ccus().end(); iccu++) {
0778 for (vector<SiStripModule>::const_iterator imod = iccu->modules().begin(); imod != iccu->modules().end();
0779 imod++) {
0780 if (96 - fed_ch < imod->nApvPairs()) {
0781 fed_id++;
0782 fed_ch = 0;
0783 }
0784 for (uint16_t ipair = 0; ipair < imod->nApvPairs(); ipair++) {
0785 pair<uint16_t, uint16_t> addr = imod->activeApvPair((*imod).lldChannel(ipair));
0786 SiStripModule::FedChannel fed_channel(
0787 (fed_id - 50) / 16 + 1,
0788 (fed_id - 50) % 16 + 2,
0789 fed_id,
0790 fed_ch);
0791 const_cast<SiStripModule&>(*imod).fedCh(addr.first, fed_channel);
0792 fed_ch++;
0793 }
0794 }
0795 }
0796 }
0797 }
0798 }
0799 }
0800
0801
0802
0803 void SiStripFedCablingBuilderFromDb::assignDcuAndDetIds(SiStripFecCabling& fec_cabling,
0804 const std::vector<std::pair<uint32_t, TkDcuInfo*> >& _in) {
0805 std::vector<std::pair<uint32_t, TkDcuInfo*> > in = _in;
0806
0807
0808 if (in.empty()) {
0809 edm::LogError(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0810 << " No entries in DCU-DetId vector!";
0811 }
0812
0813
0814
0815 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0816 << " Assigning DCU ids and DetIds to constructed modules...";
0817
0818 uint16_t channels = 0;
0819 uint16_t six = 0;
0820 uint16_t four = 0;
0821 uint16_t unknown = 0;
0822 uint16_t missing = 0;
0823
0824 for (vector<SiStripFecCrate>::const_iterator icrate = fec_cabling.crates().begin();
0825 icrate != fec_cabling.crates().end();
0826 icrate++) {
0827 for (vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin(); ifec != icrate->fecs().end(); ifec++) {
0828 for (vector<SiStripRing>::const_iterator iring = ifec->rings().begin(); iring != ifec->rings().end(); iring++) {
0829 for (vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin(); iccu != iring->ccus().end(); iccu++) {
0830 for (vector<SiStripModule>::const_iterator imod = iccu->modules().begin(); imod != iccu->modules().end();
0831 imod++) {
0832 SiStripModule& module = const_cast<SiStripModule&>(*imod);
0833
0834
0835 module.nApvPairs(0);
0836
0837
0838
0839 if (!module.dcuId()) {
0840 SiStripFecKey path(
0841 icrate->fecCrate(), ifec->fecSlot(), iring->fecRing(), iccu->ccuAddr(), imod->ccuChan());
0842 uint32_t module_key = path.key();
0843 module.dcuId(module_key);
0844 stringstream ss;
0845 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0846 << " Found NULL DcuId! Setting 'dummy' value based control key 0x" << hex << setw(8) << setfill('0')
0847 << module_key << dec;
0848 edm::LogWarning(mlCabling_) << ss.str();
0849 }
0850
0851
0852
0853 if (!module.detId()) {
0854
0855
0856 SiStripConfigDb::DcuDetIdsV::iterator iter = in.end();
0857 iter = SiStripConfigDb::findDcuDetId(in.begin(), in.end(), module.dcuId());
0858 if (iter != in.end()) {
0859 if (!(iter->second)) {
0860 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0861 << " NULL pointer to TkDcuInfo!";
0862 continue;
0863 }
0864
0865
0866
0867 module.detId(iter->second->getDetId());
0868 module.nApvPairs(0);
0869
0870
0871 uint16_t pairs = iter->second->getApvNumber() / 2;
0872 channels += pairs;
0873 if (pairs == 2) {
0874 four++;
0875 } else if (pairs == 3) {
0876 six++;
0877 } else {
0878 unknown++;
0879 }
0880
0881
0882
0883 if (module.nApvPairs() != 2 && module.nApvPairs() != 3) {
0884 missing += (iter->second->getApvNumber() / 2 - module.nApvPairs());
0885 stringstream ss1;
0886 ss1 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]" << std::endl
0887 << " Module with DCU id 0x" << hex << setw(8) << setfill('0') << module.dcuId() << dec
0888 << " and DetId 0x" << hex << setw(8) << setfill('0') << module.detId() << dec
0889 << " has unexpected number of APV pairs (" << module.nApvPairs() << ")." << std::endl
0890 << " Some APV pairs may have not been detected by the FEC scan." << std::endl
0891 << " Setting to value found in static map (" << iter->second->getApvNumber() / 2 << ")...";
0892 edm::LogWarning(mlCabling_) << ss1.str();
0893 module.nApvPairs(iter->second->getApvNumber() / 2);
0894
0895 } else if (module.nApvPairs() < iter->second->getApvNumber() / 2) {
0896 missing += (iter->second->getApvNumber() / 2 - module.nApvPairs());
0897 stringstream ss2;
0898 ss2 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]" << std::endl
0899 << " Module with DCU id 0x" << hex << setw(8) << setfill('0') << module.dcuId() << dec
0900 << " and DetId 0x" << hex << setw(8) << setfill('0') << module.detId() << dec
0901 << " has number of APV pairs (" << module.nApvPairs()
0902 << ") that does not match value found in DCU-DetId vector (" << iter->second->getApvNumber() / 2
0903 << ")." << std::endl
0904 << " Some APV pairs may have not been detected by"
0905 << " the FEC scan or the DCU-DetId vector may be incorrect." << std::endl
0906 << " Setting to value found in static map (" << iter->second->getApvNumber() / 2 << ")...";
0907 edm::LogWarning(mlCabling_) << ss2.str();
0908 module.nApvPairs(iter->second->getApvNumber() / 2);
0909 }
0910
0911
0912
0913 if (!module.length()) {
0914 module.length(static_cast<uint16_t>(iter->second->getFibreLength()));
0915 }
0916
0917
0918
0919 in.erase(iter);
0920
0921 }
0922 }
0923
0924 }
0925 }
0926 }
0927 }
0928 }
0929
0930 std::stringstream sss;
0931 sss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]" << std::endl
0932 << " Connections in DCU-DetId map : " << channels << std::endl
0933 << " 4-APV modules : " << four << std::endl
0934 << " 6-APV modules : " << six << std::endl
0935 << " Unknown number of APV pairs : " << unknown << std::endl
0936 << " Total found APV pairs : " << (channels - missing) << std::endl
0937 << " Total missing APV pairs : " << missing << std::endl;
0938 edm::LogVerbatim(mlCabling_) << sss.str();
0939
0940 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0941 << " Finished assigning DCU ids and DetIds to constructed modules...";
0942
0943
0944
0945 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0946 << " Assigning \"random\" DetIds to modules with DCU ids not found in static table...";
0947
0948 uint32_t detid = 0x10000;
0949 for (vector<SiStripFecCrate>::const_iterator icrate = fec_cabling.crates().begin();
0950 icrate != fec_cabling.crates().end();
0951 icrate++) {
0952 for (vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin(); ifec != icrate->fecs().end(); ifec++) {
0953 for (vector<SiStripRing>::const_iterator iring = ifec->rings().begin(); iring != ifec->rings().end(); iring++) {
0954 for (vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin(); iccu != iring->ccus().end(); iccu++) {
0955 for (vector<SiStripModule>::const_iterator imod = iccu->modules().begin(); imod != iccu->modules().end();
0956 imod++) {
0957 SiStripModule& module = const_cast<SiStripModule&>(*imod);
0958
0959
0960
0961 if (!module.detId()) {
0962 SiStripConfigDb::DcuDetIdsV::iterator iter = in.end();
0963 iter = SiStripConfigDb::findDcuDetId(in.begin(), in.end(), module.dcuId());
0964 if (iter != in.end()) {
0965
0966
0967 SiStripConfigDb::DcuDetIdsV::iterator idcu;
0968 if (in.empty()) {
0969 idcu = in.end();
0970 } else {
0971 idcu = in.begin();
0972 while (idcu != in.end()) {
0973 if (idcu->second) {
0974 if (static_cast<uint32_t>(idcu->second->getApvNumber()) ==
0975 static_cast<uint32_t>(2 * module.nApvPairs())) {
0976 break;
0977 }
0978 }
0979 idcu++;
0980 }
0981 }
0982
0983
0984
0985 if (idcu != in.end()) {
0986 if (!(idcu->second)) {
0987 edm::LogWarning(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0988 << " NULL pointer to TkDcuInfo!";
0989 continue;
0990 }
0991
0992 module.detId(idcu->second->getDetId());
0993 in.erase(idcu);
0994
0995 stringstream ss;
0996 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
0997 << " Did not find module with DCU id 0x" << hex << setw(8) << setfill('0') << module.dcuId() << dec
0998 << " in DCU-DetId vector!" << endl
0999 << " Assigned 'random' DetId 0x" << hex << setw(8) << setfill('0') << module.detId() << dec;
1000 edm::LogWarning(mlCabling_) << ss.str();
1001
1002 } else {
1003
1004
1005 module.detId(detid);
1006 detid++;
1007
1008 stringstream ss;
1009 if (in.empty()) {
1010 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
1011 << " Did not find module with DCU id 0x" << hex << setw(8) << setfill('0') << module.dcuId()
1012 << dec << " in DCU-DetId vector!"
1013 << " Could not assign 'random' DetId as DCU-DetID map is empty!"
1014 << " Assigned DetId based on incremented counter, with value 0x" << hex << setw(8)
1015 << setfill('0') << module.detId() << dec;
1016 } else {
1017 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
1018 << " Did not find module with DCU id 0x" << hex << setw(8) << setfill('0') << module.dcuId()
1019 << dec << " in DCU-DetId vector!"
1020 << " Could not assign 'random' DetId as no modules had appropriate number of APV pairs ("
1021 << module.nApvPairs() << "). Assigned DetId based on incremented counter, with value 0x" << hex
1022 << setw(8) << setfill('0') << module.detId() << dec;
1023 }
1024 edm::LogWarning(mlCabling_) << ss.str();
1025 }
1026 }
1027 }
1028
1029 }
1030 }
1031 }
1032 }
1033 }
1034
1035 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
1036 << " Finished assigning \"random\" DetIds to modules with DCU ids not found in static table...";
1037
1038 LogTrace(mlCabling_) << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
1039 << " Assigning \"random\" DetIds to modules with DCU ids not found in static table...";
1040
1041
1042
1043 if (!in.empty()) {
1044 stringstream ss;
1045 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
1046 << " Not all DetIds have been assigned to a DcuId! " << in.size() << " DetIds are unassigned!";
1047 edm::LogWarning(mlCabling_) << ss.str();
1048 }
1049 }
1050
1051
1052
1053 void SiStripFedCablingBuilderFromDb::getFedCabling(const SiStripFecCabling& fec_cabling,
1054 SiStripFedCabling& fed_cabling) {
1055 vector<FedChannelConnection> conns;
1056 fec_cabling.connections(conns);
1057 fed_cabling.buildFedCabling(conns);
1058 }
1059
1060
1061
1062 void SiStripFedCablingBuilderFromDb::getFecCabling(const SiStripFedCabling& fed_cabling,
1063 SiStripFecCabling& fec_cabling) {
1064 fec_cabling.buildFecCabling(fed_cabling);
1065 }
1066
1067
1068
1069 void SiStripFedCablingBuilderFromDb::setIntervalFor(const edm::eventsetup::EventSetupRecordKey& key,
1070 const edm::IOVSyncValue& iov_sync,
1071 edm::ValidityInterval& iov_validity) {
1072 edm::ValidityInterval infinity(iov_sync.beginOfTime(), iov_sync.endOfTime());
1073 iov_validity = infinity;
1074 }