Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:30:32

0001 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTLookupTables.h"
0002 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTReceiverCard.h"
0003 
0004 #include <vector>
0005 using std::vector;
0006 
0007 #include <bitset>
0008 using std::bitset;
0009 
0010 #include <iostream>
0011 using std::cerr;
0012 using std::cout;
0013 using std::endl;
0014 
0015 #include <string>
0016 using std::string;
0017 
0018 L1RCTReceiverCard::L1RCTReceiverCard(int crateNumber, int cardNumber, const L1RCTLookupTables *rctLookupTables)
0019     : regions(2),
0020       crtNo(crateNumber),
0021       cardNo(cardNumber),
0022       rctLookupTables_(rctLookupTables),
0023       etIn10Bits(2),
0024       overFlowBits(2),
0025       muonBits(2),
0026       tauBits(2) {}
0027 
0028 L1RCTReceiverCard::~L1RCTReceiverCard() {}
0029 
0030 void L1RCTReceiverCard::randomInput() {
0031   std::vector<unsigned short> input(64);
0032   for (int i = 0; i < 64; i++)
0033     input.at(i) = rand() & 511;
0034   fillInput(input);
0035 }
0036 
0037 void L1RCTReceiverCard::fileInput(char *filename) {
0038   std::vector<unsigned short> input(64);
0039   unsigned short x;
0040   std::ifstream instream(filename);
0041   if (instream) {
0042     for (int i = 0; i < 64; i++) {
0043       if (!(instream >> x))
0044         break;
0045       else
0046         input.at(i) = x;
0047     }
0048   }
0049   fillInput(input);
0050 }
0051 
0052 // First layer is ecal the second is hcal.
0053 // goes in order of (for crate 0,card 0)
0054 // (Region 1)   (Region 0)
0055 // 28 24 20 16 12 08 04 00
0056 // 29 25 21 17 13 09 05 01
0057 // 30 26 22 18 14 10 06 02
0058 // 31 27 23 19 15 11 07 03
0059 //
0060 // For card 6 of crate 0 it would look like
0061 //
0062 // 12 08 04 00
0063 // 13 09 05 01
0064 // 14 10 06 02
0065 // 15 11 07 03
0066 // 16 20 24 28
0067 // 17 21 25 29
0068 // 18 22 26 30
0069 // 19 23 27 31
0070 
0071 void L1RCTReceiverCard::fillInput(const std::vector<unsigned short> &input) {
0072   std::vector<unsigned short> ecalInput(32);
0073   std::vector<unsigned short> ecalFG(32);
0074   std::vector<unsigned short> hcalInput(32);
0075   std::vector<unsigned short> hcalMuon(32);
0076 
0077   for (int i = 0; i < 32; i++) {
0078     ecalInput.at(i) = input.at(i) / 2;
0079     ecalFG.at(i) = input.at(i) & 1;
0080     hcalInput.at(i) = input.at(i + 32) / 2;
0081     hcalMuon.at(i) = input.at(i + 32) & 1;
0082     unsigned long lookup = rctLookupTables_->lookup(
0083         ecalInput.at(i), hcalInput.at(i), ecalFG.at(i), crtNo, cardNo, i);  // tower number 0-31 now
0084     unsigned short etIn7Bits = lookup & 127;
0085     unsigned short etIn9Bits = (lookup >> 8) & 511;
0086     unsigned short HE_FGBit = (lookup >> 7) & 1;
0087     unsigned short activityBit = (lookup >> 17) & 1;
0088     std::vector<unsigned short> indices = towerToRegionMap(i);
0089     unsigned short r = indices.at(0);
0090     unsigned short row = indices.at(1);
0091     unsigned short col = indices.at(2);
0092     regions.at(r).setEtIn7Bits(row, col, etIn7Bits);
0093     regions.at(r).setEtIn9Bits(row, col, etIn9Bits);
0094     regions.at(r).setHE_FGBit(row, col, HE_FGBit);
0095     regions.at(r).setMuonBit(row, col, hcalMuon.at(i));
0096     regions.at(r).setActivityBit(row, col, activityBit);
0097   }
0098 }
0099 
0100 vector<unsigned short> L1RCTReceiverCard::towerToRegionMap(int towernum) {
0101   std::vector<unsigned short> returnVec(3);
0102   unsigned short region;
0103   unsigned short towerrow;
0104   unsigned short towercol;
0105   if (crtNo < 9) {
0106     if (cardNo != 6) {
0107       if (towernum < 16) {
0108         region = 0;
0109         towerrow = towernum % 4;
0110         towercol = 3 - (towernum / 4);
0111       } else {
0112         region = 1;
0113         towerrow = towernum % 4;
0114         towercol = 7 - (towernum / 4);
0115       }
0116     } else {
0117       if (towernum < 16) {
0118         region = 0;
0119         towerrow = towernum % 4;
0120         towercol = 3 - (towernum / 4);
0121       } else {
0122         region = 1;
0123         towerrow = towernum % 4;
0124         towercol = (towernum / 4) - 4;
0125       }
0126     }
0127   } else {
0128     if (cardNo != 6) {
0129       if (towernum < 16) {
0130         region = 0;
0131         towerrow = towernum % 4;
0132         towercol = towernum / 4;
0133       } else {
0134         region = 1;
0135         towerrow = towernum % 4;
0136         towercol = (towernum / 4) - 4;
0137       }
0138     } else {
0139       if (towernum < 16) {
0140         region = 0;
0141         towerrow = towernum % 4;
0142         towercol = towernum / 4;
0143       } else {
0144         region = 1;
0145         towerrow = towernum % 4;
0146         towercol = 7 - (towernum / 4);
0147       }
0148     }
0149   }
0150   returnVec.at(0) = region;
0151   returnVec.at(1) = towerrow;
0152   returnVec.at(2) = towercol;
0153   return returnVec;
0154 }
0155 
0156 void L1RCTReceiverCard::fillTauBits() {
0157   for (int i = 0; i < 2; i++)
0158     tauBits.at(i) = calcTauBit(regions.at(i));
0159 }
0160 
0161 unsigned short L1RCTReceiverCard::calcTauBit(L1RCTRegion region) {
0162   bitset<4> etaPattern;
0163   bitset<4> phiPattern;
0164 
0165   bitset<4> badPattern5(string("0101"));
0166   bitset<4> badPattern7(string("0111"));
0167   bitset<4> badPattern9(string("1001"));
0168   bitset<4> badPattern10(string("1010"));
0169   bitset<4> badPattern11(string("1011"));
0170   bitset<4> badPattern13(string("1101"));
0171   bitset<4> badPattern14(string("1110"));
0172   bitset<4> badPattern15(string("1111"));
0173 
0174   for (int i = 0; i < 4; i++) {
0175     phiPattern[i] = region.getActivityBit(i, 0) || region.getActivityBit(i, 1) || region.getActivityBit(i, 2) ||
0176                     region.getActivityBit(i, 3);
0177     etaPattern[i] = region.getActivityBit(0, i) || region.getActivityBit(1, i) || region.getActivityBit(2, i) ||
0178                     region.getActivityBit(3, i);
0179   }
0180 
0181   bool answer;
0182 
0183   if (etaPattern != badPattern5 && etaPattern != badPattern7 && etaPattern != badPattern10 &&
0184       etaPattern != badPattern11 && etaPattern != badPattern13 && etaPattern != badPattern14 &&
0185       etaPattern != badPattern15 && phiPattern != badPattern5 && phiPattern != badPattern7 &&
0186       phiPattern != badPattern10 && phiPattern != badPattern11 && phiPattern != badPattern13 &&
0187       phiPattern != badPattern14 && phiPattern != badPattern15 && etaPattern != badPattern9 &&
0188       phiPattern != badPattern9) {  // adding in "9"
0189     // return false;
0190     answer = false;
0191   }
0192   // else return true;
0193   else {
0194     answer = true;
0195   }
0196   // std::cout << "Tau veto set to " << answer << std::endl;
0197   return answer;
0198 }
0199 
0200 void L1RCTReceiverCard::fillRegionSums() {
0201   for (int i = 0; i < 2; i++) {
0202     etIn10Bits.at(i) = (calcRegionSum(regions.at(i))) / 2;
0203     overFlowBits.at(i) = (calcRegionSum(regions.at(i)) & 1);
0204   }
0205 }
0206 
0207 unsigned short L1RCTReceiverCard::calcRegionSum(L1RCTRegion region) {
0208   unsigned short sum = 0;
0209   unsigned short overflow = 0;
0210   for (int i = 0; i < 4; i++) {
0211     for (int j = 0; j < 4; j++) {
0212       unsigned short towerEt = region.getEtIn9Bits(i, j);
0213       // If tower is saturated, peg the region to max value
0214       // if(towerEt == 0x1FF) sum = 0x3FF;  // HARDWARE DOESN'T DO THIS!!
0215       // else
0216       sum = sum + towerEt;
0217     }
0218   }
0219   if (sum > 1023) {
0220     sum = 1023;
0221     overflow = 1;
0222   }
0223   unsigned short sumFullInfo = sum * 2 + overflow;
0224   return sumFullInfo;
0225 }
0226 
0227 void L1RCTReceiverCard::fillMuonBits() {
0228   for (int i = 0; i < 2; i++)
0229     muonBits.at(i) = calcMuonBit(regions.at(i));
0230 }
0231 
0232 unsigned short L1RCTReceiverCard::calcMuonBit(L1RCTRegion region) {
0233   unsigned short muonBit = 0;
0234   for (int i = 0; i < 4; i++) {
0235     for (int j = 0; j < 4; j++) {
0236       muonBit = muonBit || region.getMuonBit(i, j);
0237     }
0238   }
0239   return muonBit;
0240 }
0241 
0242 void L1RCTReceiverCard::print() {
0243   std::cout << "Receiver Card " << cardNo << " in Crate " << crtNo << std::endl;
0244 
0245   for (int i = 0; i < 2; i++) {
0246     std::cout << "Region " << i << " information" << std::endl;
0247     regions.at(i).print();
0248     std::cout << "Region Et sum " << etIn10Bits.at(i) << std::endl;
0249     std::cout << "Tau Veto Bit " << tauBits.at(i) << std::endl;
0250     std::cout << "Muon Bit " << muonBits.at(i) << std::endl;
0251   }
0252 }