CMS 3D CMS Logo

HGCalCellUV.cc
Go to the documentation of this file.
3 #include <iostream>
4 #include <array>
5 #include <algorithm>
6 #include <cassert>
7 
8 HGCalCellUV::HGCalCellUV(double waferSize, double separation, int32_t nFine, int32_t nCoarse) {
9  HGCalCell hgcalcell(waferSize, nFine, nCoarse);
10  assert(nFine > 0 && nCoarse > 0);
11  ncell_[0] = nFine;
12  ncell_[1] = nCoarse;
13  for (int k = 0; k < 2; ++k) {
14  cellX_[k] = waferSize / (3 * ncell_[k]);
15  cellY_[k] = 0.5 * sqrt3_ * cellX_[k];
16  cellXTotal_[k] = (waferSize + separation) / (3 * ncell_[k]);
17  cellY_[k] = 0.5 * sqrt3_ * cellXTotal_[k];
18  }
19 
20  // Fill up the placement vectors
21  for (int placement = 0; placement < HGCalCell::cellPlacementTotal; ++placement) {
22  // Fine cells
23  for (int iu = 0; iu < 2 * ncell_[0]; ++iu) {
24  for (int iv = 0; iv < 2 * ncell_[0]; ++iv) {
25  int u = (placement < HGCalCell::cellPlacementExtra) ? iv : iu;
26  int v = (placement < HGCalCell::cellPlacementExtra) ? iu : iv;
27  if (((v - u) < ncell_[0]) && (u - v) <= ncell_[0]) {
28  cellPosFine_[placement][std::pair<int, int>(u, v)] = hgcalcell.cellUV2XY1(u, v, placement, 0);
29  }
30  }
31  }
32  // Coarse cells
33  for (int iu = 0; iu < 2 * ncell_[1]; ++iu) {
34  for (int iv = 0; iv < 2 * ncell_[1]; ++iv) {
35  int u = (placement < HGCalCell::cellPlacementExtra) ? iv : iu;
36  int v = (placement < HGCalCell::cellPlacementExtra) ? iu : iv;
37  if (((v - u) < ncell_[1]) && (u - v) <= ncell_[1]) {
38  cellPosCoarse_[placement][std::pair<int, int>(u, v)] = hgcalcell.cellUV2XY1(u, v, placement, 1);
39  }
40  }
41  }
42  }
43 }
44 
45 std::pair<int32_t, int32_t> HGCalCellUV::cellUVFromXY1(
46  double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug) {
47  //--- Reverse transform to placement=0, if placement index ≠ 6
48  double xloc1 = (placement >= 6) ? xloc : -xloc;
49  int rot = placement % 6;
50  static constexpr std::array<double, 6> fcos = {{1.0, cos60_, -cos60_, -1.0, -cos60_, cos60_}};
51  static constexpr std::array<double, 6> fsin = {{0.0, sin60_, sin60_, 0.0, -sin60_, -sin60_}};
52  double x = xloc1 * fcos[rot] - yloc * fsin[rot];
53  double y = xloc1 * fsin[rot] + yloc * fcos[rot];
54 
55  //--- Calculate coordinates in u,v,w system
56  double u = x * sin60_ + y * cos60_;
57  double v = -x * sin60_ + y * cos60_;
58  double w = y;
59 
60  //--- Rounding in u, v, w coordinates
61  int iu = std::floor(u / cellY_[type]) + 3 * (ncell_[type]) - 1;
62  int iv = std::floor(v / cellY_[type]) + 3 * (ncell_[type]);
63  int iw = std::floor(w / cellY_[type]) + 1;
64 
65  int isv = (iu + iw) / 3;
66  int isu = (iv + iw) / 3;
67 
68  //--- Taking care of extending cells
69  if ((iu + iw) < 0) {
70  isu = (iv + iw + 1) / 3;
71  isv = 0;
72  } else if (isv - isu > ncell_[type] - 1) {
73  isu = (iv + iw + 1) / 3;
74  isv = (iu + iw - 1) / 3;
75  } else if (isu > 2 * ncell_[type] - 1) {
76  isu = 2 * ncell_[type] - 1;
77  isv = (iu + iw - 1) / 3;
78  }
79  if (debug)
80  edm::LogVerbatim("HGCalGeom") << "cellUVFromXY1: Input " << xloc << ":" << yloc << ":" << extend << " Output "
81  << isu << ":" << isv;
82  return std::make_pair(isu, isv);
83 }
84 
85 std::pair<int32_t, int32_t> HGCalCellUV::cellUVFromXY2(
86  double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug) {
87  //--- Using multiple inequalities to find (u, v)
88  //--- Reverse transform to placement=0, if placement index ≠ 7
89  double xloc1 = (placement >= 6) ? xloc : -1 * xloc;
90  int rot = placement % 6;
91  static constexpr std::array<double, 6> fcos = {{cos60_, 1.0, cos60_, -cos60_, -1.0, -cos60_}};
92  static constexpr std::array<double, 6> fsin = {{-sin60_, 0.0, sin60_, sin60_, 0.0, -sin60_}};
93  double x = xloc1 * fcos[rot] - yloc * fsin[rot];
94  double y = xloc1 * fsin[rot] + yloc * fcos[rot];
95 
96  int32_t u(-100), v(-100);
97  int ncell = (type != 0) ? ncell_[1] : ncell_[0];
98  double r = (type != 0) ? cellY_[1] : cellY_[0];
99  double l1 = (y / r) + ncell - 1.0;
100  int l2 = std::floor((0.5 * y + 0.5 * x / sqrt3_) / r + ncell - 4.0 / 3.0);
101  int l3 = std::floor((x / sqrt3_) / r + ncell - 4.0 / 3.0);
102  double l4 = (y + sqrt3_ * x) / (2 * r) + 2 * ncell - 2;
103  double l5 = (y - sqrt3_ * x) / (2 * r) - ncell;
104  double u1 = (y / r) + ncell + 1.0;
105  int u2 = std::ceil((0.5 * y + 0.5 * x / sqrt3_) / r + ncell + 2.0 / 3.0);
106  int u3 = std::ceil((x / sqrt3_) / r + ncell);
107  double u4 = l4 + 2;
108  double u5 = l5 + 2;
109 
110  for (int ui = l2 + 1; ui < u2; ui++) {
111  for (int vi = l3 + 1; vi < u3; vi++) {
112  int c1 = 2 * ui - vi;
113  int c4 = ui + vi;
114  int c5 = ui - 2 * vi;
115  if ((c1 < u1) && (c1 > l1) && (c4 < u4) && (c4 > l4) && (c5 < u5) && (c5 > l5)) {
116  u = ui;
117  v = vi;
118  }
119  }
120  }
121 
122  //--- Taking care of extending cells
123  if (v == -1) {
124  if (y < (2 * u - v - ncell) * r) {
125  v += 1;
126  } else {
127  u += 1;
128  v += 1;
129  }
130  }
131  if (v - u == ncell) {
132  if ((y + sqrt3_ * x) < ((u + v - 2 * ncell + 1) * 2 * r)) {
133  v += -1;
134  } else {
135  u += 1;
136  }
137  }
138  if (u == 2 * ncell) {
139  if ((y - sqrt3_ * x) < ((u - 2 * v + ncell - 1) * 2 * r)) {
140  u += -1;
141  } else {
142  u += -1;
143  v += -1;
144  }
145  }
146  if (debug)
147  edm::LogVerbatim("HGCalGeom") << "cellUVFromXY2: Input " << xloc << ":" << yloc << ":" << extend << " Output " << u
148  << ":" << v;
149  return std::make_pair(u, v);
150 }
151 
152 std::pair<int32_t, int32_t> HGCalCellUV::cellUVFromXY3(
153  double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug) {
154  //--- Using Cube coordinates to find the (u, v)
155  //--- Reverse transform to placement=0, if placement index ≠ 6
156  double xloc1 = (placement >= 6) ? xloc : -xloc;
157  int rot = placement % 6;
158  static constexpr std::array<double, 6> fcos = {{1.0, cos60_, -cos60_, -1.0, -cos60_, cos60_}};
159  static constexpr std::array<double, 6> fsin = {{0.0, sin60_, sin60_, 0.0, -sin60_, -sin60_}};
160  double xprime = xloc1 * fcos[rot] - yloc * fsin[rot];
161  double yprime = xloc1 * fsin[rot] + yloc * fcos[rot];
162  double x = xprime + cellX_[type];
163  double y = yprime;
164 
165  x = x / cellX_[type];
166  y = y / cellY_[type];
167 
168  double cu = 2 * x / 3;
169  double cv = -x / 3 + y / 2;
170  double cw = -x / 3 - y / 2;
171 
172  int iu = std::round(cu);
173  int iv = std::round(cv);
174  int iw = std::round(cw);
175 
176  if (iu + iv + iw != 0) {
177  double arr[] = {std::abs(cu - iu), std::abs(cv - iv), std::abs(cw - iw)};
178  int i = std::distance(arr, std::max_element(arr, arr + 3));
179 
180  if (i == 1)
181  iv = (std::round(cv) == std::floor(cv)) ? std::ceil(cv) : std::floor(cv);
182  else if (i == 2)
183  iw = (std::round(cw) == std::floor(cw)) ? std::ceil(cw) : std::floor(cw);
184  }
185 
186  //--- Taking care of extending cells
187  int u(ncell_[type] + iv), v(ncell_[type] - 1 - iw);
188  double xcell = (1.5 * (v - u) + 0.5) * cellX_[type];
189  double ycell = (v + u - 2 * ncell_[type] + 1) * cellY_[type];
190  if (v == -1) {
191  if ((yprime - sqrt3_ * xprime) < (ycell - sqrt3_ * xcell)) {
192  v += 1;
193  } else {
194  u += 1;
195  v += 1;
196  }
197  }
198  if (v - u == ncell_[type]) {
199  if (yprime < ycell) {
200  v += -1;
201  } else {
202  u += 1;
203  }
204  }
205  if (u == 2 * ncell_[type]) {
206  if ((yprime + sqrt3_ * xprime) > (ycell + sqrt3_ * xcell)) {
207  u += -1;
208  } else {
209  u += -1;
210  v += -1;
211  }
212  }
213 
214  if (debug)
215  edm::LogVerbatim("HGCalGeom") << "cellUVFromXY3: Input " << xloc << ":" << yloc << ":" << extend << " Output " << u
216  << ":" << v;
217  return std::make_pair(u, v);
218 }
219 
220 std::pair<int, int> HGCalCellUV::cellUVFromXY4(
221  double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug) {
222  if (type != 0)
223  return cellUVFromXY4(
224  xloc, yloc, ncell_[1], cellX_[1], cellY_[1], cellXTotal_[1], cellY_[1], cellPosCoarse_[placement], extend, debug);
225  else
226  return cellUVFromXY4(
227  xloc, yloc, ncell_[0], cellX_[0], cellY_[0], cellXTotal_[0], cellY_[0], cellPosFine_[placement], extend, debug);
228 }
229 
230 std::pair<int, int> HGCalCellUV::cellUVFromXY4(double xloc,
231  double yloc,
232  int n,
233  double cellX,
234  double cellY,
235  double cellXTotal,
236  double cellYTotal,
237  std::map<std::pair<int, int>, std::pair<double, double> >& cellPos,
238  bool extend,
239  bool debug) {
240  std::pair<int, int> uv = std::make_pair(-1, -1);
241  std::map<std::pair<int, int>, std::pair<double, double> >::const_iterator itr;
242  for (itr = cellPos.begin(); itr != cellPos.end(); ++itr) {
243  double delX = std::abs(xloc - (itr->second).first);
244  double delY = std::abs(yloc - (itr->second).second);
245  if ((delX < cellX) && (delY < cellY)) {
246  if ((delX < (0.5 * cellX)) || (delY < (2.0 * cellY - sqrt3_ * delX))) {
247  uv = itr->first;
248  break;
249  }
250  }
251  }
252  if ((uv.first < 0) && extend) {
253  for (itr = cellPos.begin(); itr != cellPos.end(); ++itr) {
254  double delX = std::abs(xloc - (itr->second).first);
255  double delY = std::abs(yloc - (itr->second).second);
256  if ((delX < cellXTotal) && (delY < cellYTotal)) {
257  if ((delX < (0.5 * cellXTotal)) || (delY < (2.0 * cellYTotal - sqrt3_ * delX))) {
258  uv = itr->first;
259  break;
260  }
261  }
262  }
263  }
264  if (debug)
265  edm::LogVerbatim("HGCalGeom") << "cellUVFromXY4: Input " << xloc << ":" << yloc << ":" << extend << " Output "
266  << uv.first << ":" << uv.second;
267  return uv;
268 }
Log< level::Info, true > LogVerbatim
constexpr int32_t ceil(float num)
double waferSize
Definition: HGCalCellUV.h:42
int32_t *__restrict__ iv
std::pair< int32_t, int32_t > cellUVFromXY2(double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug)
Definition: HGCalCellUV.cc:85
T w() const
static constexpr double sqrt3_
Definition: HGCalCellUV.h:37
std::map< std::pair< int32_t, int32_t >, std::pair< double, double > > cellPosFine_[HGCalCell::cellPlacementTotal]
Definition: HGCalCellUV.h:44
static constexpr int32_t cellPlacementTotal
Definition: HGCalCell.h:26
double cellXTotal_[2]
Definition: HGCalCellUV.h:42
cv
Definition: cuy.py:363
std::pair< int32_t, int32_t > cellUVFromXY3(double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug)
Definition: HGCalCellUV.cc:152
assert(be >=bs)
double cellY_[2]
Definition: HGCalCellUV.h:42
double cellX_[2]
Definition: HGCalCellUV.h:42
std::pair< int32_t, int32_t > cellUVFromXY1(double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug)
Definition: HGCalCellUV.cc:45
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static constexpr double cos60_
Definition: HGCalCellUV.h:39
#define debug
Definition: HDRShower.cc:19
int32_t ncell_[2]
Definition: HGCalCellUV.h:41
std::pair< double, double > cellUV2XY1(int32_t u, int32_t v, int32_t placementIndex, int32_t type)
Definition: HGCalCell.cc:13
HGCalCellUV(double waferSize, double separation, int32_t nFine, int32_t nCoarse)
Definition: HGCalCellUV.cc:8
static constexpr double sin60_
Definition: HGCalCellUV.h:38
std::map< std::pair< int32_t, int32_t >, std::pair< double, double > > cellPosCoarse_[HGCalCell::cellPlacementTotal]
Definition: HGCalCellUV.h:44
std::pair< int32_t, int32_t > cellUVFromXY4(double xloc, double yloc, int32_t placement, int32_t type, bool extend, bool debug)
Definition: HGCalCellUV.cc:220
static constexpr int32_t cellPlacementExtra
Definition: HGCalCell.h:24