CMS 3D CMS Logo

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