Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-18 02:06:40

0001 // Authors: Felice Pantaleo - felice.pantaleo@cern.ch
0002 // Date: 03/2019
0003 
0004 #ifndef RecoLocalCalo_HGCalRecProducers_HGCalLayerTiles_h
0005 #define RecoLocalCalo_HGCalRecProducers_HGCalLayerTiles_h
0006 
0007 #include "RecoLocalCalo/HGCalRecProducers/interface/HGCalTilesConstants.h"
0008 #include "RecoLocalCalo/HGCalRecProducers/interface/HFNoseTilesConstants.h"
0009 #include "DataFormats/Math/interface/normalizedPhi.h"
0010 
0011 #include <vector>
0012 #include <array>
0013 #include <cmath>
0014 #include <algorithm>
0015 #include <cassert>
0016 
0017 template <typename T>
0018 class HGCalLayerTilesT {
0019 public:
0020   typedef T type;
0021   void fill(const std::vector<float>& x,
0022             const std::vector<float>& y,
0023             const std::vector<float>& eta,
0024             const std::vector<float>& phi,
0025             const std::vector<bool>& isSi) {
0026     auto cellsSize = x.size();
0027     for (unsigned int i = 0; i < cellsSize; ++i) {
0028       tiles_[getGlobalBin(x[i], y[i])].push_back(i);
0029       if (!isSi[i]) {
0030         tiles_[getGlobalBinEtaPhi(eta[i], phi[i])].push_back(i);
0031       }
0032     }
0033   }
0034 
0035   int getXBin(float x) const {
0036     constexpr float xRange = T::maxX - T::minX;
0037     static_assert(xRange >= 0.);
0038     constexpr float r = T::nColumns / xRange;
0039     int xBin = (x - T::minX) * r;
0040     xBin = std::clamp(xBin, 0, T::nColumns - 1);
0041     return xBin;
0042   }
0043 
0044   int getYBin(float y) const {
0045     constexpr float yRange = T::maxY - T::minY;
0046     static_assert(yRange >= 0.);
0047     constexpr float r = T::nRows / yRange;
0048     int yBin = (y - T::minY) * r;
0049     yBin = std::clamp(yBin, 0, T::nRows - 1);
0050     return yBin;
0051   }
0052 
0053   int getEtaBin(float eta) const {
0054     constexpr float etaRange = T::maxEta - T::minEta;
0055     static_assert(etaRange >= 0.);
0056     constexpr float r = T::nColumnsEta / etaRange;
0057     int etaBin = (eta - T::minEta) * r;
0058     etaBin = std::clamp(etaBin, 0, T::nColumnsEta - 1);
0059     return etaBin;
0060   }
0061 
0062   int getPhiBin(float phi) const {
0063     auto normPhi = normalizedPhi(phi);
0064     constexpr float r = T::nRowsPhi * M_1_PI * 0.5f;
0065     int phiBin = (normPhi + M_PI) * r;
0066     return phiBin;
0067   }
0068 
0069   int mPiPhiBin = getPhiBin(-M_PI);
0070   int pPiPhiBin = getPhiBin(M_PI);
0071 
0072   int getGlobalBin(float x, float y) const { return getXBin(x) + getYBin(y) * T::nColumns; }
0073 
0074   int getGlobalBinByBin(int xBin, int yBin) const { return xBin + yBin * T::nColumns; }
0075 
0076   int getGlobalBinEtaPhi(float eta, float phi) const {
0077     return T::nColumns * T::nRows + getEtaBin(eta) + getPhiBin(phi) * T::nColumnsEta;
0078   }
0079 
0080   int getGlobalBinByBinEtaPhi(int etaBin, int phiBin) const {
0081     return T::nColumns * T::nRows + etaBin + phiBin * T::nColumnsEta;
0082   }
0083 
0084   std::array<int, 4> searchBox(float xMin, float xMax, float yMin, float yMax) const {
0085     int xBinMin = getXBin(xMin);
0086     int xBinMax = getXBin(xMax);
0087     int yBinMin = getYBin(yMin);
0088     int yBinMax = getYBin(yMax);
0089     return std::array<int, 4>({{xBinMin, xBinMax, yBinMin, yBinMax}});
0090   }
0091 
0092   std::array<int, 4> searchBoxEtaPhi(float etaMin, float etaMax, float phiMin, float phiMax) const {
0093     int etaBinMin = getEtaBin(etaMin);
0094     int etaBinMax = getEtaBin(etaMax);
0095     int phiBinMin = getPhiBin(phiMin);
0096     int phiBinMax = getPhiBin(phiMax);
0097     // If the search window cross the phi-bin boundary, add T::nPhiBins to the
0098     // MAx value. This guarantees that the caller can perform a valid doule
0099     // loop on eta and phi. It is the caller responsibility to perform a module
0100     // operation on the phiBin values returned by this function, to explore the
0101     // correct bins.
0102     if (phiBinMax < phiBinMin) {
0103       phiBinMax += T::nRowsPhi;
0104     }
0105 
0106     return std::array<int, 4>({{etaBinMin, etaBinMax, phiBinMin, phiBinMax}});
0107   }
0108 
0109   void clear() {
0110     for (auto& t : tiles_)
0111       t.clear();
0112   }
0113 
0114   const std::vector<int>& operator[](int globalBinId) const { return tiles_[globalBinId]; }
0115 
0116 private:
0117   std::array<std::vector<int>, T::nTiles> tiles_;
0118 };
0119 
0120 using HGCalLayerTiles = HGCalLayerTilesT<HGCalTilesConstants>;
0121 using HFNoseLayerTiles = HGCalLayerTilesT<HFNoseTilesConstants>;
0122 #endif