Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-06 02:54:09

0001 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/GoldenPatternResult.h"
0002 #include "L1Trigger/L1TMuonOverlapPhase1/interface/Omtf/OMTFConfiguration.h"
0003 
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 
0006 #include <iostream>
0007 #include <ostream>
0008 #include <iomanip>
0009 #include <cmath>
0010 
0011 ////////////////////////////////////////////
0012 ////////////////////////////////////////////
0013 
0014 ////////////////////////////////////////////
0015 ////////////////////////////////////////////
0016 GoldenPatternResult::GoldenPatternResult(const OMTFConfiguration* omtfConfig)
0017     : finalise([this]() { finalise0(); }), omtfConfig(omtfConfig) {
0018   if (omtfConfig)
0019     init(omtfConfig);
0020 }
0021 
0022 ////////////////////////////////////////////
0023 ////////////////////////////////////////////
0024 
0025 void GoldenPatternResult::set(int refLayer_, int phi, int eta, int refHitPhi) {
0026   if (isValid() && this->refLayer != refLayer_) {
0027     std::cout << __FUNCTION__ << " " << __LINE__ << " this->refLayer " << this->refLayer << " refLayer_ " << refLayer_
0028               << std::endl;
0029   }
0030   assert(!isValid() || this->refLayer == refLayer_);
0031 
0032   this->refLayer = refLayer_;
0033   this->phi = phi;
0034   this->eta = eta;
0035   this->refHitPhi = refHitPhi;
0036 }
0037 
0038 void GoldenPatternResult::setStubResult(float pdfVal, bool valid, int pdfBin, int layer, MuonStubPtr stub) {
0039   if (valid) {
0040     //pdfSum and firedLayerBits is calculated in finaliseX()
0041     firedLayerBits |= (1 << layer);
0042   }
0043   stubResults[layer] = StubResult(pdfVal, valid, pdfBin, layer, stub);
0044 
0045   //stub result is added even thought it is not valid since this might be needed for debugging or optimization
0046 }
0047 
0048 void GoldenPatternResult::setStubResult(int layer, StubResult& stubResult) {
0049   if (stubResult.getValid()) {
0050     //pdfSum and firedLayerBits is calculated in finaliseX()
0051     firedLayerBits |= (1 << layer);
0052   }
0053   stubResults[layer] = stubResult;
0054 
0055   //stub result is added even thought it is not valid since this might be needed for debugging or optimization
0056 }
0057 
0058 ////////////////////////////////////////////
0059 ////////////////////////////////////////////
0060 void GoldenPatternResult::init(const OMTFConfiguration* omtfConfig) {
0061   this->omtfConfig = omtfConfig;
0062 
0063   finalizeFunction = this->omtfConfig->getGoldenPatternResultFinalizeFunction();
0064 
0065   if (finalizeFunction == 1)
0066     finalise = [this]() { finalise1(); };
0067   else if (finalizeFunction == 2)
0068     finalise = [this]() { finalise2(); };
0069   else if (finalizeFunction == 3)
0070     finalise = [this]() { finalise3(); };
0071   else if (finalizeFunction == 5)
0072     finalise = [this]() { finalise5(); };
0073   else if (finalizeFunction == 6)
0074     finalise = [this]() { finalise6(); };
0075   else if (finalizeFunction == 7)
0076     finalise = [this]() { finalise7(); };
0077   else if (finalizeFunction == 8)
0078     finalise = [this]() { finalise8(); };
0079   else if (finalizeFunction == 9)
0080     finalise = [this]() { finalise9(); };
0081   else
0082     finalise = [this]() { finalise0(); };
0083 
0084   stubResults.assign(omtfConfig->nLayers(), StubResult());
0085   reset();
0086 }
0087 
0088 void GoldenPatternResult::reset() {
0089   for (auto& stubResult : stubResults) {
0090     stubResult.reset();
0091   }
0092   valid = false;
0093   refLayer = -1;
0094   phi = 0;
0095   eta = 0;
0096   pdfSum = 0;
0097   firedLayerCnt = 0;
0098   firedLayerBits = 0;
0099   refHitPhi = 0;
0100   gpProbability1 = 0;
0101   gpProbability2 = 0;
0102 }
0103 
0104 ////////////////////////////////////////////
0105 ////////////////////////////////////////////
0106 //default version
0107 void GoldenPatternResult::finalise0() {
0108   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0109     unsigned int connectedLayer = omtfConfig->getLogicToLogic().at(iLogicLayer);
0110     //here we require that in case of the DT layers, both phi and phiB is fired
0111     if (firedLayerBits & (1 << connectedLayer)) {
0112       if (firedLayerBits & (1 << iLogicLayer)) {
0113         //now in the GoldenPattern::process1Layer1RefLayer the pdf bin 0 is returned when the layer is not fired, so this is 'if' is to assured that this pdf val is not added here
0114         pdfSum += stubResults[iLogicLayer].getPdfVal();
0115 
0116         if (omtfConfig->fwVersion() <= 4) {
0117           if (!omtfConfig->getBendingLayers().count(iLogicLayer))
0118             //in DT case, the phi and phiB layers are threaded as one, so the firedLayerCnt is increased only for the phi layer
0119             firedLayerCnt++;
0120         } else
0121           firedLayerCnt++;
0122       }
0123     } else {
0124       firedLayerBits &= ~(1 << iLogicLayer);
0125     }
0126   }
0127 
0128   valid = true;
0129   //by default result becomes valid here, but can be overwritten later
0130 }
0131 
0132 ////////////////////////////////////////////
0133 ////////////////////////////////////////////
0134 //for the algo version with thresholds
0135 void GoldenPatternResult::finalise1() {
0136   //cout<<__FUNCTION__<<":"<<__LINE__<<endl;
0137   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0138     //in this version we do not require that both phi and phiB is fired (non-zero), we thread them just independent
0139     //watch out that then the number of fired layers is bigger, and the cut on the minimal number of fired layers does not work in the same way as when the dt chamber is counted as one layer
0140     //TODO check if it affects performance
0141     pdfSum += stubResults[iLogicLayer].getPdfVal();
0142     firedLayerCnt += ((firedLayerBits & (1 << iLogicLayer)) != 0);
0143   }
0144 
0145   valid = true;
0146   //by default result becomes valid here, but can be overwritten later
0147 }
0148 
0149 ////////////////////////////////////////////
0150 ////////////////////////////////////////////
0151 //multiplication of PDF values instead of sum
0152 void GoldenPatternResult::finalise2() {
0153   pdfSum = 1.;
0154   firedLayerCnt = 0;
0155   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0156     unsigned int connectedLayer = omtfConfig->getLogicToLogic().at(iLogicLayer);
0157     //here we require that in case of the DT layers, both phi and phiB is fired
0158     if (firedLayerBits & (1 << connectedLayer)) {
0159       //now in the GoldenPattern::process1Layer1RefLayer the pdf bin 0 is returned when the layer is not fired, so this is 'if' is to assured that this pdf val is not added here
0160       if (firedLayerBits & (1 << iLogicLayer)) {
0161         pdfSum *= stubResults[iLogicLayer].getPdfVal();
0162         //in DT case, the phi and phiB layers are threaded as one, so the firedLayerCnt is increased only for the phi layer
0163         if (!omtfConfig->getBendingLayers().count(iLogicLayer))
0164           firedLayerCnt++;
0165       }
0166     } else {
0167       firedLayerBits &= ~(1 << iLogicLayer);
0168     }
0169   }
0170 
0171   if (firedLayerCnt < 3)
0172     pdfSum = 0;
0173 
0174   valid = true;
0175   //by default result becomes valid here, but can be overwritten later
0176 }
0177 
0178 ////////////////////////////////////////////
0179 ////////////////////////////////////////////
0180 //for patterns generation
0181 void GoldenPatternResult::finalise3() {
0182   firedLayerCnt = 0;
0183   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0184     //in this version we do not require that both phi and phiB is fired (non-zero), we thread them just independent
0185     //watch out that then the number of fired layers is bigger, and the cut on the minimal number of fired layers dies not work in the same way as when the dt chamber is counted as one layer
0186     //TODO check if it affects performance
0187     pdfSum += stubResults[iLogicLayer].getPdfVal();
0188 
0189     if (stubResults[iLogicLayer].getMuonStub())
0190       firedLayerCnt++;
0191   }
0192 
0193   valid = true;
0194   //by default result becomes valid here, but can be overwritten later
0195 }
0196 
0197 void GoldenPatternResult::finalise5() {
0198   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0199     unsigned int connectedLayer = omtfConfig->getLogicToLogic().at(iLogicLayer);
0200 
0201     if (omtfConfig->isBendingLayer(iLogicLayer)) {  //the DT phiB layer is counted only when the phi layer is fired
0202       if ((firedLayerBits & (1 << iLogicLayer)) && (firedLayerBits & (1 << connectedLayer))) {
0203         pdfSum += stubResults[iLogicLayer].getPdfVal();
0204         firedLayerCnt++;
0205       } else {
0206         firedLayerBits &= ~(1 << iLogicLayer);
0207         stubResults[iLogicLayer].setValid(false);
0208         //in principle the stun should be also removed from the stubResults[iLogicLayer], on the other hand ini this way can be used e.g. for debug
0209       }
0210     } else if (firedLayerBits & (1 << iLogicLayer)) {
0211       pdfSum += stubResults[iLogicLayer].getPdfVal();
0212       firedLayerCnt++;
0213     }
0214   }
0215 
0216   valid = true;
0217   //by default result becomes valid here, but can be overwritten later
0218 }
0219 
0220 void GoldenPatternResult::finalise6() {
0221   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0222     unsigned int connectedLayer = omtfConfig->getLogicToLogic().at(iLogicLayer);
0223 
0224     if (omtfConfig->isBendingLayer(iLogicLayer)) {  //the DT phiB layer is counted only when the phi layer is fired
0225       if ((firedLayerBits & (1 << iLogicLayer)) && (firedLayerBits & (1 << connectedLayer)) &&
0226           (stubResults[iLogicLayer].getMuonStub()->qualityHw >= 4)) {
0227         pdfSum += stubResults[iLogicLayer].getPdfVal();
0228         firedLayerCnt++;
0229       } else {
0230         firedLayerBits &= ~(1 << iLogicLayer);
0231         stubResults[iLogicLayer].setValid(false);
0232         //in principle the stun should be also removed from the stubResults[iLogicLayer], on the other hand ini this way can be used e.g. for debug
0233       }
0234     } else if (firedLayerBits & (1 << iLogicLayer)) {
0235       pdfSum += stubResults[iLogicLayer].getPdfVal();
0236       firedLayerCnt++;
0237     }
0238   }
0239 
0240   valid = true;
0241   //by default result becomes valid here, but can be overwritten later
0242 }
0243 
0244 void GoldenPatternResult::finalise7() {
0245   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0246     pdfSum += stubResults[iLogicLayer].getPdfVal();
0247     if (firedLayerBits & (1 << iLogicLayer)) {
0248       firedLayerCnt++;
0249     }
0250   }
0251 
0252   valid = true;
0253   //by default result becomes valid here, but can be overwritten later
0254 }
0255 
0256 void GoldenPatternResult::finalise8() {
0257   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0258     pdfSum += stubResults[iLogicLayer].getPdfVal();  //pdfSum is counted always
0259 
0260     unsigned int connectedLayer = omtfConfig->getLogicToLogic().at(iLogicLayer);
0261     if (omtfConfig->isBendingLayer(iLogicLayer)) {  //the DT phiB layer is counted only when the phi layer is fired
0262       if ((firedLayerBits & (1 << iLogicLayer)) && (firedLayerBits & (1 << connectedLayer))) {
0263         // && (stubResults[iLogicLayer].getMuonStub()->qualityHw >= 4) this is not needed, as the rejecting the low quality phiB hits is on the input of the algorithm
0264         firedLayerCnt++;
0265       } else {
0266         firedLayerBits &= ~(1 << iLogicLayer);
0267         stubResults[iLogicLayer].setValid(false);
0268         //in principle the stub should be also removed from the stubResults[iLogicLayer], on the other hand in this way can be used e.g. for debug
0269       }
0270     } else if (firedLayerBits & (1 << iLogicLayer)) {
0271       firedLayerCnt++;
0272     }
0273   }
0274 
0275   valid = true;
0276   //by default result becomes valid here, but can be overwritten later
0277 }
0278 
0279 void GoldenPatternResult::finalise9() {
0280   for (unsigned int iLogicLayer = 0; iLogicLayer < stubResults.size(); ++iLogicLayer) {
0281     unsigned int connectedLayer = omtfConfig->getLogicToLogic().at(iLogicLayer);
0282 
0283     if (omtfConfig->isBendingLayer(iLogicLayer)) {  //the DT phiB layer is counted only when the phi layer is fired
0284       if (firedLayerBits & (1 << iLogicLayer)) {
0285         if (firedLayerBits & (1 << connectedLayer)) {
0286           firedLayerCnt++;
0287           pdfSum += stubResults[iLogicLayer].getPdfVal();
0288         } else {
0289           firedLayerBits &= ~(1 << iLogicLayer);
0290           stubResults[iLogicLayer].setValid(false);
0291           //there was hit, but it did not fit to the pdf - this is not possible here, since the bending layer is fired here
0292           //therefore the below line is has no sense
0293           //if(stubResults[iLogicLayer].getPdfVal() == 0) pdfSum -= 64;;
0294           //so in this case simply:
0295           //pdfSum += 0;
0296         }
0297       } else {
0298         //bending layer fired, but not fits to the pdf, N.B works only with the patterns having "no hit value" and with noHitValueInPdf = True
0299         if (stubResults[iLogicLayer].getPdfVal() == 0)
0300           pdfSum -= 32;
0301         else
0302           pdfSum += stubResults[iLogicLayer].getPdfVal();  //bending layer not fired at all
0303       }
0304     } else {
0305       if (iLogicLayer < 10 && stubResults[iLogicLayer].getPdfVal() == 0)
0306         pdfSum -= 32;
0307       else
0308         pdfSum += stubResults[iLogicLayer].getPdfVal();
0309       if (firedLayerBits & (1 << iLogicLayer)) {  //pdfSum is counted always
0310         firedLayerCnt++;
0311       }
0312     }
0313   }
0314 
0315   valid = true;
0316   //by default result becomes valid here, but can be overwritten later
0317 }
0318 
0319 ////////////////////////////////////////////
0320 ////////////////////////////////////////////
0321 std::ostream& operator<<(std::ostream& out, const GoldenPatternResult& gpResult) {
0322   unsigned int refLayerLogicNum = gpResult.omtfConfig->getRefToLogicNumber()[gpResult.getRefLayer()];
0323 
0324   unsigned int sumOverFiredLayers = 0;
0325   for (unsigned int iLogicLayer = 0; iLogicLayer < gpResult.stubResults.size(); ++iLogicLayer) {
0326     out << " layer: " << std::setw(2) << iLogicLayer << " hit: ";
0327     if (gpResult.stubResults[iLogicLayer].getMuonStub()) {
0328       out << std::setw(4)
0329           << (gpResult.omtfConfig->isBendingLayer(iLogicLayer)
0330                   ? gpResult.stubResults[iLogicLayer].getMuonStub()->phiBHw
0331                   : gpResult.stubResults[iLogicLayer].getMuonStub()->phiHw);
0332 
0333       out << " pdfBin: " << std::setw(4) << gpResult.stubResults[iLogicLayer].getPdfBin() << " pdfVal: " << std::setw(3)
0334           << gpResult.stubResults[iLogicLayer].getPdfVal() << " fired " << gpResult.isLayerFired(iLogicLayer)
0335           << (iLogicLayer == refLayerLogicNum ? " <<< refLayer" : "");
0336 
0337       if (gpResult.isLayerFired(iLogicLayer))
0338         sumOverFiredLayers += gpResult.stubResults[iLogicLayer].getPdfVal();
0339     } else if (gpResult.stubResults[iLogicLayer].getPdfVal()) {
0340       out << "                  pdfVal: " << std::setw(3) << gpResult.stubResults[iLogicLayer].getPdfVal();
0341     }
0342     out << std::endl;
0343   }
0344 
0345   out << "  refLayer: ";
0346   out << gpResult.getRefLayer() << "\t";
0347 
0348   out << " Sum over layers: ";
0349   out << gpResult.getPdfSum() << "\t";
0350 
0351   out << " sumOverFiredLayers: ";
0352   out << sumOverFiredLayers << "\t";
0353 
0354   out << " Number of hits: ";
0355   out << gpResult.getFiredLayerCnt() << "\t";
0356 
0357   out << " GpProbability1: ";
0358   out << gpResult.getGpProbability1() << "\t";
0359 
0360   out << " GpProbability2: ";
0361   out << gpResult.getGpProbability2() << "\t";
0362 
0363   out << std::endl;
0364 
0365   return out;
0366 }
0367 ////////////////////////////////////////////
0368 ////////////////////////////////////////////