Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-29 02:26:08

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // File: HGCalNumberingScheme.cc
0003 // Description: Numbering scheme for High Granularity Calorimeter
0004 ///////////////////////////////////////////////////////////////////////////////
0005 #include "SimG4CMS/Calo/interface/HGCalNumberingScheme.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "DataFormats/ForwardDetId/interface/ForwardSubdetector.h"
0008 #include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
0009 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0010 #include "Geometry/HGCalCommonData/interface/HGCalWaferIndex.h"
0011 #include <array>
0012 #include <iostream>
0013 
0014 //#define EDM_ML_DEBUG
0015 
0016 HGCalNumberingScheme::HGCalNumberingScheme(const HGCalDDDConstants& hgc,
0017                                            const DetId::Detector& det,
0018                                            const std::string& name)
0019     : hgcons_(hgc), mode_(hgc.geomMode()), det_(det), name_(name) {
0020 #ifdef EDM_ML_DEBUG
0021   edm::LogVerbatim("HGCSim") << "Creating HGCalNumberingScheme for " << name_ << " Det " << det_ << " Mode " << mode_
0022                              << ":" << HGCalGeometryMode::Hexagon8Full << ":" << HGCalGeometryMode::Hexagon8 << ":"
0023                              << HGCalGeometryMode::Hexagon8File << ":" << HGCalGeometryMode::Hexagon8Module << ":"
0024                              << ":" << HGCalGeometryMode::Hexagon8Cassette << ":" << HGCalGeometryMode::Trapezoid << ":"
0025                              << HGCalGeometryMode::TrapezoidFile << ":" << HGCalGeometryMode::TrapezoidModule << ":"
0026                              << HGCalGeometryMode::TrapezoidCassette;
0027 #endif
0028 }
0029 
0030 HGCalNumberingScheme::~HGCalNumberingScheme() {
0031 #ifdef EDM_ML_DEBUG
0032   edm::LogVerbatim("HGCSim") << "Deleting HGCalNumberingScheme";
0033 #endif
0034 }
0035 
0036 uint32_t HGCalNumberingScheme::getUnitID(int layer, int module, int cell, int iz, const G4ThreeVector& pos, double& wt) {
0037   // module is the copy number of the wafer as placed in the layer
0038   uint32_t index(0);
0039   wt = 1.0;
0040 #ifdef EDM_ML_DEBUG
0041   edm::LogVerbatim("HGCSim") << "HGCalNumberingScheme:: input Layer " << layer << " Module " << module << " Cell "
0042                              << cell << " iz " << iz << " Position " << pos;
0043 #endif
0044   if (hgcons_.waferHexagon8()) {
0045     int cellU(0), cellV(0), waferType(-1), waferU(0), waferV(0);
0046     if (cell >= 0) {
0047       waferType = HGCalTypes::getUnpackedType(module);
0048       waferU = HGCalTypes::getUnpackedU(module);
0049       waferV = HGCalTypes::getUnpackedV(module);
0050       cellU = HGCalTypes::getUnpackedCellU(cell);
0051       cellV = HGCalTypes::getUnpackedCellV(cell);
0052     } else if (mode_ != HGCalGeometryMode::Hexagon8) {
0053       double xx = (pos.z() > 0) ? pos.x() : -pos.x();
0054       hgcons_.waferFromPosition(xx, pos.y(), layer, waferU, waferV, cellU, cellV, waferType, wt, false, false);
0055     }
0056     if (waferType >= 0) {
0057       if (hgcons_.waferHexagon8File()) {
0058         int type = hgcons_.waferType(layer, waferU, waferV, true);
0059         if (type != waferType) {
0060 #ifdef EDM_ML_DEBUG
0061           edm::LogVerbatim("HGCSim") << "HGCalNumberingScheme:: " << name_ << " Layer|u|v|Index|module|cell " << layer
0062                                      << ":" << waferU << ":" << waferV << ":"
0063                                      << HGCalWaferIndex::waferIndex(layer, waferU, waferV, false) << ":" << module
0064                                      << ":" << cell << " has a type mismatch " << waferType << ":" << type;
0065 #endif
0066           if (type != HGCSiliconDetId::HGCalCoarseThick)
0067             waferType = type;
0068         }
0069       }
0070       index = HGCSiliconDetId(det_, iz, waferType, layer, waferU, waferV, cellU, cellV).rawId();
0071 #ifdef EDM_ML_DEBUG
0072       edm::LogVerbatim("HGCSim") << "OK WaferType " << waferType << " Wafer " << waferU << ":" << waferV << " Cell "
0073                                  << cellU << ":" << cellV << " input " << cell << " wt " << wt << " Mode " << mode_;
0074     } else {
0075       edm::LogVerbatim("HGCSim") << "Bad WaferType " << waferType << " for Layer:u:v " << layer << ":" << waferU << ":"
0076                                  << waferV;
0077 #endif
0078     }
0079   } else if (hgcons_.tileTrapezoid()) {
0080     std::array<int, 3> id = hgcons_.assignCellTrap(pos.x(), pos.y(), pos.z(), layer, false);
0081     if (id[2] >= 0) {
0082       std::pair<int, int> typm = hgcons_.tileType(layer, id[0], 0);
0083       HGCScintillatorDetId detId(id[2], layer, iz * id[0], id[1], false, 0);
0084       if (typm.first >= 0) {
0085         detId.setType(typm.first);
0086         detId.setSiPM(typm.second);
0087       }
0088       index = detId.rawId();
0089 #ifdef EDM_ML_DEBUG
0090       int lay = layer + hgcons_.getLayerOffset();
0091       edm::LogVerbatim("HGCSim") << "Radius/Phi " << id[0] << ":" << id[1] << " Type " << id[2] << ":" << typm.first
0092                                  << " SiPM " << typm.second << ":" << hgcons_.tileSiPM(typm.second) << " Layer "
0093                                  << layer << ":" << lay << " z " << iz << " " << detId;
0094     } else {
0095       edm::LogVerbatim("HGCSim") << "Radius/Phi " << id[0] << ":" << id[1] << " Type " << id[2] << " Layer|iz " << layer
0096                                  << ":" << iz << " ERROR";
0097 #endif
0098     }
0099   }
0100 #ifdef EDM_ML_DEBUG
0101   bool matchOnly = (mode_ == HGCalGeometryMode::Hexagon8Module);
0102   bool debug = (mode_ == HGCalGeometryMode::Hexagon8Module);
0103   if (debug)
0104     edm::LogVerbatim("HGCSim") << "HGCalNumberingScheme::i/p " << det_ << ":" << layer << ":" << module << ":" << cell
0105                                << ":" << iz << ":" << pos.x() << ":" << pos.y() << ":" << pos.z() << " ID " << std::hex
0106                                << index << std::dec << " wt " << wt;
0107   checkPosition(index, pos, matchOnly, debug);
0108 #endif
0109   return index;
0110 }
0111 
0112 void HGCalNumberingScheme::checkPosition(uint32_t index, const G4ThreeVector& pos, bool matchOnly, bool debug) const {
0113   std::pair<float, float> xy;
0114   bool ok(false);
0115   double z1(0), tolR(14.0), tolZ(1.0);
0116   int lay(-1);
0117   if (index == 0) {
0118   } else if (DetId(index).det() == DetId::HGCalHSi) {
0119     HGCSiliconDetId id = HGCSiliconDetId(index);
0120     lay = id.layer();
0121     xy = hgcons_.locateCell(lay, id.waferU(), id.waferV(), id.cellU(), id.cellV(), false, true);
0122     z1 = hgcons_.waferZ(lay, false);
0123     ok = true;
0124     tolR = 14.0;
0125     tolZ = 1.0;
0126   } else if (DetId(index).det() == DetId::HGCalHSc) {
0127     HGCScintillatorDetId id = HGCScintillatorDetId(index);
0128     lay = id.layer();
0129     xy = hgcons_.locateCellTrap(lay, id.ietaAbs(), id.iphi(), false);
0130     z1 = hgcons_.waferZ(lay, false);
0131     ok = true;
0132     tolR = 50.0;
0133     tolZ = 5.0;
0134   }
0135   if (ok) {
0136     double r1 = std::sqrt(xy.first * xy.first + xy.second * xy.second);
0137     double r2 = pos.perp();
0138     double z2 = std::abs(pos.z());
0139     std::pair<double, double> zrange = hgcons_.rangeZ(false);
0140     std::pair<double, double> rrange = hgcons_.rangeR(z2, false);
0141     bool match = (std::abs(r1 - r2) < tolR) && (std::abs(z1 - z2) < tolZ);
0142     bool inok = ((r2 >= rrange.first) && (r2 <= rrange.second) && (z2 >= zrange.first) && (z2 <= zrange.second));
0143     bool outok = ((r1 >= rrange.first) && (r1 <= rrange.second) && (z1 >= zrange.first) && (z1 <= zrange.second));
0144     std::string ck = (((r1 < rrange.first - tolR) || (r1 > rrange.second + tolR) || (z1 < zrange.first - tolZ) ||
0145                        (z1 > zrange.second + tolZ))
0146                           ? "***** ERROR *****"
0147                           : "");
0148     if (matchOnly && match)
0149       ck = "";
0150     if (!(match && inok && outok) || debug) {
0151       edm::LogVerbatim("HGCSim") << "HGCalNumberingScheme::Detector " << det_ << " Layer " << lay << " R " << r2 << ":"
0152                                  << r1 << ":" << rrange.first << ":" << rrange.second << " Z " << z2 << ":" << z1 << ":"
0153                                  << zrange.first << ":" << zrange.second << " Match " << match << ":" << inok << ":"
0154                                  << outok << " " << ck;
0155       edm::LogVerbatim("HGCSim") << "Original " << pos.x() << ":" << pos.y() << " return " << xy.first << ":"
0156                                  << xy.second;
0157       if ((DetId(index).det() == DetId::HGCalEE) || (DetId(index).det() == DetId::HGCalHSi)) {
0158         double wt = 0, xx = ((pos.z() > 0) ? pos.x() : -pos.x());
0159         int waferU, waferV, cellU, cellV, waferType;
0160         hgcons_.waferFromPosition(xx, pos.y(), lay, waferU, waferV, cellU, cellV, waferType, wt, false, false);
0161         xy = hgcons_.locateCell(lay, waferU, waferV, cellU, cellV, false, true, true);
0162         double dx = (xx - xy.first);
0163         double dy = (pos.y() - xy.second);
0164         double dR = std::sqrt(dx * dx + dy * dy);
0165         ck = (dR > tolR) ? " ***** ERROR *****" : "";
0166         edm::LogVerbatim("HGCSim") << "HGCalNumberingScheme " << HGCSiliconDetId(index) << " original position " << xx
0167                                    << ":" << pos.y() << " derived " << xy.first << ":" << xy.second << " Difference "
0168                                    << dR << ck;
0169       }
0170     }
0171   }
0172 }