CMS 3D CMS Logo

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

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