CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/DataFormats/SiStripCommon/src/SiStripFedKey.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripFedKey.cc,v 1.16 2009/10/23 13:07:17 lowette Exp $
00002 
00003 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
00004 #include "DataFormats/SiStripCommon/interface/Constants.h" 
00005 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"
00006 #include "DataFormats/SiStripCommon/interface/ConstantsForDqm.h"
00007 #include "DataFormats/SiStripCommon/interface/ConstantsForView.h"
00008 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00009 #include <iomanip>
00010 
00011 // -----------------------------------------------------------------------------
00012 // 
00013 SiStripFedKey::SiStripFedKey( const uint16_t& fed_id,
00014                               const uint16_t& fe_unit,
00015                               const uint16_t& fe_chan,
00016                               const uint16_t& fed_apv ) :
00017   SiStripKey(),
00018   fedId_(fed_id), 
00019   feUnit_(fe_unit), 
00020   feChan_(fe_chan),
00021   fedApv_(fed_apv)
00022 {
00023   // order is important!
00024   initFromValue();
00025   initFromKey();
00026   initFromPath();
00027   initGranularity();
00028 }
00029 
00030 // -----------------------------------------------------------------------------
00031 // 
00032 SiStripFedKey::SiStripFedKey( const uint32_t& fed_key ) :
00033   SiStripKey(fed_key),
00034   fedId_(sistrip::invalid_), 
00035   feUnit_(sistrip::invalid_), 
00036   feChan_(sistrip::invalid_),
00037   fedApv_(sistrip::invalid_)
00038 {
00039   // order is important!
00040   initFromKey(); 
00041   initFromValue();
00042   initFromPath();
00043   initGranularity();
00044 }
00045 
00046 // -----------------------------------------------------------------------------
00047 // 
00048 SiStripFedKey::SiStripFedKey( const std::string& path ) :
00049   SiStripKey(path),
00050   fedId_(sistrip::invalid_), 
00051   feUnit_(sistrip::invalid_), 
00052   feChan_(sistrip::invalid_),
00053   fedApv_(sistrip::invalid_)
00054 {
00055   // order is important!
00056   initFromPath();
00057   initFromValue();
00058   initFromKey();
00059   initGranularity();
00060 }
00061 
00062 // -----------------------------------------------------------------------------
00063 // 
00064 SiStripFedKey::SiStripFedKey( const SiStripFedKey& input ) :
00065   SiStripKey(),
00066   fedId_(input.fedId()), 
00067   feUnit_(input.feUnit()), 
00068   feChan_(input.feChan()),
00069   fedApv_(input.fedApv())
00070 {
00071   key(input.key());
00072   path(input.path());
00073   granularity(input.granularity());
00074 }
00075 
00076 // -----------------------------------------------------------------------------
00077 // 
00078 SiStripFedKey::SiStripFedKey( const SiStripKey& input ) :
00079   SiStripKey(),
00080   fedId_(sistrip::invalid_),
00081   feUnit_(sistrip::invalid_),
00082   feChan_(sistrip::invalid_),
00083   fedApv_(sistrip::invalid_)
00084 {
00085   SiStripKey& temp = const_cast<SiStripKey&>(input);
00086   SiStripFedKey& fed_key = dynamic_cast<SiStripFedKey&>(temp);
00087   if ( (&fed_key) ) {
00088     key(fed_key.key());
00089     path(fed_key.path());
00090     granularity(fed_key.granularity());
00091     fedId_ = fed_key.fedId(); 
00092     feUnit_ = fed_key.feUnit(); 
00093     feChan_ = fed_key.feChan();
00094     fedApv_ = fed_key.fedApv();
00095   }
00096 }
00097 
00098 // -----------------------------------------------------------------------------
00099 // 
00100 SiStripFedKey::SiStripFedKey() : 
00101   SiStripKey(),
00102   fedId_(sistrip::invalid_),
00103   feUnit_(sistrip::invalid_),
00104   feChan_(sistrip::invalid_),
00105   fedApv_(sistrip::invalid_)
00106 {;}
00107 
00108 // -----------------------------------------------------------------------------
00109 //
00110 uint16_t SiStripFedKey::fedCh( const uint16_t& fe_unit,
00111                                const uint16_t& fe_chan ) {
00112   if ( fe_unit <= sistrip::FEUNITS_PER_FED &&
00113        fe_chan <= sistrip::FEDCH_PER_FEUNIT ) {
00114     if ( fe_unit != 0 && fe_chan != 0 ) { 
00115       return ( 95 - ( 12 * (fe_unit-1) + (fe_chan-1) ) ); 
00116     } 
00117   } 
00118   return sistrip::invalid_;  
00119 }
00120 
00121 // -----------------------------------------------------------------------------
00122 //
00123 uint16_t SiStripFedKey::feUnit( const uint16_t& fed_ch ) {
00124   if ( fed_ch < sistrip::FEDCH_PER_FED ) {
00125     return ( (95-fed_ch)/12 + 1 );
00126   }
00127   return sistrip::invalid_;
00128 }
00129 
00130 // -----------------------------------------------------------------------------
00131 //
00132 uint16_t SiStripFedKey::feChan( const uint16_t& fed_ch ) {
00133   if ( fed_ch < sistrip::FEDCH_PER_FED ) {
00134     return ( (95-fed_ch)%12 + 1 );
00135   } 
00136   return sistrip::invalid_;
00137 }
00138 
00139 // -----------------------------------------------------------------------------
00140 //
00141 uint32_t SiStripFedKey::fedIndex( const uint16_t& fed_id,
00142                                   const uint16_t& fed_ch ) {
00143   if ( fed_id < sistrip::FED_ID_MIN ||
00144        fed_id > sistrip::FED_ID_MAX ||
00145        fed_ch >= sistrip::FEDCH_PER_FED ) { return sistrip::invalid32_; }
00146   return ( fed_id * sistrip::FEDCH_PER_FED + fed_ch );
00147 }
00148 
00149 // -----------------------------------------------------------------------------
00150 // 
00151 bool SiStripFedKey::isEqual( const SiStripKey& key ) const {
00152   SiStripKey& temp = const_cast<SiStripKey&>(key);
00153   SiStripFedKey& input = dynamic_cast<SiStripFedKey&>(temp);
00154   if ( !(&input) ) { return false; }
00155   if ( fedId_ == input.fedId() &&
00156        feUnit_ == input.feUnit() &&
00157        feChan_ == input.feChan() &&
00158        fedApv_ == input.fedApv() ) { 
00159     return true;
00160   } else { return false; }
00161 }
00162 
00163 // -----------------------------------------------------------------------------
00164 // 
00165 bool SiStripFedKey::isConsistent( const SiStripKey& key ) const {
00166   SiStripKey& temp = const_cast<SiStripKey&>(key);
00167   SiStripFedKey& input = dynamic_cast<SiStripFedKey&>(temp);
00168   if ( !(&input) ) { return false; }
00169   if ( isEqual(input) ) { return true; }
00170   else if ( ( fedId_ == 0 || input.fedId() == 0 ) &&
00171             ( feUnit_ == 0 || input.feUnit() == 0 ) &&
00172             ( feChan_ == 0 || input.feChan() == 0 ) &&
00173             ( fedApv_ == 0 || input.fedApv() == 0 ) ) { 
00174     return true;
00175   } else { return false; }
00176 }
00177 
00178 // -----------------------------------------------------------------------------
00179 //
00180 bool SiStripFedKey::isValid() const { 
00181   return isValid(sistrip::FED_APV); 
00182 }
00183 
00184 // -----------------------------------------------------------------------------
00185 //
00186 bool SiStripFedKey::isValid( const sistrip::Granularity& gran ) const {
00187   if ( gran == sistrip::FED_SYSTEM ) { return true; }
00188   else if ( gran == sistrip::UNDEFINED_GRAN ||
00189             gran == sistrip::UNKNOWN_GRAN ) { return false; }
00190 
00191   if ( fedId_ != sistrip::invalid_ ) {
00192     if ( gran == sistrip::FE_DRIVER ) { return true; }
00193     if ( feUnit_ != sistrip::invalid_ ) {
00194       if ( gran == sistrip::FE_UNIT ) { return true; }
00195       if ( feChan_ != sistrip::invalid_ ) {
00196         if ( gran == sistrip::FE_CHAN ) { return true; }
00197         if ( fedApv_ != sistrip::invalid_ ) {
00198           if ( gran == sistrip::FED_APV ) { return true; }
00199         }
00200       }
00201     }
00202   }
00203   return false;
00204 }
00205 
00206 // -----------------------------------------------------------------------------
00207 //
00208 bool SiStripFedKey::isInvalid() const { 
00209   return isInvalid(sistrip::FED_APV); 
00210 }
00211 
00212 // -----------------------------------------------------------------------------
00213 //
00214 bool SiStripFedKey::isInvalid( const sistrip::Granularity& gran ) const {
00215   if ( gran == sistrip::FED_SYSTEM ) { return false; }
00216   else if ( gran == sistrip::UNDEFINED_GRAN ||
00217             gran == sistrip::UNKNOWN_GRAN ) { return false; }
00218 
00219   if ( fedId_ == sistrip::invalid_ ) {
00220     if ( gran == sistrip::FE_DRIVER ) { return true; }
00221     if ( feUnit_ == sistrip::invalid_ ) {
00222       if ( gran == sistrip::FE_UNIT ) { return true; }
00223       if ( feChan_ == sistrip::invalid_ ) {
00224         if ( gran == sistrip::FE_CHAN ) { return true; }
00225         if ( fedApv_ == sistrip::invalid_ ) {
00226           if ( gran == sistrip::FED_APV ) { return true; }
00227         }
00228       }
00229     }
00230   }
00231   return false;
00232 }
00233 
00234 // -----------------------------------------------------------------------------
00235 // 
00236 void SiStripFedKey::initFromValue() {
00237   
00238   if ( fedId_ >= sistrip::FED_ID_MIN &&
00239        fedId_ <= sistrip::FED_ID_MAX ) { 
00240     fedId_ = fedId_; 
00241   } else if ( fedId_ == 0 ) { 
00242     fedId_ = fedId_; 
00243   } else { 
00244     fedId_ = sistrip::invalid_; 
00245   }
00246   
00247   if ( feUnit_ <= sistrip::FEUNITS_PER_FED ) { feUnit_ = feUnit_; }
00248   else { feUnit_ = sistrip::invalid_; }
00249 
00250   if ( feChan_ <= sistrip::FEDCH_PER_FEUNIT ) { feChan_ = feChan_; }
00251   else { feChan_ = sistrip::invalid_; }
00252   
00253   if ( fedApv_ <= sistrip::APVS_PER_FEDCH ) { fedApv_ = fedApv_; }
00254   else { fedApv_ = sistrip::invalid_; }
00255   
00256 }
00257 
00258 // -----------------------------------------------------------------------------
00259 //
00260 void SiStripFedKey::initFromKey() {
00261 
00262   if ( key() == sistrip::invalid32_ ) { 
00263     
00264     // ---------- Set FedKey based on member data ----------
00265     
00266     // Initialise to null value
00267     key(0);
00268     
00269     // Extract FED id
00270     if ( fedId_ >= sistrip::FED_ID_MIN && 
00271          fedId_ <= sistrip::FED_ID_MAX ) { 
00272       key( key() | (fedId_<<fedIdOffset_) );
00273     } else if ( fedId_ == 0 ) {
00274       key( key() | (fedId_<<fedIdOffset_) );
00275     } else {
00276       key( key() | (fedIdMask_<<fedIdOffset_) ); 
00277     }
00278     
00279     // Extract FE unit
00280     if ( feUnit_ >= 1 && 
00281          feUnit_ <= sistrip::FEUNITS_PER_FED ) { 
00282       key( key() | (feUnit_<<feUnitOffset_) );
00283     } else if ( feUnit_ == 0 ) {
00284       key( key() | (feUnit_<<feUnitOffset_) );
00285     } else {
00286       key( key() | (feUnitMask_<<feUnitOffset_) ); 
00287     }
00288   
00289     // Extract FE chan
00290     if ( feChan_ >= 1 && 
00291          feChan_ <= sistrip::FEDCH_PER_FEUNIT ) { 
00292       key( key() | (feChan_<<feChanOffset_) );
00293     } else if ( feChan_ == 0 ) {
00294       key( key() | (feChan_<<feChanOffset_) );
00295     } else {
00296       key( key() | (feChanMask_<<feChanOffset_) ); 
00297     }
00298 
00299     // Extract FED APV
00300     if ( fedApv_ >= 1 && 
00301          fedApv_ <=  sistrip::APVS_PER_FEDCH ) { 
00302       key( key() | (fedApv_<<fedApvOffset_) );
00303     } else if ( fedApv_ == 0 ) {
00304       key( key() | (fedApv_<<fedApvOffset_) );
00305     } else {
00306       key( key() | (fedApvMask_<<fedApvOffset_) ); 
00307     }
00308     
00309     // Set invalid key if necessary
00310     if ( fedId_ == sistrip::invalid_ &&
00311          feUnit_ == sistrip::invalid_ &&
00312          feChan_ == sistrip::invalid_ &&
00313          fedApv_ == sistrip::invalid_ ) { 
00314       key( sistrip::invalid32_ ); 
00315     }
00316     
00317   } else {
00318     
00319     // ---------- Set member data based on FED key ----------
00320     
00321     fedId_  = ( key()>>fedIdOffset_ )  & fedIdMask_;
00322     feUnit_ = ( key()>>feUnitOffset_ ) & feUnitMask_;
00323     feChan_ = ( key()>>feChanOffset_ ) & feChanMask_;
00324     fedApv_ = ( key()>>fedApvOffset_ ) & fedApvMask_;
00325     
00326     if ( fedId_ == fedIdMask_ )   { fedId_ = sistrip::invalid_; } 
00327     if ( feUnit_ == feUnitMask_ ) { feUnit_ = sistrip::invalid_; } 
00328     if ( feChan_ == feChanMask_ ) { feChan_ = sistrip::invalid_; } 
00329     if ( fedApv_ == fedApvMask_ ) { fedApv_ = sistrip::invalid_; } 
00330 
00331   }
00332   
00333 }
00334 
00335 // -----------------------------------------------------------------------------
00336 // 
00337 void SiStripFedKey::initFromPath() {
00338   
00339   if ( path() == sistrip::null_ ) {
00340     
00341     // ---------- Set directory path based on member data ----------
00342       
00343     std::stringstream dir;
00344     
00345     dir << sistrip::root_ << sistrip::dir_ 
00346         << sistrip::readoutView_ << sistrip::dir_;
00347     
00348     // Add FED id
00349     if ( fedId_ ) {
00350       dir << sistrip::feDriver_ << fedId_ << sistrip::dir_;
00351 
00352       // Add FE unit
00353       if ( feUnit_ ) {
00354         dir << sistrip::feUnit_ << feUnit_ << sistrip::dir_;
00355 
00356         // Add FE channel
00357         if ( feChan_ ) {
00358           dir << sistrip::feChan_ << feChan_ << sistrip::dir_;
00359 
00360           // Add FED APV
00361           if ( fedApv_ ) {
00362             dir << sistrip::fedApv_ << fedApv_ << sistrip::dir_;
00363           }
00364         }
00365       }
00366     }
00367     
00368     std::string temp( dir.str() );
00369     path( temp );
00370     
00371   } else {
00372     
00373     // ---------- Set member data based on directory path ----------
00374     
00375     fedId_  = 0;
00376     feUnit_ = 0;
00377     feChan_ = 0;
00378     fedApv_ = 0;
00379     
00380     // Check if root is found
00381     if ( path().find( sistrip::root_ ) == std::string::npos ) {
00382       std::string temp = path();
00383       path( std::string(sistrip::root_) + sistrip::dir_ + temp );
00384     }
00385     
00386     size_t curr = 0; // current string position
00387     size_t next = 0; // next string position
00388     next = path().find( sistrip::readoutView_, curr );
00389     
00390     // Extract view 
00391     curr = next;
00392     if ( curr != std::string::npos ) { 
00393       next = path().find( sistrip::feDriver_, curr );
00394       std::string readout_view( path(), 
00395                                 curr+(sizeof(sistrip::readoutView_) - 1), 
00396                                 (next-(sizeof(sistrip::dir_) - 1))-curr );
00397       
00398       // Extract FED id
00399       curr = next;
00400       if ( curr != std::string::npos ) { 
00401         next = path().find( sistrip::feUnit_, curr );
00402         std::string fed_id( path(), 
00403                             curr+(sizeof(sistrip::feDriver_) - 1), 
00404                             (next-(sizeof(sistrip::dir_) - 1))-curr );
00405         fedId_ = atoi( fed_id.c_str() );
00406         
00407         // Extract FE unit
00408         curr = next;
00409         if ( curr != std::string::npos ) { 
00410           next = path().find( sistrip::feChan_, curr );
00411           std::string fe_unit( path(), 
00412                                curr+(sizeof(sistrip::feUnit_) - 1), 
00413                                next-curr );
00414           feUnit_ = atoi( fe_unit.c_str() );
00415           
00416           // Extract FE channel
00417           curr = next;
00418           if ( curr != std::string::npos ) { 
00419             next = path().find( sistrip::fedApv_, curr );
00420             std::string fe_chan( path(), 
00421                                  curr+(sizeof(sistrip::feChan_) - 1), 
00422                                  next-curr );
00423             feChan_ = atoi( fe_chan.c_str() );
00424             
00425             // Extract FED APV 
00426             curr = next;
00427             if ( curr != std::string::npos ) { 
00428               next = std::string::npos;
00429               std::string fed_apv( path(), 
00430                                    curr+(sizeof(sistrip::fedApv_) - 1), 
00431                                    next-curr );
00432               fedApv_ = atoi( fed_apv.c_str() );
00433             }
00434           }
00435         }
00436       }
00437     } else {
00438       std::stringstream ss;
00439       ss << sistrip::root_ << sistrip::dir_;
00440       //ss << sistrip::root_ << sistrip::dir_ 
00441       //<< sistrip::unknownView_ << sistrip::dir_;
00442       std::string temp( ss.str() );
00443       path( temp );
00444     }
00445 
00446   }
00447   
00448 }
00449 
00450 // -----------------------------------------------------------------------------
00451 // 
00452 void SiStripFedKey::initGranularity() {
00453   
00454   granularity( sistrip::FED_SYSTEM );
00455   channel(0);
00456   if ( fedId_ && fedId_ != sistrip::invalid_ ) {
00457     granularity( sistrip::FE_DRIVER ); 
00458     channel(fedId_);
00459     if ( feUnit_ && feUnit_ != sistrip::invalid_ ) {
00460       granularity( sistrip::FE_UNIT );
00461       channel(feUnit_);
00462       if ( feChan_ && feChan_ != sistrip::invalid_ ) {
00463         granularity( sistrip::FE_CHAN );
00464         channel(feChan_);
00465         if ( fedApv_ && fedApv_ != sistrip::invalid_ ) {
00466           granularity( sistrip::FED_APV );
00467           channel(fedApv_);
00468         } else if ( fedApv_ == sistrip::invalid_ ) { 
00469           granularity( sistrip::UNKNOWN_GRAN ); 
00470           channel(sistrip::invalid_);
00471         }
00472       } else if ( feChan_ == sistrip::invalid_ ) { 
00473         granularity( sistrip::UNKNOWN_GRAN ); 
00474         channel(sistrip::invalid_);
00475       }
00476     } else if ( feUnit_ == sistrip::invalid_ ) { 
00477       granularity( sistrip::UNKNOWN_GRAN ); 
00478       channel(sistrip::invalid_);
00479     }
00480   } else if ( fedId_ == sistrip::invalid_ ) { 
00481     granularity( sistrip::UNKNOWN_GRAN ); 
00482     channel(sistrip::invalid_);
00483   }
00484   
00485 }
00486 
00487 // -----------------------------------------------------------------------------
00488 //
00489 void SiStripFedKey::terse( std::stringstream& ss ) const {
00490   ss << "FED:crate/slot/id/unit/chan/apv= "
00491      << "-" << "/"
00492      << "-" << "/"
00493      << fedId() << "/"
00494      << feUnit() << "/"
00495      << feChan() << "/"
00496      << fedApv();
00497 //   ss << " FedKey"
00498 //     //<<"=0x" 
00499 //     //<< std::hex
00500 //     //<< std::setfill('0') << std::setw(8) << key() << std::setfill(' ') 
00501 //     //<< std::dec
00502 //     //<< ", " << ( isValid() ? "Valid" : "Invalid" )
00503 //     //<< ", FedCrate=" << fedCrate()
00504 //     //<< ", FedSlot=" << fedSlot()
00505 //      << ", FedId=" << fedId()
00506 //      << ", FeUnit=" << feUnit()
00507 //      << ", FeChan=" << feChan()
00508 //     //<< ", FedChannel=" << fedChannel()
00509 //      << ", FedApv=" << fedApv();
00510 }
00511 
00512 // -----------------------------------------------------------------------------
00513 //
00514 void SiStripFedKey::print( std::stringstream& ss ) const {
00515   ss << " [SiStripFedKey::print]" << std::endl
00516      << std::hex
00517      << " FED key         : 0x" 
00518      << std::setfill('0') 
00519      << std::setw(8) << key() << std::endl
00520      << std::setfill(' ') 
00521      << std::dec
00522      << " FED id          : " << fedId() << std::endl
00523      << " Front-End unit  : " << feUnit() << std::endl 
00524      << " Front-End chan  : " << feChan() << std::endl
00525      << " (internal chan) : " 
00526      << "(" << fedChannel() << ")" << std::endl
00527      << " FED APV         : " << fedApv() << std::endl
00528      << " Directory       : " << path() << std::endl
00529      << " Granularity     : "
00530      << SiStripEnumsAndStrings::granularity( granularity() ) << std::endl
00531      << " Channel         : " << channel() << std::endl
00532      << " isValid         : " << isValid();
00533 }
00534 
00535 // -----------------------------------------------------------------------------
00536 //
00537 std::ostream& operator<< ( std::ostream& os, const SiStripFedKey& input ) {
00538   std::stringstream ss;
00539   input.print(ss);
00540   os << ss.str();
00541   return os;
00542 }