File indexing completed on 2024-04-06 12:10:27
0001 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader.h"
0002 #include "EventFilter/CSCRawToDigi/interface/CSCDMBHeader.h"
0003 #include "EventFilter/CSCRawToDigi/interface/cscPackerCompare.h"
0004 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2006.h"
0005 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2007.h"
0006 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2007_rev0x50c3.h"
0007 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2013.h"
0008 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2020_TMB.h"
0009 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2020_CCLUT.h"
0010 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2020_GEM.h"
0011 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2020_Run2.h"
0012 #include "DataFormats/CSCDigi/interface/CSCCLCTDigi.h"
0013 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigi.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 #include <cmath>
0016 #include <cstring> // memcpy
0017
0018 #ifdef LOCAL_UNPACK
0019 bool CSCTMBHeader::debug = false;
0020 #else
0021 std::atomic<bool> CSCTMBHeader::debug{false};
0022 #endif
0023
0024 CSCTMBHeader::CSCTMBHeader(int firmwareVersion, int firmwareRevision)
0025 : theHeaderFormat(), theFirmwareVersion(firmwareVersion) {
0026 if (firmwareVersion == 2020) {
0027 if ((firmwareRevision < 0x4000) && (firmwareRevision > 0x0)) {
0028 bool isGEM_fw = false;
0029 bool isCCLUT_HMT_fw = false;
0030 bool isOTMB_Run2_fw = false;
0031 bool isTMB_Run3_fw = false;
0032 bool isTMB_Run2_fw = false;
0033 bool isTMB_hybrid_fw = false;
0034 bool isRun2_df = false;
0035 unsigned df_version = (firmwareRevision >> 9) & 0xF;
0036 unsigned major_ver = (firmwareRevision >> 5) & 0xF;
0037
0038 switch (df_version) {
0039 case 0x4:
0040 isTMB_hybrid_fw = true;
0041 break;
0042 case 0x3:
0043 isGEM_fw = true;
0044 break;
0045 case 0x2:
0046 isCCLUT_HMT_fw = true;
0047 break;
0048 case 0x1:
0049 isOTMB_Run2_fw = true;
0050 break;
0051 case 0x0:
0052 if (major_ver == 1)
0053 isTMB_Run2_fw = true;
0054 else
0055 isTMB_Run3_fw = true;
0056 break;
0057 default:
0058 isGEM_fw = true;
0059 }
0060 if (major_ver == 1) {
0061 isRun2_df = true;
0062 }
0063
0064 if (isGEM_fw) {
0065 if (isRun2_df) {
0066 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_Run2(firmwareRevision));
0067 } else {
0068 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_GEM());
0069 }
0070 } else if (isCCLUT_HMT_fw) {
0071 if (isRun2_df) {
0072 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_Run2(firmwareRevision));
0073 } else {
0074 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_CCLUT());
0075 }
0076 } else if (isOTMB_Run2_fw || isTMB_Run2_fw || isRun2_df) {
0077 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_Run2(firmwareRevision));
0078 } else if (isTMB_Run3_fw) {
0079 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_CCLUT());
0080 } else if (isTMB_hybrid_fw) {
0081 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_TMB());
0082 }
0083 }
0084 } else if (firmwareVersion == 2013) {
0085 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2013());
0086 } else if (firmwareVersion == 2006) {
0087 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2006());
0088 } else if (firmwareVersion == 2007) {
0089
0090
0091
0092
0093
0094 if ((firmwareRevision >= 0x50c3) || (firmwareRevision < 0x42D5)) {
0095
0096
0097 if ((firmwareRevision >= 0x6000) || (firmwareRevision < 0x42D5)) {
0098 bool isGEMfirmware = false;
0099
0100 if ((firmwareRevision < 0x4000) && (firmwareRevision > 0x0)) {
0101 if (((firmwareRevision >> 9) & 0x3) == 0x3)
0102 isGEMfirmware = true;
0103 if (isGEMfirmware) {
0104 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_GEM());
0105 } else {
0106 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2013());
0107 }
0108 }
0109 } else {
0110 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007_rev0x50c3());
0111 }
0112 } else {
0113 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007());
0114 }
0115 } else {
0116 edm::LogError("CSCTMBHeader|CSCRawToDigi") << "failed to determine TMB firmware version!!";
0117 }
0118 }
0119
0120
0121
0122
0123
0124 CSCTMBHeader::CSCTMBHeader(const unsigned short *buf) : theHeaderFormat() {
0125
0126 if (buf[0] == 0xDB0C) {
0127 theFirmwareVersion = 2007;
0128 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007(buf));
0129
0130
0131
0132
0133
0134 if ((theHeaderFormat->firmwareRevision() >= 0x50c3) || (theHeaderFormat->firmwareRevision() < 0x42D5)) {
0135
0136
0137 if ((theHeaderFormat->firmwareRevision() >= 0x6000) || (theHeaderFormat->firmwareRevision() < 0x42D5)) {
0138 theFirmwareVersion = 2013;
0139 bool isGEM_fw = false;
0140 bool isCCLUT_HMT_fw = false;
0141 bool isOTMB_Run2_fw = false;
0142 bool isTMB_Run3_fw = false;
0143 bool isTMB_Run2_fw = false;
0144 bool isTMB_hybrid_fw = false;
0145 bool isRun2_df = false;
0146 unsigned firmwareRevision = theHeaderFormat->firmwareRevision();
0147
0148 if ((firmwareRevision < 0x4000) && (firmwareRevision > 0x0)) {
0149 theFirmwareVersion = 2020;
0150 unsigned df_version = (firmwareRevision >> 9) & 0xF;
0151 unsigned major_ver = (firmwareRevision >> 5) & 0xF;
0152
0153 switch (df_version) {
0154 case 0x4:
0155 isTMB_hybrid_fw = true;
0156 break;
0157 case 0x3:
0158 isGEM_fw = true;
0159 break;
0160 case 0x2:
0161 isCCLUT_HMT_fw = true;
0162 break;
0163 case 0x1:
0164 isOTMB_Run2_fw = true;
0165 break;
0166 case 0x0:
0167 if (major_ver == 1)
0168 isTMB_Run2_fw = true;
0169 else
0170 isTMB_Run3_fw = true;
0171 break;
0172 default:
0173 isGEM_fw = true;
0174 }
0175 if (major_ver == 1) {
0176 isRun2_df = true;
0177 }
0178 }
0179 if (theFirmwareVersion == 2020) {
0180 if (isGEM_fw) {
0181 if (isRun2_df) {
0182 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_Run2(buf));
0183 } else {
0184 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_GEM(buf));
0185 }
0186 } else if (isCCLUT_HMT_fw) {
0187 if (isRun2_df) {
0188 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_Run2(buf));
0189 } else {
0190 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_CCLUT(buf));
0191 }
0192 } else if (isOTMB_Run2_fw || isTMB_Run2_fw || isRun2_df) {
0193 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_Run2(buf));
0194 } else if (isTMB_Run3_fw) {
0195 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_CCLUT(buf));
0196 } else if (isTMB_hybrid_fw) {
0197 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2020_TMB(buf));
0198 }
0199
0200 } else {
0201 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2013(buf));
0202 }
0203
0204 } else {
0205 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007_rev0x50c3(buf));
0206 }
0207 }
0208
0209 } else if (buf[0] == 0x6B0C) {
0210 theFirmwareVersion = 2006;
0211 theHeaderFormat = std::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2006(buf));
0212 } else {
0213 edm::LogError("CSCTMBHeader|CSCRawToDigi") << "failed to determine TMB firmware version!!";
0214 }
0215 }
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250 void CSCTMBHeader::add(const std::vector<CSCCLCTDigi> &digis) {
0251
0252 if (!digis.empty()) {
0253 addCLCT0(digis[0]);
0254 }
0255 if (digis.size() > 1)
0256 addCLCT1(digis[1]);
0257 }
0258
0259 void CSCTMBHeader::add(const std::vector<CSCCorrelatedLCTDigi> &digis) {
0260
0261 if (!digis.empty())
0262 addCorrelatedLCT0(digis[0]);
0263 if (digis.size() > 1)
0264 addCorrelatedLCT1(digis[1]);
0265 }
0266
0267 void CSCTMBHeader::add(const std::vector<CSCShowerDigi> &digis) {
0268 if (!digis.empty())
0269 theHeaderFormat->addShower(digis[0]);
0270 }
0271
0272 CSCTMBHeader2007 CSCTMBHeader::tmbHeader2007() const {
0273 CSCTMBHeader2007 *result = dynamic_cast<CSCTMBHeader2007 *>(theHeaderFormat.get());
0274 if (result == nullptr) {
0275 throw cms::Exception("Could not get 2007 TMB header format");
0276 }
0277 return *result;
0278 }
0279
0280 CSCTMBHeader2007_rev0x50c3 CSCTMBHeader::tmbHeader2007_rev0x50c3() const {
0281 CSCTMBHeader2007_rev0x50c3 *result = dynamic_cast<CSCTMBHeader2007_rev0x50c3 *>(theHeaderFormat.get());
0282 if (result == nullptr) {
0283 throw cms::Exception("Could not get 2007 rev0x50c3 TMB header format");
0284 }
0285 return *result;
0286 }
0287
0288 CSCTMBHeader2013 CSCTMBHeader::tmbHeader2013() const {
0289 CSCTMBHeader2013 *result = dynamic_cast<CSCTMBHeader2013 *>(theHeaderFormat.get());
0290 if (result == nullptr) {
0291 throw cms::Exception("Could not get 2013 TMB header format");
0292 }
0293 return *result;
0294 }
0295
0296 CSCTMBHeader2020_TMB CSCTMBHeader::tmbHeader2020_TMB() const {
0297 CSCTMBHeader2020_TMB *result = dynamic_cast<CSCTMBHeader2020_TMB *>(theHeaderFormat.get());
0298 if (result == nullptr) {
0299 throw cms::Exception("Could not get 2020 TMB Run3 header format");
0300 }
0301 return *result;
0302 }
0303
0304 CSCTMBHeader2020_CCLUT CSCTMBHeader::tmbHeader2020_CCLUT() const {
0305 CSCTMBHeader2020_CCLUT *result = dynamic_cast<CSCTMBHeader2020_CCLUT *>(theHeaderFormat.get());
0306 if (result == nullptr) {
0307 throw cms::Exception("Could not get 2020 (O)TMB CCLUT header format");
0308 }
0309 return *result;
0310 }
0311
0312 CSCTMBHeader2020_GEM CSCTMBHeader::tmbHeader2020_GEM() const {
0313 CSCTMBHeader2020_GEM *result = dynamic_cast<CSCTMBHeader2020_GEM *>(theHeaderFormat.get());
0314 if (result == nullptr) {
0315 throw cms::Exception("Could not get 2020 (O)TMB GEM header format");
0316 }
0317 return *result;
0318 }
0319
0320 CSCTMBHeader2020_Run2 CSCTMBHeader::tmbHeader2020_Run2() const {
0321 CSCTMBHeader2020_Run2 *result = dynamic_cast<CSCTMBHeader2020_Run2 *>(theHeaderFormat.get());
0322 if (result == nullptr) {
0323 throw cms::Exception("Could not get 2020 (O)TMB legacy Run2 header format");
0324 }
0325 return *result;
0326 }
0327
0328 CSCTMBHeader2006 CSCTMBHeader::tmbHeader2006() const {
0329 CSCTMBHeader2006 *result = dynamic_cast<CSCTMBHeader2006 *>(theHeaderFormat.get());
0330 if (result == nullptr) {
0331 throw cms::Exception("Could not get 2006 TMB header format");
0332 }
0333 return *result;
0334 }
0335
0336 void CSCTMBHeader::selfTest(int firmwareVersion, int firmwareRevision) {
0337 constexpr bool debug = false;
0338
0339
0340 for (int station = 1; station <= 4; ++station) {
0341 for (int iendcap = 1; iendcap <= 2; ++iendcap) {
0342 CSCDetId detId(iendcap, station, 1, 1, 0);
0343
0344
0345
0346
0347
0348
0349 CSCCLCTDigi clct0(1, 1, 4, 1, 0, 30, 4, 2, 1);
0350 CSCCLCTDigi clct1(1, 1, 3, 1, 1, 31, 1, 2, 2);
0351
0352
0353
0354 CSCCorrelatedLCTDigi lct0(1, 1, 2, 10, 98, 5, 0, 1, 0, 0, 0, 0);
0355 CSCCorrelatedLCTDigi lct1(2, 1, 2, 20, 15, 9, 1, 0, 0, 0, 0, 0);
0356
0357
0358
0359 if (firmwareVersion >= 2020) {
0360 bool isGEM_fw = false;
0361 bool isCCLUT_HMT_fw = false;
0362 bool isOTMB_Run2_fw = false;
0363 bool isTMB_Run3_fw = false;
0364 bool isTMB_Run2_fw = false;
0365 bool isTMB_hybrid_fw = false;
0366 bool isRun2_df = false;
0367 unsigned df_version = (firmwareRevision >> 9) & 0xF;
0368 unsigned major_ver = (firmwareRevision >> 5) & 0xF;
0369
0370 switch (df_version) {
0371 case 0x4:
0372 isTMB_hybrid_fw = true;
0373 break;
0374 case 0x3:
0375 isGEM_fw = true;
0376 break;
0377 case 0x2:
0378 isCCLUT_HMT_fw = true;
0379 break;
0380 case 0x1:
0381 isOTMB_Run2_fw = true;
0382 break;
0383 case 0x0:
0384 if (major_ver == 1)
0385 isTMB_Run2_fw = true;
0386 else
0387 isTMB_Run3_fw = true;
0388 break;
0389 default:
0390 isGEM_fw = true;
0391 }
0392 if (major_ver == 1) {
0393 isRun2_df = true;
0394 }
0395 if ((isGEM_fw || isCCLUT_HMT_fw || isTMB_Run3_fw) && !isRun2_df && !isOTMB_Run2_fw && !isTMB_Run2_fw &&
0396 !isTMB_hybrid_fw) {
0397 clct0 = CSCCLCTDigi(
0398 1, 6, 6, 1, 0, (120 % 32), (120 / 32), 2, 1, 3, 0xebf, CSCCLCTDigi::Version::Run3, true, false, 2, 6);
0399 clct1 = CSCCLCTDigi(
0400 1, 6, 3, 1, 1, (132 % 32), (132 / 32), 2, 2, 3, 0xe54, CSCCLCTDigi::Version::Run3, false, true, 1, 15);
0401 }
0402 if ((isGEM_fw || isCCLUT_HMT_fw || isTMB_Run3_fw) && !isRun2_df && !isOTMB_Run2_fw && !isTMB_Run2_fw &&
0403 !isTMB_hybrid_fw) {
0404 lct0 = CSCCorrelatedLCTDigi(
0405 1, 1, 3, 85, 120, 6, 0, 0, 0, 0, 0, 0, CSCCorrelatedLCTDigi::Version::Run3, true, false, 2, 6);
0406 lct1 = CSCCorrelatedLCTDigi(
0407 2, 1, 2, 81, 132, 3, 1, 0, 0, 0, 0, 0, CSCCorrelatedLCTDigi::Version::Run3, false, true, 0, 15);
0408 }
0409 if (isTMB_hybrid_fw) {
0410 lct0 = CSCCorrelatedLCTDigi(
0411 1, 1, 3, 85, 120, 6, 0, 0, 0, 0, 0, 0, CSCCorrelatedLCTDigi::Version::Run3, false, false, 0, 0);
0412 lct1 = CSCCorrelatedLCTDigi(
0413 2, 1, 2, 81, 132, 3, 1, 0, 0, 0, 0, 0, CSCCorrelatedLCTDigi::Version::Run3, false, false, 0, 0);
0414 }
0415 }
0416
0417 CSCTMBHeader tmbHeader(firmwareVersion, firmwareRevision);
0418 tmbHeader.addCLCT0(clct0);
0419 tmbHeader.addCLCT1(clct1);
0420 tmbHeader.addCorrelatedLCT0(lct0);
0421 tmbHeader.addCorrelatedLCT1(lct1);
0422 std::vector<CSCCLCTDigi> clcts = tmbHeader.CLCTDigis(detId.rawId());
0423
0424 assert(cscPackerCompare(clcts[0], clct0));
0425 assert(cscPackerCompare(clcts[1], clct1));
0426 if (debug) {
0427 std::cout << "Match for: " << clct0 << "\n";
0428 std::cout << " " << clct1 << "\n \n";
0429 }
0430
0431 std::vector<CSCCorrelatedLCTDigi> lcts = tmbHeader.CorrelatedLCTDigis(detId.rawId());
0432 assert(cscPackerCompare(lcts[0], lct0));
0433 assert(cscPackerCompare(lcts[1], lct1));
0434 if (debug) {
0435 std::cout << "Match for: " << lct0 << "\n";
0436 std::cout << " " << lct1 << "\n";
0437 }
0438
0439
0440 unsigned short int *data = tmbHeader.data();
0441 CSCTMBHeader newHeader(data);
0442 clcts = newHeader.CLCTDigis(detId.rawId());
0443 assert(cscPackerCompare(clcts[0], clct0));
0444 assert(cscPackerCompare(clcts[1], clct1));
0445 lcts = newHeader.CorrelatedLCTDigis(detId.rawId());
0446 assert(cscPackerCompare(lcts[0], lct0));
0447 assert(cscPackerCompare(lcts[1], lct1));
0448 }
0449 }
0450 }
0451
0452 std::ostream &operator<<(std::ostream &os, const CSCTMBHeader &hdr) {
0453 hdr.theHeaderFormat->print(os);
0454 return os;
0455 }