Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:04

0001 #include <iostream>
0002 #include <iomanip>
0003 #include <cmath>
0004 
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 
0007 #include "L1Trigger/L1TMuonOverlap/interface/GoldenPattern.h"
0008 #include "L1Trigger/L1TMuonOverlap/interface/OMTFConfiguration.h"
0009 #include "L1Trigger/L1TMuonOverlap/interface/OMTFinput.h"
0010 
0011 ////////////////////////////////////////////////////
0012 ////////////////////////////////////////////////////
0013 GoldenPattern::layerResult GoldenPattern::process1Layer1RefLayer(unsigned int iRefLayer,
0014                                                                  unsigned int iLayer,
0015                                                                  const int phiRefHit,
0016                                                                  const OMTFinput::vector1D &layerHits) {
0017   GoldenPattern::layerResult aResult;
0018 
0019   int phiMean = meanDistPhi[iLayer][iRefLayer];
0020   int phiDist = exp2(myOmtfConfig->nPdfAddrBits());
0021   ///Select hit closest to the mean of probability
0022   ///distribution in given layer
0023   for (auto itHit : layerHits) {
0024     if (itHit >= (int)myOmtfConfig->nPhiBins())
0025       continue;
0026     if (abs(itHit - phiMean - phiRefHit) < abs(phiDist))
0027       phiDist = itHit - phiMean - phiRefHit;
0028   }
0029 
0030   ///Check if phiDist is within pdf range -63 +63
0031   if (abs(phiDist) > (exp2(myOmtfConfig->nPdfAddrBits() - 1) - 1))
0032     return aResult;
0033 
0034   ///Shift phidist, so 0 is at the middle of the range
0035   phiDist += exp2(myOmtfConfig->nPdfAddrBits() - 1);
0036 
0037   int pdfVal = pdfAllRef[iLayer][iRefLayer][phiDist];
0038 
0039   return GoldenPattern::layerResult(pdfVal, pdfVal > 0);
0040 }
0041 ////////////////////////////////////////////////////
0042 ////////////////////////////////////////////////////
0043 int GoldenPattern::propagateRefPhi(int phiRef, int etaRef, unsigned int iRefLayer) {
0044   unsigned int iLayer = 2;  //MB2
0045   //if(etaRef>101) iLayer = 7;//RE2
0046   return phiRef + meanDistPhi[iLayer][iRefLayer];
0047 }
0048 ////////////////////////////////////////////////////
0049 ////////////////////////////////////////////////////
0050 void GoldenPattern::addCount(unsigned int iRefLayer,
0051                              unsigned int iLayer,
0052                              const int phiRefHit,
0053                              const OMTFinput::vector1D &layerHits) {
0054   int nHitsInLayer = 0;
0055   int phiDist = exp2(myOmtfConfig->nPdfAddrBits());
0056   for (auto itHit : layerHits) {
0057     if (itHit >= (int)myOmtfConfig->nPhiBins())
0058       continue;
0059     if (abs(itHit - phiRefHit) < phiDist)
0060       phiDist = itHit - phiRefHit;
0061     ++nHitsInLayer;
0062   }
0063   ///For making the patterns take events with a single hit in each layer
0064   if (nHitsInLayer > 1 || nHitsInLayer == 0)
0065     return;
0066 
0067   ///Shift phiDist so it is in +-Pi range
0068   if (phiDist >= (int)myOmtfConfig->nPhiBins() / 2)
0069     phiDist -= (int)myOmtfConfig->nPhiBins();
0070   if (phiDist <= -(int)myOmtfConfig->nPhiBins() / 2)
0071     phiDist += (int)myOmtfConfig->nPhiBins();
0072 
0073   ///Shift phidist, so 0 is at the middle of the range
0074   int phiDistShift = phiDist + exp2(myOmtfConfig->nPdfAddrBits() - 1);
0075 
0076   ///Check if phiDist is within pdf range
0077   ///in -64 +63 U2 code
0078   ///Find more elegant way to check this.
0079   if (phiDistShift < 0 || phiDistShift > exp2(myOmtfConfig->nPdfAddrBits()) - 1) {
0080     return;
0081   }
0082 
0083   if ((int)iLayer == myOmtfConfig->getRefToLogicNumber()[iRefLayer])
0084     ++meanDistPhiCounts[iLayer][iRefLayer];
0085   ++pdfAllRef[iLayer][iRefLayer][phiDistShift];
0086 }
0087 ////////////////////////////////////////////////////
0088 ////////////////////////////////////////////////////
0089 std::ostream &operator<<(std::ostream &out, const GoldenPattern &aPattern) {
0090   out << "GoldenPattern " << aPattern.theKey << std::endl;
0091   out << "Number of reference layers: " << aPattern.meanDistPhi[0].size()
0092       << ", number of measurement layers: " << aPattern.pdfAllRef.size() << std::endl;
0093 
0094   if (aPattern.meanDistPhi.empty())
0095     return out;
0096   if (aPattern.pdfAllRef.empty())
0097     return out;
0098 
0099   out << "Mean dist phi per layer:" << std::endl;
0100   for (unsigned int iRefLayer = 0; iRefLayer < aPattern.meanDistPhi[0].size(); ++iRefLayer) {
0101     out << "Ref layer: " << iRefLayer << " (";
0102     for (unsigned int iLayer = 0; iLayer < aPattern.meanDistPhi.size(); ++iLayer) {
0103       out << std::setw(3) << aPattern.meanDistPhi[iLayer][iRefLayer] << "\t";
0104     }
0105     out << ")" << std::endl;
0106   }
0107 
0108   if (!aPattern.meanDistPhiCounts.empty()) {
0109     out << "Counts number per layer:" << std::endl;
0110     for (unsigned int iRefLayer = 0; iRefLayer < aPattern.meanDistPhi[0].size(); ++iRefLayer) {
0111       out << "Ref layer: " << iRefLayer << " (";
0112       for (unsigned int iLayer = 0; iLayer < aPattern.meanDistPhi.size(); ++iLayer) {
0113         out << aPattern.meanDistPhiCounts[iLayer][iRefLayer] << "\t";
0114       }
0115       out << ")" << std::endl;
0116     }
0117   }
0118 
0119   unsigned int nPdfAddrBits = 7;
0120   out << "PDF per layer:" << std::endl;
0121   for (unsigned int iRefLayer = 0; iRefLayer < aPattern.pdfAllRef[0].size(); ++iRefLayer) {
0122     out << "Ref layer: " << iRefLayer;
0123     for (unsigned int iLayer = 0; iLayer < aPattern.pdfAllRef.size(); ++iLayer) {
0124       out << ", measurement layer: " << iLayer << std::endl;
0125       for (unsigned int iPdf = 0; iPdf < exp2(nPdfAddrBits); ++iPdf) {
0126         out << std::setw(2) << aPattern.pdfAllRef[iLayer][iRefLayer][iPdf] << " ";
0127       }
0128       out << std::endl;
0129     }
0130   }
0131 
0132   return out;
0133 }
0134 ////////////////////////////////////////////////////
0135 ////////////////////////////////////////////////////
0136 void GoldenPattern::reset() {
0137   GoldenPattern::vector1D meanDistPhi1D(myOmtfConfig->nRefLayers());
0138   GoldenPattern::vector2D meanDistPhi2D(myOmtfConfig->nLayers());
0139   meanDistPhi2D.assign(myOmtfConfig->nLayers(), meanDistPhi1D);
0140   meanDistPhi = meanDistPhi2D;
0141   meanDistPhiCounts = meanDistPhi2D;
0142 
0143   GoldenPattern::vector1D pdf1D(exp2(myOmtfConfig->nPdfAddrBits()));
0144   GoldenPattern::vector2D pdf2D(myOmtfConfig->nRefLayers());
0145   GoldenPattern::vector3D pdf3D(myOmtfConfig->nLayers());
0146 
0147   pdf2D.assign(myOmtfConfig->nRefLayers(), pdf1D);
0148   pdfAllRef.assign(myOmtfConfig->nLayers(), pdf2D);
0149 }
0150 ////////////////////////////////////////////////////
0151 ////////////////////////////////////////////////////
0152 void GoldenPattern::normalise(unsigned int nPdfAddrBits) {
0153   for (unsigned int iRefLayer = 0; iRefLayer < pdfAllRef[0].size(); ++iRefLayer) {
0154     for (unsigned int iLayer = 0; iLayer < pdfAllRef.size(); ++iLayer) {
0155       for (unsigned int iPdf = 0; iPdf < pdfAllRef[iLayer][iRefLayer].size(); ++iPdf) {
0156         float pVal = log((float)pdfAllRef[iLayer][iRefLayer][iPdf] /
0157                          meanDistPhiCounts[myOmtfConfig->getRefToLogicNumber()[iRefLayer]][iRefLayer]);
0158         if (pVal < log(myOmtfConfig->minPdfVal()))
0159           continue;
0160         meanDistPhi[iLayer][iRefLayer] +=
0161             (iPdf - exp2(myOmtfConfig->nPdfAddrBits() - 1)) * pdfAllRef[iLayer][iRefLayer][iPdf];
0162         if ((int)iLayer != myOmtfConfig->getRefToLogicNumber()[iRefLayer])
0163           meanDistPhiCounts[iLayer][iRefLayer] += pdfAllRef[iLayer][iRefLayer][iPdf];
0164       }
0165     }
0166   }
0167 
0168   ///Mean dist phi
0169   for (unsigned int iRefLayer = 0; iRefLayer < meanDistPhi[0].size(); ++iRefLayer) {
0170     for (unsigned int iLayer = 0; iLayer < meanDistPhi.size(); ++iLayer) {
0171       if (!meanDistPhiCounts.empty() && meanDistPhiCounts[iLayer][iRefLayer]) {
0172         if (meanDistPhiCounts[iLayer][iRefLayer] < 1000)
0173           meanDistPhi[iLayer][iRefLayer] = 0;
0174         else
0175           meanDistPhi[iLayer][iRefLayer] =
0176               rint((float)meanDistPhi[iLayer][iRefLayer] / meanDistPhiCounts[iLayer][iRefLayer]);
0177       }
0178     }
0179   }
0180   const float minPlog = log(myOmtfConfig->minPdfVal());
0181   const unsigned int nPdfValBits = myOmtfConfig->nPdfValBits();
0182   ///Probabilities. Normalise and change from float to integer values
0183   float pVal;
0184   int digitisedVal, truncatedValue;
0185   for (unsigned int iRefLayer = 0; iRefLayer < pdfAllRef[0].size(); ++iRefLayer) {
0186     for (unsigned int iLayer = 0; iLayer < pdfAllRef.size(); ++iLayer) {
0187       for (unsigned int iPdf = 0; iPdf < pdfAllRef[iLayer][iRefLayer].size(); ++iPdf) {
0188         if (!meanDistPhiCounts[myOmtfConfig->getRefToLogicNumber()[iRefLayer]][iRefLayer] ||
0189             !pdfAllRef[iLayer][iRefLayer][iPdf])
0190           continue;
0191         pVal = log((float)pdfAllRef[iLayer][iRefLayer][iPdf] /
0192                    meanDistPhiCounts[myOmtfConfig->getRefToLogicNumber()[iRefLayer]][iRefLayer]);
0193         ///If there are only a few counts in given measurement layer, set pdf value to 0
0194         if ((pVal < minPlog || meanDistPhiCounts[iLayer][iRefLayer] < 1000)) {
0195           pdfAllRef[iLayer][iRefLayer][iPdf] = 0;
0196           continue;
0197         }
0198         ///Digitisation
0199         ///Values remapped 0->std::pow(2,nPdfValBits)
0200         ///          minPlog->0
0201         digitisedVal = rint((std::pow(2, nPdfValBits) - 1) - (pVal / minPlog) * (std::pow(2, nPdfValBits) - 1));
0202         ///Make sure digitised value is saved using nBitsVal bits
0203         truncatedValue = 0 | (digitisedVal & ((int)pow(2, nPdfValBits) - 1));
0204         pdfAllRef[iLayer][iRefLayer][iPdf] = truncatedValue;
0205       }
0206     }
0207   }
0208 
0209   vector3D pdfAllRefTmp = pdfAllRef;
0210   for (unsigned int iRefLayer = 0; iRefLayer < pdfAllRef[0].size(); ++iRefLayer) {
0211     for (unsigned int iLayer = 0; iLayer < pdfAllRef.size(); ++iLayer) {
0212       for (unsigned int iPdf = 0; iPdf < pdfAllRef[iLayer][iRefLayer].size(); ++iPdf) {
0213         pdfAllRef[iLayer][iRefLayer][iPdf] = 0;
0214         ///Shift pdf index by meanDistPhi
0215         int index =
0216             iPdf - exp2(myOmtfConfig->nPdfAddrBits() - 1) - meanDistPhi[iLayer][iRefLayer] + exp2(nPdfAddrBits - 1);
0217         if (index < 0 || index > exp2(nPdfAddrBits) - 1)
0218           continue;
0219         pdfAllRef[iLayer][iRefLayer][index] = pdfAllRefTmp[iLayer][iRefLayer][iPdf];
0220       }
0221     }
0222   }
0223 }
0224 ////////////////////////////////////////////////////
0225 ////////////////////////////////////////////////////
0226 bool GoldenPattern::hasCounts() {
0227   for (unsigned int iRefLayer = 0; iRefLayer < meanDistPhi[0].size(); ++iRefLayer) {
0228     for (unsigned int iLayer = 0; iLayer < meanDistPhi.size(); ++iLayer) {
0229       if (!meanDistPhiCounts.empty() && meanDistPhiCounts[iLayer][iRefLayer])
0230         return true;
0231     }
0232   }
0233   return false;
0234 }
0235 ////////////////////////////////////////////////////
0236 ////////////////////////////////////////////////////