CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/DataFormats/EcalDetId/src/EcalScDetId.cc

Go to the documentation of this file.
00001 #include "DataFormats/EcalDetId/interface/EcalScDetId.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 
00004 #include <iostream>
00005 #include <cassert>
00006 
00007 short EcalScDetId::xyz2HashedIndex[EcalScDetId::IX_MAX][EcalScDetId::IY_MAX][EcalScDetId::nEndcaps];
00008 
00009 EcalScDetId EcalScDetId::hashedIndex2DetId[kSizeForDenseIndexing];
00010 
00011 
00012 EcalScDetId::EcalScDetId() : DetId() {
00013 }
00014 
00015 EcalScDetId::EcalScDetId(uint32_t rawid) : DetId(rawid) {
00016 }
00017 
00018 EcalScDetId::EcalScDetId(int ix, int iy, int iz) : DetId(Ecal,EcalEndcap) 
00019 {
00020   if(!validDetId(ix,iy,iz))
00021     {
00022       throw cms::Exception("InvalidDetId") << "EcalScDetId:  Cannot create object.  Indexes out of bounds \n" 
00023                                            << "x = " << ix << " y = " << iy << " z = " << iz;
00024     }
00025   const int scBit = 1<<15; //bit set to 1 to distinguish from crystal id (EEDetId)
00026   //                         and for a reasonale behaviour of DetId ccomparison operators.
00027   id_|=(iy&0x7f)|((ix&0x7f)<<7)|((iz>0)?(1<<14):(0))|scBit;
00028 }
00029   
00030 EcalScDetId::EcalScDetId(const DetId& gen) {
00031   if (!gen.null() && (gen.det()!=Ecal || gen.subdetId()!=EcalEndcap)) {
00032     throw cms::Exception("InvalidDetId"); 
00033   }
00034   id_=gen.rawId();
00035 }
00036   
00037 EcalScDetId& EcalScDetId::operator=(const DetId& gen) {
00038   if (!gen.null() && ( gen.det()!=Ecal || gen.subdetId()!=EcalEndcap )) {
00039     throw cms::Exception("InvalidDetId"); 
00040   }
00041   id_=gen.rawId();
00042   return *this;
00043 }
00044   
00045 int EcalScDetId::iquadrant() const {
00046   const int xMiddle = IX_MAX/2; //y = 0 between xMiddle and xMiddle+1
00047   const int yMiddle = IY_MAX/2; //x = 0 between yMiddle and yMiddle+1
00048   if (iy()>yMiddle){// y>0
00049     if(ix()>xMiddle)   //             A y            
00050       return 1;        //             |              
00051     else               //      Q2     |    Q1        
00052       return 2;        //             |              
00053   } else{// y<0        //   ----------o---------> x   
00054     if(ix()>xMiddle)   //             |               
00055       return 4;        //      Q3     |    Q4       
00056     else               //             |               
00057       return 3;
00058   }
00059   //Should never be reached
00060   return -1;
00061 }  
00062 
00063 bool EcalScDetId::validDetId(int iX, int iY, int iZ) {
00064   static const char endcapMap[401] = {
00065     "       XXXXXX       "
00066     "    XXXXXXXXXXXX    "
00067     "   XXXXXXXXXXXXXX   "
00068     "  XXXXXXXXXXXXXXXX  "
00069     " XXXXXXXXXXXXXXXXXX "
00070     " XXXXXXXXXXXXXXXXXX "             //    Z
00071     " XXXXXXXXXXXXXXXXXX "             //     x-----> X
00072     "XXXXXXXXXXXXXXXXXXXX"             //     |
00073     "XXXXXXXXX  XXXXXXXXX"             //     |
00074     "XXXXXXXX    XXXXXXXX"//_          //     |
00075     "XXXXXXXX    XXXXXXXX"             //     V Y
00076     "XXXXXXXXX  XXXXXXXXX"
00077     "XXXXXXXXXXXXXXXXXXXX"
00078     " XXXXXXXXXXXXXXXXXX "
00079     " XXXXXXXXXXXXXXXXXX "
00080     " XXXXXXXXXXXXXXXXXX "
00081     "  XXXXXXXXXXXXXXXX  "
00082     "   XXXXXXXXXXXXXX   "
00083     "    XXXXXXXXXXXX    "
00084     "       XXXXXX       "};
00085 
00086   return abs(iZ)==1 && endcapMap[iX-1+(iY-1)*20]!=' ';
00087 }
00088 
00089 std::ostream& operator<<(std::ostream& s,const EcalScDetId& id) {
00090   return s << "(EE iz " << ((id.zside()>0)?("+ "):("- ")) << " ix " << id.ix() << " , iy " << id.iy() << ')';
00091 }
00092 
00093 void EcalScDetId::checkHashedIndexMap(){
00094   static bool initialized = false;
00095   if(initialized) return;
00096   int hashedIndex = -1;
00097   for(int iZ = -1; iZ <= +1; iZ+=2){
00098     for(int iY = IY_MIN; iY <= IY_MAX; ++iY){
00099       for(int iX = IX_MIN; iX <= IX_MAX; ++iX){
00100         if(validDetId(iX,iY,iZ)){
00101           xyz2HashedIndex[iX-IX_MIN][iY-IY_MIN][iZ>0?1:0] = ++hashedIndex;
00102           assert((unsigned)hashedIndex < sizeof(hashedIndex2DetId)/sizeof(hashedIndex2DetId[0]));
00103              hashedIndex2DetId[hashedIndex] = EcalScDetId(iX, iY, iZ);
00104         }
00105       }
00106     }
00107   }
00108   initialized = true;
00109 }