Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:03

0001 #include "DataFormats/EcalDetId/interface/EcalScDetId.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 
0004 #include <ostream>
0005 #include <cassert>
0006 #include <mutex>
0007 
0008 short EcalScDetId::xyz2HashedIndex[EcalScDetId::IX_MAX][EcalScDetId::IY_MAX][EcalScDetId::nEndcaps];
0009 
0010 EcalScDetId EcalScDetId::hashedIndex2DetId[kSizeForDenseIndexing];
0011 
0012 EcalScDetId::EcalScDetId() : DetId() {}
0013 
0014 EcalScDetId::EcalScDetId(uint32_t rawid) : DetId(rawid) {}
0015 
0016 EcalScDetId::EcalScDetId(int ix, int iy, int iz) : DetId(Ecal, EcalEndcap) {
0017   if (!validDetId(ix, iy, iz)) {
0018     throw cms::Exception("InvalidDetId") << "EcalScDetId:  Cannot create object.  Indexes out of bounds \n"
0019                                          << "x = " << ix << " y = " << iy << " z = " << iz;
0020   }
0021   const int scBit = 1 << 15;  //bit set to 1 to distinguish from crystal id (EEDetId)
0022   //                         and for a reasonale behaviour of DetId ccomparison operators.
0023   id_ |= (iy & 0x7f) | ((ix & 0x7f) << 7) | ((iz > 0) ? (1 << 14) : (0)) | scBit;
0024 }
0025 
0026 EcalScDetId::EcalScDetId(const DetId& gen) {
0027   if (!gen.null() && (gen.det() != Ecal || gen.subdetId() != EcalEndcap)) {
0028     throw cms::Exception("InvalidDetId");
0029   }
0030   id_ = gen.rawId();
0031 }
0032 
0033 EcalScDetId& EcalScDetId::operator=(const DetId& gen) {
0034   if (!gen.null() && (gen.det() != Ecal || gen.subdetId() != EcalEndcap)) {
0035     throw cms::Exception("InvalidDetId");
0036   }
0037   id_ = gen.rawId();
0038   return *this;
0039 }
0040 
0041 int EcalScDetId::iquadrant() const {
0042   const int xMiddle = IX_MAX / 2;  //y = 0 between xMiddle and xMiddle+1
0043   const int yMiddle = IY_MAX / 2;  //x = 0 between yMiddle and yMiddle+1
0044   if (iy() > yMiddle) {            // y>0
0045     if (ix() > xMiddle)            //             A y
0046       return 1;                    //             |
0047     else                           //      Q2     |    Q1
0048       return 2;                    //             |
0049   } else {                         // y<0          //   ----------o---------> x
0050     if (ix() > xMiddle)            //             |
0051       return 4;                    //      Q3     |    Q4
0052     else                           //             |
0053       return 3;
0054   }
0055   //Should never be reached
0056   return -1;
0057 }
0058 
0059 bool EcalScDetId::validDetId(int iX, int iY, int iZ) {
0060   static const char endcapMap[401] = {
0061       "       XXXXXX       "
0062       "    XXXXXXXXXXXX    "
0063       "   XXXXXXXXXXXXXX   "
0064       "  XXXXXXXXXXXXXXXX  "
0065       " XXXXXXXXXXXXXXXXXX "
0066       " XXXXXXXXXXXXXXXXXX "  //    Z
0067       " XXXXXXXXXXXXXXXXXX "  //     x-----> X
0068       "XXXXXXXXXXXXXXXXXXXX"  //     |
0069       "XXXXXXXXX  XXXXXXXXX"  //     |
0070       "XXXXXXXX    XXXXXXXX"  //_          //     |
0071       "XXXXXXXX    XXXXXXXX"  //     V Y
0072       "XXXXXXXXX  XXXXXXXXX"
0073       "XXXXXXXXXXXXXXXXXXXX"
0074       " XXXXXXXXXXXXXXXXXX "
0075       " XXXXXXXXXXXXXXXXXX "
0076       " XXXXXXXXXXXXXXXXXX "
0077       "  XXXXXXXXXXXXXXXX  "
0078       "   XXXXXXXXXXXXXX   "
0079       "    XXXXXXXXXXXX    "
0080       "       XXXXXX       "};
0081 
0082   return abs(iZ) == 1 && endcapMap[iX - 1 + (iY - 1) * 20] != ' ';
0083 }
0084 
0085 std::ostream& operator<<(std::ostream& s, const EcalScDetId& id) {
0086   return s << "(EE iz " << ((id.zside() > 0) ? ("+ ") : ("- ")) << " ix " << id.ix() << " , iy " << id.iy() << ')';
0087 }
0088 
0089 //NOTE: When looping is possible in constexpr, this should be changed to one
0090 static std::once_flag initializedFlag;
0091 void EcalScDetId::checkHashedIndexMap() {
0092   std::call_once(initializedFlag, []() {
0093     int hashedIndex = -1;
0094     for (int iZ = -1; iZ <= +1; iZ += 2) {
0095       for (int iY = IY_MIN; iY <= IY_MAX; ++iY) {
0096         for (int iX = IX_MIN; iX <= IX_MAX; ++iX) {
0097           if (validDetId(iX, iY, iZ)) {
0098             xyz2HashedIndex[iX - IX_MIN][iY - IY_MIN][iZ > 0 ? 1 : 0] = ++hashedIndex;
0099             assert((unsigned)hashedIndex < sizeof(hashedIndex2DetId) / sizeof(hashedIndex2DetId[0]));
0100             hashedIndex2DetId[hashedIndex] = EcalScDetId(iX, iY, iZ);
0101           }
0102         }
0103       }
0104     }
0105   });
0106 }