CMS 3D CMS Logo

HGCalDDDConstants.cc
Go to the documentation of this file.
2 
15 
16 #include <algorithm>
17 #include <bitset>
18 #include <iterator>
19 #include <functional>
20 #include <numeric>
21 
22 //#define EDM_ML_DEBUG
23 using namespace geant_units::operators;
24 
26  : hgpar_(hp), sqrt3_(std::sqrt(3.0)), mode_(hgpar_->mode_), fullAndPart_(waferHexagon8File()) {
27 #ifdef EDM_ML_DEBUG
28  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::Mode " << mode_ << " FullAndPart " << fullAndPart_;
29 #endif
30  if (waferHexagon6() || waferHexagon8()) {
33  hexside_ = 2.0 * rmax_ * tan30deg_;
34  hexsideT_ = 2.0 * rmaxT_ * tan30deg_;
35  hgcell_ = std::make_unique<HGCalCell>(2.0 * rmaxT_, hgpar_->nCellsFine_, hgpar_->nCellsCoarse_);
36  hgcellUV_ = std::make_unique<HGCalCellUV>(
38  cellOffset_ = std::make_unique<HGCalCellOffset>(hgpar_->waferSize_,
44 #ifdef EDM_ML_DEBUG
45  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::rmax_ " << rmax_ << ":" << rmaxT_ << ":" << hexside_ << ":"
46  << hexsideT_ << " CellSize "
49 #endif
50  } else {
51  hgcell_.reset();
52  hgcellUV_.reset();
53  cellOffset_.reset();
54  }
55  if (cassetteMode()) {
57 #ifdef EDM_ML_DEBUG
58  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::Setup HGCalCassette for " << hgpar_->cassettes_
59  << " cassettes";
60 #endif
61  }
62  // init maps and constants
63  modHalf_ = 0;
65  for (int simreco = 0; simreco < 2; ++simreco) {
66  tot_layers_[simreco] = layersInit((bool)simreco);
67  max_modules_layer_[simreco].resize(tot_layers_[simreco] + 1);
68  for (unsigned int layer = 1; layer <= tot_layers_[simreco]; ++layer) {
69  max_modules_layer_[simreco][layer] = modulesInit(layer, (bool)simreco);
70  if (simreco == 1) {
71  modHalf_ += max_modules_layer_[simreco][layer];
73 #ifdef EDM_ML_DEBUG
74  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::Layer " << layer << " with "
75  << max_modules_layer_[simreco][layer] << ":" << modHalf_ << " modules in RECO";
76  } else {
77  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::Layer " << layer << " with "
78  << max_modules_layer_[simreco][layer] << " modules in SIM";
79 #endif
80  }
81  }
82 #ifdef EDM_ML_DEBUG
83  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::SimReco " << simreco << " with " << tot_layers_[simreco]
84  << " Layers";
85 #endif
86  }
87  tot_wafers_ = wafers();
88 
89 #ifdef EDM_ML_DEBUG
90  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants initialized for " << name << " with " << layers(false) << ":"
91  << layers(true) << " layers, " << wafers() << ":" << 2 * modHalf_
92  << " wafers with maximum " << maxWafersPerLayer_ << " per layer and "
93  << "maximum of " << maxCells(false) << ":" << maxCells(true) << " cells";
94 #endif
95  if (waferHexagon6() || waferHexagon8()) {
96  int wminT(9999999), wmaxT(-9999999), kount1(0), kount2(0);
97  for (unsigned int i = 0; i < getTrFormN(); ++i) {
98  int lay0 = getTrForm(i).lay;
99  int wmin(9999999), wmax(-9999999), kount(0);
100  for (int wafer = 0; wafer < sectors(); ++wafer) {
101  bool waferIn = waferInLayer(wafer, lay0, true);
102  if (waferHexagon8()) {
103  int kndx = HGCalWaferIndex::waferIndex(lay0,
106  waferIn_[kndx] = waferIn;
107  }
108  if (waferIn) {
109  int waferU = ((waferHexagon6()) ? wafer : HGCalWaferIndex::waferU(hgpar_->waferCopy_[wafer]));
110  if (waferU < wmin)
111  wmin = waferU;
112  if (waferU > wmax)
113  wmax = waferU;
114  ++kount;
115  }
116  }
117  if (wminT > wmin)
118  wminT = wmin;
119  if (wmaxT < wmax)
120  wmaxT = wmax;
121  if (kount1 < kount)
122  kount1 = kount;
123  kount2 += kount;
124 #ifdef EDM_ML_DEBUG
125  int lay1 = getIndex(lay0, true).first;
126  edm::LogVerbatim("HGCalGeom") << "Index " << i << " Layer " << lay0 << ":" << lay1 << " Wafer " << wmin << ":"
127  << wmax << ":" << kount;
128 #endif
129  HGCWaferParam a1{{wmin, wmax, kount}};
130  waferLayer_[lay0] = a1;
131  }
132  waferMax_ = std::array<int, 4>{{wminT, wmaxT, kount1, kount2}};
133 #ifdef EDM_ML_DEBUG
134  edm::LogVerbatim("HGCalGeom") << "Overall wafer statistics: " << wminT << ":" << wmaxT << ":" << kount1 << ":"
135  << kount2;
136 #endif
137  }
138 }
139 
140 std::pair<int, int> HGCalDDDConstants::assignCell(float x, float y, int lay, int subSec, bool reco) const {
141  const auto& index = getIndex(lay, reco);
142  if (index.first < 0)
143  return std::make_pair(-1, -1);
144  if (waferHexagon6()) {
145  float xx = (reco) ? x : HGCalParameters::k_ScaleFromDDD * x;
146  float yy = (reco) ? y : HGCalParameters::k_ScaleFromDDD * y;
147 
148  // First the wafer
149  int wafer = cellHex(xx, yy, rmax_, hgpar_->waferPosX_, hgpar_->waferPosY_);
150  if (wafer < 0 || wafer >= static_cast<int>(hgpar_->waferTypeT_.size())) {
151  edm::LogWarning("HGCalGeom") << "Wafer no. out of bound for " << wafer << ":" << (hgpar_->waferTypeT_).size()
152  << ":" << (hgpar_->waferPosX_).size() << ":" << (hgpar_->waferPosY_).size()
153  << " ***** ERROR *****";
154  return std::make_pair(-1, -1);
155  } else {
156  // Now the cell
157  xx -= hgpar_->waferPosX_[wafer];
158  yy -= hgpar_->waferPosY_[wafer];
159  if (hgpar_->waferTypeT_[wafer] == 1)
160  return std::make_pair(wafer,
161  cellHex(xx,
162  yy,
165  hgpar_->cellFineY_));
166  else
167  return std::make_pair(wafer,
168  cellHex(xx,
169  yy,
172  hgpar_->cellCoarseY_));
173  }
174  } else {
175  return std::make_pair(-1, -1);
176  }
177 }
178 
180  float x, float y, int zside, int lay, bool reco, bool extend, bool debug) const {
181  int waferU(0), waferV(0), waferType(-1), cellU(0), cellV(0);
182  if (waferHexagon8()) {
183  double xx = (reco) ? HGCalParameters::k_ScaleToDDD * x : x;
184  double yy = (reco) ? HGCalParameters::k_ScaleToDDD * y : y;
185  double wt(1.0);
186 #ifdef EDM_ML_DEBUG
187  edm::LogVerbatim("HGCalGeom") << "assignCellHex x " << x << ":" << xx << " y " << y << ":" << yy << " Lay " << lay;
188 #endif
189  waferFromPosition(xx, yy, zside, lay, waferU, waferV, cellU, cellV, waferType, wt, extend, debug);
190  }
191  return std::array<int, 5>{{waferU, waferV, waferType, cellU, cellV}};
192 }
193 
194 std::array<int, 3> HGCalDDDConstants::assignCellTrap(float x, float y, float z, int layer, bool reco) const {
195  int irad(-1), iphi(-1), type(-1);
196  const auto& indx = getIndex(layer, reco);
197  if (indx.first < 0)
198  return std::array<int, 3>{{irad, iphi, type}};
199  int zside = (z > 0) ? 1 : -1;
200  double xx = (reco) ? (zside * x) : (zside * HGCalParameters::k_ScaleFromDDD * x);
201  double yy = (reco) ? y : HGCalParameters::k_ScaleFromDDD * y;
202  int ll = layer - hgpar_->firstLayer_;
203  xx -= hgpar_->xLayerHex_[ll];
204  yy -= hgpar_->yLayerHex_[ll];
205  double phi = (((yy == 0.0) && (xx == 0.0)) ? 0. : std::atan2(yy, xx));
206  if (phi < 0)
207  phi += (2.0 * M_PI);
208  if (indx.second != 0)
209  iphi = (1 + static_cast<int>(phi / indx.second)) % hgpar_->scintCells(layer);
210  if (iphi == 0)
212  if (cassetteMode()) {
214  auto cshift = hgcassette_.getShift(layer, -1, cassette);
215 #ifdef EDM_ML_DEBUG
216  std::ostringstream st1;
217  st1 << "Cassette " << cassette << " Shift " << cshift.first << ":" << cshift.second << " Original " << xx << ":"
218  << yy;
219 #endif
220  xx += (zside * cshift.first);
221  yy -= cshift.second;
222 #ifdef EDM_ML_DEBUG
223  st1 << " Shifted " << xx << ":" << yy;
224  edm::LogVerbatim("HGCalGeomT") << st1.str();
225 #endif
226  }
228  double r = std::sqrt(xx * xx + yy * yy);
229  auto ir = std::lower_bound(hgpar_->radiusLayer_[type].begin(), hgpar_->radiusLayer_[type].end(), r);
230  irad = static_cast<int>(ir - hgpar_->radiusLayer_[type].begin());
231  irad = std::clamp(irad, hgpar_->iradMinBH_[indx.first], hgpar_->iradMaxBH_[indx.first]);
232 #ifdef EDM_ML_DEBUG
233  edm::LogVerbatim("HGCalGeomT") << "assignCellTrap Input " << x << ":" << y << ":" << z << ":" << layer << ":" << reco
234  << " x|y|r " << xx << ":" << yy << ":" << r << " phi " << phi << ":"
235  << convertRadToDeg(phi) << " o/p " << irad << ":" << iphi << ":" << type;
236 #endif
237  if (!tileExist(zside, layer, irad, iphi)) {
238  if (tileRingEdge(r, layer, irad)) {
239  if (std::abs(r - hgpar_->radiusLayer_[type][irad - 1]) < tol_) {
240  --irad;
241  if (irad <= hgpar_->iradMinBH_[indx.first])
242  irad = hgpar_->iradMinBH_[indx.first];
243  } else {
244  ++irad;
245  if (irad > hgpar_->iradMaxBH_[indx.first])
246  irad = hgpar_->iradMaxBH_[indx.first];
247  }
248 #ifdef EDM_ML_DEBUG
249  edm::LogVerbatim("HGCalGeomT") << "assignCellTrap: ring # modified to " << irad << ":"
250  << hgpar_->iradMinBH_[indx.first] << ":" << hgpar_->iradMaxBH_[indx.first];
251  ;
252 #endif
253  } else if (tilePhiEdge(phi, layer, iphi)) {
254  if (std::abs(phi - hgpar_->scintCellSize(layer) * (iphi - 1)) < tol_) {
255  --iphi;
256  if (iphi <= 0)
257  iphi = 1;
258  } else {
259  ++iphi;
260  if (iphi > hgpar_->scintCells(layer))
261  iphi = 1;
262  }
263 #ifdef EDM_ML_DEBUG
264  edm::LogVerbatim("HGCalGeomT") << "assignCellTrap: iphi # modified to " << iphi << ":"
265  << hgpar_->scintCells(layer);
266 #endif
267  }
268  }
269  return std::array<int, 3>{{irad, iphi, type}};
270 }
271 
273  bool shift(false);
274  if (cassetteMode()) {
276  auto ktr = hgpar_->waferInfoMap_.find(indx);
277  if (ktr != hgpar_->waferInfoMap_.end()) {
278  auto cshift = hgcassette_.getShift(layer, zside, (ktr->second).cassette);
279  if ((cshift.first != 0) || (cshift.second != 0))
280  shift = true;
281  }
282  }
283  return shift;
284 }
285 
287  bool shift(false);
288  if (cassetteMode()) {
289  auto cshift = hgcassette_.getShift(layer, zside, cassetteTile(iphi));
290  if ((cshift.first != 0) || (cshift.second != 0))
291  shift = true;
292  }
293  return shift;
294 }
295 
296 std::pair<double, double> HGCalDDDConstants::cellEtaPhiTrap(int type, int irad) const {
297  double dr(0), df(0);
298  if (tileTrapezoid()) {
299  double r = 0.5 * ((hgpar_->radiusLayer_[type][irad - 1] + hgpar_->radiusLayer_[type][irad]));
300  dr = (hgpar_->radiusLayer_[type][irad] - hgpar_->radiusLayer_[type][irad - 1]);
301  df = r * hgpar_->cellSize_[type];
302  }
303  return std::make_pair(dr, df);
304 }
305 
306 bool HGCalDDDConstants::cellInLayer(int waferU, int waferV, int cellU, int cellV, int lay, int zside, bool reco) const {
307  const auto& indx = getIndex(lay, true);
308  if (indx.first >= 0) {
309  if (cassetteMode()) {
310  int indx = HGCalWaferIndex::waferIndex(lay, waferU, waferV);
311  auto ktr = hgpar_->waferInfoMap_.find(indx);
312  int part = (ktr != hgpar_->waferInfoMap_.end()) ? (ktr->second).part : HGCalTypes::WaferFull;
313  return HGCalWaferMask::goodCell(cellU, cellV, part);
315  int indx = HGCalWaferIndex::waferIndex(lay, waferU, waferV);
316  auto ktr = hgpar_->waferInfoMap_.find(indx);
318  if (ktr != hgpar_->waferInfoMap_.end()) {
319  thck = (ktr->second).type;
320  part = (ktr->second).part;
321  rotn = (ktr->second).orient;
322  }
323  int ncell = ((thck == HGCalTypes::WaferHD120) || (thck == HGCalTypes::WaferHD200)) ? hgpar_->nCellsFine_
325  return HGCalWaferMask::goodCell(cellU, cellV, ncell, part, rotn);
326  } else if (waferHexagon8() || waferHexagon6()) {
327  const auto& xy =
328  ((waferHexagon8()) ? locateCell(zside, lay, waferU, waferV, cellU, cellV, reco, true, false, false, false)
329  : locateCell(cellU, lay, waferU, reco));
330  double rpos = sqrt(xy.first * xy.first + xy.second * xy.second);
331  return ((rpos >= hgpar_->rMinLayHex_[indx.first]) && (rpos <= hgpar_->rMaxLayHex_[indx.first]));
332  } else {
333  return true;
334  }
335  } else {
336  return false;
337  }
338 }
339 
341  double thick(-1);
342  int type = waferType(layer, waferU, waferV, false);
343  if (type >= 0) {
344  if (waferHexagon8()) {
345  thick = 10000.0 * hgpar_->cellThickness_[type]; // cm to micron
346  } else if (waferHexagon6()) {
347  thick = 100.0 * (type + 1); // type = 1,2,3 for 100,200,300 micron
348  }
349  }
350  return thick;
351 }
352 
354  int indx = ((waferHexagon8()) ? ((type >= 1) ? 1 : 0) : ((type == 1) ? 1 : 0));
355  double cell = (tileTrapezoid() ? 0.5 * hgpar_->cellSize_[indx]
357  return cell;
358 }
359 
360 int32_t HGCalDDDConstants::cellType(int type, int cellU, int cellV, int iz, int fwdBack, int orient) const {
361  int placement = (orient < 0) ? HGCalCell::cellPlacementOld : HGCalCell::cellPlacementIndex(iz, fwdBack, orient);
364  auto cellType = HGCalCell::cellType(cellU, cellV, ncell, placement);
365  return cellType.first;
366 }
367 
368 double HGCalDDDConstants::distFromEdgeHex(double x, double y, double z) const {
369  // Assming the point is within a hexagonal plane of the wafer, calculate
370  // the shortest distance from the edge
371  if (z < 0)
372  x = -x;
373  double dist(0);
374  // Input x, y in Geant4 unit and transformed to CMSSW standard
377  if (waferHexagon8()) {
378  int ll = layerIndex(getLayer(z, false), false);
379  xx -= hgpar_->xLayerHex_[ll];
380  yy -= hgpar_->yLayerHex_[ll];
381  }
382  int sizew = static_cast<int>(hgpar_->waferPosX_.size());
383  int wafer = sizew;
384  // Transform to the local coordinate frame of the wafer first
385  for (int k = 0; k < sizew; ++k) {
386  double dx = std::abs(xx - hgpar_->waferPosX_[k]);
387  double dy = std::abs(yy - hgpar_->waferPosY_[k]);
388  if ((dx <= rmax_) && (dy <= hexside_) && ((dy <= 0.5 * hexside_) || (dx * tan30deg_ <= (hexside_ - dy)))) {
389  wafer = k;
390  xx -= hgpar_->waferPosX_[k];
391  yy -= hgpar_->waferPosY_[k];
392  break;
393  }
394  }
395  // Look at only one quarter (both x,y are positive)
396  if (wafer < sizew) {
397  if (std::abs(yy) < 0.5 * hexside_) {
398  dist = rmax_ - std::abs(xx);
399  } else {
400  dist = 0.5 * ((rmax_ - std::abs(xx)) - sqrt3_ * (std::abs(yy) - 0.5 * hexside_));
401  }
402  } else {
403  dist = 0;
404  }
406 #ifdef EDM_ML_DEBUG
407  edm::LogVerbatim("HGCalGeom") << "DistFromEdgeHex: Local " << xx << ":" << yy << " wafer " << wafer << " flag "
408  << (wafer < sizew) << " Distance " << rmax_ << ":" << (rmax_ - std::abs(xx)) << ":"
409  << (std::abs(yy) - 0.5 * hexside_) << ":" << 0.5 * hexside_ << ":" << dist;
410 #endif
411  return dist;
412 }
413 
414 double HGCalDDDConstants::distFromEdgeTrap(double x, double y, double z) const {
415  // Assming the point is within the eta-phi plane of the scintillator tile,
416  // calculate the shortest distance from the edge
417  int lay = getLayer(z, false);
418  double xx = (z < 0) ? -x : x;
419  int indx = layerIndex(lay, false);
420  double r = HGCalParameters::k_ScaleFromDDD * std::sqrt(x * x + y * y);
421  double phi = (r == 0. ? 0. : std::atan2(y, xx));
422  if (phi < 0)
423  phi += (2.0 * M_PI);
424  int type = hgpar_->scintType(lay);
425  double cell = hgpar_->scintCellSize(lay);
426  // Compare with the center of the tile find distances along R and also phi
427  // Take the smaller value
428  auto ir = std::lower_bound(hgpar_->radiusLayer_[type].begin(), hgpar_->radiusLayer_[type].end(), r);
429  int irad = static_cast<int>(ir - hgpar_->radiusLayer_[type].begin());
430  irad = std::clamp(irad, hgpar_->iradMinBH_[indx], hgpar_->iradMaxBH_[indx]);
431  int iphi = 1 + static_cast<int>(phi / cell);
432  double dphi = std::max(0.0, (0.5 * cell - std::abs(phi - (iphi - 0.5) * cell)));
433  double dist = std::min((r - hgpar_->radiusLayer_[type][irad - 1]), (hgpar_->radiusLayer_[type][irad] - r));
434 #ifdef EDM_ML_DEBUG
435  edm::LogVerbatim("HGCalGeom") << "DistFromEdgeTrap: Global " << x << ":" << y << ":" << z << " Layer " << lay
436  << " Index " << indx << ":" << type << " xx " << xx << " R " << r << ":" << irad << ":"
437  << hgpar_->radiusLayer_[type][irad - 1] << ":" << hgpar_->radiusLayer_[type][irad]
438  << " Phi " << phi << ":" << iphi << ":" << (iphi - 0.5) * cell << " cell " << cell
439  << " Dphi " << dphi << " Dist " << dist << ":" << r * dphi;
440 #endif
441  return HGCalParameters::k_ScaleToDDD * std::min(r * dphi, dist);
442 }
443 
444 int HGCalDDDConstants::getLayer(double z, bool reco) const {
445  // Get the layer # from the gloabl z coordinate
446  unsigned int k = 0;
448  const auto& zLayerHex = hgpar_->zLayerHex_;
449  auto itr = std::find_if(zLayerHex.begin() + 1, zLayerHex.end(), [&k, &zz, &zLayerHex](double zLayer) {
450  ++k;
451  return zz < 0.5 * (zLayerHex[k - 1] + zLayerHex[k]);
452  });
453  int lay = (itr == zLayerHex.end()) ? static_cast<int>(zLayerHex.size()) : k;
454  if (waferHexagon6() && reco) {
455  int indx = layerIndex(lay, false);
456  if (indx >= 0)
457  lay = hgpar_->layerGroupO_[indx];
458  } else {
459  lay += (hgpar_->firstLayer_ - 1);
460  }
461  return lay;
462 }
463 
464 HGCalParameters::hgtrap HGCalDDDConstants::getModule(unsigned int indx, bool hexType, bool reco) const {
466  if (hexType) {
467  if (indx >= hgpar_->waferTypeL_.size())
468  edm::LogWarning("HGCalGeom") << "Wafer no. out bound for index " << indx << ":" << (hgpar_->waferTypeL_).size()
469  << ":" << (hgpar_->waferPosX_).size() << ":" << (hgpar_->waferPosY_).size()
470  << " ***** ERROR *****";
471  unsigned int type =
472  ((indx < hgpar_->waferTypeL_.size()) ? hgpar_->waferTypeL_[indx] - 1 : HGCSiliconDetId::HGCalLD300);
473  mytr = hgpar_->getModule(type, reco);
474  } else {
475  mytr = hgpar_->getModule(indx, reco);
476  }
477  return mytr;
478 }
479 
480 std::vector<HGCalParameters::hgtrap> HGCalDDDConstants::getModules() const {
481  std::vector<HGCalParameters::hgtrap> mytrs;
482  for (unsigned int k = 0; k < hgpar_->moduleLayR_.size(); ++k)
483  mytrs.emplace_back(hgpar_->getModule(k, true));
484  return mytrs;
485 }
486 
487 int HGCalDDDConstants::getPhiBins(int lay) const { return (tileTrapezoid() ? hgpar_->scintCells(lay) : 0); }
488 
489 std::pair<double, double> HGCalDDDConstants::getRangeR(int lay, bool reco) const {
490  int indx = layerIndex(lay, false);
491  if ((indx >= 0) && (indx < static_cast<int>(hgpar_->rMinLayHex_.size())))
492  return std::make_pair(hgpar_->rMinLayHex_[indx], hgpar_->rMaxLayHex_[indx]);
493  else
494  return std::make_pair(0, -1.);
495 }
496 
497 std::pair<int, int> HGCalDDDConstants::getREtaRange(int lay) const {
498  int irmin(0), irmax(0);
499  if (tileTrapezoid()) {
500  int indx = layerIndex(lay, false);
501  if ((indx >= 0) && (indx < static_cast<int>(hgpar_->iradMinBH_.size()))) {
502  irmin = hgpar_->iradMinBH_[indx];
503  irmax = hgpar_->iradMaxBH_[indx];
504  }
505  }
506  return std::make_pair(irmin, irmax);
507 }
508 
509 std::vector<HGCalParameters::hgtrform> HGCalDDDConstants::getTrForms() const {
510  std::vector<HGCalParameters::hgtrform> mytrs;
511  for (unsigned int k = 0; k < hgpar_->trformIndex_.size(); ++k)
512  mytrs.emplace_back(hgpar_->getTrForm(k));
513  return mytrs;
514 }
515 
517  // Get the module type for scinitllator
518  if (tileTrapezoid()) {
519  return hgpar_->scintType(layer);
520  } else {
521  return -1;
522  }
523 }
524 
526  // Get the module type for a silicon wafer
527  if (waferHexagon8()) {
529  return ((itr == hgpar_->typesInLayers_.end() ? 2 : hgpar_->waferTypeL_[itr->second]));
530  } else {
531  return -1;
532  }
533 }
534 
535 std::pair<double, double> HGCalDDDConstants::getXY(int layer, double x, double y, bool forwd) const {
536  int ll = layer - hgpar_->firstLayer_;
537  double x0(x), y0(y);
538  if ((!hgpar_->layerType_.empty()) && (ll < static_cast<int>(hgpar_->layerRotV_.size()))) {
539  if (forwd) {
540  x0 = x * hgpar_->layerRotV_[ll].first + y * hgpar_->layerRotV_[ll].second;
541  y0 = y * hgpar_->layerRotV_[ll].first - x * hgpar_->layerRotV_[ll].second;
542  } else {
543  x0 = x * hgpar_->layerRotV_[ll].first - y * hgpar_->layerRotV_[ll].second;
544  y0 = y * hgpar_->layerRotV_[ll].first + x * hgpar_->layerRotV_[ll].second;
545  }
546  }
547 #ifdef EDM_ML_DEBUG
548  double x1(x0), y1(y0);
549  if (ll < static_cast<int>(hgpar_->layerRotV_.size())) {
550  if (forwd) {
551  x1 = x0 * hgpar_->layerRotV_[ll].first - y0 * hgpar_->layerRotV_[ll].second;
552  y1 = y0 * hgpar_->layerRotV_[ll].first + x0 * hgpar_->layerRotV_[ll].second;
553  } else {
554  x1 = x0 * hgpar_->layerRotV_[ll].first + y0 * hgpar_->layerRotV_[ll].second;
555  y1 = y0 * hgpar_->layerRotV_[ll].first - x0 * hgpar_->layerRotV_[ll].second;
556  }
557  }
558  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants: Layer " << layer << ":" << ll << " mode " << forwd << " x " << x
559  << ":" << x0 << ":" << x1 << " y " << y << ":" << y0 << ":" << y1;
560 #endif
561  return std::make_pair(x0, y0);
562 }
563 
566 }
567 
568 bool HGCalDDDConstants::isHalfCell(int waferType, int cell) const {
569  if (waferType < 1 || cell < 0)
570  return false;
572  ? hgpar_->cellCoarseHalf_[cell]
573  : hgpar_->cellFineHalf_[cell];
574 }
575 
576 bool HGCalDDDConstants::isValidHex(int lay, int mod, int cell, bool reco) const {
577  // Check validity for a layer|wafer|cell of pre-TDR version
578  bool result(false), resultMod(false);
579  int cellmax(0);
580  if (waferHexagon6()) {
581  int32_t copyNumber = hgpar_->waferCopy_[mod];
582  result = ((lay > 0 && lay <= static_cast<int>(layers(reco))));
583  if (result) {
584  const int32_t lay_idx = reco ? (lay - 1) * 3 + 1 : lay;
585  const auto& the_modules = hgpar_->copiesInLayers_[lay_idx];
586  auto moditr = the_modules.find(copyNumber);
587  result = resultMod = (moditr != the_modules.end());
588 #ifdef EDM_ML_DEBUG
589  if (!result)
590  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants: Layer " << lay << ":" << lay_idx << " Copy " << copyNumber
591  << ":" << mod << " Flag " << result;
592 #endif
593  if (result) {
594  if (moditr->second >= 0) {
595  if (mod >= static_cast<int>(hgpar_->waferTypeT_.size()))
596  edm::LogWarning("HGCalGeom") << "Module no. out of bound for " << mod << " to be compared with "
597  << (hgpar_->waferTypeT_).size() << " ***** ERROR *****";
598  cellmax = (((hgpar_->waferTypeT_[mod] - 1) == HGCSiliconDetId::HGCalHD120) ||
600  ? static_cast<int>(hgpar_->cellFineX_.size())
601  : static_cast<int>(hgpar_->cellCoarseX_.size());
602  result = (cell >= 0 && cell <= cellmax);
603  } else {
604  result = isValidCell(lay_idx, mod, cell);
605  }
606  }
607  }
608  }
609 
610 #ifdef EDM_ML_DEBUG
611  if (!result)
612  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants: Layer " << lay << ":"
613  << (lay > 0 && (lay <= static_cast<int>(layers(reco)))) << " Module " << mod << ":"
614  << resultMod << " Cell " << cell << ":" << cellmax << ":"
615  << (cell >= 0 && cell <= cellmax) << ":" << maxCells(reco);
616 #endif
617  return result;
618 }
619 
620 bool HGCalDDDConstants::isValidHex8(int layer, int modU, int modV, bool fullAndPart) const {
621  // Check validity for a layer|wafer|cell of post-TDR version
622  int indx = HGCalWaferIndex::waferIndex(layer, modU, modV);
623  auto itr = hgpar_->typesInLayers_.find(indx);
624 #ifdef EDM_ML_DEBUG
625  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:WaferType " << layer << ":" << modU << ":" << modV
626  << ":" << indx << " Test " << (itr != hgpar_->typesInLayers_.end());
627 #endif
628  if (itr == hgpar_->typesInLayers_.end()) {
629 #ifdef EDM_ML_DEBUG
630  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:: Cannot find " << layer << ":" << modU << ":" << modV
631  << " in wadferIndex";
632 #endif
633  return false;
634  }
635 
636  if (fullAndPart_) {
637  auto ktr = hgpar_->waferInfoMap_.find(indx);
638 #ifdef EDM_ML_DEBUG
639  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:WaferInfoMap " << layer << ":" << modU << ":"
640  << modV << ":" << indx << " Test " << (ktr != hgpar_->waferInfoMap_.end());
641 #endif
642  if (ktr == hgpar_->waferInfoMap_.end()) {
643 #ifdef EDM_ML_DEBUG
644  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:: Cannot find " << layer << ":" << modU << ":" << modV
645  << " in wadferInfoMap";
646 #endif
647  return false;
648  }
649  } else {
650  auto jtr = waferIn_.find(indx);
651 #ifdef EDM_ML_DEBUG
652  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:WaferIn " << jtr->first << ":" << jtr->second;
653 #endif
654  if (!(jtr->second)) {
655 #ifdef EDM_ML_DEBUG
656  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:: Cannot find " << layer << ":" << modU << ":" << modV
657  << " in wadferIn";
658 #endif
659  return false;
660  }
661  }
662 
663  if (fullAndPart || fullAndPart_) {
664  auto ktr = hgpar_->waferTypes_.find(indx);
665  if (ktr != hgpar_->waferTypes_.end()) {
666  if (hgpar_->waferMaskMode_ > 0) {
667  if (ktr->second.first == HGCalTypes::WaferOut) {
668 #ifdef EDM_ML_DEBUG
669  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:: Cannot find " << layer << ":" << modU << ":" << modV
670  << " due to WaferOut";
671 #endif
672  return false;
673  }
674  } else {
675  if (ktr->second.first < HGCalTypes::WaferCornerMin) {
676 #ifdef EDM_ML_DEBUG
677  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:: Cannot find " << layer << ":" << modU << ":" << modV
678  << " due to WaferCornerMin";
679 #endif
680  return false;
681  }
682  }
683  }
684  }
685  return true;
686 }
687 
688 bool HGCalDDDConstants::isValidHex8(int layer, int modU, int modV, int cellU, int cellV, bool fullAndPart) const {
689  // First check validity for a layer|wafer| of post TDR version
690 #ifdef EDM_ML_DEBUG
691  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:: layer|wafer " << layer << ":" << modU << ":" << modV << ":"
692  << fullAndPart << " Valid " << isValidHex8(layer, modU, modV, fullAndPart);
693 #endif
694  if (!isValidHex8(layer, modU, modV, fullAndPart))
695  return false;
696  int indx = HGCalWaferIndex::waferIndex(layer, modU, modV);
697  auto itr = hgpar_->typesInLayers_.find(indx);
698  int type = hgpar_->waferTypeL_[itr->second];
699  int N = (((hgpar_->waferTypeL_[itr->second] == HGCalTypes::WaferHD120) ||
700  (hgpar_->waferTypeL_[itr->second] == HGCalTypes::WaferHD200))
702  : hgpar_->nCellsCoarse_);
703 #ifdef EDM_ML_DEBUG
704  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:Cell " << cellU << ":" << cellV << ":" << N
705  << " Tests " << (cellU >= 0) << ":" << (cellU < 2 * N) << ":" << (cellV >= 0) << ":"
706  << (cellV < 2 * N) << ":" << ((cellV - cellU) < N) << ":" << ((cellU - cellV) <= N);
707 #endif
708  if ((cellU < 0) || (cellU >= 2 * N) || (cellV < 0) || (cellV >= 2 * N)) {
709 #ifdef EDM_ML_DEBUG
710  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:: Cannot statisfy Cell 1 condition " << cellU << ":" << cellV
711  << ":" << N;
712 #endif
713  return false;
714  }
715  if (((cellV - cellU) >= N) || ((cellU - cellV) > N)) {
716 #ifdef EDM_ML_DEBUG
717  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:: Cannot statisfy Cell 2 condition " << cellU << ":" << cellV
718  << ":" << N;
719 #endif
720  return false;
721  }
722  return isValidCell8(layer, modU, modV, cellU, cellV, type);
723 }
724 
725 bool HGCalDDDConstants::isValidTrap(int zside, int layer, int irad, int iphi) const {
726  // Check validity for a layer|eta|phi of scintillator
727  const auto& indx = getIndex(layer, true);
728  if (indx.first < 0)
729  return false;
730  bool ok = ((irad >= hgpar_->iradMinBH_[indx.first]) && (irad <= (hgpar_->iradMaxBH_[indx.first] + 1)) && (iphi > 0) &&
731  (iphi <= hgpar_->scintCells(layer)));
732  bool valid = ((ok && trapezoidFile()) ? tileExist(zside, layer, irad, iphi) : ok);
733 #ifdef EDM_ML_DEBUG
734  bool tileEx = trapezoidFile() ? tileExist(zside, layer, irad, iphi) : true;
735  edm::LogVerbatim("HGCalGeomT") << "HGCalDDDConstants::isValidityTrap: Input " << zside << ":" << layer << ":" << irad
736  << ":" << iphi << " Range on Ring " << hgpar_->iradMinBH_[indx.first] << ":"
737  << (hgpar_->iradMaxBH_[indx.first] + 1)
738  << " Range on phi 0:" << hgpar_->scintCells(layer) << " tileExist " << tileEx
739  << " Valid " << ok << ":" << valid;
740 #endif
741  return valid;
742 }
743 
745  return (hgpar_->firstLayer_ + tot_layers_[static_cast<int>(reco)] - 1);
746 }
747 
748 unsigned int HGCalDDDConstants::layers(bool reco) const { return tot_layers_[static_cast<int>(reco)]; }
749 
750 int HGCalDDDConstants::layerIndex(int lay, bool reco) const {
751  int ll = lay - hgpar_->firstLayer_;
752  if (ll < 0 || ll >= static_cast<int>(hgpar_->layerIndex_.size()))
753  return -1;
754  if (waferHexagon6()) {
755  if (reco && ll >= static_cast<int>(hgpar_->depthIndex_.size()))
756  return -1;
757  return (reco ? hgpar_->depthLayerF_[ll] : hgpar_->layerIndex_[ll]);
758  } else {
759  return (hgpar_->layerIndex_[ll]);
760  }
761 }
762 
763 unsigned int HGCalDDDConstants::layersInit(bool reco) const {
764  return (reco ? hgpar_->depthIndex_.size() : hgpar_->layerIndex_.size());
765 }
766 
767 std::pair<float, float> HGCalDDDConstants::localToGlobal8(
768  int zside, int lay, int waferU, int waferV, double localX, double localY, bool reco, bool debug) const {
769  double x(localX), y(localY);
770  bool rotx =
772  if (debug)
773  edm::LogVerbatim("HGCalGeom") << "LocalToGlobal8 " << lay << ":" << (lay - hgpar_->firstLayer_) << ":" << rotx
774  << " Local (" << x << ":" << y << ") Reco " << reco;
775  if (!reco) {
778  }
779  const auto& xy = waferPositionNoRot(lay, waferU, waferV, reco, debug);
780  x += xy.first;
781  y += xy.second;
782  int indx = HGCalWaferIndex::waferIndex(lay, waferU, waferV);
783  auto ktr = hgpar_->waferInfoMap_.find(indx);
784  if (cassetteMode() && (ktr != hgpar_->waferInfoMap_.end())) {
785  auto cshift = hgcassette_.getShift(lay, -1, (ktr->second).cassette);
786  std::ostringstream st1;
787  if (debug)
788  st1 << "Cassette " << (ktr->second).cassette << " Shift " << cshift.first << ":" << cshift.second << " Original "
789  << x << ":" << y;
790  if (!reco) {
791  x -= ((HGCalParameters::k_ScaleToDDD)*zside * cshift.first);
792  y += ((HGCalParameters::k_ScaleToDDD)*cshift.second);
793  } else {
794  x -= (zside * cshift.first);
795  y += cshift.second;
796  }
797  if (debug) {
798  st1 << " Final " << x << ":" << y;
799  edm::LogVerbatim("HGCalGeom") << st1.str();
800  }
801  }
802  if (debug)
803  edm::LogVerbatim("HGCalGeom") << "With wafer " << x << ":" << y << " by adding " << xy.first << ":" << xy.second;
804  return (rotx ? getXY(lay, x, y, false) : std::make_pair(x, y));
805 }
806 
807 std::pair<float, float> HGCalDDDConstants::locateCell(int cell, int lay, int type, bool reco) const {
808  // type refers to wafer # for hexagon cell
809  float x(999999.), y(999999.);
810  const auto& index = getIndex(lay, reco);
811  int i = index.first;
812  if (i < 0)
813  return std::make_pair(x, y);
814  if (waferHexagon6()) {
815  x = hgpar_->waferPosX_[type];
816  y = hgpar_->waferPosY_[type];
817 #ifdef EDM_ML_DEBUG
818  float x0(x), y0(y);
819 #endif
822  x += hgpar_->cellFineX_[cell];
823  y += hgpar_->cellFineY_[cell];
824  } else {
825  x += hgpar_->cellCoarseX_[cell];
826  y += hgpar_->cellCoarseY_[cell];
827  }
828 #ifdef EDM_ML_DEBUG
829  edm::LogVerbatim("HGCalGeom") << "LocateCell (Wafer) " << x0 << ":" << y0 << " Final " << x << ":" << y;
830 #endif
831  if (!reco) {
834  }
835  }
836  return std::make_pair(x, y);
837 }
838 
839 std::pair<float, float> HGCalDDDConstants::locateCell(int zside,
840  int lay,
841  int waferU,
842  int waferV,
843  int cellU,
844  int cellV,
845  bool reco,
846  bool all,
847  bool norot,
848  bool cog,
849  bool debug) const {
850  double x(0), y(0);
851  int indx = HGCalWaferIndex::waferIndex(lay, waferU, waferV);
852  auto itr = hgpar_->typesInLayers_.find(indx);
853  int type = ((itr == hgpar_->typesInLayers_.end()) ? 2 : hgpar_->waferTypeL_[itr->second]);
854  int layertype = layerType(lay);
855  bool rotx = (norot) ? false : (layertype == HGCalTypes::WaferCenterR);
856  if (debug) {
857  edm::LogVerbatim("HGCalGeom") << "LocateCell " << lay << ":" << (lay - hgpar_->firstLayer_) << ":" << layertype
858  << ":" << rotx << ":" << waferU << ":" << waferV << ":" << indx << ":"
859  << (itr == hgpar_->typesInLayers_.end()) << ":" << type << " Flags " << reco << ":"
860  << all;
861  }
862  auto ktr = hgpar_->waferInfoMap_.end();
863  int place(HGCalCell::cellPlacementOld);
864  if (waferHexagon8File()) {
865  if (cassetteMode()) {
866  ktr = hgpar_->waferInfoMap_.find(indx);
867  if (ktr != hgpar_->waferInfoMap_.end())
868  place = HGCalCell::cellPlacementIndex(1, HGCalTypes::layerFrontBack(layertype), (ktr->second).orient);
869  }
870  int part = partialWaferType(lay, waferU, waferV);
871  auto xy = (waferHexagon8Fine() || cog) ? cellOffset_->cellOffsetUV2XY1(cellU, cellV, place, type, part)
872  : hgcell_->cellUV2XY2(cellU, cellV, place, type);
873  x = xy.first;
874  y = xy.second;
875  if (debug)
876  edm::LogVerbatim("HGCalGeom") << "Type " << type << " Place " << place << " Cell " << cellU << ":" << cellV
877  << " Position " << x << ":" << y;
878  } else {
879  int kndx = cellV * 100 + cellU;
881  auto jtr = hgpar_->cellFineIndex_.find(kndx);
882  if (jtr != hgpar_->cellFineIndex_.end()) {
883  x = hgpar_->cellFineX_[jtr->second];
884  y = hgpar_->cellFineY_[jtr->second];
885  }
886  if (debug)
887  edm::LogVerbatim("HGCalGeom") << "Fine " << cellU << ":" << cellV << ":" << kndx << ":" << x << ":" << y << ":"
888  << (jtr != hgpar_->cellFineIndex_.end());
889  } else {
890  auto jtr = hgpar_->cellCoarseIndex_.find(kndx);
891  if (jtr != hgpar_->cellCoarseIndex_.end()) {
892  x = hgpar_->cellCoarseX_[jtr->second];
893  y = hgpar_->cellCoarseY_[jtr->second];
894  }
895  if (debug)
896  edm::LogVerbatim("HGCalGeom") << "Coarse " << cellU << ":" << cellV << ":" << kndx << ":" << x << ":" << y
897  << ":" << (jtr != hgpar_->cellCoarseIndex_.end());
898  }
899  }
900  if (!reco) {
903  }
904  if (all) {
905  const auto& xy = waferPositionNoRot(lay, waferU, waferV, reco, debug);
906  x += xy.first;
907  y += xy.second;
908  if (cassetteMode() && (ktr != hgpar_->waferInfoMap_.end())) {
909  auto cshift = hgcassette_.getShift(lay, -1, (ktr->second).cassette);
910  std::ostringstream st1;
911  if (debug)
912  st1 << "Cassette " << (ktr->second).cassette << " Shift " << cshift.first << ":" << cshift.second
913  << " Original " << x << ":" << y;
914  if (!reco) {
915  x -= ((HGCalParameters::k_ScaleToDDD)*cshift.first);
916  y += ((HGCalParameters::k_ScaleToDDD)*cshift.second);
917  } else {
918  x -= cshift.first;
919  y += cshift.second;
920  }
921  if (debug) {
922  st1 << " Final " << x << ":" << y;
923  edm::LogVerbatim("HGCalGeom") << st1.str();
924  }
925  }
926  if (debug)
927  edm::LogVerbatim("HGCalGeom") << "With wafer " << x << ":" << y << " by adding " << xy.first << ":" << xy.second;
928  }
929  return (rotx ? getXY(lay, x, y, false) : std::make_pair(x, y));
930 }
931 
932 std::pair<float, float> HGCalDDDConstants::locateCell(const HGCSiliconDetId& id, bool cog, bool debug) const {
933  return locateCell(
934  id.zside(), id.layer(), id.waferU(), id.waferV(), id.cellU(), id.cellV(), true, true, false, cog, debug);
935 }
936 
937 std::pair<float, float> HGCalDDDConstants::locateCell(const HGCScintillatorDetId& id, bool debug) const {
938  return locateCellTrap(id.zside(), id.layer(), id.iradius(), id.iphi(), true, debug);
939 }
940 
941 std::pair<float, float> HGCalDDDConstants::locateCellHex(int cell, int wafer, bool reco) const {
942  float x(0), y(0);
943  if ((hgpar_->waferTypeT_[wafer] - 1 == HGCSiliconDetId::HGCalHD120) ||
945  x = hgpar_->cellFineX_[cell];
946  y = hgpar_->cellFineY_[cell];
947  } else {
948  x = hgpar_->cellCoarseX_[cell];
949  y = hgpar_->cellCoarseY_[cell];
950  }
951  if (!reco) {
954  }
955  return std::make_pair(x, y);
956 }
957 
958 std::pair<float, float> HGCalDDDConstants::locateCellTrap(
959  int zside, int lay, int irad, int iphi, bool reco, bool debug) const {
960  float x(0), y(0);
961  const auto& indx = getIndex(lay, reco);
962  if (indx.first >= 0) {
963  int ir = std::abs(irad);
964  int type = hgpar_->scintType(lay);
965  double phi = (iphi - 0.5) * indx.second;
966  double z = hgpar_->zLayerHex_[indx.first];
967  double r = 0.5 * (hgpar_->radiusLayer_[type][ir - 1] + hgpar_->radiusLayer_[type][ir]);
968  std::pair<double, double> range = rangeR(z, true);
969  if (debug)
970  edm::LogVerbatim("HGCalGeom") << "locateCellTrap:: Input " << lay << ":" << irad << ":" << iphi << ":" << reco
971  << " IR " << ir << ":" << hgpar_->iradMinBH_[indx.first] << ":"
972  << hgpar_->iradMaxBH_[indx.first] << " Type " << type << " Z " << indx.first << ":"
973  << z << " phi " << phi << ":" << convertRadToDeg(phi) << " R " << r << ":"
974  << range.first << ":" << range.second;
975  if (!trapezoidFile())
976  r = std::max(range.first, std::min(r, range.second));
977  x = r * std::cos(phi);
978  y = r * std::sin(phi);
979  int ll = lay - hgpar_->firstLayer_;
980  x += hgpar_->xLayerHex_[ll];
981  y += hgpar_->yLayerHex_[ll];
982  if (irad < 0)
983  x = -x;
984  if (cassetteMode()) {
986  auto cshift = hgcassette_.getShift(lay, -1, cassette);
987  std::ostringstream st1;
988  if (debug)
989  st1 << "Cassette " << cassette << " Shift " << cshift.first << ":" << cshift.second << " Original " << x << ":"
990  << y;
991  x -= cshift.first;
992  y += cshift.second;
993  if (debug) {
994  st1 << " Final " << x << ":" << y;
995  edm::LogVerbatim("HGCalGeom") << st1.str();
996  }
997  }
998  }
999  if (!reco) {
1002  }
1003  return std::make_pair(x, y);
1004 }
1005 
1006 bool HGCalDDDConstants::maskCell(const DetId& detId, int corners) const {
1007  bool mask(false);
1008  if (corners > 2 && corners <= static_cast<int>(HGCalParameters::k_CornerSize)) {
1009  if (waferHexagon8()) {
1010  int N(0), layer(0), waferU(0), waferV(0), u(0), v(0);
1011  if (detId.det() == DetId::Forward) {
1012  HFNoseDetId id(detId);
1013  N = getUVMax(id.type());
1014  layer = id.layer();
1015  waferU = id.waferU();
1016  waferV = id.waferV();
1017  u = id.cellU();
1018  v = id.cellV();
1019  } else {
1021  N = getUVMax(id.type());
1022  layer = id.layer();
1023  waferU = id.waferU();
1024  waferV = id.waferV();
1025  u = id.cellU();
1026  v = id.cellV();
1027  }
1029  auto itr = hgpar_->waferTypes_.find(wl);
1030  auto ktr = hgpar_->waferInfoMap_.find(wl);
1031 #ifdef EDM_ML_DEBUG
1032  edm::LogVerbatim("HGCalGeom") << "MaskCell: Layer " << layer << " Wafer " << waferU << ":" << waferV << " Index "
1033  << wl << ":" << (itr != hgpar_->waferTypes_.end()) << ":"
1034  << (ktr != hgpar_->waferInfoMap_.end());
1035 #endif
1036  if (cassetteMode()) {
1037  int part = (ktr != hgpar_->waferInfoMap_.end()) ? (ktr->second).part : HGCalTypes::WaferFull;
1038  mask = !(HGCalWaferMask::goodCell(u, v, part));
1039  } else if (itr != hgpar_->waferTypes_.end()) {
1040  if ((itr->second).second <= HGCalTypes::k_OffsetRotation)
1041  mask = HGCalWaferMask::maskCell(u, v, N, (itr->second).first, (itr->second).second, corners);
1042  else
1044  u, v, N, (itr->second).first, ((itr->second).second - HGCalTypes::k_OffsetRotation)));
1045  }
1046  }
1047  }
1048  return mask;
1049 }
1050 
1052  int cells(0);
1053  for (unsigned int i = 0; i < layers(reco); ++i) {
1054  int lay = reco ? hgpar_->depth_[i] : hgpar_->layer_[i];
1055  if (cells < maxCells(lay, reco))
1056  cells = maxCells(lay, reco);
1057  }
1058  return cells;
1059 }
1060 
1061 int HGCalDDDConstants::maxCells(int lay, bool reco) const {
1062  const auto& index = getIndex(lay, reco);
1063  if (index.first < 0)
1064  return 0;
1065  if (waferHexagon6()) {
1066  unsigned int cells(0);
1067  for (unsigned int k = 0; k < hgpar_->waferTypeT_.size(); ++k) {
1068  if (waferInLayerTest(k, index.first, hgpar_->defineFull_)) {
1069  unsigned int cell = (((hgpar_->waferTypeT_[k] - 1) == HGCSiliconDetId::HGCalHD120) ||
1071  ? (hgpar_->cellFineX_.size())
1072  : (hgpar_->cellCoarseX_.size());
1073  if (cell > cells)
1074  cells = cell;
1075  }
1076  }
1077  return static_cast<int>(cells);
1078  } else if (waferHexagon8()) {
1079  int cells(0);
1080  for (unsigned int k = 0; k < hgpar_->waferCopy_.size(); ++k) {
1081  if (waferInLayerTest(k, index.first, hgpar_->defineFull_)) {
1084  int type =
1085  ((itr == hgpar_->typesInLayers_.end()) ? HGCSiliconDetId::HGCalLD300 : hgpar_->waferTypeL_[itr->second]);
1087  ? hgpar_->nCellsFine_
1088  : hgpar_->nCellsCoarse_;
1089  cells = std::max(cells, 3 * N * N);
1090  }
1091  }
1092  return cells;
1093  } else if (tileTrapezoid()) {
1094  return hgpar_->scintCells(index.first + hgpar_->firstLayer_);
1095  } else {
1096  return 0;
1097  }
1098 }
1099 
1100 int HGCalDDDConstants::maxRows(int lay, bool reco) const {
1101  int kymax(0);
1102  const auto& index = getIndex(lay, reco);
1103  int i = index.first;
1104  if (i < 0)
1105  return kymax;
1106  if (waferHexagon6()) {
1107  for (unsigned int k = 0; k < hgpar_->waferCopy_.size(); ++k) {
1109  int ky = ((hgpar_->waferCopy_[k]) / 100) % 100;
1110  if (ky > kymax)
1111  kymax = ky;
1112  }
1113  }
1114  } else if (waferHexagon8()) {
1115  kymax = 1 + 2 * hgpar_->waferUVMaxLayer_[index.first];
1116  }
1117  return kymax;
1118 }
1119 
1120 int HGCalDDDConstants::modifyUV(int uv, int type1, int type2) const {
1121  // Modify u/v for transition of type1 to type2
1122  int uvx(uv);
1123  if (type1 != type2) {
1124  if ((type1 == HGCSiliconDetId::HGCalHD120) || (type1 == HGCSiliconDetId::HGCalHD200)) {
1125  if ((type2 == HGCSiliconDetId::HGCalLD200) || (type2 == HGCSiliconDetId::HGCalLD300))
1126  uvx = (2 * uv + 1) / 3;
1127  } else {
1128  if ((type2 == HGCSiliconDetId::HGCalHD120) || (type2 == HGCSiliconDetId::HGCalHD200))
1129  uvx = (3 * uv) / 2;
1130  }
1131  }
1132  return uvx;
1133 }
1134 
1135 int HGCalDDDConstants::modules(int lay, bool reco) const {
1136  if (getIndex(lay, reco).first < 0)
1137  return 0;
1138  else
1139  return max_modules_layer_[static_cast<int>(reco)][lay];
1140 }
1141 
1142 int HGCalDDDConstants::modulesInit(int lay, bool reco) const {
1143  int nmod(0);
1144  const auto& index = getIndex(lay, reco);
1145  if (index.first < 0)
1146  return nmod;
1147  if (!tileTrapezoid()) {
1148  for (unsigned int k = 0; k < hgpar_->waferPosX_.size(); ++k) {
1149  if (waferInLayerTest(k, index.first, hgpar_->defineFull_))
1150  ++nmod;
1151  }
1152  } else {
1153  nmod = 1 + hgpar_->lastModule_[index.first] - hgpar_->firstModule_[index.first];
1154  }
1155  return nmod;
1156 }
1157 
1160 }
1161 
1165  ? tileCount(0, -1)
1166  : 0;
1167  if (cells == 0) {
1168  unsigned int nlayer = (reco) ? hgpar_->depth_.size() : hgpar_->layer_.size();
1169  for (unsigned k = 0; k < nlayer; ++k) {
1170  std::vector<int> ncells = numberCells(((reco) ? hgpar_->depth_[k] : hgpar_->layer_[k]), reco);
1171  cells = std::accumulate(ncells.begin(), ncells.end(), cells);
1172  }
1173  }
1174  return cells;
1175 }
1176 
1177 std::vector<int> HGCalDDDConstants::numberCells(int lay, bool reco) const {
1178  const auto& index = getIndex(lay, reco);
1179  int i = index.first;
1180  std::vector<int> ncell;
1181  if (i >= 0) {
1182  if (waferHexagon6()) {
1183  for (unsigned int k = 0; k < hgpar_->waferTypeT_.size(); ++k) {
1185  unsigned int cell = (((hgpar_->waferTypeT_[k] - 1) == HGCSiliconDetId::HGCalHD120) ||
1187  ? (hgpar_->cellFineX_.size())
1188  : (hgpar_->cellCoarseX_.size());
1189  ncell.emplace_back(static_cast<int>(cell));
1190  }
1191  }
1192  } else if (tileTrapezoid()) {
1193  int nphi = hgpar_->scintCells(lay);
1194  for (int k = hgpar_->firstModule_[i]; k <= hgpar_->lastModule_[i]; ++k)
1195  ncell.emplace_back(nphi);
1196  } else {
1197  for (unsigned int k = 0; k < hgpar_->waferCopy_.size(); ++k) {
1198  if (waferInLayerTest(k, index.first, hgpar_->defineFull_)) {
1199  int cell = numberCellsHexagon(lay,
1202  true);
1203  ncell.emplace_back(cell);
1204  }
1205  }
1206  }
1207  }
1208  return ncell;
1209 }
1210 
1212  if (wafer >= 0 && wafer < static_cast<int>(hgpar_->waferTypeT_.size())) {
1213  if (((hgpar_->waferTypeT_[wafer] - 1) == HGCSiliconDetId::HGCalHD120) ||
1214  ((hgpar_->waferTypeT_[wafer] - 1) == HGCSiliconDetId::HGCalHD200))
1215  return static_cast<int>(hgpar_->cellFineX_.size());
1216  else
1217  return static_cast<int>(hgpar_->cellCoarseX_.size());
1218  } else {
1219  return 0;
1220  }
1221 }
1222 
1223 int HGCalDDDConstants::numberCellsHexagon(int lay, int waferU, int waferV, bool flag) const {
1225  int type = ((itr == hgpar_->typesInLayers_.end()) ? HGCSiliconDetId::HGCalLD300 : hgpar_->waferTypeL_[itr->second]);
1227  : hgpar_->nCellsCoarse_;
1228  if (flag)
1229  return (3 * N * N);
1230  else
1231  return N;
1232 }
1233 
1234 std::pair<double, double> HGCalDDDConstants::rangeR(double z, bool reco) const {
1235  double rmin(0), rmax(0), zz(0);
1236  if (hgpar_->detectorType_ > 0) {
1238  if (hgpar_->detectorType_ <= 2) {
1240  } else {
1241  rmin = HGCalGeomTools::radius(
1243  }
1244  if ((hgpar_->detectorType_ == 2) && (zz >= hgpar_->zLayerHex_[hgpar_->firstMixedLayer_ - 1])) {
1245  rmax = HGCalGeomTools::radius(
1247  } else {
1249  }
1250  }
1251  if (!reco) {
1254  }
1255 #ifdef EDM_ML_DEBUG
1256  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:rangeR: " << z << ":" << zz << " R " << rmin << ":" << rmax;
1257 #endif
1258  return std::make_pair(rmin, rmax);
1259 }
1260 
1261 std::pair<double, double> HGCalDDDConstants::rangeRLayer(int lay, bool reco) const {
1262  double rmin(0), rmax(0);
1263  const auto& index = getIndex(lay, reco);
1264  if (index.first >= 0 && index.first < static_cast<int>(hgpar_->rMinLayHex_.size())) {
1265  rmin = hgpar_->rMinLayHex_[index.first];
1266  rmax = hgpar_->rMaxLayHex_[index.first];
1267  }
1268  if (!reco) {
1271  }
1272 #ifdef EDM_ML_DEBUG
1273  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:rangeR: " << lay << ":" << index.first << " R " << rmin << ":"
1274  << rmax;
1275 #endif
1276  return std::make_pair(rmin, rmax);
1277 }
1278 
1279 std::pair<double, double> HGCalDDDConstants::rangeZ(bool reco) const {
1280  double zmin = (hgpar_->zLayerHex_[0] - hgpar_->waferThick_);
1281  double zmax = (hgpar_->zLayerHex_[hgpar_->zLayerHex_.size() - 1] + hgpar_->waferThick_);
1282 #ifdef EDM_ML_DEBUG
1283  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:rangeZ: " << zmin << ":" << zmax << ":" << hgpar_->waferThick_;
1284 #endif
1285  if (!reco) {
1288  }
1289  return std::make_pair(zmin, zmax);
1290 }
1291 
1292 std::pair<int, int> HGCalDDDConstants::rowColumnWafer(int wafer) const {
1293  int row(0), col(0);
1294  if (wafer < static_cast<int>(hgpar_->waferCopy_.size())) {
1295  int copy = hgpar_->waferCopy_[wafer];
1298  ;
1299  }
1300  return std::make_pair(row, col);
1301 }
1302 
1305 }
1306 
1307 std::pair<int, int> HGCalDDDConstants::simToReco(int cell, int lay, int mod, bool half) const {
1308  if (!waferHexagon6()) {
1309  return std::make_pair(cell, lay);
1310  } else {
1311  const auto& index = getIndex(lay, false);
1312  int i = index.first;
1313  if (i < 0) {
1314  edm::LogWarning("HGCalGeom") << "Wrong Layer # " << lay << " not in the list ***** ERROR *****";
1315  return std::make_pair(-1, -1);
1316  }
1317  if (mod >= static_cast<int>(hgpar_->waferTypeL_.size())) {
1318  edm::LogWarning("HGCalGeom") << "Invalid Wafer # " << mod << "should be < " << (hgpar_->waferTypeL_).size()
1319  << " ***** ERROR *****";
1320  return std::make_pair(-1, -1);
1321  }
1322  int depth(-1);
1323  int kx = cell;
1324  int type = hgpar_->waferTypeL_[mod];
1325  if (type == 1) {
1326  depth = hgpar_->layerGroup_[i];
1327  } else if (type == 2) {
1328  depth = hgpar_->layerGroupM_[i];
1329  } else {
1330  depth = hgpar_->layerGroupO_[i];
1331  }
1332  return std::make_pair(kx, depth);
1333  }
1334 }
1335 
1337  int laymin(layer), laymax(layer), ringmin(ring), ringmax(ring), kount(0);
1338  if (layer == 0) {
1339  laymin = hgpar_->firstLayer_;
1340  laymax = lastLayer(true);
1341  }
1342 #ifdef EDM_ML_DEBUG
1343  edm::LogVerbatim("HGCalGeom") << "tileCount: layer " << layer << " ring " << ring << " layerMin/Max " << laymin << ":"
1344  << laymax;
1345 #endif
1346  for (int lay = laymin; lay <= laymax; ++lay) {
1347  if (ring < 0) {
1348  int ll = lay - hgpar_->firstLayer_;
1349  ringmin = hgpar_->tileRingRange_[ll].first;
1350  ringmax = hgpar_->tileRingRange_[ll].second;
1351  }
1352 #ifdef EDM_ML_DEBUG
1353  edm::LogVerbatim("HGCalGeom") << "tileCount: lay " << lay << ":" << (lay - hgpar_->firstLayer_) << " rings "
1354  << ringmin << ":" << ringmax;
1355 #endif
1356  for (int rin = ringmin; rin <= ringmax; ++rin) {
1357  int indx = HGCalTileIndex::tileIndex(lay, rin + 1, 0);
1358  auto itr = hgpar_->tileInfoMap_.find(indx);
1359 #ifdef EDM_ML_DEBUG
1360  edm::LogVerbatim("HGCalGeom") << "tileCount: rin " << rin << " indx " << indx << " itr "
1361  << (itr != hgpar_->tileInfoMap_.end());
1362 #endif
1363  if (itr != hgpar_->tileInfoMap_.end()) {
1364  for (int k = 0; k < 4; ++k) {
1365  std::bitset<24> b(itr->second.hex[k]);
1366  kount += b.count();
1367  }
1368  }
1369 #ifdef EDM_ML_DEBUG
1370  edm::LogVerbatim("HGCalGeom") << "tileCount: lay|rin " << lay << ":" << rin << " kount " << kount;
1371 #endif
1372  }
1373  }
1374  return (3 * kount);
1375 }
1376 
1377 bool HGCalDDDConstants::tileExist(int zside, int layer, int ring, int phi) const {
1378  int indx = HGCalTileIndex::tileIndex(layer, ring, 0);
1379  auto itr = hgpar_->tileInfoMap_.find(indx);
1380  bool ok = (itr == hgpar_->tileInfoMap_.end()) ? false : HGCalTileIndex::tileExist(itr->second.hex, zside, phi);
1381  return ok;
1382 }
1383 
1385  int indx = HGCalTileIndex::tileIndex(layer, ring, 0);
1386  auto itr = hgpar_->tileInfoMap_.find(indx);
1387  return ((itr == hgpar_->tileInfoMap_.end()) ? HGCalParameters::tileInfo() : itr->second);
1388 }
1389 
1390 bool HGCalDDDConstants::tilePhiEdge(double phi, int layer, int iphi) const {
1391  double dif1 = std::abs(phi - hgpar_->scintCellSize(layer) * (iphi - 1));
1392  double dif2 = std::abs(phi - hgpar_->scintCellSize(layer) * iphi);
1393 #ifdef EDM_ML_DEBUG
1394  edm::LogVerbatim("HGCalGeomT") << "HGCalDDDConstants::tilePhiEdge:: input: " << phi << ":" << layer << ":" << iphi
1395  << " Differences " << dif1 << ":" << dif2;
1396 #endif
1397  return ((dif1 < tol_) || (dif2 < tol_));
1398 }
1399 
1400 bool HGCalDDDConstants::tileRingEdge(double r, int layer, int ring) const {
1401  int type = hgpar_->scintType(layer);
1402  double dif1 = std::abs(r - hgpar_->radiusLayer_[type][ring - 1]);
1403  double dif2 = std::abs(r - hgpar_->radiusLayer_[type][ring]);
1404 #ifdef EDM_ML_DEBUG
1405  edm::LogVerbatim("HGCalGeomT") << "HGCalDDDConstants::tileRingEdge:: input: " << r << ":" << layer << ":" << ring
1406  << " Differences " << dif1 << ":" << dif2;
1407 #endif
1408  return ((dif1 < tol_) || (dif2 < tol_));
1409 }
1410 std::pair<int, int> HGCalDDDConstants::tileRings(int layer) const {
1411  if (trapezoidFile()) {
1412  int ll = layer - hgpar_->firstLayer_;
1413  if (ll >= 0 && ll < static_cast<int>(hgpar_->tileRingRange_.size()))
1414  return hgpar_->tileRingRange_[ll];
1415  }
1416  return std::make_pair(0, 0);
1417 }
1418 
1419 std::pair<int, int> HGCalDDDConstants::tileType(int layer, int ring, int phi) const {
1420  int indx = HGCalTileIndex::tileIndex(layer, ring, phi);
1421  int type(-1), sipm(-1);
1422  auto itr = hgpar_->tileInfoMap_.find(indx);
1423  if (itr != hgpar_->tileInfoMap_.end()) {
1424  type = 1 + (itr->second).type;
1425  sipm = ((itr->second).sipm == HGCalTypes::SiPMLarge) ? 0 : 1;
1426  }
1427  return std::make_pair(type, sipm);
1428 }
1429 
1431  const int ncopies = hgpar_->waferCopy_.size();
1432  int wafer(ncopies);
1433  bool result(false);
1434  for (int k = 0; k < ncopies; ++k) {
1435  if (copy == hgpar_->waferCopy_[k]) {
1436  wafer = k;
1437  result = true;
1438  break;
1439  }
1440  }
1441  if (!result) {
1442  wafer = -1;
1443 #ifdef EDM_ML_DEBUG
1444  edm::LogVerbatim("HGCalGeom") << "Cannot find " << copy << " in a list of " << ncopies << " members";
1445  for (int k = 0; k < ncopies; ++k)
1446  edm::LogVerbatim("HGCalGeom") << "[" << k << "] " << hgpar_->waferCopy_[k];
1447 #endif
1448  }
1449 #ifdef EDM_ML_DEBUG
1450  edm::LogVerbatim("HGCalGeom") << "WaferFromCopy " << copy << ":" << wafer << ":" << result;
1451 #endif
1452  return wafer;
1453 }
1454 
1455 void HGCalDDDConstants::waferFromPosition(const double x, const double y, int& wafer, int& icell, int& celltyp) const {
1456  // Input x, y in Geant4 unit and transformed to CMSSW standard
1459  int size_ = static_cast<int>(hgpar_->waferCopy_.size());
1460  wafer = size_;
1461  for (int k = 0; k < size_; ++k) {
1462  double dx = std::abs(xx - hgpar_->waferPosX_[k]);
1463  double dy = std::abs(yy - hgpar_->waferPosY_[k]);
1464  if (dx <= rmax_ && dy <= hexside_) {
1465  if ((dy <= 0.5 * hexside_) || (dx * tan30deg_ <= (hexside_ - dy))) {
1466  wafer = k;
1467  celltyp = hgpar_->waferTypeT_[k];
1468  xx -= hgpar_->waferPosX_[k];
1469  yy -= hgpar_->waferPosY_[k];
1470  break;
1471  }
1472  }
1473  }
1474  if (wafer < size_) {
1475  if ((celltyp - 1 == HGCSiliconDetId::HGCalHD120) || (celltyp - 1 == HGCSiliconDetId::HGCalHD200))
1476  icell = cellHex(
1478  else
1479  icell = cellHex(xx,
1480  yy,
1483  hgpar_->cellCoarseY_);
1484  } else {
1485  wafer = -1;
1486 #ifdef EDM_ML_DEBUG
1487  edm::LogWarning("HGCalGeom") << "Cannot get wafer type corresponding to " << x << ":" << y << " " << xx << ":"
1488  << yy;
1489 #endif
1490  }
1491 #ifdef EDM_ML_DEBUG
1492  edm::LogVerbatim("HGCalGeom") << "Position " << x << ":" << y << " Wafer " << wafer << ":" << size_ << " XX " << xx
1493  << ":" << yy << " Cell " << icell << " Type " << celltyp;
1494 #endif
1495 }
1496 
1498  const double y,
1499  const int zside,
1500  const int layer,
1501  int& waferU,
1502  int& waferV,
1503  int& cellU,
1504  int& cellV,
1505  int& celltype,
1506  double& wt,
1507  bool extend,
1508  bool debug) const {
1509  // Expect x, y as in SIM step
1510  bool waferin = ((waferU == 0) && (waferV == 0));
1511  if (waferin)
1512  waferU = waferV = 1 + hgpar_->waferUVMax_;
1513  cellU = cellV = celltype = 0;
1514  if ((hgpar_->xLayerHex_.empty()) || (hgpar_->yLayerHex_.empty()))
1515  return;
1516  int ll = layer - hgpar_->firstLayer_;
1517  int layertype = layerType(layer);
1518  bool rotx = ((!hgpar_->layerType_.empty()) && (layertype == HGCalTypes::WaferCenterR));
1519  double xx(0), yy(0);
1520  if (rotx) {
1521  std::pair<double, double> xy =
1523  xx = xy.first - hgpar_->xLayerHex_[ll];
1524  yy = xy.second - hgpar_->yLayerHex_[ll];
1525  } else {
1528  }
1529  if (debug)
1530  edm::LogVerbatim("HGCalGeom") << "waferFromPosition:: Layer " << layer << ":" << ll << " Rot " << rotx << " X " << x
1531  << ":" << xx << " Y " << y << ":" << yy << " side " << zside << " extend " << extend
1532  << " initial wafer index " << waferU << ":" << waferV;
1533  ;
1534  double rmax = extend ? rmaxT_ : rmax_;
1535  double hexside = extend ? hexsideT_ : hexside_;
1536  if (waferin) {
1537  double tolmin(100.0);
1538  for (unsigned int k = 0; k < hgpar_->waferPosX_.size(); ++k) {
1539  double dx0(0), dy0(0);
1542  if (cassetteMode()) {
1544  auto ktr = hgpar_->waferInfoMap_.find(indx);
1545  if (ktr != hgpar_->waferInfoMap_.end()) {
1546  auto cshift = hgcassette_.getShift(layer, -1, (ktr->second).cassette);
1547  if (debug)
1548  edm::LogVerbatim("HGCalGeom")
1549  << "Cassette " << (ktr->second).cassette << " Shift " << cshift.first << ":" << cshift.second;
1550  dx0 = -cshift.first;
1551  dy0 = cshift.second;
1552  }
1553  }
1554  double dx = std::abs(xx - dx0 - hgpar_->waferPosX_[k]);
1555  double dy = std::abs(yy - dy0 - hgpar_->waferPosY_[k]);
1556  constexpr double tolc = 0.01;
1557  if (debug) {
1558  edm::LogVerbatim("HGCalGeom") << "Wafer " << waferU << ":" << waferV << " position " << xx << ":" << yy
1559  << " Distance " << dx << ":" << dy << " diff0 " << (dx - rmax) << ":"
1560  << (dy - hexside) << " diff1 " << (dy - 0.5 * hexside) << ":"
1561  << (dx * tan30deg_ - (hexside - dy));
1562  if ((dx - rmax) <= tolc && (dy - hexside) <= tolc) {
1563  tolmin = std::min(tolmin, (dy - 0.5 * hexside));
1564  tolmin = std::min(tolmin, (dx * tan30deg_ - (hexside - dy)));
1565  }
1566  }
1567  if ((dx - rmax) <= tolc && (dy - hexside) <= tolc) {
1568  if (((dy - 0.5 * hexside) <= tolc) || ((dx * tan30deg_ - (hexside - dy)) <= tolc)) {
1569  if (waferHexagon8File()) {
1572  if (debug)
1573  edm::LogVerbatim("HGCalGeom")
1574  << "Position (" << x << ", " << y << ") Wafer type:partial:orient:cassette " << celltype << ":"
1578  } else {
1580  celltype = ((itr == hgpar_->typesInLayers_.end()) ? HGCSiliconDetId::HGCalLD300
1581  : hgpar_->waferTypeL_[itr->second]);
1582  }
1583  if (debug)
1584  edm::LogVerbatim("HGCalGeom")
1585  << "WaferFromPosition:: Input " << layer << ":" << ll << ":" << hgpar_->firstLayer_ << ":" << rotx
1586  << ":" << x << ":" << y << ":" << hgpar_->xLayerHex_[ll] << ":" << hgpar_->yLayerHex_[ll] << ":" << xx
1587  << ":" << yy << " compared with " << hgpar_->waferPosX_[k] << ":" << hgpar_->waferPosY_[k]
1588  << " difference " << dx << ":" << dy << ":" << dx * tan30deg_ << ":" << (hexside_ - dy)
1589  << " comparator " << rmax_ << ":" << rmaxT_ << ":" << hexside_ << ":" << hexsideT_ << " wafer "
1590  << waferU << ":" << waferV << ":" << celltype;
1591  xx -= (dx0 + hgpar_->waferPosX_[k]);
1592  yy -= (dy0 + hgpar_->waferPosY_[k]);
1593  break;
1594  }
1595  }
1596  }
1597  if (debug)
1598  edm::LogVerbatim("HGCalGeom") << "Tolmin " << tolmin;
1599  } else {
1600  for (unsigned int k = 0; k < hgpar_->waferPosX_.size(); ++k) {
1601  double dx0(0), dy0(0);
1604  if (cassetteMode()) {
1606  auto ktr = hgpar_->waferInfoMap_.find(indx);
1607  if (ktr != hgpar_->waferInfoMap_.end()) {
1608  auto cshift = hgcassette_.getShift(layer, -1, (ktr->second).cassette);
1609  if (debug)
1610  edm::LogVerbatim("HGCalGeom")
1611  << "Cassette " << (ktr->second).cassette << " Shift " << cshift.first << ":" << cshift.second;
1612  dx0 = -cshift.first;
1613  dy0 = cshift.second;
1614  }
1615  }
1616  if (waferHexagon8File()) {
1619  if (debug)
1620  edm::LogVerbatim("HGCalGeom") << "Position (" << x << ", " << y << ") Wafer type:partial:orient:cassette "
1621  << celltype << ":" << HGCalWaferType::getPartial(index, hgpar_->waferInfoMap_)
1624  } else {
1626  celltype =
1627  ((itr == hgpar_->typesInLayers_.end()) ? HGCSiliconDetId::HGCalLD300 : hgpar_->waferTypeL_[itr->second]);
1628  }
1629  xx -= (dx0 + hgpar_->waferPosX_[k]);
1630  yy -= (dy0 + hgpar_->waferPosY_[k]);
1631  break;
1632  }
1633  }
1634  }
1635  if ((std::abs(waferU) <= hgpar_->waferUVMax_) && (celltype >= 0)) {
1637  if (cassetteMode()) {
1639  auto ktr = hgpar_->waferInfoMap_.find(indx);
1640  if (ktr != hgpar_->waferInfoMap_.end()) {
1641  place = HGCalCell::cellPlacementIndex(1, HGCalTypes::layerFrontBack(layertype), (ktr->second).orient);
1642  part = (ktr->second).part;
1643  if (debug)
1644  edm::LogVerbatim("HGCalGeom") << "waferFromPosition: frontback " << layertype << ":"
1645  << HGCalTypes::layerFrontBack(layertype) << " Orient " << (ktr->second).orient
1646  << " place " << place << " part " << part;
1647  }
1648  }
1649  cellHex(xx, yy, celltype, place, part, cellU, cellV, extend, debug);
1650  wt = ((((celltype == HGCSiliconDetId::HGCalHD120) || (celltype == HGCSiliconDetId::HGCalHD200)) &&
1651  (hgpar_->useSimWt_ > 0))
1652  ? (hgpar_->cellThickness_[celltype] / hgpar_->waferThick_)
1653  : 1.0);
1654  } else {
1655  cellU = cellV = 2 * hgpar_->nCellsFine_;
1656  wt = 1.0;
1657  celltype = -1;
1658  }
1659  if ((celltype < 0) && debug) {
1660  double x1(xx);
1661  double y1(yy);
1662  edm::LogVerbatim("HGCalGeom") << "waferfFromPosition: Bad type for X " << x << ":" << x1 << ":" << xx << " Y " << y
1663  << ":" << y1 << ":" << yy << " Wafer " << waferU << ":" << waferV << " Cell " << cellU
1664  << ":" << cellV;
1665  for (unsigned int k = 0; k < hgpar_->waferPosX_.size(); ++k) {
1666  double dx = std::abs(x1 - hgpar_->waferPosX_[k]);
1667  double dy = std::abs(y1 - hgpar_->waferPosY_[k]);
1668  edm::LogVerbatim("HGCalGeom") << "Wafer [" << k << "] Position (" << hgpar_->waferPosX_[k] << ", "
1669  << hgpar_->waferPosY_[k] << ") difference " << dx << ":" << dy << ":"
1670  << dx * tan30deg_ << ":" << hexside - dy << " Paramerers " << rmax << ":"
1671  << hexside;
1672  }
1673  }
1674  edm::LogVerbatim("HGCalGeomX") << "Input x:y:layer " << x << ":" << y << ":" << layer << " Wafer " << waferU << ":"
1675  << waferV << " Cell " << cellU << ":" << cellV << ":" << celltype << " wt " << wt;
1676 }
1677 
1678 bool HGCalDDDConstants::waferInLayer(int wafer, int lay, bool reco) const {
1679  const auto& indx = getIndex(lay, reco);
1680  if (indx.first < 0)
1681  return false;
1682  return waferInLayerTest(wafer, indx.first, hgpar_->defineFull_);
1683 }
1684 
1685 bool HGCalDDDConstants::waferFullInLayer(int wafer, int lay, bool reco) const {
1686  const auto& indx = getIndex(lay, reco);
1687  if (indx.first < 0)
1688  return false;
1689  return waferInLayerTest(wafer, indx.first, false);
1690 }
1691 
1693  int indx = HGCalWaferIndex::waferIndex(lay, waferU, waferV);
1694  auto itr = hgpar_->waferInfoMap_.find(indx);
1695  return ((itr == hgpar_->waferInfoMap_.end()) ? HGCalParameters::waferInfo() : itr->second);
1696 }
1697 
1698 std::pair<double, double> HGCalDDDConstants::waferParameters(bool reco) const {
1699  if (reco)
1700  return std::make_pair(rmax_, hexside_);
1701  else
1703 }
1704 
1705 std::pair<double, double> HGCalDDDConstants::waferPosition(int wafer, bool reco) const {
1706  double xx(0), yy(0);
1707  if (wafer >= 0 && wafer < static_cast<int>(hgpar_->waferPosX_.size())) {
1708  xx = hgpar_->waferPosX_[wafer];
1709  yy = hgpar_->waferPosY_[wafer];
1710  }
1711  if (!reco) {
1714  }
1715  return std::make_pair(xx, yy);
1716 }
1717 
1718 std::pair<double, double> HGCalDDDConstants::waferPosition(
1719  int lay, int waferU, int waferV, bool reco, bool debug) const {
1720  int ll = lay - hgpar_->firstLayer_;
1721  bool rotx = ((!hgpar_->layerType_.empty()) && (hgpar_->layerType_[ll] == HGCalTypes::WaferCenterR));
1722 #ifdef EDM_ML_DEBUG
1723  if (debug)
1724  edm::LogVerbatim("HGCalGeom") << "Layer " << lay << ":" << ll << " Rotation " << rotx << " U:V " << waferU << ":"
1725  << waferV;
1726 #endif
1727  auto xy = waferPositionNoRot(lay, waferU, waferV, reco, debug);
1728  std::pair<double, double> xy0 = (rotx) ? getXY(lay, xy.first, xy.second, false) : xy;
1729 #ifdef EDM_ML_DEBUG
1730  if (debug)
1731  edm::LogVerbatim("HGCalGeom") << "Without and with rotation " << xy.first << ":" << xy.second << ":" << xy0.first
1732  << ":" << xy0.second;
1733 #endif
1734  return xy0;
1735 }
1736 
1737 int HGCalDDDConstants::waferFileIndex(unsigned int kk) const {
1738  if (kk < hgpar_->waferInfoMap_.size()) {
1739  auto itr = hgpar_->waferInfoMap_.begin();
1740  std::advance(itr, kk);
1741  return itr->first;
1742  } else
1743  return 0;
1744 }
1745 
1746 std::tuple<int, int, int, int> HGCalDDDConstants::waferFileInfo(unsigned int kk) const {
1747  if (kk < hgpar_->waferInfoMap_.size()) {
1748  auto itr = hgpar_->waferInfoMap_.begin();
1749  std::advance(itr, kk);
1750  return std::make_tuple(itr->second.type, itr->second.part, itr->second.orient, itr->second.cassette);
1751  } else
1752  return std::make_tuple(0, 0, 0, 0);
1753 }
1754 
1755 std::tuple<int, int, int, int> HGCalDDDConstants::waferFileInfoFromIndex(int kk) const {
1756  auto itr = hgpar_->waferInfoMap_.find(kk);
1757  if (itr != hgpar_->waferInfoMap_.end()) {
1758  return std::make_tuple(itr->second.type, itr->second.part, itr->second.orient, itr->second.cassette);
1759  } else
1760  return std::make_tuple(0, 0, 0, 0);
1761 }
1762 
1764  HepGeom::Point3D<float>& loc, const DetId& id, bool useWafer, bool reco, bool debug) const {
1765  HGCSiliconDetId detid(id);
1766  double x(0), y(0);
1767  if (useWafer) {
1768  auto xyw = waferPositionNoRot(detid.layer(), detid.waferU(), detid.waferV(), reco, debug);
1769  x = xyw.first;
1770  y = xyw.second;
1771  }
1772  auto xy = getXY(detid.layer(), (x + loc.x()), (y + loc.y()), false);
1773  double zz = (detid.zside() < 0) ? -(loc.z() + waferZ(detid.layer(), reco)) : (loc.z() + waferZ(detid.layer(), reco));
1774  double xx = (detid.zside() < 0) ? -xy.first : xy.first;
1775  return GlobalPoint(xx, xy.second, zz);
1776 }
1777 
1779  int wafer(0);
1780  if (!tileTrapezoid()) {
1781  for (unsigned int i = 0; i < layers(true); ++i) {
1782  int lay = hgpar_->depth_[i];
1783  wafer += modules(lay, true);
1784  }
1785  } else {
1786  wafer = static_cast<int>(hgpar_->moduleLayR_.size());
1787  }
1788  return wafer;
1789 }
1790 
1791 int HGCalDDDConstants::wafers(int layer, int type) const {
1792  int wafer(0);
1793  if (!tileTrapezoid()) {
1794  auto itr = waferLayer_.find(layer);
1795  if (itr != waferLayer_.end()) {
1796  unsigned ity = (type > 0 && type <= 2) ? type : 0;
1797  wafer = (itr->second)[ity];
1798  }
1799  } else {
1800  const auto& index = getIndex(layer, true);
1801  wafer = 1 + hgpar_->lastModule_[index.first] - hgpar_->firstModule_[index.first];
1802  }
1803  return wafer;
1804 }
1805 
1806 int HGCalDDDConstants::waferType(DetId const& id, bool fromFile) const {
1807  int type(1);
1808  if (waferHexagon8()) {
1809  if (fromFile && (waferFileSize() > 0)) {
1810  int layer(0), waferU(0), waferV(0);
1811  if (id.det() != DetId::Forward) {
1812  HGCSiliconDetId hid(id);
1813  layer = hid.layer();
1814  waferU = hid.waferU();
1815  waferV = hid.waferV();
1816  } else {
1817  HFNoseDetId hid(id);
1818  layer = hid.layer();
1819  waferU = hid.waferU();
1820  waferV = hid.waferV();
1821  }
1823  if (itr != hgpar_->waferInfoMap_.end())
1824  type = (itr->second).type;
1825  } else {
1826  type = ((id.det() != DetId::Forward) ? HGCSiliconDetId(id).type() : HFNoseDetId(id).type());
1827  }
1828  } else if (waferHexagon6()) {
1829  type = waferTypeL(HGCalDetId(id).wafer()) - 1;
1830  }
1831  return type;
1832 }
1833 
1834 int HGCalDDDConstants::waferType(int layer, int waferU, int waferV, bool fromFile) const {
1836  if (waferHexagon8()) {
1837  if (fromFile && (waferFileSize() > 0)) {
1839  if (itr != hgpar_->waferInfoMap_.end())
1840  type = (itr->second).type;
1841  } else {
1843  if (itr != hgpar_->typesInLayers_.end())
1844  type = hgpar_->waferTypeL_[itr->second];
1845  }
1846  } else if (waferHexagon6()) {
1847  if ((waferU >= 0) && (waferU < static_cast<int>(hgpar_->waferTypeL_.size())))
1848  type = (hgpar_->waferTypeL_[waferU] - 1);
1849  }
1850  return type;
1851 }
1852 
1853 std::tuple<int, int, int> HGCalDDDConstants::waferType(HGCSiliconDetId const& id, bool fromFile) const {
1854  const auto& index = HGCalWaferIndex::waferIndex(id.layer(), id.waferU(), id.waferV());
1855  int type(-1), part(-1), orient(-1);
1856  if (fromFile && (waferFileSize() > 0)) {
1857  auto itr = hgpar_->waferInfoMap_.find(index);
1858  if (itr != hgpar_->waferInfoMap_.end()) {
1859  type = (itr->second).type;
1860  part = (itr->second).part;
1861  orient = (itr->second).orient;
1862  }
1863  } else {
1864  auto ktr = hgpar_->typesInLayers_.find(index);
1865  if (ktr != hgpar_->typesInLayers_.end())
1866  type = hgpar_->waferTypeL_[ktr->second];
1867  auto itr = hgpar_->waferTypes_.find(index);
1868  if (itr != hgpar_->waferTypes_.end()) {
1869  if ((itr->second).second < HGCalTypes::k_OffsetRotation) {
1870  orient = (itr->second).second;
1871  if ((itr->second).first == HGCalGeomTools::k_allCorners) {
1873  } else if ((itr->second).first == HGCalGeomTools::k_fiveCorners) {
1875  } else if ((itr->second).first == HGCalGeomTools::k_fourCorners) {
1877  } else if ((itr->second).first == HGCalGeomTools::k_threeCorners) {
1879  }
1880  } else {
1881  part = (itr->second).first;
1882  orient = ((itr->second).second - HGCalTypes::k_OffsetRotation);
1883  }
1884  } else {
1886  orient = 0;
1887  }
1888  }
1889  return std::make_tuple(type, part, orient);
1890 }
1891 
1893  int layer, int waferU, int waferV, bool fromFile, bool debug) const {
1894  int type(HGCalTypes::WaferOut), rotn(0);
1896  bool withinList(true);
1897  if (fromFile && (waferFileSize() > 0)) {
1898  auto itr = hgpar_->waferInfoMap_.find(wl);
1899  withinList = (itr != hgpar_->waferInfoMap_.end());
1900  if (withinList) {
1901  type = (itr->second).part;
1902  rotn = (itr->second).orient;
1903  }
1904  } else {
1905  auto itr = hgpar_->waferTypes_.find(wl);
1906  if (waferHexagon8()) {
1907  withinList = (itr != hgpar_->waferTypes_.end());
1908  if (withinList) {
1909  if ((itr->second).second < HGCalTypes::k_OffsetRotation) {
1910  rotn = (itr->second).second;
1911  if ((itr->second).first == HGCalGeomTools::k_allCorners) {
1913  } else if ((itr->second).first == HGCalGeomTools::k_fiveCorners) {
1915  } else if ((itr->second).first == HGCalGeomTools::k_fourCorners) {
1917  } else if ((itr->second).first == HGCalGeomTools::k_threeCorners) {
1919  }
1920  } else {
1921  type = (itr->second).first;
1922  rotn = ((itr->second).second - HGCalTypes::k_OffsetRotation);
1923  }
1924  } else {
1926  rotn = HGCalTypes::WaferCorner0;
1927  }
1928  }
1929  }
1930 #ifdef EDM_ML_DEBUG
1931  if (debug)
1932  edm::LogVerbatim("HGCalGeom") << "waferTypeRotation: Layer " << layer << " Wafer " << waferU << ":" << waferV
1933  << " Index " << std::hex << wl << std::dec << ":" << withinList << " Type " << type
1934  << " Rotation " << rotn;
1935 #endif
1936  return std::make_pair(type, rotn);
1937 }
1938 
1940  bool type(false);
1941  if (waferHexagon8()) {
1943  type = (hgpar_->waferTypes_.find(wl) != hgpar_->waferTypes_.end());
1944  } else if (waferHexagon6()) {
1945  int wl = HGCalWaferIndex::waferIndex(layer, waferU, 0, true);
1946  type = (hgpar_->waferTypes_.find(wl) != hgpar_->waferTypes_.end());
1947  }
1948  return type;
1949 }
1950 
1951 double HGCalDDDConstants::waferZ(int lay, bool reco) const {
1952  const auto& index = getIndex(lay, reco);
1953  if (index.first < 0)
1954  return 0;
1955  else
1957 }
1958 
1960  double xx, double yy, const double& cellR, const std::vector<double>& posX, const std::vector<double>& posY) const {
1961  int num(0);
1962  const double tol(0.00001);
1963  double cellY = 2.0 * cellR * tan30deg_;
1964  for (unsigned int k = 0; k < posX.size(); ++k) {
1965  double dx = std::abs(xx - posX[k]);
1966  double dy = std::abs(yy - posY[k]);
1967  if (dx <= (cellR + tol) && dy <= (cellY + tol)) {
1968  double xmax = (dy <= 0.5 * cellY) ? cellR : (cellR - (dy - 0.5 * cellY) / tan30deg_);
1969  if (dx <= (xmax + tol)) {
1970  num = k;
1971  break;
1972  }
1973  }
1974  }
1975  return num;
1976 }
1977 
1979  double xloc, double yloc, int cellType, int place, int part, int& cellU, int& cellV, bool extend, bool debug) const {
1980  if (cassetteMode()) {
1981  auto uv = (part == HGCalTypes::WaferFull)
1982  ? hgcellUV_->cellUVFromXY3(xloc, yloc, place, cellType, true, debug)
1983  : hgcellUV_->cellUVFromXY1(xloc, yloc, place, cellType, part, true, debug);
1984  cellU = uv.first;
1985  cellV = uv.second;
1986  } else if (waferHexagon8File()) {
1987  auto uv = hgcellUV_->cellUVFromXY3(xloc, yloc, place, cellType, extend, debug);
1988  cellU = uv.first;
1989  cellV = uv.second;
1990  } else {
1992  ? hgpar_->nCellsFine_
1993  : hgpar_->nCellsCoarse_;
1994  double delY = 2 * rmax_ / (3 * ncell);
1995  double delX = 0.5 * delY * sqrt3_;
1996  double delYT = (extend) ? (2 * rmaxT_ / (3 * ncell)) : delY;
1997  double delXT = 0.5 * delYT * sqrt3_;
1998  double v0 = ((xloc / delY - 1.0) / 1.5);
1999  int cv0 = (v0 > 0) ? (ncell + static_cast<int>(v0 + 0.5)) : (ncell - static_cast<int>(-v0 + 0.5));
2000  double u0 = (0.5 * yloc / delX + 0.5 * cv0);
2001  int cu0 = (u0 > 0) ? (ncell / 2 + static_cast<int>(u0 + 0.5)) : (ncell / 2 - static_cast<int>(-u0 + 0.5));
2002  cu0 = std::max(0, std::min(cu0, 2 * ncell - 1));
2003  cv0 = std::max(0, std::min(cv0, 2 * ncell - 1));
2004  if (cv0 - cu0 >= ncell)
2005  cv0 = cu0 + ncell - 1;
2006  if (debug)
2007  edm::LogVerbatim("HGCalGeom") << "cellHex: input " << xloc << ":" << yloc << ":" << cellType << " parameter "
2008  << delX << ":" << delY << " u0 " << u0 << ":" << cu0 << " v0 " << v0 << ":" << cv0;
2009  bool found(false);
2010  static constexpr int shift[3] = {0, 1, -1};
2011  for (int i1 = 0; i1 < 3; ++i1) {
2012  cellU = cu0 + shift[i1];
2013  for (int i2 = 0; i2 < 3; ++i2) {
2014  cellV = cv0 + shift[i2];
2015  if (((cellV - cellU) < ncell) && ((cellU - cellV) <= ncell) && (cellU >= 0) && (cellV >= 0) &&
2016  (cellU < 2 * ncell) && (cellV < 2 * ncell)) {
2017  double xc = (1.5 * (cellV - ncell) + 1.0) * delY;
2018  double yc = (2 * cellU - cellV - ncell) * delX;
2019  if ((std::abs(yloc - yc) <= delXT) && (std::abs(xloc - xc) <= delYT) &&
2020  ((std::abs(xloc - xc) <= 0.5 * delYT) ||
2021  (std::abs(yloc - yc) <= sqrt3_ * (delYT - std::abs(xloc - xc))))) {
2022  if (debug)
2023  edm::LogVerbatim("HGCalGeom")
2024  << "cellHex: local " << xc << ":" << yc << " difference " << std::abs(xloc - xc) << ":"
2025  << std::abs(yloc - yc) << ":" << sqrt3_ * (delY - std::abs(yloc - yc)) << " comparator " << delX
2026  << ":" << delY << " (u,v) = (" << cellU << "," << cellV << ")";
2027  found = true;
2028  break;
2029  }
2030  }
2031  }
2032  if (found)
2033  break;
2034  }
2035  if (!found) {
2036  cellU = cu0;
2037  cellV = cv0;
2038  }
2039  }
2040 }
2041 
2042 std::pair<int, float> HGCalDDDConstants::getIndex(int lay, bool reco) const {
2043  int indx = layerIndex(lay, reco);
2044  if (indx < 0)
2045  return std::make_pair(-1, 0);
2046  float cell(0);
2047  if (waferHexagon6()) {
2048  cell = (reco ? hgpar_->moduleCellR_[0] : hgpar_->moduleCellS_[0]);
2049  } else {
2050  if (waferHexagon8()) {
2051  cell = (reco ? hgpar_->moduleCellR_[0] : hgpar_->moduleCellS_[0]);
2052  } else {
2053  cell = hgpar_->scintCellSize(lay);
2054  }
2055  }
2056  return std::make_pair(indx, cell);
2057 }
2058 
2060  int ll(-1);
2061  if (waferHexagon6() && reco) {
2062  ll = static_cast<int>(std::find(hgpar_->depthLayerF_.begin(), hgpar_->depthLayerF_.end(), index) -
2063  hgpar_->depthLayerF_.begin());
2064  if (ll == static_cast<int>(hgpar_->depthLayerF_.size()))
2065  ll = -1;
2066  } else {
2067  ll = static_cast<int>(std::find(hgpar_->layerIndex_.begin(), hgpar_->layerIndex_.end(), index) -
2068  hgpar_->layerIndex_.begin());
2069  if (ll == static_cast<int>(hgpar_->layerIndex_.size()))
2070  ll = -1;
2071  }
2072 #ifdef EDM_ML_DEBUG
2073  edm::LogVerbatim("HGCalGeom") << "LayerFromIndex for " << index << ":" << reco << ":" << waferHexagon6() << " is"
2074  << ll << ":" << (ll + hgpar_->firstLayer_);
2075 #endif
2076  return ((ll < 0) ? ll : (ll + hgpar_->firstLayer_));
2077 }
2078 
2079 bool HGCalDDDConstants::isValidCell(int lay, int wafer, int cell) const {
2080  // Calculate the position of the cell
2081  // Works for options HGCalHexagon/HGCalHexagonFull
2082  double x = hgpar_->waferPosX_[wafer];
2083  double y = hgpar_->waferPosY_[wafer];
2084  if (((hgpar_->waferTypeT_[wafer] - 1) == HGCSiliconDetId::HGCalHD120) ||
2085  ((hgpar_->waferTypeT_[wafer] - 1) == HGCSiliconDetId::HGCalHD200)) {
2086  x += hgpar_->cellFineX_[cell];
2087  y += hgpar_->cellFineY_[cell];
2088  } else {
2089  x += hgpar_->cellCoarseX_[cell];
2090  y += hgpar_->cellCoarseY_[cell];
2091  }
2092  double rr = sqrt(x * x + y * y);
2093  bool result = ((rr >= hgpar_->rMinLayHex_[lay - 1]) && (rr <= hgpar_->rMaxLayHex_[lay - 1]) &&
2094  (wafer < static_cast<int>(hgpar_->waferPosX_.size())));
2095 #ifdef EDM_ML_DEBUG
2096  if (!result)
2097  edm::LogVerbatim("HGCalGeom") << "Input " << lay << ":" << wafer << ":" << cell << " Position " << x << ":" << y
2098  << ":" << rr << " Compare Limits " << hgpar_->rMinLayHex_[lay - 1] << ":"
2099  << hgpar_->rMaxLayHex_[lay - 1] << " Flag " << result;
2100 #endif
2101  return result;
2102 }
2103 
2104 bool HGCalDDDConstants::isValidCell8(int lay, int waferU, int waferV, int cellU, int cellV, int type) const {
2105  bool result(false);
2106  auto partn = waferTypeRotation(lay, waferU, waferV, false, false);
2107 #ifdef EDM_ML_DEBUG
2108  edm::LogVerbatim("HGCalGeom") << "waferHexagon8 " << waferHexagon8File() << ":" << mode_ << ":" << cassetteMode()
2109  << " part " << partn.first << ":" << partn.second;
2110 #endif
2111  if (cassetteMode()) {
2112  result = HGCalWaferMask::goodCell(cellU, cellV, partn.first);
2113 #ifdef EDM_ML_DEBUG
2114  edm::LogVerbatim("HGCalGeom") << "Input " << lay << ":" << waferU << ":" << waferV << ":" << cellU << ":" << cellV
2115  << " Result " << result << " from goodCell";
2116 #endif
2117  } else {
2118  float x(0), y(0);
2119  int kndx = cellV * 100 + cellU;
2121  auto ktr = hgpar_->cellFineIndex_.find(kndx);
2122  if (ktr != hgpar_->cellFineIndex_.end()) {
2123  x = hgpar_->cellFineX_[ktr->second];
2124  y = hgpar_->cellFineY_[ktr->second];
2125  }
2126 #ifdef EDM_ML_DEBUG
2127  edm::LogVerbatim("HGCalGeom") << "Fine " << cellU << ":" << cellV << ":" << kndx << ":" << x << ":" << y << ":"
2128  << (ktr != hgpar_->cellFineIndex_.end());
2129 #endif
2130  } else {
2131  auto ktr = hgpar_->cellCoarseIndex_.find(kndx);
2132  if (ktr != hgpar_->cellCoarseIndex_.end()) {
2133  x = hgpar_->cellCoarseX_[ktr->second];
2134  y = hgpar_->cellCoarseY_[ktr->second];
2135  }
2136 #ifdef EDM_ML_DEBUG
2137  edm::LogVerbatim("HGCalGeom") << "Coarse " << cellU << ":" << cellV << ":" << kndx << ":" << x << ":" << y << ":"
2138  << (ktr != hgpar_->cellCoarseIndex_.end());
2139 #endif
2140  }
2141  const auto& xy = waferPositionNoRot(lay, waferU, waferV, true, false);
2142  x += xy.first;
2143  y += xy.second;
2144 #ifdef EDM_ML_DEBUG
2145  edm::LogVerbatim("HGCalGeom") << "With wafer (" << waferU << "," << waferV << ") " << x << ":" << y;
2146 #endif
2147  double rr = sqrt(x * x + y * y);
2148  int ll = lay - hgpar_->firstLayer_;
2149  double tol = waferHexagon8File() ? 0.5 : 0.0;
2150  result = (((rr + tol) >= hgpar_->rMinLayHex_[ll]) && (rr <= hgpar_->rMaxLayHex_[ll]));
2151 #ifdef EDM_ML_DEBUG
2152  edm::LogVerbatim("HGCalGeom") << "Input " << lay << ":" << ll << ":" << waferU << ":" << waferV << ":" << cellU
2153  << ":" << cellV << " Position " << x << ":" << y << ":" << rr << " Compare Limits "
2154  << hgpar_->rMinLayHex_[ll] << ":" << hgpar_->rMaxLayHex_[ll] << " Flag " << result
2155  << " from Radius Limits";
2156 #endif
2157  if (result && waferHexagon8File()) {
2159  : hgpar_->nCellsCoarse_;
2160  result = HGCalWaferMask::goodCell(cellU, cellV, N, partn.first, partn.second);
2161 #ifdef EDM_ML_DEBUG
2162  edm::LogVerbatim("HGCalGeom") << "Input " << lay << ":" << waferU << ":" << waferV << ":" << cellU << ":" << cellV
2163  << " N " << N << " part " << partn.first << ":" << partn.second << " Result "
2164  << result << " from goodCell";
2165 #endif
2166  }
2167  }
2168  return result;
2169 }
2170 
2171 int32_t HGCalDDDConstants::waferIndex(int wafer, int index) const {
2172  int layer = layerFromIndex(index, true);
2176 #ifdef EDM_ML_DEBUG
2177  edm::LogVerbatim("HGCalGeom") << "WaferIndex for " << wafer << ":" << index << " (" << layer << ":" << waferU << ":"
2178  << waferV << ") " << indx;
2179 #endif
2180  return indx;
2181 }
2182 
2183 bool HGCalDDDConstants::waferInLayerTest(int wafer, int lay, bool full) const {
2184  bool in = (waferHexagon6()) ? true : false;
2185  if (!in) {
2186  double xpos = hgpar_->waferPosX_[wafer] + hgpar_->xLayerHex_[lay];
2187  double ypos = hgpar_->waferPosY_[wafer] + hgpar_->yLayerHex_[lay];
2188  std::pair<int, int> corner = HGCalGeomTools::waferCorner(
2189  xpos, ypos, rmax_, hexside_, hgpar_->rMinLayHex_[lay], hgpar_->rMaxLayHex_[lay], in);
2190  in = (full ? (corner.first > 0) : (corner.first == static_cast<int>(HGCalParameters::k_CornerSize)));
2191  if (in && fullAndPart_) {
2192  int indx = waferIndex(wafer, lay);
2193  in = (hgpar_->waferInfoMap_.find(indx) != hgpar_->waferInfoMap_.end());
2194 #ifdef EDM_ML_DEBUG
2195  if (!in)
2196  edm::LogVerbatim("HGCalGeom") << "WaferInLayerTest: Layer " << lay << " wafer " << wafer << " index " << indx
2197  << "( " << HGCalWaferIndex::waferLayer(indx) << ", "
2198  << HGCalWaferIndex::waferU(indx) << ", " << HGCalWaferIndex::waferV(indx)
2199  << ") in " << in;
2200 #endif
2201  }
2202 #ifdef EDM_ML_DEBUG
2203  edm::LogVerbatim("HGCalGeom") << "WaferInLayerTest: Layer " << lay << " wafer " << wafer << " R-limits "
2204  << hgpar_->rMinLayHex_[lay] << ":" << hgpar_->rMaxLayHex_[lay] << " Corners "
2205  << corner.first << ":" << corner.second << " In " << in;
2206 #endif
2207  }
2208  return in;
2209 }
2210 
2211 std::pair<double, double> HGCalDDDConstants::waferPositionNoRot(
2212  int lay, int waferU, int waferV, bool reco, bool debug) const {
2213  int ll = lay - hgpar_->firstLayer_;
2214  double x = hgpar_->xLayerHex_[ll];
2215  double y = hgpar_->yLayerHex_[ll];
2216 #ifdef EDM_ML_DEBUG
2217  if (debug)
2218  edm::LogVerbatim("HGCalGeom") << "Layer " << lay << ":" << ll << " Shift " << hgpar_->xLayerHex_[ll] << ":"
2219  << hgpar_->yLayerHex_[ll] << " U:V " << waferU << ":" << waferV;
2220 #endif
2221  if (!reco) {
2224  }
2225  const auto& xy = waferPosition(waferU, waferV, reco);
2226  x += xy.first;
2227  y += xy.second;
2228 #ifdef EDM_ML_DEBUG
2229  if (debug)
2230  edm::LogVerbatim("HGCalGeom") << "With wafer " << x << ":" << y << ":" << xy.first << ":" << xy.second;
2231 #endif
2232  return std::make_pair(x, y);
2233 }
2234 
2235 std::pair<double, double> HGCalDDDConstants::waferPosition(int waferU, int waferV, bool reco) const {
2236  double xx(0), yy(0);
2237  int indx = HGCalWaferIndex::waferIndex(0, waferU, waferV);
2238  auto itr = hgpar_->wafersInLayers_.find(indx);
2239  if (itr != hgpar_->wafersInLayers_.end()) {
2240  xx = hgpar_->waferPosX_[itr->second];
2241  yy = hgpar_->waferPosY_[itr->second];
2242  }
2243  if (!reco) {
2246  }
2247  return std::make_pair(xx, yy);
2248 }
2249 
2251 
std::vector< int > iradMaxBH_
bool maskCell(const DetId &id, int corners) const
size
Write out results.
std::pair< double, double > rangeRLayer(int lay, bool reco) const
double waferZ(int layer, bool reco) const
std::vector< double > waferPosY_
Log< level::Info, true > LogVerbatim
std::vector< int > layer_
static constexpr int scintillatorCassette
const int nphi
static int32_t cellPlacementIndex(int32_t iz, int32_t frontBack, int32_t orient)
Definition: HGCalCell.cc:239
static constexpr double tol_
void waferFromPosition(const double x, const double y, int &wafer, int &icell, int &celltyp) const
std::pair< double, double > rangeZ(bool reco) const
std::vector< int > depthLayerF_
std::vector< int > depth_
std::vector< double > zFrontMin_
std::vector< double > moduleCellR_
std::pair< int, int > tileRings(int layer) const
unsigned int layersInit(bool reco) const
hgtrap getModule(unsigned int k, bool reco) const
static constexpr int32_t cellPlacementOld
Definition: HGCalCell.h:25
bool isValidCell(int layindex, int wafer, int cell) const
static int getType(int index, const HGCalParameters::waferInfo_map &wafers)
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)
static constexpr int k_OffsetRotation
Definition: HGCalTypes.h:93
std::vector< std::pair< double, double > > layerRotV_
int getPhiBins(int lay) const
HGCalCassette hgcassette_
int layer() const
get the layer #
Definition: HFNoseDetId.h:57
bool cassetteShiftScintillator(int zside, int layer, int iphi) const
std::pair< double, double > cellEtaPhiTrap(int type, int irad) const
std::vector< bool > cellCoarseHalf_
static constexpr int32_t WaferHD200
Definition: HGCalTypes.h:33
int scintType(const int layer) const
std::vector< bool > cellFineHalf_
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
static std::pair< int32_t, int32_t > cellType(int32_t u, int32_t v, int32_t ncell, int32_t placementIndex)
Definition: HGCalCell.cc:251
std::pair< double, double > waferParameters(bool reco) const
bool waferInLayerTest(int wafer, int lay, bool full) const
static int32_t getUnpackedU(int id)
Definition: HGCalTypes.cc:16
std::vector< int > moduleLayR_
static constexpr int k_fourCorners
bool waferFullInLayer(int wafer, int lay, bool reco) const
double distFromEdgeTrap(double x, double y, double z) const
std::unique_ptr< HGCalCellUV > hgcellUV_
Simrecovecs max_modules_layer_
HGCalParameters::hgtrap getModule(unsigned int k, bool hexType, bool reco) const
int32_t waferU(const int32_t index)
HGCalParameters::waferInfo waferInfo(int lay, int waferU, int waferV) const
int32_t waferLayer(const int32_t index)
bool trapezoidFile() const
int waferU() const
Definition: HFNoseDetId.h:76
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
bool isValidHex(int lay, int mod, int cell, bool reco) const
bool tileRingEdge(double rho, int layer, int ring) const
static bool goodCell(int u, int v, int N, int type, int rotn)
static constexpr int32_t WaferOut
Definition: HGCalTypes.h:56
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
std::pair< double, double > getShift(int layer, int zside, int cassette, bool scnt=false) const
static constexpr int32_t WaferThree
Definition: HGCalTypes.h:42
std::vector< double > cellFineY_
static constexpr int32_t WaferLD300
Definition: HGCalTypes.h:32
std::pair< int, int > getREtaRange(int lay) const
int lastLayer(bool reco) const
static int getPartial(int index, const HGCalParameters::waferInfo_map &wafers)
static int32_t getUnpackedV(int id)
Definition: HGCalTypes.cc:22
std::pair< double, double > getXY(int layer, double x, double y, bool forwd) const
bool waferHexagon8() const
std::pair< int, int > waferTypeRotation(int layer, int waferU, int waferV, bool fromFile, bool debug) const
int modulesInit(int lay, bool reco) const
std::unordered_map< int32_t, bool > waferIn_
int zside(DetId const &)
int layerFromIndex(int index, 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
int32_t cellType(int type, int waferU, int waferV, int iz, int fwdBack, int orient) const
std::vector< int > layerGroupM_
bool waferHexagon6() const
bool isValidHex8(int lay, int waferU, int waferV, bool fullAndPart) const
float float float z
hgtrform getTrForm(unsigned int k) const
static constexpr uint32_t k_CornerSize
std::pair< double, double > getRangeR(int, bool reco) const
static int getOrient(int index, const HGCalParameters::waferInfo_map &wafers)
wafer_map wafersInLayers_
bool waferInLayer(int wafer, int lay, bool reco) const
U second(std::pair< T, U > const &p)
double scintCellSize(const int layer) const
HGCalParameters::tileInfo tileInfo(int zside, int layer, int ring) const
std::vector< double > cellCoarseX_
static constexpr int scintillatorFile
std::map< int, HGCWaferParam > waferLayer_
int32_t tileIndex(int32_t layer, int32_t ring, int32_t phi)
std::vector< std::pair< int, int > > tileRingRange_
constexpr int32_t waferV() const
static constexpr int32_t WaferFull
Definition: HGCalTypes.h:35
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_
unsigned int waferFileSize() const
std::unique_ptr< HGCalCell > hgcell_
int getTypeHex(int layer, int waferU, int waferV) const
static constexpr int32_t WaferHalf
Definition: HGCalTypes.h:39
dif1
Definition: cuy.py:893
bool isHalfCell(int waferType, int cell) const
bool waferHexagon8Fine() const
int maxCells(bool reco) const
std::vector< double > cellSize_
std::tuple< int, int, int, int > waferFileInfoFromIndex(int kk) const
std::vector< int > waferUVMaxLayer_
std::tuple< int, int, int, int > waferFileInfo(unsigned int kk) const
std::array< int, 5 > assignCellHex(float x, float y, int zside, int lay, bool reco, bool extend, bool debug) const
int waferFileIndex(unsigned int kk) const
std::vector< int > layerIndex_
std::vector< double > yLayerHex_
T sqrt(T t)
Definition: SSEVec.h:23
HGCalDDDConstants(const HGCalParameters *hp, const std::string &name)
static const float tolmin
Definition: FlatHexagon.cc:21
int layerIndex(int lay, bool reco) const
std::vector< double > rMaxFront_
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Definition: GenABIO.cc:168
constexpr int32_t zside() const
get the z-side of the cell (1/-1)
bool cellInLayer(int waferU, int waferV, int cellU, int cellV, int lay, int zside, bool reco) const
static constexpr int32_t WaferOrient0
Definition: HGCalTypes.h:63
int getUVMax(int type) const
bool isValidCell8(int lay, int waferU, int waferV, int cellU, int cellV, int type) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::pair< int, int > simToReco(int cell, int layer, int mod, bool half) const
std::pair< int, float > getIndex(int lay, bool reco) const
static constexpr int32_t WaferHD120
Definition: HGCalTypes.h:30
static int getCassette(int index, const HGCalParameters::waferInfo_map &wafers)
bool isValidTrap(int zside, int lay, int ieta, int iphi) const
bool waferVirtual(int layer, int waferU, int waferV) const
int tileCount(int layer, int ring) const
std::pair< int, int > assignCell(float x, float y, int lay, int subSec, bool reco) const
std::vector< double > slopeTop_
std::vector< HGCalParameters::hgtrap > getModules() const
double mouseBite(bool reco) const
unsigned int layers(bool reco) const
static constexpr int32_t WaferCorner0
Definition: HGCalTypes.h:13
bool tileTrapezoid() const
std::pair< int, int > tileType(int layer, int ring, int phi) const
bool tilePhiEdge(double phi, int layer, int iphi) const
std::pair< float, float > locateCellTrap(int zside, int lay, int ieta, int iphi, bool reco, bool debug) const
std::vector< double > rMinLayHex_
int32_t waferIndex(int32_t layer, int32_t waferU, int32_t waferV, bool old=false)
int getTypeTrap(int layer) const
void setParameter(int cassette, const std::vector< double > &shifts, bool both=true)
Definition: HGCalCassette.cc:8
int32_t tileCassette(int32_t, int32_t, int32_t, int32_t)
int type() const
get the type
Definition: HFNoseDetId.h:51
dif2
Definition: cuy.py:895
double cellSizeHex(int type) const
std::vector< double > zLayerHex_
std::vector< int > layerType_
#define M_PI
waferT_map waferTypes_
std::pair< double, double > rangeR(double z, bool reco) const
std::pair< double, double > waferPosition(int wafer, bool reco) const
std::vector< double > rMaxLayHex_
std::vector< double > slopeMin_
static constexpr int32_t WaferCenterR
Definition: HGCalTypes.h:27
Definition: DetId.h:17
std::vector< int > lastModule_
std::array< int, 4 > waferMax_
static constexpr double k_ScaleToDDD
int numberCellsHexagon(int wafer) const
std::vector< double > radiusMixBoundary_
std::array< int, 3 > assignCellTrap(float x, float y, float z, int lay, bool reco) const
#define debug
Definition: HDRShower.cc:19
bool tileExist(int zside, int layer, int ring, int phi) const
#define N
Definition: blowfish.cc:9
std::vector< double > cellThickness_
static constexpr int k_fiveCorners
GlobalPoint waferLocal2Global(HepGeom::Point3D< float > &loc, const DetId &id, bool useWafer, bool reco, bool debug) const
std::vector< int > layerGroup_
int numberCells(bool reco) const
part
Definition: HCALResponse.h:20
std::vector< double > moduleCellS_
std::pair< float, float > locateCellHex(int cell, int wafer, bool reco) const
double cellThickness(int layer, int waferU, int waferV) const
int waferType(DetId const &id, bool fromFile) const
double b
Definition: hdecay.h:120
wafer_map cellCoarseIndex_
std::pair< int, int > rowColumnWafer(const int wafer) const
int waferV() const
Definition: HFNoseDetId.h:79
unsigned int getTrFormN() const
constexpr int32_t layer() const
get the layer #
HGCalParameters::hgtrform getTrForm(unsigned int k) const
std::vector< double > rMinFront_
std::pair< float, float > locateCell(int cell, int lay, int type, bool reco) const
std::vector< int > iradMinBH_
bool waferHexagon8File() const
std::vector< double > cellFineX_
int maxRows(int lay, bool reco) const
wafer_map typesInLayers_
const HGCalGeometryMode::GeometryMode mode_
int32_t waferIndex(int wafer, int index) const
static constexpr double k_ScaleFromDDD
std::vector< HGCalParameters::hgtrform > getTrForms() const
std::vector< int > layerGroupO_
fixed size matrix
int getLayer(double z, bool reco) const
#define TYPELOOKUP_DATA_REG(_dataclass_)
Definition: typelookup.h:102
static constexpr int k_threeCorners
bool cassetteShiftSilicon(int zside, int layer, int waferU, int waferV) const
std::array< int, 3 > HGCWaferParam
bool cassetteMode() const
int32_t waferV(const int32_t index)
int waferTypeL(int wafer) const
std::vector< int > waferCopy_
col
Definition: cuy.py:1009
std::vector< double > cassetteShift_
bool tileExist(const int32_t *hex, int32_t zside, int32_t phi)
static unsigned int const shift
float x
std::vector< int > depthIndex_
static constexpr double tan30deg_
double guardRingOffset(bool reco) const
std::unique_ptr< HGCalCellOffset > cellOffset_
int cassetteTile(int iphi) const
std::vector< double > zFrontTop_
double sensorSizeOffset(bool reco) const
std::vector< double > radiusLayer_[2]
std::array< uint32_t, 2 > tot_layers_
static constexpr int32_t WaferFive
Definition: HGCalTypes.h:36
std::vector< int > waferTypeT_
int modules(int lay, bool reco) const
static constexpr int32_t WaferCornerMin
Definition: HGCalTypes.h:76
int modifyUV(int uv, int type1, int type2) const
std::vector< double > cellCoarseY_
Log< level::Warning, false > LogWarning
constexpr int32_t waferU() const
waferInfo_map waferInfoMap_
constexpr int32_t type() const
get the type
__host__ __device__ V wmin
std::pair< float, float > localToGlobal8(int zside, int lay, int waferU, int waferV, double localX, double localY, bool reco, bool debug) const
const HGCalParameters * hgpar_
std::vector< double > waferPosX_
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
int scintCells(const int layer) const
static constexpr int32_t WaferLD200
Definition: HGCalTypes.h:31
tileInfo_map tileInfoMap_
static constexpr int k_allCorners
if(threadIdxLocalY==0 &&threadIdxLocalX==0)
int cellHex(double xx, double yy, const double &cellR, const std::vector< double > &posX, const std::vector< double > &posY) const
int partialWaferType(int lay, int waferU, int waferV) const
std::vector< int > waferTypeL_
std::vector< double > xLayerHex_
std::pair< double, double > waferPositionNoRot(int lay, int waferU, int waferV, bool reco, bool debug) const
double distFromEdgeHex(double x, double y, double z) const
int layerType(int lay) const
int waferFromCopy(int copy) const
__host__ __device__ V V wmax
static constexpr int32_t layerFrontBack(int32_t layerOrient)
Definition: HGCalTypes.h:137
static bool maskCell(int u, int v, int N, int ncor, int fcor, int corners)