CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibFormats/SiStripObjects/src/SiStripModule.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripModule.cc,v 1.19 2009/10/27 09:50:29 lowette Exp $
00002 
00003 #include "CalibFormats/SiStripObjects/interface/SiStripModule.h"
00004 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 #include <iostream>
00007 #include <iomanip>
00008 
00009 using namespace sistrip;
00010 
00011 // -----------------------------------------------------------------------------
00012 //
00013 SiStripModule::SiStripModule( const FedChannelConnection& conn ) 
00014   : key_( conn.fecCrate(), 
00015           conn.fecSlot(), 
00016           conn.fecRing(), 
00017           conn.ccuAddr(), 
00018           conn.ccuChan() ),
00019     apv32_(0), 
00020     apv33_(0), 
00021     apv34_(0), 
00022     apv35_(0), 
00023     apv36_(0), 
00024     apv37_(0), 
00025     dcu0x00_(0), 
00026     mux0x43_(0), 
00027     pll0x44_(0), 
00028     lld0x60_(0), 
00029     dcuId_(0), 
00030     detId_(0), 
00031     nApvPairs_(0),
00032     cabling_(), 
00033     length_(0) 
00034 { 
00035   addDevices( conn ); 
00036 }
00037 
00038 // -----------------------------------------------------------------------------
00039 //
00040 void SiStripModule::addDevices( const FedChannelConnection& conn ) {
00041   
00042   if ( key_.fecCrate() && key_.fecCrate() != conn.fecCrate() ) {
00043     edm::LogWarning(mlCabling_)
00044       << "SiStripModule::" << __func__ << "]"
00045       << " Unexpected FEC crate ("
00046       << conn.fecCrate() << ") for this module ("
00047       << key_.fecCrate() << ")!";
00048     return;
00049   }
00050 
00051   if ( key_.fecSlot() && key_.fecSlot() != conn.fecSlot() ) {
00052     edm::LogWarning(mlCabling_)
00053             << "SiStripModule::" << __func__ << "]"
00054             << " Unexpected FEC slot ("
00055             << conn.fecSlot() << ") for this module ("
00056             << key_.fecSlot() << ")!";
00057     return;
00058   }
00059 
00060   if ( key_.fecRing() && key_.fecRing() != conn.fecRing() ) {
00061     edm::LogWarning(mlCabling_)
00062       << "SiStripModule::" << __func__ << "]"
00063       << " Unexpected FEC ring ("
00064       << conn.fecRing() << ") for this module ("
00065       << key_.fecRing() << ")!";
00066     return;
00067   }
00068 
00069   if ( key_.ccuAddr() && key_.ccuAddr() != conn.ccuAddr() ) {
00070     edm::LogWarning(mlCabling_)
00071       << "SiStripModule::" << __func__ << "]"
00072       << " Unexpected CCU addr ("
00073       << conn.ccuAddr() << ") for this module ("
00074       << key_.ccuAddr() << ")!";
00075     return;
00076   }
00077 
00078   if ( key_.ccuChan() && key_.ccuChan() != conn.ccuChan() ) {
00079     edm::LogWarning(mlCabling_)
00080       << "SiStripModule::" << __func__ << "]"
00081       << " Unexpected CCU chan ("
00082       << conn.ccuChan() << ") for this module ("
00083       << key_.ccuChan() << ")!";
00084     return;
00085   }
00086 
00087   // APVs
00088   if ( conn.i2cAddr(0) ) { addApv( conn.i2cAddr(0) ); }
00089   if ( conn.i2cAddr(1) ) { addApv( conn.i2cAddr(1) ); }
00090   
00091   // Detector
00092   dcuId( conn.dcuId() ); 
00093   detId( conn.detId() ); 
00094   nApvPairs( conn.nApvPairs() ); 
00095   
00096   // FED cabling
00097   FedChannel fed_ch( conn.fedCrate(), 
00098                      conn.fedSlot(), 
00099                      conn.fedId(), 
00100                      conn.fedCh() ); 
00101   fedCh( conn.i2cAddr(0), fed_ch );
00102   
00103   // DCU, MUX, PLL, LLD
00104   if ( conn.dcu() ) { dcu0x00_ = true; }
00105   if ( conn.mux() ) { mux0x43_ = true; }
00106   if ( conn.pll() ) { pll0x44_ = true; }
00107   if ( conn.lld() ) { lld0x60_ = true; }
00108   
00109 }
00110 
00111 // -----------------------------------------------------------------------------
00112 //
00113 std::vector<uint16_t> SiStripModule::activeApvs() const {
00114   std::vector<uint16_t> apvs;
00115   if ( apv32_ ) { apvs.push_back( apv32_ ); }
00116   if ( apv33_ ) { apvs.push_back( apv33_ ); }
00117   if ( apv34_ ) { apvs.push_back( apv34_ ); }
00118   if ( apv35_ ) { apvs.push_back( apv35_ ); }
00119   if ( apv36_ ) { apvs.push_back( apv36_ ); }
00120   if ( apv37_ ) { apvs.push_back( apv37_ ); }
00121   return apvs;
00122 }
00123 
00124 // -----------------------------------------------------------------------------
00125 //
00126  const uint16_t& SiStripModule::activeApv( const uint16_t& apv_address ) const {
00127   if      ( apv_address == 0 || apv_address == 32 ) { return apv32_; }
00128   else if ( apv_address == 1 || apv_address == 33 ) { return apv33_; }
00129   else if ( apv_address == 2 || apv_address == 34 ) { return apv34_; }
00130   else if ( apv_address == 3 || apv_address == 35 ) { return apv35_; }
00131   else if ( apv_address == 4 || apv_address == 36 ) { return apv36_; }
00132   else if ( apv_address == 5 || apv_address == 37 ) { return apv37_; }
00133   else {
00134     edm::LogWarning(mlCabling_)
00135       << "SiStripModule::" << __func__ << "]"
00136       << " Unexpected I2C address or number (" 
00137       << apv_address << ") for this module!";
00138   }
00139   static const uint16_t address = 0;
00140   return address;
00141 }
00142 
00143 // -----------------------------------------------------------------------------
00144 //
00145 void SiStripModule::addApv( const uint16_t& apv_address ) {
00146 
00147   // Some checks on value of APV I2C address
00148   if ( apv_address == 0 ) {
00149     edm::LogWarning(mlCabling_)
00150       << "SiStripModule::" << __func__ << "]"
00151       << " Null APV I2C address!"; 
00152     return;
00153   } else if ( apv_address < 32 && apv_address > 37 ) {
00154     edm::LogWarning(mlCabling_)
00155             << "SiStripModule::" << __func__ << "]"
00156             << " Unexpected I2C address (" 
00157             << apv_address << ") for APV!"; 
00158     return;
00159   }
00160 
00161   bool added_apv = false; 
00162   if      ( !apv32_ && apv_address == 32 ) { apv32_ = 32; added_apv = true; }
00163   else if ( !apv33_ && apv_address == 33 ) { apv33_ = 33; added_apv = true; }
00164   else if ( !apv34_ && apv_address == 34 ) { apv34_ = 34; added_apv = true; }
00165   else if ( !apv35_ && apv_address == 35 ) { apv35_ = 35; added_apv = true; }
00166   else if ( !apv36_ && apv_address == 36 ) { apv36_ = 36; added_apv = true; }
00167   else if ( !apv37_ && apv_address == 37 ) { apv37_ = 37; added_apv = true; }
00168   
00169   std::stringstream ss;
00170   ss << "SiStripModule::" << __func__ << "]";
00171   if ( added_apv ) { ss << " Added new APV for"; }
00172   else { ss << " APV already exists for"; }
00173   ss << " Crate/FEC/Ring/CCU/Module: "
00174      << key_.fecCrate() << "/"
00175      << key_.fecSlot() << "/"
00176      << key_.fecRing() << "/"
00177      << key_.ccuAddr() << "/"
00178      << key_.ccuChan() << "/"
00179      << apv_address;
00180   //if ( added_apv ) { LogTrace(mlCabling_) << ss.str(); }
00181   /* else */ if ( !added_apv ) { edm::LogWarning(mlCabling_) << ss.str(); }
00182   
00183 }
00184 
00185 // -----------------------------------------------------------------------------
00186 //
00187 void SiStripModule::nApvPairs( const uint16_t& npairs ) { 
00188   if ( npairs == 2 || npairs == 3 ) { nApvPairs_ = npairs; } 
00189   else if ( npairs == 0 ) {
00190     nApvPairs_ = 0;
00191     if ( apv32_ || apv33_ ) { nApvPairs_++; }
00192     if ( apv34_ || apv35_ ) { nApvPairs_++; }
00193     if ( apv36_ || apv37_ ) { nApvPairs_++; }
00194   } else { 
00195     edm::LogWarning(mlCabling_)
00196       << "SiStripModule::" << __func__ << "]"
00197       << " Unexpected number of APV pairs: " 
00198       << npairs;
00199   }
00200 } 
00201 
00202 // -----------------------------------------------------------------------------
00203 //
00204 SiStripModule::PairOfU16 SiStripModule::activeApvPair( const uint16_t& lld_channel ) const {
00205   if      ( lld_channel == 1 ) { return PairOfU16(apv32_,apv33_); }
00206   else if ( lld_channel == 2 ) { return PairOfU16(apv34_,apv35_); }
00207   else if ( lld_channel == 3 ) { return PairOfU16(apv36_,apv37_); }
00208   else { 
00209     edm::LogWarning(mlCabling_)
00210       << "SiStripModule::" << __func__ << "]"
00211       << " Unexpected LLD channel: " << lld_channel;
00212     return PairOfU16(0,0); 
00213   }
00214 }
00215 
00216 // -----------------------------------------------------------------------------
00217 //
00218 uint16_t SiStripModule::lldChannel( const uint16_t& apv_pair_num ) const {
00219   if ( apv_pair_num > 2 ) {
00220     edm::LogWarning(mlCabling_)
00221       << "SiStripModule::" << __func__ << "]"
00222       << " Unexpected APV pair number: " << apv_pair_num;
00223     return 0;
00224   }
00225   if ( nApvPairs_ != 2 && nApvPairs_ != 3 ) {
00226     edm::LogWarning(mlCabling_)
00227       << "SiStripModule::" << __func__ << "]"
00228       << " Unexpected number of APV pairs: " << nApvPairs_;
00229     return 0;
00230   }
00231   if ( nApvPairs_ == 2 && apv_pair_num == 1 ) { return 3; }
00232   else if ( nApvPairs_ == 2 && apv_pair_num == 2 ) { 
00233     edm::LogWarning(mlCabling_)
00234       << "[SiStripFecCabling::" << __func__ << "]"
00235       << " APV pair number is incompatible with"
00236       << " respect to number of !";
00237     return 0;
00238   } else { return apv_pair_num + 1; } 
00239 }
00240 
00241 // -----------------------------------------------------------------------------
00242 //
00243 uint16_t SiStripModule::apvPairNumber( const uint16_t& lld_channel ) const {
00244   if ( lld_channel < 1 || lld_channel > 3 ) {
00245     edm::LogWarning(mlCabling_)
00246       << "SiStripModule::" << __func__ << "]"
00247       << " Unexpected LLD channel: " << lld_channel;
00248     return 0;
00249   }
00250   if ( nApvPairs_ != 2 && nApvPairs_ != 3 ) {
00251     edm::LogWarning(mlCabling_)
00252       << "SiStripModule::" << __func__ << "]"
00253       << " Unexpected number of APV pairs: " << nApvPairs_;
00254     return 0;
00255   }
00256   if ( nApvPairs_ == 2 && lld_channel == 3 ) { return 1; }
00257   else if ( nApvPairs_ == 2 && lld_channel == 2 ) { 
00258     edm::LogWarning(mlCabling_)
00259       << "SiStripModule::" << __func__ << "]"
00260       << " LLD channel is incompatible with"
00261       << " respect to number of APV pairs!";
00262     return 0;
00263   } else { return lld_channel - 1; }
00264 }
00265 
00266 // -----------------------------------------------------------------------------
00267 //
00268 SiStripModule::FedChannel SiStripModule::fedCh( const uint16_t& apv_pair ) const {
00269 
00270   FedChannel fed_ch(0,0,0,0);
00271   
00272   if ( !nApvPairs() ) {
00273     
00274     edm::LogWarning(mlCabling_)
00275       << "SiStripModule::" << __func__ << "]"
00276       << " No APV pairs exist!";
00277     return fed_ch; 
00278 
00279   } else {
00280 
00281     uint16_t lld_ch = 0;
00282     if ( nApvPairs() == 2 ) {
00283 
00284       if      ( apv_pair == 0 ) { lld_ch = 1; }
00285       else if ( apv_pair == 1 ) { lld_ch = 3; }
00286       else { 
00287         edm::LogWarning(mlCabling_)
00288           << "SiStripModule::" << __func__ << "]"
00289           << " Unexpected pair number! " << apv_pair;
00290       }
00291 
00292     } else if ( nApvPairs() == 3 ) {
00293 
00294       if      ( apv_pair == 0 ) { lld_ch = 1; }
00295       else if ( apv_pair == 1 ) { lld_ch = 2; }
00296       else if ( apv_pair == 2 ) { lld_ch = 3; }
00297       else { 
00298         edm::LogWarning(mlCabling_)
00299           << "SiStripModule::" << __func__ << "]"
00300           << " Unexpected pair number! " << apv_pair;
00301       }
00302 
00303     } else {
00304 
00305       edm::LogWarning(mlCabling_) 
00306         << "SiStripModule::" << __func__ << "]"
00307         << " Unexpected number of APV pairs: " << nApvPairs();
00308 
00309     }
00310     
00311     FedCabling::const_iterator ipair = cabling_.find( lld_ch );
00312     if ( ipair != cabling_.end() ) { return (*ipair).second; }
00313     else { return fed_ch; }
00314 
00315   }
00316 
00317 }
00318 
00319 // -----------------------------------------------------------------------------
00320 //
00321 bool SiStripModule::fedCh( const uint16_t& apv_address, 
00322                            const FedChannel& fed_ch ) {
00323   // Determine LLD channel
00324   int16_t lld_ch = 1;
00325   if      ( apv_address == 32 || apv_address == 33 ) { lld_ch = 1; }
00326   else if ( apv_address == 34 || apv_address == 35 ) { lld_ch = 2; }
00327   else if ( apv_address == 36 || apv_address == 37 ) { lld_ch = 3; }
00328   else if ( apv_address == 0 ) { ; } //@@ do nothing?
00329   else { 
00330     edm::LogWarning(mlCabling_) << "[SiStripModule::fedCh]" 
00331                                 << " Unexpected I2C address (" 
00332                                 << apv_address << ") for APV!"; 
00333     return false;
00334   }
00335   // Search for entry in std::map
00336   //@@ use FedKey as key instead of lld chan? what about "duplicates"? 
00337   //@@ always append to std::map? then can have >3 entries. useful for debug?
00338   FedCabling::iterator ipair = cabling_.find( lld_ch );
00339   if ( ipair == cabling_.end() ) { cabling_[lld_ch] = fed_ch; }
00340   else { ipair->second = fed_ch; }
00341   return true;
00342 }
00343 
00344 // -----------------------------------------------------------------------------
00345 //
00346 void SiStripModule::print( std::stringstream& ss ) const {
00347 
00348   ss << " [SiStripModule::" << __func__ << "]" << std::endl
00349      << " Crate/FEC/Ring/CCU/Module               : "
00350      << key().fecCrate() << "/"
00351      << key().fecSlot() << "/"
00352      << key().fecRing() << "/"
00353      << key().ccuAddr() << "/"
00354      << key().ccuChan() << std::endl;
00355 
00356   ss << " ActiveApvs                              : ";
00357   std::vector<uint16_t> apvs = activeApvs();
00358   if ( apvs.empty() ) { ss << "NONE!"; }
00359   std::vector<uint16_t>::const_iterator iapv = apvs.begin();
00360   for ( ; iapv != apvs.end(); iapv++ ) { ss << *iapv << ", "; }
00361   ss << std::endl;
00362   
00363   ss << " DcuId/DetId/nPairs                      : "
00364      << std::hex
00365      << "0x" << std::setfill('0') << std::setw(8) << dcuId() << "/"
00366      << "0x" << std::setfill('0') << std::setw(8) << detId() << "/"
00367      << std::dec
00368      << nApvPairs() << std::endl;
00369   
00370   FedCabling channels = fedChannels();
00371   ss << " ApvPairNum/FedCrate/FedSlot/FedId/FedCh : ";
00372   FedCabling::const_iterator ichan = channels.begin();
00373   for ( ; ichan != channels.end(); ichan++ ) {
00374     ss << ichan->first << "/"
00375        << ichan->second.fedCrate_ << "/"
00376        << ichan->second.fedSlot_ << "/"
00377        << ichan->second.fedId_ << "/"
00378        << ichan->second.fedCh_ << ", ";
00379   }
00380   ss << std::endl;
00381   
00382   ss << " DCU/MUX/PLL/LLD found                   : "
00383      << bool(dcu0x00_) << "/"
00384      << bool(mux0x43_) << "/"
00385      << bool(pll0x44_) << "/"
00386      << bool(lld0x60_);
00387   
00388 }
00389 
00390 // -----------------------------------------------------------------------------
00391 //@@ NEEDS MODIFYING!!!!
00392 void SiStripModule::terse( std::stringstream& ss ) const {
00393 
00394   ss << " [SiStripModule::" << __func__ << "]" << std::endl
00395      << " Crate/FEC/Ring/CCU/Module               : "
00396      << key().fecCrate() << "/"
00397      << key().fecSlot() << "/"
00398      << key().fecRing() << "/"
00399      << key().ccuAddr() << "/"
00400      << key().ccuChan() << std::endl;
00401 
00402   ss << " ActiveApvs                              : ";
00403   std::vector<uint16_t> apvs = activeApvs();
00404   if ( apvs.empty() ) { ss << "NONE!"; }
00405   std::vector<uint16_t>::const_iterator iapv = apvs.begin();
00406   for ( ; iapv != apvs.end(); iapv++ ) { ss << *iapv << ", "; }
00407   ss << std::endl;
00408   
00409   ss << " DcuId/DetId/nPairs                      : "
00410      << std::hex
00411      << "0x" << std::setfill('0') << std::setw(8) << dcuId() << "/"
00412      << "0x" << std::setfill('0') << std::setw(8) << detId() << "/"
00413      << std::dec
00414      << nApvPairs() << std::endl;
00415   
00416   FedCabling channels = fedChannels();
00417   ss << " ApvPairNum/FedCrate/FedSlot/FedId/FedCh : ";
00418   FedCabling::const_iterator ichan = channels.begin();
00419   for ( ; ichan != channels.end(); ichan++ ) {
00420     ss << ichan->first << "/"
00421        << ichan->second.fedCrate_ << "/"
00422        << ichan->second.fedSlot_ << "/"
00423        << ichan->second.fedId_ << "/"
00424        << ichan->second.fedCh_ << ", ";
00425   }
00426   ss << std::endl;
00427   
00428   ss << " DCU/MUX/PLL/LLD found                   : "
00429      << bool(dcu0x00_) << "/"
00430      << bool(mux0x43_) << "/"
00431      << bool(pll0x44_) << "/"
00432      << bool(lld0x60_);
00433   
00434 }
00435 
00436 // -----------------------------------------------------------------------------
00437 //
00438 std::ostream& operator<< ( std::ostream& os, const SiStripModule& device ) {
00439   std::stringstream ss;
00440   device.print(ss);
00441   os << ss.str();
00442   return os;
00443 }