Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:19

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) {
0196     edm::LogWarning(mlCabling_) << ss.str();
0197   }
0198 }
0199 
0200 // -----------------------------------------------------------------------------
0201 //
0202 void SiStripModule::nApvPairs(const uint16_t &npairs) {
0203   if (npairs == 2 || npairs == 3) {
0204     nApvPairs_ = npairs;
0205   } else if (npairs == 0) {
0206     nApvPairs_ = 0;
0207     if (apv32_ || apv33_) {
0208       nApvPairs_++;
0209     }
0210     if (apv34_ || apv35_) {
0211       nApvPairs_++;
0212     }
0213     if (apv36_ || apv37_) {
0214       nApvPairs_++;
0215     }
0216   } else {
0217     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0218                                 << " Unexpected number of APV pairs: " << npairs;
0219   }
0220 }
0221 
0222 // -----------------------------------------------------------------------------
0223 //
0224 SiStripModule::PairOfU16 SiStripModule::activeApvPair(const uint16_t &lld_channel) const {
0225   if (lld_channel == 1) {
0226     return PairOfU16(apv32_, apv33_);
0227   } else if (lld_channel == 2) {
0228     return PairOfU16(apv34_, apv35_);
0229   } else if (lld_channel == 3) {
0230     return PairOfU16(apv36_, apv37_);
0231   } else {
0232     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0233                                 << " Unexpected LLD channel: " << lld_channel;
0234     return PairOfU16(0, 0);
0235   }
0236 }
0237 
0238 // -----------------------------------------------------------------------------
0239 //
0240 uint16_t SiStripModule::lldChannel(const uint16_t &apv_pair_num) const {
0241   if (apv_pair_num > 2) {
0242     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0243                                 << " Unexpected APV pair number: " << apv_pair_num;
0244     return 0;
0245   }
0246   if (nApvPairs_ != 2 && nApvPairs_ != 3) {
0247     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0248                                 << " Unexpected number of APV pairs: " << nApvPairs_;
0249     return 0;
0250   }
0251   if (nApvPairs_ == 2 && apv_pair_num == 1) {
0252     return 3;
0253   } else if (nApvPairs_ == 2 && apv_pair_num == 2) {
0254     edm::LogWarning(mlCabling_) << "[SiStripFecCabling::" << __func__ << "]"
0255                                 << " APV pair number is incompatible with"
0256                                 << " respect to number of !";
0257     return 0;
0258   } else {
0259     return apv_pair_num + 1;
0260   }
0261 }
0262 
0263 // -----------------------------------------------------------------------------
0264 //
0265 uint16_t SiStripModule::apvPairNumber(const uint16_t &lld_channel) const {
0266   if (lld_channel < 1 || lld_channel > 3) {
0267     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0268                                 << " Unexpected LLD channel: " << lld_channel;
0269     return 0;
0270   }
0271   if (nApvPairs_ != 2 && nApvPairs_ != 3) {
0272     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0273                                 << " Unexpected number of APV pairs: " << nApvPairs_;
0274     return 0;
0275   }
0276   if (nApvPairs_ == 2 && lld_channel == 3) {
0277     return 1;
0278   } else if (nApvPairs_ == 2 && lld_channel == 2) {
0279     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0280                                 << " LLD channel is incompatible with"
0281                                 << " respect to number of APV pairs!";
0282     return 0;
0283   } else {
0284     return lld_channel - 1;
0285   }
0286 }
0287 
0288 // -----------------------------------------------------------------------------
0289 //
0290 SiStripModule::FedChannel SiStripModule::fedCh(const uint16_t &apv_pair) const {
0291   FedChannel fed_ch(0, 0, 0, 0);
0292 
0293   if (!nApvPairs()) {
0294     edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0295                                 << " No APV pairs exist!";
0296     return fed_ch;
0297 
0298   } else {
0299     uint16_t lld_ch = 0;
0300     if (nApvPairs() == 2) {
0301       if (apv_pair == 0) {
0302         lld_ch = 1;
0303       } else if (apv_pair == 1) {
0304         lld_ch = 3;
0305       } else {
0306         edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0307                                     << " Unexpected pair number! " << apv_pair;
0308       }
0309 
0310     } else if (nApvPairs() == 3) {
0311       if (apv_pair == 0) {
0312         lld_ch = 1;
0313       } else if (apv_pair == 1) {
0314         lld_ch = 2;
0315       } else if (apv_pair == 2) {
0316         lld_ch = 3;
0317       } else {
0318         edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0319                                     << " Unexpected pair number! " << apv_pair;
0320       }
0321 
0322     } else {
0323       edm::LogWarning(mlCabling_) << "SiStripModule::" << __func__ << "]"
0324                                   << " Unexpected number of APV pairs: " << nApvPairs();
0325     }
0326 
0327     FedCabling::const_iterator ipair = cabling_.find(lld_ch);
0328     if (ipair != cabling_.end()) {
0329       return (*ipair).second;
0330     } else {
0331       return fed_ch;
0332     }
0333   }
0334 }
0335 
0336 // -----------------------------------------------------------------------------
0337 //
0338 bool SiStripModule::fedCh(const uint16_t &apv_address, const FedChannel &fed_ch) {
0339   // Determine LLD channel
0340   int16_t lld_ch = 1;
0341   if (apv_address == 32 || apv_address == 33) {
0342     lld_ch = 1;
0343   } else if (apv_address == 34 || apv_address == 35) {
0344     lld_ch = 2;
0345   } else if (apv_address == 36 || apv_address == 37) {
0346     lld_ch = 3;
0347   } else if (apv_address == 0) {
0348     ;
0349   }  //@@ do nothing?
0350   else {
0351     edm::LogWarning(mlCabling_) << "[SiStripModule::fedCh]"
0352                                 << " Unexpected I2C address (" << apv_address << ") for APV!";
0353     return false;
0354   }
0355   // Search for entry in std::map
0356   //@@ use FedKey as key instead of lld chan? what about "duplicates"?
0357   //@@ always append to std::map? then can have >3 entries. useful for debug?
0358   FedCabling::iterator ipair = cabling_.find(lld_ch);
0359   if (ipair == cabling_.end()) {
0360     cabling_[lld_ch] = fed_ch;
0361   } else {
0362     ipair->second = fed_ch;
0363   }
0364   return true;
0365 }
0366 
0367 // -----------------------------------------------------------------------------
0368 //
0369 void SiStripModule::print(std::stringstream &ss) const {
0370   ss << " [SiStripModule::" << __func__ << "]" << std::endl
0371      << " Crate/FEC/Ring/CCU/Module               : " << key().fecCrate() << "/" << key().fecSlot() << "/"
0372      << key().fecRing() << "/" << key().ccuAddr() << "/" << key().ccuChan() << std::endl;
0373 
0374   ss << " ActiveApvs                              : ";
0375   std::vector<uint16_t> apvs = activeApvs();
0376   if (apvs.empty()) {
0377     ss << "NONE!";
0378   }
0379   std::vector<uint16_t>::const_iterator iapv = apvs.begin();
0380   for (; iapv != apvs.end(); ++iapv) {
0381     ss << *iapv << ", ";
0382   }
0383   ss << std::endl;
0384 
0385   ss << " DcuId/DetId/nPairs                      : " << std::hex << "0x" << std::setfill('0') << std::setw(8)
0386      << dcuId() << "/"
0387      << "0x" << std::setfill('0') << std::setw(8) << detId() << "/" << std::dec << nApvPairs() << std::endl;
0388 
0389   FedCabling channels = fedChannels();
0390   ss << " ApvPairNum/FedCrate/FedSlot/FedId/FedCh : ";
0391   FedCabling::const_iterator ichan = channels.begin();
0392   for (; ichan != channels.end(); ++ichan) {
0393     ss << ichan->first << "/" << ichan->second.fedCrate_ << "/" << ichan->second.fedSlot_ << "/" << ichan->second.fedId_
0394        << "/" << ichan->second.fedCh_ << ", ";
0395   }
0396   ss << std::endl;
0397 
0398   ss << " DCU/MUX/PLL/LLD found                   : " << bool(dcu0x00_) << "/" << bool(mux0x43_) << "/"
0399      << bool(pll0x44_) << "/" << bool(lld0x60_);
0400 }
0401 
0402 // -----------------------------------------------------------------------------
0403 //@@ NEEDS MODIFYING!!!!
0404 void SiStripModule::terse(std::stringstream &ss) const {
0405   ss << " [SiStripModule::" << __func__ << "]" << std::endl
0406      << " Crate/FEC/Ring/CCU/Module               : " << key().fecCrate() << "/" << key().fecSlot() << "/"
0407      << key().fecRing() << "/" << key().ccuAddr() << "/" << key().ccuChan() << std::endl;
0408 
0409   ss << " ActiveApvs                              : ";
0410   std::vector<uint16_t> apvs = activeApvs();
0411   if (apvs.empty()) {
0412     ss << "NONE!";
0413   }
0414   std::vector<uint16_t>::const_iterator iapv = apvs.begin();
0415   for (; iapv != apvs.end(); ++iapv) {
0416     ss << *iapv << ", ";
0417   }
0418   ss << std::endl;
0419 
0420   ss << " DcuId/DetId/nPairs                      : " << std::hex << "0x" << std::setfill('0') << std::setw(8)
0421      << dcuId() << "/"
0422      << "0x" << std::setfill('0') << std::setw(8) << detId() << "/" << std::dec << nApvPairs() << std::endl;
0423 
0424   FedCabling channels = fedChannels();
0425   ss << " ApvPairNum/FedCrate/FedSlot/FedId/FedCh : ";
0426   FedCabling::const_iterator ichan = channels.begin();
0427   for (; ichan != channels.end(); ++ichan) {
0428     ss << ichan->first << "/" << ichan->second.fedCrate_ << "/" << ichan->second.fedSlot_ << "/" << ichan->second.fedId_
0429        << "/" << ichan->second.fedCh_ << ", ";
0430   }
0431   ss << std::endl;
0432 
0433   ss << " DCU/MUX/PLL/LLD found                   : " << bool(dcu0x00_) << "/" << bool(mux0x43_) << "/"
0434      << bool(pll0x44_) << "/" << bool(lld0x60_);
0435 }
0436 
0437 // -----------------------------------------------------------------------------
0438 //
0439 std::ostream &operator<<(std::ostream &os, const SiStripModule &device) {
0440   std::stringstream ss;
0441   device.print(ss);
0442   os << ss.str();
0443   return os;
0444 }