Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "L1Trigger/TrackFindingTMTT/interface/HTrphi.h"
0002 #include "L1Trigger/TrackFindingTMTT/interface/InputData.h"
0003 #include "L1Trigger/TrackFindingTMTT/interface/TP.h"
0004 #include "L1Trigger/TrackFindingTMTT/interface/L1fittedTrack.h"
0005 #include "L1Trigger/TrackFindingTMTT/interface/Settings.h"
0006 #include "L1Trigger/TrackFindingTMTT/interface/PrintL1trk.h"
0007 #include "FWCore/ServiceRegistry/interface/Service.h"
0008 
0009 #include "DataFormats/Math/interface/deltaPhi.h"
0010 #include "FWCore/Utilities/interface/Exception.h"
0011 
0012 #include <vector>
0013 #include <limits>
0014 #include <atomic>
0015 #include <sstream>
0016 #include <mutex>
0017 
0018 using namespace std;
0019 
0020 namespace tmtt {
0021 
0022   //=== The r-phi Hough Transform array for a single (eta,phi) sector.
0023   //===
0024   //=== Its axes are (q/Pt, phiTrk), where phiTrk is the phi at which the track crosses a
0025   //=== user-configurable radius from the beam-line.
0026 
0027   //=== Initialise
0028 
0029   HTrphi::HTrphi(const Settings* settings,
0030                  unsigned int iPhiSec,
0031                  unsigned int iEtaReg,
0032                  float etaMinSector,
0033                  float etaMaxSector,
0034                  float phiCentreSector,
0035                  HTrphi::ErrorMonitor* errMon)
0036       : HTbase(settings, iPhiSec, iEtaReg, settings->houghNbinsPt(), settings->houghNbinsPhi()),
0037         invPtToDphi_((settings->invPtToDphi())),
0038         shape_(static_cast<HTshape>(settings->shape())),  // shape of HT cells
0039 
0040         //--- Specification of HT q/Pt axis.
0041         maxAbsQoverPtAxis_(1. / settings->houghMinPt()),  // Max. |q/Pt| covered by  HT array.
0042         nBinsQoverPtAxis_(settings->houghNbinsPt()),      // No. of bins in HT array in q/Pt.
0043         binSizeQoverPtAxis_(2 * maxAbsQoverPtAxis_ / nBinsQoverPtAxis_),
0044 
0045         //--- Specification of HT phiTrk axis
0046         // (phiTrk corresponds to phi where track crosses radius = chosenRofPhi_).
0047         chosenRofPhi_(settings->chosenRofPhi()),
0048         phiCentreSector_(phiCentreSector),                           // Centre of phiTrk sector.
0049         maxAbsPhiTrkAxis_(M_PI / float(settings->numPhiSectors())),  // Half-width of phiTrk axis in HT array.
0050         nBinsPhiTrkAxis_(settings->houghNbinsPhi()),                 // No. of bins in HT array phiTrk
0051         binSizePhiTrkAxis_(2 * maxAbsPhiTrkAxis_ / nBinsPhiTrkAxis_),
0052         errMon_(errMon) {
0053     // Deal with unusually shaped HT cells.
0054     if (shape_ != HTshape::square)
0055       binSizeQoverPtAxis_ = 2. * maxAbsQoverPtAxis_ / (nBinsQoverPtAxis_ - 1.);
0056     if (shape_ == HTshape::hexagon)
0057       binSizePhiTrkAxis_ = 2. * maxAbsPhiTrkAxis_ / (nBinsPhiTrkAxis_ - 1. / 6.);
0058     else if (shape_ == HTshape::diamond)
0059       binSizePhiTrkAxis_ = 2. * maxAbsPhiTrkAxis_ / (nBinsPhiTrkAxis_ - 1. / 2.);
0060 
0061     // Optionally merge 2x2 neighbouring cells into a single cell at low Pt, to reduce efficiency loss due to
0062     // scattering. (Do this if either of options EnableMerge2x2 or MiniHTstage are enabled.
0063     // N.B These two options are never both enabled).
0064     enableMerge2x2_ = (settings->enableMerge2x2() || settings->miniHTstage());
0065     if (settings->miniHTstage()) {
0066       // Mini-HT stage cfg: Merge all bins, irrespective of Pt.
0067       minInvPtToMerge2x2_ = 0.;
0068     } else {
0069       // Merged cells cfg: Merge bins below specified Pt threshold.
0070       minInvPtToMerge2x2_ = 1. / (settings->maxPtToMerge2x2());
0071       if (minInvPtToMerge2x2_ > maxAbsQoverPtAxis_)
0072         enableMerge2x2_ = false;
0073     }
0074 
0075     // Merging 2x2 cells into 1 merged cell is only allowed if HT array dimensions are even.
0076     // (This restriction could be removed along q/Pt axis, since there are also unmerged cells there. But this
0077     // would require correcting the code after each called to mergedCell() below, since
0078     //  "if (i%2 == 1) iStore = i - 1" not correct in this case).
0079     if (enableMerge2x2_ && (nBinsQoverPtAxis_ % 2 != 0 || nBinsPhiTrkAxis_ % 2 != 0))
0080       throw cms::Exception("BadConfig") << "HTrphi: You are not allowed to set EnableMerge2x2 or MiniHTstage = True if "
0081                                            "you have an odd number of bins "
0082                                            "in r-phi HT array "
0083                                         << nBinsQoverPtAxis_ << " " << nBinsPhiTrkAxis_;
0084 
0085     //--- Other options used when filling the HT.
0086 
0087     // Don't fill all HT cells nominally crossed by line corresponding to stub.
0088     killSomeHTCellsRphi_ = settings->killSomeHTCellsRphi();
0089 
0090     // Used to kill excess stubs or tracks that can't be transmitted within time-multiplexed period.
0091     nReceivedStubs_ = 0;
0092     busyInputSectorKill_ = settings_->busyInputSectorKill();  // Kill excess stubs going fron GP to HT?
0093     busySectorKill_ = settings_->busySectorKill();            // Kill excess tracks flowing out of HT?
0094     // Max. num. of stubs that can be sent from GP to HT within TM period
0095     busyInputSectorNumStubs_ = settings_->busyInputSectorNumStubs();
0096     // Max. num. of stubs that can be sent out of HT within TM period
0097     busySectorNumStubs_ = settings_->busySectorNumStubs();
0098     // or individual m bin (=q/Pt) ranges to be output to optical links.
0099     busySectorMbinRanges_ = settings_->busySectorMbinRanges();
0100     // Specifies which m bins should be grouped together by BusySectorMbinRanges. If empty, then they are grouped in order 0,1,2,3,4,5 ...
0101     busySectorMbinOrder_ = settings_->busySectorMbinOrder();
0102     // m bin ranges option disabled if vector empty
0103     busySectorUseMbinRanges_ = (not busySectorMbinRanges_.empty());
0104     busySectorUseMbinOrder_ = (not busySectorMbinOrder_.empty());
0105 
0106     bool rescaleMbins = false;
0107     if (busySectorUseMbinRanges_) {
0108       // Check if the total number of bins specified in cfg option BusySectorMbinRanges corresponds
0109       // to the number of m bins (q/Pt) in the HT. If not, determine how much the ranges must be scaled
0110       // to make this true.
0111       unsigned int nTotalBins = 0;
0112       for (unsigned int j = 0; j < busySectorMbinRanges_.size(); j++) {
0113         nTotalBins += busySectorMbinRanges_[j];
0114       }
0115       rescaleMbins = (nTotalBins != nBinsQoverPtAxis_);
0116       // No rescaling allowed with MBinOrder option.
0117       if (rescaleMbins && busySectorUseMbinOrder_)
0118         throw cms::Exception("BadConfig") << "HTrphi: BusySectorUserMbin error";
0119       float rescaleFactor = rescaleMbins ? float(nBinsQoverPtAxis_) / float(nTotalBins) : 1.;
0120       // Find lower and upper inclusive limits of each m bin range to be sent to a separate optical link.
0121       busySectorMbinLow_.resize(busySectorMbinRanges_.size());
0122       busySectorMbinHigh_.resize(busySectorMbinRanges_.size());
0123       float mBinSum = 0.;
0124       for (unsigned int i = 0; i < busySectorMbinRanges_.size(); i++) {
0125         busySectorMbinLow_[i] = std::round(mBinSum);
0126         busySectorMbinHigh_[i] = std::round(mBinSum + rescaleFactor * busySectorMbinRanges_[i]) - 1;
0127         mBinSum += rescaleFactor * busySectorMbinRanges_[i];
0128       }
0129     }
0130     //
0131     for (unsigned int i = 0; i < nBinsQoverPtAxis_; i++) {
0132       for (unsigned int j = 0; j < nBinsPhiTrkAxis_; j++) {
0133         pair<float, float> helix = this->helix2Dconventional(i, j);  // Get track params at centre of cell.
0134         float qOverPt = helix.first;
0135         // Check if this cell is merged with its neighbours (as in low Pt region).
0136         bool mergedCell = false;
0137         if (enableMerge2x2_ && this->mergedCell(i, j))
0138           mergedCell = true;
0139         // Initialize each cell in HT array.
0140         HTbase::htArray_(i, j) =
0141             std::make_unique<HTcell>(settings, iPhiSec, iEtaReg, etaMinSector, etaMaxSector, qOverPt, i, mergedCell);
0142       }
0143     }
0144 
0145     std::stringstream text;
0146     text << "\n";
0147     text << "=== R-PHI HOUGH TRANSFORM AXES RANGES: abs(q/Pt) < " << maxAbsQoverPtAxis_ << " & abs(track-phi) < "
0148          << maxAbsPhiTrkAxis_ << " ===\n";
0149     text << "=== R-PHI HOUGH TRANSFORM ARRAY SIZE: q/Pt bins = " << nBinsQoverPtAxis_
0150          << " & track-phi bins = " << nBinsPhiTrkAxis_ << " ===\n";
0151     text << "=== R-PHI HOUGH TRANSFORM BIN SIZE: BIN(q/Pt) = " << binSizeQoverPtAxis_
0152          << " & BIN(track-phi) = " << binSizePhiTrkAxis_ << " ===\n\n";
0153     if (busySectorKill_ && busySectorUseMbinRanges_ && rescaleMbins) {
0154       text << "=== R-PHI HOUGH TRANSFORM WARNING: Rescaled m bin ranges specified by cfg parameter "
0155               "BusySectorMbinRanges, as they were inconsistent with total number of m bins in HT.\n";
0156       text << "=== Rescaled values for BusySectorMbinRanges =";
0157       for (unsigned int i = 0; i < busySectorMbinRanges_.size(); i++) {
0158         text << " " << (busySectorMbinHigh_[i] - busySectorMbinLow_[i] + 1);
0159       }
0160     }
0161     text << "\n";
0162     static std::once_flag printOnce;
0163     std::call_once(printOnce, [](string t) { PrintL1trk() << t; }, text.str());
0164 
0165     // Note helix parameters at the centre of each HT cell.
0166     cellCenters_.clear();
0167     for (unsigned int m = 0; m < nBinsQoverPtAxis_; m++) {
0168       std::vector<std::pair<float, float> > binCenters;
0169       for (unsigned int c = 0; c < nBinsPhiTrkAxis_; c++)
0170         binCenters.push_back(this->helix2Dhough(m, c));
0171       cellCenters_.push_back(binCenters);
0172     }
0173   }
0174 
0175   //=== Add stub to HT array.
0176   //=== If eta subsectors are being used within each sector, specify which ones the stub is compatible with.
0177 
0178   void HTrphi::store(Stub* stub, const vector<bool>& inEtaSubSecs) {
0179     // Optionally, only store stubs that can be sent from GP to HT within TM period.
0180     if ((!busyInputSectorKill_) || (nReceivedStubs_ < busyInputSectorNumStubs_)) {
0181       nReceivedStubs_++;
0182 
0183       unsigned int jPhiTrkBinMinLast = 0;  // Used for error checking
0184       unsigned int jPhiTrkBinMaxLast = 99999;
0185 
0186       // Loop over q/Pt related bins in HT array.
0187       for (unsigned int i = 0; i < nBinsQoverPtAxis_; i++) {
0188         if (shape_ == HTshape::square) {
0189           //--- This is a traditional HT with square cells.
0190 
0191           // In this q/Pt bin, find the range of phi bins that this stub is consistent with.
0192           pair<unsigned int, unsigned int> jRange = this->iPhiRange(stub, i);
0193           unsigned int jPhiTrkBinMin = jRange.first;
0194           unsigned int jPhiTrkBinMax = jRange.second;
0195 
0196           // Store stubs in these cells.
0197           for (unsigned int j = jPhiTrkBinMin; j <= jPhiTrkBinMax; j++) {
0198             bool canStoreStub = true;
0199             unsigned int iStore = i;
0200             unsigned int jStore = j;
0201 
0202             // Optionally merge 2x2 neighbouring cells into a single cell at low Pt, to reduce efficiency loss
0203             // due to scattering.
0204             if (enableMerge2x2_) {
0205               // Check if this cell is merged with its neighbours (as in low Pt region).
0206               if (this->mergedCell(i, j)) {
0207                 // Get location of cell that this cell is merged into (iStore, jStore).
0208                 // Calculation assumes HT array has even number of bins in both dimensions.
0209                 if (i % 2 == 1)
0210                   iStore = i - 1;
0211                 if (j % 2 == 1)
0212                   jStore = j - 1;
0213                 // If this stub was already stored in this merged 2x2 cell, then don't store it again.
0214                 if (HTbase::htArray_(iStore, jStore)->stubStoredInCell(stub))
0215                   canStoreStub = false;
0216               }
0217             }
0218 
0219             if (canStoreStub)
0220               HTbase::htArray_(iStore, jStore)->store(stub, inEtaSubSecs);  // Calls HTcell::store()
0221           }
0222 
0223           // Check that limitations of firmware would not prevent stub being stored correctly in this HT column.
0224           if (errMon_ != nullptr) {
0225             this->countFirmwareErrors(i, jPhiTrkBinMin, jPhiTrkBinMax, jPhiTrkBinMinLast, jPhiTrkBinMaxLast);
0226             jPhiTrkBinMinLast = jPhiTrkBinMin;
0227             jPhiTrkBinMaxLast = jPhiTrkBinMax;
0228           }
0229 
0230         } else {
0231           //--- This is are novel HT with unusual shaped cells.
0232 
0233           if (shape_ == HTshape::diamond) {
0234             //--- This HT has diamond shaped cells.
0235 
0236             float qOverPtBin = -maxAbsQoverPtAxis_ + i * binSizeQoverPtAxis_;
0237             float phiTrk = reco::deltaPhi(stub->phi(), phiCentreSector_) +
0238                            invPtToDphi_ * qOverPtBin * (stub->r() - chosenRofPhi_) + maxAbsPhiTrkAxis_;
0239             if (i % 2 == 0)
0240               phiTrk += binSizePhiTrkAxis_ / 2.;
0241             unsigned int binCenter = std::floor(phiTrk / binSizePhiTrkAxis_);
0242             if (binCenter < nBinsPhiTrkAxis_)
0243               HTbase::htArray_(i, binCenter)->store(stub, inEtaSubSecs);
0244 
0245           } else if (shape_ == HTshape::hexagon) {
0246             //--- This HT has hexagonal cells (with two of its sides parallel to the phi axis).
0247 
0248             float qOverPtBin = -maxAbsQoverPtAxis_ + i * binSizeQoverPtAxis_;
0249             float qOverPtBinVar = binSizeQoverPtAxis_;
0250             float phiTrk = reco::deltaPhi(stub->phi(), phiCentreSector_) +
0251                            invPtToDphi_ * qOverPtBin * (stub->r() - chosenRofPhi_) + maxAbsPhiTrkAxis_;
0252             float phiTrkVar = invPtToDphi_ * qOverPtBinVar * std::abs(stub->r() - chosenRofPhi_);
0253             float phiTrkMin = phiTrk - phiTrkVar;
0254             float phiTrkMax = phiTrk + phiTrkVar;
0255             if (i % 2 == 0)
0256               phiTrk += binSizePhiTrkAxis_ / 6.;
0257             else {
0258               phiTrk -= binSizePhiTrkAxis_ / 3.;
0259               phiTrkMin -= binSizePhiTrkAxis_ / 2.;
0260               phiTrkMax -= binSizePhiTrkAxis_ / 2.;
0261             }
0262             unsigned int iCenter = std::floor(phiTrk / binSizePhiTrkAxis_ * 3.);
0263             unsigned int iMin = std::floor(phiTrkMin / binSizePhiTrkAxis_ * 3.);
0264             unsigned int iMax = std::floor(phiTrkMax / binSizePhiTrkAxis_ * 3.);
0265             std::pair<bool, unsigned int> binCenter;
0266             std::pair<bool, unsigned int> binMin;
0267             std::pair<bool, unsigned int> binMax;
0268             binCenter.second = iCenter / 3;
0269             binMin.second = iMin / 3;
0270             binMax.second = iMax / 3;
0271             binCenter.first = !(iCenter % 3 == 2);
0272             binMin.first = (iMin % 3 == 0);
0273             binMax.first = (iMax % 3 == 0);
0274             if (binCenter.first && binCenter.second < nBinsPhiTrkAxis_)
0275               HTbase::htArray_(i, binCenter.second)->store(stub, inEtaSubSecs);
0276             else if (binMin.first && binMin.second < nBinsPhiTrkAxis_)
0277               HTbase::htArray_(i, binMin.second)->store(stub, inEtaSubSecs);
0278             else if (binMax.first && binMax.second < nBinsPhiTrkAxis_)
0279               HTbase::htArray_(i, binMax.second)->store(stub, inEtaSubSecs);
0280 
0281           } else if (shape_ == HTshape::brick) {
0282             //--- This HT has square cells with alternate rows shifted horizontally by 0.5*cell_width.
0283 
0284             float qOverPtBin = -maxAbsQoverPtAxis_ + i * binSizeQoverPtAxis_;
0285             float qOverPtBinVar = binSizeQoverPtAxis_;
0286             float phiTrk = reco::deltaPhi(stub->phi(), phiCentreSector_) +
0287                            invPtToDphi_ * qOverPtBin * (stub->r() - chosenRofPhi_) + maxAbsPhiTrkAxis_;
0288             float phiTrkVar = invPtToDphi_ * qOverPtBinVar * std::abs(stub->r() - chosenRofPhi_);
0289             float phiTrkMin = phiTrk - phiTrkVar;
0290             float phiTrkMax = phiTrk + phiTrkVar;
0291             unsigned int iMin = std::floor(phiTrkMin / binSizePhiTrkAxis_ * 2.);
0292             unsigned int iMax = std::floor(phiTrkMax / binSizePhiTrkAxis_ * 2.);
0293             std::pair<bool, unsigned int> binMin;
0294             std::pair<bool, unsigned int> binMax;
0295             binMin.second = iMin / 2;
0296             binMax.second = iMax / 2;
0297             binMin.first = (iMin % 2 == i % 2);
0298             binMax.first = (iMax % 2 == i % 2);
0299             if (binMin.first && binMin.second < nBinsPhiTrkAxis_)
0300               HTbase::htArray_(i, binMin.second)->store(stub, inEtaSubSecs);
0301             else if (binMax.first && binMax.second < nBinsPhiTrkAxis_)
0302               HTbase::htArray_(i, binMax.second)->store(stub, inEtaSubSecs);
0303           }
0304         }
0305       }
0306       // Note max. |gradient| that the line corresponding to any stub in any of the r-phi HT arrays could have.
0307       // Firmware assumes this should not exceed 1.0;
0308       if (errMon_ != nullptr) {
0309         errMon_->maxLineGradient = max(errMon_->maxLineGradient.load(), this->calcLineGradArray(stub->r()));
0310       }
0311     }
0312   }
0313 
0314   //=== Determine the m-bin (q/pt) range the specified track is in. (Used if outputting each m bin range on a different opto-link).
0315 
0316   unsigned int HTrphi::getMbinRange(const L1track2D& trk) const {
0317     if (busySectorUseMbinRanges_) {
0318       unsigned int mBin = trk.cellLocationHT().first;
0319       unsigned int mBinOrder;
0320       if (busySectorUseMbinOrder_) {
0321         // User wants to group bins in a wierd order.
0322         mBinOrder = 99999;
0323         for (unsigned int k = 0; k < busySectorMbinOrder_.size(); k++) {
0324           if (mBin == busySectorMbinOrder_[k])
0325             mBinOrder = k;
0326         }
0327         if (mBinOrder == 99999)
0328           throw cms::Exception("LogicError") << "HTrphi::getMbinRange() mBinOrder calculation wrong.";
0329       } else {
0330         // User grouping bins in numerical order 0,1,2,3,4,5...
0331         mBinOrder = mBin;
0332       }
0333       for (unsigned int i = 0; i < busySectorMbinRanges_.size(); i++) {
0334         if (mBinOrder >= busySectorMbinLow_[i] && mBinOrder <= busySectorMbinHigh_[i])
0335           return i;
0336       }
0337       throw cms::Exception("LogicError") << "HTrphi::getMbinRange() messed up";
0338     } else {
0339       return 0;
0340     }
0341   }
0342 
0343   //=== For a given Q/Pt bin, find the range of phi bins that a given stub is consistent with.
0344   //=== Return as a pair (min bin, max bin)
0345   //=== If it range lies outside the HT array, then the min bin will be set larger than the max bin.
0346 
0347   pair<unsigned int, unsigned int> HTrphi::iPhiRange(const Stub* stub, unsigned int iQoverPtBin, bool debug) const {
0348     // Note q/Pt value corresponding to centre of this bin.
0349     float qOverPtBin = -maxAbsQoverPtAxis_ + (iQoverPtBin + 0.5) * binSizeQoverPtAxis_;
0350     // Note change in this q/Pt value needed to reach either edge of the bin.
0351     float qOverPtBinVar = 0.5 * binSizeQoverPtAxis_;
0352 
0353     // Reducing effective bin width can reduce fake rate.
0354     //qOverPtVar = 0.4*binSizeQoverPtAxis_;
0355 
0356     // Calculate range of track-phi that would allow a track in this q/Pt range to pass through the stub.
0357     float phiTrk = stub->phi() + invPtToDphi_ * qOverPtBin * (stub->r() - chosenRofPhi_);
0358     // The next line does the phiTrk calculation without the usual approximation, but it doesn't
0359     // improve performance.
0360     //float phiTrk    = stub->phi() + asin(invPtToDphi_ * qOverPtBin * stub->r()) - asin(invPtToDphi_ * qOverPtBin * chosenRofPhi_);
0361     float phiTrkVar = invPtToDphi_ * qOverPtBinVar * std::abs(stub->r() - chosenRofPhi_);
0362     float phiTrkMin = phiTrk - phiTrkVar;
0363     float phiTrkMax = phiTrk + phiTrkVar;
0364 
0365     float deltaPhiMin = reco::deltaPhi(phiTrkMin, phiCentreSector_);  // Offset to centre of sector.
0366     float deltaPhiMax = reco::deltaPhi(phiTrkMax, phiCentreSector_);
0367     pair<float, float> phiTrkRange(deltaPhiMin, deltaPhiMax);
0368 
0369     // Determine which HT array cell range in track-phi this range "phiTrkRange" corresponds to.
0370     pair<unsigned int, unsigned int> iPhiTrkBinRange = this->HTbase::convertCoordRangeToBinRange(
0371         phiTrkRange, nBinsPhiTrkAxis_, (-maxAbsPhiTrkAxis_), binSizePhiTrkAxis_, killSomeHTCellsRphi_);
0372 
0373     return iPhiTrkBinRange;
0374   }
0375 
0376   //=== Check that limitations of firmware would not prevent stub being stored correctly in this HT column.
0377 
0378   void HTrphi::countFirmwareErrors(unsigned int iQoverPtBin,
0379                                    unsigned int jPhiTrkBinMin,
0380                                    unsigned int jPhiTrkBinMax,
0381                                    unsigned int jPhiTrkBinMinLast,
0382                                    unsigned int jPhiTrkBinMaxLast) {
0383     // Only do check if stub is being stored somewhere in this HT column.
0384     if (jPhiTrkBinMax >= jPhiTrkBinMin) {
0385       //--- Remaining code below checks that firmware could successfully store this stub in this column.
0386       //   (a) Does cell lie NE, E or SE of cell filled in previous column?
0387       bool OK_a = (jPhiTrkBinMin + 1 >= jPhiTrkBinMinLast) && (jPhiTrkBinMax <= jPhiTrkBinMaxLast + 1);
0388       //   (b) Are no more than 2 cells filled in this column
0389       bool OK_b = (jPhiTrkBinMax - jPhiTrkBinMin + 1 <= 2);
0390 
0391       if (!OK_a)
0392         errMon_->numErrorsTypeA++;
0393       if (!OK_b)
0394         errMon_->numErrorsTypeB++;
0395       errMon_->numErrorsNorm++;  // No. of times a stub is added to an HT column.
0396     }
0397   }
0398 
0399   //=== Get the values of the track helix params corresponding to middle of a specified HT cell (i,j).
0400   //=== The helix parameters returned will be those corresponding to the two axes of the HT array.
0401   //=== So they might be (q/pt, phi0) or (q/pt, phi65) etc. depending on the configuration.
0402 
0403   pair<float, float> HTrphi::helix2Dhough(unsigned int i, unsigned int j) const {
0404     unsigned int qOverPtBin = i;
0405     unsigned int phiTrkBin = j;
0406 
0407     // If using merged 2x2 cells in low Pt parts of array, must correct for this.
0408     bool merged = false;
0409     if (enableMerge2x2_) {
0410       // Check if this cell is merged with its neighbours (as in low Pt region).
0411       if (this->mergedCell(i, j)) {
0412         merged = true;
0413         // Get location of cell that this cell is merged into (iStore, jStore).
0414         // Calculation assumes HT array has even number of bins in both dimensions.
0415         if (i % 2 == 1)
0416           qOverPtBin = i - 1;
0417         if (j % 2 == 1)
0418           phiTrkBin = j - 1;
0419       }
0420     }
0421 
0422     float qOverPtBinCenter = .5;
0423     float phiTrkBinCenter = .5;
0424 
0425     if (shape_ != HTshape::square) {
0426       qOverPtBinCenter = 0.;
0427 
0428       float evenPhiPos = 0., oddPhiPos = 0.;
0429       if (shape_ == HTshape::hexagon) {
0430         evenPhiPos = 1. / 6.;
0431         oddPhiPos = 2. / 3.;
0432       } else if (shape_ == HTshape::diamond) {
0433         evenPhiPos = 0.;
0434         oddPhiPos = 0.5;
0435       } else if (shape_ == HTshape::brick) {
0436         evenPhiPos = 0.25;
0437         oddPhiPos = 0.75;
0438       }
0439       phiTrkBinCenter = (qOverPtBin % 2 == 0) ? evenPhiPos : oddPhiPos;
0440     }
0441 
0442     float qOverPt = -maxAbsQoverPtAxis_ + (qOverPtBin + qOverPtBinCenter) * binSizeQoverPtAxis_;
0443     float phiTrk = -maxAbsPhiTrkAxis_ + (phiTrkBin + phiTrkBinCenter) * binSizePhiTrkAxis_;
0444 
0445     if (merged) {
0446       qOverPt += 0.5 * binSizeQoverPtAxis_;
0447       phiTrk += 0.5 * binSizePhiTrkAxis_;
0448     }
0449 
0450     // Correct phiTrk to centre of sector, taking care of 2*pi wrapping
0451     phiTrk = reco::deltaPhi(phiTrk + phiCentreSector_, 0.);
0452     return pair<float, float>(qOverPt, phiTrk);
0453   }
0454 
0455   //=== Get the values of the track helix params corresponding to middle of a specified HT cell (i,j).
0456   //=== The helix parameters returned will be always be (q/pt, phi0), irrespective of how the axes
0457   //=== of the HT array are defined.
0458 
0459   pair<float, float> HTrphi::helix2Dconventional(unsigned int i, unsigned int j) const {
0460     // Get the helix parameters corresponding to the axes definitions of the HT.
0461     pair<float, float> helix2Dht = this->helix2Dhough(i, j);
0462     // Convert to the conventionally agreed pair of helix parameters, (q/pt, phi0).
0463     float qOverPt = helix2Dht.first;  // easy
0464     // If HT defined track phi other than at r=0, must correct to get phi0. Allow for 2*pi wrapping of phi.
0465     float phi0 = reco::deltaPhi(helix2Dht.second + invPtToDphi_ * chosenRofPhi_ * qOverPt, 0.);
0466     return pair<float, float>(qOverPt, phi0);
0467   }
0468 
0469   //=== Which cell in HT array should this TP be in, based on its true trajectory?
0470   //=== (If TP is outside HT array, it it put in the closest bin inside it).
0471 
0472   pair<unsigned int, unsigned int> HTrphi::trueCell(const TP* tp) const {
0473     // Get HT axis variables corresponding to this TP.
0474     float qOverPt = tp->qOverPt();
0475     float phiTrk = tp->trkPhiAtR(chosenRofPhi_);
0476     // Measure phi relative to centre of sector.
0477     float deltaPhi = reco::deltaPhi(phiTrk, phiCentreSector_);
0478     // Convert to bin numbers inside HT array.
0479     int iQoverPt = floor((qOverPt - (-maxAbsQoverPtAxis_)) / binSizeQoverPtAxis_);
0480     int iPhiTrk = floor((deltaPhi - (-maxAbsPhiTrkAxis_)) / binSizePhiTrkAxis_);
0481     // Check if this cell was within the HT array.
0482     if (iQoverPt >= 0 && iQoverPt < int(nBinsQoverPtAxis_) && iPhiTrk >= 0 && iPhiTrk < int(nBinsPhiTrkAxis_)) {
0483       // Check if this cell is merged with its neighbours (as in low Pt region), and if so return merged cell location.
0484       // New: because 2nd stage mini HT may recreate tracks from merged cells with finer cell granularity, one can't predict
0485       //      if a merged cell was used to create a track merely by looking at its cell location.
0486       //      So instead ask L1track3D, which knows if it was created from a merged HT cell or not.
0487       ;
0488     } else {
0489       // TP is not in this HT array at all. Flag this by setting "outside" bin index to 0 (Nbins) if outside array below (above).
0490       if (iQoverPt < 0)
0491         iQoverPt = 0;
0492       if (iQoverPt >= int(nBinsQoverPtAxis_))
0493         iQoverPt = nBinsQoverPtAxis_ - 1;
0494       if (iPhiTrk < 0)
0495         iPhiTrk = 0;
0496       if (iPhiTrk >= int(nBinsPhiTrkAxis_))
0497         iPhiTrk = nBinsPhiTrkAxis_ - 1;
0498     }
0499     return pair<unsigned int, unsigned int>(iQoverPt, iPhiTrk);
0500   }
0501 
0502   //=== Which cell in HT array should this fitted track be in, based on its fitted trajectory?
0503   //=== Always uses beam-spot constrained trajectory if available.
0504   //=== (If fitted track is outside HT array, it it put in the closest bin inside it).
0505 
0506   pair<unsigned int, unsigned int> HTrphi::cell(const L1fittedTrack* fitTrk) const {
0507     bool beamConstraint = fitTrk->done_bcon();  // Is beam-spot constraint available? (e.g. 5 param helix fit)
0508     // Get HT axis variables corresponding to this fitted track.
0509     float qOverPt = beamConstraint ? fitTrk->qOverPt_bcon() : fitTrk->qOverPt();
0510     // Convert phi0 to phi at chosen radius used in HT.
0511     float phiTrk = fitTrk->phiAtChosenR(beamConstraint);
0512     // Measure phi relative to centre of sector.
0513     float deltaPhi = reco::deltaPhi(phiTrk, phiCentreSector_);
0514     // Convert to bin numbers inside HT array.
0515     int iQoverPt = 999999;
0516     int iPhiTrk = 999999;
0517 
0518     if (shape_ == HTshape::square) {
0519       //--- This is a traditional HT with square cells.
0520 
0521       iQoverPt = floor((qOverPt - (-maxAbsQoverPtAxis_)) / binSizeQoverPtAxis_);
0522       iPhiTrk = floor((deltaPhi - (-maxAbsPhiTrkAxis_)) / binSizePhiTrkAxis_);
0523 
0524       // Check if this cell was within the HT array.
0525       if (iQoverPt >= 0 && iQoverPt < int(nBinsQoverPtAxis_) && iPhiTrk >= 0 && iPhiTrk < int(nBinsPhiTrkAxis_)) {
0526         // Check if this cell is merged with its neighbours (as in low Pt region), and if so return merged cell location.
0527         // New: because 2nd stage mini HT may recreate tracks from merged cells with finer cell granularity, one can't predict
0528         //      if a merged cell was used to create a track merely by looking at its cell location.
0529         //      So instead ask L1track3D, which knows if it was created from a merged HT cell or not.
0530         ;
0531       } else {
0532         // Fitted track is not in this HT array at all. Flag this by setting "outside" bin index to 0 (Nbins-1) if outside array below (above).
0533         if (iQoverPt < 0)
0534           iQoverPt = 0;
0535         if (iQoverPt >= int(nBinsQoverPtAxis_))
0536           iQoverPt = nBinsQoverPtAxis_ - 1;
0537         if (iPhiTrk < 0)
0538           iPhiTrk = 0;
0539         if (iPhiTrk >= int(nBinsPhiTrkAxis_))
0540           iPhiTrk = nBinsPhiTrkAxis_ - 1;
0541       }
0542 
0543     } else {
0544       //--- This is are novel HT with unusual shaped cells.
0545 
0546       float minD = std::numeric_limits<float>::infinity();
0547       float d(0);
0548       unsigned int m(0);
0549       for (const auto& binCenters : cellCenters_) {
0550         unsigned int c(0);
0551         for (auto cellCenter : binCenters) {
0552           d = std::pow((cellCenter.first - qOverPt) / (float)binSizeQoverPtAxis_, 2) +
0553               std::pow((cellCenter.second - phiTrk) / (float)binSizePhiTrkAxis_, 2);
0554           if (d < minD) {
0555             minD = d;
0556             iQoverPt = m;
0557             iPhiTrk = c;
0558           }
0559           c++;
0560         }
0561         m++;
0562       }
0563       // Fitted track is not in this HT array at all. Flag this by setting "outside" bin index to 0 (Nbins-1) if outside array below (above).
0564       if (iQoverPt < 0)
0565         iQoverPt = 0;
0566       if (iQoverPt >= int(nBinsQoverPtAxis_))
0567         iQoverPt = nBinsQoverPtAxis_ - 1;
0568       if (iPhiTrk < 0)
0569         iPhiTrk = 0;
0570       if (iPhiTrk >= int(nBinsPhiTrkAxis_))
0571         iPhiTrk = nBinsPhiTrkAxis_ - 1;
0572     }
0573 
0574     return pair<unsigned int, unsigned int>(iQoverPt, iPhiTrk);
0575   }
0576 
0577   //=== Check if specified cell is merged with its 2x2 neighbours into a single cell,
0578   //=== as it is in low Pt region.
0579 
0580   bool HTrphi::mergedCell(unsigned int iQoverPtBin, unsigned int jPhiTrkBin) const {
0581     bool merge = false;
0582 
0583     if (enableMerge2x2_) {
0584       unsigned int i = iQoverPtBin;
0585       //unsigned int j = jPhiTrkBin;
0586 
0587       // Calculate number of merged bins on each q/Pt side of array.
0588       float fMergeBins = (maxAbsQoverPtAxis_ - minInvPtToMerge2x2_) / (2. * binSizeQoverPtAxis_);
0589       // Number of unmerged bins this corresponds to, which must be even, since each merged bin comprises two normal q/pt bins.
0590       unsigned int numQoverPtBinsToMerge = 2 * min((unsigned int)(std::round(fMergeBins)), (nBinsQoverPtAxis_ / 4));
0591       const float small = 0.001;
0592       if (minInvPtToMerge2x2_ < small && (unsigned int)(std::round(2. * fMergeBins)) % 2 == 1)
0593         numQoverPtBinsToMerge++;
0594       unsigned int iB = (nBinsQoverPtAxis_ - 1) - i;  // Count backwards across array.
0595       if (min(i, iB) < numQoverPtBinsToMerge)
0596         merge = true;
0597     }
0598 
0599     return merge;
0600   }
0601 
0602   //=== Calculate line |gradient| of stubs in HT array, so can check it doesn't exceed 1.
0603 
0604   float HTrphi::calcLineGradArray(float r) const {
0605     float grad = std::abs(invPtToDphi_ * (r - chosenRofPhi_));
0606     // Convert it to units of bin width.
0607     grad *= binSizeQoverPtAxis_ / binSizePhiTrkAxis_;
0608     if (shape_ == HTshape::hexagon)
0609       grad *= 3.;
0610     else if (shape_ == HTshape::diamond)
0611       grad *= 2.;
0612     else if (shape_ == HTshape::brick)
0613       grad *= 4.;
0614     return grad;
0615   }
0616 
0617   //=== If requested, kill those tracks in this sector that can't be read out during the time-multiplexed period, because
0618   //=== the HT has associated too many stubs to tracks.
0619 
0620   list<L1track2D> HTrphi::killTracksBusySec(const list<L1track2D>& tracks) const {
0621     list<L1track2D> outTracks;
0622 
0623     if (busySectorKill_) {
0624       unsigned int nStubsOut = 0;  // #stubs assigned to tracks in this sector.
0625       // #stubs assigned to each m bin range in this sector.
0626       vector<unsigned int> nStubsOutInRange(busySectorMbinRanges_.size(), 0);
0627 
0628       for (const L1track2D& trk : tracks) {
0629         bool keep = true;
0630         unsigned int nStubs = trk.numStubs();  // #stubs on this track.
0631         if (busySectorUseMbinRanges_) {  // Are tracks from different m bin ranges output seperately to increase bandwidth?
0632           unsigned int mBinRange = this->getMbinRange(trk);  // Which m bin range is this track in?
0633           nStubsOutInRange[mBinRange] += nStubs;
0634           if (nStubsOutInRange[mBinRange] > busySectorNumStubs_)
0635             keep = false;
0636         } else {
0637           nStubsOut += nStubs;
0638           if (nStubsOut > busySectorNumStubs_)
0639             keep = false;
0640         }
0641 
0642         if (keep)
0643           outTracks.push_back(trk);
0644       }
0645 
0646     } else {
0647       outTracks = tracks;
0648     }
0649 
0650     return outTracks;
0651   }
0652 
0653   //=== Define the order in which the hardware processes rows of the HT array when it outputs track candidates.
0654   //=== Currently corresponds to highest Pt tracks first.
0655   //=== If two tracks have the same Pt, the -ve charge one is output before the +ve charge one.
0656 
0657   vector<unsigned int> HTrphi::rowOrder(unsigned int numRows) const {
0658     vector<unsigned int> iOrder;
0659 
0660     // Logic slightly different depending on whether HT array has even or odd number of rows.
0661     const bool oddNumRows = (numRows % 2 == 1);
0662 
0663     // This selects middle rows first before moving to exterior ones.
0664     if (oddNumRows) {
0665       unsigned int middleRow = (numRows - 1) / 2;
0666       iOrder.push_back(middleRow);
0667       for (unsigned int i = 1; i <= (numRows - 1) / 2; i++) {
0668         iOrder.push_back(middleRow - i);  // -ve charge
0669         iOrder.push_back(middleRow + i);  // +ve charge
0670       }
0671     } else {
0672       unsigned int startRowPos = numRows / 2;
0673       unsigned int startRowNeg = startRowPos - 1;
0674       for (unsigned int i = 0; i < numRows / 2; i++) {
0675         iOrder.push_back(startRowNeg - i);  // -ve charge
0676         iOrder.push_back(startRowPos + i);  // +ve charge
0677       }
0678     }
0679 
0680     return iOrder;
0681   }
0682 
0683 }  // namespace tmtt