Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:36:59

0001 #include "L1Trigger/L1TMuonEndCap/interface/PtAssignmentEngineAux2017.h"
0002 
0003 #include <iostream>
0004 
0005 // From here down, exact copy of code used for training BDT: EMTFPtAssign2017/src/PtLUTVarCalc.cc
0006 
0007 // Arrays that map the integer dPhi --> dPhi-units. 1/60th of a degree per unit; 255 units --> 4.25 degrees, 511 --> 8.52 degrees
0008 
0009 // 256 max units----
0010 // For use in dPhi34 in mode 15.  Derived manually from dPhiNLBMap_5bit_256Max for now; should generate algorithmically. - AWB 17.03.17
0011 static const int dPhiNLBMap_4bit_256Max[16] = {0, 1, 2, 3, 4, 6, 8, 10, 12, 16, 20, 25, 31, 46, 68, 136};
0012 
0013 // For use in dPhi23, dPhi24, and dPhi34 in 3- and 4-station modes (7, 11, 13, 14, 15), except for dPhi23 in mode 7 and dPhi34 in mode 15
0014 static const int dPhiNLBMap_5bit_256Max[32] = {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15,
0015                                                16, 17, 19, 20, 21, 23, 25, 28, 31, 34, 39, 46, 55, 68, 91, 136};
0016 // 512 max units----
0017 // For use in all dPhiAB (where "A" and "B" are the first two stations in the track) in all modes
0018 static const int dPhiNLBMap_7bit_512Max[128] = {
0019     0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,
0020     22,  23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,
0021     44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,
0022     66,  67,  68,  69,  71,  72,  73,  74,  75,  76,  77,  79,  80,  81,  83,  84,  86,  87,  89,  91,  92,  94,
0023     96,  98,  100, 102, 105, 107, 110, 112, 115, 118, 121, 124, 127, 131, 135, 138, 143, 147, 152, 157, 162, 168,
0024     174, 181, 188, 196, 204, 214, 224, 235, 247, 261, 276, 294, 313, 336, 361, 391, 427, 470};
0025 
0026 int PtAssignmentEngineAux2017::getNLBdPhi(int dPhi, int bits, int max) const {
0027   emtf_assert((bits == 4 && max == 256) || (bits == 5 && max == 256) || (bits == 7 && max == 512));
0028 
0029   int dPhi_ = max;
0030   int sign_ = 1;
0031   if (dPhi < 0)
0032     sign_ = -1;
0033   dPhi = sign_ * dPhi;
0034 
0035   if (max == 256) {
0036     if (bits == 4) {
0037       dPhi_ = dPhiNLBMap_4bit_256Max[(1 << bits) - 1];
0038       for (int edge = 0; edge < (1 << bits) - 1; edge++) {
0039         if (dPhiNLBMap_4bit_256Max[edge] <= dPhi && dPhiNLBMap_4bit_256Max[edge + 1] > dPhi) {
0040           dPhi_ = dPhiNLBMap_4bit_256Max[edge];
0041           break;
0042         }
0043       }
0044     }  // End conditional: if (bits == 4)
0045     if (bits == 5) {
0046       dPhi_ = dPhiNLBMap_5bit_256Max[(1 << bits) - 1];
0047       for (int edge = 0; edge < (1 << bits) - 1; edge++) {
0048         if (dPhiNLBMap_5bit_256Max[edge] <= dPhi && dPhiNLBMap_5bit_256Max[edge + 1] > dPhi) {
0049           dPhi_ = dPhiNLBMap_5bit_256Max[edge];
0050           break;
0051         }
0052       }
0053     }  // End conditional: if (bits == 5)
0054   }  // End conditional: if (max == 256)
0055 
0056   else if (max == 512) {
0057     if (bits == 7) {
0058       dPhi_ = dPhiNLBMap_7bit_512Max[(1 << bits) - 1];
0059       for (int edge = 0; edge < (1 << bits) - 1; edge++) {
0060         if (dPhiNLBMap_7bit_512Max[edge] <= dPhi && dPhiNLBMap_7bit_512Max[edge + 1] > dPhi) {
0061           dPhi_ = dPhiNLBMap_7bit_512Max[edge];
0062           break;
0063         }
0064       }
0065     }  // End conditional: if (bits == 7)
0066   }  // End conditional: else if (max == 512)
0067 
0068   emtf_assert(abs(sign_) == 1 && dPhi_ >= 0 && dPhi_ < max);
0069   return (sign_ * dPhi_);
0070 }  // End function: int PtAssignmentEngineAux2017::getNLBdPhi()
0071 
0072 int PtAssignmentEngineAux2017::getNLBdPhiBin(int dPhi, int bits, int max) const {
0073   emtf_assert((bits == 4 && max == 256) || (bits == 5 && max == 256) || (bits == 7 && max == 512));
0074 
0075   int dPhiBin_ = (1 << bits) - 1;
0076   int sign_ = 1;
0077   if (dPhi < 0)
0078     sign_ = -1;
0079   dPhi = sign_ * dPhi;
0080 
0081   if (max == 256) {
0082     if (bits == 4) {
0083       for (int edge = 0; edge < (1 << bits) - 1; edge++) {
0084         if (dPhiNLBMap_4bit_256Max[edge] <= dPhi && dPhiNLBMap_4bit_256Max[edge + 1] > dPhi) {
0085           dPhiBin_ = edge;
0086           break;
0087         }
0088       }
0089     }  // End conditional: if (bits == 4)
0090     if (bits == 5) {
0091       for (int edge = 0; edge < (1 << bits) - 1; edge++) {
0092         if (dPhiNLBMap_5bit_256Max[edge] <= dPhi && dPhiNLBMap_5bit_256Max[edge + 1] > dPhi) {
0093           dPhiBin_ = edge;
0094           break;
0095         }
0096       }
0097     }  // End conditional: if (bits == 5)
0098   }  // End conditional: if (max == 256)
0099 
0100   else if (max == 512) {
0101     if (bits == 7) {
0102       for (int edge = 0; edge < (1 << bits) - 1; edge++) {
0103         if (dPhiNLBMap_7bit_512Max[edge] <= dPhi && dPhiNLBMap_7bit_512Max[edge + 1] > dPhi) {
0104           dPhiBin_ = edge;
0105           break;
0106         }
0107       }
0108     }  // End conditional: if (bits == 7)
0109   }  // End conditional: else if (max == 512)
0110 
0111   emtf_assert(dPhiBin_ >= 0 && dPhiBin_ < pow(2, bits));
0112   return (dPhiBin_);
0113 }  // End function: int PtAssignmentEngineAux2017::getNLBdPhiBin()
0114 
0115 int PtAssignmentEngineAux2017::getdPhiFromBin(int dPhiBin, int bits, int max) const {
0116   emtf_assert((bits == 4 && max == 256) || (bits == 5 && max == 256) || (bits == 7 && max == 512));
0117 
0118   int dPhi_ = (1 << bits) - 1;
0119 
0120   if (dPhiBin > (1 << bits) - 1)
0121     dPhiBin = (1 << bits) - 1;
0122 
0123   if (max == 256) {
0124     if (bits == 4)
0125       dPhi_ = dPhiNLBMap_4bit_256Max[dPhiBin];
0126     if (bits == 5)
0127       dPhi_ = dPhiNLBMap_5bit_256Max[dPhiBin];
0128   }  // End conditional: if (max == 256)
0129 
0130   else if (max == 512) {
0131     if (bits == 7)
0132       dPhi_ = dPhiNLBMap_7bit_512Max[dPhiBin];
0133   }  // End conditional: else if (max == 512)
0134 
0135   emtf_assert(dPhi_ >= 0 && dPhi_ < max);
0136   return (dPhi_);
0137 }  // End function: int PtAssignmentEngineAux2017::getdPhiFromBin()
0138 
0139 int PtAssignmentEngineAux2017::getCLCT(int clct, int endcap, int dPhiSign, int bits) const {
0140   // std::cout << "Inside getCLCT: clct = " << clct << ", endcap = " << endcap
0141   //             << ", dPhiSign = " << dPhiSign << ", bits = " << bits << std::endl;
0142 
0143   emtf_assert(clct >= 0 && clct <= 10 && abs(endcap) == 1 && abs(dPhiSign) == 1 && (bits == 2 || bits == 3));
0144 
0145   // Convention here: endcap == +/-1, dPhiSign = +/-1.
0146   int clct_ = 0;
0147   int sign_ = -1 * endcap * dPhiSign;  // CLCT bend is with dPhi in ME-, opposite in ME+
0148 
0149   // CLCT pattern can be converted into |bend| x sign as follows:
0150   // |bend| = (10 + (pattern % 2) - pattern) / 2
0151   //   * 10 --> 0, 9/8 --> 1, 7/6 --> 2, 5/4 --> 3, 3/2 --> 4, 0 indicates RPC hit
0152   //  sign  = ((pattern % 2) == 1 ? -1 : 1) * (endcap == 1 ? -1 : 1)
0153   //   * In ME+, even CLCTs have negative sign, odd CLCTs have positive
0154 
0155   // For use in all 3- and 4-station modes (7, 11, 13, 14, 15)
0156   // Bends [-4, -3, -2] --> 0, [-1, 0] --> 1, [+1] --> 2, [+2, +3, +4] --> 3
0157   if (bits == 2) {
0158     switch (clct) {
0159       case 10:
0160         clct_ = 1;
0161         break;
0162       case 9:
0163         clct_ = (sign_ > 0 ? 1 : 2);
0164         break;
0165       case 8:
0166         clct_ = (sign_ > 0 ? 2 : 1);
0167         break;
0168       case 7:
0169         clct_ = (sign_ > 0 ? 0 : 3);
0170         break;
0171       case 6:
0172         clct_ = (sign_ > 0 ? 3 : 0);
0173         break;
0174       case 5:
0175         clct_ = (sign_ > 0 ? 0 : 3);
0176         break;
0177       case 4:
0178         clct_ = (sign_ > 0 ? 3 : 0);
0179         break;
0180       case 3:
0181         clct_ = (sign_ > 0 ? 0 : 3);
0182         break;
0183       case 2:
0184         clct_ = (sign_ > 0 ? 3 : 0);
0185         break;
0186       case 1:
0187         clct_ = (sign_ > 0 ? 0 : 3);
0188         break;
0189       case 0:
0190         clct_ = 0;
0191         break;
0192       default:
0193         clct_ = 1;
0194         break;
0195     }
0196   }  // End conditional: if (bits == 2)
0197 
0198   // For use in all 2-station modes (3, 5, 6, 9, 10, 12)
0199   // Bends [isRPC] --> 0, [-4, -3] --> 1, [-2] --> 2, [-1] --> 3, [0] --> 4, [+1] --> 5, [+2] --> 6, [+3, +4] --> 7
0200   else if (bits == 3) {
0201     switch (clct) {
0202       case 10:
0203         clct_ = 4;
0204         break;
0205       case 9:
0206         clct_ = (sign_ > 0 ? 3 : 5);
0207         break;
0208       case 8:
0209         clct_ = (sign_ > 0 ? 5 : 3);
0210         break;
0211       case 7:
0212         clct_ = (sign_ > 0 ? 2 : 6);
0213         break;
0214       case 6:
0215         clct_ = (sign_ > 0 ? 6 : 2);
0216         break;
0217       case 5:
0218         clct_ = (sign_ > 0 ? 1 : 7);
0219         break;
0220       case 4:
0221         clct_ = (sign_ > 0 ? 7 : 1);
0222         break;
0223       case 3:
0224         clct_ = (sign_ > 0 ? 1 : 7);
0225         break;
0226       case 2:
0227         clct_ = (sign_ > 0 ? 7 : 1);
0228         break;
0229       case 1:
0230         clct_ = (sign_ > 0 ? 1 : 7);
0231         break;
0232       case 0:
0233         clct_ = 0;
0234         break;
0235       default:
0236         clct_ = 4;
0237         break;
0238     }
0239   }  // End conditional: else if (bits == 3)
0240 
0241   // std::cout << "  * Output clct_ = " << clct_ << std::endl;
0242 
0243   emtf_assert(clct_ >= 0 && clct_ < pow(2, bits));
0244   return clct_;
0245 }  // End function: int PtAssignmentEngineAux2017::getCLCT()
0246 
0247 int PtAssignmentEngineAux2017::unpackCLCT(int clct, int endcap, int dPhiSign, int bits) const {
0248   // std::cout << "Inside unpackCLCT: clct = " << clct << ", endcap = " << endcap
0249   //             << ", dPhiSign = " << dPhiSign << ", bits = " << bits << std::endl;
0250 
0251   emtf_assert(bits == 2 || bits == 3);
0252   emtf_assert(clct >= 0 && clct < pow(2, bits));
0253   emtf_assert(abs(dPhiSign) == 1);
0254 
0255   // Convention here: endcap == +/-1, dPhiSign = +/-1.
0256   int clct_ = -1;
0257   int sign_ = -1 * endcap * dPhiSign;  // CLCT bend is with dPhi in ME-, opposite in ME+
0258 
0259   if (bits == 2) {
0260     switch (clct) {
0261       case 1:
0262         clct_ = 10;
0263         break;
0264       case 2:
0265         clct_ = (sign_ > 0 ? 8 : 9);
0266         break;
0267       case 3:
0268         clct_ = (sign_ > 0 ? 4 : 5);
0269         break;
0270       case 0:
0271         clct_ = 0;
0272         break;
0273       default:
0274         break;
0275     }
0276   } else if (bits == 3) {
0277     switch (clct) {
0278       case 4:
0279         clct_ = 10;
0280         break;
0281       case 5:
0282         clct_ = (sign_ > 0 ? 8 : 9);
0283         break;
0284       case 3:
0285         clct_ = (sign_ > 0 ? 9 : 8);
0286         break;
0287       case 6:
0288         clct_ = (sign_ > 0 ? 6 : 7);
0289         break;
0290       case 2:
0291         clct_ = (sign_ > 0 ? 7 : 6);
0292         break;
0293       case 7:
0294         clct_ = (sign_ > 0 ? 4 : 5);
0295         break;
0296       case 1:
0297         clct_ = (sign_ > 0 ? 5 : 4);
0298         break;
0299       case 0:
0300         clct_ = 0;
0301         break;
0302       default:
0303         break;
0304     }
0305   }
0306 
0307   // std::cout << "  * Output clct_ = " << clct_ << std::endl;
0308 
0309   emtf_assert(clct_ >= 0 && clct_ <= 10);
0310   return clct_;
0311 }  // End function: int PtAssignmentEngineAux2017::unpackCLCT()
0312 
0313 int PtAssignmentEngineAux2017::getdTheta(int dTheta, int bits) const {
0314   emtf_assert(bits == 2 || bits == 3);
0315 
0316   int dTheta_ = -99;
0317 
0318   // For use in mode 15
0319   if (bits == 2) {
0320     if (abs(dTheta) <= 1)
0321       dTheta_ = 2;
0322     else if (abs(dTheta) <= 2)
0323       dTheta_ = 1;
0324     else if (dTheta <= -3)
0325       dTheta_ = 0;
0326     else
0327       dTheta_ = 3;
0328   }  // End conditional: if (bits == 2)
0329 
0330   // For use in all 2- and 3-station modes (all modes except 15)
0331   else if (bits == 3) {
0332     if (dTheta <= -4)
0333       dTheta_ = 0;
0334     else if (dTheta == -3)
0335       dTheta_ = 1;
0336     else if (dTheta == -2)
0337       dTheta_ = 2;
0338     else if (dTheta == -1)
0339       dTheta_ = 3;
0340     else if (dTheta == 0)
0341       dTheta_ = 4;
0342     else if (dTheta == +1)
0343       dTheta_ = 5;
0344     else if (dTheta == +2)
0345       dTheta_ = 6;
0346     else
0347       dTheta_ = 7;
0348   }  // End conditional: if (bits == 3)
0349 
0350   emtf_assert(dTheta_ >= 0 && dTheta_ < pow(2, bits));
0351   return (dTheta_);
0352 }  // End function: int PtAssignmentEngineAux2017::getdTheta()
0353 
0354 int PtAssignmentEngineAux2017::unpackdTheta(int dTheta, int bits) const {
0355   emtf_assert(bits == 2 || bits == 3);
0356 
0357   int dTheta_ = -99;
0358 
0359   if (bits == 2) {  // For use in mode 15
0360     switch (dTheta) {
0361       case 2:
0362         dTheta_ = 0;
0363         break;
0364       case 1:
0365         dTheta_ = -2;
0366         break;
0367       case 0:
0368         dTheta_ = -3;
0369         break;
0370       case 3:
0371         dTheta_ = 3;
0372         break;
0373       default:
0374         break;
0375     }
0376   } else if (bits == 3) {  // For use in all 2- and 3-station modes (all modes except 15)
0377     switch (dTheta) {
0378       case 0:
0379         dTheta_ = -4;
0380         break;
0381       case 1:
0382         dTheta_ = -3;
0383         break;
0384       case 2:
0385         dTheta_ = -2;
0386         break;
0387       case 3:
0388         dTheta_ = -1;
0389         break;
0390       case 4:
0391         dTheta_ = 0;
0392         break;
0393       case 5:
0394         dTheta_ = 1;
0395         break;
0396       case 6:
0397         dTheta_ = 2;
0398         break;
0399       case 7:
0400         dTheta_ = 3;
0401         break;
0402       default:
0403         break;
0404     }
0405   }
0406 
0407   emtf_assert(dTheta_ >= -4 && dTheta_ <= 3);
0408   return (dTheta_);
0409 }  // End function: int PtAssignmentEngineAux2017::unpackdTheta(int dTheta, int bits)
0410 
0411 int PtAssignmentEngineAux2017::getTheta(int theta, int st1_ring2, int bits) const {
0412   emtf_assert(theta >= 5 && theta < 128 && (st1_ring2 == 0 || st1_ring2 == 1) && (bits == 4 || bits == 5));
0413 
0414   int theta_ = -99;
0415 
0416   // For use in mode 15
0417   if (bits == 4) {
0418     if (st1_ring2 == 0) {
0419       // Should rarely fail ... should change to using ME1 for track theta - AWB 05.06.17
0420       if (theta > 52) {
0421         // std::cout << "\n\n*** Bizzare case of mode 15 track with ME1/1 LCT and track theta = " << theta << std::endl;
0422       }
0423       theta_ = (std::min(std::max(theta, 5), 52) - 5) / 6;
0424     } else if (st1_ring2 == 1) {
0425       // Should rarely fail ... should change to using ME1 for track theta - AWB 05.06.17
0426       if (theta < 46 || theta > 87) {
0427         // std::cout << "\n\n*** Bizzare case of mode 15 track with ME1/2 LCT and track theta = " << theta << std::endl;
0428       }
0429       theta_ = ((std::min(std::max(theta, 46), 87) - 46) / 7) + 8;
0430     }
0431   }  // End conditional: if (bits == 4)
0432 
0433   // For use in all 2- and 3-station modes (all modes except 15)
0434   else if (bits == 5) {
0435     if (st1_ring2 == 0) {
0436       theta_ = (std::max(theta, 1) - 1) / 4;
0437     } else if (st1_ring2 == 1) {
0438       theta_ = ((std::min(theta, 104) - 1) / 4) + 6;
0439     }
0440   }  // End conditional: else if (bits == 5)
0441 
0442   emtf_assert(theta_ >= 0 && ((bits == 4 && theta_ <= 13) || (bits == 5 && theta_ < pow(2, bits))));
0443   return (theta_);
0444 }  // End function: int PtAssignmentEngineAux2017::getTheta()
0445 
0446 void PtAssignmentEngineAux2017::unpackTheta(int& theta, int& st1_ring2, int bits) const {
0447   emtf_assert(bits == 4 || bits == 5);
0448   emtf_assert(theta >= 0 && theta < pow(2, bits));
0449 
0450   // For use in mode 15
0451   if (bits == 4) {
0452     if (theta < 8) {
0453       st1_ring2 = 0;
0454       theta = (theta * 6) + 5;
0455     } else {
0456       st1_ring2 = 1;
0457       theta = ((theta - 8) * 7) + 46;
0458     }
0459   } else if (bits == 5) {
0460     if (theta < 15) {
0461       st1_ring2 = 0;
0462       theta = (theta * 4) + 1;
0463     } else {
0464       st1_ring2 = 1;
0465       theta = ((theta - 6) * 4) + 1;
0466     }
0467   }
0468 
0469   emtf_assert(theta >= 5 && theta <= 104);
0470 
0471 }  // End function: void PtAssignmentEngineAux2017::unpackTheta()
0472 
0473 int PtAssignmentEngineAux2017::unpackSt1Ring2(int theta, int bits) const {
0474   emtf_assert(bits == 4 || bits == 5);
0475   emtf_assert(theta >= 0 && theta < pow(2, bits));
0476 
0477   // For use in mode 15
0478   if (bits == 4) {
0479     if (theta < 6)
0480       return 0;
0481     else
0482       return 1;
0483   } else {
0484     if (theta < 15)
0485       return 0;
0486     else
0487       return 1;
0488   }
0489 
0490 }  // End function: void PtAssignmentEngineAux2017::unpackSt1Ring2()
0491 
0492 int PtAssignmentEngineAux2017::get2bRPC(int clctA, int clctB, int clctC) const {
0493   int rpc_2b = -99;
0494 
0495   if (clctA == 0)
0496     rpc_2b = 0;
0497   else if (clctC == 0)
0498     rpc_2b = 1;
0499   else if (clctB == 0)
0500     rpc_2b = 2;
0501   else
0502     rpc_2b = 3;
0503 
0504   emtf_assert(rpc_2b >= 0 && rpc_2b < 4);
0505   return (rpc_2b);
0506 }  // End function: int PtAssignmentEngineAux2017::get2bRPC()
0507 
0508 void PtAssignmentEngineAux2017::unpack2bRPC(int rpc_2b, int& rpcA, int& rpcB, int& rpcC) const {
0509   emtf_assert(rpc_2b >= 0 && rpc_2b < 4);
0510 
0511   rpcA = 0;
0512   rpcB = 0;
0513   rpcC = 0;
0514 
0515   if (rpc_2b == 0)
0516     rpcA = 1;
0517   else if (rpc_2b == 1)
0518     rpcC = 1;
0519   else if (rpc_2b == 2)
0520     rpcB = 1;
0521 
0522 }  // End function: int PtAssignmentEngineAux2017::unpack2bRPC()
0523 
0524 int PtAssignmentEngineAux2017::get8bMode15(
0525     int theta, int st1_ring2, int endcap, int sPhiAB, int clctA, int clctB, int clctC, int clctD) const {
0526   // std::cout << "Inside get8bMode15, theta = " << theta << ", st1_ring2 = " << st1_ring2 << ", endcap = " << endcap << ", sPhiAB = " << sPhiAB
0527   //             << ", clctA = " << clctA << ", clctB = " << clctB << ", clctC = " << clctC << ", clctD = " << clctD << std::endl;
0528 
0529   if (st1_ring2)
0530     theta = (std::min(std::max(theta, 46), 87) - 46) / 7;
0531   else
0532     theta = (std::min(std::max(theta, 5), 52) - 5) / 6;
0533 
0534   emtf_assert(theta >= 0 && theta < 10);
0535 
0536   int clctA_2b = getCLCT(clctA, endcap, sPhiAB, 2);
0537 
0538   int nRPC = (clctA == 0) + (clctB == 0) + (clctC == 0) + (clctD == 0);
0539   int rpc_word, rpc_clct, mode15_8b;
0540 
0541   if (st1_ring2) {
0542     if (nRPC >= 2 && clctA == 0 && clctB == 0)
0543       rpc_word = 0;
0544     else if (nRPC >= 2 && clctA == 0 && clctC == 0)
0545       rpc_word = 1;
0546     else if (nRPC >= 2 && clctA == 0 && clctD == 0)
0547       rpc_word = 2;
0548     else if (nRPC == 1 && clctA == 0)
0549       rpc_word = 3;
0550     else if (nRPC >= 2 && clctD == 0 && clctB == 0)
0551       rpc_word = 4;
0552     else if (nRPC >= 2 && clctD == 0 && clctC == 0)
0553       rpc_word = 8;
0554     else if (nRPC >= 2 && clctB == 0 && clctC == 0)
0555       rpc_word = 12;
0556     else if (nRPC == 1 && clctD == 0)
0557       rpc_word = 16;
0558     else if (nRPC == 1 && clctB == 0)
0559       rpc_word = 20;
0560     else if (nRPC == 1 && clctC == 0)
0561       rpc_word = 24;
0562     else
0563       rpc_word = 28;
0564     rpc_clct = rpc_word + clctA_2b;
0565     mode15_8b = (theta * 32) + rpc_clct + 64;
0566   } else {
0567     if (theta >= 4 && clctD == 0)
0568       rpc_word = 0;
0569     else if (theta >= 4 && clctC == 0)
0570       rpc_word = 1;
0571     else if (theta >= 4)
0572       rpc_word = 2;
0573     else
0574       rpc_word = 3;
0575     rpc_clct = rpc_word * 4 + clctA_2b;
0576     mode15_8b = ((theta % 4) * 16) + rpc_clct;
0577   }
0578 
0579   // std::cout << "  * Output mode15_8b = " << mode15_8b << std::endl;
0580 
0581   emtf_assert(mode15_8b >= 0 && mode15_8b < pow(2, 8));
0582   return (mode15_8b);
0583 
0584 }  // End function: int PtAssignmentEngineAux2017::get8bMode15()
0585 
0586 void PtAssignmentEngineAux2017::unpack8bMode15(int mode15_8b,
0587                                                int& theta,
0588                                                int& st1_ring2,
0589                                                int endcap,
0590                                                int sPhiAB,
0591                                                int& clctA,
0592                                                int& rpcA,
0593                                                int& rpcB,
0594                                                int& rpcC,
0595                                                int& rpcD) const {
0596   // std::cout << "Inside unpack8bMode15, mode15_8b = " << mode15_8b << ", theta = " << theta
0597   //             << ", st1_ring2 = " << st1_ring2  << ", endcap = " << endcap << ", sPhiAB = " << sPhiAB << ", clctA = " << clctA
0598   //             << ", rpcA = " << rpcA << ", rpcB = " << rpcB << ", rpcC = " << rpcC << ", rpcD = " << rpcD << std::endl;
0599 
0600   emtf_assert(mode15_8b >= 0 && mode15_8b < pow(2, 8));
0601   emtf_assert(abs(endcap) == 1 && abs(sPhiAB) == 1);
0602 
0603   rpcA = 0;
0604   rpcB = 0;
0605   rpcC = 0;
0606   rpcD = 0;
0607 
0608   if (mode15_8b >= 64)
0609     st1_ring2 = 1;
0610   else
0611     st1_ring2 = 0;
0612 
0613   int rpc_clct, rpc_word, clctA_2b, nRPC = -1;
0614 
0615   if (st1_ring2) {
0616     rpc_clct = (mode15_8b % 32);
0617     theta = (mode15_8b - 64 - rpc_clct) / 32;
0618     theta += 8;
0619 
0620     if (rpc_clct < 4)
0621       clctA_2b = 0;
0622     else
0623       clctA_2b = (rpc_clct % 4);
0624     rpc_word = rpc_clct - clctA_2b;
0625 
0626     // if (clctA_2b != 0) clctA = unpackCLCT(clctA_2b, endcap, sPhiAB, 2);
0627     clctA = clctA_2b;
0628 
0629     switch (rpc_word) {
0630       case 0:
0631         nRPC = 2;
0632         rpcA = 1;
0633         rpcB = 1;
0634         break;
0635       case 1:
0636         nRPC = 2;
0637         rpcA = 1;
0638         rpcC = 1;
0639         break;
0640       case 2:
0641         nRPC = 2;
0642         rpcA = 1;
0643         rpcD = 1;
0644         break;
0645       case 3:
0646         nRPC = 1;
0647         rpcA = 1;
0648         break;
0649       case 4:
0650         nRPC = 2;
0651         rpcD = 1;
0652         rpcB = 1;
0653         break;
0654       case 8:
0655         nRPC = 2;
0656         rpcD = 1;
0657         rpcC = 1;
0658         break;
0659       case 12:
0660         nRPC = 2;
0661         rpcB = 1;
0662         rpcC = 1;
0663         break;
0664       case 16:
0665         nRPC = 1;
0666         rpcD = 1;
0667         break;
0668       case 20:
0669         nRPC = 1;
0670         rpcB = 1;
0671         break;
0672       case 24:
0673         nRPC = 1;
0674         rpcC = 1;
0675         break;
0676       case 28:
0677         nRPC = 0;
0678         break;
0679       default:
0680         break;
0681     }
0682   }  // End conditional: if (st1_ring2)
0683   else {
0684     rpc_clct = (mode15_8b % 16);
0685     theta = (mode15_8b - rpc_clct) / 16;
0686     clctA_2b = (rpc_clct % 4);
0687     rpc_word = (rpc_clct - clctA_2b) / 4;
0688 
0689     // if (clctA_2b != 0) clctA = unpackCLCT(clctA_2b, endcap, sPhiAB, 2);
0690     clctA = clctA_2b;
0691 
0692     switch (rpc_word) {
0693       case 0:
0694         nRPC = 1;
0695         theta += 4;
0696         rpcD = 1;
0697         break;
0698       case 1:
0699         nRPC = 1;
0700         theta += 4;
0701         rpcC = 1;
0702         break;
0703       case 2:
0704         nRPC = 0;
0705         theta += 4;
0706         break;
0707       case 3:
0708         nRPC = 0;
0709         break;
0710       default:
0711         break;
0712     }
0713   }
0714 
0715   // std::cout << "  * Output theta = " << theta << ", st1_ring2 = " << st1_ring2 << ", clctA = " << clctA
0716   //             << ", rpcA = " << rpcA << ", rpcB = " << rpcB << ", rpcC = " << rpcC << ", rpcD = " << rpcD << std::endl;
0717 
0718   emtf_assert(nRPC >= 0);
0719 
0720 }  // End function: void PtAssignmentEngineAux2017::unpack8bMode15()
0721 
0722 // _____________________________________________________________________________
0723 // From here down, code was originally in PtLUTVarCalc.cc
0724 
0725 int PtAssignmentEngineAux2017::calcTrackTheta(const int th1,
0726                                               const int th2,
0727                                               const int th3,
0728                                               const int th4,
0729                                               const int st1_ring2,
0730                                               const int mode,
0731                                               const bool BIT_COMP) const {
0732   int theta = -99;
0733 
0734   if ((mode % 8) / 4 > 0)  // Has station 2 hit
0735     theta = th2;
0736   else if ((mode % 4) / 2 > 0)  // Has station 3 hit
0737     theta = th3;
0738   else if ((mode % 2) > 0)  // Has station 4 hit
0739     theta = th4;
0740 
0741   emtf_assert(theta > 0);
0742 
0743   if (BIT_COMP) {
0744     int nBits = (mode == 15 ? 4 : 5);
0745     theta = getTheta(theta, st1_ring2, nBits);
0746   }
0747 
0748   return theta;
0749 }
0750 
0751 void PtAssignmentEngineAux2017::calcDeltaPhis(int& dPh12,
0752                                               int& dPh13,
0753                                               int& dPh14,
0754                                               int& dPh23,
0755                                               int& dPh24,
0756                                               int& dPh34,
0757                                               int& dPhSign,
0758                                               int& dPhSum4,
0759                                               int& dPhSum4A,
0760                                               int& dPhSum3,
0761                                               int& dPhSum3A,
0762                                               int& outStPh,
0763                                               const int ph1,
0764                                               const int ph2,
0765                                               const int ph3,
0766                                               const int ph4,
0767                                               const int mode,
0768                                               const bool BIT_COMP) const {
0769   dPh12 = ph2 - ph1;
0770   dPh13 = ph3 - ph1;
0771   dPh14 = ph4 - ph1;
0772   dPh23 = ph3 - ph2;
0773   dPh24 = ph4 - ph2;
0774   dPh34 = ph4 - ph3;
0775   dPhSign = 0;
0776 
0777   if (mode >= 8) {           // First hit is station 1
0778     if ((mode % 8) / 4 > 0)  // Has station 2 hit
0779       dPhSign = (dPh12 >= 0 ? +1 : -1);
0780     else if ((mode % 4) / 2 > 0)  // Has station 3 hit
0781       dPhSign = (dPh13 >= 0 ? +1 : -1);
0782     else if ((mode % 2) > 0)  // Has station 4 hit
0783       dPhSign = (dPh14 >= 0 ? +1 : -1);
0784   } else if ((mode % 8) / 4 > 0) {  // First hit is station 2
0785     if ((mode % 4) / 2 > 0)         // Has station 3 hit
0786       dPhSign = (dPh23 >= 0 ? +1 : -1);
0787     else if ((mode % 2) > 0)  // Has station 4 hit
0788       dPhSign = (dPh24 >= 0 ? +1 : -1);
0789   } else if ((mode % 4) / 2 > 0) {  // First hit is station 3
0790     if ((mode % 2) > 0)             // Has station 4 hit
0791       dPhSign = (dPh34 >= 0 ? +1 : -1);
0792   }
0793 
0794   emtf_assert(dPhSign != 0);
0795 
0796   dPh12 *= dPhSign;
0797   dPh13 *= dPhSign;
0798   dPh14 *= dPhSign;
0799   dPh23 *= dPhSign;
0800   dPh24 *= dPhSign;
0801   dPh34 *= dPhSign;
0802 
0803   if (BIT_COMP) {
0804     int nBitsA = 7;
0805     int nBitsB = 7;
0806     int nBitsC = 7;
0807     int maxA = 512;
0808     int maxB = 512;
0809     int maxC = 512;
0810 
0811     if (mode == 7 || mode == 11 || mode > 12) {
0812       nBitsB = 5;
0813       maxB = 256;
0814       nBitsC = 5;
0815       maxC = 256;
0816     }
0817     if (mode == 15) {
0818       nBitsC = 4;
0819       maxC = 256;
0820     }
0821 
0822     dPh12 = getNLBdPhi(dPh12, nBitsA, maxA);
0823     dPh13 = getNLBdPhi(dPh13, nBitsA, maxA);
0824     dPh14 = getNLBdPhi(dPh14, nBitsA, maxA);
0825     if (mode == 7)
0826       dPh23 = getNLBdPhi(dPh23, nBitsA, maxA);
0827     else
0828       dPh23 = getNLBdPhi(dPh23, nBitsB, maxB);
0829     dPh24 = getNLBdPhi(dPh24, nBitsB, maxB);
0830     dPh34 = getNLBdPhi(dPh34, nBitsC, maxC);
0831 
0832     // Some delta phi values must be computed from others
0833     switch (mode) {
0834       case 15:
0835         dPh13 = dPh12 + dPh23;
0836         dPh14 = dPh13 + dPh34;
0837         dPh24 = dPh23 + dPh34;
0838         break;
0839       case 14:
0840         dPh13 = dPh12 + dPh23;
0841         break;
0842       case 13:
0843         dPh14 = dPh12 + dPh24;
0844         break;
0845       case 11:
0846         dPh14 = dPh13 + dPh34;
0847         break;
0848       case 7:
0849         dPh24 = dPh23 + dPh34;
0850         break;
0851       default:
0852         break;
0853     }
0854 
0855   }  // End conditional: if (BIT_COMP)
0856 
0857   // Compute summed quantities
0858   if (mode == 15)
0859     calcDeltaPhiSums(dPhSum4, dPhSum4A, dPhSum3, dPhSum3A, outStPh, dPh12, dPh13, dPh14, dPh23, dPh24, dPh34);
0860 
0861 }  // End function: void PtAssignmentEngineAux2017::calcDeltaPhis()
0862 
0863 void PtAssignmentEngineAux2017::calcDeltaThetas(int& dTh12,
0864                                                 int& dTh13,
0865                                                 int& dTh14,
0866                                                 int& dTh23,
0867                                                 int& dTh24,
0868                                                 int& dTh34,
0869                                                 const int th1,
0870                                                 const int th2,
0871                                                 const int th3,
0872                                                 const int th4,
0873                                                 const int mode,
0874                                                 const bool BIT_COMP) const {
0875   dTh12 = th2 - th1;
0876   dTh13 = th3 - th1;
0877   dTh14 = th4 - th1;
0878   dTh23 = th3 - th2;
0879   dTh24 = th4 - th2;
0880   dTh34 = th4 - th3;
0881 
0882   if (BIT_COMP) {
0883     int nBits = (mode == 15 ? 2 : 3);
0884 
0885     dTh12 = getdTheta(dTh12, nBits);
0886     dTh13 = getdTheta(dTh13, nBits);
0887     dTh14 = getdTheta(dTh14, nBits);
0888     dTh23 = getdTheta(dTh23, nBits);
0889     dTh24 = getdTheta(dTh24, nBits);
0890     dTh34 = getdTheta(dTh34, nBits);
0891   }  // End conditional: if (BIT_COMP)
0892 
0893 }  // Enf function: void PtAssignmentEngineAux2017::calcDeltaThetas()
0894 
0895 void PtAssignmentEngineAux2017::calcBends(int& bend1,
0896                                           int& bend2,
0897                                           int& bend3,
0898                                           int& bend4,
0899                                           const int pat1,
0900                                           const int pat2,
0901                                           const int pat3,
0902                                           const int pat4,
0903                                           const int dPhSign,
0904                                           const int endcap,
0905                                           const int mode,
0906                                           const bool BIT_COMP) const {
0907   bend1 = calcBendFromPattern(pat1, endcap);
0908   bend2 = calcBendFromPattern(pat2, endcap);
0909   bend3 = calcBendFromPattern(pat3, endcap);
0910   bend4 = calcBendFromPattern(pat4, endcap);
0911 
0912   if (BIT_COMP) {
0913     int nBits = 3;
0914     if (mode == 7 || mode == 11 || mode > 12)
0915       nBits = 2;
0916 
0917     if (mode / 8 > 0)  // Has station 1 hit
0918       bend1 = getCLCT(pat1, endcap, dPhSign, nBits);
0919     if ((mode % 8) / 4 > 0)  // Has station 2 hit
0920       bend2 = getCLCT(pat2, endcap, dPhSign, nBits);
0921     if ((mode % 4) / 2 > 0)  // Has station 3 hit
0922       bend3 = getCLCT(pat3, endcap, dPhSign, nBits);
0923     if ((mode % 2) > 0)  // Has station 4 hit
0924       bend4 = getCLCT(pat4, endcap, dPhSign, nBits);
0925   }  // End conditional: if (BIT_COMP)
0926 
0927 }  // End function: void PtAssignmentEngineAux2017::calcBends()
0928 
0929 void PtAssignmentEngineAux2017::calcRPCs(int& RPC1,
0930                                          int& RPC2,
0931                                          int& RPC3,
0932                                          int& RPC4,
0933                                          const int mode,
0934                                          const int st1_ring2,
0935                                          const int theta,
0936                                          const bool BIT_COMP) const {
0937   if (BIT_COMP) {
0938     // Mask some invalid locations for RPC hits
0939     // theta is assumed to be the compressed, mode 15 version
0940     if (mode == 15 && !st1_ring2) {
0941       RPC1 = 0;
0942       RPC2 = 0;
0943       if (theta < 4) {
0944         RPC3 = 0;
0945         RPC4 = 0;
0946       }
0947     }
0948 
0949     int nRPC = (RPC1 == 1) + (RPC2 == 1) + (RPC3 == 1) + (RPC4 == 1);
0950 
0951     // In 3- and 4-station modes, only specify some combinations of RPCs
0952     if (nRPC >= 2) {
0953       if (mode == 15) {
0954         if (RPC1 == 1 && RPC2 == 1) {
0955           RPC3 = 0;
0956           RPC4 = 0;
0957         } else if (RPC1 == 1 && RPC3 == 1) {
0958           RPC4 = 0;
0959         } else if (RPC4 == 1 && RPC2 == 1) {
0960           RPC3 = 0;
0961         } else if (RPC3 == 1 && RPC4 == 1 && !st1_ring2) {
0962           RPC3 = 0;
0963         }
0964       } else if (mode == 14) {
0965         if (RPC1 == 1) {
0966           RPC2 = 0;
0967           RPC3 = 0;
0968         } else if (RPC3 == 1) {
0969           RPC2 = 0;
0970         }
0971       } else if (mode == 13) {
0972         if (RPC1 == 1) {
0973           RPC2 = 0;
0974           RPC4 = 0;
0975         } else if (RPC4 == 1) {
0976           RPC2 = 0;
0977         }
0978       } else if (mode == 11) {
0979         if (RPC1 == 1) {
0980           RPC3 = 0;
0981           RPC4 = 0;
0982         } else if (RPC4 == 1) {
0983           RPC3 = 0;
0984         }
0985       } else if (mode == 7) {
0986         if (RPC2 == 1) {
0987           RPC3 = 0;
0988           RPC4 = 0;
0989         } else if (RPC4 == 1) {
0990           RPC3 = 0;
0991         }
0992       }
0993 
0994     }  // End conditional: if (nRPC >= 2)
0995   }  // End conditional: if (BIT_COMP)
0996 
0997 }  // End function: void PtAssignmentEngineAux2017::calcRPCs()
0998 
0999 int PtAssignmentEngineAux2017::calcBendFromPattern(const int pattern, const int endcap) const {
1000   int bend = -99;
1001   if (pattern < 0)
1002     return bend;
1003 
1004   if (pattern == 10)
1005     bend = 0;
1006   else if ((pattern % 2) == 0)
1007     bend = (10 - pattern) / 2;
1008   else if ((pattern % 2) == 1)
1009     bend = -1 * (11 - pattern) / 2;
1010 
1011   // Reverse to match dPhi convention
1012   if (endcap == 1)
1013     bend *= -1;
1014 
1015   emtf_assert(bend != -99);
1016   return bend;
1017 }
1018 
1019 void PtAssignmentEngineAux2017::calcDeltaPhiSums(int& dPhSum4,
1020                                                  int& dPhSum4A,
1021                                                  int& dPhSum3,
1022                                                  int& dPhSum3A,
1023                                                  int& outStPh,
1024                                                  const int dPh12,
1025                                                  const int dPh13,
1026                                                  const int dPh14,
1027                                                  const int dPh23,
1028                                                  const int dPh24,
1029                                                  const int dPh34) const {
1030   dPhSum4 = dPh12 + dPh13 + dPh14 + dPh23 + dPh24 + dPh34;
1031   dPhSum4A = abs(dPh12) + abs(dPh13) + abs(dPh14) + abs(dPh23) + abs(dPh24) + abs(dPh34);
1032   int devSt1 = abs(dPh12) + abs(dPh13) + abs(dPh14);
1033   int devSt2 = abs(dPh12) + abs(dPh23) + abs(dPh24);
1034   int devSt3 = abs(dPh13) + abs(dPh23) + abs(dPh34);
1035   int devSt4 = abs(dPh14) + abs(dPh24) + abs(dPh34);
1036 
1037   if (devSt4 > devSt3 && devSt4 > devSt2 && devSt4 > devSt1)
1038     outStPh = 4;
1039   else if (devSt3 > devSt4 && devSt3 > devSt2 && devSt3 > devSt1)
1040     outStPh = 3;
1041   else if (devSt2 > devSt4 && devSt2 > devSt3 && devSt2 > devSt1)
1042     outStPh = 2;
1043   else if (devSt1 > devSt4 && devSt1 > devSt3 && devSt1 > devSt2)
1044     outStPh = 1;
1045   else
1046     outStPh = 0;
1047 
1048   if (outStPh == 4) {
1049     dPhSum3 = dPh12 + dPh13 + dPh23;
1050     dPhSum3A = abs(dPh12) + abs(dPh13) + abs(dPh23);
1051   } else if (outStPh == 3) {
1052     dPhSum3 = dPh12 + dPh14 + dPh24;
1053     dPhSum3A = abs(dPh12) + abs(dPh14) + abs(dPh24);
1054   } else if (outStPh == 2) {
1055     dPhSum3 = dPh13 + dPh14 + dPh34;
1056     dPhSum3A = abs(dPh13) + abs(dPh14) + abs(dPh34);
1057   } else {
1058     dPhSum3 = dPh23 + dPh24 + dPh34;
1059     dPhSum3A = abs(dPh23) + abs(dPh24) + abs(dPh34);
1060   }
1061 
1062 }  // End function: void PtAssignmentEngineAux2017::calcDeltaPhiSums()