File indexing completed on 2024-09-07 04:35:05
0001 #include "CalibTracker/SiPixelConnectivity/interface/PixelToLNKAssociateFromAscii.h"
0002
0003 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0004 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0005 #include "DataFormats/TrackerCommon/interface/PixelPannelType.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007
0008 #include <ostream>
0009 #include <fstream>
0010 #include "FWCore/Utilities/interface/Exception.h"
0011
0012 using namespace std;
0013
0014 PixelToLNKAssociateFromAscii::PixelToLNKAssociateFromAscii(const string& fn, const bool phase) {
0015 phase1_ = phase;
0016 init(fn);
0017 }
0018 std::string PixelToLNKAssociateFromAscii::version() const { return theVersion; }
0019
0020 const PixelToLNKAssociateFromAscii::CablingRocId* PixelToLNKAssociateFromAscii::operator()(
0021 const PixelToLNKAssociateFromAscii::DetectorRocId& roc) const {
0022 typedef std::vector<std::pair<DetectorRocId, CablingRocId> >::const_iterator IM;
0023 for (IM im = theConnection.begin(); im != theConnection.end(); im++) {
0024 if ((*(im->first.module) == *roc.module) && (im->first.rocDetId == roc.rocDetId)) {
0025 return &(im->second);
0026 }
0027 }
0028 return nullptr;
0029 }
0030
0031
0032 void PixelToLNKAssociateFromAscii::init(const string& cfg_name) {
0033 edm::LogInfo(" init, input file: ") << cfg_name;
0034
0035 std::ifstream file(cfg_name.c_str());
0036 if (!file) {
0037 edm::LogError(" ** PixelToLNKAssociateFromAscii,init ** ") << " cant open data file: " << cfg_name;
0038 return;
0039 } else {
0040 edm::LogInfo("PixelToLNKAssociateFromAscii, read data from: ") << cfg_name;
0041 }
0042
0043 string line;
0044 int fedId = -1;
0045 int linkId = -1;
0046
0047 try {
0048 while (getline(file, line)) {
0049
0050
0051
0052 string::size_type pos = line.find('#');
0053 if (pos != string::npos)
0054 line = line.erase(pos);
0055
0056 string::size_type posF = line.find("FED:");
0057 string::size_type posL = line.find("LNK:");
0058 string::size_type posM = line.find("MOD:");
0059 string::size_type posR = line.find("ROC:");
0060
0061 edm::LogInfo("") << " read line: " << line;
0062
0063
0064
0065
0066 if (line.compare(0, 3, "VER") == 0) {
0067 edm::LogInfo("version: ") << line;
0068 theVersion = line;
0069 }
0070
0071
0072
0073
0074 else if (posF != string::npos) {
0075 line = line.substr(posF + 4);
0076 fedId = atoi(line.c_str());
0077 }
0078
0079
0080
0081
0082 else if (posL != string::npos) {
0083 string srtL = line.substr(posL + 4);
0084 linkId = atoi(srtL.c_str());
0085 }
0086
0087
0088
0089
0090 if (posM != string::npos) {
0091 if (posR != string::npos) {
0092 string strM = line.substr(posM + 4, posR - posM - 5);
0093 string::size_type pos = strM.find(' ');
0094 if (pos != string::npos)
0095 strM = strM.substr(pos + 1);
0096 string strR = line.substr(posR + 4);
0097 Range range = readRange(strR);
0098
0099 addConnections(fedId, linkId, strM, range);
0100 } else {
0101 string strM = line.substr(posM + 4);
0102 string::size_type pos = strM.find(' ');
0103 if (pos != string::npos)
0104 strM = strM.substr(pos + 1);
0105 addConnections(fedId, linkId, strM, Range(0, 0));
0106 }
0107 }
0108 }
0109 } catch (exception& err) {
0110 edm::LogError("**PixelToLNKAssociateFromAscii** exception") << err.what();
0111 }
0112
0113
0114
0115
0116 std::ostringstream str;
0117 str << " **PixelToLNKAssociateFromAscii ** CONNECTIONS: " << endl;
0118 typedef vector<pair<DetectorRocId, CablingRocId> >::const_iterator ICON;
0119 for (ICON ic = theConnection.begin(); ic != theConnection.end(); ic++) {
0120 str << (*ic).first.module->name() << ", rocDetId=" << (*ic).first.rocDetId << ", fedId=" << ic->second.fedId
0121 << ", linkId=" << ic->second.linkId << ", rocLinkId=" << ic->second.rocLinkId << endl;
0122 }
0123 edm::LogInfo("PixelToLNKAssociateFromAscii") << str.str();
0124 }
0125
0126 void PixelToLNKAssociateFromAscii::addConnections(int fedId, int linkId, std::string module, Range rocDetIds) {
0127 string::size_type pos;
0128
0129
0130 pos = module.find("BPix");
0131 if (pos != string::npos) {
0132 string module0 = module;
0133
0134 string::size_type p = module0.find(' ');
0135
0136
0137
0138 if (p != string::npos)
0139 module0 = module0.substr(0, p);
0140 PixelBarrelName* name = new PixelBarrelName(module0, phase1_);
0141
0142
0143 string strP = module.substr(pos + 6, 2);
0144 PixelBarrelName::Shell part;
0145 if (strP == "mO")
0146 part = PixelBarrelName::mO;
0147 else if (strP == "mI")
0148 part = PixelBarrelName::mI;
0149 else if (strP == "pO")
0150 part = PixelBarrelName::pO;
0151 else
0152 part = PixelBarrelName::pI;
0153
0154
0155
0156
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 int rocLnkId = 0;
0184 bool loopExecuted = false;
0185 for (int rocDetId = rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
0186 loopExecuted = true;
0187 rocLnkId++;
0188 DetectorRocId detectorRocId;
0189
0190 detectorRocId.module = name;
0191 detectorRocId.rocDetId = rocDetId;
0192
0193 CablingRocId cablingRocId;
0194 cablingRocId.fedId = fedId;
0195 cablingRocId.linkId = linkId;
0196 cablingRocId.rocLinkId = rocLnkId;
0197
0198 edm::LogInfo(" roc ") << rocDetId << " " << rocLnkId << " " << name->isHalfModule() << endl;
0199 if (name->isHalfModule() && (rocDetIds.min() > 7) &&
0200 (part == PixelBarrelName::mO || part == PixelBarrelName::mI)) {
0201
0202
0203 edm::LogInfo(" special for half modules ");
0204 cablingRocId.rocLinkId = rocLnkId;
0205 detectorRocId.rocDetId = rocDetId - 8;
0206 }
0207 theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0208 }
0209 if (!loopExecuted)
0210 delete name;
0211 }
0212
0213
0214 pos = module.find("FPix");
0215 if (pos != string::npos) {
0216 string strH = module.substr(pos + 6, 2);
0217 PixelEndcapName::HalfCylinder part;
0218 if (strH == "mO")
0219 part = PixelEndcapName::mO;
0220 else if (strH == "mI")
0221 part = PixelEndcapName::mI;
0222 else if (strH == "pO")
0223 part = PixelEndcapName::pO;
0224 else
0225 part = PixelEndcapName::pI;
0226 module = module.substr(pos + 9);
0227
0228
0229 pos = module.find('_');
0230 if (pos == string::npos)
0231 throw cms::Exception("problem with disk formatting");
0232 int disk = atoi(module.substr(1, pos - 1).c_str());
0233 module = module.substr(pos + 1);
0234
0235
0236 pos = module.find('_');
0237 if (pos == string::npos)
0238 throw cms::Exception("problem with blade formatting");
0239 int blade = atoi(module.substr(3, pos - 3).c_str());
0240 module = module.substr(pos + 1);
0241
0242
0243 pos = module.find('_');
0244 if (pos == string::npos)
0245 throw cms::Exception("problem with pannel formatting");
0246 int pannel = atoi(module.substr(3, pos - 3).c_str());
0247 module = module.substr(pos + 1);
0248
0249
0250
0251
0252
0253
0254 int ring = 1;
0255
0256 PixelPannelType::PannelType pannelType;
0257 if (phase1_) {
0258 pannelType = PixelPannelType::p2x8;
0259
0260
0261
0262 pos = module.find("RNG");
0263 if (pos == string::npos)
0264 throw cms::Exception("problem with ring formatting");
0265 ring = atoi(module.substr(pos + 3, 1).c_str());
0266
0267
0268 } else {
0269 pos = module.find("TYP:");
0270 if (pos == string::npos)
0271 throw cms::Exception("problem with pannel type formatting");
0272 string strT = module.substr(pos + 5, 3);
0273 string strT4 = module.substr(pos + 5, 4);
0274 ring = 1;
0275 if (strT == "P3R")
0276 pannelType = PixelPannelType::p3R;
0277 else if (strT == "P3L")
0278 pannelType = PixelPannelType::p3L;
0279 else if (strT == "P4R")
0280 pannelType = PixelPannelType::p4R;
0281 else if (strT == "P4L")
0282 pannelType = PixelPannelType::p4L;
0283 else if (strT4 == "P2X8")
0284 pannelType = PixelPannelType::p2x8;
0285 else
0286 throw cms::Exception("problem with pannel type formatting (unrecoginzed word)");
0287 }
0288
0289
0290 if (pannelType == PixelPannelType::p4L) {
0291
0292 int rocLnkId = 0;
0293 for (int plaq = 1; plaq <= 4; plaq++) {
0294 Range rocs;
0295 int firstRoc = 0;
0296 int step = 0;
0297 if (plaq == 1) {
0298 rocs = Range(0, 1);
0299 firstRoc = 1;
0300 step = -1;
0301 }
0302 if (plaq == 2) {
0303 rocs = Range(0, 5);
0304 firstRoc = 0;
0305 step = +1;
0306 }
0307 if (plaq == 3) {
0308 rocs = Range(0, 7);
0309 firstRoc = 0;
0310 step = +1;
0311 }
0312 if (plaq == 4) {
0313 rocs = Range(0, 4);
0314 firstRoc = 0;
0315 step = +1;
0316 }
0317 PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
0318 for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
0319 rocLnkId++;
0320 int rocDetId = firstRoc + step * iroc;
0321
0322 DetectorRocId detectorRocId;
0323
0324 detectorRocId.module = name;
0325 detectorRocId.rocDetId = rocDetId;
0326
0327 CablingRocId cablingRocId;
0328 cablingRocId.fedId = fedId;
0329 cablingRocId.linkId = linkId;
0330 cablingRocId.rocLinkId = rocLnkId;
0331
0332 theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0333
0334 }
0335 }
0336 } else if (pannelType == PixelPannelType::p4R) {
0337
0338 int rocLnkId = 0;
0339 for (int plaq = 4; plaq >= 1; plaq--) {
0340 Range rocs;
0341 int firstRoc = 0;
0342 int step = 0;
0343 if (plaq == 1) {
0344 rocs = Range(0, 1);
0345 firstRoc = 1;
0346 step = -1;
0347 }
0348 if (plaq == 2) {
0349 rocs = Range(0, 5);
0350 firstRoc = 3;
0351 step = +1;
0352 }
0353 if (plaq == 3) {
0354 rocs = Range(0, 7);
0355 firstRoc = 4;
0356 step = +1;
0357 }
0358 if (plaq == 4) {
0359 rocs = Range(0, 4);
0360 firstRoc = 0;
0361 step = +1;
0362 }
0363 PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
0364 for (int iroc = rocs.min(); iroc - rocs.max() <= 0; iroc++) {
0365 rocLnkId++;
0366 int rocDetId = firstRoc + step * iroc;
0367 if (rocDetId > rocs.max())
0368 rocDetId = (rocDetId - 1) % rocs.max();
0369
0370 DetectorRocId detectorRocId;
0371
0372 detectorRocId.module = name;
0373 detectorRocId.rocDetId = rocDetId;
0374 CablingRocId cablingRocId;
0375 cablingRocId.fedId = fedId;
0376 cablingRocId.linkId = linkId;
0377 cablingRocId.rocLinkId = rocLnkId;
0378
0379 theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0380
0381 }
0382 }
0383 } else if (pannelType == PixelPannelType::p3L) {
0384
0385 int rocLnkId = 0;
0386 for (int plaq = 1; plaq <= 3; plaq++) {
0387 Range rocs;
0388 int firstRoc = 0;
0389 int step = 0;
0390 if (plaq == 1) {
0391 rocs = Range(0, 5);
0392 firstRoc = 0;
0393 step = 1;
0394 }
0395 if (plaq == 2) {
0396 rocs = Range(0, 7);
0397 firstRoc = 0;
0398 step = 1;
0399 }
0400 if (plaq == 3) {
0401 rocs = Range(0, 9);
0402 firstRoc = 0;
0403 step = 1;
0404 }
0405 PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
0406 for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
0407 rocLnkId++;
0408 int rocDetId = firstRoc + step * iroc;
0409
0410 DetectorRocId detectorRocId;
0411 detectorRocId.module = name;
0412 detectorRocId.rocDetId = rocDetId;
0413
0414 CablingRocId cablingRocId;
0415 cablingRocId.fedId = fedId;
0416 cablingRocId.linkId = linkId;
0417 cablingRocId.rocLinkId = rocLnkId;
0418
0419 theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0420
0421 }
0422 }
0423 } else if (pannelType == PixelPannelType::p3R) {
0424
0425 int rocLnkId = 0;
0426 for (int plaq = 3; plaq >= 1; plaq--) {
0427 Range rocs;
0428 int firstRoc = 0;
0429 int step = 0;
0430 if (plaq == 1) {
0431 rocs = Range(0, 5);
0432 firstRoc = 3;
0433 step = 1;
0434 }
0435 if (plaq == 2) {
0436 rocs = Range(0, 7);
0437 firstRoc = 4;
0438 step = 1;
0439 }
0440 if (plaq == 3) {
0441 rocs = Range(0, 9);
0442 firstRoc = 5;
0443 step = 1;
0444 }
0445 PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, plaq);
0446 for (int iroc = rocs.min(); iroc <= rocs.max(); iroc++) {
0447 rocLnkId++;
0448 int rocDetId = firstRoc + step * iroc;
0449 if (rocDetId > rocs.max())
0450 rocDetId = (rocDetId - 1) % rocs.max();
0451
0452 DetectorRocId detectorRocId;
0453 detectorRocId.module = name;
0454 detectorRocId.rocDetId = rocDetId;
0455
0456 CablingRocId cablingRocId;
0457 cablingRocId.fedId = fedId;
0458 cablingRocId.linkId = linkId;
0459 cablingRocId.rocLinkId = rocLnkId;
0460
0461 theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0462
0463 }
0464 }
0465
0466 } else if (pannelType == PixelPannelType::p2x8) {
0467
0468 int rocLnkId = 0;
0469
0470
0471 PixelEndcapName* name = new PixelEndcapName(part, disk, blade, pannel, ring, phase1_);
0472 bool loopExecuted = false;
0473 for (int rocDetId = rocDetIds.min(); rocDetId <= rocDetIds.max(); rocDetId++) {
0474 loopExecuted = true;
0475 rocLnkId++;
0476 DetectorRocId detectorRocId;
0477 detectorRocId.module = name;
0478 detectorRocId.rocDetId = rocDetId;
0479 CablingRocId cablingRocId;
0480 cablingRocId.fedId = fedId;
0481 cablingRocId.linkId = linkId;
0482 cablingRocId.rocLinkId = rocLnkId;
0483 theConnection.push_back(make_pair(detectorRocId, cablingRocId));
0484 edm::LogInfo("PixelToLNKAssociateFromAscii FPix ")
0485 << " rocDetId: " << rocDetId << " rocLnkId:" << rocLnkId << " fedId = " << fedId << " linkId = " << linkId
0486 << " name = " << name->name();
0487
0488
0489
0490
0491
0492
0493 }
0494 if (!loopExecuted) {
0495 delete name;
0496 }
0497
0498 }
0499 }
0500 }
0501
0502 PixelToLNKAssociateFromAscii::Range PixelToLNKAssociateFromAscii::readRange(const string& l) const {
0503
0504 string l1, l2;
0505 int i1 = -1, i2 = -1;
0506 int len = l.size();
0507
0508
0509
0510 string::size_type p = l.find(',');
0511 if (p != string::npos) {
0512
0513 l1 = l.substr(0, p - 1 + 1);
0514 l2 = l.substr(p + 1, len - 1 - p);
0515 i1 = stoi(l1);
0516 i2 = stoi(l2);
0517
0518 }
0519
0520 return Range(i1, i2);
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555 }