Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:14:20

0001 #include "DataFormats/ForwardDetId/interface/ForwardSubdetector.h"
0002 #include "DataFormats/ForwardDetId/interface/HFNoseDetId.h"
0003 #include "DataFormats/ForwardDetId/interface/HGCalDetId.h"
0004 #include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
0005 #include "DataFormats/ForwardDetId/interface/HGCScintillatorDetId.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "Geometry/HGCalCommonData/interface/HGCalCell.h"
0008 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0009 #include "Geometry/CaloTopology/interface/HGCalTopology.h"
0010 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
0011 
0012 //#define EDM_ML_DEBUG
0013 
0014 HGCalTopology::HGCalTopology(const HGCalDDDConstants& hdcons, int det) : hdcons_(hdcons) {
0015   sectors_ = hdcons_.sectors();
0016   layers_ = hdcons_.layers(true);
0017   cells_ = hdcons_.maxCells(true);
0018   mode_ = hdcons_.geomMode();
0019   cellMax_ = hdcons_.maxCellUV();
0020   waferOff_ = hdcons_.waferUVMax();
0021   waferMax_ = 2 * waferOff_ + 1;
0022   kHGhalf_ = sectors_ * layers_ * cells_;
0023   firstLay_ = hdcons_.firstLayer();
0024   if (waferHexagon6()) {
0025     det_ = DetId::Forward;
0026     subdet_ = (ForwardSubdetector)(det);
0027     kHGeomHalf_ = sectors_ * layers_;
0028     types_ = 2;
0029   } else if (det == static_cast<int>(DetId::Forward)) {
0030     det_ = DetId::Forward;
0031     subdet_ = HFNose;
0032     kHGeomHalf_ = sectors_ * layers_;
0033     types_ = 3;
0034   } else if (tileTrapezoid()) {
0035     det_ = (DetId::Detector)(det);
0036     subdet_ = ForwardEmpty;
0037     kHGeomHalf_ = sectors_ * layers_ * cellMax_;
0038     types_ = 3;
0039   } else {
0040     det_ = (DetId::Detector)(det);
0041     subdet_ = ForwardEmpty;
0042     kHGeomHalf_ = sectors_ * layers_;
0043     types_ = 3;
0044   }
0045   kHGhalfType_ = sectors_ * layers_ * cells_ * types_;
0046   kSizeForDenseIndexing = static_cast<unsigned int>(2 * kHGhalf_);
0047 #ifdef EDM_ML_DEBUG
0048   edm::LogVerbatim("HGCalGeom") << "HGCalTopology initialized for detector " << det << ":" << det_ << ":" << subdet_
0049                                 << " having " << sectors_ << " Sectors, " << layers_ << " Layers from " << firstLay_
0050                                 << ", " << cells_ << " cells and total channels " << kSizeForDenseIndexing << ":"
0051                                 << (2 * kHGeomHalf_);
0052 #endif
0053 }
0054 
0055 unsigned int HGCalTopology::allGeomModules() const {
0056   return (tileTrapezoid() ? (unsigned int)(2 * hdcons_.numberCells(true)) : (unsigned int)(2 * hdcons_.wafers()));
0057 }
0058 
0059 std::vector<DetId> HGCalTopology::neighbors(const DetId& idin) const {
0060   std::vector<DetId> ids;
0061   HGCalTopology::DecodedDetId id = decode(idin);
0062   if (waferHexagon8()) {
0063     int celltype = hdcons_.cellType(
0064         id.iType, id.iCell1, id.iCell2, id.zSide, 1, -1);  // Temporary fix - later for v17 define fwd back and orient
0065 #ifdef EDM_ML_DEBUG
0066     edm::LogVerbatim("HGCalGeom") << "Type:WaferU:WaferV " << id.iType << ":" << id.iCell1 << ":" << id.iCell2
0067                                   << " CellType " << celltype;
0068 #endif
0069     switch (celltype) {
0070       case (HGCalCell::centralCell): {
0071         // cell within the wafer
0072 #ifdef EDM_ML_DEBUG
0073         edm::LogVerbatim("HGCalGeom") << "Cell Type 0";
0074 #endif
0075         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2);
0076         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 - 1);
0077         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2 - 1);
0078         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2);
0079         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 + 1);
0080         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2 + 1);
0081         break;
0082       }
0083       case (HGCalCell::bottomLeftEdge): {
0084         // bottom left edge
0085         int wu1(id.iSec1), wv1(id.iSec2 - 1);
0086         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0087         int N1 = hdcons_.getUVMax(t1);
0088         int v1 = hdcons_.modifyUV(id.iCell2, id.iType, t1);
0089 #ifdef EDM_ML_DEBUG
0090         edm::LogVerbatim("HGCalGeom") << "Cell Type 1 "
0091                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << v1;
0092 #endif
0093         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2);
0094         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 - 1);
0095         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, 2 * N1 - 1, v1 + N1 - 1);
0096         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, 2 * N1 - 1, v1 + N1);
0097         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 + 1);
0098         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2 + 1);
0099         break;
0100       }
0101       case (HGCalCell::leftEdge): {
0102         // left edege
0103         int wu1(id.iSec1 + 1), wv1(id.iSec2);
0104         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0105         int N1 = hdcons_.getUVMax(t1);
0106         int u1 = hdcons_.modifyUV(id.iCell1, id.iType, t1);
0107 #ifdef EDM_ML_DEBUG
0108         edm::LogVerbatim("HGCalGeom") << "Cell Type 2 "
0109                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << u1;
0110 #endif
0111         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2);
0112         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, u1 + N1, 2 * N1 - 1);
0113         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, u1 + N1 - 1, 2 * N1 - 1);
0114         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2);
0115         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 + 1);
0116         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2 + 1);
0117         break;
0118       }
0119       case (HGCalCell::topLeftEdge): {
0120         // top left edge
0121         int wu1(id.iSec1 + 1), wv1(id.iSec2 + 1);
0122         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0123         int N1 = hdcons_.getUVMax(t1);
0124         int v1 = hdcons_.modifyUV(id.iCell2, id.iType, t1);
0125 #ifdef EDM_ML_DEBUG
0126         edm::LogVerbatim("HGCalGeom") << "Cell Type 3 "
0127                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << v1;
0128 #endif
0129         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, v1 + 1, v1 + N1);
0130         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, v1, v1 + N1 - 1);
0131         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2 - 1);
0132         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2);
0133         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 + 1);
0134         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2 + 1);
0135         break;
0136       }
0137       case (HGCalCell::topRightEdge): {
0138         // top right edge
0139         int wu1(id.iSec1), wv1(id.iSec2 + 1);
0140         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0141         int N1 = hdcons_.getUVMax(t1);
0142         int v1 = hdcons_.modifyUV(id.iCell2, id.iType, t1);
0143 #ifdef EDM_ML_DEBUG
0144         edm::LogVerbatim("HGCalGeom") << "Cell Type 4 "
0145                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << v1;
0146 #endif
0147         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, 0, v1 - N1);
0148         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 - 1);
0149         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2 - 1);
0150         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2);
0151         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 + 1);
0152         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, 0, v1 - N1 + 1);
0153         break;
0154       }
0155       case (HGCalCell::rightEdge): {
0156         // right edge
0157         int wu1(id.iSec1 - 1), wv1(id.iSec2);
0158         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0159         int N1 = hdcons_.getUVMax(t1);
0160         int u1 = hdcons_.modifyUV(id.iCell1, id.iType, t1);
0161 #ifdef EDM_ML_DEBUG
0162         edm::LogVerbatim("HGCalGeom") << "Cell Type 5 "
0163                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << u1;
0164 #endif
0165         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2);
0166         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 - 1);
0167         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2 - 1);
0168         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2);
0169         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, u1 - N1, 0);
0170         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, u1 - N1 + 1, 0);
0171         break;
0172       }
0173       case (HGCalCell::bottomRightEdge): {
0174         // bottom right edge
0175         int wu1(id.iSec1 - 1), wv1(id.iSec2 - 1);
0176         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0177         int N1 = hdcons_.getUVMax(t1);
0178         int u1 = hdcons_.modifyUV(id.iCell1, id.iType, t1);
0179 #ifdef EDM_ML_DEBUG
0180         edm::LogVerbatim("HGCalGeom") << "Cell Type 6 "
0181                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << u1;
0182 #endif
0183         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2);
0184         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 - 1);
0185         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2 - 1);
0186         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, u1 + N1 - 1, u1 - 1);
0187         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, u1 + N1, u1);
0188         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2 + 1);
0189         break;
0190       }
0191       case (HGCalCell::bottomCorner): {
0192         // bottom corner
0193         int wu1(id.iSec1), wv1(id.iSec2 - 1);
0194         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0195         int N1 = hdcons_.getUVMax(t1);
0196         int v1 = hdcons_.modifyUV(id.iCell2, id.iType, t1);
0197         int wu2(id.iSec1 - 1), wv2(id.iSec2 - 1);
0198         int t2 = hdcons_.getTypeHex(id.iLay, wu2, wv2);
0199         int N2 = hdcons_.getUVMax(t2);
0200         int u2 = hdcons_.modifyUV(id.iCell1, id.iType, t2);
0201 #ifdef EDM_ML_DEBUG
0202         edm::LogVerbatim("HGCalGeom") << "Cell Type 11 "
0203                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << v1 << ":" << t2
0204                                       << ":" << N2 << ":" << u2;
0205 #endif
0206         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2);
0207         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 - 1);
0208         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, 2 * N1 - 1, v1 + N1 - 1);
0209         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, 2 * N1 - 1, v1 + N1);
0210         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, u2 + N2, u2);
0211         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2 + 1);
0212         break;
0213       }
0214       case (HGCalCell::bottomLeftCorner): {
0215         // bottom left corner
0216         int wu1(id.iSec1 + 1), wv1(id.iSec2);
0217         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0218         int N1 = hdcons_.getUVMax(t1);
0219         int u1 = hdcons_.modifyUV(id.iCell1, id.iType, t1);
0220         int wu2(id.iSec1), wv2(id.iSec2 - 1);
0221         int t2 = hdcons_.getTypeHex(id.iLay, wu2, wv2);
0222         int N2 = hdcons_.getUVMax(t2);
0223         int v2 = hdcons_.modifyUV(id.iCell2, id.iType, t2);
0224 #ifdef EDM_ML_DEBUG
0225         edm::LogVerbatim("HGCalGeom") << "Cell Type 12 "
0226                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << u1 << ":" << t2
0227                                       << ":" << N2 << ":" << v2;
0228 #endif
0229         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2);
0230         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, u1 + N1, 2 * N1 - 1);
0231         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, 2 * N2 - 1, v2 + N2 - 1);
0232         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, 2 * N2 - 1, v2 + N2);
0233         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 + 1);
0234         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2 + 1);
0235         break;
0236       }
0237       case (HGCalCell::topLeftCorner): {
0238         // top left corner
0239         int wu1(id.iSec1 + 1), wv1(id.iSec2 + 1);
0240         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0241         int N1 = hdcons_.getUVMax(t1);
0242         int v1 = hdcons_.modifyUV(id.iCell2, id.iType, t1);
0243         int wu2(id.iSec1 + 1), wv2(id.iSec2);
0244         int t2 = hdcons_.getTypeHex(id.iLay, wu2, wv2);
0245         int N2 = hdcons_.getUVMax(t2);
0246         int u2 = hdcons_.modifyUV(id.iCell1, id.iType, t2);
0247 #ifdef EDM_ML_DEBUG
0248         edm::LogVerbatim("HGCalGeom") << "Cell Type 13 "
0249                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << v1 << ":" << t2
0250                                       << ":" << N2 << ":" << u2;
0251 #endif
0252         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, v1 + 1, N1 + v1);
0253         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, v1, N1 + v1 - 1);
0254         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, u2 + N2 - 1, 2 * N2 - 1);
0255         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2);
0256         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 + 1);
0257         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2 + 1);
0258         break;
0259       }
0260       case (HGCalCell::topCorner): {
0261         // top corner
0262         int wu1(id.iSec1 + 1), wv1(id.iSec2 + 1);
0263         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0264         int N1 = hdcons_.getUVMax(t1);
0265         int v1 = hdcons_.modifyUV(id.iCell2, id.iType, t1);
0266         int wu2(id.iSec1), wv2(id.iSec2 + 1);
0267         int t2 = hdcons_.getTypeHex(id.iLay, wu2, wv2);
0268         int N2 = hdcons_.getUVMax(t2);
0269         int v2 = hdcons_.modifyUV(id.iCell2, id.iType, t2);
0270 #ifdef EDM_ML_DEBUG
0271         edm::LogVerbatim("HGCalGeom") << "Cell Type 14 "
0272                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << v1 << ":" << t2
0273                                       << ":" << N2 << ":" << v2;
0274 #endif
0275         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, v1 + 1, v1 + N1);
0276         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, v1, v1 + N1 - 1);
0277         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2 - 1);
0278         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2);
0279         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 + 1);
0280         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, 0, v2 - N2 + 1);
0281         break;
0282       }
0283       case (HGCalCell::topRightCorner): {
0284         // top right corner
0285         int wu1(id.iSec1), wv1(id.iSec2 + 1);
0286         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0287         int N1 = hdcons_.getUVMax(t1);
0288         int v1 = hdcons_.modifyUV(id.iCell2, id.iType, t1);
0289         int wu2(id.iSec1 - 1), wv2(id.iSec2);
0290         int t2 = hdcons_.getTypeHex(id.iLay, wu2, wv2);
0291         int N2 = hdcons_.getUVMax(t2);
0292         int u2 = hdcons_.modifyUV(id.iCell1, id.iType, t2);
0293 #ifdef EDM_ML_DEBUG
0294         edm::LogVerbatim("HGCalGeom") << "Cell Type 15 "
0295                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << v1 << ":" << t2
0296                                       << ":" << N2 << ":" << u2;
0297 #endif
0298         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, 0, v1 - N1);
0299         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 - 1);
0300         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2 - 1);
0301         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2);
0302         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, u2 - N2, 0);
0303         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, u2 - N2 + 1, 0);
0304         break;
0305       }
0306       case (HGCalCell::bottomRightCorner): {
0307         // bottom right corner
0308         int wu1(id.iSec1 - 1), wv1(id.iSec2 - 1);
0309         int t1 = hdcons_.getTypeHex(id.iLay, wu1, wv1);
0310         int N1 = hdcons_.getUVMax(t1);
0311         int u1 = hdcons_.modifyUV(id.iCell1, id.iType, t1);
0312         int wu2(id.iSec1 - 1), wv2(id.iSec2);
0313         int t2 = hdcons_.getTypeHex(id.iLay, wu2, wv2);
0314         int N2 = hdcons_.getUVMax(t2);
0315         int u2 = hdcons_.modifyUV(id.iCell1, id.iType, t2);
0316 #ifdef EDM_ML_DEBUG
0317         edm::LogVerbatim("HGCalGeom") << "Cell Type 16 "
0318                                       << ":" << wu1 << ":" << wv1 << ":" << t1 << ":" << N1 << ":" << u1 << ":" << t2
0319                                       << ":" << N2 << ":" << u2;
0320 #endif
0321         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 + 1, id.iCell2);
0322         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2 - 1);
0323         addHGCSiliconId(ids, id.det, id.zSide, id.iType, id.iLay, id.iSec1, id.iSec2, id.iCell1 - 1, id.iCell2 - 1);
0324         addHGCSiliconId(ids, id.det, id.zSide, t1, id.iLay, wu1, wv1, u1 + N1 - 1, u1 - 1);
0325         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, u2 - N2, 0);
0326         addHGCSiliconId(ids, id.det, id.zSide, t2, id.iLay, wu2, wv2, u2 - N2 + 1, 0);
0327         break;
0328       }
0329       default:
0330         // Not valid u, v
0331         int N = hdcons_.getUVMax(id.iType);
0332         edm::LogWarning("HGCalGeom") << "u:v " << id.iCell1 << ":" << id.iCell2 << " Tests " << (id.iCell1 > 2 * N - 1)
0333                                      << ":" << (id.iCell2 > 2 * N - 1) << ":" << (id.iCell2 >= (id.iCell1 + N)) << ":"
0334                                      << (id.iCell1 > (id.iCell2 + N)) << " ERROR";
0335     }
0336   } else if (tileTrapezoid()) {
0337     int iphi1 = (id.iCell1 > 1) ? id.iCell1 - 1 : hdcons_.getUVMax(id.iType);
0338     int iphi2 = (id.iCell1 < hdcons_.getUVMax(id.iType)) ? id.iCell1 + 1 : 1;
0339     addHGCSCintillatorId(ids, id.zSide, id.iType, id.iLay, id.iSec1 - 1, id.iCell1);
0340     addHGCSCintillatorId(ids, id.zSide, id.iType, id.iLay, id.iSec1 - 1, iphi1);
0341     addHGCSCintillatorId(ids, id.zSide, id.iType, id.iLay, id.iSec1, iphi1);
0342     addHGCSCintillatorId(ids, id.zSide, id.iType, id.iLay, id.iSec1 + 1, iphi1);
0343     addHGCSCintillatorId(ids, id.zSide, id.iType, id.iLay, id.iSec1 + 1, id.iCell1);
0344     addHGCSCintillatorId(ids, id.zSide, id.iType, id.iLay, id.iSec1 + 1, iphi2);
0345     addHGCSCintillatorId(ids, id.zSide, id.iType, id.iLay, id.iSec1, iphi2);
0346     addHGCSCintillatorId(ids, id.zSide, id.iType, id.iLay, id.iSec1 - 1, iphi2);
0347   }
0348   return ids;
0349 }
0350 
0351 uint32_t HGCalTopology::detId2denseId(const DetId& idin) const {
0352   HGCalTopology::DecodedDetId id = decode(idin);
0353   uint32_t idx;
0354   if (waferHexagon6()) {
0355     int type = (id.iType > 0) ? 1 : 0;
0356     idx = (uint32_t)(((id.zSide > 0) ? kHGhalfType_ : 0) +
0357                      ((((id.iCell1 - 1) * layers_ + id.iLay - 1) * sectors_ + id.iSec1) * types_ + type));
0358 #ifdef EDM_ML_DEBUG
0359     edm::LogVerbatim("HGCalGeom") << "Input Hex " << id.zSide << ":" << id.iLay << ":" << id.iSec1 << ":" << id.iCell1
0360                                   << ":" << id.iType << " Constants " << kHGeomHalf_ << ":" << layers_ << ":"
0361                                   << sectors_ << ":" << types_ << " o/p " << idx;
0362 #endif
0363   } else if (tileTrapezoid()) {
0364     idx =
0365         (uint32_t)(((id.zSide > 0) ? kHGhalfType_ : 0) +
0366                    ((((id.iCell1 - 1) * layers_ + id.iLay - firstLay_) * sectors_ + id.iSec1 - 1) * types_ + id.iType));
0367 #ifdef EDM_ML_DEBUG
0368     edm::LogVerbatim("HGCalGeom") << "Input Trap " << id.zSide << ":" << id.iLay << ":" << id.iSec1 << ":" << id.iCell1
0369                                   << ":" << id.iType << " Constants " << kHGeomHalf_ << ":" << layers_ << ":"
0370                                   << sectors_ << ":" << types_ << " o/p " << idx;
0371 #endif
0372   } else {
0373     idx =
0374         (uint32_t)(((id.zSide > 0) ? kHGhalfType_ : 0) +
0375                    (((((id.iCell1 * cellMax_ + id.iCell2) * layers_ + id.iLay - 1) * waferMax_ + id.iSec1 + waferOff_) *
0376                          waferMax_ +
0377                      id.iSec2 + waferOff_) *
0378                         types_ +
0379                     id.iType));
0380 #ifdef EDM_ML_DEBUG
0381     edm::LogVerbatim("HGCalGeom") << "Input Hex8 " << id.zSide << ":" << id.iLay << ":" << id.iSec1 << ":" << id.iSec2
0382                                   << ":" << id.iCell1 << ":" << id.iCell2 << ":" << id.iType << " Constants "
0383                                   << kHGeomHalf_ << ":" << cellMax_ << ":" << layers_ << ":" << waferMax_ << ":"
0384                                   << waferOff_ << ":" << types_ << " o/p " << idx;
0385 #endif
0386   }
0387   return idx;
0388 }
0389 
0390 DetId HGCalTopology::denseId2detId(uint32_t hi) const {
0391   HGCalTopology::DecodedDetId id;
0392   if (validHashIndex(hi)) {
0393     id.zSide = (static_cast<int>(hi) < kHGhalfType_ ? -1 : 1);
0394     int di = (static_cast<int>(hi) % kHGhalfType_);
0395     if (waferHexagon6()) {
0396       int type = (di % types_);
0397       id.iType = (type == 0 ? -1 : 1);
0398       id.iSec1 = (((di - type) / types_) % sectors_);
0399       id.iLay = (((((di - type) / types_) - id.iSec1 + 1) / sectors_) % layers_ + 1);
0400       id.iCell1 = (((((di - type) / types_) - id.iSec1 + 1) / sectors_ - id.iLay + 1) / layers_ + 1);
0401 #ifdef EDM_ML_DEBUG
0402       edm::LogVerbatim("HGCalGeom") << "Input Hex " << hi << " o/p " << id.zSide << ":" << id.iLay << ":" << id.iType
0403                                     << ":" << id.iSec1 << ":" << id.iCell1;
0404 #endif
0405     } else if (tileTrapezoid()) {
0406       int type = (di % types_);
0407       id.iType = type;
0408       id.iSec1 = (((di - type) / types_) % sectors_) + 1;
0409       id.iLay = (((((di - type) / types_) - id.iSec1 + 1) / sectors_) % layers_ + firstLay_);
0410       id.iCell1 = (((((di - type) / types_) - id.iSec1 + 1) / sectors_ - id.iLay + firstLay_) / layers_ + 1);
0411 #ifdef EDM_ML_DEBUG
0412       edm::LogVerbatim("HGCalGeom") << "Input Trap " << hi << " o/p " << id.zSide << ":" << id.iLay << ":" << id.iType
0413                                     << ":" << id.iSec1 << ":" << id.iCell1;
0414 #endif
0415     } else {
0416       int type = (di % types_);
0417       id.iType = type;
0418       di = (di - type) / types_;
0419       id.iSec2 = (di % waferMax_) - waferOff_;
0420       di = (di - id.iSec2 - waferOff_) / waferMax_;
0421       id.iSec1 = (di % waferMax_) - waferOff_;
0422       di = (di - id.iSec1 - waferOff_) / waferMax_;
0423       id.iLay = (di % layers_) + 1;
0424       di = (di - id.iLay + 1) / layers_;
0425       id.iCell2 = (di % cellMax_);
0426       id.iCell1 = (di - id.iCell2) / cellMax_;
0427 #ifdef EDM_ML_DEBUG
0428       edm::LogVerbatim("HGCalGeom") << "Input Hex8 " << hi << " o/p " << id.zSide << ":" << id.iLay << ":" << id.iType
0429                                     << ":" << id.iSec1 << ":" << id.iSec2 << ":" << id.iCell1 << ":" << id.iCell2;
0430 #endif
0431     }
0432   }
0433   return encode(id);
0434 }
0435 
0436 uint32_t HGCalTopology::detId2denseGeomId(const DetId& idin) const {
0437   HGCalTopology::DecodedDetId id = decode(idin);
0438   uint32_t idx;
0439   if (waferHexagon6()) {
0440     idx = (uint32_t)(((id.zSide > 0) ? kHGeomHalf_ : 0) + (id.iLay - 1) * sectors_ + id.iSec1);
0441 #ifdef EDM_ML_DEBUG
0442     edm::LogVerbatim("HGCalGeom") << "Geom Hex I/P " << id.zSide << ":" << id.iLay << ":" << id.iSec1 << ":" << id.iType
0443                                   << " Constants " << kHGeomHalf_ << ":" << layers_ << ":" << sectors_ << " o/p "
0444                                   << idx;
0445 #endif
0446   } else if (tileTrapezoid()) {
0447     idx = (uint32_t)(((id.zSide > 0) ? kHGeomHalf_ : 0) +
0448                      (((id.iLay - firstLay_) * sectors_ + id.iSec1 - 1) * cellMax_ + id.iCell1 - 1));
0449 #ifdef EDM_ML_DEBUG
0450     edm::LogVerbatim("HGCalGeom") << "Geom Trap I/P " << id.zSide << ":" << id.iLay << ":" << id.iSec1 << ":"
0451                                   << id.iCell1 << ":" << id.iType << " Constants " << kHGeomHalf_ << ":" << layers_
0452                                   << ":" << firstLay_ << ":" << sectors_ << ":" << cellMax_ << " o/p " << idx;
0453 #endif
0454   } else {
0455     idx = (uint32_t)(((id.zSide > 0) ? kHGeomHalf_ : 0) +
0456                      (((id.iLay - 1) * waferMax_ + id.iSec1 + waferOff_) * waferMax_ + id.iSec2 + waferOff_));
0457 #ifdef EDM_ML_DEBUG
0458     edm::LogVerbatim("HGCalGeom") << "Geom Hex8 I/P " << id.zSide << ":" << id.iLay << ":" << id.iSec1 << ":"
0459                                   << id.iSec2 << ":" << id.iType << " Constants " << kHGeomHalf_ << ":" << layers_
0460                                   << ":" << waferMax_ << ":" << waferOff_ << " o/p " << idx;
0461 #endif
0462   }
0463   return idx;
0464 }
0465 
0466 bool HGCalTopology::valid(const DetId& idin) const {
0467   HGCalTopology::DecodedDetId id = decode(idin);
0468   bool flag;
0469   if (waferHexagon6()) {
0470     flag = (idin.det() == det_ && idin.subdetId() == static_cast<int>(subdet_) && id.iCell1 >= 0 &&
0471             id.iCell1 < cells_ && id.iLay > 0 && id.iLay <= layers_ && id.iSec1 >= 0 && id.iSec1 <= sectors_);
0472     if (flag)
0473       flag = hdcons_.isValidHex(id.iLay, id.iSec1, id.iCell1, true);
0474   } else if (tileTrapezoid()) {
0475     flag = ((idin.det() == det_) && hdcons_.isValidTrap(id.zSide, id.iLay, id.iSec1, id.iCell1));
0476   } else {
0477     flag = ((idin.det() == det_) && hdcons_.isValidHex8(id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2, false));
0478   }
0479   return flag;
0480 }
0481 
0482 bool HGCalTopology::valid(const DetId& idin, int cornerMin) const {
0483   if (waferHexagon8()) {
0484     HGCalTopology::DecodedDetId id = decode(idin);
0485     bool mask = (cornerMin < HGCalTypes::WaferCornerMin) ? false : hdcons_.maskCell(idin, cornerMin);
0486     bool flag = ((idin.det() == det_) &&
0487                  hdcons_.isValidHex8(
0488                      id.iLay, id.iSec1, id.iSec2, id.iCell1, id.iCell2, (cornerMin >= HGCalTypes::WaferCornerMin)));
0489     return (flag && (!mask));
0490   } else {
0491     return valid(idin);
0492   }
0493 }
0494 
0495 bool HGCalTopology::validModule(const DetId& idin, int cornerMin) const {
0496   if (idin.det() != det_) {
0497     return false;
0498   } else if ((idin.det() == DetId::HGCalEE) || (idin.det() == DetId::HGCalHSi)) {
0499     HGCalTopology::DecodedDetId id = decode(idin);
0500     return hdcons_.isValidHex8(id.iLay, id.iSec1, id.iSec2, (cornerMin >= HGCalTypes::WaferCornerMin));
0501   } else {
0502     return valid(idin);
0503   }
0504 }
0505 
0506 DetId HGCalTopology::offsetBy(const DetId startId, int nrStepsX, int nrStepsY) const {
0507   if (startId.det() == DetId::Forward && startId.subdetId() == static_cast<int>(subdet_)) {
0508     DetId id = changeXY(startId, nrStepsX, nrStepsY);
0509     if (valid(id))
0510       return id;
0511   }
0512   return DetId(0);
0513 }
0514 
0515 DetId HGCalTopology::switchZSide(const DetId startId) const {
0516   HGCalTopology::DecodedDetId id_ = decode(startId);
0517   id_.zSide = -id_.zSide;
0518   DetId id = encode(id_);
0519   if (valid(id))
0520     return id;
0521   else
0522     return DetId(0);
0523 }
0524 
0525 HGCalTopology::DecodedDetId HGCalTopology::geomDenseId2decId(const uint32_t& hi) const {
0526   HGCalTopology::DecodedDetId id;
0527   if (hi < totalGeomModules()) {
0528     id.zSide = (static_cast<int>(hi) < kHGeomHalf_ ? -1 : 1);
0529     int di = (static_cast<int>(hi) % kHGeomHalf_);
0530     if (waferHexagon6()) {
0531       id.iSec1 = (di % sectors_);
0532       di = (di - id.iSec1) / sectors_;
0533       id.iLay = (di % layers_) + 1;
0534       id.iType = ((di - id.iLay + 1) / layers_ == 0) ? -1 : 1;
0535 #ifdef EDM_ML_DEBUG
0536       edm::LogVerbatim("HGCalGeom") << "Geom Hex I/P " << hi << " O/P " << id.zSide << ":" << id.iType << ":" << id.iLay
0537                                     << ":" << id.iSec1;
0538 #endif
0539     } else if (tileTrapezoid()) {
0540       id.iCell1 = (di % cellMax_) + 1;
0541       di = (di - id.iCell1 + 1) / cellMax_;
0542       id.iSec1 = (di % sectors_) + 1;
0543       di = (di - id.iSec1 + 1) / sectors_;
0544       id.iLay = (di % layers_) + firstLay_;
0545       id.iType = (di - id.iLay + firstLay_) / layers_;
0546 #ifdef EDM_ML_DEBUG
0547       edm::LogVerbatim("HGCalGeom") << "Geom Trap I/P " << hi << " O/P " << id.zSide << ":" << id.iType << ":"
0548                                     << id.iLay << ":" << id.iSec1 << ":" << id.iCell1;
0549 #endif
0550     } else {
0551       id.iSec2 = (di % waferMax_) - waferOff_;
0552       di = (di - id.iSec2 - waferOff_) / waferMax_;
0553       id.iSec1 = (di % waferMax_) - waferOff_;
0554       di = (di - id.iSec1 - waferOff_) / waferMax_;
0555       id.iLay = (di % layers_) + 1;
0556       id.iType = (di - id.iLay + 1) / layers_;
0557 #ifdef EDM_ML_DEBUG
0558       edm::LogVerbatim("HGCalGeom") << "Geom Hex8 I/P " << hi << " O/P " << id.zSide << ":" << id.iType << ":"
0559                                     << id.iLay << ":" << id.iSec1 << ":" << id.iSec2;
0560 #endif
0561     }
0562   }
0563   return id;
0564 }
0565 
0566 void HGCalTopology::addHGCSCintillatorId(
0567     std::vector<DetId>& ids, int zside, int type, int lay, int iradius, int iphi) const {
0568 #ifdef EDM_ML_DEBUG
0569   edm::LogVerbatim("HGCalGeom") << "addHGCSCintillatorId " << zside << ":" << type << ":" << lay << ":" << iradius
0570                                 << ":" << iphi << " ==> Validity " << hdcons_.isValidTrap(zside, lay, iradius, iphi);
0571 #endif
0572   if (hdcons_.isValidTrap(zside, lay, iradius, iphi)) {
0573     HGCScintillatorDetId id(type, lay, zside * iradius, iphi);
0574     ids.emplace_back(DetId(id));
0575   }
0576 }
0577 
0578 void HGCalTopology::addHGCSiliconId(
0579     std::vector<DetId>& ids, int det, int zside, int type, int lay, int waferU, int waferV, int cellU, int cellV) const {
0580 #ifdef EDM_ML_DEBUG
0581   edm::LogVerbatim("HGCalGeom") << "addHGCSiliconId " << det << ":" << zside << ":" << type << ":" << lay << ":"
0582                                 << waferU << ":" << waferV << ":" << cellU << ":" << cellV << " ==> Validity "
0583                                 << hdcons_.isValidHex8(lay, waferU, waferV, cellU, cellV, false);
0584 #endif
0585   if (hdcons_.isValidHex8(lay, waferU, waferV, cellU, cellV, false)) {
0586     if (det == static_cast<int>(ForwardSubdetector::HFNose)) {
0587       HFNoseDetId id(DetId::Forward, zside, type, lay, waferU, waferV, cellU, cellV);
0588       ids.emplace_back(DetId(id));
0589     } else {
0590       HGCSiliconDetId id((DetId::Detector)(det), zside, type, lay, waferU, waferV, cellU, cellV);
0591       ids.emplace_back(DetId(id));
0592     }
0593   }
0594 }
0595 
0596 HGCalTopology::DecodedDetId HGCalTopology::decode(const DetId& startId) const {
0597   HGCalTopology::DecodedDetId idx;
0598   if (waferHexagon6()) {
0599     HGCalDetId id(startId);
0600     idx.iCell1 = id.cell();
0601     idx.iCell2 = 0;
0602     idx.iLay = id.layer();
0603     idx.iSec1 = id.wafer();
0604     idx.iSec2 = 0;
0605     idx.iType = id.waferType();
0606     idx.zSide = id.zside();
0607     idx.det = id.subdetId();
0608   } else if (tileTrapezoid()) {
0609     HGCScintillatorDetId id(startId);
0610     idx.iCell1 = id.iphi();
0611     idx.iCell2 = 0;
0612     idx.iLay = id.layer();
0613     idx.iSec1 = id.ietaAbs();
0614     idx.iSec2 = 0;
0615     idx.iType = id.type();
0616     idx.zSide = id.zside();
0617     idx.det = static_cast<int>(id.subdet());
0618   } else if (det_ == DetId::Forward && subdet_ == ForwardSubdetector::HFNose) {
0619     HFNoseDetId id(startId);
0620     idx.iCell1 = id.cellU();
0621     idx.iCell2 = id.cellV();
0622     idx.iLay = id.layer();
0623     idx.iSec1 = id.waferU();
0624     idx.iSec2 = id.waferV();
0625     idx.iType = id.type();
0626     idx.zSide = id.zside();
0627     idx.det = static_cast<int>(id.subdet());
0628   } else {
0629     HGCSiliconDetId id(startId);
0630     idx.iCell1 = id.cellU();
0631     idx.iCell2 = id.cellV();
0632     idx.iLay = id.layer();
0633     idx.iSec1 = id.waferU();
0634     idx.iSec2 = id.waferV();
0635     idx.iType = id.type();
0636     idx.zSide = id.zside();
0637     idx.det = static_cast<int>(id.subdet());
0638   }
0639   return idx;
0640 }
0641 
0642 DetId HGCalTopology::encode(const HGCalTopology::DecodedDetId& idx) const {
0643   DetId id;
0644 #ifdef EDM_ML_DEBUG
0645   edm::LogVerbatim("HGCalGeomX") << "Encode " << idx.det << ":" << idx.zSide << ":" << idx.iType << ":" << idx.iLay
0646                                  << ":" << idx.iSec1 << ":" << idx.iSec2 << ":" << idx.iCell1 << ":" << idx.iCell2;
0647 #endif
0648   if (waferHexagon6()) {
0649     id =
0650         HGCalDetId((ForwardSubdetector)(idx.det), idx.zSide, idx.iLay, ((idx.iType > 0) ? 1 : 0), idx.iSec1, idx.iCell1)
0651             .rawId();
0652   } else if (tileTrapezoid()) {
0653     HGCScintillatorDetId hid(idx.iType, idx.iLay, idx.zSide * idx.iSec1, idx.iCell1);
0654     std::pair<int, int> typm = hdcons_.tileType(hid.layer(), hid.ring(), 0);
0655     if (typm.first >= 0) {
0656       hid.setType(typm.first);
0657       hid.setSiPM(typm.second);
0658     }
0659     id = hid.rawId();
0660   } else if (det_ == DetId::Forward && subdet_ == ForwardSubdetector::HFNose) {
0661     id = HFNoseDetId(idx.zSide, idx.iType, idx.iLay, idx.iSec1, idx.iSec2, idx.iCell1, idx.iCell2).rawId();
0662   } else {
0663     id = HGCSiliconDetId(
0664              (DetId::Detector)(idx.det), idx.zSide, idx.iType, idx.iLay, idx.iSec1, idx.iSec2, idx.iCell1, idx.iCell2)
0665              .rawId();
0666   }
0667   return id;
0668 }
0669 
0670 DetId HGCalTopology::changeXY(const DetId& id, int nrStepsX, int nrStepsY) const { return DetId(); }
0671 
0672 DetId HGCalTopology::changeZ(const DetId& id, int nrStepsZ) const { return DetId(); }
0673 
0674 #include "FWCore/Utilities/interface/typelookup.h"
0675 
0676 TYPELOOKUP_DATA_REG(HGCalTopology);