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