00001 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003
00004
00005 const int EBDetId::kModuleBoundaries[4] = { 25, 45, 65, 85 };
00006
00007
00008 EBDetId::EBDetId(int index1, int index2, int mode)
00009 : DetId(Ecal,EcalBarrel)
00010 {
00011 int crystal_ieta;
00012 int crystal_iphi;
00013 if (mode == ETAPHIMODE) {
00014 crystal_ieta = index1;
00015 crystal_iphi = index2;
00016 } else if (mode == SMCRYSTALMODE) {
00017 int SM = index1;
00018 int crystal = index2;
00019 int i = (int) floor((crystal-1) / kCrystalsInPhi);
00020 int j = ((crystal-1) - (kCrystalsInPhi*i));
00021 if (SM <= 18) {
00022 crystal_ieta = i + 1;
00023 crystal_iphi = ((SM-1) * kCrystalsInPhi) + (kCrystalsInPhi-j);
00024 } else {
00025 crystal_ieta = -(i+1);
00026 crystal_iphi = ((SM-19) * kCrystalsInPhi) + j+1;
00027 }
00028 } else {
00029 throw cms::Exception("InvalidDetId") << "EBDetId: Cannot create object. Unknown mode for (int, int) constructor.";
00030 }
00031
00032 if ( !validDetId(crystal_ieta, crystal_iphi) ) {
00033
00034 throw cms::Exception("InvalidDetId") << "EBDetId: Cannot create object. Indexes out of bounds \n"
00035 << "eta = " << crystal_ieta << " phi = " << crystal_iphi;
00036 }
00037 id_|=((crystal_ieta>0)?(0x10000|(crystal_ieta<<9)):((-crystal_ieta)<<9))|(crystal_iphi&0x1FF);
00038 }
00039
00040 EBDetId::EBDetId(const DetId& gen) {
00041 if (!gen.null() && ( gen.det()!=Ecal || gen.subdetId()!=EcalBarrel )) {
00042 throw cms::Exception("InvalidDetId");
00043 }
00044 id_=gen.rawId();
00045 }
00046
00047 bool EBDetId::validDetId(int i, int j) {
00048
00049 bool valid = true;
00050 if (i < -MAX_IETA || i == 0 || i > MAX_IETA ||
00051 j < MIN_IPHI || j > MAX_IPHI) {
00052 valid = false;
00053 }
00054 return valid;
00055
00056 }
00057
00058 EBDetId& EBDetId::operator=(const DetId& gen) {
00059 if (!gen.null() && ( gen.det()!=Ecal || gen.subdetId()!=EcalBarrel )) {
00060 throw cms::Exception("InvalidDetId");
00061 }
00062 id_=gen.rawId();
00063 return *this;
00064 }
00065
00066
00067 int EBDetId::ism() const {
00068 int id = ( iphi() - 1 ) / kCrystalsInPhi + 1;
00069 if ( zside() < 0 ) id += 18;
00070 return id;
00071 }
00072
00073 int EBDetId::im() const {
00074 for (int i=0; i < 4 ; i++)
00075 if ( ietaAbs() <= kModuleBoundaries[i] )
00076 return i+1;
00077
00078 return -1;
00079 }
00080
00081
00082 int EBDetId::ic() const {
00083 int ie = ietaAbs() -1;
00084 int ip;
00085 if (zside() < 0) {
00086 ip = (( iphi() -1 ) % kCrystalsInPhi ) + 1;
00087 } else {
00088 ip = kCrystalsInPhi - ((iphi() -1 ) % kCrystalsInPhi );
00089 }
00090
00091 return (ie * kCrystalsInPhi) + ip;
00092 }
00093
00094
00095 int EBDetId::numberBySM() const {
00096 return (ism()-1) * kCrystalsPerSM + ic() -1;
00097 }
00098
00099
00100 int EBDetId::tower_iphi() const {
00101 int iphi_simple=((iphi()-1)/5)+1;
00102 iphi_simple-=2;
00103 return ((iphi_simple<=0)?(iphi_simple+72):(iphi_simple));
00104 }
00105
00106
00107 bool EBDetId::isNextToBoundary(EBDetId id) {
00108 return isNextToEtaBoundary( id ) || isNextToPhiBoundary( id );
00109 }
00110
00111 bool EBDetId::isNextToEtaBoundary(EBDetId id) {
00112 int ieta = id.ietaSM();
00113 return ieta == 1 || std::binary_search( kModuleBoundaries, kModuleBoundaries + 4, ieta ) || ieta == 85;
00114 }
00115
00116 bool EBDetId::isNextToPhiBoundary(EBDetId id) {
00117 int iphi = id.iphiSM();
00118 return iphi == 1 || iphi == 20;
00119 }
00120
00121
00122
00123 std::ostream& operator<<(std::ostream& s,const EBDetId& id) {
00124 return s << "(EB ieta " << id.ieta() << ", iphi" << id.iphi()
00125 << " ; ism " << id.ism() << " , ic " << id.ic() << ')';
00126 }
00127