File indexing completed on 2024-04-06 11:58:18
0001
0002
0003
0004
0005
0006 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
0007 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0008 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 #include "FWCore/Utilities/interface/typelookup.h"
0011
0012 #include <iostream>
0013
0014
0015 SiStripDetCabling::SiStripDetCabling(const TrackerTopology *const topology) : fedCabling_(nullptr), tTopo(topology) {}
0016 SiStripDetCabling::~SiStripDetCabling() {}
0017
0018
0019 SiStripDetCabling::SiStripDetCabling(const SiStripFedCabling &fedcabling, const TrackerTopology *const topology)
0020 : fullcabling_(), connected_(), detected_(), undetected_(), fedCabling_(&fedcabling), tTopo(topology) {
0021
0022
0023
0024
0025 auto feds = fedcabling.fedIds();
0026 for (auto ifed = feds.begin(); ifed != feds.end(); ifed++) {
0027
0028 SiStripFedCabling::ConnsConstIterRange conns = fedcabling.fedConnections(*ifed);
0029 for (auto iconn = conns.begin(); iconn != conns.end(); iconn++) {
0030 addDevices(*iconn,
0031 fullcabling_);
0032
0033 bool have_fed_id = iconn->fedId();
0034 std::vector<int> vector_of_connected_apvs;
0035 if (have_fed_id) {
0036
0037 int which_apv_pair = iconn->apvPairNumber();
0038
0039
0040
0041 if (iconn->detId() == 0 || iconn->detId() == sistrip::invalid32_ ||
0042 iconn->apvPairNumber() == sistrip::invalid_ || iconn->nApvPairs() == sistrip::invalid_) {
0043 continue;
0044 }
0045
0046 if (iconn->i2cAddr(0))
0047 vector_of_connected_apvs.push_back(2 * which_apv_pair + 0);
0048 if (iconn->i2cAddr(1))
0049 vector_of_connected_apvs.push_back(2 * which_apv_pair + 1);
0050 }
0051 if (!vector_of_connected_apvs.empty()) {
0052 std::map<uint32_t, std::vector<int>> map_of_connected_apvs;
0053 map_of_connected_apvs.insert(std::make_pair(iconn->detId(), vector_of_connected_apvs));
0054 addFromSpecificConnection(connected_, map_of_connected_apvs, &(connectionCount[0]));
0055 }
0056 }
0057 }
0058
0059 SiStripFedCabling::ConnsConstIterRange detected_fed_connections = fedcabling.detectedDevices();
0060 for (std::vector<FedChannelConnection>::const_iterator idtct = detected_fed_connections.begin();
0061 idtct != detected_fed_connections.end();
0062 ++idtct) {
0063 addDevices(*idtct, fullcabling_);
0064 bool have_fed_id = idtct->fedId();
0065 std::vector<int> vector_of_detected_apvs;
0066 if (!have_fed_id) {
0067 int which_apv_pair = idtct->apvPairNumber();
0068
0069 if (idtct->i2cAddr(0))
0070 vector_of_detected_apvs.push_back(2 * which_apv_pair + 0);
0071 if (idtct->i2cAddr(1))
0072 vector_of_detected_apvs.push_back(2 * which_apv_pair + 1);
0073 }
0074 if (!vector_of_detected_apvs.empty()) {
0075 std::map<uint32_t, std::vector<int>> map_of_detected_apvs;
0076 map_of_detected_apvs.insert(std::make_pair(idtct->detId(), vector_of_detected_apvs));
0077 addFromSpecificConnection(detected_, map_of_detected_apvs, &(connectionCount[1]));
0078 }
0079 }
0080
0081 SiStripFedCabling::ConnsConstIterRange undetected_fed_connections = fedcabling.undetectedDevices();
0082 for (std::vector<FedChannelConnection>::const_iterator iudtct = undetected_fed_connections.begin();
0083 iudtct != undetected_fed_connections.end();
0084 ++iudtct) {
0085 addDevices(*iudtct, fullcabling_);
0086 bool have_fed_id = iudtct->fedId();
0087 std::vector<int> vector_of_undetected_apvs;
0088 if (!have_fed_id) {
0089 int which_apv_pair = iudtct->apvPairNumber();
0090
0091 if (iudtct->i2cAddr(0))
0092 vector_of_undetected_apvs.push_back(2 * which_apv_pair + 0);
0093 if (iudtct->i2cAddr(1))
0094 vector_of_undetected_apvs.push_back(2 * which_apv_pair + 1);
0095 }
0096 if (!vector_of_undetected_apvs.empty()) {
0097 std::map<uint32_t, std::vector<int>> map_of_undetected_apvs;
0098 map_of_undetected_apvs.insert(std::make_pair(iudtct->detId(), vector_of_undetected_apvs));
0099 addFromSpecificConnection(undetected_, map_of_undetected_apvs, &(connectionCount[2]));
0100 }
0101 }
0102 }
0103
0104
0105 void SiStripDetCabling::addDevices(const FedChannelConnection &conn,
0106 std::map<uint32_t, std::vector<const FedChannelConnection *>> &conns) {
0107 if (conn.detId() && conn.detId() != sistrip::invalid32_ &&
0108 conn.apvPairNumber() != sistrip::invalid_) {
0109 if (conn.fedId() == 0 || conn.fedId() == sistrip::invalid_) {
0110 edm::LogInfo("") << " SiStripDetCabling::addDevices for connection "
0111 "associated to detid "
0112 << conn.detId() << " apvPairNumber " << conn.apvPairNumber() << "the fedId is " << conn.fedId();
0113 return;
0114 }
0115
0116
0117 if (conn.apvPairNumber() >= conns[conn.detId()].size()) {
0118 conns[conn.detId()].resize(conn.apvPairNumber() + 1);
0119 }
0120
0121 conns[conn.detId()][conn.apvPairNumber()] = &conn;
0122 }
0123 }
0124
0125
0126 void SiStripDetCabling::addDevices(
0127 const FedChannelConnection &conn) {
0128
0129 addDevices(conn, fullcabling_);
0130 }
0131
0132
0133
0134 void SiStripDetCabling::addActiveDetectorsRawIds(std::vector<uint32_t> &vector_to_fill_with_detids) const {
0135 for (std::map<uint32_t, std::vector<int>>::const_iterator conn_it = connected_.begin(); conn_it != connected_.end();
0136 ++conn_it) {
0137 vector_to_fill_with_detids.push_back(conn_it->first);
0138 }
0139
0140 }
0141
0142
0143 void SiStripDetCabling::addAllDetectorsRawIds(std::vector<uint32_t> &vector_to_fill_with_detids) const {
0144 for (std::map<uint32_t, std::vector<int>>::const_iterator conn_it = connected_.begin(); conn_it != connected_.end();
0145 ++conn_it) {
0146 vector_to_fill_with_detids.push_back(conn_it->first);
0147 }
0148 for (std::map<uint32_t, std::vector<int>>::const_iterator conn_it = detected_.begin(); conn_it != detected_.end();
0149 ++conn_it) {
0150 vector_to_fill_with_detids.push_back(conn_it->first);
0151 }
0152 for (std::map<uint32_t, std::vector<int>>::const_iterator conn_it = undetected_.begin(); conn_it != undetected_.end();
0153 ++conn_it) {
0154 vector_to_fill_with_detids.push_back(conn_it->first);
0155 }
0156
0157
0158 }
0159
0160
0161 const std::vector<const FedChannelConnection *> &SiStripDetCabling::getConnections(
0162 uint32_t det_id) const {
0163 std::map<uint32_t, std::vector<const FedChannelConnection *>>::const_iterator detcabl_it =
0164 fullcabling_.find(det_id);
0165
0166 if (!(detcabl_it == fullcabling_.end())) {
0167 return (detcabl_it->second);
0168 } else {
0169
0170 const static std::vector<const FedChannelConnection *> default_empty_fedchannelconnection;
0171 return default_empty_fedchannelconnection;
0172 }
0173 }
0174
0175
0176 const FedChannelConnection &SiStripDetCabling::getConnection(uint32_t det_id, unsigned short apv_pair) const {
0177 const std::vector<const FedChannelConnection *> &fcconns = getConnections(det_id);
0178 for (std::vector<const FedChannelConnection *>::const_iterator iconn = fcconns.begin(); iconn != fcconns.end();
0179 ++iconn) {
0180 if (((*iconn) != nullptr) && (((*iconn)->apvPairNumber()) == apv_pair)) {
0181
0182 return (**iconn);
0183 }
0184 }
0185
0186
0187 const static FedChannelConnection default_empty_fedchannelconnection;
0188 return default_empty_fedchannelconnection;
0189 }
0190
0191
0192 const unsigned int SiStripDetCabling::getDcuId(uint32_t det_id) const {
0193 const std::vector<const FedChannelConnection *> &fcconns = getConnections(det_id);
0194 if (!fcconns.empty()) {
0195
0196
0197 for (size_t i = 0; i < fcconns.size(); ++i)
0198 if (fcconns.at(i) && fcconns.at(i)->detId() != sistrip::invalid32_ && fcconns.at(i)->detId() != 0)
0199 return (fcconns.at(i))->dcuId();
0200
0201 }
0202
0203 unsigned int default_zero_value = 0;
0204 return default_zero_value;
0205 }
0206
0207
0208
0209 const uint16_t SiStripDetCabling::nApvPairs(uint32_t det_id) const {
0210 const std::vector<const FedChannelConnection *> &fcconns = getConnections(det_id);
0211 if (!fcconns.empty()) {
0212
0213
0214 for (size_t i = 0; i < fcconns.size(); ++i) {
0215 if ((fcconns.at(i) != nullptr) && (fcconns.at(i)->nApvPairs() != sistrip::invalid_)) {
0216 return fcconns.at(i)->nApvPairs();
0217 }
0218 }
0219 }
0220
0221
0222
0223 return 0;
0224 }
0225
0226
0227 void SiStripDetCabling::addConnected(std::map<uint32_t, std::vector<int>> &map_to_add_to) const {
0228 addFromSpecificConnection(map_to_add_to, connected_);
0229 }
0230
0231
0232 void SiStripDetCabling::addDetected(std::map<uint32_t, std::vector<int>> &map_to_add_to) const {
0233 addFromSpecificConnection(map_to_add_to, detected_);
0234 }
0235
0236
0237 void SiStripDetCabling::addUnDetected(std::map<uint32_t, std::vector<int>> &map_to_add_to) const {
0238 addFromSpecificConnection(map_to_add_to, undetected_);
0239 }
0240
0241
0242
0243 void SiStripDetCabling::addNotConnectedAPVs(std::map<uint32_t, std::vector<int>> &map_to_add_to) const {
0244 addFromSpecificConnection(map_to_add_to, detected_);
0245 addFromSpecificConnection(map_to_add_to, undetected_);
0246 }
0247
0248
0249 void SiStripDetCabling::addFromSpecificConnection(std::map<uint32_t, std::vector<int>> &map_to_add_to,
0250 const std::map<uint32_t, std::vector<int>> &specific_connection,
0251 std::map<int16_t, uint32_t> *connectionsToFill) const {
0252 for (std::map<uint32_t, std::vector<int>>::const_iterator conn_it = specific_connection.begin();
0253 conn_it != specific_connection.end();
0254 ++conn_it) {
0255 uint32_t new_detid = conn_it->first;
0256 std::vector<int> new_apv_vector = conn_it->second;
0257 std::map<uint32_t, std::vector<int>>::iterator it = map_to_add_to.find(new_detid);
0258 if (it == map_to_add_to.end()) {
0259 std::sort(new_apv_vector.begin(),
0260 new_apv_vector.end());
0261 map_to_add_to.insert(std::make_pair(new_detid, new_apv_vector));
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271 if (connectionsToFill) {
0272 (*connectionsToFill)[layerSearch(new_detid)]++;
0273 }
0274
0275 } else {
0276
0277 std::vector<int> existing_apv_vector = it->second;
0278 for (std::vector<int>::iterator inew = new_apv_vector.begin(); inew != new_apv_vector.end(); ++inew) {
0279 bool there_already = false;
0280 for (std::vector<int>::iterator iold = existing_apv_vector.begin(); iold != existing_apv_vector.end(); ++iold) {
0281 if (*iold == *inew) {
0282 there_already = true;
0283 break;
0284 }
0285 }
0286 if (!there_already) {
0287 existing_apv_vector.push_back(*inew);
0288 std::sort(existing_apv_vector.begin(),
0289 existing_apv_vector.end());
0290 } else {
0291
0292
0293 }
0294 }
0295 }
0296 }
0297 }
0298
0299 int16_t SiStripDetCabling::layerSearch(const uint32_t detId) const {
0300 const DetId detectorId = DetId(detId);
0301 const int subdet = detectorId.subdetId();
0302 if (subdet == StripSubdetector::TIB) {
0303 return tTopo->layer(detId);
0304 } else if (subdet == StripSubdetector::TID) {
0305
0306 return 10 + (tTopo->side(detId) - 1) * 3 + tTopo->layer(detId);
0307 } else if (subdet == StripSubdetector::TOB) {
0308 return 100 + tTopo->layer(detId);
0309 } else if (subdet == StripSubdetector::TEC) {
0310
0311 return 1000 + (tTopo->side(detId) - 1) * 9 + tTopo->layer(detId);
0312 }
0313 return 0;
0314 }
0315
0316
0317
0318 uint32_t SiStripDetCabling::detNumber(const std::string &subDet, const uint16_t layer, const int connectionType) const {
0319 uint16_t subDetLayer = layer;
0320
0321 if (subDet == "TID-")
0322 subDetLayer += 10;
0323 else if (subDet == "TID+")
0324 subDetLayer += 10 + 3;
0325 else if (subDet == "TOB")
0326 subDetLayer += 100;
0327 else if (subDet == "TEC-")
0328 subDetLayer += 1000;
0329 else if (subDet == "TEC+")
0330 subDetLayer += 1000 + 9;
0331 else if (subDet != "TIB") {
0332 LogDebug("SiStripDetCabling") << "Error: Wrong subDet. Please use one of TIB, TID, TOB, TEC." << std::endl;
0333 return 0;
0334 }
0335 auto found = connectionCount[connectionType].find(subDetLayer);
0336 if (found != connectionCount[connectionType].end()) {
0337 return found->second;
0338 }
0339 return 0;
0340 }
0341
0342
0343
0344 void SiStripDetCabling::getAllDetectorsContiguousIds(std::map<uint32_t, unsigned int> &allToContiguous) const {
0345 allToContiguous.clear();
0346 std::vector<uint32_t> all;
0347 addAllDetectorsRawIds(all);
0348 std::sort(all.begin(), all.end());
0349 unsigned int contiguousIndex = 0;
0350 for (std::vector<uint32_t>::const_iterator idet = all.begin(); idet != all.end(); ++idet) {
0351 ++contiguousIndex;
0352 allToContiguous.insert(std::make_pair(*idet, contiguousIndex));
0353 }
0354 }
0355
0356
0357 void SiStripDetCabling::getActiveDetectorsContiguousIds(std::map<uint32_t, unsigned int> &connectedToContiguous) const {
0358 connectedToContiguous.clear();
0359 std::vector<uint32_t> connected;
0360 addAllDetectorsRawIds(connected);
0361 std::sort(connected.begin(),
0362 connected.end());
0363
0364 std::map<uint32_t, unsigned int> allToContiguous;
0365 getAllDetectorsContiguousIds(allToContiguous);
0366 for (std::vector<uint32_t>::const_iterator idet = connected.begin(); idet != connected.end();
0367 ++idet) {
0368 std::map<uint32_t, unsigned int>::iterator deco = allToContiguous.find(*idet);
0369 if (deco != allToContiguous.end()) {
0370 connectedToContiguous.insert(*deco);
0371 }
0372 }
0373 }
0374
0375 bool SiStripDetCabling::IsConnected(const uint32_t &det_id) const { return IsInMap(det_id, connected_); }
0376
0377 bool SiStripDetCabling::IsDetected(const uint32_t &det_id) const { return IsInMap(det_id, detected_); }
0378 bool SiStripDetCabling::IsUndetected(const uint32_t &det_id) const { return IsInMap(det_id, undetected_); }
0379 bool SiStripDetCabling::IsInMap(const uint32_t &det_id, const std::map<uint32_t, std::vector<int>> &map) const {
0380 std::map<uint32_t, std::vector<int>>::const_iterator it = map.find(det_id);
0381 return (it != map.end());
0382 }
0383
0384
0385
0386 void SiStripDetCabling::print(std::stringstream &ss) const {
0387 uint32_t valid = 0;
0388 uint32_t total = 0;
0389 typedef std::vector<const FedChannelConnection *> Conns;
0390 typedef std::map<uint32_t, Conns> ConnsMap;
0391 ConnsMap::const_iterator ii = fullcabling_.begin();
0392 ConnsMap::const_iterator jj = fullcabling_.end();
0393 ss << "[SiStripDetCabling::" << __func__ << "]"
0394 << " Printing DET cabling for " << fullcabling_.size() << " modules " << std::endl;
0395 for (; ii != jj; ++ii) {
0396 ss << "Printing " << ii->second.size() << " connections for DetId: " << ii->first << std::endl;
0397 Conns::const_iterator iii = ii->second.begin();
0398 Conns::const_iterator jjj = ii->second.end();
0399 for (; iii != jjj; ++iii) {
0400 if ((*iii)->isConnected()) {
0401 valid++;
0402 }
0403 total++;
0404 ss << **iii << std::endl;
0405 }
0406 }
0407 ss << "Number of connected: " << valid << std::endl << "Number of connections: " << total << std::endl;
0408 }
0409
0410 void SiStripDetCabling::printSummary(std::stringstream &ss, const TrackerTopology *trackerTopo) const {
0411 for (int connectionType = 0; connectionType < 3; ++connectionType) {
0412 if (connectionType == 0)
0413 ss << "Connected modules:" << std::endl;
0414 else if (connectionType == 1)
0415 ss << "Detected modules:" << std::endl;
0416 else
0417 ss << "Undetected modules:" << std::endl;
0418 ss << "SubDet and layer\t modules" << std::endl;
0419 std::map<int16_t, uint32_t>::const_iterator iter = connectionCount[connectionType].begin();
0420 for (; iter != connectionCount[connectionType].end(); ++iter) {
0421 uint32_t subDetLayer = iter->first;
0422 uint32_t modules = iter->second;
0423 if (int(subDetLayer / 10) == 0) {
0424 ss << "TIB \t layer " << subDetLayer << " \t" << modules << std::endl;
0425 } else if (int(subDetLayer / 100) == 0) {
0426 int layer = subDetLayer % 10;
0427 if (layer <= 3)
0428 ss << "TID- \t disk " << layer << "\t" << modules << std::endl;
0429 else
0430 ss << "TID+ \t disk " << layer - 3 << "\t" << modules << std::endl;
0431 } else if (int(subDetLayer / 1000) == 0) {
0432 int layer = subDetLayer % 100;
0433 ss << "TOB \t layer " << layer << " \t" << modules << std::endl;
0434 } else {
0435 int layer = subDetLayer % 100;
0436 if (layer <= 9)
0437 ss << "TEC- \t disk " << layer << " \t" << modules << std::endl;
0438 else
0439 ss << "TEC+ \t disk " << layer - 9 << " \t" << modules << std::endl;
0440 }
0441 }
0442 }
0443 }
0444
0445 void SiStripDetCabling::printDebug(std::stringstream &ss, const TrackerTopology * ) const { print(ss); }