File indexing completed on 2024-06-06 04:27:03
0001 #include "L1Trigger/L1TMuonEndCap/interface/SectorProcessorLUT.h"
0002
0003 #include <iostream>
0004 #include <fstream>
0005
0006 #include "FWCore/ParameterSet/interface/FileInPath.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009
0010 SectorProcessorLUT::SectorProcessorLUT() : version_(0xFFFFFFFF) {}
0011
0012 SectorProcessorLUT::~SectorProcessorLUT() {}
0013
0014 void SectorProcessorLUT::read(bool pc_lut_data, int pc_lut_version) {
0015 if (version_ == pc_lut_version)
0016 return;
0017
0018 edm::LogInfo("L1T") << "EMTF using pc_lut_ver: " << pc_lut_version << ", configured for "
0019 << (pc_lut_data ? "data" : "MC");
0020
0021 std::string coord_lut_dir = "";
0022 if (pc_lut_version == 0)
0023 coord_lut_dir = "ph_lut_v1";
0024 else if (pc_lut_version == 1)
0025 coord_lut_dir = "ph_lut_v2";
0026 else if (pc_lut_version == 2 && pc_lut_data)
0027 coord_lut_dir = "ph_lut_v3_data";
0028 else if (pc_lut_version == 3 && pc_lut_data)
0029 coord_lut_dir = "ph_lut_Run3_2022_data";
0030 else if (pc_lut_version == 4 && pc_lut_data)
0031 coord_lut_dir = "ph_lut_Run3_2023_data";
0032 else if (pc_lut_version == 5 && pc_lut_data)
0033 coord_lut_dir = "ph_lut_Run3_2024_data";
0034 else if (pc_lut_version >= 2)
0035 coord_lut_dir = "ph_lut_v2";
0036 else if (pc_lut_version == -1 && pc_lut_data)
0037 coord_lut_dir = "ph_lut_v3_data";
0038 else if (pc_lut_version == -1)
0039 coord_lut_dir = "ph_lut_v2";
0040 else
0041 throw cms::Exception("L1TMuonEndCap")
0042 << "Trying to use EMTF pc_lut_version = " << pc_lut_version << ", does not exist!";
0043
0044
0045 std::string coord_lut_path = "L1Trigger/L1TMuon/data/emtf_luts/" + coord_lut_dir + "/";
0046
0047 read_file(coord_lut_path + "ph_init_neighbor.txt", ph_init_neighbor_);
0048 read_file(coord_lut_path + "ph_disp_neighbor.txt", ph_disp_neighbor_);
0049 read_file(coord_lut_path + "th_init_neighbor.txt", th_init_neighbor_);
0050 read_file(coord_lut_path + "th_disp_neighbor.txt", th_disp_neighbor_);
0051 read_file(coord_lut_path + "th_lut_neighbor.txt", th_lut_neighbor_);
0052 read_file(coord_lut_path + "th_corr_lut_neighbor.txt", th_corr_lut_neighbor_);
0053
0054 std::string cppf_coord_lut_path = "L1Trigger/L1TMuon/data/cppf/";
0055 bool use_local_cppf_files = (pc_lut_version == -1);
0056 if (use_local_cppf_files) {
0057 cppf_coord_lut_path = "L1Trigger/L1TMuon/data/cppf_luts/angleScale_v1/";
0058 }
0059
0060 read_cppf_file(cppf_coord_lut_path,
0061 cppf_ph_lut_,
0062 cppf_th_lut_,
0063 use_local_cppf_files);
0064
0065 if (ph_init_neighbor_.size() != 2 * 6 * 61) {
0066 throw cms::Exception("L1TMuonEndCap") << "Expected ph_init_neighbor_ to get " << 2 * 6 * 61 << " values, "
0067 << "got " << ph_init_neighbor_.size() << " values.";
0068 }
0069
0070 if (ph_disp_neighbor_.size() != 2 * 6 * 61) {
0071 throw cms::Exception("L1TMuonEndCap") << "Expected ph_disp_neighbor_ to get " << 2 * 6 * 61 << " values, "
0072 << "got " << ph_disp_neighbor_.size() << " values.";
0073 }
0074
0075 if (th_init_neighbor_.size() != 2 * 6 * 61) {
0076 throw cms::Exception("L1TMuonEndCap") << "Expected th_init_neighbor_ to get " << 2 * 6 * 61 << " values, "
0077 << "got " << th_init_neighbor_.size() << " values.";
0078 }
0079
0080 if (th_disp_neighbor_.size() != 2 * 6 * 61) {
0081 throw cms::Exception("L1TMuonEndCap") << "Expected th_disp_neighbor_ to get " << 2 * 6 * 61 << " values, "
0082 << "got " << th_disp_neighbor_.size() << " values.";
0083 }
0084
0085 if (th_lut_neighbor_.size() != 2 * 6 * 61 * 128) {
0086 throw cms::Exception("L1TMuonEndCap") << "Expected th_lut_neighbor_ to get " << 2 * 6 * 61 * 128 << " values, "
0087 << "got " << th_lut_neighbor_.size() << " values.";
0088 }
0089
0090 if (th_corr_lut_neighbor_.size() != 2 * 6 * 7 * 128) {
0091 throw cms::Exception("L1TMuonEndCap") << "Expected th_corr_lut_neighbor_ to get " << 2 * 6 * 7 * 128 << " values, "
0092 << "got " << th_corr_lut_neighbor_.size() << " values.";
0093 }
0094
0095 if (cppf_ph_lut_.size() !=
0096 2 * 6 * 6 * 6 * 3 *
0097 64) {
0098 throw cms::Exception("L1TMuonEndCap") << "Expected cppf_ph_lut_ to get " << 2 * 6 * 6 * 6 * 3 * 64 << " values, "
0099 << "got " << cppf_ph_lut_.size() << " values.";
0100 }
0101
0102 if (cppf_th_lut_.size() !=
0103 2 * 6 * 6 * 6 * 3) {
0104 throw cms::Exception("L1TMuonEndCap") << "Expected cppf_th_lut_ to get " << 2 * 6 * 6 * 6 * 3 << " values, "
0105 << "got " << cppf_th_lut_.size() << " values.";
0106 }
0107
0108
0109
0110
0111 ph_patt_corr_ = {0, 0, 5, 5, 5, 5, 2, 2, 2, 2, 0};
0112 if (ph_patt_corr_.size() != 11) {
0113 throw cms::Exception("L1TMuonEndCap") << "Expected ph_patt_corr_ to get " << 11 << " values, "
0114 << "got " << ph_patt_corr_.size() << " values.";
0115 }
0116
0117 ph_patt_corr_sign_ = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0};
0118 if (ph_patt_corr_sign_.size() != 11) {
0119 throw cms::Exception("L1TMuonEndCap") << "Expected ph_patt_corr_sign_ to get " << 11 << " values, "
0120 << "got " << ph_patt_corr_sign_.size() << " values.";
0121 }
0122
0123 ph_zone_offset_ = {39, 57, 76, 39, 58, 76, 41, 60, 79, 95, 114, 132, 95, 114, 133, 98, 116, 135,
0124 38, 76, 113, 39, 58, 76, 95, 114, 132, 38, 76, 113, 39, 58, 76, 95, 114, 132,
0125 38, 76, 113, 38, 57, 76, 95, 113, 132, 21, 21, 23, 1, 21, 1, 21, 1, 20};
0126 if (ph_zone_offset_.size() != 6 * 9) {
0127 throw cms::Exception("L1TMuonEndCap") << "Expected ph_zone_offset_ to get " << 6 * 9 << " values, "
0128 << "got " << ph_zone_offset_.size() << " values.";
0129 }
0130
0131
0132
0133 ph_init_hard_ = {39, 57, 76, 39, 58, 76, 41, 60, 79, 39, 57, 76, 21, 21, 23, 21, 95, 114, 132, 95,
0134 114, 133, 98, 116, 135, 95, 114, 132, 0, 0, 0, 0, 38, 76, 113, 39, 58, 76, 95, 114,
0135 132, 1, 21, 0, 0, 0, 0, 0, 38, 76, 113, 39, 58, 76, 95, 114, 132, 1, 21, 0,
0136 0, 0, 0, 0, 38, 76, 113, 38, 57, 76, 95, 113, 132, 1, 20, 0, 0, 0, 0, 0};
0137 if (ph_init_hard_.size() != 5 * 16) {
0138 throw cms::Exception("L1TMuonEndCap") << "Expected ph_init_hard_ to get " << 5 * 16 << " values, "
0139 << "got " << ph_init_hard_.size() << " values.";
0140 }
0141
0142 version_ = pc_lut_version;
0143 return;
0144 }
0145
0146 uint32_t SectorProcessorLUT::get_ph_init(int fw_endcap, int fw_sector, int pc_lut_id) const {
0147 const uint32_t index = (fw_endcap * 6 + fw_sector) * 61 + pc_lut_id;
0148 uint32_t entry = 0;
0149
0150 if (index < ph_init_neighbor_.size()) {
0151 entry = ph_init_neighbor_.at(index);
0152 } else {
0153 edm::LogError("L1T") << "Could not retrieve entry from LUT. fw_endcap: " << fw_endcap
0154 << ", fw_sector: " << fw_sector << ", pc_lut_id: " << pc_lut_id;
0155 }
0156 return entry;
0157 }
0158
0159 uint32_t SectorProcessorLUT::get_ph_disp(int fw_endcap, int fw_sector, int pc_lut_id) const {
0160 const uint32_t index = (fw_endcap * 6 + fw_sector) * 61 + pc_lut_id;
0161 uint32_t entry = 0;
0162
0163 if (index < ph_disp_neighbor_.size()) {
0164 entry = ph_disp_neighbor_.at(index);
0165 } else {
0166 edm::LogError("L1T") << "Could not retrieve entry from LUT. fw_endcap: " << fw_endcap
0167 << ", fw_sector: " << fw_sector << ", pc_lut_id: " << pc_lut_id;
0168 }
0169 return entry;
0170 }
0171
0172 uint32_t SectorProcessorLUT::get_th_init(int fw_endcap, int fw_sector, int pc_lut_id) const {
0173 const uint32_t index = (fw_endcap * 6 + fw_sector) * 61 + pc_lut_id;
0174 uint32_t entry = 0;
0175
0176 if (index < th_init_neighbor_.size()) {
0177 entry = th_init_neighbor_.at(index);
0178 } else {
0179 edm::LogError("L1T") << "Could not retrieve entry from LUT. fw_endcap: " << fw_endcap
0180 << ", fw_sector: " << fw_sector << ", pc_lut_id: " << pc_lut_id;
0181 }
0182 return entry;
0183 }
0184
0185 uint32_t SectorProcessorLUT::get_th_disp(int fw_endcap, int fw_sector, int pc_lut_id) const {
0186 const uint32_t index = (fw_endcap * 6 + fw_sector) * 61 + pc_lut_id;
0187 uint32_t entry = 0;
0188
0189 if (index < th_disp_neighbor_.size()) {
0190 entry = th_disp_neighbor_.at(index);
0191 } else {
0192 edm::LogError("L1T") << "Could not retrieve entry from LUT. fw_endcap: " << fw_endcap
0193 << ", fw_sector: " << fw_sector << ", pc_lut_id: " << pc_lut_id;
0194 }
0195 return entry;
0196 }
0197
0198 uint32_t SectorProcessorLUT::get_th_lut(int fw_endcap, int fw_sector, int pc_lut_id, int pc_wire_id) const {
0199 int pc_lut_id2 = pc_lut_id;
0200
0201
0202 if ((9 <= pc_lut_id2 && pc_lut_id2 < 12) || (25 <= pc_lut_id2 && pc_lut_id2 < 28))
0203 pc_lut_id2 -= 9;
0204
0205 if (pc_lut_id2 == 15)
0206 pc_lut_id2 -= 3;
0207
0208 const uint32_t index = ((fw_endcap * 6 + fw_sector) * 61 + pc_lut_id2) * 128 + pc_wire_id;
0209 uint32_t entry = 0;
0210
0211 if (index < th_lut_neighbor_.size()) {
0212 entry = th_lut_neighbor_.at(index);
0213 } else {
0214 edm::LogError("L1T") << "Could not retrieve entry from LUT. fw_endcap: " << fw_endcap
0215 << ", fw_sector: " << fw_sector << ", pc_lut_id: " << pc_lut_id
0216 << ", pc_wire_id: " << pc_wire_id;
0217 }
0218 return entry;
0219 }
0220
0221 uint32_t SectorProcessorLUT::get_th_corr_lut(int fw_endcap, int fw_sector, int pc_lut_id, int pc_wire_strip_id) const {
0222 int pc_lut_id2 = pc_lut_id;
0223
0224
0225 if ((9 <= pc_lut_id2 && pc_lut_id2 < 12) || (25 <= pc_lut_id2 && pc_lut_id2 < 28))
0226 pc_lut_id2 -= 9;
0227
0228 if (pc_lut_id2 == 15)
0229 pc_lut_id2 -= 3;
0230
0231 if (pc_lut_id2 <= 3) {
0232 pc_lut_id2 -= 0;
0233 } else if (pc_lut_id2 == 12) {
0234 pc_lut_id2 -= 9;
0235 } else if (16 <= pc_lut_id2 && pc_lut_id2 < 19) {
0236 pc_lut_id2 -= 12;
0237 } else {
0238 edm::LogError("L1T") << "get_th_corr_lut(): out of range pc_lut_id: " << pc_lut_id;
0239 }
0240
0241 const uint32_t index = ((fw_endcap * 6 + fw_sector) * 7 + pc_lut_id2) * 128 + pc_wire_strip_id;
0242 uint32_t entry = 0;
0243
0244 if (index < th_corr_lut_neighbor_.size()) {
0245 entry = th_corr_lut_neighbor_.at(index);
0246 } else {
0247 edm::LogError("L1T") << "Could not retrieve entry from LUT. fw_endcap: " << fw_endcap
0248 << ", fw_sector: " << fw_sector << ", pc_lut_id: " << pc_lut_id
0249 << ", pc_wire_strip_id: " << pc_wire_strip_id;
0250 }
0251 return entry;
0252 }
0253
0254 uint32_t SectorProcessorLUT::get_ph_patt_corr(int pattern) const {
0255 const uint32_t index = pattern;
0256 uint32_t entry = 0;
0257
0258 if (index < ph_patt_corr_.size()) {
0259 entry = ph_patt_corr_.at(index);
0260 } else {
0261 edm::LogError("L1T") << "Could not retrieve entry from LUT. pattern: " << pattern;
0262 }
0263 return entry;
0264 }
0265
0266 uint32_t SectorProcessorLUT::get_ph_patt_corr_sign(int pattern) const {
0267 const uint32_t index = pattern;
0268 uint32_t entry = 0;
0269
0270 if (index < ph_patt_corr_sign_.size()) {
0271 entry = ph_patt_corr_sign_.at(index);
0272 } else {
0273 edm::LogError("L1T") << "Could not retrieve entry from LUT. pattern: " << pattern;
0274 }
0275 return entry;
0276 }
0277
0278 uint32_t SectorProcessorLUT::get_ph_zone_offset(int pc_station, int pc_chamber) const {
0279 const uint32_t index = pc_station * 9 + pc_chamber;
0280 uint32_t entry = 0;
0281
0282 if (index < ph_zone_offset_.size()) {
0283 entry = ph_zone_offset_.at(index);
0284 } else {
0285 edm::LogError("L1T") << "Could not retrieve entry from LUT. pc_station: " << pc_station
0286 << ", pc_chamber: " << pc_chamber;
0287 }
0288 return entry;
0289 }
0290
0291 uint32_t SectorProcessorLUT::get_ph_init_hard(int fw_station, int fw_cscid) const {
0292 const uint32_t index = fw_station * 16 + fw_cscid;
0293 uint32_t entry = 0;
0294
0295 if (index < ph_init_hard_.size()) {
0296 entry = ph_init_hard_.at(index);
0297 } else {
0298 edm::LogError("L1T") << "Could not retrieve entry from LUT. fw_station: " << fw_station
0299 << ", fw_cscid: " << fw_cscid;
0300 }
0301 return entry;
0302 }
0303
0304 uint32_t SectorProcessorLUT::get_cppf_lut_id(
0305 int rpc_region, int rpc_sector, int rpc_station, int rpc_ring, int rpc_subsector, int rpc_roll) const {
0306 uint32_t iendcap = (rpc_region == -1) ? 1 : 0;
0307 uint32_t isector = (rpc_sector - 1);
0308 uint32_t istationring = (rpc_station >= 3) ? ((rpc_station - 3) * 2 + (rpc_ring - 2) + 2) : (rpc_station - 1);
0309 uint32_t isubsector = (rpc_subsector - 1);
0310 uint32_t iroll = (rpc_roll - 1);
0311 return ((((iendcap * 6 + isector) * 6 + istationring) * 6 + isubsector) * 3 + iroll);
0312 }
0313
0314 uint32_t SectorProcessorLUT::get_cppf_ph_lut(int rpc_region,
0315 int rpc_sector,
0316 int rpc_station,
0317 int rpc_ring,
0318 int rpc_subsector,
0319 int rpc_roll,
0320 int halfstrip,
0321 bool is_neighbor) const {
0322 const uint32_t th_index = get_cppf_lut_id(rpc_region, rpc_sector, rpc_station, rpc_ring, rpc_subsector, rpc_roll);
0323 const uint32_t ph_index = (th_index * 64) + (halfstrip - 1);
0324 uint32_t ph = 0;
0325
0326 if (ph_index < cppf_ph_lut_.size()) {
0327 ph = cppf_ph_lut_.at(ph_index);
0328 } else {
0329 edm::LogError("L1T") << "Could not retrieve entry from LUT. rpc_region: " << rpc_region
0330 << ", rpc_sector: " << rpc_sector << ", rpc_station: " << rpc_station
0331 << ", rpc_ring: " << rpc_ring << ", rpc_subsector: " << rpc_subsector
0332 << ", rpc_roll: " << rpc_roll << ", halfstrip: " << halfstrip
0333 << ", is_neighbor: " << is_neighbor;
0334 }
0335
0336 if (!is_neighbor && rpc_subsector == 2)
0337 ph += 900;
0338 return ph;
0339 }
0340
0341 uint32_t SectorProcessorLUT::get_cppf_th_lut(
0342 int rpc_region, int rpc_sector, int rpc_station, int rpc_ring, int rpc_subsector, int rpc_roll) const {
0343 const uint32_t th_index = get_cppf_lut_id(rpc_region, rpc_sector, rpc_station, rpc_ring, rpc_subsector, rpc_roll);
0344 uint32_t th = 0;
0345
0346 if (th_index < cppf_th_lut_.size()) {
0347 th = cppf_th_lut_.at(th_index);
0348 } else {
0349 edm::LogError("L1T") << "Could not retrieve entry from LUT. rpc_region: " << rpc_region
0350 << ", rpc_sector: " << rpc_sector << ", rpc_station: " << rpc_station
0351 << ", rpc_ring: " << rpc_ring << ", rpc_subsector: " << rpc_subsector
0352 << ", rpc_roll: " << rpc_roll;
0353 }
0354 return th;
0355 }
0356
0357 void SectorProcessorLUT::read_file(const std::string& filename, std::vector<uint32_t>& vec) {
0358 vec.clear();
0359
0360 std::ifstream infile;
0361 infile.open(edm::FileInPath(filename).fullPath().c_str());
0362
0363 int buf;
0364 while (infile >> buf) {
0365 buf = (buf == -999) ? 0 : buf;
0366 vec.push_back(buf);
0367 }
0368 infile.close();
0369 }
0370
0371 void SectorProcessorLUT::read_cppf_file(const std::string& filename,
0372 std::vector<uint32_t>& vec1,
0373 std::vector<uint32_t>& vec2,
0374 bool local) {
0375 auto get_rpc_region = [](uint32_t id) { return (static_cast<int>((id >> 0) & 0X3) + (-1)); };
0376 auto get_rpc_sector = [](uint32_t id) { return (static_cast<int>((id >> 7) & 0XF) + (1)); };
0377 auto get_rpc_ring = [](uint32_t id) { return (static_cast<int>((id >> 2) & 0X7) + (1)); };
0378 auto get_rpc_station = [](uint32_t id) { return (static_cast<int>((id >> 5) & 0X3) + (1)); };
0379 auto get_rpc_subsector = [](uint32_t id) { return (static_cast<int>((id >> 12) & 0X7) + (1)); };
0380 auto get_rpc_roll = [](uint32_t id) { return (static_cast<int>((id >> 15) & 0X7) + (0)); };
0381
0382 std::vector<std::string> cppf_filenames = {
0383 "angleScale_RPC_CPPFp1.txt",
0384 "angleScale_RPC_CPPFp2.txt",
0385 "angleScale_RPC_CPPFp3.txt",
0386 "angleScale_RPC_CPPFp4.txt",
0387 "angleScale_RPC_CPPFn1.txt",
0388 "angleScale_RPC_CPPFn2.txt",
0389 "angleScale_RPC_CPPFn3.txt",
0390 "angleScale_RPC_CPPFn4.txt",
0391 };
0392
0393 vec1.clear();
0394 vec2.clear();
0395 vec1.resize(2 * 6 * 6 * 6 * 3 * 64, 0);
0396 vec2.resize(2 * 6 * 6 * 6 * 3, 0);
0397
0398 for (size_t i = 0; i < cppf_filenames.size(); ++i) {
0399 std::ifstream infile;
0400 infile.open(edm::FileInPath(filename + cppf_filenames.at(i)).fullPath().c_str());
0401
0402
0403
0404 int buf1, buf2, buf3, buf4, buf5, buf6;
0405
0406 int buf1_prev = 0, buf2_prev = 0, halfstrip_prev = 0;
0407 int line_num = 0;
0408 int count_dir = -1;
0409 int dStrip = 0;
0410 while ((infile >> buf1) && (infile >> buf2) && (infile >> buf3) && (infile >> buf4) && (infile >> buf5) &&
0411 (infile >> buf6)) {
0412 if ((line_num % 192) == 191)
0413 line_num += 1;
0414 line_num += 1;
0415
0416
0417 if ((line_num % 2) == 1) {
0418 buf1_prev = buf1;
0419 buf2_prev = buf2;
0420 }
0421
0422 if (local && (buf1 == 0 || buf2 == 0)) {
0423 throw cms::Exception("L1TMuonEndCap") << "Expected non-0 values, got buf1 = " << buf1 << ", buf2 = " << buf2;
0424 }
0425 if (!local && (buf1_prev == 0 || buf2_prev == 0)) {
0426 throw cms::Exception("L1TMuonEndCap")
0427 << "Expected non-0 values, got buf1_prev = " << buf1_prev << ", buf2_prev = " << buf2_prev;
0428 }
0429
0430 uint32_t id = (local ? buf1 : buf1_prev);
0431 int32_t rpc_region = get_rpc_region(id);
0432 int32_t rpc_sector = get_rpc_sector(id);
0433 int32_t rpc_station = get_rpc_station(id);
0434 int32_t rpc_ring = get_rpc_ring(id);
0435 int32_t rpc_subsector = get_rpc_subsector(id);
0436 int32_t rpc_roll = get_rpc_roll(id);
0437
0438
0439 if (buf2_prev * 2 > halfstrip_prev + 8 ||
0440 buf2_prev * 2 < halfstrip_prev - 8) {
0441 if (buf2_prev == 1)
0442 count_dir = +1;
0443 else
0444 count_dir = -1;
0445 }
0446 if (count_dir == -1)
0447 dStrip = (buf2_prev * 2 == halfstrip_prev ? 1 : 0);
0448 if (count_dir == +1)
0449 dStrip = (buf2_prev * 2 == halfstrip_prev + 2 ? 1 : 0);
0450 if (buf2_prev * 2 < halfstrip_prev - 8 && buf2_prev == 1)
0451 dStrip = 1;
0452
0453
0454 uint32_t halfstrip =
0455 (local ? buf2
0456 : buf2_prev * 2 -
0457 dStrip);
0458 halfstrip_prev = halfstrip;
0459
0460 uint32_t ph = buf5;
0461 uint32_t th = buf6;
0462
0463 const uint32_t th_index = get_cppf_lut_id(rpc_region, rpc_sector, rpc_station, rpc_ring, rpc_subsector, rpc_roll);
0464 const uint32_t ph_index = (th_index * 64) + (halfstrip - 1);
0465
0466
0467
0468
0469 vec1.at(ph_index) = ph;
0470 if (halfstrip == 1)
0471 vec2.at(th_index) = th;
0472
0473
0474 if (!local && (line_num % 192) == 191)
0475 vec1.at(ph_index + 1) = ph;
0476
0477 }
0478 infile.close();
0479 }
0480 }