File indexing completed on 2025-08-06 08:17:30
0001 #include "TowerInfoDefs.h"
0002 #include "RawTowerDefs.h"
0003
0004 #include <cstdlib>
0005 #include <iostream>
0006
0007 #pragma GCC diagnostic push
0008 #pragma GCC diagnostic ignored "-Wunused-function"
0009 static const int emcadc[8][8] = {
0010 {62, 60, 46, 44, 30, 28, 14, 12},
0011 {63, 61, 47, 45, 31, 29, 15, 13},
0012 {58, 56, 42, 40, 26, 24, 10, 8},
0013 {59, 57, 43, 41, 27, 25, 11, 9},
0014 {54, 52, 38, 36, 22, 20, 6, 4},
0015 {55, 53, 39, 37, 23, 21, 7, 5},
0016 {50, 48, 34, 32, 18, 16, 2, 0},
0017 {51, 49, 35, 33, 19, 17, 3, 1}};
0018
0019 static const int hcaladc[8][2] = {
0020 {0, 1},
0021 {2, 3},
0022 {4, 5},
0023 {6, 7},
0024 {8, 9},
0025 {10, 11},
0026 {12, 13},
0027 {14, 15}};
0028 static const int epdchnlmap[16][2] = {
0029 {0, 0},
0030 {1, 2},
0031 {3, 4},
0032 {5, 6},
0033 {7, 8},
0034 {9, 10},
0035 {11, 12},
0036 {13, 14},
0037 {15, 16},
0038 {17, 18},
0039 {19, 20},
0040 {21, 22},
0041 {23, 24},
0042 {25, 26},
0043 {27, 28},
0044 {29, 30}};
0045
0046 static const int epd_phimap[31] = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
0047 static const int epd_rmap[31] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15};
0048
0049 unsigned int TowerInfoDefs::encode_emcal(const unsigned int towerIndex)
0050 {
0051 static int phimap[64];
0052 static int etamap[64];
0053 static int etabinoffset[4];
0054 static int ifirst = 1;
0055 if (ifirst == 1)
0056 {
0057 for (int j = 0; j < 8; j++)
0058 {
0059 for (int k = 0; k < 8; k++)
0060 {
0061 etamap[emcadc[j][k]] = j;
0062 phimap[emcadc[j][k]] = k;
0063 }
0064 }
0065 etabinoffset[0] = 24;
0066 etabinoffset[1] = 0;
0067 etabinoffset[2] = 48;
0068 etabinoffset[3] = 72;
0069 ifirst = 0;
0070 }
0071 const int channels_per_sector = 64;
0072 const int supersector = 64 * 12;
0073 const int nchannelsperpacket = 64 * 3;
0074 const int maxphibin = 7;
0075 const int maxetabin = 23;
0076 int supersectornumber = towerIndex / supersector;
0077 int packet = (towerIndex % supersector) / nchannelsperpacket;
0078 if (packet < 0 || packet > 3)
0079 {
0080 std::cout << "Attempting to access channel with invalid value in EMCal " << packet << std::endl;
0081 exit(1);
0082 }
0083 int interfaceboard = ((towerIndex % supersector) % nchannelsperpacket) / channels_per_sector;
0084 int interfaceboard_channel = ((towerIndex % supersector) % nchannelsperpacket) % channels_per_sector;
0085 int localphibin = phimap[interfaceboard_channel];
0086 if (packet == 0 || packet == 1)
0087 {
0088 localphibin = maxphibin - localphibin;
0089 }
0090 int localetabin = etamap[interfaceboard_channel];
0091 int packet_etabin = localetabin + 8 * interfaceboard;
0092 if (packet == 0 || packet == 1)
0093 {
0094 packet_etabin = maxetabin - packet_etabin;
0095 }
0096 unsigned int globaletabin = packet_etabin + etabinoffset[packet];
0097 unsigned int globalphibin = localphibin + supersectornumber * 8;
0098 unsigned int key = globalphibin + (globaletabin << 16U);
0099 return key;
0100 }
0101
0102 unsigned int TowerInfoDefs::encode_emcal(const unsigned int etabin, const unsigned int phibin)
0103 {
0104 unsigned int key = phibin + (etabin << 16U);
0105 return key;
0106 }
0107
0108 unsigned int TowerInfoDefs::decode_emcal(const unsigned int tower_key)
0109 {
0110 const int etabinoffset[4] = {24, 0, 48, 72};
0111 const int etabinmap[4] = {1, 0, 2, 3};
0112 const int channels_per_sector = 64;
0113 const int supersector = 64 * 12;
0114 const int nchannelsperpacket = 64 * 3;
0115 const int maxphibin = 7;
0116 const int maxetabin = 23;
0117
0118 unsigned int etabin = tower_key >> 16U;
0119 unsigned int phibin = tower_key - (etabin << 16U);
0120 int packet = etabinmap[(int) etabin / 24];
0121 int localetabin = etabin - etabinoffset[packet];
0122 int localphibin = phibin % 8;
0123 int supersectornumber = phibin / 8;
0124 int ib = 0;
0125 if (packet == 0 || packet == 1)
0126 {
0127 localetabin = maxetabin - localetabin;
0128 }
0129 ib = localetabin / 8;
0130 unsigned int index = 0;
0131 if (packet == 0 || packet == 1)
0132 {
0133 localphibin = maxphibin - localphibin;
0134 }
0135 localetabin = localetabin % 8;
0136 unsigned int localindex = emcadc[localetabin][localphibin];
0137 index = localindex + channels_per_sector * ib + packet * nchannelsperpacket + supersector * supersectornumber;
0138 return index;
0139 }
0140
0141 unsigned int TowerInfoDefs::encode_hcal(const unsigned int towerIndex)
0142 {
0143 static int phimap[64];
0144 static int etamap[64];
0145 static int etabinoffset[4];
0146 static int phibinoffset[4];
0147 static int ifirst = 1;
0148 if (ifirst == 1)
0149 {
0150 for (int j = 0; j < 8; j++)
0151 {
0152 for (int k = 0; k < 2; k++)
0153 {
0154 etamap[hcaladc[j][k]] = j;
0155 phimap[hcaladc[j][k]] = k;
0156 }
0157 }
0158 etabinoffset[0] = 0;
0159 etabinoffset[1] = 8;
0160 etabinoffset[2] = 16;
0161 etabinoffset[3] = 0;
0162
0163 phibinoffset[0] = 0;
0164 phibinoffset[1] = 2;
0165 phibinoffset[2] = 4;
0166 phibinoffset[3] = 6;
0167 ifirst = 0;
0168 }
0169
0170 const int channels_per_sector = 16;
0171 const int supersector = 16 * 4 * 3;
0172 const int nchannelsperpacket = channels_per_sector * 4;
0173
0174
0175 int supersectornumber = towerIndex / supersector;
0176 int packet = (towerIndex % supersector) / nchannelsperpacket;
0177 if (packet < 0 || packet > 3)
0178 {
0179 std::cout << "Attempting to access channel with invalid value ih HCAL " << packet << std::endl;
0180 exit(1);
0181 }
0182 int interfaceboard = ((towerIndex % supersector) % nchannelsperpacket) / channels_per_sector;
0183 int interfaceboard_channel = ((towerIndex % supersector) % nchannelsperpacket) % channels_per_sector;
0184 int localphibin = phimap[interfaceboard_channel] + phibinoffset[interfaceboard];
0185 int localetabin = etamap[interfaceboard_channel];
0186 int packet_etabin = localetabin;
0187 unsigned int globaletabin = packet_etabin + etabinoffset[packet];
0188 unsigned int globalphibin = localphibin + supersectornumber * 8;
0189 unsigned int key = globalphibin + (globaletabin << 16U);
0190 return key;
0191 }
0192
0193
0194 unsigned int TowerInfoDefs::encode_hcal(const unsigned int etabin, const unsigned int phibin)
0195 {
0196 unsigned int key = phibin + (etabin << 16U);
0197 return key;
0198 }
0199
0200 unsigned int TowerInfoDefs::decode_hcal(const unsigned int tower_key)
0201 {
0202 int channels_per_sector = 16;
0203 int supersector = 16 * 4 * 3;
0204 int nchannelsperpacket = channels_per_sector * 4;
0205 int etabinoffset[3] = {0, 8, 16};
0206 int phibinoffset[4] = {0, 2, 4, 6};
0207 unsigned int etabin = tower_key >> 16U;
0208 unsigned int phibin = tower_key - (etabin << 16U);
0209 int packet = etabin / 8;
0210 int localetabin = etabin - etabinoffset[packet];
0211 int localphibin = phibin % 8;
0212 int supersectornumber = phibin / 8;
0213 int ib = localphibin / 2;
0214 unsigned int index = 0;
0215 localphibin = localphibin - phibinoffset[ib];
0216 unsigned int localindex = hcaladc[localetabin][localphibin];
0217 index = localindex + channels_per_sector * ib + packet * nchannelsperpacket + supersector * supersectornumber;
0218 return index;
0219 }
0220
0221
0222 unsigned int TowerInfoDefs::getCaloTowerPhiBin(const unsigned int key)
0223 {
0224 unsigned int etabin = key >> 16U;
0225 unsigned int phibin = key - (etabin << 16U);
0226 return phibin;
0227 }
0228
0229
0230 unsigned int TowerInfoDefs::getCaloTowerEtaBin(const unsigned int key)
0231 {
0232 unsigned int etabin = key >> 16U;
0233 return etabin;
0234 }
0235
0236
0237 std::pair<int, int> TowerInfoDefs::getEMCalSectorIB(const unsigned int towerIndex)
0238 {
0239 constexpr int channels_per_sector = 384;
0240 constexpr int channels_per_ib = 64;
0241
0242 int k = towerIndex / channels_per_sector;
0243
0244 int sector = (k % 2) ? (k - 1) / 2 : (k / 2) + 32;
0245 int ib = (towerIndex % channels_per_sector) / channels_per_ib;
0246
0247 return std::make_pair(sector, ib);
0248 }
0249
0250 unsigned int TowerInfoDefs::encode_epd(const unsigned int towerIndex)
0251 {
0252 constexpr unsigned int channels_per_sector = 31;
0253 constexpr unsigned int supersector = channels_per_sector * 12;
0254 unsigned int supersectornumber = towerIndex / supersector;
0255 unsigned int sector = ((towerIndex % supersector)) / channels_per_sector;
0256 unsigned int channel = ((towerIndex % supersector)) % channels_per_sector;
0257 unsigned int key = channel + (sector << 5U) + (supersectornumber << 9U);
0258 return key;
0259 }
0260
0261
0262 unsigned int TowerInfoDefs::encode_epd(const unsigned int arm, const unsigned int rbin, const unsigned int phibin)
0263 {
0264 if (rbin == 0 && phibin > 11)
0265 {
0266 std::cout << __PRETTY_FUNCTION__ << " encode_epd invalid phibin value: " << phibin << " where max valid phibin is 11" << std::endl;
0267 exit(1);
0268 }
0269
0270 unsigned int sector = phibin / 2;
0271 if (rbin == 0)
0272 {
0273 sector = phibin;
0274 }
0275
0276 int channel = 0;
0277 if (rbin != 0)
0278 {
0279 channel = epdchnlmap[rbin][phibin - 2 * sector];
0280 }
0281
0282 unsigned int key = channel + (sector << 5U) + (arm << 9U);
0283 return key;
0284 }
0285
0286 unsigned int TowerInfoDefs::decode_epd(const unsigned int tower_key)
0287 {
0288 int channels_per_sector = 31;
0289 int supersector = channels_per_sector * 12;
0290 unsigned int ns_sector = tower_key >> 9U;
0291 unsigned int sector = (tower_key - (ns_sector << 9U)) >> 5U;
0292 unsigned int channel = tower_key - (ns_sector << 9U) - (sector << 5U);
0293 unsigned int index = ns_sector * supersector + sector * channels_per_sector + channel;
0294 return index;
0295 }
0296
0297
0298 unsigned int TowerInfoDefs::get_epd_arm(unsigned int key)
0299 {
0300 unsigned int arm = key >> 9U;
0301 return arm;
0302 }
0303
0304 unsigned int TowerInfoDefs::get_epd_sector(unsigned int key)
0305 {
0306 unsigned int arm = get_epd_arm(key);
0307 unsigned int sector = (key - (arm << 9U)) >> 5U;
0308 return sector;
0309 }
0310
0311 unsigned int TowerInfoDefs::get_epd_rbin(unsigned int key)
0312 {
0313 unsigned int arm = get_epd_arm(key);
0314 unsigned int sector = get_epd_sector(key);
0315 unsigned int channel = key - (sector << 5U) - (arm << 9U);
0316 unsigned int rbin = epd_rmap[channel];
0317 return rbin;
0318 }
0319
0320 unsigned int TowerInfoDefs::get_epd_phibin(unsigned int key)
0321 {
0322 int flip[24] = {1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1};
0323 unsigned int arm = get_epd_arm(key);
0324 unsigned int rbin = get_epd_rbin(key);
0325 unsigned int sector = get_epd_sector(key);
0326 unsigned int channel = key - (sector << 5U) - (arm << 9U);
0327 unsigned int phibin = epd_phimap[channel] + 2 * sector;
0328
0329 if (arm == 1)
0330 {
0331 phibin = phibin + flip[phibin];
0332 }
0333
0334 if (rbin == 0)
0335 {
0336 phibin = sector;
0337 }
0338
0339 return phibin;
0340 }
0341
0342 unsigned int TowerInfoDefs::encode_zdc(const unsigned int towerIndex)
0343 {
0344 if (towerIndex > 51)
0345 {
0346 std::cout << "Attempting to access zdc channel with invalid number " << towerIndex << std::endl;
0347 exit(1);
0348 }
0349 unsigned int key = towerIndex;
0350 return key;
0351 }
0352
0353 unsigned int TowerInfoDefs::decode_zdc(const unsigned int key)
0354 {
0355 unsigned int index = 999;
0356 for (unsigned int i = 0; i < 52; i++)
0357 {
0358 if (encode_zdc(i) == key)
0359 {
0360 index = i;
0361 break;
0362 }
0363 }
0364 return index;
0365 }
0366
0367 bool TowerInfoDefs::isZDC(const unsigned int towerIndex)
0368 {
0369 bool is_zdc = false;
0370
0371 if (towerIndex < 16)
0372 {
0373 is_zdc = true;
0374 }
0375 return is_zdc;
0376 }
0377
0378
0379 int TowerInfoDefs::get_zdc_side(const unsigned int key)
0380 {
0381 if (key & 8U)
0382 {
0383 return 1;
0384 }
0385 return 0;
0386 }
0387
0388 bool TowerInfoDefs::isSMD(const unsigned int towerIndex)
0389 {
0390 bool is_smd = false;
0391
0392 if ((towerIndex > 17 && towerIndex < 34) || (towerIndex > 35 && towerIndex < 52))
0393 {
0394 is_smd = true;
0395 }
0396 return is_smd;
0397 }
0398
0399
0400 int TowerInfoDefs::get_smd_side(const unsigned int key)
0401 {
0402 if (key < 34)
0403 {
0404 return 1;
0405 }
0406 return 0;
0407 }
0408
0409 bool TowerInfoDefs::isVeto(const unsigned int towerIndex)
0410 {
0411 bool is_veto = false;
0412
0413 if ((towerIndex > 15 && towerIndex < 18) || (towerIndex > 33 && towerIndex < 36))
0414 {
0415 is_veto = true;
0416 }
0417 return is_veto;
0418 }
0419
0420 int TowerInfoDefs::get_veto_side(const unsigned int key)
0421 {
0422 if (key & 2U)
0423 {
0424 return 0;
0425 }
0426 return 1;
0427 }
0428
0429
0430 unsigned int TowerInfoDefs::encode_mbd(const unsigned int pmtIndex)
0431 {
0432 unsigned int arm = pmtIndex / 128;
0433 unsigned int type = (pmtIndex % 16) / 8;
0434 unsigned int channel = (pmtIndex % 8) + ((pmtIndex / 16) * 8);
0435 if (channel > 63)
0436 {
0437 channel -= 64;
0438 }
0439
0440 unsigned int key = (arm << 7U) | (type << 6U) | channel;
0441
0442 return key;
0443 }
0444
0445 unsigned int TowerInfoDefs::decode_mbd(const unsigned int key)
0446 {
0447 unsigned int arm = (key >> 7U) & 0x1U;
0448 unsigned int type = (key >> 6U) & 0x1U;
0449 unsigned int channel = key & 0x3fU;
0450
0451 unsigned int index = (arm * 128) + (type * 8) + (channel % 8) + (channel / 8) * 16;
0452
0453 return index;
0454 }
0455
0456 unsigned int TowerInfoDefs::get_mbd_arm(const unsigned int key)
0457 {
0458 return (key >> 7U) & 0x1U;
0459 }
0460
0461 unsigned int TowerInfoDefs::get_mbd_side(const unsigned int key)
0462 {
0463 return get_mbd_arm(key);
0464 }
0465
0466 unsigned int TowerInfoDefs::get_mbd_type(const unsigned int key)
0467 {
0468 return (key >> 6U) & 0x1U;
0469 }
0470
0471 unsigned int TowerInfoDefs::get_mbd_channel(const unsigned int key)
0472 {
0473 return key & 0x3fU;
0474 }
0475
0476
0477 RawTowerDefs::keytype TowerInfoDefs::get_emcal_geokey_at_channel(const unsigned int towerIndex)
0478 {
0479 unsigned int towerkey = encode_emcal(towerIndex);
0480 unsigned int etabin = getCaloTowerEtaBin(towerkey);
0481 unsigned int phibin = getCaloTowerPhiBin(towerkey);
0482 const RawTowerDefs::keytype key = RawTowerDefs::encode_towerid(RawTowerDefs::CalorimeterId::CEMC, etabin, phibin);
0483 return key;
0484 }
0485
0486
0487 RawTowerDefs::keytype TowerInfoDefs::get_hcalin_geokey_at_channel(const unsigned int towerIndex)
0488 {
0489 unsigned int towerkey = encode_hcal(towerIndex);
0490 unsigned int etabin = getCaloTowerEtaBin(towerkey);
0491 unsigned int phibin = getCaloTowerPhiBin(towerkey);
0492 const RawTowerDefs::keytype key = RawTowerDefs::encode_towerid(RawTowerDefs::CalorimeterId::HCALIN, etabin, phibin);
0493 return key;
0494 }
0495
0496
0497 RawTowerDefs::keytype TowerInfoDefs::get_hcalout_geokey_at_channel(const unsigned int towerIndex)
0498 {
0499 unsigned int towerkey = encode_hcal(towerIndex);
0500 unsigned int etabin = getCaloTowerEtaBin(towerkey);
0501 unsigned int phibin = getCaloTowerPhiBin(towerkey);
0502 const RawTowerDefs::keytype key = RawTowerDefs::encode_towerid(RawTowerDefs::CalorimeterId::HCALOUT, etabin, phibin);
0503 return key;
0504 }
0505
0506 #pragma GCC diagnostic pop