Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-14 22:36:04

0001 #include "Geometry/HGCalCommonData/interface/HGCalCellUV.h"
0002 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include <iostream>
0005 #include <array>
0006 #include <algorithm>
0007 #include <cassert>
0008 
0009 HGCalCellUV::HGCalCellUV(double waferSize, double separation, int32_t nFine, int32_t nCoarse) {
0010   HGCalCell hgcalcell(waferSize, nFine, nCoarse);
0011   assert(nFine > 0 && nCoarse > 0);
0012   ncell_[0] = nFine;
0013   ncell_[1] = nCoarse;
0014   for (int k = 0; k < 2; ++k) {
0015     cellX_[k] = waferSize / (3 * ncell_[k]);
0016     cellY_[k] = 0.5 * sqrt3_ * cellX_[k];
0017     cellXTotal_[k] = (waferSize + separation) / (3 * ncell_[k]);
0018     cellYTotal_[k] = 0.5 * sqrt3_ * cellXTotal_[k];
0019   }
0020 
0021   // Fill up the placement vectors
0022   for (int placement = 0; placement < HGCalCell::cellPlacementTotal; ++placement) {
0023     // Fine cells
0024     for (int iu = 0; iu < 2 * ncell_[0]; ++iu) {
0025       for (int iv = 0; iv < 2 * ncell_[0]; ++iv) {
0026         int u = (placement < HGCalCell::cellPlacementExtra) ? iv : iu;
0027         int v = (placement < HGCalCell::cellPlacementExtra) ? iu : iv;
0028         if (((v - u) < ncell_[0]) && (u - v) <= ncell_[0]) {
0029           cellPosFine_[placement][std::pair<int, int>(u, v)] = hgcalcell.cellUV2XY1(u, v, placement, 0);
0030         }
0031       }
0032     }
0033     // Coarse cells
0034     for (int iu = 0; iu < 2 * ncell_[1]; ++iu) {
0035       for (int iv = 0; iv < 2 * ncell_[1]; ++iv) {
0036         int u = (placement < HGCalCell::cellPlacementExtra) ? iv : iu;
0037         int v = (placement < HGCalCell::cellPlacementExtra) ? iu : iv;
0038         if (((v - u) < ncell_[1]) && (u - v) <= ncell_[1]) {
0039           cellPosCoarse_[placement][std::pair<int, int>(u, v)] = hgcalcell.cellUV2XY1(u, v, placement, 1);
0040         }
0041       }
0042     }
0043   }
0044 }
0045 
0046 std::pair<int32_t, int32_t> HGCalCellUV::cellUVFromXY1(
0047     double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug) const {
0048   if (type != 0)
0049     type = 1;
0050   //--- Reverse transform to placement=0, if placement index ≠ 6
0051   double xloc1 = (placement >= HGCalCell::cellPlacementExtra) ? xloc : -xloc;
0052   double cellY = (extend) ? cellYTotal_[type] : cellY_[type];
0053   int rot = placement % HGCalCell::cellPlacementExtra;
0054   static constexpr std::array<double, 6> fcos = {{1.0, cos60_, -cos60_, -1.0, -cos60_, cos60_}};
0055   static constexpr std::array<double, 6> fsin = {{0.0, sin60_, sin60_, 0.0, -sin60_, -sin60_}};
0056   double x = xloc1 * fcos[rot] - yloc * fsin[rot];
0057   double y = xloc1 * fsin[rot] + yloc * fcos[rot];
0058 
0059   //--- Calculate coordinates in u,v,w system
0060   double u = x * sin60_ + y * cos60_;
0061   double v = -x * sin60_ + y * cos60_;
0062   double w = y;
0063 
0064   //--- Rounding in u, v, w coordinates
0065   int iu = std::floor(u / cellY) + 3 * (ncell_[type]) - 1;
0066   int iv = std::floor(v / cellY) + 3 * (ncell_[type]);
0067   int iw = std::floor(w / cellY) + 1;
0068 
0069   int isv = (iu + iw) / 3;
0070   int isu = (iv + iw) / 3;
0071 
0072   //--- Taking care of extending cells
0073   if ((iu + iw) < 0) {
0074     isu = (iv + iw + 1) / 3;
0075     isv = 0;
0076   } else if (isv - isu > ncell_[type] - 1) {
0077     isu = (iv + iw + 1) / 3;
0078     isv = (iu + iw - 1) / 3;
0079   } else if (isu > 2 * ncell_[type] - 1) {
0080     isu = 2 * ncell_[type] - 1;
0081     isv = (iu + iw - 1) / 3;
0082   }
0083   if (debug)
0084     edm::LogVerbatim("HGCalGeom") << "cellUVFromXY1: Input " << xloc << ":" << yloc << ":" << extend << " Output "
0085                                   << isu << ":" << isv;
0086   return std::make_pair(isu, isv);
0087 }
0088 
0089 std::pair<int32_t, int32_t> HGCalCellUV::cellUVFromXY2(
0090     double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug) const {
0091   //--- Using multiple inequalities to find (u, v)
0092   //--- Reverse transform to placement=0, if placement index ≠ 7
0093   if (type != 0)
0094     type = 1;
0095   double cellY = (extend) ? cellYTotal_[type] : cellY_[type];
0096   double xloc1 = (placement >= HGCalCell::cellPlacementExtra) ? xloc : -1 * xloc;
0097   int rot = placement % HGCalCell::cellPlacementExtra;
0098   static constexpr std::array<double, 6> fcos = {{cos60_, 1.0, cos60_, -cos60_, -1.0, -cos60_}};
0099   static constexpr std::array<double, 6> fsin = {{-sin60_, 0.0, sin60_, sin60_, 0.0, -sin60_}};
0100   double x = xloc1 * fcos[rot] - yloc * fsin[rot];
0101   double y = xloc1 * fsin[rot] + yloc * fcos[rot];
0102 
0103   int32_t u(-100), v(-100);
0104   int ncell = ncell_[type];
0105   double r = cellY;
0106   double l1 = (y / r) + ncell - 1.0;
0107   int l2 = std::floor((0.5 * y + 0.5 * x / sqrt3_) / r + ncell - 4.0 / 3.0);
0108   int l3 = std::floor((x / sqrt3_) / r + ncell - 4.0 / 3.0);
0109   double l4 = (y + sqrt3_ * x) / (2 * r) + 2 * ncell - 2;
0110   double l5 = (y - sqrt3_ * x) / (2 * r) - ncell;
0111   double u1 = (y / r) + ncell + 1.0;
0112   int u2 = std::ceil((0.5 * y + 0.5 * x / sqrt3_) / r + ncell + 2.0 / 3.0);
0113   int u3 = std::ceil((x / sqrt3_) / r + ncell);
0114   double u4 = l4 + 2;
0115   double u5 = l5 + 2;
0116 
0117   for (int ui = l2 + 1; ui < u2; ui++) {
0118     for (int vi = l3 + 1; vi < u3; vi++) {
0119       int c1 = 2 * ui - vi;
0120       int c4 = ui + vi;
0121       int c5 = ui - 2 * vi;
0122       if ((c1 < u1) && (c1 > l1) && (c4 < u4) && (c4 > l4) && (c5 < u5) && (c5 > l5)) {
0123         u = ui;
0124         v = vi;
0125       }
0126     }
0127   }
0128 
0129   //--- Taking care of extending cells
0130   if (v == -1) {
0131     if (y < (2 * u - v - ncell) * r) {
0132       v += 1;
0133     } else {
0134       u += 1;
0135       v += 1;
0136     }
0137   }
0138   if (v - u == ncell) {
0139     if ((y + sqrt3_ * x) < ((u + v - 2 * ncell + 1) * 2 * r)) {
0140       v += -1;
0141     } else {
0142       u += 1;
0143     }
0144   }
0145   if (u == 2 * ncell) {
0146     if ((y - sqrt3_ * x) < ((u - 2 * v + ncell - 1) * 2 * r)) {
0147       u += -1;
0148     } else {
0149       u += -1;
0150       v += -1;
0151     }
0152   }
0153   if (debug)
0154     edm::LogVerbatim("HGCalGeom") << "cellUVFromXY2: Input " << xloc << ":" << yloc << ":" << extend << " Output " << u
0155                                   << ":" << v;
0156   return std::make_pair(u, v);
0157 }
0158 
0159 std::pair<int32_t, int32_t> HGCalCellUV::cellUVFromXY3(
0160     double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug) const {
0161   //--- Using Cube coordinates to find the (u, v)
0162   //--- Reverse transform to placement=0, if placement index ≠ 6
0163   if (type != 0)
0164     type = 1;
0165   double cellX = (extend) ? cellXTotal_[type] : cellX_[type];
0166   double cellY = (extend) ? cellYTotal_[type] : cellY_[type];
0167   double xloc1 = (placement >= HGCalCell::cellPlacementExtra) ? xloc : -xloc;
0168   int rot = placement % HGCalCell::cellPlacementExtra;
0169   static constexpr std::array<double, 6> fcos = {{1.0, cos60_, -cos60_, -1.0, -cos60_, cos60_}};
0170   static constexpr std::array<double, 6> fsin = {{0.0, sin60_, sin60_, 0.0, -sin60_, -sin60_}};
0171   double xprime = xloc1 * fcos[rot] - yloc * fsin[rot];
0172   double yprime = xloc1 * fsin[rot] + yloc * fcos[rot];
0173   double x = xprime + cellX;
0174   double y = yprime;
0175 
0176   x = x / cellX;
0177   y = y / cellY;
0178 
0179   double cu = 2 * x / 3;
0180   double cv = -x / 3 + y / 2;
0181   double cw = -x / 3 - y / 2;
0182 
0183   int iu = std::round(cu);
0184   int iv = std::round(cv);
0185   int iw = std::round(cw);
0186 
0187   if (iu + iv + iw != 0) {
0188     double arr[] = {std::abs(cu - iu), std::abs(cv - iv), std::abs(cw - iw)};
0189     int i = std::distance(arr, std::max_element(arr, arr + 3));
0190 
0191     if (i == 1)
0192       iv = (std::round(cv) == std::floor(cv)) ? std::ceil(cv) : std::floor(cv);
0193     else if (i == 2)
0194       iw = (std::round(cw) == std::floor(cw)) ? std::ceil(cw) : std::floor(cw);
0195   }
0196 
0197   //--- Taking care of extending cells
0198   int u(ncell_[type] + iv), v(ncell_[type] - 1 - iw);
0199   double xcell = (1.5 * (v - u) + 0.5) * cellX_[type];
0200   double ycell = (v + u - 2 * ncell_[type] + 1) * cellY;
0201   if (v == -1) {
0202     if ((yprime - sqrt3_ * xprime) < (ycell - sqrt3_ * xcell)) {
0203       v += 1;
0204     } else {
0205       u += 1;
0206       v += 1;
0207     }
0208   }
0209   if (v - u == ncell_[type]) {
0210     if (yprime < ycell) {
0211       v += -1;
0212     } else {
0213       u += 1;
0214     }
0215   }
0216   if (u == 2 * ncell_[type]) {
0217     if ((yprime + sqrt3_ * xprime) > (ycell + sqrt3_ * xcell)) {
0218       u += -1;
0219     } else {
0220       u += -1;
0221       v += -1;
0222     }
0223   }
0224 
0225   if (debug)
0226     edm::LogVerbatim("HGCalGeom") << "cellUVFromXY3: Input " << xloc << ":" << yloc << ":" << extend << " Output " << u
0227                                   << ":" << v;
0228   return std::make_pair(u, v);
0229 }
0230 
0231 std::pair<int, int> HGCalCellUV::cellUVFromXY4(
0232     double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug) {
0233   if (type != 0)
0234     return cellUVFromXY4(
0235         xloc, yloc, ncell_[1], cellX_[1], cellY_[1], cellXTotal_[1], cellY_[1], cellPosCoarse_[placement], extend, debug);
0236   else
0237     return cellUVFromXY4(
0238         xloc, yloc, ncell_[0], cellX_[0], cellY_[0], cellXTotal_[0], cellY_[0], cellPosFine_[placement], extend, debug);
0239 }
0240 
0241 std::pair<int, int> HGCalCellUV::cellUVFromXY4(double xloc,
0242                                                double yloc,
0243                                                int n,
0244                                                double cellX,
0245                                                double cellY,
0246                                                double cellXTotal,
0247                                                double cellYTotal,
0248                                                std::map<std::pair<int, int>, std::pair<double, double> >& cellPos,
0249                                                bool extend,
0250                                                bool debug) {
0251   std::pair<int, int> uv = std::make_pair(-1, -1);
0252   std::map<std::pair<int, int>, std::pair<double, double> >::const_iterator itr;
0253   for (itr = cellPos.begin(); itr != cellPos.end(); ++itr) {
0254     double delX = std::abs(xloc - (itr->second).first);
0255     double delY = std::abs(yloc - (itr->second).second);
0256     if ((delX < cellX) && (delY < cellY)) {
0257       if ((delX < (0.5 * cellX)) || (delY < (2.0 * cellY - sqrt3_ * delX))) {
0258         uv = itr->first;
0259         break;
0260       }
0261     }
0262   }
0263   if ((uv.first < 0) && extend) {
0264     for (itr = cellPos.begin(); itr != cellPos.end(); ++itr) {
0265       double delX = std::abs(xloc - (itr->second).first);
0266       double delY = std::abs(yloc - (itr->second).second);
0267       if ((delX < cellXTotal) && (delY < cellYTotal)) {
0268         if ((delX < (0.5 * cellXTotal)) || (delY < (2.0 * cellYTotal - sqrt3_ * delX))) {
0269           uv = itr->first;
0270           break;
0271         }
0272       }
0273     }
0274   }
0275   if (debug)
0276     edm::LogVerbatim("HGCalGeom") << "cellUVFromXY4: Input " << xloc << ":" << yloc << ":" << extend << " Output "
0277                                   << uv.first << ":" << uv.second;
0278   return uv;
0279 }
0280 
0281 std::pair<int32_t, int32_t> HGCalCellUV::cellUVFromXY1(
0282     double xloc, double yloc, int32_t placement, int32_t type, int32_t partial, bool extend, bool debug) const {
0283   if (type != 0)
0284     type = 1;
0285   double cellX = (extend) ? cellXTotal_[type] : cellX_[type];
0286   double cellY = (extend) ? cellYTotal_[type] : cellY_[type];
0287   std::pair<int, int> uv = HGCalCellUV::cellUVFromXY1(xloc, yloc, placement, type, extend, debug);
0288   int u = uv.first;
0289   int v = uv.second;
0290   if (partial == HGCalTypes::WaferLDTop) {
0291     if (u * HGCalTypes::edgeWaferLDTop[0] + v * HGCalTypes::edgeWaferLDTop[1] == HGCalTypes::edgeWaferLDTop[2] + 1) {
0292       double xloc1 = (placement >= HGCalCell::cellPlacementExtra) ? xloc : -xloc;
0293       int rot = placement % HGCalCell::cellPlacementExtra;
0294       static constexpr std::array<double, 6> fcos = {{1.0, cos60_, -cos60_, -1.0, -cos60_, cos60_}};
0295       static constexpr std::array<double, 6> fsin = {{0.0, sin60_, sin60_, 0.0, -sin60_, -sin60_}};
0296       double xprime = -1 * (xloc1 * fcos[rot] - yloc * fsin[rot]);
0297       double yprime = xloc1 * fsin[rot] + yloc * fcos[rot];
0298       double xcell = -1 * (1.5 * (v - u) + 0.5) * cellX;
0299       double ycell = (v + u - 2 * ncell_[type] + 1) * cellY;
0300       if ((yprime - sqrt3_ * xprime) > (ycell - sqrt3_ * xcell)) {
0301         u += -1;
0302       } else {
0303         u += -1;
0304         v += -1;
0305       }
0306     }
0307   } else if (partial == HGCalTypes::WaferHDBottom) {
0308     if (u * HGCalTypes::edgeWaferHDBottom[0] + v * HGCalTypes::edgeWaferHDBottom[1] ==
0309         HGCalTypes::edgeWaferHDBottom[2] + 1) {
0310       double xloc1 = (placement >= HGCalCell::cellPlacementExtra) ? xloc : -xloc;
0311       int rot = placement % HGCalCell::cellPlacementExtra;
0312       static constexpr std::array<double, 6> fcos = {{1.0, cos60_, -cos60_, -1.0, -cos60_, cos60_}};
0313       static constexpr std::array<double, 6> fsin = {{0.0, sin60_, sin60_, 0.0, -sin60_, -sin60_}};
0314       double xprime = -1 * (xloc1 * fcos[rot] - yloc * fsin[rot]);
0315       double yprime = xloc1 * fsin[rot] + yloc * fcos[rot];
0316       double xcell = -1 * (1.5 * (v - u) + 0.5) * cellX;
0317       double ycell = (v + u - 2 * ncell_[type] + 1) * cellY;
0318       if ((yprime - sqrt3_ * xprime) > (ycell - sqrt3_ * xcell)) {
0319         u += 1;
0320         v += 1;
0321       } else {
0322         u += 1;
0323       }
0324     }
0325   }
0326   if (debug)
0327     edm::LogVerbatim("HGCalGeom") << "cellUVFromXY5: Input " << xloc << ":" << yloc << ":" << extend << " Output " << u
0328                                   << ":" << v;
0329   return std::make_pair(u, v);
0330 }