CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HGCalDDDConstants.cc
Go to the documentation of this file.
2 
17 
18 #include <algorithm>
19 #include <bitset>
20 #include <iterator>
21 #include <functional>
22 #include <numeric>
23 
24 //#define EDM_ML_DEBUG
25 using namespace geant_units::operators;
26 
28  : hgpar_(hp),
29  sqrt3_(std::sqrt(3.0)),
30  mode_(hgpar_->mode_),
31  fullAndPart_(((mode_ == HGCalGeometryMode::Hexagon8File) || (mode_ == HGCalGeometryMode::Hexagon8Module))) {
32 #ifdef EDM_ML_DEBUG
33  edm::LogVerbatim("HGCalGeom") << "Mode " << mode_ << " FullAndPart " << fullAndPart_;
34 #endif
35  if (waferHexagon6() || waferHexagon8()) {
38  hexside_ = 2.0 * rmax_ * tan30deg_;
39  hexsideT_ = 2.0 * rmaxT_ * tan30deg_;
40 #ifdef EDM_ML_DEBUG
41  edm::LogVerbatim("HGCalGeom") << "rmax_ " << rmax_ << ":" << rmaxT_ << ":" << hexside_ << ":" << hexsideT_
42  << " CellSize " << 0.5 * HGCalParameters::k_ScaleFromDDD * hgpar_->cellSize_[0] << ":"
44 #endif
45  }
46  // init maps and constants
47  modHalf_ = 0;
49  for (int simreco = 0; simreco < 2; ++simreco) {
50  tot_layers_[simreco] = layersInit((bool)simreco);
51  max_modules_layer_[simreco].resize(tot_layers_[simreco] + 1);
52  for (unsigned int layer = 1; layer <= tot_layers_[simreco]; ++layer) {
53  max_modules_layer_[simreco][layer] = modulesInit(layer, (bool)simreco);
54  if (simreco == 1) {
55  modHalf_ += max_modules_layer_[simreco][layer];
57 #ifdef EDM_ML_DEBUG
58  edm::LogVerbatim("HGCalGeom") << "Layer " << layer << " with " << max_modules_layer_[simreco][layer] << ":"
59  << modHalf_ << " modules in RECO";
60  } else {
61  edm::LogVerbatim("HGCalGeom") << "Layer " << layer << " with " << max_modules_layer_[simreco][layer]
62  << " modules in SIM";
63 #endif
64  }
65  }
66 #ifdef EDM_ML_DEBUG
67  edm::LogVerbatim("HGCalGeom") << "SimReco " << simreco << " with " << tot_layers_[simreco] << " Layers";
68 #endif
69  }
70  tot_wafers_ = wafers();
71 
72 #ifdef EDM_ML_DEBUG
73  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants initialized for " << name << " with " << layers(false) << ":"
74  << layers(true) << " layers, " << wafers() << ":" << 2 * modHalf_
75  << " wafers with maximum " << maxWafersPerLayer_ << " per layer and "
76  << "maximum of " << maxCells(false) << ":" << maxCells(true) << " cells";
77 #endif
78  if (waferHexagon6() || waferHexagon8()) {
79  int wminT(9999999), wmaxT(-9999999), kount1(0), kount2(0);
80  for (unsigned int i = 0; i < getTrFormN(); ++i) {
81  int lay0 = getTrForm(i).lay;
82  int wmin(9999999), wmax(-9999999), kount(0);
83  for (int wafer = 0; wafer < sectors(); ++wafer) {
84  bool waferIn = waferInLayer(wafer, lay0, true);
85  if (waferHexagon8()) {
86  int kndx = HGCalWaferIndex::waferIndex(lay0,
89  waferIn_[kndx] = waferIn;
90  }
91  if (waferIn) {
92  int waferU = ((waferHexagon6()) ? wafer : HGCalWaferIndex::waferU(hgpar_->waferCopy_[wafer]));
93  if (waferU < wmin)
94  wmin = waferU;
95  if (waferU > wmax)
96  wmax = waferU;
97  ++kount;
98  }
99  }
100  if (wminT > wmin)
101  wminT = wmin;
102  if (wmaxT < wmax)
103  wmaxT = wmax;
104  if (kount1 < kount)
105  kount1 = kount;
106  kount2 += kount;
107 #ifdef EDM_ML_DEBUG
108  int lay1 = getIndex(lay0, true).first;
109  edm::LogVerbatim("HGCalGeom") << "Index " << i << " Layer " << lay0 << ":" << lay1 << " Wafer " << wmin << ":"
110  << wmax << ":" << kount;
111 #endif
112  HGCWaferParam a1{{wmin, wmax, kount}};
113  waferLayer_[lay0] = a1;
114  }
115  waferMax_ = std::array<int, 4>{{wminT, wmaxT, kount1, kount2}};
116 #ifdef EDM_ML_DEBUG
117  edm::LogVerbatim("HGCalGeom") << "Overall wafer statistics: " << wminT << ":" << wmaxT << ":" << kount1 << ":"
118  << kount2;
119 #endif
120  }
121 }
122 
124 
125 std::pair<int, int> HGCalDDDConstants::assignCell(float x, float y, int lay, int subSec, bool reco) const {
126  const auto& index = getIndex(lay, reco);
127  if (index.first < 0)
128  return std::make_pair(-1, -1);
129  if (waferHexagon6()) {
130  float xx = (reco) ? x : HGCalParameters::k_ScaleFromDDD * x;
131  float yy = (reco) ? y : HGCalParameters::k_ScaleFromDDD * y;
132 
133  // First the wafer
134  int wafer = cellHex(xx, yy, rmax_, hgpar_->waferPosX_, hgpar_->waferPosY_);
135  if (wafer < 0 || wafer >= (int)(hgpar_->waferTypeT_.size())) {
136  edm::LogWarning("HGCalGeom") << "Wafer no. out of bound for " << wafer << ":" << (hgpar_->waferTypeT_).size()
137  << ":" << (hgpar_->waferPosX_).size() << ":" << (hgpar_->waferPosY_).size()
138  << " ***** ERROR *****";
139  return std::make_pair(-1, -1);
140  } else {
141  // Now the cell
142  xx -= hgpar_->waferPosX_[wafer];
143  yy -= hgpar_->waferPosY_[wafer];
144  if (hgpar_->waferTypeT_[wafer] == 1)
145  return std::make_pair(wafer,
146  cellHex(xx,
147  yy,
150  hgpar_->cellFineY_));
151  else
152  return std::make_pair(wafer,
153  cellHex(xx,
154  yy,
157  hgpar_->cellCoarseY_));
158  }
159  } else {
160  return std::make_pair(-1, -1);
161  }
162 }
163 
165  float x, float y, int lay, bool reco, bool extend, bool debug) const {
166  int waferU(0), waferV(0), waferType(-1), cellU(0), cellV(0);
167  if (waferHexagon8()) {
168  double xx = (reco) ? HGCalParameters::k_ScaleToDDD * x : x;
169  double yy = (reco) ? HGCalParameters::k_ScaleToDDD * y : y;
170  double wt(1.0);
171 #ifdef EDM_ML_DEBUG
172  edm::LogVerbatim("HGCalGeom") << "assignCellHex x " << x << ":" << xx << " y " << y << ":" << yy << " Lay " << lay;
173 #endif
174  waferFromPosition(xx, yy, lay, waferU, waferV, cellU, cellV, waferType, wt, extend, debug);
175  }
176  return std::array<int, 5>{{waferU, waferV, waferType, cellU, cellV}};
177 }
178 
179 std::array<int, 3> HGCalDDDConstants::assignCellTrap(float x, float y, float z, int layer, bool reco) const {
180  int irad(-1), iphi(-1), type(-1);
181  const auto& indx = getIndex(layer, reco);
182  if (indx.first < 0)
183  return std::array<int, 3>{{irad, iphi, type}};
184  double xx = (z > 0) ? x : -x;
185  double r = (reco ? std::sqrt(x * x + y * y) : HGCalParameters::k_ScaleFromDDD * std::sqrt(x * x + y * y));
186  double phi = (r == 0. ? 0. : std::atan2(y, xx));
187  if (phi < 0)
188  phi += (2.0 * M_PI);
189  type = hgpar_->scintType(layer);
190  auto ir = std::lower_bound(hgpar_->radiusLayer_[type].begin(), hgpar_->radiusLayer_[type].end(), r);
191  irad = (int)(ir - hgpar_->radiusLayer_[type].begin());
192  irad = std::clamp(irad, hgpar_->iradMinBH_[indx.first], hgpar_->iradMaxBH_[indx.first]);
193  iphi = 1 + (int)(phi / indx.second);
194 #ifdef EDM_ML_DEBUG
195  edm::LogVerbatim("HGCalGeom") << "assignCellTrap Input " << x << ":" << y << ":" << z << ":" << layer << ":" << reco
196  << " x|r " << xx << ":" << r << " phi " << phi << " o/p " << irad << ":" << iphi << ":"
197  << type;
198 #endif
199  return std::array<int, 3>{{irad, iphi, type}};
200 }
201 
202 std::pair<double, double> HGCalDDDConstants::cellEtaPhiTrap(int type, int irad) const {
203  double dr(0), df(0);
204  if (tileTrapezoid()) {
205  double r = 0.5 * ((hgpar_->radiusLayer_[type][irad - 1] + hgpar_->radiusLayer_[type][irad]));
206  dr = (hgpar_->radiusLayer_[type][irad] - hgpar_->radiusLayer_[type][irad - 1]);
207  df = r * hgpar_->cellSize_[type];
208  }
209  return std::make_pair(dr, df);
210 }
211 
212 bool HGCalDDDConstants::cellInLayer(int waferU, int waferV, int cellU, int cellV, int lay, bool reco) const {
213  const auto& indx = getIndex(lay, true);
214  if (indx.first >= 0) {
215  if (waferHexagon8() || waferHexagon6()) {
216  const auto& xy = ((waferHexagon8()) ? locateCell(lay, waferU, waferV, cellU, cellV, reco, true, false, false)
217  : locateCell(cellU, lay, waferU, reco));
218  double rpos = sqrt(xy.first * xy.first + xy.second * xy.second);
219  return ((rpos >= hgpar_->rMinLayHex_[indx.first]) && (rpos <= hgpar_->rMaxLayHex_[indx.first]));
220  } else {
221  return true;
222  }
223  } else {
224  return false;
225  }
226 }
227 
229  double thick(-1);
230  int type = waferType(layer, waferU, waferV, false);
231  if (type >= 0) {
232  if (waferHexagon8()) {
233  thick = 10000.0 * hgpar_->cellThickness_[type]; // cm to micron
234  } else if (waferHexagon6()) {
235  thick = 100.0 * (type + 1); // type = 1,2,3 for 100,200,300 micron
236  }
237  }
238  return thick;
239 }
240 
242  int indx = ((waferHexagon8()) ? ((type >= 1) ? 1 : 0) : ((type == 1) ? 1 : 0));
243  double cell = (tileTrapezoid() ? 0.5 * hgpar_->cellSize_[indx]
245  return cell;
246 }
247 
248 HGCalTypes::CellType HGCalDDDConstants::cellType(int type, int cellU, int cellV) const {
249  // type=0: in the middle; 1..6: the edges clocwise from bottom left;
250  // =11..16: the corners clockwise from bottom
251  int N = (type == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
252  if (cellU == 0) {
253  if (cellV == 0)
255  else if (cellV - cellU == N - 1)
257  else
259  } else if (cellV == 0) {
260  if (cellU - cellV == N)
262  else
264  } else if (cellU - cellV == N) {
265  if (cellU == 2 * N - 1)
267  else
269  } else if (cellU == 2 * N - 1) {
270  if (cellV == 2 * N - 1)
272  else
274  } else if (cellV == 2 * N - 1) {
275  if (cellV - cellU == N - 1)
277  else
279  } else if (cellV - cellU == N - 1) {
281  } else if ((cellU > 2 * N - 1) || (cellV > 2 * N - 1) || (cellV >= (cellU + N)) || (cellU > (cellV + N))) {
283  } else {
285  }
286 }
287 
288 double HGCalDDDConstants::distFromEdgeHex(double x, double y, double z) const {
289  // Assming the point is within a hexagonal plane of the wafer, calculate
290  // the shortest distance from the edge
291  if (z < 0)
292  x = -x;
293  double dist(0);
294  // Input x, y in Geant4 unit and transformed to CMSSW standard
295  double xx = HGCalParameters::k_ScaleFromDDD * x;
296  double yy = HGCalParameters::k_ScaleFromDDD * y;
297  if (waferHexagon8()) {
298  int ll = layerIndex(getLayer(z, false), false);
299  xx -= hgpar_->xLayerHex_[ll];
300  yy -= hgpar_->yLayerHex_[ll];
301  }
302  int sizew = (int)(hgpar_->waferPosX_.size());
303  int wafer = sizew;
304  // Transform to the local coordinate frame of the wafer first
305  for (int k = 0; k < sizew; ++k) {
306  double dx = std::abs(xx - hgpar_->waferPosX_[k]);
307  double dy = std::abs(yy - hgpar_->waferPosY_[k]);
308  if ((dx <= rmax_) && (dy <= hexside_) && ((dy <= 0.5 * hexside_) || (dx * tan30deg_ <= (hexside_ - dy)))) {
309  wafer = k;
310  xx -= hgpar_->waferPosX_[k];
311  yy -= hgpar_->waferPosY_[k];
312  break;
313  }
314  }
315  // Look at only one quarter (both x,y are positive)
316  if (wafer < sizew) {
317  if (std::abs(yy) < 0.5 * hexside_) {
318  dist = rmax_ - std::abs(xx);
319  } else {
320  dist = 0.5 * ((rmax_ - std::abs(xx)) - sqrt3_ * (std::abs(yy) - 0.5 * hexside_));
321  }
322  } else {
323  dist = 0;
324  }
326 #ifdef EDM_ML_DEBUG
327  edm::LogVerbatim("HGCalGeom") << "DistFromEdgeHex: Local " << xx << ":" << yy << " wafer " << wafer << " flag "
328  << (wafer < sizew) << " Distance " << rmax_ << ":" << (rmax_ - std::abs(xx)) << ":"
329  << (std::abs(yy) - 0.5 * hexside_) << ":" << 0.5 * hexside_ << ":" << dist;
330 #endif
331  return dist;
332 }
333 
334 double HGCalDDDConstants::distFromEdgeTrap(double x, double y, double z) const {
335  // Assming the point is within the eta-phi plane of the scintillator tile,
336  // calculate the shortest distance from the edge
337  int lay = getLayer(z, false);
338  double xx = (z < 0) ? -x : x;
339  int indx = layerIndex(lay, false);
340  double r = HGCalParameters::k_ScaleFromDDD * std::sqrt(x * x + y * y);
341  double phi = (r == 0. ? 0. : std::atan2(y, xx));
342  if (phi < 0)
343  phi += (2.0 * M_PI);
344  int type = hgpar_->scintType(lay);
345  double cell = hgpar_->scintCellSize(lay);
346  // Compare with the center of the tile find distances along R and also phi
347  // Take the smaller value
348  auto ir = std::lower_bound(hgpar_->radiusLayer_[type].begin(), hgpar_->radiusLayer_[type].end(), r);
349  int irad = (int)(ir - hgpar_->radiusLayer_[type].begin());
350  irad = std::clamp(irad, hgpar_->iradMinBH_[indx], hgpar_->iradMaxBH_[indx]);
351  int iphi = 1 + (int)(phi / cell);
352  double dphi = std::max(0.0, (0.5 * cell - std::abs(phi - (iphi - 0.5) * cell)));
353  double dist = std::min((r - hgpar_->radiusLayer_[type][irad - 1]), (hgpar_->radiusLayer_[type][irad] - r));
354 #ifdef EDM_ML_DEBUG
355  edm::LogVerbatim("HGCalGeom") << "DistFromEdgeTrap: Global " << x << ":" << y << ":" << z << " Layer " << lay
356  << " Index " << indx << ":" << type << " xx " << xx << " R " << r << ":" << irad << ":"
357  << hgpar_->radiusLayer_[type][irad - 1] << ":" << hgpar_->radiusLayer_[type][irad]
358  << " Phi " << phi << ":" << iphi << ":" << (iphi - 0.5) * cell << " cell " << cell
359  << " Dphi " << dphi << " Dist " << dist << ":" << r * dphi;
360 #endif
361  return HGCalParameters::k_ScaleToDDD * std::min(r * dphi, dist);
362 }
363 
364 int HGCalDDDConstants::getLayer(double z, bool reco) const {
365  // Get the layer # from the gloabl z coordinate
366  unsigned int k = 0;
367  double zz = (reco ? std::abs(z) : HGCalParameters::k_ScaleFromDDD * std::abs(z));
368  const auto& zLayerHex = hgpar_->zLayerHex_;
369  auto itr = std::find_if(zLayerHex.begin() + 1, zLayerHex.end(), [&k, &zz, &zLayerHex](double zLayer) {
370  ++k;
371  return zz < 0.5 * (zLayerHex[k - 1] + zLayerHex[k]);
372  });
373  int lay = (itr == zLayerHex.end()) ? static_cast<int>(zLayerHex.size()) : k;
374  if (waferHexagon6() && reco) {
375  int indx = layerIndex(lay, false);
376  if (indx >= 0)
377  lay = hgpar_->layerGroupO_[indx];
378  } else {
379  lay += (hgpar_->firstLayer_ - 1);
380  }
381  return lay;
382 }
383 
384 HGCalParameters::hgtrap HGCalDDDConstants::getModule(unsigned int indx, bool hexType, bool reco) const {
386  if (hexType) {
387  if (indx >= hgpar_->waferTypeL_.size())
388  edm::LogWarning("HGCalGeom") << "Wafer no. out bound for index " << indx << ":" << (hgpar_->waferTypeL_).size()
389  << ":" << (hgpar_->waferPosX_).size() << ":" << (hgpar_->waferPosY_).size()
390  << " ***** ERROR *****";
391  unsigned int type =
392  ((indx < hgpar_->waferTypeL_.size()) ? hgpar_->waferTypeL_[indx] - 1 : HGCSiliconDetId::HGCalCoarseThick);
393  mytr = hgpar_->getModule(type, reco);
394  } else {
395  mytr = hgpar_->getModule(indx, reco);
396  }
397  return mytr;
398 }
399 
400 std::vector<HGCalParameters::hgtrap> HGCalDDDConstants::getModules() const {
401  std::vector<HGCalParameters::hgtrap> mytrs;
402  for (unsigned int k = 0; k < hgpar_->moduleLayR_.size(); ++k)
403  mytrs.emplace_back(hgpar_->getModule(k, true));
404  return mytrs;
405 }
406 
407 int HGCalDDDConstants::getPhiBins(int lay) const { return (tileTrapezoid() ? hgpar_->scintCells(lay) : 0); }
408 
409 std::pair<int, int> HGCalDDDConstants::getREtaRange(int lay) const {
410  int irmin(0), irmax(0);
411  if (tileTrapezoid()) {
412  int indx = layerIndex(lay, false);
413  if ((indx >= 0) && (indx < static_cast<int>(hgpar_->iradMinBH_.size()))) {
414  irmin = hgpar_->iradMinBH_[indx];
415  irmax = hgpar_->iradMaxBH_[indx];
416  }
417  }
418  return std::make_pair(irmin, irmax);
419 }
420 
421 std::vector<HGCalParameters::hgtrform> HGCalDDDConstants::getTrForms() const {
422  std::vector<HGCalParameters::hgtrform> mytrs;
423  for (unsigned int k = 0; k < hgpar_->trformIndex_.size(); ++k)
424  mytrs.emplace_back(hgpar_->getTrForm(k));
425  return mytrs;
426 }
427 
429  // Get the module type for scinitllator
430  if (tileTrapezoid()) {
431  return hgpar_->scintType(layer);
432  } else {
433  return -1;
434  }
435 }
436 
438  // Get the module type for a silicon wafer
439  if (waferHexagon8()) {
440  auto itr = hgpar_->typesInLayers_.find(HGCalWaferIndex::waferIndex(layer, waferU, waferV));
441  return ((itr == hgpar_->typesInLayers_.end() ? 2 : hgpar_->waferTypeL_[itr->second]));
442  } else {
443  return -1;
444  }
445 }
446 
447 std::pair<double, double> HGCalDDDConstants::getXY(int layer, double x, double y, bool forwd) const {
448  int ll = layer - hgpar_->firstLayer_;
449  double x0(x), y0(y);
450  if ((!hgpar_->layerType_.empty()) && (ll < static_cast<int>(hgpar_->layerRotV_.size()))) {
451  if (forwd) {
452  x0 = x * hgpar_->layerRotV_[ll].first + y * hgpar_->layerRotV_[ll].second;
453  y0 = y * hgpar_->layerRotV_[ll].first - x * hgpar_->layerRotV_[ll].second;
454  } else {
455  x0 = x * hgpar_->layerRotV_[ll].first - y * hgpar_->layerRotV_[ll].second;
456  y0 = y * hgpar_->layerRotV_[ll].first + x * hgpar_->layerRotV_[ll].second;
457  }
458  }
459 #ifdef EDM_ML_DEBUG
460  double x1(x0), y1(y0);
461  if (ll < static_cast<int>(hgpar_->layerRotV_.size())) {
462  if (forwd) {
463  x1 = x0 * hgpar_->layerRotV_[ll].first - y0 * hgpar_->layerRotV_[ll].second;
464  y1 = y0 * hgpar_->layerRotV_[ll].first + x0 * hgpar_->layerRotV_[ll].second;
465  } else {
466  x1 = x0 * hgpar_->layerRotV_[ll].first + y0 * hgpar_->layerRotV_[ll].second;
467  y1 = y0 * hgpar_->layerRotV_[ll].first - x0 * hgpar_->layerRotV_[ll].second;
468  }
469  }
470  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants: Layer " << layer << ":" << ll << " mode " << forwd << " x " << x
471  << ":" << x0 << ":" << x1 << " y " << y << ":" << y0 << ":" << y1;
472 #endif
473  return std::make_pair(x0, y0);
474 }
475 
476 bool HGCalDDDConstants::isHalfCell(int waferType, int cell) const {
477  if (waferType < 1 || cell < 0)
478  return false;
479  return waferType == 2 ? hgpar_->cellCoarseHalf_[cell] : hgpar_->cellFineHalf_[cell];
480 }
481 
482 bool HGCalDDDConstants::isValidHex(int lay, int mod, int cell, bool reco) const {
483  // Check validity for a layer|wafer|cell of pre-TDR version
484  bool result(false), resultMod(false);
485  int cellmax(0);
486  if (waferHexagon6()) {
487  int32_t copyNumber = hgpar_->waferCopy_[mod];
488  result = ((lay > 0 && lay <= (int)(layers(reco))));
489  if (result) {
490  const int32_t lay_idx = reco ? (lay - 1) * 3 + 1 : lay;
491  const auto& the_modules = hgpar_->copiesInLayers_[lay_idx];
492  auto moditr = the_modules.find(copyNumber);
493  result = resultMod = (moditr != the_modules.end());
494 #ifdef EDM_ML_DEBUG
495  if (!result)
496  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants: Layer " << lay << ":" << lay_idx << " Copy " << copyNumber
497  << ":" << mod << " Flag " << result;
498 #endif
499  if (result) {
500  if (moditr->second >= 0) {
501  if (mod >= (int)(hgpar_->waferTypeT_.size()))
502  edm::LogWarning("HGCalGeom") << "Module no. out of bound for " << mod << " to be compared with "
503  << (hgpar_->waferTypeT_).size() << " ***** ERROR *****";
504  cellmax = ((hgpar_->waferTypeT_[mod] - 1 == HGCSiliconDetId::HGCalFine) ? (int)(hgpar_->cellFineX_.size())
505  : (int)(hgpar_->cellCoarseX_.size()));
506  result = (cell >= 0 && cell <= cellmax);
507  } else {
508  result = isValidCell(lay_idx, mod, cell);
509  }
510  }
511  }
512  }
513 
514 #ifdef EDM_ML_DEBUG
515  if (!result)
516  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants: Layer " << lay << ":"
517  << (lay > 0 && (lay <= (int)(layers(reco)))) << " Module " << mod << ":" << resultMod
518  << " Cell " << cell << ":" << cellmax << ":" << (cell >= 0 && cell <= cellmax) << ":"
519  << maxCells(reco);
520 #endif
521  return result;
522 }
523 
524 bool HGCalDDDConstants::isValidHex8(int layer, int modU, int modV, bool fullAndPart) const {
525  // Check validity for a layer|wafer|cell of post-TDR version
526  int indx = HGCalWaferIndex::waferIndex(layer, modU, modV);
527  auto itr = hgpar_->typesInLayers_.find(indx);
528 #ifdef EDM_ML_DEBUG
529  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:WaferType " << layer << ":" << modU << ":" << modV
530  << ":" << indx << " Test " << (itr != hgpar_->typesInLayers_.end());
531 #endif
532  if (itr == hgpar_->typesInLayers_.end())
533  return false;
534 
535  if (fullAndPart_) {
536  auto ktr = hgpar_->waferInfoMap_.find(indx);
537 #ifdef EDM_ML_DEBUG
538  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:WaferInfoMap " << layer << ":" << modU << ":"
539  << modV << ":" << indx << " Test " << (ktr != hgpar_->waferInfoMap_.end());
540 #endif
541  if (ktr == hgpar_->waferInfoMap_.end())
542  return false;
543  } else {
544  auto jtr = waferIn_.find(indx);
545 #ifdef EDM_ML_DEBUG
546  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:WaferIn " << jtr->first << ":" << jtr->second;
547 #endif
548  if (!(jtr->second))
549  return false;
550  }
551 
552  if (fullAndPart || fullAndPart_) {
553  auto ktr = hgpar_->waferTypes_.find(indx);
554  if (ktr != hgpar_->waferTypes_.end()) {
555  if (hgpar_->waferMaskMode_ > 0) {
556  if (ktr->second.first == HGCalTypes::WaferOut)
557  return false;
558  } else {
559  if (ktr->second.first < HGCalTypes::WaferCornerMin)
560  return false;
561  }
562  }
563  }
564  return true;
565 }
566 
567 bool HGCalDDDConstants::isValidHex8(int layer, int modU, int modV, int cellU, int cellV, bool fullAndPart) const {
568  // First check validity for a layer|wafer| of post TDR version
569  if (!isValidHex8(layer, modU, modV, fullAndPart))
570  return false;
571  int indx = HGCalWaferIndex::waferIndex(layer, modU, modV);
572  auto itr = hgpar_->typesInLayers_.find(indx);
573  int type = hgpar_->waferTypeL_[itr->second];
574  int N = ((hgpar_->waferTypeL_[itr->second] == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_);
575 #ifdef EDM_ML_DEBUG
576  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:Cell " << cellU << ":" << cellV << ":" << N
577  << " Tests " << (cellU >= 0) << ":" << (cellU < 2 * N) << ":" << (cellV >= 0) << ":"
578  << (cellV < 2 * N) << ":" << ((cellV - cellU) < N) << ":" << ((cellU - cellV) <= N);
579 #endif
580  if ((cellU < 0) || (cellU >= 2 * N) || (cellV < 0) || (cellV >= 2 * N))
581  return false;
582  if (((cellV - cellU) >= N) || ((cellU - cellV) > N))
583  return false;
584 
585  return isValidCell8(layer, modU, modV, cellU, cellV, type);
586 }
587 
588 bool HGCalDDDConstants::isValidTrap(int layer, int irad, int iphi) const {
589  // Check validity for a layer|eta|phi of scintillator
590  const auto& indx = getIndex(layer, true);
591  if (indx.first < 0)
592  return false;
593  return ((irad >= hgpar_->iradMinBH_[indx.first]) && (irad <= (hgpar_->iradMaxBH_[indx.first] + 1)) && (iphi > 0) &&
594  (iphi <= hgpar_->scintCells(layer)));
595 }
596 
597 int HGCalDDDConstants::lastLayer(bool reco) const { return (hgpar_->firstLayer_ + tot_layers_[(int)reco] - 1); }
598 
599 unsigned int HGCalDDDConstants::layers(bool reco) const { return tot_layers_[(int)reco]; }
600 
601 int HGCalDDDConstants::layerIndex(int lay, bool reco) const {
602  int ll = lay - hgpar_->firstLayer_;
603  if (ll < 0 || ll >= (int)(hgpar_->layerIndex_.size()))
604  return -1;
605  if (waferHexagon6()) {
606  if (reco && ll >= (int)(hgpar_->depthIndex_.size()))
607  return -1;
608  return (reco ? hgpar_->depthLayerF_[ll] : hgpar_->layerIndex_[ll]);
609  } else {
610  return (hgpar_->layerIndex_[ll]);
611  }
612 }
613 
614 unsigned int HGCalDDDConstants::layersInit(bool reco) const {
615  return (reco ? hgpar_->depthIndex_.size() : hgpar_->layerIndex_.size());
616 }
617 
618 std::pair<float, float> HGCalDDDConstants::localToGlobal8(
619  int lay, int waferU, int waferV, double localX, double localY, bool reco, bool debug) const {
620  double x(localX), y(localY);
621  bool rotx =
623  if (debug)
624  edm::LogVerbatim("HGCalGeom") << "LocalToGlobal8 " << lay << ":" << (lay - hgpar_->firstLayer_) << ":" << rotx
625  << " Local (" << x << ":" << y << ") Reco " << reco;
626  if (!reco) {
629  }
630  const auto& xy = waferPositionNoRot(lay, waferU, waferV, reco, debug);
631  x += xy.first;
632  y += xy.second;
633  if (debug)
634  edm::LogVerbatim("HGCalGeom") << "With wafer " << x << ":" << y << " by adding " << xy.first << ":" << xy.second;
635  return (rotx ? getXY(lay, x, y, false) : std::make_pair(x, y));
636 }
637 
638 std::pair<float, float> HGCalDDDConstants::locateCell(int cell, int lay, int type, bool reco) const {
639  // type refers to wafer # for hexagon cell
640  float x(999999.), y(999999.);
641  const auto& index = getIndex(lay, reco);
642  int i = index.first;
643  if (i < 0)
644  return std::make_pair(x, y);
645  if (waferHexagon6()) {
646  x = hgpar_->waferPosX_[type];
647  y = hgpar_->waferPosY_[type];
648 #ifdef EDM_ML_DEBUG
649  float x0(x), y0(y);
650 #endif
651  if (hgpar_->waferTypeT_[type] - 1 == HGCSiliconDetId::HGCalFine) {
652  x += hgpar_->cellFineX_[cell];
653  y += hgpar_->cellFineY_[cell];
654  } else {
655  x += hgpar_->cellCoarseX_[cell];
656  y += hgpar_->cellCoarseY_[cell];
657  }
658 #ifdef EDM_ML_DEBUG
659  edm::LogVerbatim("HGCalGeom") << "LocateCell (Wafer) " << x0 << ":" << y0 << " Final " << x << ":" << y;
660 #endif
661  if (!reco) {
664  }
665  }
666  return std::make_pair(x, y);
667 }
668 
669 std::pair<float, float> HGCalDDDConstants::locateCell(
670  int lay, int waferU, int waferV, int cellU, int cellV, bool reco, bool all, bool norot, bool debug) const {
671  double x(0), y(0);
672  int indx = HGCalWaferIndex::waferIndex(lay, waferU, waferV);
673  auto itr = hgpar_->typesInLayers_.find(indx);
674  int type = ((itr == hgpar_->typesInLayers_.end()) ? 2 : hgpar_->waferTypeL_[itr->second]);
675  bool rotx = (norot) ? false
676  : ((!hgpar_->layerType_.empty()) &&
678  if (debug) {
679  edm::LogVerbatim("HGCalGeom") << "LocateCell " << lay << ":" << (lay - hgpar_->firstLayer_) << ":" << rotx << ":"
680  << waferU << ":" << waferV << ":" << indx << ":"
681  << (itr == hgpar_->typesInLayers_.end()) << ":" << type << " Flags " << reco << ":"
682  << all;
683  }
684  int kndx = cellV * 100 + cellU;
685  if (type == 0) {
686  auto ktr = hgpar_->cellFineIndex_.find(kndx);
687  if (ktr != hgpar_->cellFineIndex_.end()) {
688  x = hgpar_->cellFineX_[ktr->second];
689  y = hgpar_->cellFineY_[ktr->second];
690  }
691  if (debug)
692  edm::LogVerbatim("HGCalGeom") << "Fine " << cellU << ":" << cellV << ":" << kndx << ":" << x << ":" << y << ":"
693  << (ktr != hgpar_->cellFineIndex_.end());
694  } else {
695  auto ktr = hgpar_->cellCoarseIndex_.find(kndx);
696  if (ktr != hgpar_->cellCoarseIndex_.end()) {
697  x = hgpar_->cellCoarseX_[ktr->second];
698  y = hgpar_->cellCoarseY_[ktr->second];
699  }
700  if (debug)
701  edm::LogVerbatim("HGCalGeom") << "Coarse " << cellU << ":" << cellV << ":" << kndx << ":" << x << ":" << y << ":"
702  << (ktr != hgpar_->cellCoarseIndex_.end());
703  }
704  if (!reco) {
707  }
708  if (all) {
709  const auto& xy = waferPositionNoRot(lay, waferU, waferV, reco, debug);
710  x += xy.first;
711  y += xy.second;
712  if (debug)
713  edm::LogVerbatim("HGCalGeom") << "With wafer " << x << ":" << y << " by adding " << xy.first << ":" << xy.second;
714  }
715  return (rotx ? getXY(lay, x, y, false) : std::make_pair(x, y));
716 }
717 
718 std::pair<float, float> HGCalDDDConstants::locateCell(const HGCSiliconDetId& id, bool debug) const {
719  int lay(id.layer());
720  double r = 0.5 * (hgpar_->waferSize_ + hgpar_->sensorSeparation_);
721  double R = 2.0 * r / sqrt3_;
722  int ncells = (id.type() == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
723  int n2 = ncells / 2;
724  auto xyoff = geomTools_.shiftXY(hgpar_->layerCenter_[lay - 1], (2.0 * r));
725  double xpos = xyoff.first + ((-2 * id.waferU() + id.waferV()) * r);
726  double ypos = xyoff.second + (1.5 * id.waferV() * R);
727 #ifdef EDM_ML_DEBUG
728  if (debug)
729  edm::LogVerbatim("HGCalGeom") << "LocateCell " << id << " Lay " << lay << " r:R " << r << ":" << R << " N "
730  << ncells << ":" << n2 << " Off " << xyoff.first << ":" << xyoff.second << " Pos "
731  << xpos << ":" << ypos;
732 #endif
733  double R1 = hgpar_->waferSize_ / (3.0 * ncells);
734  double r1 = 0.5 * R1 * sqrt3_;
735  xpos += ((1.5 * (id.cellV() - ncells) + 1.0) * R1);
736  ypos += ((id.cellU() - 0.5 * id.cellV() - n2) * 2 * r1);
737 #ifdef EDM_ML_DEBUG
738  if (debug)
739  edm::LogVerbatim("HGCalGeom") << "LocateCell r1:R1 " << r1 << ":" << R1 << " dx:dy "
740  << ((1.5 * (id.cellV() - ncells) + 1.0) * R1) << ":"
741  << ((id.cellU() - 0.5 * id.cellV() - n2) * 2 * r1) << " Pos " << xpos << ":" << ypos;
742 #endif
743  std::pair<double, double> xy = getXY(id.layer(), xpos, ypos, true);
744  return std::make_pair(xy.first * id.zside(), xy.second);
745 }
746 
747 std::pair<float, float> HGCalDDDConstants::locateCell(const HGCScintillatorDetId& id, bool debug) const {
748  int lay(id.layer()), iphi(id.iphi()), ir(id.iradiusAbs());
749  double phi = (iphi - 0.5) * hgpar_->scintCellSize(lay);
750  int type = (id.type() > 0) ? 1 : 0;
751  double r = (((ir + 1) < static_cast<int>(hgpar_->radiusLayer_[type].size()))
752  ? (0.5 * (hgpar_->radiusLayer_[type][ir] + hgpar_->radiusLayer_[type][ir - 1]))
753  : (1.5 * hgpar_->radiusLayer_[type][ir] - 0.5 * hgpar_->radiusLayer_[type][ir - 1]));
754 #ifdef EDM_ML_DEBUG
755  if (debug)
756  edm::LogVerbatim("HGCalGeom") << "LocateCell lay:ir:iphi:type " << lay << ":" << ir << ":" << iphi << ":" << type
757  << " r:phi " << r << ":" << convertRadToDeg(phi) << " x:y "
758  << (r * cos(phi) * id.zside()) << ":" << (r * sin(phi));
759 #endif
760  return std::make_pair(r * cos(phi) * id.zside(), r * sin(phi));
761 }
762 
763 std::pair<float, float> HGCalDDDConstants::locateCellHex(int cell, int wafer, bool reco) const {
764  float x(0), y(0);
765  if (hgpar_->waferTypeT_[wafer] - 1 == HGCSiliconDetId::HGCalFine) {
766  x = hgpar_->cellFineX_[cell];
767  y = hgpar_->cellFineY_[cell];
768  } else {
769  x = hgpar_->cellCoarseX_[cell];
770  y = hgpar_->cellCoarseY_[cell];
771  }
772  if (!reco) {
775  }
776  return std::make_pair(x, y);
777 }
778 
779 std::pair<float, float> HGCalDDDConstants::locateCellTrap(int lay, int irad, int iphi, bool reco) const {
780  float x(0), y(0);
781  const auto& indx = getIndex(lay, reco);
782  if (indx.first >= 0) {
783  int ir = std::abs(irad);
784  int type = hgpar_->scintType(lay);
785  double phi = (iphi - 0.5) * indx.second;
786  double z = hgpar_->zLayerHex_[indx.first];
787  double r = 0.5 * (hgpar_->radiusLayer_[type][ir - 1] + hgpar_->radiusLayer_[type][ir]);
788  std::pair<double, double> range = rangeR(z, true);
789 #ifdef EDM_ML_DEBUG
790  edm::LogVerbatim("HGCalGeom") << "locateCellTrap:: Input " << lay << ":" << irad << ":" << iphi << ":" << reco
791  << " IR " << ir << ":" << hgpar_->iradMinBH_[indx.first] << ":"
792  << hgpar_->iradMaxBH_[indx.first] << " Type " << type << " Z " << indx.first << ":"
793  << z << " phi " << phi << " R " << r << ":" << range.first << ":" << range.second;
794 #endif
796  r = std::max(range.first, std::min(r, range.second));
797  x = r * std::cos(phi);
798  y = r * std::sin(phi);
799  if (irad < 0)
800  x = -x;
801  }
802  if (!reco) {
805  }
806  return std::make_pair(x, y);
807 }
808 
809 bool HGCalDDDConstants::maskCell(const DetId& detId, int corners) const {
810  bool mask(false);
811  if (corners > 2 && corners <= (int)(HGCalParameters::k_CornerSize)) {
812  if (waferHexagon8()) {
813  int N(0), layer(0), waferU(0), waferV(0), u(0), v(0);
814  if (detId.det() == DetId::Forward) {
815  HFNoseDetId id(detId);
816  N = getUVMax(id.type());
817  layer = id.layer();
818  waferU = id.waferU();
819  waferV = id.waferV();
820  u = id.cellU();
821  v = id.cellV();
822  } else {
823  HGCSiliconDetId id(detId);
824  N = getUVMax(id.type());
825  layer = id.layer();
826  waferU = id.waferU();
827  waferV = id.waferV();
828  u = id.cellU();
829  v = id.cellV();
830  }
832  auto itr = hgpar_->waferTypes_.find(wl);
833 #ifdef EDM_ML_DEBUG
834  edm::LogVerbatim("HGCalGeom") << "MaskCell: Layer " << layer << " Wafer " << waferU << ":" << waferV << " Index "
835  << wl << ":" << (itr != hgpar_->waferTypes_.end());
836 #endif
837  if (itr != hgpar_->waferTypes_.end()) {
838  if ((itr->second).second <= HGCalWaferMask::k_OffsetRotation)
839  mask = HGCalWaferMask::maskCell(u, v, N, (itr->second).first, (itr->second).second, corners);
840  else
841  mask = !(HGCalWaferMask::goodCell(
842  u, v, N, (itr->second).first, ((itr->second).second - HGCalWaferMask::k_OffsetRotation)));
843  }
844  }
845  }
846  return mask;
847 }
848 
850  int cells(0);
851  for (unsigned int i = 0; i < layers(reco); ++i) {
852  int lay = reco ? hgpar_->depth_[i] : hgpar_->layer_[i];
853  if (cells < maxCells(lay, reco))
854  cells = maxCells(lay, reco);
855  }
856  return cells;
857 }
858 
859 int HGCalDDDConstants::maxCells(int lay, bool reco) const {
860  const auto& index = getIndex(lay, reco);
861  if (index.first < 0)
862  return 0;
863  if (waferHexagon6()) {
864  unsigned int cells(0);
865  for (unsigned int k = 0; k < hgpar_->waferTypeT_.size(); ++k) {
866  if (waferInLayerTest(k, index.first, hgpar_->defineFull_)) {
867  unsigned int cell = (hgpar_->waferTypeT_[k] - 1 == HGCSiliconDetId::HGCalFine) ? (hgpar_->cellFineX_.size())
868  : (hgpar_->cellCoarseX_.size());
869  if (cell > cells)
870  cells = cell;
871  }
872  }
873  return (int)(cells);
874  } else if (waferHexagon8()) {
875  int cells(0);
876  for (unsigned int k = 0; k < hgpar_->waferCopy_.size(); ++k) {
877  if (waferInLayerTest(k, index.first, hgpar_->defineFull_)) {
881  : hgpar_->waferTypeL_[itr->second]);
883  cells = std::max(cells, 3 * N * N);
884  }
885  }
886  return cells;
887  } else if (tileTrapezoid()) {
888  return hgpar_->scintCells(index.first + hgpar_->firstLayer_);
889  } else {
890  return 0;
891  }
892 }
893 
894 int HGCalDDDConstants::maxRows(int lay, bool reco) const {
895  int kymax(0);
896  const auto& index = getIndex(lay, reco);
897  int i = index.first;
898  if (i < 0)
899  return kymax;
900  if (waferHexagon6()) {
901  for (unsigned int k = 0; k < hgpar_->waferCopy_.size(); ++k) {
902  if (waferInLayerTest(k, i, hgpar_->defineFull_)) {
903  int ky = ((hgpar_->waferCopy_[k]) / 100) % 100;
904  if (ky > kymax)
905  kymax = ky;
906  }
907  }
908  } else if (waferHexagon8()) {
909  kymax = 1 + 2 * hgpar_->waferUVMaxLayer_[index.first];
910  }
911  return kymax;
912 }
913 
914 int HGCalDDDConstants::modifyUV(int uv, int type1, int type2) const {
915  // Modify u/v for transition of type1 to type2
916  return (((type1 == type2) || (type1 * type2 != 0)) ? uv : ((type1 == 0) ? (2 * uv + 1) / 3 : (3 * uv) / 2));
917 }
918 
919 int HGCalDDDConstants::modules(int lay, bool reco) const {
920  if (getIndex(lay, reco).first < 0)
921  return 0;
922  else
923  return max_modules_layer_[(int)reco][lay];
924 }
925 
926 int HGCalDDDConstants::modulesInit(int lay, bool reco) const {
927  int nmod(0);
928  const auto& index = getIndex(lay, reco);
929  if (index.first < 0)
930  return nmod;
931  if (!tileTrapezoid()) {
932  for (unsigned int k = 0; k < hgpar_->waferPosX_.size(); ++k) {
933  if (waferInLayerTest(k, index.first, hgpar_->defineFull_))
934  ++nmod;
935  }
936  } else {
937  nmod = 1 + hgpar_->lastModule_[index.first] - hgpar_->firstModule_[index.first];
938  }
939  return nmod;
940 }
941 
942 double HGCalDDDConstants::mouseBite(bool reco) const {
944 }
945 
947  int cells =
949  if (cells == 0) {
950  unsigned int nlayer = (reco) ? hgpar_->depth_.size() : hgpar_->layer_.size();
951  for (unsigned k = 0; k < nlayer; ++k) {
952  std::vector<int> ncells = numberCells(((reco) ? hgpar_->depth_[k] : hgpar_->layer_[k]), reco);
953  cells = std::accumulate(ncells.begin(), ncells.end(), cells);
954  }
955  }
956  return cells;
957 }
958 
959 std::vector<int> HGCalDDDConstants::numberCells(int lay, bool reco) const {
960  const auto& index = getIndex(lay, reco);
961  int i = index.first;
962  std::vector<int> ncell;
963  if (i >= 0) {
964  if (waferHexagon6()) {
965  for (unsigned int k = 0; k < hgpar_->waferTypeT_.size(); ++k) {
966  if (waferInLayerTest(k, i, hgpar_->defineFull_)) {
967  unsigned int cell = (hgpar_->waferTypeT_[k] - 1 == HGCSiliconDetId::HGCalFine)
968  ? (hgpar_->cellFineX_.size())
969  : (hgpar_->cellCoarseX_.size());
970  ncell.emplace_back((int)(cell));
971  }
972  }
973  } else if (tileTrapezoid()) {
974  int nphi = hgpar_->scintCells(lay);
975  for (int k = hgpar_->firstModule_[i]; k <= hgpar_->lastModule_[i]; ++k)
976  ncell.emplace_back(nphi);
977  } else {
978  for (unsigned int k = 0; k < hgpar_->waferCopy_.size(); ++k) {
979  if (waferInLayerTest(k, index.first, hgpar_->defineFull_)) {
980  int cell = numberCellsHexagon(lay,
983  true);
984  ncell.emplace_back(cell);
985  }
986  }
987  }
988  }
989  return ncell;
990 }
991 
993  if (wafer >= 0 && wafer < (int)(hgpar_->waferTypeT_.size())) {
994  if (hgpar_->waferTypeT_[wafer] - 1 == HGCSiliconDetId::HGCalFine)
995  return (int)(hgpar_->cellFineX_.size());
996  else
997  return (int)(hgpar_->cellCoarseX_.size());
998  } else {
999  return 0;
1000  }
1001 }
1002 
1003 int HGCalDDDConstants::numberCellsHexagon(int lay, int waferU, int waferV, bool flag) const {
1004  auto itr = hgpar_->typesInLayers_.find(HGCalWaferIndex::waferIndex(lay, waferU, waferV));
1005  int type =
1006  ((itr == hgpar_->typesInLayers_.end()) ? HGCSiliconDetId::HGCalCoarseThick : hgpar_->waferTypeL_[itr->second]);
1007  int N = (type == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
1008  if (flag)
1009  return (3 * N * N);
1010  else
1011  return N;
1012 }
1013 
1014 std::pair<double, double> HGCalDDDConstants::rangeR(double z, bool reco) const {
1015  double rmin(0), rmax(0), zz(0);
1016  if (hgpar_->detectorType_ > 0) {
1017  zz = (reco ? std::abs(z) : HGCalParameters::k_ScaleFromDDD * std::abs(z));
1018  if (hgpar_->detectorType_ <= 2) {
1020  } else {
1021  rmin = HGCalGeomTools::radius(
1023  }
1024  if ((hgpar_->detectorType_ == 2) && (zz >= hgpar_->zLayerHex_[hgpar_->firstMixedLayer_ - 1])) {
1025  rmax = HGCalGeomTools::radius(
1027  } else {
1029  }
1030  }
1031  if (!reco) {
1034  }
1035 #ifdef EDM_ML_DEBUG
1036  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:rangeR: " << z << ":" << zz << " R " << rmin << ":" << rmax;
1037 #endif
1038  return std::make_pair(rmin, rmax);
1039 }
1040 
1041 std::pair<double, double> HGCalDDDConstants::rangeRLayer(int lay, bool reco) const {
1042  double rmin(0), rmax(0);
1043  const auto& index = getIndex(lay, reco);
1044  if (index.first >= 0 && index.first < static_cast<int>(hgpar_->rMinLayHex_.size())) {
1045  rmin = hgpar_->rMinLayHex_[index.first];
1046  rmax = hgpar_->rMaxLayHex_[index.first];
1047  }
1048  if (!reco) {
1051  }
1052 #ifdef EDM_ML_DEBUG
1053  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:rangeR: " << lay << ":" << index.first << " R " << rmin << ":"
1054  << rmax;
1055 #endif
1056  return std::make_pair(rmin, rmax);
1057 }
1058 
1059 std::pair<double, double> HGCalDDDConstants::rangeZ(bool reco) const {
1060  double zmin = (hgpar_->zLayerHex_[0] - hgpar_->waferThick_);
1061  double zmax = (hgpar_->zLayerHex_[hgpar_->zLayerHex_.size() - 1] + hgpar_->waferThick_);
1062 #ifdef EDM_ML_DEBUG
1063  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:rangeZ: " << zmin << ":" << zmax << ":" << hgpar_->waferThick_;
1064 #endif
1065  if (!reco) {
1068  }
1069  return std::make_pair(zmin, zmax);
1070 }
1071 
1072 std::pair<int, int> HGCalDDDConstants::rowColumnWafer(int wafer) const {
1073  int row(0), col(0);
1074  if (wafer < (int)(hgpar_->waferCopy_.size())) {
1075  int copy = hgpar_->waferCopy_[wafer];
1076  col = HGCalTypes::getUnpackedU(copy);
1077  row = HGCalTypes::getUnpackedV(copy);
1078  ;
1079  }
1080  return std::make_pair(row, col);
1081 }
1082 
1083 std::pair<int, int> HGCalDDDConstants::simToReco(int cell, int lay, int mod, bool half) const {
1084  if (!waferHexagon6()) {
1085  return std::make_pair(cell, lay);
1086  } else {
1087  const auto& index = getIndex(lay, false);
1088  int i = index.first;
1089  if (i < 0) {
1090  edm::LogWarning("HGCalGeom") << "Wrong Layer # " << lay << " not in the list ***** ERROR *****";
1091  return std::make_pair(-1, -1);
1092  }
1093  if (mod >= (int)(hgpar_->waferTypeL_).size()) {
1094  edm::LogWarning("HGCalGeom") << "Invalid Wafer # " << mod << "should be < " << (hgpar_->waferTypeL_).size()
1095  << " ***** ERROR *****";
1096  return std::make_pair(-1, -1);
1097  }
1098  int depth(-1);
1099  int kx = cell;
1100  int type = hgpar_->waferTypeL_[mod];
1101  if (type == 1) {
1102  depth = hgpar_->layerGroup_[i];
1103  } else if (type == 2) {
1104  depth = hgpar_->layerGroupM_[i];
1105  } else {
1106  depth = hgpar_->layerGroupO_[i];
1107  }
1108  return std::make_pair(kx, depth);
1109  }
1110 }
1111 
1113  int laymin(layer), laymax(layer), ringmin(ring), ringmax(ring), kount(0);
1114  if (layer == 0) {
1115  laymin = hgpar_->firstLayer_;
1116  laymax = lastLayer(true);
1117  }
1118  for (int lay = laymin; lay <= laymax; ++lay) {
1119  if (ring < 0) {
1120  int ll = lay - hgpar_->firstLayer_;
1121  ringmin = hgpar_->tileRingRange_[ll].first;
1122  ringmax = hgpar_->tileRingRange_[ll].second;
1123  }
1124  for (int rin = ringmin; rin <= ringmax; ++rin) {
1125  int indx = HGCalTileIndex::tileIndex(lay, rin + 1, 0);
1126  auto itr = hgpar_->tileInfoMap_.find(indx);
1127  if (itr != hgpar_->tileInfoMap_.end()) {
1128  for (int k = 0; k < 4; ++k) {
1129  std::bitset<24> b(itr->second.hex[k]);
1130  kount += b.count();
1131  }
1132  }
1133  }
1134  }
1135  return (3 * kount);
1136 }
1137 
1139  const int ncopies = hgpar_->waferCopy_.size();
1140  int wafer(ncopies);
1141  bool result(false);
1142  for (int k = 0; k < ncopies; ++k) {
1143  if (copy == hgpar_->waferCopy_[k]) {
1144  wafer = k;
1145  result = true;
1146  break;
1147  }
1148  }
1149  if (!result) {
1150  wafer = -1;
1151 #ifdef EDM_ML_DEBUG
1152  edm::LogVerbatim("HGCalGeom") << "Cannot find " << copy << " in a list of " << ncopies << " members";
1153  for (int k = 0; k < ncopies; ++k)
1154  edm::LogVerbatim("HGCalGeom") << "[" << k << "] " << hgpar_->waferCopy_[k];
1155 #endif
1156  }
1157 #ifdef EDM_ML_DEBUG
1158  edm::LogVerbatim("HGCalGeom") << "WaferFromCopy " << copy << ":" << wafer << ":" << result;
1159 #endif
1160  return wafer;
1161 }
1162 
1163 void HGCalDDDConstants::waferFromPosition(const double x, const double y, int& wafer, int& icell, int& celltyp) const {
1164  // Input x, y in Geant4 unit and transformed to CMSSW standard
1165  double xx = HGCalParameters::k_ScaleFromDDD * x;
1166  double yy = HGCalParameters::k_ScaleFromDDD * y;
1167  int size_ = static_cast<int>(hgpar_->waferCopy_.size());
1168  wafer = size_;
1169  for (int k = 0; k < size_; ++k) {
1170  double dx = std::abs(xx - hgpar_->waferPosX_[k]);
1171  double dy = std::abs(yy - hgpar_->waferPosY_[k]);
1172  if (dx <= rmax_ && dy <= hexside_) {
1173  if ((dy <= 0.5 * hexside_) || (dx * tan30deg_ <= (hexside_ - dy))) {
1174  wafer = k;
1175  celltyp = hgpar_->waferTypeT_[k];
1176  xx -= hgpar_->waferPosX_[k];
1177  yy -= hgpar_->waferPosY_[k];
1178  break;
1179  }
1180  }
1181  }
1182  if (wafer < size_) {
1183  if (celltyp - 1 == HGCSiliconDetId::HGCalFine)
1184  icell = cellHex(
1186  else
1187  icell = cellHex(xx,
1188  yy,
1191  hgpar_->cellCoarseY_);
1192  } else {
1193  wafer = -1;
1194 #ifdef EDM_ML_DEBUG
1195  edm::LogWarning("HGCalGeom") << "Cannot get wafer type corresponding to " << x << ":" << y << " " << xx << ":"
1196  << yy;
1197 #endif
1198  }
1199 #ifdef EDM_ML_DEBUG
1200  edm::LogVerbatim("HGCalGeom") << "Position " << x << ":" << y << " Wafer " << wafer << ":" << size_ << " XX " << xx
1201  << ":" << yy << " Cell " << icell << " Type " << celltyp;
1202 #endif
1203 }
1204 
1206  const double y,
1207  const int layer,
1208  int& waferU,
1209  int& waferV,
1210  int& cellU,
1211  int& cellV,
1212  int& celltype,
1213  double& wt,
1214  bool extend,
1215  bool debug) const {
1216  waferU = waferV = 1 + hgpar_->waferUVMax_;
1217  cellU = cellV = celltype = 0;
1218  if ((hgpar_->xLayerHex_.empty()) || (hgpar_->yLayerHex_.empty()))
1219  return;
1220  int ll = layer - hgpar_->firstLayer_;
1221  bool rotx = ((!hgpar_->layerType_.empty()) && (hgpar_->layerType_[ll] == HGCalTypes::WaferCenterR));
1222  double xx(0), yy(0);
1223  if (rotx) {
1224  std::pair<double, double> xy =
1226  xx = xy.first - hgpar_->xLayerHex_[ll];
1227  yy = xy.second - hgpar_->yLayerHex_[ll];
1228  } else {
1231  }
1232 #ifdef EDM_ML_DEBUG
1233  if (debug)
1234  edm::LogVerbatim("HGCalGeom") << "waferFromPosition:: Layer " << layer << ":" << ll << " Rot " << rotx << " X " << x
1235  << ":" << xx << " Y " << y << ":" << yy;
1236 #endif
1237  double rmax = extend ? rmaxT_ : rmax_;
1238  double hexside = extend ? hexsideT_ : hexside_;
1239  for (unsigned int k = 0; k < hgpar_->waferPosX_.size(); ++k) {
1240  double dx = std::abs(xx - hgpar_->waferPosX_[k]);
1241  double dy = std::abs(yy - hgpar_->waferPosY_[k]);
1242  if (dx <= rmax && dy <= hexside) {
1243  if ((dy <= 0.5 * hexside) || (dx * tan30deg_ <= (hexside - dy))) {
1247  int index = HGCalWaferIndex::waferIndex(layer, waferU, waferV);
1248  celltype = HGCalWaferType::getType(index, hgpar_->waferInfoMap_);
1249  } else {
1250  auto itr = hgpar_->typesInLayers_.find(HGCalWaferIndex::waferIndex(layer, waferU, waferV));
1251  celltype = ((itr == hgpar_->typesInLayers_.end()) ? HGCSiliconDetId::HGCalCoarseThick
1252  : hgpar_->waferTypeL_[itr->second]);
1253  }
1254 #ifdef EDM_ML_DEBUG
1255  if (debug)
1256  edm::LogVerbatim("HGCalGeom") << "WaferFromPosition:: Input " << layer << ":" << ll << ":"
1257  << hgpar_->firstLayer_ << ":" << rotx << ":" << x << ":" << y << ":"
1258  << hgpar_->xLayerHex_[ll] << ":" << hgpar_->yLayerHex_[ll] << ":" << xx << ":"
1259  << yy << " compared with " << hgpar_->waferPosX_[k] << ":"
1260  << hgpar_->waferPosY_[k] << " difference " << dx << ":" << dy << ":"
1261  << dx * tan30deg_ << ":" << (hexside_ - dy) << " comparator " << rmax_ << ":"
1262  << rmaxT_ << ":" << hexside_ << ":" << hexsideT_ << " wafer " << waferU << ":"
1263  << waferV << ":" << celltype;
1264 #endif
1265  xx -= hgpar_->waferPosX_[k];
1266  yy -= hgpar_->waferPosY_[k];
1267  break;
1268  }
1269  }
1270  }
1271  if ((std::abs(waferU) <= hgpar_->waferUVMax_) && (celltype >= 0)) {
1272  cellHex(xx, yy, celltype, cellU, cellV, extend, debug);
1273  wt = (((celltype < 2) && (mode_ != HGCalGeometryMode::Hexagon8Module))
1274  ? (hgpar_->cellThickness_[celltype] / hgpar_->waferThick_)
1275  : 1.0);
1276  } else {
1277  cellU = cellV = 2 * hgpar_->nCellsFine_;
1278  wt = 1.0;
1279  celltype = -1;
1280  }
1281  if ((celltype < 0) && debug) {
1282  double x1(xx);
1283  double y1(yy);
1284  edm::LogVerbatim("HGCalGeom") << "waferfFromPosition: Bad type for X " << x << ":" << x1 << ":" << xx << " Y " << y
1285  << ":" << y1 << ":" << yy << " Wafer " << waferU << ":" << waferV << " Cell " << cellU
1286  << ":" << cellV;
1287  for (unsigned int k = 0; k < hgpar_->waferPosX_.size(); ++k) {
1288  double dx = std::abs(x1 - hgpar_->waferPosX_[k]);
1289  double dy = std::abs(y1 - hgpar_->waferPosY_[k]);
1290  edm::LogVerbatim("HGCalGeom") << "Wafer [" << k << "] Position (" << hgpar_->waferPosX_[k] << ", "
1291  << hgpar_->waferPosY_[k] << ") difference " << dx << ":" << dy << ":"
1292  << dx * tan30deg_ << ":" << hexside - dy << " Paramerers " << rmax << ":"
1293  << hexside;
1294  }
1295  }
1296 }
1297 
1298 bool HGCalDDDConstants::waferInLayer(int wafer, int lay, bool reco) const {
1299  const auto& indx = getIndex(lay, reco);
1300  if (indx.first < 0)
1301  return false;
1302  return waferInLayerTest(wafer, indx.first, hgpar_->defineFull_);
1303 }
1304 
1305 bool HGCalDDDConstants::waferFullInLayer(int wafer, int lay, bool reco) const {
1306  const auto& indx = getIndex(lay, reco);
1307  if (indx.first < 0)
1308  return false;
1309  return waferInLayerTest(wafer, indx.first, false);
1310 }
1311 
1312 std::pair<double, double> HGCalDDDConstants::waferParameters(bool reco) const {
1313  if (reco)
1314  return std::make_pair(rmax_, hexside_);
1315  else
1317 }
1318 
1319 std::pair<double, double> HGCalDDDConstants::waferPosition(int wafer, bool reco) const {
1320  double xx(0), yy(0);
1321  if (wafer >= 0 && wafer < (int)(hgpar_->waferPosX_.size())) {
1322  xx = hgpar_->waferPosX_[wafer];
1323  yy = hgpar_->waferPosY_[wafer];
1324  }
1325  if (!reco) {
1328  }
1329  return std::make_pair(xx, yy);
1330 }
1331 
1332 std::pair<double, double> HGCalDDDConstants::waferPosition(
1333  int lay, int waferU, int waferV, bool reco, bool debug) const {
1334  int ll = lay - hgpar_->firstLayer_;
1335  bool rotx = ((!hgpar_->layerType_.empty()) && (hgpar_->layerType_[ll] == HGCalTypes::WaferCenterR));
1336 #ifdef EDM_ML_DEBUG
1337  if (debug)
1338  edm::LogVerbatim("HGCalGeom") << "Layer " << lay << ":" << ll << " Rotation " << rotx << " U:V " << waferU << ":"
1339  << waferV;
1340 #endif
1341  auto xy = waferPositionNoRot(lay, waferU, waferV, reco, debug);
1342  std::pair<double, double> xy0 = (rotx) ? getXY(lay, xy.first, xy.second, false) : xy;
1343 #ifdef EDM_ML_DEBUG
1344  if (debug)
1345  edm::LogVerbatim("HGCalGeom") << "Without and with rotation " << xy.first << ":" << xy.second << ":" << xy0.first
1346  << ":" << xy0.second;
1347 #endif
1348  return xy0;
1349 }
1350 
1351 int HGCalDDDConstants::waferType(DetId const& id, bool fromFile) const {
1352  int type(1);
1353  if (waferHexagon8()) {
1354  if (fromFile && (waferFileSize() > 0)) {
1355  int layer(0), waferU(0), waferV(0);
1356  if (id.det() != DetId::Forward) {
1357  HGCSiliconDetId hid(id);
1358  layer = hid.layer();
1359  waferU = hid.waferU();
1360  waferV = hid.waferV();
1361  } else {
1362  HFNoseDetId hid(id);
1363  layer = hid.layer();
1364  waferU = hid.waferU();
1365  waferV = hid.waferV();
1366  }
1367  auto itr = hgpar_->waferInfoMap_.find(HGCalWaferIndex::waferIndex(layer, waferU, waferV));
1368  if (itr != hgpar_->waferInfoMap_.end())
1369  type = (itr->second).type;
1370  } else {
1371  type = ((id.det() != DetId::Forward) ? HGCSiliconDetId(id).type() : HFNoseDetId(id).type());
1372  }
1373  } else if (waferHexagon6()) {
1374  type = waferTypeL(HGCalDetId(id).wafer()) - 1;
1375  }
1376  return type;
1377 }
1378 
1379 int HGCalDDDConstants::waferType(int layer, int waferU, int waferV, bool fromFile) const {
1381  if (waferHexagon8()) {
1382  if (fromFile && (waferFileSize() > 0)) {
1383  auto itr = hgpar_->waferInfoMap_.find(HGCalWaferIndex::waferIndex(layer, waferU, waferV));
1384  if (itr != hgpar_->waferInfoMap_.end())
1385  type = (itr->second).type;
1386  } else {
1387  auto itr = hgpar_->typesInLayers_.find(HGCalWaferIndex::waferIndex(layer, waferU, waferV));
1388  if (itr != hgpar_->typesInLayers_.end())
1389  type = hgpar_->waferTypeL_[itr->second];
1390  }
1391  } else if (waferHexagon6()) {
1392  if ((waferU >= 0) && (waferU < (int)(hgpar_->waferTypeL_.size())))
1393  type = (hgpar_->waferTypeL_[waferU] - 1);
1394  }
1395  return type;
1396 }
1397 
1398 std::tuple<int, int, int> HGCalDDDConstants::waferType(HGCSiliconDetId const& id, bool fromFile) const {
1399  const auto& index = HGCalWaferIndex::waferIndex(id.layer(), id.waferU(), id.waferV());
1400  int type(-1), part(-1), orient(-1);
1401  if (fromFile && (waferFileSize() > 0)) {
1402  auto itr = hgpar_->waferInfoMap_.find(index);
1403  if (itr != hgpar_->waferInfoMap_.end()) {
1404  type = (itr->second).type;
1405  part = (itr->second).part;
1406  orient = (itr->second).orient;
1407  }
1408  } else {
1409  auto ktr = hgpar_->typesInLayers_.find(index);
1410  if (ktr != hgpar_->typesInLayers_.end())
1411  type = hgpar_->waferTypeL_[ktr->second];
1412  auto itr = hgpar_->waferTypes_.find(index);
1413  if (itr != hgpar_->waferTypes_.end()) {
1414  if ((itr->second).second < HGCalWaferMask::k_OffsetRotation) {
1415  orient = (itr->second).second;
1416  if ((itr->second).first == HGCalGeomTools::k_allCorners) {
1418  } else if ((itr->second).first == HGCalGeomTools::k_fiveCorners) {
1420  } else if ((itr->second).first == HGCalGeomTools::k_fourCorners) {
1422  } else if ((itr->second).first == HGCalGeomTools::k_threeCorners) {
1424  }
1425  } else {
1426  part = (itr->second).first;
1427  orient = ((itr->second).second - HGCalWaferMask::k_OffsetRotation);
1428  }
1429  } else {
1431  orient = 0;
1432  }
1433  }
1434  return std::make_tuple(type, part, orient);
1435 }
1436 
1438  int layer, int waferU, int waferV, bool fromFile, bool debug) const {
1439  int type(HGCalTypes::WaferOut), rotn(0);
1440  int wl = HGCalWaferIndex::waferIndex(layer, waferU, waferV);
1441  bool withinList(true);
1442  if (fromFile && (waferFileSize() > 0)) {
1443  auto itr = hgpar_->waferInfoMap_.find(wl);
1444  withinList = (itr != hgpar_->waferInfoMap_.end());
1445  if (withinList) {
1446  type = (itr->second).part;
1447  rotn = (itr->second).orient;
1448  }
1449  } else {
1450  auto itr = hgpar_->waferTypes_.find(wl);
1451  if (waferHexagon8()) {
1452  withinList = (itr != hgpar_->waferTypes_.end());
1453  if (withinList) {
1454  if ((itr->second).second < HGCalWaferMask::k_OffsetRotation) {
1455  rotn = (itr->second).second;
1456  if ((itr->second).first == HGCalGeomTools::k_allCorners) {
1458  } else if ((itr->second).first == HGCalGeomTools::k_fiveCorners) {
1460  } else if ((itr->second).first == HGCalGeomTools::k_fourCorners) {
1462  } else if ((itr->second).first == HGCalGeomTools::k_threeCorners) {
1464  }
1465  } else {
1466  type = (itr->second).first;
1467  rotn = ((itr->second).second - HGCalWaferMask::k_OffsetRotation);
1468  }
1469  } else {
1471  rotn = HGCalTypes::WaferCorner0;
1472  }
1473  }
1474  }
1475 #ifdef EDM_ML_DEBUG
1476  if (debug)
1477  edm::LogVerbatim("HGCalGeom") << "waferTypeRotation: Layer " << layer << " Wafer " << waferU << ":" << waferV
1478  << " Index " << std::hex << wl << std::dec << ":" << withinList << " Type " << type
1479  << " Rotation " << rotn;
1480 #endif
1481  return std::make_pair(type, rotn);
1482 }
1483 
1485  bool type(false);
1486  if (waferHexagon8()) {
1487  int wl = HGCalWaferIndex::waferIndex(layer, waferU, waferV, false);
1488  type = (hgpar_->waferTypes_.find(wl) != hgpar_->waferTypes_.end());
1489  } else if (waferHexagon6()) {
1490  int wl = HGCalWaferIndex::waferIndex(layer, waferU, 0, true);
1491  type = (hgpar_->waferTypes_.find(wl) != hgpar_->waferTypes_.end());
1492  }
1493  return type;
1494 }
1495 
1496 double HGCalDDDConstants::waferZ(int lay, bool reco) const {
1497  const auto& index = getIndex(lay, reco);
1498  if (index.first < 0)
1499  return 0;
1500  else
1501  return (reco ? hgpar_->zLayerHex_[index.first] : HGCalParameters::k_ScaleToDDD * hgpar_->zLayerHex_[index.first]);
1502 }
1503 
1505  int wafer(0);
1506  if (!tileTrapezoid()) {
1507  for (unsigned int i = 0; i < layers(true); ++i) {
1508  int lay = hgpar_->depth_[i];
1509  wafer += modules(lay, true);
1510  }
1511  } else {
1512  wafer = (int)(hgpar_->moduleLayR_.size());
1513  }
1514  return wafer;
1515 }
1516 
1517 int HGCalDDDConstants::wafers(int layer, int type) const {
1518  int wafer(0);
1519  if (!tileTrapezoid()) {
1520  auto itr = waferLayer_.find(layer);
1521  if (itr != waferLayer_.end()) {
1522  unsigned ity = (type > 0 && type <= 2) ? type : 0;
1523  wafer = (itr->second)[ity];
1524  }
1525  } else {
1526  const auto& index = getIndex(layer, true);
1527  wafer = 1 + hgpar_->lastModule_[index.first] - hgpar_->firstModule_[index.first];
1528  }
1529  return wafer;
1530 }
1531 
1533  double xx, double yy, const double& cellR, const std::vector<double>& posX, const std::vector<double>& posY) const {
1534  int num(0);
1535  const double tol(0.00001);
1536  double cellY = 2.0 * cellR * tan30deg_;
1537  for (unsigned int k = 0; k < posX.size(); ++k) {
1538  double dx = std::abs(xx - posX[k]);
1539  double dy = std::abs(yy - posY[k]);
1540  if (dx <= (cellR + tol) && dy <= (cellY + tol)) {
1541  double xmax = (dy <= 0.5 * cellY) ? cellR : (cellR - (dy - 0.5 * cellY) / tan30deg_);
1542  if (dx <= (xmax + tol)) {
1543  num = k;
1544  break;
1545  }
1546  }
1547  }
1548  return num;
1549 }
1550 
1552  double xloc, double yloc, int cellType, int& cellU, int& cellV, bool extend, bool debug) const {
1553  int N = (cellType == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
1554  double Rc = 2 * rmax_ / (3 * N);
1555  double rc = 0.5 * Rc * sqrt3_;
1556  double RcT = (extend) ? (2 * rmaxT_ / (3 * N)) : Rc;
1557  double rcT = 0.5 * RcT * sqrt3_;
1558  double v0 = ((xloc / Rc - 1.0) / 1.5);
1559  int cv0 = (v0 > 0) ? (N + (int)(v0 + 0.5)) : (N - (int)(-v0 + 0.5));
1560  double u0 = (0.5 * yloc / rc + 0.5 * cv0);
1561  int cu0 = (u0 > 0) ? (N / 2 + (int)(u0 + 0.5)) : (N / 2 - (int)(-u0 + 0.5));
1562  cu0 = std::max(0, std::min(cu0, 2 * N - 1));
1563  cv0 = std::max(0, std::min(cv0, 2 * N - 1));
1564  if (cv0 - cu0 >= N)
1565  cv0 = cu0 + N - 1;
1566 #ifdef EDM_ML_DEBUG
1567  if (debug)
1568  edm::LogVerbatim("HGCalGeom") << "cellHex: input " << xloc << ":" << yloc << ":" << cellType << " parameter " << rc
1569  << ":" << Rc << " u0 " << u0 << ":" << cu0 << " v0 " << v0 << ":" << cv0;
1570 #endif
1571  bool found(false);
1572  static constexpr int shift[3] = {0, 1, -1};
1573  for (int i1 = 0; i1 < 3; ++i1) {
1574  cellU = cu0 + shift[i1];
1575  for (int i2 = 0; i2 < 3; ++i2) {
1576  cellV = cv0 + shift[i2];
1577  if (((cellV - cellU) < N) && ((cellU - cellV) <= N) && (cellU >= 0) && (cellV >= 0) && (cellU < 2 * N) &&
1578  (cellV < 2 * N)) {
1579  double xc = (1.5 * (cellV - N) + 1.0) * Rc;
1580  double yc = (2 * cellU - cellV - N) * rc;
1581  if ((std::abs(yloc - yc) <= rcT) && (std::abs(xloc - xc) <= RcT) &&
1582  ((std::abs(xloc - xc) <= 0.5 * RcT) || (std::abs(yloc - yc) <= sqrt3_ * (RcT - std::abs(xloc - xc))))) {
1583 #ifdef EDM_ML_DEBUG
1584  if (debug)
1585  edm::LogVerbatim("HGCalGeom")
1586  << "cellHex: local " << xc << ":" << yc << " difference " << std::abs(xloc - xc) << ":"
1587  << std::abs(yloc - yc) << ":" << sqrt3_ * (Rc - std::abs(yloc - yc)) << " comparator " << rc << ":"
1588  << Rc << " (u,v) = (" << cellU << "," << cellV << ")";
1589 #endif
1590  found = true;
1591  break;
1592  }
1593  }
1594  }
1595  if (found)
1596  break;
1597  }
1598  if (!found) {
1599  cellU = cu0;
1600  cellV = cv0;
1601  }
1602 }
1603 
1604 std::pair<int, float> HGCalDDDConstants::getIndex(int lay, bool reco) const {
1605  int indx = layerIndex(lay, reco);
1606  if (indx < 0)
1607  return std::make_pair(-1, 0);
1608  float cell(0);
1609  if (waferHexagon6()) {
1610  cell = (reco ? hgpar_->moduleCellR_[0] : hgpar_->moduleCellS_[0]);
1611  } else {
1612  if (waferHexagon8()) {
1613  cell = (reco ? hgpar_->moduleCellR_[0] : hgpar_->moduleCellS_[0]);
1614  } else {
1615  cell = hgpar_->scintCellSize(lay);
1616  }
1617  }
1618  return std::make_pair(indx, cell);
1619 }
1620 
1622  int ll(-1);
1623  if (waferHexagon6() && reco) {
1624  ll = static_cast<int>(std::find(hgpar_->depthLayerF_.begin(), hgpar_->depthLayerF_.end(), index) -
1625  hgpar_->depthLayerF_.begin());
1626  if (ll == static_cast<int>(hgpar_->depthLayerF_.size()))
1627  ll = -1;
1628  } else {
1629  ll = static_cast<int>(std::find(hgpar_->layerIndex_.begin(), hgpar_->layerIndex_.end(), index) -
1630  hgpar_->layerIndex_.begin());
1631  if (ll == static_cast<int>(hgpar_->layerIndex_.size()))
1632  ll = -1;
1633  }
1634 #ifdef EDM_ML_DEBUG
1635  edm::LogVerbatim("HGCalGeom") << "LayerFromIndex for " << index << ":" << reco << ":" << waferHexagon6() << " is"
1636  << ll << ":" << (ll + hgpar_->firstLayer_);
1637 #endif
1638  return ((ll < 0) ? ll : (ll + hgpar_->firstLayer_));
1639 }
1640 
1641 bool HGCalDDDConstants::isValidCell(int lay, int wafer, int cell) const {
1642  // Calculate the position of the cell
1643  // Works for options HGCalHexagon/HGCalHexagonFull
1644  double x = hgpar_->waferPosX_[wafer];
1645  double y = hgpar_->waferPosY_[wafer];
1646  if (hgpar_->waferTypeT_[wafer] - 1 == HGCSiliconDetId::HGCalFine) {
1647  x += hgpar_->cellFineX_[cell];
1648  y += hgpar_->cellFineY_[cell];
1649  } else {
1650  x += hgpar_->cellCoarseX_[cell];
1651  y += hgpar_->cellCoarseY_[cell];
1652  }
1653  double rr = sqrt(x * x + y * y);
1654  bool result = ((rr >= hgpar_->rMinLayHex_[lay - 1]) && (rr <= hgpar_->rMaxLayHex_[lay - 1]) &&
1655  (wafer < (int)(hgpar_->waferPosX_.size())));
1656 #ifdef EDM_ML_DEBUG
1657  if (!result)
1658  edm::LogVerbatim("HGCalGeom") << "Input " << lay << ":" << wafer << ":" << cell << " Position " << x << ":" << y
1659  << ":" << rr << " Compare Limits " << hgpar_->rMinLayHex_[lay - 1] << ":"
1660  << hgpar_->rMaxLayHex_[lay - 1] << " Flag " << result;
1661 #endif
1662  return result;
1663 }
1664 
1665 bool HGCalDDDConstants::isValidCell8(int lay, int waferU, int waferV, int cellU, int cellV, int type) const {
1666  float x(0), y(0);
1667  int kndx = cellV * 100 + cellU;
1668  if (type == 0) {
1669  auto ktr = hgpar_->cellFineIndex_.find(kndx);
1670  if (ktr != hgpar_->cellFineIndex_.end()) {
1671  x = hgpar_->cellFineX_[ktr->second];
1672  y = hgpar_->cellFineY_[ktr->second];
1673  }
1674 #ifdef EDM_ML_DEBUG
1675  edm::LogVerbatim("HGCalGeom") << "Fine " << cellU << ":" << cellV << ":" << kndx << ":" << x << ":" << y << ":"
1676  << (ktr != hgpar_->cellFineIndex_.end());
1677 #endif
1678  } else {
1679  auto ktr = hgpar_->cellCoarseIndex_.find(kndx);
1680  if (ktr != hgpar_->cellCoarseIndex_.end()) {
1681  x = hgpar_->cellCoarseX_[ktr->second];
1682  y = hgpar_->cellCoarseY_[ktr->second];
1683  }
1684 #ifdef EDM_ML_DEBUG
1685  edm::LogVerbatim("HGCalGeom") << "Coarse " << cellU << ":" << cellV << ":" << kndx << ":" << x << ":" << y << ":"
1686  << (ktr != hgpar_->cellCoarseIndex_.end());
1687 #endif
1688  }
1689  const auto& xy = waferPositionNoRot(lay, waferU, waferV, true, false);
1690  x += xy.first;
1691  y += xy.second;
1692 #ifdef EDM_ML_DEBUG
1693  edm::LogVerbatim("HGCalGeom") << "With wafer (" << waferU << "," << waferV << ") " << x << ":" << y;
1694 #endif
1695  double rr = sqrt(x * x + y * y);
1696  int ll = lay - hgpar_->firstLayer_;
1697  bool result = ((rr >= hgpar_->rMinLayHex_[ll]) && (rr <= hgpar_->rMaxLayHex_[ll]));
1698 #ifdef EDM_ML_DEBUG
1699  edm::LogVerbatim("HGCalGeom") << "Input " << lay << ":" << ll << ":" << waferU << ":" << waferV << ":" << cellU << ":"
1700  << cellV << " Position " << x << ":" << y << ":" << rr << " Compare Limits "
1701  << hgpar_->rMinLayHex_[ll] << ":" << hgpar_->rMaxLayHex_[ll] << " Flag " << result;
1702 #endif
1704  int N = (type == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
1705  auto partn = waferTypeRotation(lay, waferU, waferV, false, false);
1706  result = HGCalWaferMask::goodCell(cellU, cellV, N, partn.first, partn.second);
1707 #ifdef EDM_ML_DEBUG
1708  edm::LogVerbatim("HGCalGeom") << "Input " << lay << ":" << waferU << ":" << waferV << ":" << cellU << ":" << cellV
1709  << " N " << N << " part " << partn.first << ":" << partn.second << " Result "
1710  << result;
1711 #endif
1712  }
1713  return result;
1714 }
1715 
1716 int32_t HGCalDDDConstants::waferIndex(int wafer, int index) const {
1717  int layer = layerFromIndex(index, true);
1720  int indx = HGCalWaferIndex::waferIndex(layer, waferU, waferV);
1721 #ifdef EDM_ML_DEBUG
1722  edm::LogVerbatim("HGCalGeom") << "WaferIndex for " << wafer << ":" << index << " (" << layer << ":" << waferU << ":"
1723  << waferV << ") " << indx;
1724 #endif
1725  return indx;
1726 }
1727 
1728 bool HGCalDDDConstants::waferInLayerTest(int wafer, int lay, bool full) const {
1729  bool in = (waferHexagon6()) ? true : false;
1730  if (!in) {
1731  double xpos = hgpar_->waferPosX_[wafer] + hgpar_->xLayerHex_[lay];
1732  double ypos = hgpar_->waferPosY_[wafer] + hgpar_->yLayerHex_[lay];
1733  std::pair<int, int> corner = HGCalGeomTools::waferCorner(
1734  xpos, ypos, rmax_, hexside_, hgpar_->rMinLayHex_[lay], hgpar_->rMaxLayHex_[lay], in);
1735  in = (full ? (corner.first > 0) : (corner.first == (int)(HGCalParameters::k_CornerSize)));
1736  if (in && fullAndPart_) {
1737  int indx = waferIndex(wafer, lay);
1738  in = (hgpar_->waferInfoMap_.find(indx) != hgpar_->waferInfoMap_.end());
1739 #ifdef EDM_ML_DEBUG
1740  if (!in)
1741  edm::LogVerbatim("HGCalGeom") << "WaferInLayerTest: Layer " << lay << " wafer " << wafer << " index " << indx
1742  << "( " << HGCalWaferIndex::waferLayer(indx) << ", "
1743  << HGCalWaferIndex::waferU(indx) << ", " << HGCalWaferIndex::waferV(indx)
1744  << ") in " << in;
1745 #endif
1746  }
1747 #ifdef EDM_ML_DEBUG
1748  edm::LogVerbatim("HGCalGeom") << "WaferInLayerTest: Layer " << lay << " wafer " << wafer << " R-limits "
1749  << hgpar_->rMinLayHex_[lay] << ":" << hgpar_->rMaxLayHex_[lay] << " Corners "
1750  << corner.first << ":" << corner.second << " In " << in;
1751 #endif
1752  }
1753  return in;
1754 }
1755 
1756 std::pair<double, double> HGCalDDDConstants::waferPositionNoRot(
1757  int lay, int waferU, int waferV, bool reco, bool debug) const {
1758  int ll = lay - hgpar_->firstLayer_;
1759  double x = hgpar_->xLayerHex_[ll];
1760  double y = hgpar_->yLayerHex_[ll];
1761 #ifdef EDM_ML_DEBUG
1762  if (debug)
1763  edm::LogVerbatim("HGCalGeom") << "Layer " << lay << ":" << ll << " Shift " << hgpar_->xLayerHex_[ll] << ":"
1764  << hgpar_->yLayerHex_[ll] << " U:V " << waferU << ":" << waferV;
1765 #endif
1766  if (!reco) {
1769  }
1770  const auto& xy = waferPosition(waferU, waferV, reco);
1771  x += xy.first;
1772  y += xy.second;
1773 #ifdef EDM_ML_DEBUG
1774  if (debug)
1775  edm::LogVerbatim("HGCalGeom") << "With wafer " << x << ":" << y << ":" << xy.first << ":" << xy.second;
1776 #endif
1777  return std::make_pair(x, y);
1778 }
1779 
1780 std::pair<double, double> HGCalDDDConstants::waferPosition(int waferU, int waferV, bool reco) const {
1781  double xx(0), yy(0);
1782  int indx = HGCalWaferIndex::waferIndex(0, waferU, waferV);
1783  auto itr = hgpar_->wafersInLayers_.find(indx);
1784  if (itr != hgpar_->wafersInLayers_.end()) {
1785  xx = hgpar_->waferPosX_[itr->second];
1786  yy = hgpar_->waferPosY_[itr->second];
1787  }
1788  if (!reco) {
1791  }
1792  return std::make_pair(xx, yy);
1793 }
1794 
1796 
std::vector< int > iradMaxBH_
bool isHalfCell(int waferType, int cell) const
std::vector< double > waferPosY_
Log< level::Info, true > LogVerbatim
std::vector< int > layer_
const int nphi
std::vector< int > depthLayerF_
bool isValidTrap(int lay, int ieta, int iphi) const
std::vector< int > depth_
std::vector< double > zFrontMin_
std::vector< double > moduleCellR_
int getTypeTrap(int layer) const
wafer_map cellFineIndex_
layer_map copiesInLayers_
static void radius(double zf, double zb, std::vector< double > const &zFront1, std::vector< double > const &rFront1, std::vector< double > const &slope1, std::vector< double > const &zFront2, std::vector< double > const &rFront2, std::vector< double > const &slope2, int flag, std::vector< double > &zz, std::vector< double > &rin, std::vector< double > &rout)
int getLayer(double z, bool reco) const
std::vector< std::pair< double, double > > layerRotV_
std::vector< HGCalParameters::hgtrap > getModules() const
bool waferHexagon6() const
std::vector< bool > cellCoarseHalf_
std::vector< bool > cellFineHalf_
uint16_t *__restrict__ id
int scintType(const int layer) const
void waferFromPosition(const double x, const double y, int &wafer, int &icell, int &celltyp) const
int type() const
get the type
Definition: HFNoseDetId.h:50
static int32_t getUnpackedU(int id)
Definition: HGCalTypes.cc:16
std::vector< int > moduleLayR_
static constexpr int k_fourCorners
double cellSizeHex(int type) const
HGCalParameters::hgtrform getTrForm(unsigned int k) const
Simrecovecs max_modules_layer_
int lastLayer(bool reco) const
std::pair< int, int > waferTypeRotation(int layer, int waferU, int waferV, bool fromFile=false, bool debug=false) const
double cellThickness(int layer, int waferU, int waferV) const
unsigned int layersInit(bool reco) const
int32_t waferU(const int32_t index)
int32_t waferLayer(const int32_t index)
int waferU() const
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
static bool goodCell(int u, int v, int N, int type, int rotn)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
bool cellInLayer(int waferU, int waferV, int cellU, int cellV, int lay, bool reco) const
int maxRows(int lay, bool reco) const
int getTypeHex(int layer, int waferU, int waferV) const
std::vector< double > cellFineY_
unsigned int waferFileSize() const
static int32_t getUnpackedV(int id)
Definition: HGCalTypes.cc:22
bool waferInLayerTest(int wafer, int lay, bool full) const
std::unordered_map< int32_t, bool > waferIn_
int zside(DetId const &)
int modulesInit(int lay, bool reco) const
std::pair< float, float > locateCell(int cell, int lay, int type, bool reco) const
std::vector< uint32_t > trformIndex_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
bool isValidHex(int lay, int mod, int cell, bool reco) const
std::vector< int > layerGroupM_
bool isValidCell8(int lay, int waferU, int waferV, int cellU, int cellV, int type) const
double scintCellSize(const int layer) const
float float float z
int cellHex(double xx, double yy, const double &cellR, const std::vector< double > &posX, const std::vector< double > &posY) const
int32_t waferIndex(int wafer, int index) const
int layerIndex(int lay, bool reco) const
static constexpr uint32_t k_CornerSize
constexpr std::array< uint8_t, layerIndexSize > layer
const uint16_t range(const Frame &aFrame)
tuple result
Definition: mps_fire.py:311
wafer_map wafersInLayers_
HGCalGeomTools geomTools_
std::pair< float, float > locateCellHex(int cell, int wafer, bool reco) const
U second(std::pair< T, U > const &p)
std::vector< double > cellCoarseX_
static constexpr int scintillatorFile
std::pair< double, double > rangeR(double z, bool reco) const
std::map< int, HGCWaferParam > waferLayer_
int32_t tileIndex(int32_t layer, int32_t ring, int32_t phi)
std::vector< std::pair< int, int > > tileRingRange_
int modules(int lay, bool reco) const
static std::pair< int32_t, int32_t > waferCorner(double xpos, double ypos, double r, double R, double rMin, double rMax, bool oldBug=false)
std::vector< int > firstModule_
std::pair< int, int > simToReco(int cell, int layer, int mod, bool half) const
int getType(double xpos, double ypos, double zpos)
unsigned int getTrFormN() const
double distFromEdgeTrap(double x, double y, double z) const
std::pair< double, double > rangeZ(bool reco) const
unsigned int layers(bool reco) const
int numberCellsHexagon(int wafer) const
std::vector< double > cellSize_
std::vector< int > waferUVMaxLayer_
std::vector< HGCalParameters::hgtrform > getTrForms() const
int waferV() const
Definition: HFNoseDetId.h:78
int layer() const
get the layer #
Definition: HFNoseDetId.h:56
std::pair< double, double > cellEtaPhiTrap(int type, int irad) const
std::vector< int > layerIndex_
double mouseBite(bool reco) const
std::vector< double > yLayerHex_
hgtrform getTrForm(unsigned int k) const
std::pair< double, double > shiftXY(int waferPosition, double waferSize) const
int type() const
get the type
T sqrt(T t)
Definition: SSEVec.h:19
std::pair< int, float > getIndex(int lay, bool reco) const
int waferU() const
Definition: HFNoseDetId.h:75
int layer() const
get the layer #
HGCalDDDConstants(const HGCalParameters *hp, const std::string &name)
std::pair< float, float > locateCellTrap(int lay, int ieta, int iphi, bool reco) const
if(conf_.getParameter< bool >("UseStripCablingDB"))
std::pair< float, float > localToGlobal8(int lay, int waferU, int waferV, double localX, double localY, bool reco, bool debug) const
std::vector< double > rMaxFront_
int waferV() const
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
bool tileTrapezoid() const
Definition: GenABIO.cc:168
int scintCells(const int layer) const
hgtrap getModule(unsigned int k, bool reco) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
uint16_t const *__restrict__ x
Definition: gpuClustering.h:43
std::vector< double > slopeTop_
std::vector< int > layerCenter_
bool isValidCell(int layindex, int wafer, int cell) const
int waferTypeL(int wafer) const
int getPhiBins(int lay) const
std::vector< double > rMinLayHex_
int32_t waferIndex(int32_t layer, int32_t waferU, int32_t waferV, bool old=false)
HGCalTypes::CellType cellType(int type, int waferU, int waferV) const
std::pair< int, int > getREtaRange(int lay) const
std::pair< int, int > rowColumnWafer(const int wafer) const
std::vector< double > zLayerHex_
double waferZ(int layer, bool reco) const
std::vector< int > layerType_
#define M_PI
def all
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
waferT_map waferTypes_
#define TYPELOOKUP_DATA_REG(_dataclass_)
Definition: typelookup.h:102
std::pair< double, double > getXY(int layer, double x, double y, bool forwd) const
bool waferVirtual(int layer, int waferU, int waferV) const
std::vector< double > rMaxLayHex_
caConstants::TupleMultiplicity const CAHitNtupletGeneratorKernelsGPU::HitToTuple const cms::cuda::AtomicPairCounter GPUCACell const *__restrict__ cells
std::vector< double > slopeMin_
Basic2DVector< T > xy() const
Definition: DetId.h:17
std::vector< int > lastModule_
std::array< int, 4 > waferMax_
int waferFromCopy(int copy) const
static constexpr double k_ScaleToDDD
std::vector< double > radiusMixBoundary_
#define debug
Definition: HDRShower.cc:19
#define N
Definition: blowfish.cc:9
std::vector< double > cellThickness_
static constexpr int k_fiveCorners
std::vector< int > layerGroup_
constexpr uint16_t localX(uint16_t px)
part
Definition: HCALResponse.h:20
std::vector< double > moduleCellS_
bool maskCell(const DetId &id, int corners) const
constexpr uint16_t localY(uint16_t py, uint16_t n)
double b
Definition: hdecay.h:118
std::array< int, 5 > assignCellHex(float x, float y, int lay, bool reco, bool extend=false, bool debug=false) const
int numberCells(bool reco) const
wafer_map cellCoarseIndex_
bool waferFullInLayer(int wafer, int lay, bool reco) const
bool isValidHex8(int lay, int waferU, int waferV, bool fullAndPart=false) const
std::vector< double > rMinFront_
std::vector< int > iradMinBH_
std::vector< double > cellFineX_
wafer_map typesInLayers_
const HGCalGeometryMode::GeometryMode mode_
static constexpr double k_ScaleFromDDD
std::pair< double, double > waferParameters(bool reco) const
std::vector< int > layerGroupO_
static constexpr int k_OffsetRotation
static constexpr int k_threeCorners
__host__ __device__ constexpr RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
std::array< int, 3 > HGCWaferParam
int32_t waferV(const int32_t index)
std::vector< int > waferCopy_
bool waferHexagon8() const
int tileCount(int layer, int ring) const
static unsigned int const shift
std::vector< int > depthIndex_
std::pair< double, double > waferPosition(int wafer, bool reco) const
std::pair< int, int > assignCell(float x, float y, int lay, int subSec, bool reco) const
static constexpr double tan30deg_
std::array< int, 3 > assignCellTrap(float x, float y, float z, int lay, bool reco) const
std::vector< double > zFrontTop_
std::vector< double > radiusLayer_[2]
std::array< uint32_t, 2 > tot_layers_
std::vector< int > waferTypeT_
static constexpr int32_t WaferCornerMin
Definition: HGCalTypes.h:90
std::vector< double > cellCoarseY_
Log< level::Warning, false > LogWarning
waferInfo_map waferInfoMap_
HGCalParameters::hgtrap getModule(unsigned int k, bool hexType, bool reco) const
std::pair< double, double > waferPositionNoRot(int lay, int waferU, int waferV, bool reco, bool debug=false) const
int col
Definition: cuy.py:1009
__host__ __device__ V wmin
const HGCalParameters * hgpar_
int layerFromIndex(int index, bool reco) const
int maxCells(bool reco) const
std::vector< double > waferPosX_
int modifyUV(int uv, int type1, int type2) const
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
tileInfo_map tileInfoMap_
static constexpr int k_allCorners
std::vector< int > waferTypeL_
std::vector< double > xLayerHex_
std::pair< double, double > rangeRLayer(int lay, bool reco) const
tuple size
Write out results.
int getUVMax(int type) const
double distFromEdgeHex(double x, double y, double z) const
bool waferInLayer(int wafer, int lay, bool reco) const
int waferType(DetId const &id, bool fromFile=false) const
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
__host__ __device__ V V wmax
static bool maskCell(int u, int v, int N, int ncor, int fcor, int corners)