CMS 3D CMS Logo

HGCalDDDConstants.cc
Go to the documentation of this file.
2 
12 
13 #include "CLHEP/Units/GlobalPhysicalConstants.h"
14 #include "CLHEP/Units/GlobalSystemOfUnits.h"
15 
16 #include <algorithm>
17 #include <functional>
18 #include <numeric>
19 
20 //#define EDM_ML_DEBUG
21 
22 static const int maxType = 2;
23 static const int minType = 0;
24 
26  const std::string& name) : hgpar_(hp),
27  sqrt3_(std::sqrt(3.0)) {
28  mode_ = hgpar_->mode_;
34  std::cos(30.0*CLHEP::deg));
35  hexside_ = 2.0 * rmax_ * tan30deg_;
36 #ifdef EDM_ML_DEBUG
37  edm::LogVerbatim("HGCalGeom") << "rmax_ " << rmax_ << ":" << hexside_
38  << " CellSize " << 0.5*HGCalParameters::k_ScaleFromDDD*hgpar_->cellSize_[0]
40 #endif
41  }
42  // init maps and constants
43  modHalf_ = 0;
45  for (int simreco = 0; simreco < 2; ++simreco) {
46  tot_layers_[simreco] = layersInit((bool)simreco);
47  max_modules_layer_[simreco].resize(tot_layers_[simreco]+1);
48  for (unsigned int layer=1; layer <= tot_layers_[simreco]; ++layer) {
49  max_modules_layer_[simreco][layer] = modulesInit(layer,(bool)simreco);
50  if (simreco == 1) {
51  modHalf_ += max_modules_layer_[simreco][layer];
53  max_modules_layer_[simreco][layer]);
54 #ifdef EDM_ML_DEBUG
55  edm::LogVerbatim("HGCalGeom") << "Layer " << layer << " with "
56  << max_modules_layer_[simreco][layer]
57  << ":" << modHalf_ << " modules";
58 #endif
59  }
60  }
61  }
62  tot_wafers_ = wafers();
63 
64 #ifdef EDM_ML_DEBUG
65  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants initialized for "
66  << name << " with " << layers(false) << ":"
67  << layers(true) << " layers, " << wafers()
68  << ":" << 2*modHalf_ << " wafers with maximum "
69  << maxWafersPerLayer_ << " per layer and "
70  << "maximum of " << maxCells(false) << ":"
71  << maxCells(true) << " cells";
72 #endif
77  int wminT(9999999), wmaxT(-9999999), kount1(0), kount2(0);
78  for (unsigned int i=0; i<getTrFormN(); ++i) {
79  int lay0 = getTrForm(i).lay;
80  int wmin(9999999), wmax(-9999999), kount(0);
81  for (int wafer=0; wafer<sectors(); ++wafer) {
82  bool waferIn = waferInLayer(wafer,lay0,true);
86  waferIn_[kndx] = waferIn;
87  }
88  if (waferIn) {
89  int waferU = (((mode_ == HGCalGeometryMode::Hexagon) ||
92  if (waferU < wmin) wmin = waferU;
93  if (waferU > wmax) wmax = waferU;
94  ++kount;
95  }
96  }
97  if (wminT > wmin) wminT = wmin;
98  if (wmaxT < wmax) wmaxT = wmax;
99  if (kount1 < kount) kount1= kount;
100  kount2 += kount;
101 #ifdef EDM_ML_DEBUG
102  int lay1 = getIndex(lay0,true).first;
103  edm::LogVerbatim("HGCalGeom") << "Index " << i << " Layer " << lay0
104  << ":" << lay1 << " Wafer " << wmin
105  << ":" << wmax << ":" << kount;
106 #endif
107  HGCWaferParam a1{ {wmin,wmax,kount} };
108  waferLayer_[lay0] = a1;
109  }
110  waferMax_ = std::array<int,4>{ {wminT,wmaxT,kount1,kount2} };
111 #ifdef EDM_ML_DEBUG
112  edm::LogVerbatim("HGCalGeom") << "Overall wafer statistics: " << wminT
113  << ":" << wmaxT << ":" << kount1 << ":"
114  << kount2;
115 #endif
116  }
117 }
118 
120 
121 std::pair<int,int> HGCalDDDConstants::assignCell(float x, float y, int lay,
122  int subSec, bool reco) const {
123  const auto & index = getIndex(lay, reco);
124  if (index.first < 0) return std::make_pair(-1,-1);
127  float xx = (reco) ? x : HGCalParameters::k_ScaleFromDDD*x;
128  float yy = (reco) ? y : HGCalParameters::k_ScaleFromDDD*y;
129 
130  //First the wafer
131  int wafer = cellHex(xx, yy, rmax_, hgpar_->waferPosX_, hgpar_->waferPosY_);
132  if (wafer < 0 || wafer >= (int)(hgpar_->waferTypeT_.size())) {
133  edm::LogWarning("HGCalGeom") << "Wafer no. out of bound for " << wafer
134  << ":" << (hgpar_->waferTypeT_).size()
135  << ":" << (hgpar_->waferPosX_).size()
136  << ":" << (hgpar_->waferPosY_).size()
137  << " ***** ERROR *****";
138  return std::make_pair(-1,-1);
139  } else {
140  // Now the cell
141  xx -= hgpar_->waferPosX_[wafer];
142  yy -= hgpar_->waferPosY_[wafer];
143  if (hgpar_->waferTypeT_[wafer] == 1)
144  return std::make_pair(wafer,cellHex(xx, yy,
147  else
148  return std::make_pair(wafer,cellHex(xx, yy,
151  }
152  } else {
153  return std::make_pair(-1,-1);
154  }
155 }
156 
157 std::array<int,5> HGCalDDDConstants::assignCellHex(float x, float y, int lay,
158  bool reco) const {
159  int waferU(0), waferV(0), waferType(-1), cellU(0), cellV(0);
162  double xx = (reco) ? HGCalParameters::k_ScaleToDDD*x : x;
163  double yy = (reco) ? HGCalParameters::k_ScaleToDDD*y : y;
164  double wt(1.0);
165  waferFromPosition(xx,yy,lay,waferU,waferV,cellU,cellV,waferType,wt);
166  }
167  return std::array<int,5>{ {waferU,waferV,waferType,cellU,cellV} };
168 }
169 
170 std::array<int,3> HGCalDDDConstants::assignCellTrap(float x, float y,
171  float z, int layer,
172  bool reco) const {
173 
174  int irad(-1), iphi(-1), type(-1);
175  const auto & indx = getIndex(layer,reco);
176  if (indx.first < 0) return std::array<int,3>{ {irad,iphi,type} };
177  double xx = (z > 0) ? x : -x;
178  double r = (reco ? std::sqrt(x*x+y*y) :
180  double phi = (r == 0. ? 0. : std::atan2(y,xx));
181  if (phi < 0) phi += (2.0*M_PI);
182  type = hgpar_->scintType(layer);
183  auto ir = std::lower_bound(hgpar_->radiusLayer_[type].begin(),
184  hgpar_->radiusLayer_[type].end(), r);
185  irad = (int)(ir - hgpar_->radiusLayer_[type].begin());
186  irad = std::min(std::max(irad,hgpar_->iradMinBH_[indx.first]),
187  hgpar_->iradMaxBH_[indx.first]);
188  iphi = 1 + (int)(phi/indx.second);
189 #ifdef EDM_ML_DEBUG
190  edm::LogVerbatim("HGCalGeom") << "assignCellTrap Input " << x << ":" << y
191  << ":" << z << ":" << layer << ":" << reco
192  << " x|r " << xx << ":" << r << " phi "
193  << phi << " o/p " << irad << ":" << iphi
194  << ":" << type;
195 #endif
196  return std::array<int,3>{ {irad,iphi,type} };
197 }
198 
199 bool HGCalDDDConstants::cellInLayer(int waferU, int waferV, int cellU,
200  int cellV, int lay, bool reco) const {
201  const auto & indx = getIndex(lay,true);
202  if (indx.first >= 0) {
207  const auto & xy = (((mode_ == HGCalGeometryMode::Hexagon8) ||
209  locateCell(lay, waferU, waferV, cellU, cellV, reco, true, false) :
210  locateCell(cellU, lay, waferU, reco));
211  double rpos = sqrt(xy.first*xy.first + xy.second*xy.second);
212  return ((rpos >= hgpar_->rMinLayHex_[indx.first]) &&
213  (rpos <= hgpar_->rMaxLayHex_[indx.first]));
214  } else {
215  return true;
216  }
217  } else {
218  return false;
219  }
220 }
221 
222 double HGCalDDDConstants::cellThickness(int layer, int waferU,
223  int waferV) const {
224 
225  double thick(-1);
228  auto itr = hgpar_->typesInLayers_.find(HGCalWaferIndex::waferIndex(layer,waferU,waferV));
229  int type = ((itr == hgpar_->typesInLayers_.end() ? maxType :
230  hgpar_->waferTypeL_[itr->second]));
231  thick = 10000.0*hgpar_->cellThickness_[type];
232  } else if ((mode_ == HGCalGeometryMode::Hexagon) ||
234  int type = (((waferU>=0)&&(waferU<(int)(hgpar_->waferTypeL_.size()))) ?
235  hgpar_->waferTypeL_[waferU] : minType);
236  thick = 100.0*type;
237  }
238  return thick;
239 }
240 
242  int indx = (((mode_ == HGCalGeometryMode::Hexagon8) ||
244  ((type >= 1) ? 1 : 0) : ((type == 1) ? 1 : 0));
245  double cell = ((mode_ == HGCalGeometryMode::Trapezoid) ?
246  0.5*hgpar_->cellSize_[indx] :
248  return cell;
249 }
250 
252  int cellV) const {
253  // type=0: in the middle; 1..6: the edges clocwise from bottom left;
254  // =11..16: the corners clockwise from bottom
255  int N = (type == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
256  if (cellU == 0) {
257  if (cellV == 0) return HGCalDDDConstants::CellType::BottomLeftCorner;
258  else if (cellV-cellU == N-1) return HGCalDDDConstants::CellType::BottomCorner;
260  } else if (cellV == 0) {
261  if (cellU-cellV == N) return HGCalDDDConstants::CellType::TopLeftCorner;
263  } else if (cellU-cellV == N) {
264  if (cellU == 2*N-1) return HGCalDDDConstants::CellType::TopCorner;
266  } else if (cellU == 2*N-1) {
267  if (cellV == 2*N-1) return HGCalDDDConstants::CellType::TopRightCorner;
269  } else if (cellV == 2*N-1) {
270  if (cellV-cellU == N-1) return HGCalDDDConstants::CellType::BottomRightCorner;
272  } else if (cellV-cellU == N-1) {
274  } else if ((cellU > 2*N-1) || (cellV > 2*N-1) || (cellV >= (cellU+N)) ||
275  (cellU > (cellV+N))) {
277  } else {
279  }
280 }
281 
282 double HGCalDDDConstants::distFromEdgeHex(double x, double y, double z) const {
283 
284  //Assming the point is within a hexagonal plane of the wafer, calculate
285  //the shortest distance from the edge
286  if (z < 0) x = -x;
287  double dist(0);
288  //Input x, y in Geant4 unit and transformed to CMSSW standard
291  int sizew = (int)(hgpar_->waferPosX_.size());
292  int wafer = sizew;
293  //Transform to the local coordinate frame of the wafer first
294  for (int k=0; k<sizew; ++k) {
295  double dx = std::abs(xx-hgpar_->waferPosX_[k]);
296  double dy = std::abs(yy-hgpar_->waferPosY_[k]);
297  if ((dx <= rmax_) && (dy <= hexside_) &&
298  ((dy <= 0.5*hexside_) || (dx*tan30deg_ <= (hexside_-dy)))) {
299  wafer = k;
300  xx -= hgpar_->waferPosX_[k];
301  yy -= hgpar_->waferPosY_[k];
302  break;
303  }
304  }
305  //Look at only one quarter (both x,y are positive)
306  if (wafer < sizew) {
307  if (std::abs(yy) < 0.5*hexside_) {
308  dist = rmax_ - std::abs(xx);
309  } else {
310  dist = 0.5*((rmax_-std::abs(xx))-sqrt3_*(std::abs(yy)-0.5*hexside_));
311  }
312  } else {
313  dist = 0;
314  }
316 #ifdef EDM_ML_DEBUG
317  edm::LogVerbatim("HGCalGeom") << "DistFromEdgeHex: Local " << xx << ":"
318  << yy << " wafer " << wafer << " flag "
319  << (wafer < sizew) << " Distance " << rmax_
320  << ":" << (rmax_-std::abs(xx)) << ":"
321  << (std::abs(yy)-0.5*hexside_) << ":"
322  << 0.5*hexside_ << ":" << dist;
323 #endif
324  return dist;
325 }
326 
327 double HGCalDDDConstants::distFromEdgeTrap(double x, double y, double z) const {
328 
329  //Assming the point is within the eta-phi plane of the scintillator tile,
330  //calculate the shortest distance from the edge
331  int lay = getLayer(z,false);
332  double xx = (z < 0) ? -x : x;
333  int indx = layerIndex(lay,false);
334  double r = HGCalParameters::k_ScaleFromDDD*std::sqrt(x*x+y*y);
335  double phi = (r == 0. ? 0. : std::atan2(y,xx));
336  if (phi < 0) phi += (2.0*M_PI);
337  int type = hgpar_->scintType(lay);
338  double cell = hgpar_->scintCellSize(lay);
339  //Compare with the center of the tile find distances along R and also phi
340  //Take the smaller value
341  auto ir = std::lower_bound(hgpar_->radiusLayer_[type].begin(),
342  hgpar_->radiusLayer_[type].end(), r);
343  int irad = (int)(ir-hgpar_->radiusLayer_[type].begin());
344  if (irad < hgpar_->iradMinBH_[indx]) irad = hgpar_->iradMinBH_[indx];
345  else if (irad > hgpar_->iradMaxBH_[indx]) irad = hgpar_->iradMaxBH_[indx];
346  int iphi = 1 + (int)(phi/cell);
347  double dphi = std::max(0.0,(0.5*cell-std::abs(phi-(iphi-0.5)*cell)));
348  double dist = std::min((r-hgpar_->radiusLayer_[type][irad-1]),
349  (hgpar_->radiusLayer_[type][irad]-r));
350 #ifdef EDM_ML_DEBUG
351  edm::LogVerbatim("HGCalGeom") << "DistFromEdgeTrap: Global " << x << ":"
352  << y << ":" << z << " Layer " << lay
353  << " Index " << indx << ":" << type << " xx "
354  << xx << " R " << r << ":" << irad << ":"
355  << hgpar_->radiusLayer_[type][irad-1] << ":"
356  << hgpar_->radiusLayer_[type][irad]
357  << " Phi " << phi << ":" << iphi << ":"
358  << (iphi-0.5)*cell << " cell " << cell
359  << " Dphi " << dphi << " Dist " << dist
360  << ":" << r*dphi;
361 #endif
362  return HGCalParameters::k_ScaleToDDD*std::min(r*dphi,dist);
363 }
364 
365 int HGCalDDDConstants::getLayer(double z, bool reco) const {
366 
367  //Get the layer # from the gloabl z coordinate
368  unsigned int k = 0;
369  double zz = (reco ? std::abs(z) :
371  const auto& zLayerHex = hgpar_->zLayerHex_;
372  std::find_if(zLayerHex.begin()+1,zLayerHex.end(),[&k,&zz,&zLayerHex](double zLayer){ ++k; return zz < 0.5*(zLayerHex[k-1]+zLayerHex[k]);});
373  int lay = k;
374  if (((mode_ == HGCalGeometryMode::Hexagon) ||
376  int indx = layerIndex(lay,false);
377  if (indx >= 0) lay = hgpar_->layerGroup_[indx];
378  } else {
379  lay += (hgpar_->firstLayer_ - 1);
380  }
381  return lay;
382 }
383 
385  bool hexType,
386  bool reco) const {
387 
389  if (hexType) {
390  if (indx >= hgpar_->waferTypeL_.size())
391  edm::LogWarning("HGCalGeom") << "Wafer no. out bound for index " << indx
392  << ":" << (hgpar_->waferTypeL_).size()
393  << ":" << (hgpar_->waferPosX_).size()
394  << ":" << (hgpar_->waferPosY_).size()
395  << " ***** ERROR *****";
396  unsigned int type = ((indx < hgpar_->waferTypeL_.size()) ?
397  hgpar_->waferTypeL_[indx] : 3);
398  if (type > 0) --type;
399  mytr = hgpar_->getModule(type, reco);
400  } else {
401  mytr = hgpar_->getModule(indx,reco);
402  }
403  return mytr;
404 }
405 
406 std::vector<HGCalParameters::hgtrap> HGCalDDDConstants::getModules() const {
407 
408  std::vector<HGCalParameters::hgtrap> mytrs;
409  for (unsigned int k=0; k<hgpar_->moduleLayR_.size(); ++k)
410  mytrs.emplace_back(hgpar_->getModule(k,true));
411  return mytrs;
412 }
413 
414 std::vector<HGCalParameters::hgtrform> HGCalDDDConstants::getTrForms() const {
415 
416  std::vector<HGCalParameters::hgtrform> mytrs;
417  for (unsigned int k=0; k<hgpar_->trformIndex_.size(); ++k)
418  mytrs.emplace_back(hgpar_->getTrForm(k));
419  return mytrs;
420 }
421 
422 int HGCalDDDConstants::getTypeTrap(int layer) const {
423  //Get the module type for scinitllator
425  return hgpar_->scintType(layer);
426  } else {
427  return -1;
428  }
429 }
430 
431 int HGCalDDDConstants::getTypeHex(int layer, int waferU, int waferV) const {
432  //Get the module type for a silicon wafer
435  auto itr = hgpar_->typesInLayers_.find(HGCalWaferIndex::waferIndex(layer,waferU,waferV));
436  return ((itr == hgpar_->typesInLayers_.end() ? 2 :
437  hgpar_->waferTypeL_[itr->second]));
438  } else {
439  return -1;
440  }
441 }
442 
443 bool HGCalDDDConstants::isHalfCell(int waferType, int cell) const {
444  if (waferType < 1 || cell < 0) return false;
445  return waferType == 2 ? hgpar_->cellCoarseHalf_[cell] : hgpar_->cellFineHalf_[cell];
446 }
447 
448 bool HGCalDDDConstants::isValidHex(int lay, int mod, int cell,
449  bool reco) const {
450  //Check validity for a layer|wafer|cell of pre-TDR version
451  bool result(false), resultMod(false);
452  int cellmax(0);
455  int32_t copyNumber = hgpar_->waferCopy_[mod];
456  result = ((lay > 0 && lay <= (int)(layers(reco))));
457  if (result) {
458  const int32_t lay_idx = reco ? (lay-1)*3 + 1 : lay;
459  const auto& the_modules = hgpar_->copiesInLayers_[lay_idx];
460  auto moditr = the_modules.find(copyNumber);
461  result = resultMod = (moditr != the_modules.end());
462 #ifdef EDM_ML_DEBUG
463  if (!result)
464  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants: Layer " << lay
465  << ":" << lay_idx << " Copy "
466  << copyNumber << ":" << mod
467  << " Flag " << result;
468 #endif
469  if (result) {
470  if (moditr->second >= 0) {
471  if (mod >= (int)(hgpar_->waferTypeT_.size()))
472  edm::LogWarning("HGCalGeom") << "Module no. out of bound for "
473  << mod << " to be compared with "
474  << (hgpar_->waferTypeT_).size()
475  << " ***** ERROR *****";
476  cellmax = ((hgpar_->waferTypeT_[mod]==1) ?
477  (int)(hgpar_->cellFineX_.size()) :
478  (int)(hgpar_->cellCoarseX_.size()));
479  result = (cell >=0 && cell <= cellmax);
480  } else {
481  result = isValidCell(lay_idx, mod, cell);
482  }
483  }
484  }
485  }
486 
487 #ifdef EDM_ML_DEBUG
488  if (!result)
489  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants: Layer " << lay << ":"
490  << (lay > 0 && (lay <= (int)(layers(reco))))
491  << " Module " << mod << ":" << resultMod
492  << " Cell " << cell << ":" << cellmax << ":"
493  << (cell >=0 && cell <= cellmax)
494  << ":" << maxCells(reco);
495 #endif
496  return result;
497 }
498 
499 bool HGCalDDDConstants::isValidHex8(int layer, int modU, int modV, int cellU,
500  int cellV) const {
501  //Check validity for a layer|wafer|cell of post-TDR version
502  int indx = HGCalWaferIndex::waferIndex(layer,modU,modV);
503  auto itr = hgpar_->typesInLayers_.find(indx);
504 #ifdef EDM_ML_DEBUG
505  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:WaferType "
506  << layer << ":" << modU << ":" << modV << ":"
507  << indx << " Test "
508  << (itr == hgpar_->typesInLayers_.end());
509 #endif
510  if (itr == hgpar_->typesInLayers_.end()) return false;
511  auto jtr = waferIn_.find(indx);
512 #ifdef EDM_ML_DEBUG
513  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:WaferIn "
514  << jtr->first << ":" << jtr->second;
515 #endif
516  if (!(jtr->second)) return false;
517  int N = ((hgpar_->waferTypeL_[itr->second] == 0) ? hgpar_->nCellsFine_ :
519 #ifdef EDM_ML_DEBUG
520  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants::isValidHex8:Cell "
521  << cellU << ":" << cellV << ":" << N
522  << " Tests " << (cellU >= 0) << ":"
523  << (cellU < 2*N) << ":" << (cellV >= 0) << ":"
524  << (cellV < 2*N) << ":" << ((cellV-cellU) < N)
525  << ":" << ((cellU-cellV) <= N);
526 #endif
527  if ((cellU >= 0) && (cellU < 2*N) && (cellV >= 0) && (cellV < 2*N)) {
528  return (((cellV-cellU) < N) && ((cellU-cellV) <= N));
529  } else {
530  return false;
531  }
532 }
533 
534 bool HGCalDDDConstants::isValidTrap(int layer, int irad, int iphi) const {
535  //Check validity for a layer|eta|phi of scintillator
536  const auto & indx = getIndex(layer,true);
537  if (indx.first < 0) return false;
538  return ((irad >= hgpar_->iradMinBH_[indx.first]) &&
539  (irad <= hgpar_->iradMaxBH_[indx.first]) &&
540  (iphi > 0) && (iphi <= hgpar_->scintCells(layer)));
541 }
542 
544  return (hgpar_->firstLayer_+tot_layers_[(int)reco]-1);
545 }
546 
547 unsigned int HGCalDDDConstants::layers(bool reco) const {
548  return tot_layers_[(int)reco];
549 }
550 
551 int HGCalDDDConstants::layerIndex(int lay, bool reco) const {
552  int ll = lay - hgpar_->firstLayer_;
553  if (ll<0 || ll>=(int)(hgpar_->layerIndex_.size())) return -1;
556  if (reco && ll>=(int)(hgpar_->depthIndex_.size())) return -1;
557  return (reco ? hgpar_->depthLayerF_[ll] : hgpar_->layerIndex_[ll]);
558  } else {
559  return (hgpar_->layerIndex_[ll]);
560  }
561 }
562 
563 unsigned int HGCalDDDConstants::layersInit(bool reco) const {
564  return (reco ? hgpar_->depthIndex_.size() : hgpar_->layerIndex_.size());
565 }
566 
567 std::pair<float,float> HGCalDDDConstants::locateCell(int cell, int lay,
568  int type, bool reco) const {
569  // type refers to wafer # for hexagon cell
570  float x(999999.), y(999999.);
571  const auto & index = getIndex(lay, reco);
572  int i = index.first;
573  if (i < 0) return std::make_pair(x,y);
576  x = hgpar_->waferPosX_[type];
577  y = hgpar_->waferPosY_[type];
578  if (hgpar_->waferTypeT_[type] == 1) {
579  x += hgpar_->cellFineX_[cell];
580  y += hgpar_->cellFineY_[cell];
581  } else {
582  x += hgpar_->cellCoarseX_[cell];
583  y += hgpar_->cellCoarseY_[cell];
584  }
585  if (!reco) {
588  }
589  }
590  return std::make_pair(x,y);
591 }
592 
593 std::pair<float,float> HGCalDDDConstants::locateCell(int lay, int waferU,
594  int waferV, int cellU,
595  int cellV, bool reco,
596  bool all, bool
597 #ifdef EDM_ML_DEBUG
598  debug
599 #endif
600  ) const {
601 
602  float x(0), y(0);
603  int indx = HGCalWaferIndex::waferIndex(lay,waferU,waferV);
604  auto itr = hgpar_->typesInLayers_.find(indx);
605  int type = ((itr == hgpar_->typesInLayers_.end()) ? 2 :
606  hgpar_->waferTypeL_[itr->second]);
607 #ifdef EDM_ML_DEBUG
608  if (debug)
609  edm::LogVerbatim("HGCalGeom") << "LocateCell " << lay << ":" << waferU
610  << ":" << waferV << ":" << indx << ":"
611  << (itr == hgpar_->typesInLayers_.end())
612  << ":" << type;
613 #endif
614  int kndx = cellV*100 + cellU;
615  if (type == 0) {
616  auto ktr = hgpar_->cellFineIndex_.find(kndx);
617  if (ktr != hgpar_->cellFineIndex_.end()) {
618  x = hgpar_->cellFineX_[ktr->second];
619  y = hgpar_->cellFineY_[ktr->second];
620  }
621 #ifdef EDM_ML_DEBUG
622  if (debug)
623  edm::LogVerbatim("HGCalGeom") << "Fine " << cellU << ":" << cellV << ":"
624  << kndx << ":" << x << ":" << y << ":"
625  << (ktr != hgpar_->cellFineIndex_.end());
626 #endif
627  } else {
628  auto ktr = hgpar_->cellCoarseIndex_.find(kndx);
629  if (ktr != hgpar_->cellCoarseIndex_.end()) {
630  x = hgpar_->cellCoarseX_[ktr->second];
631  y = hgpar_->cellCoarseY_[ktr->second];
632  }
633 #ifdef EDM_ML_DEBUG
634  if (debug)
635  edm::LogVerbatim("HGCalGeom") << "Coarse " << cellU << ":" << cellV <<":"
636  << kndx << ":" << x << ":" << y << ":"
637  << (ktr != hgpar_->cellCoarseIndex_.end());
638 #endif
639  }
640  if (!reco) {
643  }
644  if (all) {
645  const auto & xy = waferPosition(waferU, waferV, reco);
646  x += xy.first;
647  y += xy.second;
648 #ifdef EDM_ML_DEBUG
649  if (debug)
650  edm::LogVerbatim("HGCalGeom") << "With wafer " << x << ":" << y << ":"
651  << xy.first << ":" << xy.second;
652 #endif
653  }
654  return std::make_pair(x,y);
655 }
656 
657 std::pair<float,float> HGCalDDDConstants::locateCellHex(int cell, int wafer,
658  bool reco) const {
659  float x(0), y(0);
660  if (hgpar_->waferTypeT_[wafer] == 1) {
661  x = hgpar_->cellFineX_[cell];
662  y = hgpar_->cellFineY_[cell];
663  } else {
664  x = hgpar_->cellCoarseX_[cell];
665  y = hgpar_->cellCoarseY_[cell];
666  }
667  if (!reco) {
670  }
671  return std::make_pair(x,y);
672 }
673 
674 std::pair<float,float> HGCalDDDConstants::locateCellTrap(int lay, int irad,
675  int iphi, bool reco) const {
676 
677  float x(0), y(0);
678  const auto & indx = getIndex(lay,reco);
679  if (indx.first >= 0) {
680  int ir = std::abs(irad);
681  int type = hgpar_->scintType(lay);
682  double phi = (iphi-0.5)*indx.second;
683  double z = hgpar_->zLayerHex_[indx.first];
684  double r = 0.5*(hgpar_->radiusLayer_[type][ir-1] +
685  hgpar_->radiusLayer_[type][ir]);
686  std::pair<double,double> range = rangeR(z,true);
687  r = std::max(range.first, std::min(r,range.second));
688  x = r*std::cos(phi);
689  y = r*std::sin(phi);
690  if (irad < 0) x =-x;
691  }
692  if (!reco) {
695  }
696  return std::make_pair(x,y);
697 }
698 
699 bool HGCalDDDConstants::maskCell(const DetId& detId, int corners) const {
700  bool mask(false);
701  if (corners > 2 && corners < (int)(HGCalParameters::k_CornerSize)) {
704  int N(0), layer(0), waferU(0), waferV(0), u(0), v(0);
705  if (detId.det() == DetId::Forward) {
706  HFNoseDetId id(detId);
707  N = getUVMax(id.type());
708  layer = id.layer();
709  waferU = id.waferU();
710  waferV = id.waferV();
711  u = id.cellU();
712  v = id.cellV();
713  } else {
714  HGCSiliconDetId id(detId);
715  N = getUVMax(id.type());
716  layer = id.layer();
717  waferU = id.waferU();
718  waferV = id.waferV();
719  u = id.cellU();
720  v = id.cellV();
721  }
722  int wl = HGCalWaferIndex::waferIndex(layer,waferU,waferV);
723  auto itr = hgpar_->waferTypes_.find(wl);
724 #ifdef EDM_ML_DEBUG
725  edm::LogVerbatim("HGCalGeom") << "MaskCell: Layer " << layer
726  << " Wafer " << waferU << ":"
727  << waferV << " Index " << wl << ":"
728  << (itr != hgpar_->waferTypes_.end());
729 #endif
730  if (itr != hgpar_->waferTypes_.end()) {
731  int ncor = (itr->second).first;
732  int fcor = (itr->second).second;
733  if (ncor < corners) {
734  mask = true;
735  } else {
736  if (ncor == 4) {
737  switch (fcor) {
738  case(0): { mask = (v >= N); break; }
739  case(1): { mask = (u >= N); break; }
740  case(2): { mask = (u <= v); break; }
741  case(3): { mask = (v < N); break; }
742  case(4): { mask = (u < N); break; }
743  default: { mask = (u > v); break; }
744  }
745  } else {
746  switch (fcor) {
747  case(0): {
748  if (ncor == 3) {
749  mask = !((u > 2*v) && (v < N));
750  } else {
751  mask = ((u >= N) && (v >= N) && ((u+v) > (3*N-2)));
752  }
753  break;
754  }
755  case(1): {
756  if (ncor == 3) {
757  mask = !((u+v) < N);
758  } else {
759  mask = ((u >= N) && (u > v) && ((2*u-v) > 2*N));
760  }
761  break;
762  }
763  case(2): {
764  if (ncor == 3) {
765  mask = !((u < N) && (v > u) && (v > (2*u-1)));
766  } else {
767  mask = ((u > 2*v) && (v < N));
768  }
769  break;
770  }
771  case(3): {
772  if (ncor == 3) {
773  mask = !((v >= u) && ((2*v-u) > (2*N-2)));
774  } else {
775  mask = ((u+v) < N);
776  }
777  break;
778  }
779  case(4) : {
780  if (ncor == 3) {
781  mask = !((u >= N) && (v >= N) && ((u+v) > (3*N-2)));
782  } else {
783  mask = ((u < N) && (v > u) && (v > (2*u-1)));
784  }
785  break;
786  }
787  default: {
788  if (ncor == 3) {
789  mask = !((u >= N) && (u > v) && ((2*u-v) > 2*N));
790  } else {
791  mask = ((v >= u) && ((2*v-u) > (2*N-2)));
792  }
793  break;
794  }
795  }
796  }
797 #ifdef EDM_ML_DEBUG
798  edm::LogVerbatim("HGCalGeom") << "Corners: " << ncor << ":" << fcor
799  << " N " << N << " u " << u << " v "
800  << v << " Mask " << mask;
801 #endif
802  }
803  }
804  }
805  }
806  return mask;
807 }
808 
810 
811  int cells(0);
812  for (unsigned int i = 0; i<layers(reco); ++i) {
813  int lay = reco ? hgpar_->depth_[i] : hgpar_->layer_[i];
814  if (cells < maxCells(lay, reco)) cells = maxCells(lay, reco);
815  }
816  return cells;
817 }
818 
819 int HGCalDDDConstants::maxCells(int lay, bool reco) const {
820 
821  const auto & index = getIndex(lay, reco);
822  if (index.first < 0) return 0;
825  unsigned int cells(0);
826  for (unsigned int k=0; k<hgpar_->waferTypeT_.size(); ++k) {
827  if (waferInLayerTest(k,index.first,hgpar_->defineFull_)) {
828  unsigned int cell = (hgpar_->waferTypeT_[k]==1) ?
829  (hgpar_->cellFineX_.size()) : (hgpar_->cellCoarseX_.size());
830  if (cell > cells) cells = cell;
831  }
832  }
833  return (int)(cells);
834  } else if ((mode_ == HGCalGeometryMode::Hexagon8) ||
836  int cells(0);
837  for (unsigned int k=0; k<hgpar_->waferCopy_.size(); ++k) {
838  if (waferInLayerTest(k,index.first,hgpar_->defineFull_)) {
840  int type = ((itr == hgpar_->typesInLayers_.end()) ? 2 :
841  hgpar_->waferTypeL_[itr->second]);
842  int N = (type == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
843  cells = std::max(cells,3*N*N);
844  }
845  }
846  return cells;
847  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
848  return hgpar_->scintCells(index.first+hgpar_->firstLayer_);
849  } else {
850  return 0;
851  }
852 }
853 
854 int HGCalDDDConstants::maxRows(int lay, bool reco) const {
855 
856  int kymax(0);
857  const auto & index = getIndex(lay, reco);
858  int i = index.first;
859  if (i < 0) return kymax;
862  for (unsigned int k=0; k<hgpar_->waferCopy_.size(); ++k) {
864  int ky = ((hgpar_->waferCopy_[k])/100)%100;
865  if (ky > kymax) kymax = ky;
866  }
867  }
868  } else if ((mode_ == HGCalGeometryMode::Hexagon8) ||
870  kymax = 1+2*hgpar_->waferUVMaxLayer_[index.first];
871  }
872  return kymax;
873 }
874 
875 int HGCalDDDConstants::modifyUV(int uv, int type1, int type2) const {
876  // Modify u/v for transition of type1 to type2
877  return (((type1==type2) || (type1*type2 !=0)) ? uv :
878  ((type1==0) ? (2*uv+1)/3 : (3*uv)/2));
879 }
880 
881 int HGCalDDDConstants::modules(int lay, bool reco) const {
882  if (getIndex(lay,reco).first < 0) return 0;
883  else return max_modules_layer_[(int)reco][lay];
884 }
885 
886 int HGCalDDDConstants::modulesInit(int lay, bool reco) const {
887  int nmod(0);
888  const auto & index = getIndex(lay, reco);
889  if (index.first < 0) return nmod;
891  for (unsigned int k=0; k<hgpar_->waferPosX_.size(); ++k) {
892  if (waferInLayerTest(k,index.first,hgpar_->defineFull_)) ++nmod;
893  }
894  } else {
895  nmod = 1+hgpar_->lastModule_[index.first]-hgpar_->firstModule_[index.first];
896  }
897  return nmod;
898 }
899 
900 double HGCalDDDConstants::mouseBite(bool reco) const {
901 
903 }
904 
906 
907  int cells(0);
908  unsigned int nlayer = (reco) ? hgpar_->depth_.size() : hgpar_->layer_.size();
909  for (unsigned k=0; k<nlayer; ++k) {
910  std::vector<int> ncells = numberCells(((reco) ? hgpar_->depth_[k] : hgpar_->layer_[k]), reco);
911  cells = std::accumulate(ncells.begin(),ncells.end(),cells);
912  }
913  return cells;
914 }
915 
916 std::vector<int> HGCalDDDConstants::numberCells(int lay, bool reco) const {
917 
918  const auto & index = getIndex(lay, reco);
919  int i = index.first;
920  std::vector<int> ncell;
921  if (i >= 0) {
924  for (unsigned int k=0; k<hgpar_->waferTypeT_.size(); ++k) {
926  unsigned int cell = (hgpar_->waferTypeT_[k]==1) ?
927  (hgpar_->cellFineX_.size()) : (hgpar_->cellCoarseX_.size());
928  ncell.emplace_back((int)(cell));
929  }
930  }
931  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
932  int nphi = hgpar_->scintCells(lay);
933  for (int k=hgpar_->firstModule_[i]; k<=hgpar_->lastModule_[i]; ++k)
934  ncell.emplace_back(nphi);
935  } else {
936  for (unsigned int k=0; k<hgpar_->waferCopy_.size(); ++k) {
937  if (waferInLayerTest(k,index.first,hgpar_->defineFull_)) {
938  int cell = numberCellsHexagon(lay,
941  true);
942  ncell.emplace_back(cell);
943  }
944  }
945  }
946  }
947  return ncell;
948 }
949 
951 
952  if (wafer >= 0 && wafer < (int)(hgpar_->waferTypeT_.size())) {
953  if (hgpar_->waferTypeT_[wafer]==1)
954  return (int)(hgpar_->cellFineX_.size());
955  else
956  return (int)(hgpar_->cellCoarseX_.size());
957  } else {
958  return 0;
959  }
960 }
961 
962 int HGCalDDDConstants::numberCellsHexagon(int lay, int waferU, int waferV,
963  bool flag) const {
964  auto itr = hgpar_->typesInLayers_.find(HGCalWaferIndex::waferIndex(lay,waferU,waferV));
965  int type = ((itr == hgpar_->typesInLayers_.end()) ? 2 :
966  hgpar_->waferTypeL_[itr->second]);
967  int N = (type == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
968  if (flag) return (3*N*N);
969  else return N;
970 }
971 
972 std::pair<double,double> HGCalDDDConstants::rangeR(double z, bool reco) const {
973  double rmin(0), rmax(0), zz(0);
974  if (hgpar_->detectorType_ > 0) {
975  zz = (reco ? std::abs(z) : HGCalParameters::k_ScaleFromDDD*std::abs(z));
976  if (hgpar_->detectorType_ <= 2) {
979  } else {
984  }
985  if ((hgpar_->detectorType_ == 2) &&
986  (zz >= hgpar_->zLayerHex_[hgpar_->firstMixedLayer_ - 1])) {
991  } else {
994  }
995  }
996  if (!reco) {
999  }
1000 #ifdef EDM_ML_DEBUG
1001  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:rangeR: " << z << ":"
1002  << zz << " R " << rmin << ":" << rmax;
1003 #endif
1004  return std::pair<double,double>(rmin,rmax);
1005 }
1006 
1007 std::pair<double,double> HGCalDDDConstants::rangeZ(bool reco) const {
1008  double zmin = (hgpar_->zLayerHex_[0] - hgpar_->waferThick_);
1009  double zmax = (hgpar_->zLayerHex_[hgpar_->zLayerHex_.size()-1] +
1010  hgpar_->waferThick_);
1011 #ifdef EDM_ML_DEBUG
1012  edm::LogVerbatim("HGCalGeom") << "HGCalDDDConstants:rangeZ: " << zmin << ":"
1013  << zmax << ":" << hgpar_->waferThick_;
1014 #endif
1015  if (!reco) {
1018  }
1019  return std::pair<double,double>(zmin,zmax);
1020 }
1021 
1022 std::pair<int,int> HGCalDDDConstants::rowColumnWafer(int wafer) const {
1023  int row(0), col(0);
1024  if (wafer < (int)(hgpar_->waferCopy_.size())) {
1025  int copy = hgpar_->waferCopy_[wafer];
1026  col = copy%100;
1027  if ((copy/10000)%10 != 0) col = -col;
1028  row = (copy/100)%100;
1029  if ((copy/100000)%10 != 0) row = -row;
1030  }
1031  return std::make_pair(row,col);
1032 }
1033 
1034 std::pair<int,int> HGCalDDDConstants::simToReco(int cell, int lay, int mod,
1035  bool half) const {
1036 
1037  if ((mode_ != HGCalGeometryMode::Hexagon) &&
1039  return std::make_pair(cell,lay);
1040  } else {
1041  const auto & index = getIndex(lay, false);
1042  int i = index.first;
1043  if (i < 0) {
1044  edm::LogWarning("HGCalGeom") << "Wrong Layer # " << lay
1045  << " not in the list ***** ERROR *****";
1046  return std::make_pair(-1,-1);
1047  }
1048  if (mod >= (int)(hgpar_->waferTypeL_).size()) {
1049  edm::LogWarning("HGCalGeom") << "Invalid Wafer # " << mod
1050  << "should be < "
1051  << (hgpar_->waferTypeL_).size()
1052  << " ***** ERROR *****";
1053  return std::make_pair(-1,-1);
1054  }
1055  int depth(-1);
1056  int kx = cell;
1057  int type = hgpar_->waferTypeL_[mod];
1058  if (type == 1) {
1059  depth = hgpar_->layerGroup_[i];
1060  } else if (type == 2) {
1061  depth = hgpar_->layerGroupM_[i];
1062  } else {
1063  depth = hgpar_->layerGroupO_[i];
1064  }
1065  return std::make_pair(kx,depth);
1066  }
1067 }
1068 
1070  const int ncopies = hgpar_->waferCopy_.size();
1071  int wafer(ncopies);
1072  bool result(false);
1073  for (int k=0; k<ncopies; ++k) {
1074  if (copy == hgpar_->waferCopy_[k]) {
1075  wafer = k;
1076  result = true;
1077  break;
1078  }
1079  }
1080  if (!result) {
1081  wafer = -1;
1082 #ifdef EDM_ML_DEBUG
1083  edm::LogVerbatim("HGCalGeom") << "Cannot find " << copy << " in a list of "
1084  << ncopies << " members";
1085  for (int k=0; k<ncopies; ++k)
1086  edm::LogVerbatim("HGCalGeom") << "[" << k << "] " << hgpar_->waferCopy_[k];
1087 #endif
1088  }
1089  return wafer;
1090 }
1091 
1092 void HGCalDDDConstants::waferFromPosition(const double x, const double y,
1093  int& wafer, int& icell,
1094  int& celltyp) const {
1095  //Input x, y in Geant4 unit and transformed to CMSSW standard
1098  int size_ = (int)(hgpar_->waferCopy_.size());
1099  wafer = size_;
1100  for (int k=0; k<size_; ++k) {
1101  double dx = std::abs(xx-hgpar_->waferPosX_[k]);
1102  double dy = std::abs(yy-hgpar_->waferPosY_[k]);
1103  if (dx <= rmax_ && dy <= hexside_) {
1104  if ((dy <= 0.5*hexside_) || (dx*tan30deg_ <= (hexside_-dy))) {
1105  wafer = k;
1106  celltyp = hgpar_->waferTypeT_[k];
1107  xx -= hgpar_->waferPosX_[k];
1108  yy -= hgpar_->waferPosY_[k];
1109  break;
1110  }
1111  }
1112  }
1113  if (wafer < size_) {
1114  if (celltyp == 1)
1115  icell = cellHex(xx, yy, 0.5*HGCalParameters::k_ScaleFromDDD*hgpar_->cellSize_[0],
1117  else
1118  icell = cellHex(xx, yy, 0.5*HGCalParameters::k_ScaleFromDDD*hgpar_->cellSize_[1],
1120  } else {
1121  wafer = -1;
1122 #ifdef EDM_ML_DEBUG
1123  edm::LogWarning("HGCalGeom") << "Cannot get wafer type corresponding to "
1124  << x << ":" << y << " " << xx << ":" << yy;
1125 #endif
1126  }
1127 #ifdef EDM_ML_DEBUG
1128  edm::LogVerbatim("HGCalGeom") << "Position " << x << ":" << y << " Wafer "
1129  << wafer << ":" << size_ << " XX " << xx << ":"
1130  << yy << " Cell " << icell << " Type " << celltyp;
1131 #endif
1132 }
1133 
1134 void HGCalDDDConstants::waferFromPosition(const double x, const double y,
1135  const int layer, int& waferU,
1136  int& waferV, int& cellU, int& cellV,
1137  int& celltype, double& wt, bool
1138 #ifdef EDM_ML_DEBUG
1139  debug
1140 #endif
1141  ) const {
1142 
1145  waferU = waferV = 1+hgpar_->waferUVMax_;
1146  for (unsigned int k=0; k<hgpar_->waferPosX_.size(); ++k) {
1147  double dx = std::abs(xx-hgpar_->waferPosX_[k]);
1148  double dy = std::abs(yy-hgpar_->waferPosY_[k]);
1149  if (dx <= rmax_ && dy <= hexside_) {
1150  if ((dy <= 0.5*hexside_) || (dx*tan30deg_ <= (hexside_-dy))) {
1153  auto itr = hgpar_->typesInLayers_.find(HGCalWaferIndex::waferIndex(layer,waferU,waferV));
1154  celltype = ((itr == hgpar_->typesInLayers_.end()) ? 2 :
1155  hgpar_->waferTypeL_[itr->second]);
1156 #ifdef EDM_ML_DEBUG
1157  if (debug)
1158  edm::LogVerbatim("HGCalGeom") << "WaferFromPosition:: Input " << xx
1159  << ":" << yy << " compared with "
1160  << hgpar_->waferPosX_[k] << ":"
1161  << hgpar_->waferPosY_[k]
1162  << " difference " << dx << ":" << dy
1163  << ":" << dx*tan30deg_ << ":"
1164  << (hexside_-dy) << " comparator "
1165  << rmax_ << ":" << hexside_ <<" wafer "
1166  << waferU << ":" << waferV << ":"
1167  << celltype;
1168 #endif
1169  xx -= hgpar_->waferPosX_[k];
1170  yy -= hgpar_->waferPosY_[k];
1171  break;
1172  }
1173  }
1174  }
1175  if (std::abs(waferU) <= hgpar_->waferUVMax_) {
1176  cellHex(xx, yy, celltype, cellU, cellV
1177 #ifdef EDM_ML_DEBUG
1178  , debug
1179 #endif
1180  );
1181  wt = ((celltype < 2) ?
1182  (hgpar_->cellThickness_[celltype]/hgpar_->waferThick_) : 1.0);
1183  } else {
1184  cellU = cellV = 2*hgpar_->nCellsFine_;
1185  wt = 1.0;
1186  celltype =-1;
1187  }
1188 #ifdef EDM_ML_DEBUG
1189  if (celltype < 0) {
1191  double y1(HGCalParameters::k_ScaleFromDDD*y);
1192  edm::LogVerbatim("HGCalGeom") << "waferFromPosition: Bad type for X "
1193  << x << ":" << x1 << ":" << xx << " Y " << y
1194  << ":" << y1 << ":" << yy << " Wafer "
1195  << waferU << ":" << waferV << " Cell "
1196  << cellU << ":" << cellV;
1197  for (unsigned int k=0; k<hgpar_->waferPosX_.size(); ++k) {
1198  double dx = std::abs(x1-hgpar_->waferPosX_[k]);
1199  double dy = std::abs(y1-hgpar_->waferPosY_[k]);
1200  edm::LogVerbatim("HGCalGeom") << "Wafer [" << k << "] Position ("
1201  << hgpar_->waferPosX_[k] << ", "
1202  << hgpar_->waferPosY_[k] << ") difference "
1203  << dx << ":" << dy << ":" << dx*tan30deg_
1204  << ":" << hexside_-dy << " Paramerers "
1205  << rmax_ << ":" << hexside_;
1206  }
1207  }
1208 #endif
1209 }
1210 
1211 bool HGCalDDDConstants::waferInLayer(int wafer, int lay, bool reco) const {
1212 
1213  const auto & indx = getIndex(lay, reco);
1214  if (indx.first < 0) return false;
1215  return waferInLayerTest(wafer,indx.first,hgpar_->defineFull_);
1216 }
1217 
1218 bool HGCalDDDConstants::waferFullInLayer(int wafer, int lay, bool reco) const {
1219  const auto & indx = getIndex(lay, reco);
1220  if (indx.first < 0) return false;
1221  return waferInLayerTest(wafer,indx.first,false);
1222 }
1223 
1224 std::pair<double,double> HGCalDDDConstants::waferPosition(int wafer,
1225  bool reco) const {
1226 
1227  double xx(0), yy(0);
1228  if (wafer >= 0 && wafer < (int)(hgpar_->waferPosX_.size())) {
1229  xx = hgpar_->waferPosX_[wafer];
1230  yy = hgpar_->waferPosY_[wafer];
1231  }
1232  if (!reco) {
1235  }
1236  return std::make_pair(xx,yy);
1237 }
1238 
1239 std::pair<double,double> HGCalDDDConstants::waferPosition(int waferU,
1240  int waferV,
1241  bool reco) const {
1242 
1243  double xx(0), yy(0);
1244  int indx = HGCalWaferIndex::waferIndex(0,waferU,waferV);
1245  auto itr = hgpar_->wafersInLayers_.find(indx);
1246  if (itr != hgpar_->wafersInLayers_.end()) {
1247  xx = hgpar_->waferPosX_[itr->second];
1248  yy = hgpar_->waferPosY_[itr->second];
1249  }
1250  if (!reco) {
1253  }
1254  return std::make_pair(xx,yy);
1255 }
1256 
1257 int HGCalDDDConstants::waferType(DetId const& id) const {
1258 
1259  int type(1);
1262  type = ((id.det() != DetId::Forward) ? (1 + HGCSiliconDetId(id).type()) :
1263  (1 + HFNoseDetId(id).type()));
1264  } else if ((mode_ == HGCalGeometryMode::Hexagon) ||
1266  type = waferTypeL(HGCalDetId(id).wafer());
1267  }
1268  return type;
1269 }
1270 
1271 bool HGCalDDDConstants::waferVirtual(int layer, int waferU, int waferV) const {
1272  bool type(false);
1275  int wl = HGCalWaferIndex::waferIndex(layer,waferU,waferV,false);
1276  type = (hgpar_->waferTypes_.find(wl) != hgpar_->waferTypes_.end());
1277  } else if ((mode_ == HGCalGeometryMode::Hexagon) ||
1279  int wl = HGCalWaferIndex::waferIndex(layer,waferU,0,true);
1280  type = (hgpar_->waferTypes_.find(wl) != hgpar_->waferTypes_.end());
1281  }
1282  return type;
1283 }
1284 
1285 double HGCalDDDConstants::waferZ(int lay, bool reco) const {
1286  const auto & index = getIndex(lay, reco);
1287  if (index.first < 0) return 0;
1288  else return (reco ? hgpar_->zLayerHex_[index.first] :
1290 }
1291 
1293 
1294  int wafer(0);
1296  for (unsigned int i = 0; i<layers(true); ++i) {
1297  int lay = hgpar_->depth_[i];
1298  wafer += modules(lay, true);
1299  }
1300  } else {
1301  wafer = (int)(hgpar_->moduleLayR_.size());
1302  }
1303  return wafer;
1304 }
1305 
1306 int HGCalDDDConstants::wafers(int layer, int type) const {
1307 
1308  int wafer(0);
1310  auto itr = waferLayer_.find(layer);
1311  if (itr != waferLayer_.end()) {
1312  unsigned ity = (type > 0 && type <= 2) ? type : 0;
1313  wafer = (itr->second)[ity];
1314  }
1315  } else {
1316  const auto & index = getIndex(layer, true);
1317  wafer = 1+hgpar_->lastModule_[index.first]-hgpar_->firstModule_[index.first];
1318  }
1319  return wafer;
1320 }
1321 
1322 int HGCalDDDConstants::cellHex(double xx, double yy,
1323  const double& cellR,
1324  const std::vector<double>& posX,
1325  const std::vector<double>& posY) const {
1326  int num(0);
1327  const double tol(0.00001);
1328  double cellY = 2.0*cellR*tan30deg_;
1329  for (unsigned int k=0; k<posX.size(); ++k) {
1330  double dx = std::abs(xx - posX[k]);
1331  double dy = std::abs(yy - posY[k]);
1332  if (dx <= (cellR+tol) && dy <= (cellY+tol)) {
1333  double xmax = (dy<=0.5*cellY) ? cellR : (cellR-(dy-0.5*cellY)/tan30deg_);
1334  if (dx <= (xmax+tol)) {
1335  num = k;
1336  break;
1337  }
1338  }
1339  }
1340  return num;
1341 }
1342 
1343 void HGCalDDDConstants::cellHex(double xloc, double yloc, int cellType,
1344  int& cellU, int& cellV, bool
1345 #ifdef EDM_ML_DEBUG
1346  debug
1347 #endif
1348  ) const {
1349  int N = (cellType == 0) ? hgpar_->nCellsFine_ : hgpar_->nCellsCoarse_;
1350  double Rc = 2*rmax_/(3*N);
1351  double rc = 0.5*Rc*sqrt3_;
1352  double v0 = ((xloc/Rc -1.0)/1.5);
1353  int cv0 = (v0 > 0) ? (N + (int)(v0+0.5)) : (N - (int)(-v0+0.5));
1354  double u0 = (0.5*yloc/rc+0.5*cv0);
1355  int cu0 = (u0 > 0) ? (N/2 + (int)(u0+0.5)) : (N/2 - (int)(-u0+0.5));
1356  cu0 = std::max(0,std::min(cu0,2*N-1));
1357  cv0 = std::max(0,std::min(cv0,2*N-1));
1358  if (cv0-cu0 >= N) cv0 = cu0+N-1;
1359 #ifdef EDM_ML_DEBUG
1360  if (debug)
1361  edm::LogVerbatim("HGCalGeom") << "cellHex: input " << xloc << ":"
1362  << yloc << ":" << cellType << " parameter "
1363  << rc << ":" << Rc << " u0 " << u0 << ":"
1364  << cu0 << " v0 " << v0 << ":" << cv0;
1365 #endif
1366  bool found(false);
1367  static const int shift[3] = {0,1,-1};
1368  for (int i1=0; i1<3; ++i1) {
1369  cellU = cu0 + shift[i1];
1370  for (int i2=0; i2<3; ++i2) {
1371  cellV = cv0 + shift[i2];
1372  if (((cellV-cellU) < N) && ((cellU-cellV) <= N) && (cellU >= 0) &&
1373  (cellV >= 0) && (cellU < 2*N) && (cellV < 2*N)) {
1374  double xc = (1.5*(cellV-N)+1.0)*Rc;
1375  double yc = (2*cellU-cellV-N)*rc;
1376  if ((std::abs(yloc-yc) <= rc) && (std::abs(xloc-xc) <= Rc) &&
1377  ((std::abs(xloc-xc) <= 0.5*Rc) ||
1378  (std::abs(yloc-yc) <= sqrt3_*(Rc-std::abs(xloc-xc))))) {
1379 #ifdef EDM_ML_DEBUG
1380  if (debug)
1381  edm::LogVerbatim("HGCalGeom") << "cellHex: local " << xc << ":"
1382  << yc << " difference "
1383  << std::abs(xloc-xc) << ":"
1384  << std::abs(yloc-yc) << ":"
1385  << sqrt3_*(Rc-std::abs(yloc-yc))
1386  << " comparator " << rc << ":" << Rc
1387  << " (u,v) = (" << cellU << ","
1388  << cellV << ")";
1389 #endif
1390  found = true; break;
1391  }
1392  }
1393  }
1394  if (found) break;
1395  }
1396  if (!found) { cellU = cu0; cellV = cv0; }
1397 }
1398 
1399 std::pair<int,float> HGCalDDDConstants::getIndex(int lay, bool reco) const {
1400 
1401  int indx = layerIndex(lay,reco);
1402  if (indx<0) return std::make_pair(-1,0);
1403  float cell(0);
1404  if ((mode_ == HGCalGeometryMode::Hexagon) ||
1406  cell = (reco ? hgpar_->moduleCellR_[0] : hgpar_->moduleCellS_[0]);
1407  } else {
1410  cell = (reco ? hgpar_->moduleCellR_[0] : hgpar_->moduleCellS_[0]);
1411  } else {
1412  cell = hgpar_->scintCellSize(lay);
1413  }
1414  }
1415  return std::make_pair(indx,cell);
1416 }
1417 
1418 bool HGCalDDDConstants::isValidCell(int lay, int wafer, int cell) const {
1419 
1420  // Calculate the position of the cell
1421  // Works for options HGCalHexagon/HGCalHexagonFull
1422  double x = hgpar_->waferPosX_[wafer];
1423  double y = hgpar_->waferPosY_[wafer];
1424  if (hgpar_->waferTypeT_[wafer] == 1) {
1425  x += hgpar_->cellFineX_[cell];
1426  y += hgpar_->cellFineY_[cell];
1427  } else {
1428  x += hgpar_->cellCoarseX_[cell];
1429  y += hgpar_->cellCoarseY_[cell];
1430  }
1431  double rr = sqrt(x*x+y*y);
1432  bool result = ((rr >= hgpar_->rMinLayHex_[lay-1]) &&
1433  (rr <= hgpar_->rMaxLayHex_[lay-1]) &&
1434  (wafer < (int)(hgpar_->waferPosX_.size())));
1435 #ifdef EDM_ML_DEBUG
1436  if (!result)
1437  edm::LogVerbatim("HGCalGeom") << "Input " << lay << ":" << wafer << ":"
1438  << cell << " Position " << x << ":" << y
1439  << ":" << rr << " Compare Limits "
1440  << hgpar_->rMinLayHex_[lay-1] << ":"
1441  << hgpar_->rMaxLayHex_[lay-1]
1442  << " Flag " << result;
1443 #endif
1444  return result;
1445 }
1446 
1447 bool HGCalDDDConstants::waferInLayerTest(int wafer, int lay, bool full) const {
1448 
1449  const double waferX = hgpar_->waferPosX_[wafer];
1450  const double waferY = hgpar_->waferPosY_[wafer];
1452  xc[0] = waferX; yc[0] = waferY+hexside_;
1453  xc[1] = waferX-rmax_; yc[1] = waferY+0.5*hexside_;
1454  if ((mode_ == HGCalGeometryMode::Hexagon) ||
1455  (mode_ == HGCalGeometryMode::HexagonFull)) { // Till bug in l1 fixed
1456  xc[2] = waferX+rmax_; yc[2] = waferY-0.5*hexside_;
1457  } else {
1458  xc[2] = waferX-rmax_; yc[2] = waferY-0.5*hexside_;
1459  }
1460  xc[3] = waferX; yc[3] = waferY-hexside_;
1461  xc[4] = waferX+rmax_; yc[4] = waferY-0.5*hexside_;
1462  xc[5] = waferX+rmax_; yc[5] = waferY+0.5*hexside_;
1463  bool cornerOne(false), cornerAll(true);
1464  for (unsigned int k=0; k<HGCalParameters::k_CornerSize; ++k) {
1465  double rpos = std::sqrt(xc[k]*xc[k]+yc[k]*yc[k]);
1466  if ((rpos >= hgpar_->rMinLayHex_[lay]) &&
1467  (rpos <= hgpar_->rMaxLayHex_[lay])) cornerOne = true;
1468  else cornerAll = false;
1469  }
1470  bool in = full ? cornerOne : cornerAll;
1471 #ifdef EDM_ML_DEBUG
1472  edm::LogVerbatim("HGCalGeom") << "WaferInLayerTest: Layer " << lay
1473  << " wafer " << wafer << " R-limits "
1474  << hgpar_->rMinLayHex_[lay] << ":"
1475  << hgpar_->rMaxLayHex_[lay] << " Corners "
1476  << cornerOne << ":" << cornerAll << " In "
1477  << in;
1478 #endif
1479  return in;
1480 }
1481 
1483 
std::vector< int > iradMaxBH_
size
Write out results.
bool isHalfCell(int waferType, int cell) const
std::vector< double > waferPosY_
type
Definition: HCALResponse.h:21
std::vector< int > layer_
std::vector< int > depthLayerF_
bool isValidTrap(int lay, int ieta, int iphi) const
std::vector< int > depth_
std::vector< double > zFrontMin_
std::vector< double > moduleCellR_
int getTypeTrap(int layer) const
wafer_map cellFineIndex_
layer_map copiesInLayers_
int getLayer(double z, bool reco) const
std::vector< HGCalParameters::hgtrap > getModules() const
std::vector< bool > cellCoarseHalf_
std::vector< bool > cellFineHalf_
std::array< uint32_t, 2 > tot_layers_
def copy(args, dbName)
int scintType(const int layer) const
std::map< int, HGCWaferParam > waferLayer_
void waferFromPosition(const double x, const double y, int &wafer, int &icell, int &celltyp) const
int type() const
get the type
Definition: HFNoseDetId.h:52
std::vector< int > moduleLayR_
double cellSizeHex(int type) const
HGCalParameters::hgtrform getTrForm(unsigned int k) const
Simrecovecs max_modules_layer_
int lastLayer(bool reco) const
double cellThickness(int layer, int waferU, int waferV) const
static double radius(double z, std::vector< double > const &zFront, std::vector< double > const &rFront, std::vector< double > const &slope)
unsigned int layersInit(bool reco) const
static int32_t waferV(const int32_t index)
std::array< int, 4 > waferMax_
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
bool cellInLayer(int waferU, int waferV, int cellU, int cellV, int lay, bool reco) const
int maxRows(int lay, bool reco) const
int getTypeHex(int layer, int waferU, int waferV) const
std::vector< double > cellFineY_
bool waferInLayerTest(int wafer, int lay, bool full) const
int modulesInit(int lay, bool reco) const
std::pair< float, float > locateCell(int cell, int lay, int type, bool reco) const
std::vector< uint32_t > trformIndex_
bool isValidHex(int lay, int mod, int cell, bool reco) const
std::vector< int > layerGroupM_
HGCalGeometryMode::GeometryMode mode_
HGCalGeometryMode::GeometryMode mode_
double scintCellSize(const int layer) const
int cellHex(double xx, double yy, const double &cellR, const std::vector< double > &posX, const std::vector< double > &posY) const
int layerIndex(int lay, bool reco) const
wafer_map wafersInLayers_
std::pair< float, float > locateCellHex(int cell, int wafer, bool reco) const
U second(std::pair< T, U > const &p)
std::vector< double > cellCoarseX_
std::pair< double, double > rangeR(double z, bool reco) const
int modules(int lay, bool reco) const
std::vector< int > firstModule_
std::pair< int, int > simToReco(int cell, int layer, int mod, bool half) const
unsigned int getTrFormN() const
double distFromEdgeTrap(double x, double y, double z) const
std::pair< double, double > rangeZ(bool reco) const
unsigned int layers(bool reco) const
bool isValidHex8(int lay, int modU, int modV, int cellU, int cellV) const
int numberCellsHexagon(int wafer) const
std::vector< double > cellSize_
std::vector< int > waferUVMaxLayer_
std::vector< HGCalParameters::hgtrform > getTrForms() const
std::vector< int > layerIndex_
double mouseBite(bool reco) const
susybsm::HSCParticleRefProd hp
Definition: classes.h:27
hgtrform getTrForm(unsigned int k) const
int type() const
get the type
T sqrt(T t)
Definition: SSEVec.h:18
std::pair< int, float > getIndex(int lay, bool reco) const
static double k_ScaleFromDDD
HGCalDDDConstants(const HGCalParameters *hp, const std::string &name)
std::pair< float, float > locateCellTrap(int lay, int ieta, int iphi, bool reco) const
std::vector< double > rMaxFront_
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Definition: GenABIO.cc:180
int scintCells(const int layer) const
hgtrap getModule(unsigned int k, bool reco) const
CellType cellType(int type, int waferU, int waferV) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::unordered_map< int32_t, bool > waferIn_
std::vector< double > slopeTop_
static int32_t waferU(const int32_t index)
bool isValidCell(int layindex, int wafer, int cell) const
int waferTypeL(int wafer) const
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< double > rMinLayHex_
static int32_t waferIndex(int32_t layer, int32_t waferU, int32_t waferV, bool old=false)
std::pair< int, int > rowColumnWafer(const int wafer) const
std::vector< double > zLayerHex_
double waferZ(int layer, bool reco) const
#define M_PI
waferT_map waferTypes_
int k[5][pyjets_maxn]
#define TYPELOOKUP_DATA_REG(_dataclass_)
Definition: typelookup.h:96
int waferType(DetId const &id) const
bool waferVirtual(int layer, int waferU, int waferV) const
static const int maxType
std::vector< double > rMaxLayHex_
static uint32_t k_CornerSize
std::vector< double > slopeMin_
Definition: DetId.h:18
std::vector< int > lastModule_
int waferFromCopy(int copy) const
std::vector< double > radiusMixBoundary_
#define debug
Definition: HDRShower.cc:19
#define N
Definition: blowfish.cc:9
std::vector< double > cellThickness_
std::vector< int > layerGroup_
std::vector< double > moduleCellS_
static double k_ScaleToDDD
bool maskCell(const DetId &id, int corners) const
int numberCells(bool reco) const
wafer_map cellCoarseIndex_
bool waferFullInLayer(int wafer, int lay, bool reco) const
std::vector< double > rMinFront_
std::vector< int > iradMinBH_
std::vector< double > cellFineX_
wafer_map typesInLayers_
std::vector< int > layerGroupO_
static const int minType
fixed size matrix
std::array< int, 5 > assignCellHex(float x, float y, int lay, bool reco) const
std::vector< int > waferCopy_
col
Definition: cuy.py:1010
#define EDM_ML_DEBUG
static unsigned int const shift
std::vector< int > depthIndex_
std::pair< double, double > waferPosition(int wafer, bool reco) const
std::pair< int, int > assignCell(float x, float y, int lay, int subSec, bool reco) const
std::array< int, 3 > assignCellTrap(float x, float y, float z, int lay, bool reco) const
std::vector< double > zFrontTop_
std::vector< double > radiusLayer_[2]
std::vector< int > waferTypeT_
std::vector< double > cellCoarseY_
HGCalParameters::hgtrap getModule(unsigned int k, bool hexType, bool reco) const
const HGCalParameters * hgpar_
int maxCells(bool reco) const
std::vector< double > waferPosX_
int modifyUV(int uv, int type1, int type2) const
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
std::vector< int > waferTypeL_
int getUVMax(int type) const
static double tan30deg_
double distFromEdgeHex(double x, double y, double z) const
std::array< int, 3 > HGCWaferParam
bool waferInLayer(int wafer, int lay, bool reco) const
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:39