Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:31

0001 
0002 #include "CalibFormats/SiStripObjects/interface/SiStripModule.h"
0003 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include <iomanip>
0006 #include <iostream>
0007 
0008 using namespace sistrip;
0009 
0010 // -----------------------------------------------------------------------------
0011 //
0012 SiStripModule::SiStripModule(const FedChannelConnection &conn)
0013     : key_(conn.fecCrate(), conn.fecSlot(), conn.fecRing(), conn.ccuAddr(), conn.ccuChan()),
0014       apv32_(0),
0015       apv33_(0),
0016       apv34_(0),
0017       apv35_(0),
0018       apv36_(0),
0019       apv37_(0),
0020       dcu0x00_(0),
0021       mux0x43_(0),
0022       pll0x44_(0),
0023       lld0x60_(0),
0024       dcuId_(0),
0025       detId_(0),
0026       nApvPairs_(0),
0027       cabling_(),
0028       length_(0) {
0029   addDevices(conn);
0030 }
0031 
0032 // -----------------------------------------------------------------------------
0033 //
0034 void SiStripModule::addDevices(const FedChannelConnection &conn) {
0035   if (key_.fecCrate() && key_.fecCrate() != conn.fecCrate()) {
0036     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0037                                 << " Unexpected FEC crate (" << conn.fecCrate() << ") for this module ("
0038                                 << key_.fecCrate() << ")!";
0039     return;
0040   }
0041 
0042   if (key_.fecSlot() && key_.fecSlot() != conn.fecSlot()) {
0043     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0044                                 << " Unexpected FEC slot (" << conn.fecSlot() << ") for this module (" << key_.fecSlot()
0045                                 << ")!";
0046     return;
0047   }
0048 
0049   if (key_.fecRing() && key_.fecRing() != conn.fecRing()) {
0050     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0051                                 << " Unexpected FEC ring (" << conn.fecRing() << ") for this module (" << key_.fecRing()
0052                                 << ")!";
0053     return;
0054   }
0055 
0056   if (key_.ccuAddr() && key_.ccuAddr() != conn.ccuAddr()) {
0057     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0058                                 << " Unexpected CCU addr (" << conn.ccuAddr() << ") for this module (" << key_.ccuAddr()
0059                                 << ")!";
0060     return;
0061   }
0062 
0063   if (key_.ccuChan() && key_.ccuChan() != conn.ccuChan()) {
0064     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0065                                 << " Unexpected CCU chan (" << conn.ccuChan() << ") for this module (" << key_.ccuChan()
0066                                 << ")!";
0067     return;
0068   }
0069 
0070   // APVs
0071   if (conn.i2cAddr(0)) {
0072     addApv(conn.i2cAddr(0));
0073   }
0074   if (conn.i2cAddr(1)) {
0075     addApv(conn.i2cAddr(1));
0076   }
0077 
0078   // Detector
0079   dcuId(conn.dcuId());
0080   detId(conn.detId());
0081   nApvPairs(conn.nApvPairs());
0082 
0083   // FED cabling
0084   FedChannel fed_ch(conn.fedCrate(), conn.fedSlot(), conn.fedId(), conn.fedCh());
0085   fedCh(conn.i2cAddr(0), fed_ch);
0086 
0087   // DCU, MUX, PLL, LLD
0088   if (conn.dcu()) {
0089     dcu0x00_ = true;
0090   }
0091   if (conn.mux()) {
0092     mux0x43_ = true;
0093   }
0094   if (conn.pll()) {
0095     pll0x44_ = true;
0096   }
0097   if (conn.lld()) {
0098     lld0x60_ = true;
0099   }
0100 }
0101 
0102 // -----------------------------------------------------------------------------
0103 //
0104 std::vector<uint16_t> SiStripModule::activeApvs() const {
0105   std::vector<uint16_t> apvs;
0106   if (apv32_) {
0107     apvs.push_back(apv32_);
0108   }
0109   if (apv33_) {
0110     apvs.push_back(apv33_);
0111   }
0112   if (apv34_) {
0113     apvs.push_back(apv34_);
0114   }
0115   if (apv35_) {
0116     apvs.push_back(apv35_);
0117   }
0118   if (apv36_) {
0119     apvs.push_back(apv36_);
0120   }
0121   if (apv37_) {
0122     apvs.push_back(apv37_);
0123   }
0124   return apvs;
0125 }
0126 
0127 // -----------------------------------------------------------------------------
0128 //
0129 const uint16_t &SiStripModule::activeApv(const uint16_t &apv_address) const {
0130   if (apv_address == 0 || apv_address == 32) {
0131     return apv32_;
0132   } else if (apv_address == 1 || apv_address == 33) {
0133     return apv33_;
0134   } else if (apv_address == 2 || apv_address == 34) {
0135     return apv34_;
0136   } else if (apv_address == 3 || apv_address == 35) {
0137     return apv35_;
0138   } else if (apv_address == 4 || apv_address == 36) {
0139     return apv36_;
0140   } else if (apv_address == 5 || apv_address == 37) {
0141     return apv37_;
0142   } else {
0143     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0144                                 << " Unexpected I2C address or number (" << apv_address << ") for this module!";
0145   }
0146   static const uint16_t address = 0;
0147   return address;
0148 }
0149 
0150 // -----------------------------------------------------------------------------
0151 //
0152 void SiStripModule::addApv(const uint16_t &apv_address) {
0153   // Some checks on value of APV I2C address
0154   if (apv_address == 0) {
0155     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0156                                 << " Null APV I2C address!";
0157     return;
0158   } else if (apv_address < 32 || apv_address > 37) {
0159     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0160                                 << " Unexpected I2C address (" << apv_address << ") for APV!";
0161     return;
0162   }
0163 
0164   bool added_apv = false;
0165   if (!apv32_ && apv_address == 32) {
0166     apv32_ = 32;
0167     added_apv = true;
0168   } else if (!apv33_ && apv_address == 33) {
0169     apv33_ = 33;
0170     added_apv = true;
0171   } else if (!apv34_ && apv_address == 34) {
0172     apv34_ = 34;
0173     added_apv = true;
0174   } else if (!apv35_ && apv_address == 35) {
0175     apv35_ = 35;
0176     added_apv = true;
0177   } else if (!apv36_ && apv_address == 36) {
0178     apv36_ = 36;
0179     added_apv = true;
0180   } else if (!apv37_ && apv_address == 37) {
0181     apv37_ = 37;
0182     added_apv = true;
0183   }
0184 
0185   std::stringstream ss;
0186   ss << "SiStripModule::" << __func__ << "]";
0187   if (added_apv) {
0188     ss << " Added new APV for";
0189   } else {
0190     ss << " APV already exists for";
0191   }
0192   ss << " Crate/FEC/Ring/CCU/Module: " << key_.fecCrate() << "/" << key_.fecSlot() << "/" << key_.fecRing() << "/"
0193      << key_.ccuAddr() << "/" << key_.ccuChan() << "/" << apv_address;
0194   // if ( added_apv ) { LogTrace(mlCabling_) << ss.str(); }
0195   /* else */ if (!added_apv) { edm::LogWarning(mlCabling_) << ss.str(); }
0196 }
0197 
0198 // -----------------------------------------------------------------------------
0199 //
0200 void SiStripModule::nApvPairs(const uint16_t &npairs) {
0201   if (npairs == 2 || npairs == 3) {
0202     nApvPairs_ = npairs;
0203   } else if (npairs == 0) {
0204     nApvPairs_ = 0;
0205     if (apv32_ || apv33_) {
0206       nApvPairs_++;
0207     }
0208     if (apv34_ || apv35_) {
0209       nApvPairs_++;
0210     }
0211     if (apv36_ || apv37_) {
0212       nApvPairs_++;
0213     }
0214   } else {
0215     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0216                                 << " Unexpected number of APV pairs: " << npairs;
0217   }
0218 }
0219 
0220 // -----------------------------------------------------------------------------
0221 //
0222 SiStripModule::PairOfU16 SiStripModule::activeApvPair(const uint16_t &lld_channel) const {
0223   if (lld_channel == 1) {
0224     return PairOfU16(apv32_, apv33_);
0225   } else if (lld_channel == 2) {
0226     return PairOfU16(apv34_, apv35_);
0227   } else if (lld_channel == 3) {
0228     return PairOfU16(apv36_, apv37_);
0229   } else {
0230     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0231                                 << " Unexpected LLD channel: " << lld_channel;
0232     return PairOfU16(0, 0);
0233   }
0234 }
0235 
0236 // -----------------------------------------------------------------------------
0237 //
0238 uint16_t SiStripModule::lldChannel(const uint16_t &apv_pair_num) const {
0239   if (apv_pair_num > 2) {
0240     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0241                                 << " Unexpected APV pair number: " << apv_pair_num;
0242     return 0;
0243   }
0244   if (nApvPairs_ != 2 && nApvPairs_ != 3) {
0245     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0246                                 << " Unexpected number of APV pairs: " << nApvPairs_;
0247     return 0;
0248   }
0249   if (nApvPairs_ == 2 && apv_pair_num == 1) {
0250     return 3;
0251   } else if (nApvPairs_ == 2 && apv_pair_num == 2) {
0252     edm::LogWarning(mlCabling_) << "[SiStripFecCabling::" << __func__ << "]"
0253                                 << " APV pair number is incompatible with"
0254                                 << " respect to number of !";
0255     return 0;
0256   } else {
0257     return apv_pair_num + 1;
0258   }
0259 }
0260 
0261 // -----------------------------------------------------------------------------
0262 //
0263 uint16_t SiStripModule::apvPairNumber(const uint16_t &lld_channel) const {
0264   if (lld_channel < 1 || lld_channel > 3) {
0265     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0266                                 << " Unexpected LLD channel: " << lld_channel;
0267     return 0;
0268   }
0269   if (nApvPairs_ != 2 && nApvPairs_ != 3) {
0270     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0271                                 << " Unexpected number of APV pairs: " << nApvPairs_;
0272     return 0;
0273   }
0274   if (nApvPairs_ == 2 && lld_channel == 3) {
0275     return 1;
0276   } else if (nApvPairs_ == 2 && lld_channel == 2) {
0277     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0278                                 << " LLD channel is incompatible with"
0279                                 << " respect to number of APV pairs!";
0280     return 0;
0281   } else {
0282     return lld_channel - 1;
0283   }
0284 }
0285 
0286 // -----------------------------------------------------------------------------
0287 //
0288 SiStripModule::FedChannel SiStripModule::fedCh(const uint16_t &apv_pair) const {
0289   FedChannel fed_ch(0, 0, 0, 0);
0290 
0291   if (!nApvPairs()) {
0292     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0293                                 << " No APV pairs exist!";
0294     return fed_ch;
0295 
0296   } else {
0297     uint16_t lld_ch = 0;
0298     if (nApvPairs() == 2) {
0299       if (apv_pair == 0) {
0300         lld_ch = 1;
0301       } else if (apv_pair == 1) {
0302         lld_ch = 3;
0303       } else {
0304         edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0305                                     << " Unexpected pair number! " << apv_pair;
0306       }
0307 
0308     } else if (nApvPairs() == 3) {
0309       if (apv_pair == 0) {
0310         lld_ch = 1;
0311       } else if (apv_pair == 1) {
0312         lld_ch = 2;
0313       } else if (apv_pair == 2) {
0314         lld_ch = 3;
0315       } else {
0316         edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0317                                     << " Unexpected pair number! " << apv_pair;
0318       }
0319 
0320     } else {
0321       edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0322                                   << " Unexpected number of APV pairs: " << nApvPairs();
0323     }
0324 
0325     FedCabling::const_iterator ipair = cabling_.find(lld_ch);
0326     if (ipair != cabling_.end()) {
0327       return (*ipair).second;
0328     } else {
0329       return fed_ch;
0330     }
0331   }
0332 }
0333 
0334 // -----------------------------------------------------------------------------
0335 //
0336 bool SiStripModule::fedCh(const uint16_t &apv_address, const FedChannel &fed_ch) {
0337   // Determine LLD channel
0338   int16_t lld_ch = 1;
0339   if (apv_address == 32 || apv_address == 33) {
0340     lld_ch = 1;
0341   } else if (apv_address == 34 || apv_address == 35) {
0342     lld_ch = 2;
0343   } else if (apv_address == 36 || apv_address == 37) {
0344     lld_ch = 3;
0345   } else if (apv_address == 0) {
0346     ;
0347   }  //@@ do nothing?
0348   else {
0349     edm::LogWarning(mlCabling_) << "[SiStripModule::fedCh]"
0350                                 << " Unexpected I2C address (" << apv_address << ") for APV!";
0351     return false;
0352   }
0353   // Search for entry in std::map
0354   //@@ use FedKey as key instead of lld chan? what about "duplicates"?
0355   //@@ always append to std::map? then can have >3 entries. useful for debug?
0356   FedCabling::iterator ipair = cabling_.find(lld_ch);
0357   if (ipair == cabling_.end()) {
0358     cabling_[lld_ch] = fed_ch;
0359   } else {
0360     ipair->second = fed_ch;
0361   }
0362   return true;
0363 }
0364 
0365 // -----------------------------------------------------------------------------
0366 //
0367 void SiStripModule::print(std::stringstream &ss) const {
0368   ss << " [SiStripModule::" << __func__ << "]" << std::endl
0369      << " Crate/FEC/Ring/CCU/Module               : " << key().fecCrate() << "/" << key().fecSlot() << "/"
0370      << key().fecRing() << "/" << key().ccuAddr() << "/" << key().ccuChan() << std::endl;
0371 
0372   ss << " ActiveApvs                              : ";
0373   std::vector<uint16_t> apvs = activeApvs();
0374   if (apvs.empty()) {
0375     ss << "NONE!";
0376   }
0377   std::vector<uint16_t>::const_iterator iapv = apvs.begin();
0378   for (; iapv != apvs.end(); ++iapv) {
0379     ss << *iapv << ", ";
0380   }
0381   ss << std::endl;
0382 
0383   ss << " DcuId/DetId/nPairs                      : " << std::hex << "0x" << std::setfill('0') << std::setw(8)
0384      << dcuId() << "/"
0385      << "0x" << std::setfill('0') << std::setw(8) << detId() << "/" << std::dec << nApvPairs() << std::endl;
0386 
0387   FedCabling channels = fedChannels();
0388   ss << " ApvPairNum/FedCrate/FedSlot/FedId/FedCh : ";
0389   FedCabling::const_iterator ichan = channels.begin();
0390   for (; ichan != channels.end(); ++ichan) {
0391     ss << ichan->first << "/" << ichan->second.fedCrate_ << "/" << ichan->second.fedSlot_ << "/" << ichan->second.fedId_
0392        << "/" << ichan->second.fedCh_ << ", ";
0393   }
0394   ss << std::endl;
0395 
0396   ss << " DCU/MUX/PLL/LLD found                   : " << bool(dcu0x00_) << "/" << bool(mux0x43_) << "/"
0397      << bool(pll0x44_) << "/" << bool(lld0x60_);
0398 }
0399 
0400 // -----------------------------------------------------------------------------
0401 //@@ NEEDS MODIFYING!!!!
0402 void SiStripModule::terse(std::stringstream &ss) const {
0403   ss << " [SiStripModule::" << __func__ << "]" << std::endl
0404      << " Crate/FEC/Ring/CCU/Module               : " << key().fecCrate() << "/" << key().fecSlot() << "/"
0405      << key().fecRing() << "/" << key().ccuAddr() << "/" << key().ccuChan() << std::endl;
0406 
0407   ss << " ActiveApvs                              : ";
0408   std::vector<uint16_t> apvs = activeApvs();
0409   if (apvs.empty()) {
0410     ss << "NONE!";
0411   }
0412   std::vector<uint16_t>::const_iterator iapv = apvs.begin();
0413   for (; iapv != apvs.end(); ++iapv) {
0414     ss << *iapv << ", ";
0415   }
0416   ss << std::endl;
0417 
0418   ss << " DcuId/DetId/nPairs                      : " << std::hex << "0x" << std::setfill('0') << std::setw(8)
0419      << dcuId() << "/"
0420      << "0x" << std::setfill('0') << std::setw(8) << detId() << "/" << std::dec << nApvPairs() << std::endl;
0421 
0422   FedCabling channels = fedChannels();
0423   ss << " ApvPairNum/FedCrate/FedSlot/FedId/FedCh : ";
0424   FedCabling::const_iterator ichan = channels.begin();
0425   for (; ichan != channels.end(); ++ichan) {
0426     ss << ichan->first << "/" << ichan->second.fedCrate_ << "/" << ichan->second.fedSlot_ << "/" << ichan->second.fedId_
0427        << "/" << ichan->second.fedCh_ << ", ";
0428   }
0429   ss << std::endl;
0430 
0431   ss << " DCU/MUX/PLL/LLD found                   : " << bool(dcu0x00_) << "/" << bool(mux0x43_) << "/"
0432      << bool(pll0x44_) << "/" << bool(lld0x60_);
0433 }
0434 
0435 // -----------------------------------------------------------------------------
0436 //
0437 std::ostream &operator<<(std::ostream &os, const SiStripModule &device) {
0438   std::stringstream ss;
0439   device.print(ss);
0440   os << ss.str();
0441   return os;
0442 }