CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/DataFormats/SiStripCommon/src/SiStripFecKey.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripFecKey.cc,v 1.22 2009/10/23 13:07:17 lowette Exp $
00002 
00003 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
00004 #include "DataFormats/SiStripCommon/interface/SiStripNullKey.h"
00005 #include "DataFormats/SiStripCommon/interface/Constants.h" 
00006 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"
00007 #include "DataFormats/SiStripCommon/interface/ConstantsForDqm.h"
00008 #include "DataFormats/SiStripCommon/interface/ConstantsForView.h"
00009 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00010 #include <iomanip>
00011 
00012 // -----------------------------------------------------------------------------
00013 //
00014 SiStripFecKey::SiStripFecKey( const uint16_t& fec_crate, 
00015                               const uint16_t& fec_slot, 
00016                               const uint16_t& fec_ring, 
00017                               const uint16_t& ccu_addr, 
00018                               const uint16_t& ccu_chan,
00019                               const uint16_t& lld_chan,
00020                               const uint16_t& i2c_addr ) :
00021   SiStripKey(),
00022   fecCrate_(fec_crate), 
00023   fecSlot_(fec_slot),
00024   fecRing_(fec_ring), 
00025   ccuAddr_(ccu_addr),
00026   ccuChan_(ccu_chan),
00027   lldChan_(lld_chan),
00028   i2cAddr_(i2c_addr)
00029 {
00030   // order is important!
00031   initFromValue();
00032   initFromKey();
00033   initFromPath();
00034   initGranularity();
00035 }
00036 
00037 // -----------------------------------------------------------------------------
00038 // 
00039 SiStripFecKey::SiStripFecKey( const uint32_t& fec_key ) :
00040   SiStripKey(fec_key),
00041   fecCrate_(sistrip::invalid_), 
00042   fecSlot_(sistrip::invalid_),
00043   fecRing_(sistrip::invalid_), 
00044   ccuAddr_(sistrip::invalid_),
00045   ccuChan_(sistrip::invalid_), 
00046   lldChan_(sistrip::invalid_),
00047   i2cAddr_(sistrip::invalid_)
00048 {
00049   // order is important!
00050   initFromKey(); 
00051   initFromValue();
00052   initFromPath();
00053   initGranularity();
00054 }
00055 
00056 // -----------------------------------------------------------------------------
00057 // 
00058 SiStripFecKey::SiStripFecKey( const std::string& path ) :
00059   SiStripKey(path),
00060   fecCrate_(sistrip::invalid_), 
00061   fecSlot_(sistrip::invalid_),
00062   fecRing_(sistrip::invalid_), 
00063   ccuAddr_(sistrip::invalid_),
00064   ccuChan_(sistrip::invalid_), 
00065   lldChan_(sistrip::invalid_),
00066   i2cAddr_(sistrip::invalid_)
00067 {
00068   // order is important!
00069   initFromPath();
00070   initFromValue();
00071   initFromKey(); 
00072   initGranularity();
00073 }
00074 
00075 // -----------------------------------------------------------------------------
00076 // 
00077 SiStripFecKey::SiStripFecKey( const SiStripFecKey& input ) :
00078   SiStripKey(),
00079   fecCrate_(input.fecCrate()), 
00080   fecSlot_(input.fecSlot()),
00081   fecRing_(input.fecRing()), 
00082   ccuAddr_(input.ccuAddr()),
00083   ccuChan_(input.ccuChan()), 
00084   lldChan_(input.lldChan()), 
00085   i2cAddr_(input.i2cAddr())
00086 {
00087   key(input.key());
00088   path(input.path());
00089   granularity(input.granularity());
00090 }
00091 
00092 // -----------------------------------------------------------------------------
00093 // 
00094 SiStripFecKey::SiStripFecKey( const SiStripKey& input ) :
00095   SiStripKey(),
00096   fecCrate_(sistrip::invalid_), 
00097   fecSlot_(sistrip::invalid_),
00098   fecRing_(sistrip::invalid_), 
00099   ccuAddr_(sistrip::invalid_),
00100   ccuChan_(sistrip::invalid_), 
00101   lldChan_(sistrip::invalid_),
00102   i2cAddr_(sistrip::invalid_)
00103 {
00104   SiStripKey& temp = const_cast<SiStripKey&>(input);
00105   SiStripFecKey& fec_key = dynamic_cast<SiStripFecKey&>(temp);
00106   if ( (&fec_key) ) {
00107     key(fec_key.key());
00108     path(fec_key.path());
00109     granularity(fec_key.granularity());
00110     fecCrate_ = fec_key.fecCrate(); 
00111     fecSlot_ = fec_key.fecSlot();
00112     fecRing_ = fec_key.fecRing(); 
00113     ccuAddr_ = fec_key.ccuAddr();
00114     ccuChan_ = fec_key.ccuChan(); 
00115     lldChan_ = fec_key.lldChan();
00116     i2cAddr_ = fec_key.i2cAddr();
00117   }
00118 }
00119 
00120 // -----------------------------------------------------------------------------
00121 // 
00122 SiStripFecKey::SiStripFecKey( const SiStripKey& input,
00123                               const sistrip::Granularity& gran ) :
00124   SiStripKey(),
00125   fecCrate_(0), 
00126   fecSlot_(0),
00127   fecRing_(0), 
00128   ccuAddr_(0),
00129   ccuChan_(0), 
00130   lldChan_(0),
00131   i2cAddr_(0)
00132 {
00133   SiStripKey& temp = const_cast<SiStripKey&>(input);
00134   SiStripFecKey& fec_key = dynamic_cast<SiStripFecKey&>(temp);
00135   if ( (&fec_key) ) {
00136     
00137     if ( gran == sistrip::FEC_CRATE || gran == sistrip::FEC_SLOT ||
00138          gran == sistrip::FEC_RING || gran == sistrip::CCU_ADDR ||
00139          gran == sistrip::CCU_CHAN || gran == sistrip::LLD_CHAN ||
00140          gran == sistrip::APV ) {
00141       fecCrate_ = fec_key.fecCrate(); 
00142     }
00143 
00144     if ( gran == sistrip::FEC_SLOT || gran == sistrip::FEC_RING ||
00145          gran == sistrip::CCU_ADDR || gran == sistrip::CCU_CHAN ||
00146          gran == sistrip::LLD_CHAN || gran == sistrip::APV ) {
00147       fecSlot_ = fec_key.fecSlot();
00148     }
00149 
00150     if ( gran == sistrip::FEC_RING || gran == sistrip::CCU_ADDR ||
00151          gran == sistrip::CCU_CHAN || gran == sistrip::LLD_CHAN ||
00152          gran == sistrip::APV ) {
00153       fecRing_ = fec_key.fecRing(); 
00154     }
00155 
00156     if ( gran == sistrip::CCU_ADDR || gran == sistrip::CCU_CHAN ||
00157          gran == sistrip::LLD_CHAN || gran == sistrip::APV ) {
00158       ccuAddr_ = fec_key.ccuAddr();
00159     }
00160 
00161     if ( gran == sistrip::CCU_CHAN || gran == sistrip::LLD_CHAN ||
00162          gran == sistrip::APV ) {
00163       ccuChan_ = fec_key.ccuChan(); 
00164     }
00165 
00166     if ( gran == sistrip::LLD_CHAN || gran == sistrip::APV ) {
00167       lldChan_ = fec_key.lldChan();
00168     }
00169 
00170     if ( gran == sistrip::APV ) {
00171       i2cAddr_ = fec_key.i2cAddr();
00172     }
00173 
00174     initFromValue();
00175     initFromKey();
00176     initFromPath();
00177     initGranularity();
00178     
00179   }
00180 
00181 }
00182 
00183 // -----------------------------------------------------------------------------
00184 // 
00185 SiStripFecKey::SiStripFecKey() :
00186   SiStripKey(),
00187   fecCrate_(sistrip::invalid_), 
00188   fecSlot_(sistrip::invalid_),
00189   fecRing_(sistrip::invalid_), 
00190   ccuAddr_(sistrip::invalid_),
00191   ccuChan_(sistrip::invalid_), 
00192   lldChan_(sistrip::invalid_),
00193   i2cAddr_(sistrip::invalid_)
00194 {;}
00195 
00196 // -----------------------------------------------------------------------------
00197 // 
00198 uint16_t SiStripFecKey::hybridPos( const uint16_t& i2c_addr ) {
00199   if ( i2c_addr < sistrip::APV_I2C_MIN ||
00200        i2c_addr > sistrip::APV_I2C_MAX ) {
00201     return sistrip::invalid_;
00202   }
00203   return ( i2c_addr - sistrip::APV_I2C_MIN + 1 );
00204 }
00205 
00206 // -----------------------------------------------------------------------------
00207 // 
00208 uint16_t SiStripFecKey::i2cAddr( const uint16_t& hybrid_pos ) {
00209   if ( !hybrid_pos ||
00210        hybrid_pos > 
00211        ( sistrip::APV_I2C_MAX - 
00212          sistrip::APV_I2C_MIN + 1 ) ) {
00213     return sistrip::invalid_;
00214   }
00215   return ( hybrid_pos + sistrip::APV_I2C_MIN - 1 );
00216 }
00217 
00218 // -----------------------------------------------------------------------------
00219 // 
00220 uint16_t SiStripFecKey::i2cAddr( const uint16_t& lld_chan,
00221                                  const bool& first_apv ) {
00222   if ( lld_chan < sistrip::LLD_CHAN_MIN ||
00223        lld_chan > sistrip::LLD_CHAN_MAX ) {
00224     return sistrip::invalid_; 
00225   }
00226   return ( sistrip::APV_I2C_MIN + lld_chan * sistrip::APVS_PER_CHAN - (first_apv?2:1) );
00227 }
00228 
00229 // -----------------------------------------------------------------------------
00230 // 
00231 uint16_t SiStripFecKey::lldChan( const uint16_t& i2c_addr ) {
00232   if ( i2c_addr == 0 ) { return 0; }
00233   else if ( i2c_addr < sistrip::APV_I2C_MIN ||
00234             i2c_addr > sistrip::APV_I2C_MAX ) {
00235     return sistrip::invalid_;
00236   }
00237   return ( ( i2c_addr - sistrip::APV_I2C_MIN ) / 2 + 1 );
00238 }
00239 
00240 // -----------------------------------------------------------------------------
00241 // 
00242 bool SiStripFecKey::firstApvOfPair( const uint16_t& i2c_addr ) {
00243   if ( i2c_addr < sistrip::APV_I2C_MIN ||
00244        i2c_addr > sistrip::APV_I2C_MAX ) {
00245     return sistrip::invalid_;
00246   }
00247   return ( ( ( i2c_addr - sistrip::APV_I2C_MIN ) % 2 ) == 0 );
00248 }
00249 
00250 // -----------------------------------------------------------------------------
00251 // 
00252 bool SiStripFecKey::isEqual( const SiStripKey& key ) const {
00253   SiStripKey& temp = const_cast<SiStripKey&>(key);
00254   SiStripFecKey& input = dynamic_cast<SiStripFecKey&>(temp);
00255   if ( !(&input) ) { return false; }
00256   if ( fecCrate_ == input.fecCrate() &&
00257        fecSlot_ == input.fecSlot() &&
00258        fecRing_ == input.fecRing() &&
00259        ccuAddr_ == input.ccuAddr() &&
00260        ccuChan_ == input.ccuChan() &&
00261        lldChan_ == input.lldChan() &&
00262        i2cAddr_ == input.i2cAddr() ) { 
00263     return true;
00264   } else { return false; }
00265 }
00266 
00267 // -----------------------------------------------------------------------------
00268 // 
00269 bool SiStripFecKey::isConsistent( const SiStripKey& key ) const {
00270   SiStripKey& temp = const_cast<SiStripKey&>(key);
00271   SiStripFecKey& input = dynamic_cast<SiStripFecKey&>(temp);
00272   if ( !(&input) ) { return false; }
00273   if ( isEqual(input) ) { return true; }
00274   else if ( ( fecCrate_ == 0 || input.fecCrate() == 0 ) &&
00275             ( fecSlot_ == 0 || input.fecSlot() == 0 ) &&
00276             ( fecRing_ == 0 || input.fecRing() == 0 ) &&
00277             ( ccuAddr_ == 0 || input.ccuAddr() == 0 ) &&
00278             ( lldChan_ == 0 || input.lldChan() == 0 ) &&
00279             ( i2cAddr_ == 0 || input.i2cAddr() == 0 ) ) {
00280     return true;
00281   } else { return false; }
00282 }
00283 
00284 // -----------------------------------------------------------------------------
00285 //
00286 bool SiStripFecKey::isValid() const { 
00287   return isValid(sistrip::APV); 
00288 }
00289 
00290 // -----------------------------------------------------------------------------
00291 //
00292 bool SiStripFecKey::isValid( const sistrip::Granularity& gran ) const {
00293   if ( gran == sistrip::FEC_SYSTEM ) { return true; }
00294   else if ( gran == sistrip::UNDEFINED_GRAN ||
00295             gran == sistrip::UNKNOWN_GRAN ) { return false; }
00296   
00297   if ( fecCrate_ != sistrip::invalid_ ) {
00298     if ( gran == sistrip::FEC_CRATE ) { return true; }
00299     if ( fecSlot_ != sistrip::invalid_ ) {
00300       if ( gran == sistrip::FEC_RING ) { return true; }
00301       if ( fecRing_ != sistrip::invalid_ ) {
00302         if ( gran == sistrip::FEC_RING ) { return true; }
00303         if ( ccuAddr_ != sistrip::invalid_ ) {
00304           if ( gran == sistrip::CCU_ADDR ) { return true; }
00305           if ( ccuChan_ != sistrip::invalid_ ) {
00306             if ( gran == sistrip::CCU_CHAN ) { return true; }
00307             if ( lldChan_ != sistrip::invalid_ ) {
00308               if ( gran == sistrip::LLD_CHAN ) { return true; }
00309               if ( i2cAddr_ != sistrip::invalid_ ) {
00310                 if ( gran == sistrip::APV ) { return true; }
00311               }
00312             }
00313           }
00314         }
00315       }
00316     }
00317   }
00318   return false;
00319 }
00320 
00321 // -----------------------------------------------------------------------------
00322 //
00323 bool SiStripFecKey::isInvalid() const { 
00324   return isInvalid(sistrip::APV); 
00325 }
00326 
00327 // -----------------------------------------------------------------------------
00328 //
00329 bool SiStripFecKey::isInvalid( const sistrip::Granularity& gran ) const {
00330   if ( gran == sistrip::FEC_SYSTEM ) { return false; }
00331   else if ( gran == sistrip::UNDEFINED_GRAN ||
00332             gran == sistrip::UNKNOWN_GRAN ) { return false; }
00333   
00334   if ( fecCrate_ == sistrip::invalid_ ) {
00335     if ( gran == sistrip::FEC_CRATE ) { return true; }
00336     if ( fecSlot_ == sistrip::invalid_ ) {
00337       if ( gran == sistrip::FEC_RING ) { return true; }
00338       if ( fecRing_ == sistrip::invalid_ ) {
00339         if ( gran == sistrip::FEC_RING ) { return true; }
00340         if ( ccuAddr_ == sistrip::invalid_ ) {
00341           if ( gran == sistrip::CCU_ADDR ) { return true; }
00342           if ( ccuChan_ == sistrip::invalid_ ) {
00343             if ( gran == sistrip::CCU_CHAN ) { return true; }
00344             if ( lldChan_ == sistrip::invalid_ ) {
00345               if ( gran == sistrip::LLD_CHAN  ) { return true; }
00346               if ( i2cAddr_ == sistrip::invalid_ ) {
00347                 if ( gran == sistrip::APV  ) { return true; }
00348               }
00349             }
00350           }
00351         }
00352       }
00353     }
00354   }
00355   return false;
00356 }
00357 
00358 // -----------------------------------------------------------------------------
00359 //
00360 void SiStripFecKey::initFromValue() {
00361 
00362   // FEC crate  
00363   if ( fecCrate_ >= sistrip::FEC_CRATE_MIN &&
00364        fecCrate_ <= sistrip::FEC_CRATE_MAX ) {
00365     fecCrate_ = fecCrate_;
00366   } else if ( fecCrate_ == 0 ) { 
00367     fecCrate_ = 0;
00368   } else { fecCrate_ = sistrip::invalid_; }
00369 
00370   // FEC slot
00371   if ( fecSlot_ >= sistrip::CRATE_SLOT_MIN &&
00372        fecSlot_ <= sistrip::CRATE_SLOT_MAX ) {
00373     fecSlot_ = fecSlot_;
00374   } else if ( fecSlot_ == 0 ) { 
00375     fecSlot_ = 0;
00376   } else { fecSlot_ = sistrip::invalid_; }
00377 
00378   // FEC ring
00379   if ( fecRing_ >= sistrip::FEC_RING_MIN &&
00380        fecRing_ <= sistrip::FEC_RING_MAX ) {
00381     fecRing_ = fecRing_;
00382   } else if ( fecRing_ == 0 ) { 
00383     fecRing_ = 0;
00384   } else { fecRing_ = sistrip::invalid_; }
00385 
00386   // CCU addr
00387   if ( ccuAddr_ >= sistrip::CCU_ADDR_MIN &&
00388        ccuAddr_ <= sistrip::CCU_ADDR_MAX ) {
00389     ccuAddr_ = ccuAddr_;
00390   } else if ( ccuAddr_ == 0 ) { 
00391     ccuAddr_ = 0;
00392   } else { ccuAddr_ = sistrip::invalid_; }
00393 
00394   // CCU chan
00395   if ( ccuChan_ >= sistrip::CCU_CHAN_MIN &&
00396        ccuChan_ <= sistrip::CCU_CHAN_MAX ) {
00397     ccuChan_ = ccuChan_;
00398   } else if ( ccuChan_ == 0 ) { 
00399     ccuChan_ = 0;
00400   } else { ccuChan_ = sistrip::invalid_; }
00401   
00402   // LLD channel
00403   if ( lldChan_ >= sistrip::LLD_CHAN_MIN &&
00404        lldChan_ <= sistrip::LLD_CHAN_MAX ) {
00405     lldChan_ = lldChan_;
00406   } else if ( lldChan_ == 0 ) { 
00407     lldChan_ = 0;
00408   } else { lldChan_ = sistrip::invalid_; }
00409   
00410   // APV I2C address
00411   if ( i2cAddr_ >= sistrip::APV_I2C_MIN &&
00412        i2cAddr_ <= sistrip::APV_I2C_MAX ) { 
00413     i2cAddr_ = i2cAddr_; 
00414     if ( lldChan_ && lldChan( i2cAddr_ ) != lldChan_ ) { 
00415       i2cAddr_ = sistrip::invalid_;
00416       key( key() | (i2cAddrMask_<<i2cAddrOffset_) ); 
00417     }
00418   } else if ( i2cAddr_ == 0 ) { 
00419     i2cAddr_ = 0;
00420   } else { i2cAddr_ = sistrip::invalid_; }
00421   
00422 }
00423 
00424 // -----------------------------------------------------------------------------
00425 //
00426 void SiStripFecKey::initFromKey() {
00427   
00428   if ( key() == sistrip::invalid32_ ) { 
00429 
00430     // ---------- Set FecKey based on member data ----------
00431     
00432     // Initialise to null value
00433     key(0);
00434     
00435     // Extract FEC crate  
00436     if ( fecCrate_ >= sistrip::FEC_CRATE_MIN &&
00437          fecCrate_ <= sistrip::FEC_CRATE_MAX ) {
00438       key( key() | (fecCrate_<<fecCrateOffset_) );
00439     } else if ( fecCrate_ == 0 ) { 
00440       key( key() | (fecCrate_<<fecCrateOffset_) );
00441     } else { 
00442       key( key() | (fecCrateMask_<<fecCrateOffset_) ); 
00443     }
00444 
00445     // Extract FEC slot
00446     if ( fecSlot_ >= sistrip::CRATE_SLOT_MIN &&
00447          fecSlot_ <= sistrip::CRATE_SLOT_MAX ) {
00448       key( key() | (fecSlot_<<fecSlotOffset_) );
00449     } else if ( fecSlot_ == 0 ) { 
00450       key( key() | (fecSlot_<<fecSlotOffset_) );
00451     } else { 
00452       key( key() | (fecSlotMask_<<fecSlotOffset_) ); 
00453     }
00454 
00455     // Extract FEC ring
00456     if ( fecRing_ >= sistrip::FEC_RING_MIN &&
00457          fecRing_ <= sistrip::FEC_RING_MAX ) {
00458       key( key() | (fecRing_<<fecRingOffset_) );
00459     } else if ( fecRing_ == 0 ) { 
00460       key( key() | (fecRing_<<fecRingOffset_) );
00461     } else { 
00462       key( key() | (fecRingMask_<<fecRingOffset_) ); 
00463     }
00464 
00465     // Extract CCU addr
00466     if ( ccuAddr_ >= sistrip::CCU_ADDR_MIN &&
00467          ccuAddr_ <= sistrip::CCU_ADDR_MAX ) {
00468       key( key() | (ccuAddr_<<ccuAddrOffset_) );
00469     } else if ( ccuAddr_ == 0 ) { 
00470       key( key() | (ccuAddr_<<ccuAddrOffset_) );
00471     } else { 
00472       key( key() | (ccuAddrMask_<<ccuAddrOffset_) ); 
00473     }
00474 
00475     // Extract CCU chan
00476     if ( ccuChan_ >= sistrip::CCU_CHAN_MIN &&
00477          ccuChan_ <= sistrip::CCU_CHAN_MAX ) {
00478       key( key() | ( (ccuChan_-(sistrip::CCU_CHAN_MIN-1)) << ccuChanOffset_ ) ); 
00479     } else if ( ccuChan_ == 0 ) { 
00480       key( key() | (ccuChan_<<ccuChanOffset_) );
00481     } else { 
00482       key( key() | (ccuChanMask_<<ccuChanOffset_) ); 
00483     }
00484     
00485     // Extract LLD channel
00486     if ( lldChan_ >= sistrip::LLD_CHAN_MIN &&
00487          lldChan_ <= sistrip::LLD_CHAN_MAX ) {
00488       key( key() | (lldChan_<<lldChanOffset_) ); 
00489     } else if ( lldChan_ == 0 ) { 
00490       key( key() | (lldChan_<<lldChanOffset_) );
00491     } else { 
00492       key( key() | (lldChanMask_<<lldChanOffset_) ); 
00493     }
00494     
00495     // Extract APV I2C address
00496     if ( i2cAddr_ >= sistrip::APV_I2C_MIN &&
00497          i2cAddr_ <= sistrip::APV_I2C_MAX ) {
00498       key( key() | ( ( firstApvOfPair( i2cAddr_ ) ? 1 : 2 ) << i2cAddrOffset_ ) ); // key encodes APV number (1 or 2)
00499       if ( lldChan_ && lldChan( i2cAddr_ ) != lldChan_ ) { 
00500         i2cAddr_ = sistrip::invalid_;
00501         key( key() | (i2cAddrMask_<<i2cAddrOffset_) ); 
00502       }
00503     } else if ( i2cAddr_ == 0 ) { 
00504       key( key() | (i2cAddr_<<i2cAddrOffset_) );
00505     } else { 
00506       key( key() | (i2cAddrMask_<<i2cAddrOffset_) ); 
00507     }
00508     
00509   } else {
00510     
00511     // ---------- Set member data based on FEC key ----------
00512 
00513     fecCrate_ = ( key()>>fecCrateOffset_ ) & fecCrateMask_;
00514     fecSlot_  = ( key()>>fecSlotOffset_ )  & fecSlotMask_;
00515     fecRing_  = ( key()>>fecRingOffset_ )  & fecRingMask_;
00516     ccuAddr_  = ( key()>>ccuAddrOffset_ )  & ccuAddrMask_;
00517     ccuChan_  = ( key()>>ccuChanOffset_ )  & ccuChanMask_;
00518     lldChan_  = ( key()>>lldChanOffset_ )  & lldChanMask_;
00519     i2cAddr_  = ( key()>>i2cAddrOffset_ )  & i2cAddrMask_;
00520 
00521     if ( fecCrate_ == fecCrateMask_ ) { fecCrate_ = sistrip::invalid_; } 
00522     if ( fecSlot_ == fecSlotMask_ ) { fecSlot_ = sistrip::invalid_; } 
00523     if ( fecRing_ == fecRingMask_ ) { fecRing_ = sistrip::invalid_; } 
00524     if ( ccuAddr_ == ccuAddrMask_ ) { ccuAddr_ = sistrip::invalid_; } 
00525     if ( ccuChan_ == ccuChanMask_ ) { ccuChan_ = sistrip::invalid_; }
00526     else if ( ccuChan_ ) { ccuChan_ += (sistrip::CCU_CHAN_MIN-1); }
00527     if ( lldChan_ == lldChanMask_ ) { lldChan_ = sistrip::invalid_; }
00528     if ( i2cAddr_ == i2cAddrMask_ ) { i2cAddr_ = sistrip::invalid_; }
00529     else if ( i2cAddr_ && lldChan_ != lldChanMask_ ) { i2cAddr_ = i2cAddr( lldChan_, 2-i2cAddr_ ); }
00530     
00531   }
00532   
00533 }
00534 
00535 // -----------------------------------------------------------------------------
00536 // 
00537 void SiStripFecKey::initFromPath() {
00538   
00539   if ( path() == sistrip::null_ ) {
00540     
00541     // ---------- Set directory path based on member data ----------
00542 
00543     std::stringstream dir;
00544     
00545     dir << sistrip::root_ << sistrip::dir_ 
00546         << sistrip::controlView_ << sistrip::dir_;
00547 
00548     // Add FEC crate
00549     if ( fecCrate_ ) {
00550       dir << sistrip::fecCrate_ << fecCrate_ << sistrip::dir_;
00551       
00552       // Add FEC slot
00553       if ( fecSlot_ ) {
00554         dir << sistrip::fecSlot_ << fecSlot_ << sistrip::dir_;
00555         
00556         // Add FEC ring
00557         if ( fecRing_ ) {
00558           dir << sistrip::fecRing_ << fecRing_ << sistrip::dir_;
00559           
00560           // Add CCU address
00561           if ( ccuAddr_ ) {
00562             dir << sistrip::ccuAddr_ << ccuAddr_ << sistrip::dir_;
00563             
00564             // Add CCU channel
00565             if ( ccuChan_ ) {
00566               dir << sistrip::ccuChan_ << ccuChan_ << sistrip::dir_;
00567 
00568               // Add LLD channel
00569               if ( lldChan_ ) {
00570                 dir << sistrip::lldChan_ << lldChan_ << sistrip::dir_;
00571 
00572                 // Add APV I2C address
00573                 if ( i2cAddr_ ) {
00574                   dir << sistrip::apv_ << i2cAddr_ << sistrip::dir_;
00575                 }
00576               }
00577             }
00578           }
00579         }
00580       }
00581     }
00582     
00583     std::string temp( dir.str() );
00584     path( temp );
00585 
00586   } else {
00587     
00588     // ---------- Set member data based on directory path ----------
00589     
00590     fecCrate_ = 0;
00591     fecSlot_  = 0;
00592     fecRing_  = 0;
00593     ccuAddr_  = 0;
00594     ccuChan_  = 0;
00595     lldChan_  = 0;
00596     i2cAddr_  = 0;
00597 
00598     // Check if root is found
00599     if ( path().find( sistrip::root_ ) == std::string::npos ) {
00600       std::string temp = path();
00601       path( std::string(sistrip::root_) + sistrip::dir_ + temp );
00602     }
00603     
00604     size_t curr = 0; // current string position
00605     size_t next = 0; // next string position
00606     next = path().find( sistrip::controlView_, curr );
00607 
00608     // Extract view 
00609     curr = next;
00610     if ( curr != std::string::npos ) { 
00611       next = path().find( sistrip::fecCrate_, curr );
00612       std::string control_view( path(), 
00613                                 curr+(sizeof(sistrip::controlView_) - 1), 
00614                                 next-(sizeof(sistrip::dir_) - 1)-curr );
00615       
00616       // Extract FEC crate
00617       curr = next;
00618       if ( curr != std::string::npos ) { 
00619         next = path().find( sistrip::fecSlot_, curr );
00620         std::string fec_crate( path(), 
00621                                curr+(sizeof(sistrip::fecCrate_) - 1), 
00622                                next-(sizeof(sistrip::dir_) - 1)-curr );
00623         fecCrate_ = std::atoi( fec_crate.c_str() );
00624 
00625         // Extract FEC slot
00626         curr = next;
00627         if ( curr != std::string::npos ) { 
00628           next = path().find( sistrip::fecRing_, curr );
00629           std::string fec_slot( path(), 
00630                                 curr+(sizeof(sistrip::fecSlot_) - 1), 
00631                                 next-(sizeof(sistrip::dir_) - 1)-curr );
00632           fecSlot_ = std::atoi( fec_slot.c_str() );
00633 
00634           // Extract FEC ring
00635           curr = next;
00636           if ( curr != std::string::npos ) { 
00637             next = path().find( sistrip::ccuAddr_, curr );
00638             std::string fec_ring( path(), 
00639                                   curr+(sizeof(sistrip::fecRing_) - 1),
00640                                   next-(sizeof(sistrip::dir_) - 1)-curr );
00641             fecRing_ = std::atoi( fec_ring.c_str() );
00642 
00643             // Extract CCU address
00644             curr = next;
00645             if ( curr != std::string::npos ) { 
00646               next = path().find( sistrip::ccuChan_, curr );
00647               std::string ccu_addr( path(), 
00648                                     curr+(sizeof(sistrip::ccuAddr_) - 1), 
00649                                     next-(sizeof(sistrip::dir_) - 1)-curr );
00650               ccuAddr_ = std::atoi( ccu_addr.c_str() );
00651 
00652               // Extract CCU channel
00653               curr = next;
00654               if ( curr != std::string::npos ) { 
00655                 next = path().find( sistrip::lldChan_, curr );
00656                 std::string ccu_chan( path(), 
00657                                       curr+(sizeof(sistrip::ccuChan_) - 1), 
00658                                       next-(sizeof(sistrip::dir_) - 1)-curr );
00659                 ccuChan_ = std::atoi( ccu_chan.c_str() );
00660                 
00661                 // Extract LLD channel
00662                 curr = next;
00663                 if ( curr != std::string::npos ) { 
00664                   next = path().find( sistrip::apv_, curr );
00665                   std::string lld_chan( path(), 
00666                                         curr+(sizeof(sistrip::lldChan_) - 1), 
00667                                         next-(sizeof(sistrip::dir_) - 1)-curr );
00668                   lldChan_ = std::atoi( lld_chan.c_str() );
00669                   
00670                   // Extract I2C address
00671                   curr = next;
00672                   if ( curr != std::string::npos ) { 
00673                     next = std::string::npos;
00674                     std::string i2c_addr( path(), 
00675                                           curr+(sizeof(sistrip::apv_) - 1),
00676                                           next-curr );
00677                     i2cAddr_ = std::atoi( i2c_addr.c_str() );
00678                   }
00679                 }
00680               }
00681             }
00682           }
00683         }
00684       }
00685     } else {
00686       std::stringstream ss;
00687       ss << sistrip::root_ << sistrip::dir_;
00688       //ss << sistrip::root_ << sistrip::dir_
00689       //<< sistrip::unknownView_ << sistrip::dir_;
00690       std::string temp( ss.str() );
00691       path( temp );
00692     }
00693     
00694   }
00695   
00696 }
00697 
00698 // -----------------------------------------------------------------------------
00699 // 
00700 void SiStripFecKey::initGranularity() {
00701   
00702   granularity( sistrip::FEC_SYSTEM );
00703   channel(0);
00704   if ( fecCrate_ && fecCrate_ != sistrip::invalid_ ) {
00705     granularity( sistrip::FEC_CRATE ); 
00706     channel(fecCrate_);
00707     if ( fecSlot_ && fecSlot_ != sistrip::invalid_ ) {
00708       granularity( sistrip::FEC_SLOT );
00709       channel(fecSlot_);
00710       if ( fecRing_ && fecRing_ != sistrip::invalid_ ) {
00711         granularity( sistrip::FEC_RING );
00712         channel(fecRing_);
00713         if ( ccuAddr_ && ccuAddr_ != sistrip::invalid_ ) {
00714           granularity( sistrip::CCU_ADDR );
00715           channel(ccuAddr_);
00716           if ( ccuChan_ && ccuChan_ != sistrip::invalid_ ) {
00717             granularity( sistrip::CCU_CHAN );
00718             channel(ccuChan_);
00719             if ( lldChan_ && lldChan_ != sistrip::invalid_ ) {
00720               granularity( sistrip::LLD_CHAN );
00721               channel(lldChan_);
00722               if ( i2cAddr_ && i2cAddr_ != sistrip::invalid_ ) {
00723                 granularity( sistrip::APV );
00724                 channel(i2cAddr_);
00725               } else if ( i2cAddr_ == sistrip::invalid_ ) { 
00726                 granularity( sistrip::UNKNOWN_GRAN ); 
00727                 channel(sistrip::invalid_);
00728               }
00729             } else if ( lldChan_ == sistrip::invalid_ ) { 
00730               granularity( sistrip::UNKNOWN_GRAN ); 
00731               channel(sistrip::invalid_);
00732             }
00733           } else if ( ccuChan_ == sistrip::invalid_ ) { 
00734             granularity( sistrip::UNKNOWN_GRAN ); 
00735             channel(sistrip::invalid_);
00736           }
00737         } else if ( ccuAddr_ == sistrip::invalid_ ) { 
00738           granularity( sistrip::UNKNOWN_GRAN ); 
00739           channel(sistrip::invalid_);
00740         }
00741       } else if ( fecRing_ == sistrip::invalid_ ) { 
00742         granularity( sistrip::UNKNOWN_GRAN ); 
00743         channel(sistrip::invalid_);
00744       }
00745     } else if ( fecSlot_ == sistrip::invalid_ ) { 
00746       granularity( sistrip::UNKNOWN_GRAN ); 
00747       channel(sistrip::invalid_);
00748     }
00749   } else if ( fecCrate_ == sistrip::invalid_ ) { 
00750     granularity( sistrip::UNKNOWN_GRAN ); 
00751     channel(sistrip::invalid_);
00752   }
00753 
00754 }
00755 
00756 // -----------------------------------------------------------------------------
00757 //
00758 void SiStripFecKey::terse( std::stringstream& ss ) const {
00759   ss << "FEC:crate/slot/ring/CCU/module/LLD/I2C= "
00760      << fecCrate() << "/"
00761      << fecSlot() << "/"
00762      << fecRing() << "/"
00763      << ccuAddr() << "/"
00764      << ccuChan() << "/"
00765      << lldChan() << "/"
00766      << i2cAddr();
00767 //   ss << " FecKey"
00768 //     //<< "=0x" 
00769 //     //<< std::hex
00770 //     //<< std::setfill('0') << std::setw(8) << key() << std::setfill(' ') 
00771 //     //<< std::dec
00772 //     //<< ", " << ( isValid() ? "Valid" : "Invalid" )
00773 //      << ", Crate=" << fecCrate()
00774 //      << ", Slot=" << fecSlot()
00775 //      << ", Ring=" << fecRing()
00776 //      << ", CCU=" << ccuAddr()
00777 //      << ", module=" << ccuChan()
00778 //      << ", LLD=" << lldChan()
00779 //      << ", I2C=" << i2cAddr();
00780 }
00781 
00782 // -----------------------------------------------------------------------------
00783 //
00784 void SiStripFecKey::print( std::stringstream& ss ) const {
00785   ss << " [SiStripFecKey::print]" << std::endl
00786      << std::hex
00787      << " FEC key              : 0x" 
00788      << std::setfill('0') 
00789      << std::setw(8) << key() << std::endl
00790      << std::setfill(' ') 
00791      << std::dec
00792      << " FEC VME crate        : " << fecCrate() << std::endl
00793      << " FEC VME slot         : " << fecSlot() << std::endl 
00794      << " FEC control ring     : " << fecRing() << std::endl
00795      << " CCU I2C address      : " << ccuAddr() << std::endl
00796      << " CCU chan (FE module) : " << ccuChan() << std::endl
00797      << " LaserDriver channel  : " << lldChan() << std::endl 
00798      << " APV I2C address      : " << i2cAddr() << std::endl 
00799      << " Directory            : " << path() << std::endl
00800      << " Granularity          : "
00801      << SiStripEnumsAndStrings::granularity( granularity() ) << std::endl
00802      << " Channel              : " << channel() << std::endl
00803      << " isValid              : " << isValid();
00804 }
00805 
00806 // -----------------------------------------------------------------------------
00807 //
00808 std::ostream& operator<< ( std::ostream& os, const SiStripFecKey& input ) {
00809   std::stringstream ss;
00810   input.print(ss);
00811   os << ss.str();
00812   return os;
00813 }
00814 
00815 // -----------------------------------------------------------------------------
00816 //
00817 ConsistentWithKey::ConsistentWithKey( const SiStripFecKey& key ) 
00818   : mask_( key.fecCrate() ? sistrip::invalid_ : 0,
00819            key.fecSlot() ? sistrip::invalid_ : 0,
00820            key.fecRing() ? sistrip::invalid_ : 0,
00821            key.ccuAddr() ? sistrip::invalid_ : 0,
00822            key.ccuChan() ? sistrip::invalid_ : 0,
00823            key.lldChan() ? sistrip::invalid_ : 0,
00824            key.i2cAddr() ? sistrip::invalid_ : 0 ) {;}
00825 
00826 // -----------------------------------------------------------------------------
00827 //
00828 ConsistentWithKey::ConsistentWithKey() 
00829   : mask_(SiStripNullKey()) {;}
00830 
00831 // -----------------------------------------------------------------------------
00832 //
00833 bool ConsistentWithKey::operator() ( const uint32_t& a, const uint32_t& b ) const {
00834   return ( ( a & mask_.key() ) < ( b & mask_.key() ) );
00835 }