Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:37:05

0001 //---------------------------------------------------------------------------
0002 #include "L1Trigger/RPCTrigger/interface/RPCHalfSorter.h"
0003 
0004 #include "L1Trigger/RPCTrigger/interface/RPCConst.h"
0005 #include <algorithm>
0006 
0007 #include "L1Trigger/RPCTrigger/interface/MuonsGrabber.h"
0008 //---------------------------------------------------------------------------
0009 #include <set>
0010 using namespace std;
0011 
0012 /**
0013  *
0014  * Defualt constructor
0015  *
0016 */
0017 RPCHalfSorter::RPCHalfSorter(RPCTriggerConfiguration *triggerConfig) {
0018   m_TrigCnfg = triggerConfig;
0019 
0020   m_GBOutputMuons.assign(2, L1RpcTBMuonsVec());
0021 }
0022 /** 
0023  *
0024  * Runs GB algorithm for half of the detector - 6 TC (sectors).
0025  * @return 4 munons from barrel (m_GBOutputMuons[0]),
0026  * and 4 from endcaps (m_GBOutputMuons[1]).
0027 */
0028 L1RpcTBMuonsVec2 RPCHalfSorter::runHalf(L1RpcTBMuonsVec2 &tcsMuonsVec2) {
0029   //1+6+1 TC, wazne sa tylko te 6 w srodku
0030   for (unsigned int iTC = 0; iTC < tcsMuonsVec2.size() - 1; iTC++) {
0031     for (unsigned int iMu = 0; iMu < tcsMuonsVec2[iTC].size(); iMu++) {
0032       if (tcsMuonsVec2[iTC][iMu].getCode() == 0)
0033         continue;
0034       if (tcsMuonsVec2[iTC][iMu].gBDataKilledLast()) {
0035         for (unsigned int iMuN = 0; iMuN < tcsMuonsVec2[iTC + 1].size(); iMuN++) {
0036           if (tcsMuonsVec2[iTC + 1][iMuN].getCode() == 0)
0037             continue;
0038           if (tcsMuonsVec2[iTC + 1][iMuN].gBDataKilledFirst()) {
0039             int eta1 = tcsMuonsVec2[iTC][iMu].getEtaAddr();
0040             int eta2 = tcsMuonsVec2[iTC + 1][iMuN].getEtaAddr();
0041             if (eta1 > 16)
0042               eta1 = -((~eta1 & 63) + 1);
0043             if (eta2 > 16)
0044               eta2 = -((~eta2 & 63) + 1);
0045             if (abs(eta1 - eta2) <= 1) {
0046               if (tcsMuonsVec2[iTC][iMu].getCode() <= tcsMuonsVec2[iTC + 1][iMuN].getCode()) {
0047                 if (tcsMuonsVec2[iTC][iMu].getSegmentAddr() == RPCConst::m_SEGMENTS_IN_SECTOR_CNT - 1) {
0048                   tcsMuonsVec2[iTC][iMu].kill();
0049                 }
0050               } else {
0051                 tcsMuonsVec2[iTC + 1][iMuN].kill();
0052               }
0053             }
0054           }
0055         }
0056       }
0057     }
0058   }
0059 
0060   L1RpcTBMuonsVec outputBarrelMuons;
0061   L1RpcTBMuonsVec outputEndcapMuons;
0062 
0063   for (unsigned int iTC = 1; iTC < tcsMuonsVec2.size() - 1; iTC++)
0064     for (unsigned int iMu = 0; iMu < tcsMuonsVec2[iTC].size(); iMu++)
0065       if (tcsMuonsVec2[iTC][iMu].isLive()) {
0066         // if(abs(16 - tcsMuonsVec2[iTC][iMu].getEtaAddr()) <=7){
0067         // etaAddr should be encoded here in 6 bits with 2compl.
0068         if (tcsMuonsVec2[iTC][iMu].getEtaAddr() >= 57 || tcsMuonsVec2[iTC][iMu].getEtaAddr() <= 7) {
0069           outputBarrelMuons.push_back(tcsMuonsVec2[iTC][iMu]);
0070         } else {
0071           outputEndcapMuons.push_back(tcsMuonsVec2[iTC][iMu]);
0072         }
0073       }
0074 
0075   sort(outputBarrelMuons.begin(), outputBarrelMuons.end(), RPCTBMuon::TMuonMore());
0076   sort(outputEndcapMuons.begin(), outputEndcapMuons.end(), RPCTBMuon::TMuonMore());
0077 
0078   while (outputBarrelMuons.size() < RPCConst::m_FINAL_OUT_MUONS_CNT)
0079     outputBarrelMuons.push_back(RPCTBMuon());
0080   while (outputBarrelMuons.size() > RPCConst::m_FINAL_OUT_MUONS_CNT)
0081     outputBarrelMuons.pop_back();
0082 
0083   while (outputEndcapMuons.size() < RPCConst::m_FINAL_OUT_MUONS_CNT)
0084     outputEndcapMuons.push_back(RPCTBMuon());
0085   while (outputEndcapMuons.size() > RPCConst::m_FINAL_OUT_MUONS_CNT)
0086     outputEndcapMuons.pop_back();
0087 
0088   m_GBOutputMuons[0].insert(m_GBOutputMuons[0].end(), outputBarrelMuons.begin(), outputBarrelMuons.end());
0089   m_GBOutputMuons[1].insert(m_GBOutputMuons[1].end(), outputEndcapMuons.begin(), outputEndcapMuons.end());
0090   return m_GBOutputMuons;
0091 }
0092 void RPCHalfSorter::maskHSBInput(L1RpcTBMuonsVec &newVec, int mask) {
0093   if (mask < 0 || mask > 3) {
0094     throw cms::Exception("RPCHalfSorter::maskHSBInput") << " hsbMask has wrong value - " << mask << " \n";
0095   }
0096 
0097   if (mask == 1) {  // leave the best muons
0098     newVec.at(2) = RPCTBMuon();
0099     newVec.at(3) = RPCTBMuon();
0100   } else if (mask == 2) {
0101     newVec.at(0) = RPCTBMuon();
0102     newVec.at(1) = RPCTBMuon();
0103   } else {
0104     newVec.at(0) = RPCTBMuon();
0105     newVec.at(1) = RPCTBMuon();
0106     newVec.at(2) = RPCTBMuon();
0107     newVec.at(3) = RPCTBMuon();
0108   }
0109 }
0110 
0111 /** 
0112  * Runs runHalf() for 2 detecors parts.
0113  * Converts m_tower number (eta addr) from continous (0 - 32, m_tower 0 = 16)
0114  * to 2'complement.
0115  * @return 4 munons from barrel (m_GBOutputMuons[0]),
0116  * and 4 from endcaps (m_GBOutputMuons[1]).
0117 */
0118 L1RpcTBMuonsVec2 RPCHalfSorter::run(L1RpcTBMuonsVec2 &tcsMuonsVec2, edm::ESHandle<L1RPCHsbConfig> hsbConf) {
0119   m_GBOutputMuons[0].clear();
0120   m_GBOutputMuons[1].clear();
0121 
0122   L1RpcTBMuonsVec2 firstHalfTcsMuonsVec2;
0123 
0124   if (tcsMuonsVec2[m_TrigCnfg->getTCsCnt() - 1].empty() || hsbConf->getHsbMask(0, 0) == 3) {
0125     firstHalfTcsMuonsVec2.push_back(tcsMuonsVec2[m_TrigCnfg->getTCsCnt() - 1]);  //TC=11 (last one)
0126   } else {
0127     L1RpcTBMuonsVec newVec = tcsMuonsVec2[m_TrigCnfg->getTCsCnt() - 1];
0128     maskHSBInput(newVec, hsbConf->getHsbMask(0, 0));
0129     firstHalfTcsMuonsVec2.push_back(newVec);
0130   }
0131 
0132   // update sectorAddr. Dont update sectorAddr of last tc (it will be done in other half)
0133   int secAddr = 1;
0134   //                                         <6+1
0135   for (int iTC = 0; iTC < m_TrigCnfg->getTCsCnt() / 2 + 1; iTC++) {
0136     for (unsigned int iMu = 0; iMu < tcsMuonsVec2[iTC].size(); iMu++) {
0137       if (secAddr != 0 && secAddr != 7) {
0138         tcsMuonsVec2[iTC][iMu].setSectorAddr(secAddr);  // |
0139                                                         // iTC=0 - firstTrigger crate (no=1)
0140                                                         //       - in hw it has sectorAddr=1
0141         //tcsMuonsVec2[iTC][iMu].setGBData(0);       // gbData is used nowhere from now, we
0142         //      want to act same as hw
0143       }
0144     }  // iter. over muons end
0145     ++secAddr;  // Next trigger crate. Update the address
0146 
0147     if (tcsMuonsVec2[iTC].empty() || hsbConf->getHsbMask(0, iTC + 1) == 3) {
0148       firstHalfTcsMuonsVec2.push_back(tcsMuonsVec2[iTC]);
0149     } else {
0150       L1RpcTBMuonsVec newVec = tcsMuonsVec2[iTC];
0151       maskHSBInput(newVec, hsbConf->getHsbMask(0, iTC + 1));
0152       firstHalfTcsMuonsVec2.push_back(newVec);
0153     }
0154   }
0155 
0156   runHalf(firstHalfTcsMuonsVec2);
0157 
0158   unsigned int fhBMuons = m_GBOutputMuons[0].size();  // Number of first half barrel muons
0159   unsigned int fhEMuons = m_GBOutputMuons[1].size();  // Number of first half endcap muons
0160 
0161   L1RpcTBMuonsVec2 secondHalfTcsMuonsVec2;
0162   secAddr = 0;
0163   //        5                                           <12
0164   for (int iTC = m_TrigCnfg->getTCsCnt() / 2 - 1; iTC < m_TrigCnfg->getTCsCnt(); iTC++) {
0165     for (unsigned int iMu = 0; iMu < tcsMuonsVec2[iTC].size(); iMu++) {
0166       if (secAddr != 0 && secAddr != 7) {
0167         tcsMuonsVec2[iTC][iMu].setSectorAddr(secAddr);
0168         //tcsMuonsVec2[iTC][iMu].setGBData(0);       // gbData is used nowhere from now, we
0169         //      want to act same as hw
0170       }
0171     }
0172     ++secAddr;
0173     if (tcsMuonsVec2[iTC].empty() || hsbConf->getHsbMask(1, iTC - 5) == 3) {
0174       secondHalfTcsMuonsVec2.push_back(tcsMuonsVec2[iTC]);
0175     } else {
0176       L1RpcTBMuonsVec newVec = tcsMuonsVec2[iTC];
0177       maskHSBInput(newVec, hsbConf->getHsbMask(1, iTC - 5));
0178       secondHalfTcsMuonsVec2.push_back(newVec);
0179     }
0180   }
0181 
0182   //secondHalfTcsMuonsVec2.push_back(tcsMuonsVec2[0]);
0183   if (tcsMuonsVec2[0].empty() || hsbConf->getHsbMask(1, 7) == 3) {
0184     secondHalfTcsMuonsVec2.push_back(tcsMuonsVec2[0]);
0185   } else {
0186     L1RpcTBMuonsVec newVec = tcsMuonsVec2[0];
0187     maskHSBInput(newVec, hsbConf->getHsbMask(1, 7));
0188     secondHalfTcsMuonsVec2.push_back(newVec);
0189   }
0190 
0191   runHalf(secondHalfTcsMuonsVec2);
0192   // Debug
0193   for (unsigned int region = 0; region < m_GBOutputMuons.size(); ++region) {  // region: 0- barrel,1-endcaps
0194     for (unsigned int i = 0; i < m_GBOutputMuons[region].size(); ++i) {
0195       unsigned int halfNum = 0;  // Number of halfsorter: 0 - first half, 1 - second half
0196       int iMod = 0;
0197 
0198       // After second call of runHalf muons are written at the end of m_GBOutputMuons[0,1] vector
0199       // not needed - fhBMuons ==4 (always)
0200       if ((region == 0 && i >= fhBMuons) || (region == 1 && i >= fhEMuons)) {
0201         halfNum = 1;
0202         iMod = 4;
0203       }
0204       // Print out
0205       if (m_TrigCnfg->getDebugLevel() == 1) {
0206 #ifndef _STAND_ALONE
0207         // LogDebug("RPCHwDebug")<<"GB 3"<< region <<halfNum
0208         //     << " " << i - iMod << " "
0209         //     << m_GBOutputMuons[region][i].printDebugInfo(m_TrigCnfg->getDebugLevel());
0210         //MuonsGrabber::Instance().writeDataForRelativeBX(iBx);
0211         MuonsGrabber::Instance().addMuon(m_GBOutputMuons[region][i], 3, region, halfNum, i - iMod);
0212 
0213 #else
0214         std::cout << "GB 3" << region << halfNum << " " << i - iMod << " "
0215                   << m_GBOutputMuons[region][i].printDebugInfo(m_TrigCnfg->getDebugLevel()) << std::endl;
0216 #endif
0217       }
0218       // Re-number the phi addr. This should be done by fs, temporary fix (doesnt change the logic)
0219       int segment = m_GBOutputMuons[region][i].getSegmentAddr();
0220       int sector = m_GBOutputMuons[region][i].getSectorAddr() - 1 + 6 * halfNum;
0221       int pt = m_GBOutputMuons[region][i].getPtCode();
0222       if (pt != 0) {  // dont touch empty muons
0223         m_GBOutputMuons[region][i].setPhiAddr((sector * 12 + segment + 2) % 144);
0224       }
0225     }
0226   }
0227 
0228   return m_GBOutputMuons;
0229 }