Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:14

0001 
0002 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
0003 #include "DataFormats/SiStripCommon/interface/Constants.h"
0004 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"
0005 #include "DataFormats/SiStripCommon/interface/ConstantsForDqm.h"
0006 #include "DataFormats/SiStripCommon/interface/ConstantsForView.h"
0007 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0008 #include <iomanip>
0009 
0010 // -----------------------------------------------------------------------------
0011 //
0012 SiStripFedKey::SiStripFedKey(const uint16_t& fed_id,
0013                              const uint16_t& fe_unit,
0014                              const uint16_t& fe_chan,
0015                              const uint16_t& fed_apv)
0016     : SiStripKey(), fedId_(fed_id), feUnit_(fe_unit), feChan_(fe_chan), fedApv_(fed_apv) {
0017   // order is important!
0018   initFromValue();
0019   initFromKey();
0020   initFromPath();
0021   initGranularity();
0022 }
0023 
0024 // -----------------------------------------------------------------------------
0025 //
0026 SiStripFedKey::SiStripFedKey(const uint32_t& fed_key)
0027     : SiStripKey(fed_key),
0028       fedId_(sistrip::invalid_),
0029       feUnit_(sistrip::invalid_),
0030       feChan_(sistrip::invalid_),
0031       fedApv_(sistrip::invalid_) {
0032   // order is important!
0033   initFromKey();
0034   initFromValue();
0035   initFromPath();
0036   initGranularity();
0037 }
0038 
0039 // -----------------------------------------------------------------------------
0040 //
0041 SiStripFedKey::SiStripFedKey(const std::string& path)
0042     : SiStripKey(path),
0043       fedId_(sistrip::invalid_),
0044       feUnit_(sistrip::invalid_),
0045       feChan_(sistrip::invalid_),
0046       fedApv_(sistrip::invalid_) {
0047   // order is important!
0048   initFromPath();
0049   initFromValue();
0050   initFromKey();
0051   initGranularity();
0052 }
0053 
0054 // -----------------------------------------------------------------------------
0055 //
0056 SiStripFedKey::SiStripFedKey(const SiStripFedKey& input)
0057     : SiStripKey(), fedId_(input.fedId()), feUnit_(input.feUnit()), feChan_(input.feChan()), fedApv_(input.fedApv()) {
0058   key(input.key());
0059   path(input.path());
0060   granularity(input.granularity());
0061 }
0062 
0063 // -----------------------------------------------------------------------------
0064 //
0065 SiStripFedKey::SiStripFedKey(const SiStripKey& input)
0066     : SiStripKey(),
0067       fedId_(sistrip::invalid_),
0068       feUnit_(sistrip::invalid_),
0069       feChan_(sistrip::invalid_),
0070       fedApv_(sistrip::invalid_) {
0071   const SiStripFedKey& fed_key = dynamic_cast<const SiStripFedKey&>(input);
0072   key(fed_key.key());
0073   path(fed_key.path());
0074   granularity(fed_key.granularity());
0075   fedId_ = fed_key.fedId();
0076   feUnit_ = fed_key.feUnit();
0077   feChan_ = fed_key.feChan();
0078   fedApv_ = fed_key.fedApv();
0079 }
0080 
0081 // -----------------------------------------------------------------------------
0082 //
0083 SiStripFedKey::SiStripFedKey()
0084     : SiStripKey(),
0085       fedId_(sistrip::invalid_),
0086       feUnit_(sistrip::invalid_),
0087       feChan_(sistrip::invalid_),
0088       fedApv_(sistrip::invalid_) {
0089   ;
0090 }
0091 
0092 // -----------------------------------------------------------------------------
0093 //
0094 uint16_t SiStripFedKey::fedCh(const uint16_t& fe_unit, const uint16_t& fe_chan) {
0095   if (fe_unit <= sistrip::FEUNITS_PER_FED && fe_chan <= sistrip::FEDCH_PER_FEUNIT) {
0096     if (fe_unit != 0 && fe_chan != 0) {
0097       return (95 - (12 * (fe_unit - 1) + (fe_chan - 1)));
0098     }
0099   }
0100   return sistrip::invalid_;
0101 }
0102 
0103 // -----------------------------------------------------------------------------
0104 //
0105 uint16_t SiStripFedKey::feUnit(const uint16_t& fed_ch) {
0106   if (fed_ch < sistrip::FEDCH_PER_FED) {
0107     return ((95 - fed_ch) / 12 + 1);
0108   }
0109   return sistrip::invalid_;
0110 }
0111 
0112 // -----------------------------------------------------------------------------
0113 //
0114 uint16_t SiStripFedKey::feChan(const uint16_t& fed_ch) {
0115   if (fed_ch < sistrip::FEDCH_PER_FED) {
0116     return ((95 - fed_ch) % 12 + 1);
0117   }
0118   return sistrip::invalid_;
0119 }
0120 
0121 // -----------------------------------------------------------------------------
0122 //
0123 uint32_t SiStripFedKey::fedIndex(const uint16_t& fed_id, const uint16_t& fed_ch) {
0124   if (fed_id < sistrip::FED_ID_MIN || fed_id > sistrip::FED_ID_MAX || fed_ch >= sistrip::FEDCH_PER_FED) {
0125     return sistrip::invalid32_;
0126   }
0127   return (fed_id * sistrip::FEDCH_PER_FED + fed_ch);
0128 }
0129 
0130 // -----------------------------------------------------------------------------
0131 //
0132 bool SiStripFedKey::isEqual(const SiStripKey& key) const {
0133   const SiStripFedKey& input = dynamic_cast<const SiStripFedKey&>(key);
0134   if (fedId_ == input.fedId() && feUnit_ == input.feUnit() && feChan_ == input.feChan() && fedApv_ == input.fedApv()) {
0135     return true;
0136   } else {
0137     return false;
0138   }
0139 }
0140 
0141 // -----------------------------------------------------------------------------
0142 //
0143 bool SiStripFedKey::isConsistent(const SiStripKey& key) const {
0144   const SiStripFedKey& input = dynamic_cast<const SiStripFedKey&>(key);
0145   if (isEqual(input)) {
0146     return true;
0147   } else if ((fedId_ == 0 || input.fedId() == 0) && (feUnit_ == 0 || input.feUnit() == 0) &&
0148              (feChan_ == 0 || input.feChan() == 0) && (fedApv_ == 0 || input.fedApv() == 0)) {
0149     return true;
0150   } else {
0151     return false;
0152   }
0153 }
0154 
0155 // -----------------------------------------------------------------------------
0156 //
0157 bool SiStripFedKey::isValid() const { return isValid(sistrip::FED_APV); }
0158 
0159 // -----------------------------------------------------------------------------
0160 //
0161 bool SiStripFedKey::isValid(const sistrip::Granularity& gran) const {
0162   if (gran == sistrip::FED_SYSTEM) {
0163     return true;
0164   } else if (gran == sistrip::UNDEFINED_GRAN || gran == sistrip::UNKNOWN_GRAN) {
0165     return false;
0166   }
0167 
0168   if (fedId_ != sistrip::invalid_) {
0169     if (gran == sistrip::FE_DRIVER) {
0170       return true;
0171     }
0172     if (feUnit_ != sistrip::invalid_) {
0173       if (gran == sistrip::FE_UNIT) {
0174         return true;
0175       }
0176       if (feChan_ != sistrip::invalid_) {
0177         if (gran == sistrip::FE_CHAN) {
0178           return true;
0179         }
0180         if (fedApv_ != sistrip::invalid_) {
0181           if (gran == sistrip::FED_APV) {
0182             return true;
0183           }
0184         }
0185       }
0186     }
0187   }
0188   return false;
0189 }
0190 
0191 // -----------------------------------------------------------------------------
0192 //
0193 bool SiStripFedKey::isInvalid() const { return isInvalid(sistrip::FED_APV); }
0194 
0195 // -----------------------------------------------------------------------------
0196 //
0197 bool SiStripFedKey::isInvalid(const sistrip::Granularity& gran) const {
0198   if (gran == sistrip::FED_SYSTEM) {
0199     return false;
0200   } else if (gran == sistrip::UNDEFINED_GRAN || gran == sistrip::UNKNOWN_GRAN) {
0201     return false;
0202   }
0203 
0204   if (fedId_ == sistrip::invalid_) {
0205     if (gran == sistrip::FE_DRIVER) {
0206       return true;
0207     }
0208     if (feUnit_ == sistrip::invalid_) {
0209       if (gran == sistrip::FE_UNIT) {
0210         return true;
0211       }
0212       if (feChan_ == sistrip::invalid_) {
0213         if (gran == sistrip::FE_CHAN) {
0214           return true;
0215         }
0216         if (fedApv_ == sistrip::invalid_) {
0217           if (gran == sistrip::FED_APV) {
0218             return true;
0219           }
0220         }
0221       }
0222     }
0223   }
0224   return false;
0225 }
0226 
0227 // -----------------------------------------------------------------------------
0228 //
0229 void SiStripFedKey::initFromValue() {
0230   if (not((fedId_ >= sistrip::FED_ID_MIN && fedId_ <= sistrip::FED_ID_MAX) || (fedId_ == 0))) {
0231     fedId_ = sistrip::invalid_;
0232   }
0233 
0234   if (feUnit_ > sistrip::FEUNITS_PER_FED) {
0235     feUnit_ = sistrip::invalid_;
0236   }
0237 
0238   if (feChan_ > sistrip::FEDCH_PER_FEUNIT) {
0239     feChan_ = sistrip::invalid_;
0240   }
0241 
0242   if (fedApv_ > sistrip::APVS_PER_FEDCH) {
0243     fedApv_ = sistrip::invalid_;
0244   }
0245 }
0246 
0247 // -----------------------------------------------------------------------------
0248 //
0249 void SiStripFedKey::initFromKey() {
0250   if (key() == sistrip::invalid32_) {
0251     // ---------- Set FedKey based on member data ----------
0252 
0253     // Initialise to null value
0254     key(0);
0255 
0256     // Extract FED id
0257     if (fedId_ >= sistrip::FED_ID_MIN && fedId_ <= sistrip::FED_ID_MAX) {
0258       key(key() | (fedId_ << fedIdOffset_));
0259     } else if (fedId_ == 0) {
0260       key(key() | (fedId_ << fedIdOffset_));
0261     } else {
0262       key(key() | (fedIdMask_ << fedIdOffset_));
0263     }
0264 
0265     // Extract FE unit
0266     if (feUnit_ >= 1 && feUnit_ <= sistrip::FEUNITS_PER_FED) {
0267       key(key() | (feUnit_ << feUnitOffset_));
0268     } else if (feUnit_ == 0) {
0269       key(key() | (feUnit_ << feUnitOffset_));
0270     } else {
0271       key(key() | (feUnitMask_ << feUnitOffset_));
0272     }
0273 
0274     // Extract FE chan
0275     if (feChan_ >= 1 && feChan_ <= sistrip::FEDCH_PER_FEUNIT) {
0276       key(key() | (feChan_ << feChanOffset_));
0277     } else if (feChan_ == 0) {
0278       key(key() | (feChan_ << feChanOffset_));
0279     } else {
0280       key(key() | (feChanMask_ << feChanOffset_));
0281     }
0282 
0283     // Extract FED APV
0284     if (fedApv_ >= 1 && fedApv_ <= sistrip::APVS_PER_FEDCH) {
0285       key(key() | (fedApv_ << fedApvOffset_));
0286     } else if (fedApv_ == 0) {
0287       key(key() | (fedApv_ << fedApvOffset_));
0288     } else {
0289       key(key() | (fedApvMask_ << fedApvOffset_));
0290     }
0291 
0292     // Set invalid key if necessary
0293     if (fedId_ == sistrip::invalid_ && feUnit_ == sistrip::invalid_ && feChan_ == sistrip::invalid_ &&
0294         fedApv_ == sistrip::invalid_) {
0295       key(sistrip::invalid32_);
0296     }
0297 
0298   } else {
0299     // ---------- Set member data based on FED key ----------
0300 
0301     fedId_ = (key() >> fedIdOffset_) & fedIdMask_;
0302     feUnit_ = (key() >> feUnitOffset_) & feUnitMask_;
0303     feChan_ = (key() >> feChanOffset_) & feChanMask_;
0304     fedApv_ = (key() >> fedApvOffset_) & fedApvMask_;
0305 
0306     if (fedId_ == fedIdMask_) {
0307       fedId_ = sistrip::invalid_;
0308     }
0309     if (feUnit_ == feUnitMask_) {
0310       feUnit_ = sistrip::invalid_;
0311     }
0312     if (feChan_ == feChanMask_) {
0313       feChan_ = sistrip::invalid_;
0314     }
0315     if (fedApv_ == fedApvMask_) {
0316       fedApv_ = sistrip::invalid_;
0317     }
0318   }
0319 }
0320 
0321 // -----------------------------------------------------------------------------
0322 //
0323 void SiStripFedKey::initFromPath() {
0324   if (path() == sistrip::null_) {
0325     // ---------- Set directory path based on member data ----------
0326 
0327     std::stringstream dir;
0328 
0329     dir << sistrip::root_ << sistrip::dir_ << sistrip::readoutView_ << sistrip::dir_;
0330 
0331     // Add FED id
0332     if (fedId_) {
0333       dir << sistrip::feDriver_ << fedId_ << sistrip::dir_;
0334 
0335       // Add FE unit
0336       if (feUnit_) {
0337         dir << sistrip::feUnit_ << feUnit_ << sistrip::dir_;
0338 
0339         // Add FE channel
0340         if (feChan_) {
0341           dir << sistrip::feChan_ << feChan_ << sistrip::dir_;
0342 
0343           // Add FED APV
0344           if (fedApv_) {
0345             dir << sistrip::fedApv_ << fedApv_ << sistrip::dir_;
0346           }
0347         }
0348       }
0349     }
0350 
0351     std::string temp(dir.str());
0352     path(temp);
0353 
0354   } else {
0355     // ---------- Set member data based on directory path ----------
0356 
0357     fedId_ = 0;
0358     feUnit_ = 0;
0359     feChan_ = 0;
0360     fedApv_ = 0;
0361 
0362     // Check if root is found
0363     if (path().find(sistrip::root_) == std::string::npos) {
0364       std::string temp = path();
0365       path(std::string(sistrip::root_) + sistrip::dir_ + temp);
0366     }
0367 
0368     size_t curr = 0;  // current string position
0369     size_t next = 0;  // next string position
0370     next = path().find(sistrip::readoutView_, curr);
0371 
0372     // Extract view
0373     curr = next;
0374     if (curr != std::string::npos) {
0375       next = path().find(sistrip::feDriver_, curr);
0376       std::string readout_view(
0377           path(), curr + (sizeof(sistrip::readoutView_) - 1), (next - (sizeof(sistrip::dir_) - 1)) - curr);
0378 
0379       // Extract FED id
0380       curr = next;
0381       if (curr != std::string::npos) {
0382         next = path().find(sistrip::feUnit_, curr);
0383         std::string fed_id(
0384             path(), curr + (sizeof(sistrip::feDriver_) - 1), (next - (sizeof(sistrip::dir_) - 1)) - curr);
0385         fedId_ = atoi(fed_id.c_str());
0386 
0387         // Extract FE unit
0388         curr = next;
0389         if (curr != std::string::npos) {
0390           next = path().find(sistrip::feChan_, curr);
0391           std::string fe_unit(path(), curr + (sizeof(sistrip::feUnit_) - 1), next - curr);
0392           feUnit_ = atoi(fe_unit.c_str());
0393 
0394           // Extract FE channel
0395           curr = next;
0396           if (curr != std::string::npos) {
0397             next = path().find(sistrip::fedApv_, curr);
0398             std::string fe_chan(path(), curr + (sizeof(sistrip::feChan_) - 1), next - curr);
0399             feChan_ = atoi(fe_chan.c_str());
0400 
0401             // Extract FED APV
0402             curr = next;
0403             if (curr != std::string::npos) {
0404               next = std::string::npos;
0405               std::string fed_apv(path(), curr + (sizeof(sistrip::fedApv_) - 1), next - curr);
0406               fedApv_ = atoi(fed_apv.c_str());
0407             }
0408           }
0409         }
0410       }
0411     } else {
0412       std::stringstream ss;
0413       ss << sistrip::root_ << sistrip::dir_;
0414       //ss << sistrip::root_ << sistrip::dir_
0415       //<< sistrip::unknownView_ << sistrip::dir_;
0416       std::string temp(ss.str());
0417       path(temp);
0418     }
0419   }
0420 }
0421 
0422 // -----------------------------------------------------------------------------
0423 //
0424 void SiStripFedKey::initGranularity() {
0425   granularity(sistrip::FED_SYSTEM);
0426   channel(0);
0427   if (fedId_ && fedId_ != sistrip::invalid_) {
0428     granularity(sistrip::FE_DRIVER);
0429     channel(fedId_);
0430     if (feUnit_ && feUnit_ != sistrip::invalid_) {
0431       granularity(sistrip::FE_UNIT);
0432       channel(feUnit_);
0433       if (feChan_ && feChan_ != sistrip::invalid_) {
0434         granularity(sistrip::FE_CHAN);
0435         channel(feChan_);
0436         if (fedApv_ && fedApv_ != sistrip::invalid_) {
0437           granularity(sistrip::FED_APV);
0438           channel(fedApv_);
0439         } else if (fedApv_ == sistrip::invalid_) {
0440           granularity(sistrip::UNKNOWN_GRAN);
0441           channel(sistrip::invalid_);
0442         }
0443       } else if (feChan_ == sistrip::invalid_) {
0444         granularity(sistrip::UNKNOWN_GRAN);
0445         channel(sistrip::invalid_);
0446       }
0447     } else if (feUnit_ == sistrip::invalid_) {
0448       granularity(sistrip::UNKNOWN_GRAN);
0449       channel(sistrip::invalid_);
0450     }
0451   } else if (fedId_ == sistrip::invalid_) {
0452     granularity(sistrip::UNKNOWN_GRAN);
0453     channel(sistrip::invalid_);
0454   }
0455 }
0456 
0457 // -----------------------------------------------------------------------------
0458 //
0459 void SiStripFedKey::terse(std::stringstream& ss) const {
0460   ss << "FED:crate/slot/id/unit/chan/apv= "
0461      << "-"
0462      << "/"
0463      << "-"
0464      << "/" << fedId() << "/" << feUnit() << "/" << feChan() << "/" << fedApv();
0465   //   ss << " FedKey"
0466   //     //<<"=0x"
0467   //     //<< std::hex
0468   //     //<< std::setfill('0') << std::setw(8) << key() << std::setfill(' ')
0469   //     //<< std::dec
0470   //     //<< ", " << ( isValid() ? "Valid" : "Invalid" )
0471   //     //<< ", FedCrate=" << fedCrate()
0472   //     //<< ", FedSlot=" << fedSlot()
0473   //      << ", FedId=" << fedId()
0474   //      << ", FeUnit=" << feUnit()
0475   //      << ", FeChan=" << feChan()
0476   //     //<< ", FedChannel=" << fedChannel()
0477   //      << ", FedApv=" << fedApv();
0478 }
0479 
0480 // -----------------------------------------------------------------------------
0481 //
0482 void SiStripFedKey::print(std::stringstream& ss) const {
0483   ss << " [SiStripFedKey::print]" << std::endl
0484      << std::hex << " FED key         : 0x" << std::setfill('0') << std::setw(8) << key() << std::endl
0485      << std::setfill(' ') << std::dec << " FED id          : " << fedId() << std::endl
0486      << " Front-End unit  : " << feUnit() << std::endl
0487      << " Front-End chan  : " << feChan() << std::endl
0488      << " (internal chan) : "
0489      << "(" << fedChannel() << ")" << std::endl
0490      << " FED APV         : " << fedApv() << std::endl
0491      << " Directory       : " << path() << std::endl
0492      << " Granularity     : " << SiStripEnumsAndStrings::granularity(granularity()) << std::endl
0493      << " Channel         : " << channel() << std::endl
0494      << " isValid         : " << isValid();
0495 }
0496 
0497 // -----------------------------------------------------------------------------
0498 //
0499 std::ostream& operator<<(std::ostream& os, const SiStripFedKey& input) {
0500   std::stringstream ss;
0501   input.print(ss);
0502   os << ss.str();
0503   return os;
0504 }