Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:15

0001 
0002 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 
0005 using namespace std;
0006 using namespace sistrip;
0007 
0008 // -----------------------------------------------------------------------------
0009 //
0010 SiStripConfigDb::DcuDetIdsRange SiStripConfigDb::getDcuDetIds(std::string partition) {
0011   // Check
0012   if ((!dbParams_.usingDbCache() && !deviceFactory(__func__)) ||
0013       (dbParams_.usingDbCache() && !databaseCache(__func__))) {
0014     return dcuDetIds_.emptyRange();
0015   }
0016 
0017   try {
0018     if (!dbParams_.usingDbCache()) {
0019       SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0020       SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0021       for (; iter != jter; ++iter) {
0022         if (partition.empty() || partition == iter->second.partitionName()) {
0023           if (iter->second.partitionName() == SiStripPartition::defaultPartitionName_) {
0024             continue;
0025           }
0026 
0027           DcuDetIdsRange range = dcuDetIds_.find(iter->second.partitionName());
0028           if (range == dcuDetIds_.emptyRange()) {
0029             deviceFactory(__func__)->addDetIdPartition(
0030                 iter->second.partitionName(), iter->second.dcuVersion().first, iter->second.dcuVersion().second);
0031 
0032             // Retrieve DCU-DetId map
0033             DcuDetIdMap src = deviceFactory(__func__)->getInfos();
0034 
0035             // Make local copy
0036             DcuDetIdsV dst;
0037             clone(src, dst);
0038 
0039             // Add to cache
0040             dcuDetIds_.loadNext(iter->second.partitionName(), dst);
0041           }
0042         }
0043       }
0044 
0045     } else {  // Using database cache
0046 
0047       // Retrieve DCU-DetId map
0048       DcuDetIdMap* src = databaseCache(__func__)->getInfos();
0049 
0050       if (src) {
0051         // Make local copy
0052         DcuDetIdsV dst;
0053         clone(*src, dst);
0054 
0055         // Add to cache
0056         dcuDetIds_.loadNext(SiStripPartition::defaultPartitionName_, dst);
0057 
0058       } else {
0059         edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0060                                      << " NULL pointer to Dcu-DetId map!";
0061       }
0062     }
0063 
0064   } catch (...) {
0065     handleException(__func__);
0066   }
0067 
0068   // Create range object
0069   uint16_t np = 0;
0070   uint16_t nc = 0;
0071   DcuDetIdsRange range;
0072   if (!partition.empty()) {
0073     range = dcuDetIds_.find(partition);
0074     np = 1;
0075     nc = range.size();
0076   } else {
0077     if (!dcuDetIds_.empty()) {
0078       range = DcuDetIdsRange(dcuDetIds_.find(dbParams_.partitions().begin()->second.partitionName()).begin(),
0079                              dcuDetIds_.find((--(dbParams_.partitions().end()))->second.partitionName()).end());
0080     } else {
0081       range = dcuDetIds_.emptyRange();
0082     }
0083     np = dcuDetIds_.size();
0084     nc = range.size();
0085   }
0086 
0087   stringstream ss;
0088   ss << "[SiStripConfigDb::" << __func__ << "]"
0089      << " Found " << nc << " entries in DCU-DetId map";
0090   if (!dbParams_.usingDb()) {
0091     ss << " in " << dbParams_.inputDcuInfoXmlFiles().size() << " 'module.xml' file(s)";
0092   } else {
0093     if (!dbParams_.usingDbCache()) {
0094       ss << " in " << np << " database partition(s)";
0095     } else {
0096       ss << " from shared memory name '" << dbParams_.sharedMemory() << "'";
0097     }
0098   }
0099   if (dcuDetIds_.empty()) {
0100     edm::LogWarning(mlConfigDb_) << ss.str();
0101   } else {
0102     LogTrace(mlConfigDb_) << ss.str();
0103   }
0104 
0105   return range;
0106 }
0107 
0108 // -----------------------------------------------------------------------------
0109 //
0110 void SiStripConfigDb::addDcuDetIds(std::string partition, DcuDetIdsV& dcus) {
0111   stringstream ss;
0112   ss << "[SiStripConfigDb::" << __func__ << "]"
0113      << " Cannot add to local cache! This functionality not allowed!";
0114   edm::LogWarning(mlConfigDb_) << ss.str();
0115 
0116   //   if ( !deviceFactory(__func__) ) { return; }
0117 
0118   //   if ( partition.empty() ) {
0119   //     stringstream ss;
0120   //     ss << "[SiStripConfigDb::" << __func__ << "]"
0121   //        << " Partition string is empty,"
0122   //        << " therefore cannot add DCU-DetId map to local cache!";
0123   //     edm::LogWarning(mlConfigDb_) << ss.str();
0124   //     return;
0125   //   }
0126 
0127   //   if ( dcus.empty() ) {
0128   //     stringstream ss;
0129   //     ss << "[SiStripConfigDb::" << __func__ << "]"
0130   //        << " Vector of DCU-DetId map is empty,"
0131   //        << " therefore cannot add DCU-DetId map to local cache!";
0132   //     edm::LogWarning(mlConfigDb_) << ss.str();
0133   //     return;
0134   //   }
0135 
0136   //   SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0137   //   SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0138   //   for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
0139   //   if ( iter == dbParams_.partitions().end() ) {
0140   //     stringstream ss;
0141   //     ss << "[SiStripConfigDb::" << __func__ << "]"
0142   //        << " Partition \"" << partition
0143   //        << "\" not found in partition list, "
0144   //        << " therefore cannot add DCU-DetId map!";
0145   //     edm::LogWarning(mlConfigDb_) << ss.str();
0146   //     return;
0147   //   }
0148 
0149   //   DcuDetIdsRange range = dcuDetIds_.find( partition );
0150   //   if ( range == dcuDetIds_.emptyRange() ) {
0151 
0152   //     // Make local copy
0153   //     DcuDetIdsV dst;
0154   //     clone( dcus, dst );
0155 
0156   //     // Add to local cache
0157   //     dcuDetIds_.loadNext( partition, dst );
0158 
0159   //     // Some debug
0160   //     std::stringstream ss;
0161   //     ss << "[SiStripConfigDb::" << __func__ << "]"
0162   //        << " Added " << dst.size()
0163   //        << " DCU-DetId map to local cache for partition \""
0164   //        << partition << "\"" << std::endl;
0165   //     ss << "[SiStripConfigDb::" << __func__ << "]"
0166   //        << " Cache holds DCU-DetId map for "
0167   //        << dcuDetIds_.size() << " partitions.";
0168   //     LogTrace(mlConfigDb_) << ss.str();
0169 
0170   //   } else {
0171   //     stringstream ss;
0172   //     ss << "[SiStripConfigDb::" << __func__ << "]"
0173   //        << " Partition \"" << partition
0174   //        << "\" already found in local cache, "
0175   //        << " therefore cannot add new DCU-DetId map!";
0176   //     edm::LogWarning(mlConfigDb_) << ss.str();
0177   //     return;
0178   //   }
0179 }
0180 
0181 // -----------------------------------------------------------------------------
0182 //
0183 void SiStripConfigDb::uploadDcuDetIds(std::string partition) {
0184   stringstream ss;
0185   ss << "[SiStripConfigDb::" << __func__ << "]"
0186      << " Cannot upload to database! This functionality not allowed!";
0187   edm::LogWarning(mlConfigDb_) << ss.str();
0188 
0189   /*
0190     addAllDetId => all detids
0191     addAllDetId => to download (up to you)
0192     change in the detids
0193     setTkDcuInfo
0194     getCurrentStates
0195     setCurrentState
0196     addDetIpartiton
0197     AddAllDetId
0198   */
0199 
0200   //   if ( dbParams_.usingDbCache() ) {
0201   //     edm::LogWarning(mlConfigDb_)
0202   //       << "[SiStripConfigDb::" << __func__ << "]"
0203   //       << " Using database cache! No uploads allowed!";
0204   //     return;
0205   //   }
0206 
0207   //   if ( !deviceFactory(__func__) ) { return; }
0208 
0209   //   if ( dcuDetIds_.empty() ) {
0210   //     stringstream ss;
0211   //     ss << "[SiStripConfigDb::" << __func__ << "]"
0212   //        << " Found no cached DCU-DetId map, therefore no upload!";
0213   //     edm::LogWarning(mlConfigDb_) << ss.str();
0214   //     return;
0215   //   }
0216 
0217   //   try {
0218 
0219   //     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0220   //     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0221   //     for ( ; iter != jter; ++iter ) {
0222 
0223   //       if ( partition == "" || partition == iter->second.partitionName() ) {
0224 
0225   //    DcuDetIdsRange range = dcuDetIds_.find( iter->second.partitionName() );
0226   //    if ( range != dcuDetIds_.emptyRange() ) {
0227 
0228   //      // Extract
0229   //      DcuDetIdMap dst;
0230   //      clone( DcuDetIdsV( range.begin(), range.end() ), dst );
0231   //      deviceFactory(__func__)->setTkDcuInfo( dst );
0232   //      getcurrentstate
0233   //      deviceFactory(__func__)->addAllDetId();
0234 
0235   //      // Some debug
0236   //      std::stringstream ss;
0237   //      ss << "[SiStripConfigDb::" << __func__ << "]"
0238   //         << " Uploaded " << dst.size()
0239   //         << " DCU-DetId map to DB/xml for partition \""
0240   //         << iter->second.partitionName() << "\".";
0241   //      LogTrace(mlConfigDb_) << ss.str();
0242 
0243   //    } else {
0244   //      stringstream ss;
0245   //      ss << "[SiStripConfigDb::" << __func__ << "]"
0246   //         << " Vector of DCU-DetId map is empty for partition \""
0247   //         << iter->second.partitionName()
0248   //         << "\", therefore aborting upload for this partition!";
0249   //      edm::LogWarning(mlConfigDb_) << ss.str();
0250   //      continue;
0251   //    }
0252 
0253   //       } else {
0254   //    //    stringstream ss;
0255   //    //    ss << "[SiStripConfigDb::" << __func__ << "]"
0256   //    //       << " Cannot find partition \"" << partition
0257   //    //       << "\" in cached partitions list: \""
0258   //    //       << dbParams_.partitionNames( dbParams_.partitionNames() )
0259   //    //       << "\", therefore aborting upload for this partition!";
0260   //    //    edm::LogWarning(mlConfigDb_) << ss.str();
0261   //       }
0262 
0263   //     }
0264 
0265   //   } catch (... ) { handleException( __func__, "Problems updating objects in TkDcuInfoFactory!" ); }
0266 }
0267 
0268 // -----------------------------------------------------------------------------
0269 //
0270 void SiStripConfigDb::clearDcuDetIds(std::string partition) {
0271   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
0272 
0273   if (dcuDetIds_.empty()) {
0274     stringstream ss;
0275     ss << "[SiStripConfigDb::" << __func__ << "]"
0276        << " Found no cached DCU-DetId map!";
0277     //edm::LogWarning(mlConfigDb_) << ss.str();
0278     return;
0279   }
0280 
0281   // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
0282   DcuDetIds temporary_cache;
0283   if (partition.empty()) {
0284     temporary_cache = DcuDetIds();
0285   } else {
0286     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0287     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0288     for (; iter != jter; ++iter) {
0289       if (partition != iter->second.partitionName()) {
0290         DcuDetIdsRange range = dcuDetIds_.find(iter->second.partitionName());
0291         if (range != dcuDetIds_.emptyRange()) {
0292           temporary_cache.loadNext(partition, DcuDetIdsV(range.begin(), range.end()));
0293         } else {
0294           //      stringstream ss;
0295           //      ss << "[SiStripConfigDb::" << __func__ << "]"
0296           //         << " Cannot find partition \"" << iter->second.partitionName()
0297           //         << "\" in local cache!";
0298           //      edm::LogWarning(mlConfigDb_) << ss.str();
0299         }
0300       }
0301     }
0302   }
0303 
0304   // Delete objects in local cache for specified partition (or all if not specified)
0305   DcuDetIdsRange dcus;
0306   if (partition.empty()) {
0307     if (!dcuDetIds_.empty()) {
0308       dcus = DcuDetIdsRange(dcuDetIds_.find(dbParams_.partitions().begin()->second.partitionName()).begin(),
0309                             dcuDetIds_.find((--(dbParams_.partitions().end()))->second.partitionName()).end());
0310     } else {
0311       dcus = dcuDetIds_.emptyRange();
0312     }
0313   } else {
0314     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0315     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0316     for (; iter != jter; ++iter) {
0317       if (partition == iter->second.partitionName()) {
0318         break;
0319       }
0320     }
0321     dcus = dcuDetIds_.find(iter->second.partitionName());
0322   }
0323 
0324   if (dcus != dcuDetIds_.emptyRange()) {
0325     DcuDetIdsV::const_iterator ifed = dcus.begin();
0326     DcuDetIdsV::const_iterator jfed = dcus.end();
0327     for (; ifed != jfed; ++ifed) {
0328       if (ifed->second) {
0329         delete ifed->second;
0330       }
0331     }
0332   } else {
0333     stringstream ss;
0334     ss << "[SiStripConfigDb::" << __func__ << "]";
0335     if (partition.empty()) {
0336       ss << " Found no DCU-DetId map in local cache!";
0337     } else {
0338       ss << " Found no DCU-DetId map in local cache for partition \"" << partition << "\"!";
0339     }
0340     edm::LogWarning(mlConfigDb_) << ss.str();
0341   }
0342 
0343   // Overwrite local cache with temporary cache
0344   dcuDetIds_ = temporary_cache;
0345 }
0346 
0347 // -----------------------------------------------------------------------------
0348 //
0349 void SiStripConfigDb::printDcuDetIds(std::string partition) {
0350   std::stringstream ss;
0351   ss << "[SiStripConfigDb::" << __func__ << "]"
0352      << " Contents of DcuDetIds container:" << std::endl;
0353   ss << " Number of partitions: " << dcuDetIds_.size() << std::endl;
0354 
0355   // Loop through partitions
0356   uint16_t cntr = 0;
0357   DcuDetIds::const_iterator idcu = dcuDetIds_.begin();
0358   DcuDetIds::const_iterator jdcu = dcuDetIds_.end();
0359   for (; idcu != jdcu; ++idcu) {
0360     cntr++;
0361     if (partition.empty() || partition == idcu->first) {
0362       ss << "  Partition number      : " << cntr << " (out of " << dcuDetIds_.size() << ")" << std::endl;
0363       ss << "  Partition name        : \"" << idcu->first << "\"" << std::endl;
0364       ss << "  Size of DCU-DetId map : " << idcu->second.size() << std::endl;
0365     }
0366   }
0367 
0368   LogTrace(mlConfigDb_) << ss.str();
0369 }
0370 
0371 // -----------------------------------------------------------------------------
0372 //
0373 void SiStripConfigDb::clone(const DcuDetIdMap& input, DcuDetIdsV& output) const {
0374   output.clear();
0375   DcuDetIdMap::const_iterator ii = input.begin();
0376   DcuDetIdMap::const_iterator jj = input.end();
0377   for (; ii != jj; ++ii) {
0378     if (ii->second) {
0379       output.push_back(std::make_pair(ii->first, new TkDcuInfo(*(ii->second))));
0380     }
0381   }
0382 }
0383 
0384 // -----------------------------------------------------------------------------
0385 //
0386 void SiStripConfigDb::clone(const DcuDetIdsV& input, DcuDetIdMap& output) const {
0387   output.clear();
0388   DcuDetIdsV::const_iterator ii = input.begin();
0389   DcuDetIdsV::const_iterator jj = input.end();
0390   for (; ii != jj; ++ii) {
0391     if (ii->second) {
0392       output[ii->first] = new TkDcuInfo(*(ii->second));
0393     }
0394   }
0395 }
0396 
0397 // -----------------------------------------------------------------------------
0398 //
0399 void SiStripConfigDb::clone(const DcuDetIdsV& input, DcuDetIdsV& output) const {
0400   output.clear();
0401   DcuDetIdsV::const_iterator ii = input.begin();
0402   DcuDetIdsV::const_iterator jj = input.end();
0403   for (; ii != jj; ++ii) {
0404     if (ii->second) {
0405       output.push_back(std::make_pair(ii->first, new TkDcuInfo(*(ii->second))));
0406     }
0407   }
0408 }
0409 
0410 // -----------------------------------------------------------------------------
0411 //
0412 SiStripConfigDb::DcuDetIdsV::const_iterator SiStripConfigDb::findDcuDetId(DcuDetIdsV::const_iterator begin,
0413                                                                           DcuDetIdsV::const_iterator end,
0414                                                                           uint32_t dcu_id) {
0415   DcuDetIdsV::const_iterator iter = begin;
0416   DcuDetIdsV::const_iterator jter = end;
0417   for (; iter != jter; ++iter) {
0418     if (iter->second && iter->second->getDcuHardId() == dcu_id) {
0419       return iter;
0420     }
0421   }
0422   return end;
0423 }
0424 
0425 // -----------------------------------------------------------------------------
0426 //
0427 SiStripConfigDb::DcuDetIdsV::iterator SiStripConfigDb::findDcuDetId(DcuDetIdsV::iterator begin,
0428                                                                     DcuDetIdsV::iterator end,
0429                                                                     uint32_t dcu_id) {
0430   DcuDetIdsV::iterator iter = begin;
0431   DcuDetIdsV::iterator jter = end;
0432   for (; iter != jter; ++iter) {
0433     if (iter->second && iter->second->getDcuHardId() == dcu_id) {
0434       return iter;
0435     }
0436   }
0437   return end;
0438 }