Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:17

0001 
0002 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
0003 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 
0006 using namespace std;
0007 using namespace sistrip;
0008 
0009 // -----------------------------------------------------------------------------
0010 //
0011 SiStripConfigDb::DeviceDescriptionsRange SiStripConfigDb::getDeviceDescriptions(std::string partition) {
0012   // Check
0013   if ((!dbParams_.usingDbCache() && !deviceFactory(__func__)) ||
0014       (dbParams_.usingDbCache() && !databaseCache(__func__))) {
0015     return devices_.emptyRange();
0016   }
0017 
0018   try {
0019     if (!dbParams_.usingDbCache()) {
0020       SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0021       SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0022       for (; iter != jter; ++iter) {
0023         if (partition.empty() || partition == iter->second.partitionName()) {
0024           if (iter->second.partitionName() == SiStripPartition::defaultPartitionName_) {
0025             continue;
0026           }
0027 
0028           DeviceDescriptionsRange range = devices_.find(iter->second.partitionName());
0029           if (range == devices_.emptyRange()) {
0030             // Retrieve conections
0031             DeviceDescriptionsV tmp1;
0032             deviceFactory(__func__)->getFecDeviceDescriptions(iter->second.partitionName(),
0033                                                               tmp1,
0034                                                               iter->second.fecVersion().first,
0035                                                               iter->second.fecVersion().second,
0036                                                               //#ifdef USING_DATABASE_MASKING
0037                                                               iter->second.maskVersion().first,
0038                                                               iter->second.maskVersion().second,
0039                                                               //#endif
0040                                                               false);  //@@ do not get DISABLED devices
0041 
0042             // Make local copy
0043             DeviceDescriptionsV tmp2;
0044             FecFactory::vectorCopyI(tmp2, tmp1, true);
0045 
0046             // Add to cache
0047             devices_.loadNext(iter->second.partitionName(), tmp2);
0048 
0049             // Some debug
0050             DeviceDescriptionsRange range = devices_.find(iter->second.partitionName());
0051             std::stringstream ss;
0052             ss << "[SiStripConfigDb::" << __func__ << "]"
0053                << " Downloaded " << range.size() << " device descriptions to local cache for partition \""
0054                << iter->second.partitionName() << "\"" << std::endl;
0055             ss << "[SiStripConfigDb::" << __func__ << "]"
0056                << " Cache holds device descriptions for " << devices_.size() << " partitions.";
0057             LogTrace(mlConfigDb_) << ss.str();
0058           }
0059         }
0060       }
0061 
0062     } else {  // Using database cache
0063 
0064       DeviceDescriptionsV* tmp1 = databaseCache(__func__)->getDevices();
0065 
0066       if (tmp1) {
0067         // Make local copy
0068         DeviceDescriptionsV tmp2;
0069         FecFactory::vectorCopyI(tmp2, *tmp1, true);
0070 
0071         // Add to cache
0072         devices_.loadNext(SiStripPartition::defaultPartitionName_, tmp2);
0073 
0074       } else {
0075         edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0076                                      << " NULL pointer to DeviceDescriptions vector!";
0077       }
0078     }
0079 
0080   } catch (...) {
0081     handleException(__func__);
0082   }
0083 
0084   // Create range object
0085   uint16_t np = 0;
0086   uint16_t nc = 0;
0087   DeviceDescriptionsRange devs;
0088   if (!partition.empty()) {
0089     devs = devices_.find(partition);
0090     np = 1;
0091     nc = devs.size();
0092   } else {
0093     if (!devices_.empty()) {
0094       devs = DeviceDescriptionsRange(devices_.find(dbParams_.partitions().begin()->second.partitionName()).begin(),
0095                                      devices_.find((--(dbParams_.partitions().end()))->second.partitionName()).end());
0096     } else {
0097       devs = devices_.emptyRange();
0098     }
0099     np = devices_.size();
0100     nc = devs.size();
0101   }
0102 
0103   stringstream ss;
0104   ss << "[SiStripConfigDb::" << __func__ << "]"
0105      << " Found " << nc << " device descriptions";
0106   if (!dbParams_.usingDb()) {
0107     ss << " in " << dbParams_.inputFecXmlFiles().size() << " 'fec.xml' file(s)";
0108   } else {
0109     if (!dbParams_.usingDbCache()) {
0110       ss << " in " << np << " database partition(s)";
0111     } else {
0112       ss << " from shared memory name '" << dbParams_.sharedMemory() << "'";
0113     }
0114   }
0115   if (devices_.empty()) {
0116     edm::LogWarning(mlConfigDb_) << ss.str();
0117   } else {
0118     LogTrace(mlConfigDb_) << ss.str();
0119   }
0120 
0121   return devs;
0122 }
0123 
0124 // -----------------------------------------------------------------------------
0125 //
0126 SiStripConfigDb::DeviceDescriptionsRange SiStripConfigDb::getDeviceDescriptions(DeviceType device_type,
0127                                                                                 std::string partition) {
0128   typedDevices_.clear();
0129   apvDevices_.clear();
0130   muxDevices_.clear();
0131   dcuDevices_.clear();
0132   lldDevices_.clear();
0133   pllDevices_.clear();
0134   dohDevices_.clear();
0135 
0136   DeviceDescriptionsRange tmp(typedDevices_.end(), typedDevices_.end());
0137 
0138   if ((!dbParams_.usingDbCache() && !deviceFactory(__func__)) ||
0139       (dbParams_.usingDbCache() && !databaseCache(__func__))) {
0140     return tmp;
0141   }
0142 
0143   try {
0144     DeviceDescriptionsRange devs = SiStripConfigDb::getDeviceDescriptions(partition);
0145     if (!devs.empty()) {
0146       DeviceDescriptionsV tmp(devs.begin(), devs.end());
0147       typedDevices_ = FecFactory::getDeviceFromDeviceVector(tmp, device_type);
0148     }
0149   } catch (...) {
0150     handleException(__func__);
0151   }
0152 
0153   if (device_type == APV25) {
0154     apvDevices_.swap(typedDevices_);
0155     tmp = DeviceDescriptionsRange(apvDevices_.begin(), apvDevices_.end());
0156   } else if (device_type == APVMUX) {
0157     muxDevices_.swap(typedDevices_);
0158     tmp = DeviceDescriptionsRange(muxDevices_.begin(), muxDevices_.end());
0159   } else if (device_type == DCU) {
0160     dcuDevices_.swap(typedDevices_);
0161     tmp = DeviceDescriptionsRange(dcuDevices_.begin(), dcuDevices_.end());
0162   } else if (device_type == LASERDRIVER) {
0163     lldDevices_.swap(typedDevices_);
0164     tmp = DeviceDescriptionsRange(lldDevices_.begin(), lldDevices_.end());
0165   } else if (device_type == PLL) {
0166     pllDevices_.swap(typedDevices_);
0167     tmp = DeviceDescriptionsRange(pllDevices_.begin(), pllDevices_.end());
0168   } else if (device_type == DOH) {
0169     dohDevices_.swap(typedDevices_);
0170     tmp = DeviceDescriptionsRange(dohDevices_.begin(), dohDevices_.end());
0171   } else {
0172     tmp = DeviceDescriptionsRange(typedDevices_.begin(), typedDevices_.end());
0173   }
0174 
0175   //   stringstream sss;
0176   //   sss << " Number of devices:"
0177   //       << " TYP: " << typedDevices_.size()
0178   //       << " APV: " << apvDevices_.size()
0179   //       << " MUX: " << muxDevices_.size()
0180   //       << " DCU: " << dcuDevices_.size()
0181   //       << " LLD: " << lldDevices_.size()
0182   //       << " PLL: " << pllDevices_.size()
0183   //       << " DOH: " << dohDevices_.size()
0184   //       << " tmp: " << tmp.size();
0185   //   LogTrace(mlConfigDb_) << sss.str();
0186 
0187   stringstream ss;
0188   ss << "[SiStripConfigDb::" << __func__ << "]"
0189      << " Extracted " << tmp.size() << " device descriptions (for devices of type " << deviceType(device_type) << ")";
0190   LogTrace(mlConfigDb_) << ss.str();
0191 
0192   return tmp;
0193 }
0194 
0195 // -----------------------------------------------------------------------------
0196 //
0197 void SiStripConfigDb::addDeviceDescriptions(std::string partition, DeviceDescriptionsV& devs) {
0198   if (!deviceFactory(__func__)) {
0199     return;
0200   }
0201 
0202   if (partition.empty()) {
0203     stringstream ss;
0204     ss << "[SiStripConfigDb::" << __func__ << "]"
0205        << " Partition string is empty,"
0206        << " therefore cannot add device descriptions to local cache!";
0207     edm::LogWarning(mlConfigDb_) << ss.str();
0208     return;
0209   }
0210 
0211   if (devs.empty()) {
0212     stringstream ss;
0213     ss << "[SiStripConfigDb::" << __func__ << "]"
0214        << " Vector of device descriptions is empty,"
0215        << " therefore cannot add device descriptions to local cache!";
0216     edm::LogWarning(mlConfigDb_) << ss.str();
0217     return;
0218   }
0219 
0220   SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0221   SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0222   for (; iter != jter; ++iter) {
0223     if (partition == iter->second.partitionName()) {
0224       break;
0225     }
0226   }
0227   if (iter == dbParams_.partitions().end()) {
0228     stringstream ss;
0229     ss << "[SiStripConfigDb::" << __func__ << "]"
0230        << " Partition \"" << partition << "\" not found in partition list, "
0231        << " therefore cannot add device descriptions!";
0232     edm::LogWarning(mlConfigDb_) << ss.str();
0233     return;
0234   }
0235 
0236   DeviceDescriptionsRange range = devices_.find(partition);
0237   if (range == devices_.emptyRange()) {
0238     // Make local copy
0239     DeviceDescriptionsV tmp;
0240     FecFactory::vectorCopyI(tmp, devs, true);
0241 
0242     // Add to local cache
0243     devices_.loadNext(partition, tmp);
0244 
0245     // Some debug
0246     std::stringstream ss;
0247     ss << "[SiStripConfigDb::" << __func__ << "]"
0248        << " Added " << devs.size() << " device descriptions to local cache for partition \"" << partition << "\"."
0249        << " (Cache holds device descriptions for " << devices_.size() << " partitions.)";
0250     LogTrace(mlConfigDb_) << ss.str();
0251 
0252   } else {
0253     stringstream ss;
0254     ss << "[SiStripConfigDb::" << __func__ << "]"
0255        << " Partition \"" << partition << "\" already found in local cache, "
0256        << " therefore cannot add device descriptions!";
0257     edm::LogWarning(mlConfigDb_) << ss.str();
0258     return;
0259   }
0260 }
0261 
0262 // -----------------------------------------------------------------------------
0263 //
0264 void SiStripConfigDb::uploadDeviceDescriptions(std::string partition) {
0265   if (dbParams_.usingDbCache()) {
0266     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0267                                  << " Using database cache! No uploads allowed!";
0268     return;
0269   }
0270 
0271   if (!deviceFactory(__func__)) {
0272     return;
0273   }
0274 
0275   if (devices_.empty()) {
0276     stringstream ss;
0277     ss << "[SiStripConfigDb::" << __func__ << "]"
0278        << " Found no cached device descriptions, therefore no upload!";
0279     edm::LogWarning(mlConfigDb_) << ss.str();
0280     return;
0281   }
0282 
0283   try {
0284     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0285     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0286     for (; iter != jter; ++iter) {
0287       if (partition.empty() || partition == iter->second.partitionName()) {
0288         DeviceDescriptionsRange range = devices_.find(iter->second.partitionName());
0289         if (range != devices_.emptyRange()) {
0290           DeviceDescriptionsV devs(range.begin(), range.end());
0291 
0292           SiStripPartition::Versions fecVersion = iter->second.fecVersion();
0293           deviceFactory(__func__)->setFecDeviceDescriptions(devs,
0294                                                             iter->second.partitionName(),
0295                                                             &(fecVersion.first),
0296                                                             &(fecVersion.second),
0297                                                             true);  // new major version
0298 
0299           // Some debug
0300           std::stringstream ss;
0301           ss << "[SiStripConfigDb::" << __func__ << "]"
0302              << " Uploaded " << devs.size() << " device descriptions to database for partition \""
0303              << iter->second.partitionName() << "\".";
0304           LogTrace(mlConfigDb_) << ss.str();
0305 
0306         } else {
0307           stringstream ss;
0308           ss << "[SiStripConfigDb::" << __func__ << "]"
0309              << " Vector of device descriptions is empty for partition \"" << iter->second.partitionName()
0310              << "\", therefore aborting upload for this partition!";
0311           edm::LogWarning(mlConfigDb_) << ss.str();
0312           continue;
0313         }
0314 
0315       } else {
0316         //    stringstream ss;
0317         //    ss << "[SiStripConfigDb::" << __func__ << "]"
0318         //       << " Cannot find partition \"" << partition
0319         //       << "\" in cached partitions list: \""
0320         //       << dbParams_.partitionNames( dbParams_.partitionNames() )
0321         //       << "\", therefore aborting upload for this partition!";
0322         //    edm::LogWarning(mlConfigDb_) << ss.str();
0323       }
0324     }
0325 
0326   } catch (...) {
0327     handleException(__func__);
0328   }
0329 
0330   allowCalibUpload_ = true;
0331 }
0332 
0333 // -----------------------------------------------------------------------------
0334 //
0335 void SiStripConfigDb::clearDeviceDescriptions(std::string partition) {
0336   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
0337 
0338   if (devices_.empty()) {
0339     stringstream ss;
0340     ss << "[SiStripConfigDb::" << __func__ << "]"
0341        << " Found no cached device descriptions!";
0342     //edm::LogWarning(mlConfigDb_) << ss.str();
0343     return;
0344   }
0345 
0346   // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
0347   DeviceDescriptions temporary_cache;
0348   if (partition.empty()) {
0349     temporary_cache = DeviceDescriptions();
0350   } else {
0351     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0352     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0353     for (; iter != jter; ++iter) {
0354       if (partition != iter->second.partitionName()) {
0355         DeviceDescriptionsRange range = devices_.find(iter->second.partitionName());
0356         if (range != devices_.emptyRange()) {
0357           temporary_cache.loadNext(partition, DeviceDescriptionsV(range.begin(), range.end()));
0358         } else {
0359           //      stringstream ss;
0360           //      ss << "[SiStripConfigDb::" << __func__ << "]"
0361           //         << " Cannot find partition \"" << iter->second.partitionName()
0362           //         << "\" in local cache!";
0363           //      edm::LogWarning(mlConfigDb_) << ss.str();
0364         }
0365       }
0366     }
0367   }
0368 
0369   // Delete objects in local cache for specified partition (or all if not specified)
0370   DeviceDescriptionsRange devs;
0371   if (partition.empty()) {
0372     if (!devices_.empty()) {
0373       devs = DeviceDescriptionsRange(devices_.find(dbParams_.partitions().begin()->second.partitionName()).begin(),
0374                                      devices_.find((--(dbParams_.partitions().end()))->second.partitionName()).end());
0375     } else {
0376       devs = devices_.emptyRange();
0377     }
0378   } else {
0379     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0380     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0381     for (; iter != jter; ++iter) {
0382       if (partition == iter->second.partitionName()) {
0383         break;
0384       }
0385     }
0386     devs = devices_.find(iter->second.partitionName());
0387   }
0388 
0389   if (devs != devices_.emptyRange()) {
0390     DeviceDescriptionsV::const_iterator ifed = devs.begin();
0391     DeviceDescriptionsV::const_iterator jfed = devs.end();
0392     for (; ifed != jfed; ++ifed) {
0393       if (*ifed) {
0394         delete *ifed;
0395       }
0396     }
0397   } else {
0398     stringstream ss;
0399     ss << "[SiStripConfigDb::" << __func__ << "]";
0400     if (partition.empty()) {
0401       ss << " Found no device descriptions in local cache!";
0402     } else {
0403       ss << " Found no device descriptions in local cache for partition \"" << partition << "\"!";
0404     }
0405     edm::LogWarning(mlConfigDb_) << ss.str();
0406   }
0407 
0408   // Overwrite local cache with temporary cache
0409   devices_ = temporary_cache;
0410 }
0411 
0412 // -----------------------------------------------------------------------------
0413 //
0414 void SiStripConfigDb::printDeviceDescriptions(std::string partition) {
0415   std::stringstream ss;
0416   ss << "[SiStripConfigDb::" << __func__ << "]"
0417      << " Contents of DeviceDescriptions container:" << std::endl;
0418   ss << " Number of partitions: " << devices_.size() << std::endl;
0419 
0420   // Loop through partitions
0421   uint16_t cntr = 0;
0422   DeviceDescriptions::const_iterator idev = devices_.begin();
0423   DeviceDescriptions::const_iterator jdev = devices_.end();
0424   for (; idev != jdev; ++idev) {
0425     cntr++;
0426     if (partition.empty() || partition == idev->first) {
0427       ss << "  Partition number : " << cntr << " (out of " << devices_.size() << ")" << std::endl;
0428       ss << "  Partition name   : \"" << idev->first << "\"" << std::endl;
0429       ss << "  Num of devices   : " << idev->second.size() << std::endl;
0430 
0431       // Extract FEC crate, slot, etc
0432       std::map<uint32_t, vector<std::string> > devices;
0433       DeviceDescriptionsV::const_iterator iter = idev->second.begin();
0434       DeviceDescriptionsV::const_iterator jter = idev->second.end();
0435       for (; iter != jter; ++iter) {
0436         if (*iter) {
0437           DeviceAddress addr = deviceAddress(**iter);
0438           uint32_t key = SiStripFecKey(addr.fecCrate_, addr.fecSlot_, addr.fecRing_, 0, 0, 0, 0).key();
0439           std::stringstream data;
0440           data << (*iter)->getDeviceType() << "_"
0441                << SiStripFecKey(addr.fecCrate_,
0442                                 addr.fecSlot_,
0443                                 addr.fecRing_,
0444                                 addr.ccuAddr_,
0445                                 addr.ccuChan_,
0446                                 addr.lldChan_,
0447                                 addr.i2cAddr_)
0448                       .key();
0449           if (find(devices[key].begin(), devices[key].end(), data.str()) == devices[key].end()) {
0450             devices[key].push_back(data.str());
0451           }
0452         }
0453       }
0454 
0455       // Sort contents
0456       std::map<uint32_t, std::vector<std::string> > tmp;
0457       std::map<uint32_t, std::vector<std::string> >::const_iterator ii = devices.begin();
0458       std::map<uint32_t, std::vector<std::string> >::const_iterator jj = devices.end();
0459       for (; ii != jj; ++ii) {
0460         std::vector<std::string> temp = ii->second;
0461         std::sort(temp.begin(), temp.end());
0462         std::vector<std::string>::const_iterator iii = temp.begin();
0463         std::vector<std::string>::const_iterator jjj = temp.end();
0464         for (; iii != jjj; ++iii) {
0465           tmp[ii->first].push_back(*iii);
0466         }
0467       }
0468       devices.clear();
0469       devices = tmp;
0470 
0471       // Print FEC crate, slot, etc...
0472       std::map<uint32_t, std::vector<std::string> >::const_iterator idev = devices.begin();
0473       std::map<uint32_t, std::vector<std::string> >::const_iterator jdev = devices.end();
0474       for (; idev != jdev; ++idev) {
0475         SiStripFecKey key(idev->first);
0476         ss << "  Found " << std::setw(3) << idev->second.size() << " devices for FEC crate/slot/ring " << key.fecCrate()
0477            << "/" << key.fecSlot() << "/" << key.fecRing();
0478         //<< " (ccu/module/lld/i2c): ";
0479         //  if ( !idev->second.empty() ) {
0480         //    uint16_t first = idev->second.front();
0481         //    uint16_t last = idev->second.front();
0482         //    std::vector<std::string>::const_iterator chan = idev->second.begin();
0483         //    for ( ; chan != idev->second.end(); chan++ ) {
0484         //      if ( chan != idev->second.begin() ) {
0485         //        if ( *chan != last+1 ) {
0486         //      ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
0487         //      if ( chan != idev->second.end() ) { first = *(chan+1); }
0488         //        }
0489         //      }
0490         //      last = *chan;
0491         //    }
0492         //    if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
0493         ss << std::endl;
0494       }
0495     }
0496   }
0497 
0498   LogTrace(mlConfigDb_) << ss.str();
0499 }
0500 
0501 // -----------------------------------------------------------------------------
0502 //
0503 SiStripConfigDb::DeviceAddress SiStripConfigDb::deviceAddress(const deviceDescription& description) {
0504   deviceDescription& desc = const_cast<deviceDescription&>(description);
0505 
0506   DeviceAddress addr;
0507   try {
0508     addr.fecCrate_ = static_cast<uint16_t>(desc.getCrateId() + sistrip::FEC_CRATE_OFFSET);  //@@ temporary offset?
0509     addr.fecSlot_ = static_cast<uint16_t>(desc.getFecSlot());
0510     addr.fecRing_ = static_cast<uint16_t>(desc.getRingSlot() + sistrip::FEC_RING_OFFSET);  //@@ temporary offset?
0511     addr.ccuAddr_ = static_cast<uint16_t>(desc.getCcuAddress());
0512     addr.ccuChan_ = static_cast<uint16_t>(desc.getChannel());
0513     addr.lldChan_ = static_cast<uint16_t>(SiStripFecKey::lldChan(desc.getAddress()));
0514     addr.i2cAddr_ = static_cast<uint16_t>(desc.getAddress());
0515   } catch (...) {
0516     handleException(__func__);
0517   }
0518 
0519   return addr;
0520 }
0521 
0522 // -----------------------------------------------------------------------------
0523 //
0524 string SiStripConfigDb::deviceType(const enumDeviceType& device_type) const {
0525   if (device_type == PLL) {
0526     return "PLL";
0527   } else if (device_type == LASERDRIVER) {
0528     return "LLD";
0529   } else if (device_type == DOH) {
0530     return "DOH";
0531   } else if (device_type == APVMUX) {
0532     return "MUX";
0533   } else if (device_type == APV25) {
0534     return "APV";
0535   } else if (device_type == DCU) {
0536     return "DCU";
0537   } else if (device_type == GOH) {
0538     return "GOH";
0539   } else {
0540     return "UNKNOWN DEVICE!";
0541   }
0542 }