CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/DataFormats/HcalDetId/src/HcalDetId.cc

Go to the documentation of this file.
00001 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 #include <ostream>
00004 
00005 const HcalDetId HcalDetId::Undefined(HcalEmpty,0,0,0);
00006 
00007 HcalDetId::HcalDetId() : DetId() {
00008 }
00009 
00010 HcalDetId::HcalDetId(uint32_t rawid) : DetId(rawid) {
00011 }
00012 
00013 HcalDetId::HcalDetId(HcalSubdetector subdet, int tower_ieta, int tower_iphi, int depth) : DetId(Hcal,subdet) {
00014   // (no checking at this point!)
00015   id_ |= ((depth&0x1F)<<14) |
00016     ((tower_ieta>0)?(0x2000|(tower_ieta<<7)):((-tower_ieta)<<7)) |
00017     (tower_iphi&0x7F);
00018 }
00019 
00020 HcalDetId::HcalDetId(const DetId& gen) {
00021   if (!gen.null()) {
00022     HcalSubdetector subdet=(HcalSubdetector(gen.subdetId()));
00023     if (gen.det()!=Hcal || 
00024         (subdet!=HcalBarrel && subdet!=HcalEndcap && 
00025          subdet!=HcalOuter && subdet!=HcalForward ))
00026       {
00027         throw cms::Exception("Invalid DetId") << "Cannot initialize HcalDetId from " << std::hex << gen.rawId() << std::dec; 
00028       }  
00029   }
00030   id_=gen.rawId();
00031 }
00032 
00033 HcalDetId& HcalDetId::operator=(const DetId& gen) {
00034   if (!gen.null()) {
00035     HcalSubdetector subdet=(HcalSubdetector(gen.subdetId()));
00036     if (gen.det()!=Hcal || 
00037         (subdet!=HcalBarrel && subdet!=HcalEndcap && 
00038          subdet!=HcalOuter && subdet!=HcalForward ))
00039       {
00040         throw cms::Exception("Invalid DetId") << "Cannot assign HcalDetId from " << std::hex << gen.rawId() << std::dec; 
00041       }  
00042   }
00043   id_=gen.rawId();
00044   return (*this);
00045 }
00046 
00047 int HcalDetId::crystal_iphi_low() const { 
00048   int simple_iphi=((iphi()-1)*5)+1; 
00049   simple_iphi+=10;
00050   return ((simple_iphi>360)?(simple_iphi-360):(simple_iphi));
00051 }
00052 
00053 int HcalDetId::crystal_iphi_high() const { 
00054   int simple_iphi=((iphi()-1)*5)+5; 
00055   simple_iphi+=10;
00056   return ((simple_iphi>360)?(simple_iphi-360):(simple_iphi));
00057 }
00058 
00059 std::ostream& operator<<(std::ostream& s,const HcalDetId& id) {
00060   switch (id.subdet()) {
00061   case(HcalBarrel) : return s << "(HB " << id.ieta() << ',' << id.iphi() << ',' << id.depth() << ')';
00062   case(HcalEndcap) : return s << "(HE " << id.ieta() << ',' << id.iphi() << ',' << id.depth() << ')';
00063   case(HcalForward) : return s << "(HF " << id.ieta() << ',' << id.iphi() << ',' << id.depth() << ')';
00064   case(HcalOuter) : return s << "(HO " << id.ieta() << ',' << id.iphi() << ')';
00065   default : return s << id.rawId();
00066   }
00067 }
00068 
00069 bool 
00070 HcalDetId::validDetId( HcalSubdetector sd,
00071                        int             ies,
00072                        int             ip,
00073                        int             dp      ) 
00074 {
00075    const int ie ( abs( ies ) ) ;
00076 
00077    return ( ( ip >=  1         ) &&
00078             ( ip <= 72         ) &&
00079             ( dp >=  1         ) &&
00080             ( ie >=  1         ) &&
00081             ( ( ( sd == HcalBarrel ) &&
00082                 ( ( ( ie <= 14         ) &&
00083                     ( dp ==  1         )    ) ||
00084                   ( ( ( ie == 15 ) || ( ie == 16 ) ) && 
00085                     ( dp <= 2          )                ) ) ) ||
00086               (  ( sd == HcalEndcap ) &&
00087                  ( ( ( ie == 16 ) &&
00088                      ( dp ==  3 )          ) ||
00089                    ( ( ie == 17 ) &&
00090                      ( dp ==  1 )          ) ||
00091                    ( ( ie >= 18 ) &&
00092                      ( ie <= 20 ) &&
00093                      ( dp <=  2 )          ) ||
00094                    ( ( ie >= 21 ) &&
00095                      ( ie <= 26 ) &&
00096                      ( dp <=  2 ) &&
00097                      ( ip%2 == 1 )         ) ||
00098                    ( ( ie >= 27 ) &&
00099                      ( ie <= 28 ) &&
00100                      ( dp <=  3 ) &&
00101                      ( ip%2 == 1 )         ) ||
00102                    ( ( ie == 29 ) &&
00103                      ( dp <=  2 ) &&
00104                      ( ip%2 == 1 )         )          )      ) ||
00105               (  ( sd == HcalOuter ) &&
00106                  ( ie <= 15 ) &&
00107                  ( dp ==  4 )           ) ||
00108               (  ( sd == HcalForward ) &&
00109                  ( dp <=  2 )          &&
00110                  ( ( ( ie >= 29 ) &&
00111                      ( ie <= 39 ) &&
00112                      ( ip%2 == 1 )    ) ||
00113                    ( ( ie >= 40 ) &&
00114                      ( ie <= 41 ) &&
00115                      ( ip%4 == 3 )         )  ) ) ) ) ;
00116 }
00117 
00118 int 
00119 HcalDetId::hashed_index() const 
00120 {
00121    const HcalSubdetector sd ( subdet()  ) ;
00122    const int             ip ( iphi()    ) ;
00123    const int             ie ( ietaAbs() ) ;
00124    const int             dp ( depth()   ) ;
00125    const int             zn ( zside() < 0 ? 1 : 0 ) ;
00126 
00127    // HB valid DetIds: phi=1-72,eta=1-14,depth=1; phi=1-72,eta=15-16,depth=1-2
00128   
00129    // HE valid DetIds: phi=1-72,eta=16-17,depth=1; phi=1-72,eta=18-20,depth=1-2; 
00130    //                  phi=1-71(in steps of 2),eta=21-26,depth=1-2; phi=1-71(in steps of 2),eta=27-28,depth=1-3
00131    //                  phi=1-71(in steps of 2),eta=29,depth=1-2
00132 
00133    // HO valid DetIds: phi=1-72,eta=1-15,depth=4!
00134 
00135    // HF valid DetIds: phi=1-71(in steps of 2),eta=29-39,depth=1-2; phi=3-71(in steps of 4),eta=40-41,depth=1-2
00136 
00137    return ( ( sd == HcalBarrel ) ?
00138             ( ip - 1 )*18 + dp - 1 + ie - ( ie<16 ? 1 : 0 ) + zn*kHBhalf :
00139             ( ( sd == HcalEndcap ) ?
00140               2*kHBhalf + ( ip - 1 )*8 + ( ip/2 )*20 +
00141               ( ( ie==16 || ie==17 ) ? ie - 16 :
00142                 ( ( ie>=18 && ie<=20 ) ? 2 + 2*( ie - 18 ) + dp - 1 :
00143                   ( ( ie>=21 && ie<=26 ) ? 8 + 2*( ie - 21 ) + dp - 1 :
00144                     ( ( ie>=27 && ie<=28 ) ? 20 + 3*( ie - 27 ) + dp - 1 :
00145                       26 + 2*( ie - 29 ) + dp - 1 ) ) ) ) + zn*kHEhalf :
00146               ( ( sd == HcalOuter ) ?
00147                 2*kHBhalf + 2*kHEhalf + ( ip - 1 )*15 + ( ie - 1 ) + zn*kHOhalf :
00148                 ( ( sd == HcalForward ) ?
00149                   2*kHBhalf + 2*kHEhalf + 2*kHOhalf + 
00150                   ( ( ip - 1 )/4 )*4 + ( ( ip - 1 )/2 )*22 + 
00151                   2*( ie - 29 ) + ( dp - 1 ) + zn*kHFhalf : -1 ) ) ) ) ; 
00152 }
00153 
00154 HcalDetId 
00155 HcalDetId::detIdFromDenseIndex( uint32_t di )
00156 {
00157     if( validDenseIndex( di ) )
00158     {
00159       HcalSubdetector sd ( HcalBarrel ) ;
00160       int ie ( 0 ) ;
00161       int ip ( 0 ) ;
00162       int dp ( 0 ) ;
00163       int in ( di ) ;
00164       int iz ( 1 ) ;
00165       if( in > 2*( kHBhalf + kHEhalf + kHOhalf ) - 1 )
00166       {
00167          sd  = HcalForward ;
00168          in -= 2*( kHBhalf + kHEhalf + kHOhalf ) ; 
00169          iz  = ( in<kHFhalf ? 1 : -1 ) ;
00170          in %= kHFhalf ; 
00171          ip  = 4*( in/48 ) ;
00172          in %= 48 ;
00173          ip += 1 + ( in>21 ? 2 : 0 ) ;
00174          if( 3 == ip%4 ) in -= 22 ;
00175          ie  = 29 + in/2 ;
00176          dp  = 1 + in%2 ;
00177       }
00178       else
00179       {
00180          if( in > 2*( kHBhalf + kHEhalf ) - 1 )
00181          {
00182             sd  = HcalOuter ;
00183             in -= 2*( kHBhalf + kHEhalf ) ; 
00184             iz  = ( in<kHOhalf ? 1 : -1 ) ;
00185             in %= kHOhalf ; 
00186             dp  = 4 ;
00187             ip  = 1 + in/15 ;
00188             ie  = 1 + ( in - 15*( ip - 1 ) ) ;
00189          }
00190          else
00191          {
00192             if( in > 2*kHBhalf - 1 )
00193             {
00194                sd  = HcalEndcap ;
00195                in -= 2*kHBhalf ;
00196                iz  = ( in<kHEhalf ? 1 : -1 ) ;
00197                in %= kHEhalf ; 
00198                ip  = 2*( in/36 ) ;
00199                in %= 36 ;
00200                ip += 1 + in/28 ;
00201                if( 0 == ip%2 ) in %= 28 ;
00202                ie  = 15 + ( in<2 ? 1 + in : 2 + 
00203                             ( in<20 ? 1 + ( in - 2 )/2 : 9 +
00204                               ( in<26 ? 1 + ( in - 20 )/3 : 3 ) ) ) ;
00205                dp  = ( in<1 ? 3 :
00206                        ( in<2 ? 1 : 
00207                          ( in<20 ? 1 + ( in - 2 )%2 : 
00208                            ( in<26 ? 1 + ( in - 20 )%3 : 
00209                              ( 1 + ( in - 26 )%2 ) ) ) ) ) ;
00210             }
00211             else // barrel
00212             {
00213                iz  = ( di<kHBhalf ? 1 : -1 ) ;
00214                in %= kHBhalf ; 
00215                ip = in/18 + 1 ;
00216                in %= 18 ;
00217                if( in < 14 )
00218                {
00219                   dp = 1 ;
00220                   ie = in + 1 ;
00221                }
00222                else
00223                {
00224                   in %= 14 ;
00225                   dp =  1 + in%2 ;
00226                   ie = 15 + in/2 ;
00227                }
00228             }
00229          }
00230       }
00231       return HcalDetId( sd, iz*int(ie), ip, dp ) ;
00232    }
00233    else
00234    {
00235       return HcalDetId() ;
00236    }
00237 }