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 "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0004 #include "CondFormats/SiStripObjects/interface/FedChannelConnection.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 
0007 using namespace std;
0008 using namespace sistrip;
0009 
0010 // -----------------------------------------------------------------------------
0011 //
0012 SiStripConfigDb::FedDescriptionsRange SiStripConfigDb::getFedDescriptions(std::string partition) {
0013   // Check
0014   if ((!dbParams_.usingDbCache() && !deviceFactory(__func__)) ||
0015       (dbParams_.usingDbCache() && !databaseCache(__func__))) {
0016     return feds_.emptyRange();
0017   }
0018 
0019   try {
0020     if (!dbParams_.usingDbCache()) {
0021       SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0022       SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0023       for (; iter != jter; ++iter) {
0024         if (partition.empty() || partition == iter->second.partitionName()) {
0025           if (iter->second.partitionName() == SiStripPartition::defaultPartitionName_) {
0026             continue;
0027           }
0028 
0029           FedDescriptionsRange range = feds_.find(iter->second.partitionName());
0030           if (range == feds_.emptyRange()) {
0031             // Extract versions
0032             deviceFactory(__func__)->setUsingStrips(usingStrips_);
0033             int16_t major = iter->second.fedVersion().first;
0034             int16_t minor = iter->second.fedVersion().second;
0035             if (iter->second.fedVersion().first == 0 && iter->second.fedVersion().second == 0) {
0036               major = -1;  //@@ "current state" for fed factory!
0037               minor = -1;  //@@ "current state" for fed factory!
0038             }
0039 
0040             // Retrive FED descriptions
0041             FedDescriptionsV tmp1;
0042             tmp1 = *(deviceFactory(__func__)->getFed9UDescriptions(iter->second.partitionName(), major, minor));
0043 
0044             // Make local copy
0045             FedDescriptionsV tmp2;
0046             Fed9U::Fed9UDeviceFactory::vectorCopy(tmp2, tmp1);
0047 
0048             // Add to cache
0049             feds_.loadNext(iter->second.partitionName(), tmp2);
0050 
0051             // Some debug
0052             FedDescriptionsRange feds = feds_.find(iter->second.partitionName());
0053             std::stringstream ss;
0054             ss << "[SiStripConfigDb::" << __func__ << "]"
0055                << " Downloaded " << feds.size() << " FED descriptions to local cache for partition \""
0056                << iter->second.partitionName() << "\"" << std::endl;
0057             ss << "[SiStripConfigDb::" << __func__ << "]"
0058                << " Cache holds FED descriptions for " << feds_.size() << " partitions.";
0059             LogTrace(mlConfigDb_) << ss.str();
0060           }
0061         }
0062       }
0063 
0064     } else {  // Using database cache
0065 
0066       FedDescriptionsV* tmp1 = databaseCache(__func__)->getFed9UDescriptions();
0067 
0068       if (tmp1) {
0069         // Make local copy
0070         FedDescriptionsV tmp2;
0071         Fed9U::Fed9UDeviceFactory::vectorCopy(tmp2, *tmp1);
0072 
0073         // Add to cache
0074         feds_.loadNext(SiStripPartition::defaultPartitionName_, tmp2);
0075 
0076       } else {
0077         edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0078                                      << " NULL pointer to FED descriptions vector!";
0079       }
0080     }
0081 
0082   } catch (...) {
0083     handleException(__func__);
0084   }
0085 
0086   // Create range object
0087   uint16_t np = 0;
0088   uint16_t nc = 0;
0089   FedDescriptionsRange feds;
0090   if (!partition.empty()) {
0091     feds = feds_.find(partition);
0092     np = 1;
0093     nc = feds.size();
0094   } else {
0095     if (!feds_.empty()) {
0096       feds = FedDescriptionsRange(feds_.find(dbParams_.partitions().begin()->second.partitionName()).begin(),
0097                                   feds_.find((--(dbParams_.partitions().end()))->second.partitionName()).end());
0098     } else {
0099       feds = feds_.emptyRange();
0100     }
0101     np = feds_.size();
0102     nc = feds.size();
0103   }
0104 
0105   stringstream ss;
0106   ss << "[SiStripConfigDb::" << __func__ << "]"
0107      << " Found " << nc << " FED descriptions";
0108   if (!dbParams_.usingDb()) {
0109     ss << " in " << dbParams_.inputFedXmlFiles().size() << " 'fed.xml' file(s)";
0110   } else {
0111     if (!dbParams_.usingDbCache()) {
0112       ss << " in " << np << " database partition(s)";
0113     } else {
0114       ss << " from shared memory name '" << dbParams_.sharedMemory() << "'";
0115     }
0116   }
0117   if (feds_.empty()) {
0118     edm::LogWarning(mlConfigDb_) << ss.str();
0119   } else {
0120     LogTrace(mlConfigDb_) << ss.str();
0121   }
0122 
0123   return feds;
0124 }
0125 
0126 // -----------------------------------------------------------------------------
0127 //
0128 void SiStripConfigDb::addFedDescriptions(std::string partition, FedDescriptionsV& feds) {
0129   if (!deviceFactory(__func__)) {
0130     return;
0131   }
0132 
0133   if (partition.empty()) {
0134     stringstream ss;
0135     ss << "[SiStripConfigDb::" << __func__ << "]"
0136        << " Partition string is empty,"
0137        << " therefore cannot add FED descriptions to local cache!";
0138     edm::LogWarning(mlConfigDb_) << ss.str();
0139     return;
0140   }
0141 
0142   if (feds.empty()) {
0143     stringstream ss;
0144     ss << "[SiStripConfigDb::" << __func__ << "]"
0145        << " Vector of FED descriptions is empty,"
0146        << " therefore cannot add FED descriptions to local cache!";
0147     edm::LogWarning(mlConfigDb_) << ss.str();
0148     return;
0149   }
0150 
0151   SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0152   SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0153   for (; iter != jter; ++iter) {
0154     if (partition == iter->second.partitionName()) {
0155       break;
0156     }
0157   }
0158   if (iter == dbParams_.partitions().end()) {
0159     stringstream ss;
0160     ss << "[SiStripConfigDb::" << __func__ << "]"
0161        << " Partition \"" << partition << "\" not found in partition list, "
0162        << " therefore cannot add FED descriptions!";
0163     edm::LogWarning(mlConfigDb_) << ss.str();
0164     return;
0165   }
0166 
0167   FedDescriptionsRange range = feds_.find(partition);
0168   if (range == feds_.emptyRange()) {
0169     // Make local copy
0170     FedDescriptionsV tmp;
0171     Fed9U::Fed9UDeviceFactory::vectorCopy(tmp, feds);
0172 
0173     // Add to local cache
0174     feds_.loadNext(partition, tmp);
0175 
0176     // Some debug
0177     std::stringstream ss;
0178     ss << "[SiStripConfigDb::" << __func__ << "]"
0179        << " Added " << feds.size() << " FED descriptions to local cache for partition \""
0180        << iter->second.partitionName() << "\"" << std::endl;
0181     ss << "[SiStripConfigDb::" << __func__ << "]"
0182        << " Cache holds FED descriptions for " << feds_.size() << " partitions.";
0183     LogTrace(mlConfigDb_) << ss.str();
0184 
0185   } else {
0186     stringstream ss;
0187     ss << "[SiStripConfigDb::" << __func__ << "]"
0188        << " Partition \"" << partition << "\" already found in local cache, "
0189        << " therefore cannot add new FED descriptions!";
0190     edm::LogWarning(mlConfigDb_) << ss.str();
0191     return;
0192   }
0193 }
0194 
0195 // -----------------------------------------------------------------------------
0196 //
0197 void SiStripConfigDb::uploadFedDescriptions(std::string partition) {
0198   if (dbParams_.usingDbCache()) {
0199     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0200                                  << " Using database cache! No uploads allowed!";
0201     return;
0202   }
0203 
0204   if (!deviceFactory(__func__)) {
0205     return;
0206   }
0207 
0208   if (feds_.empty()) {
0209     stringstream ss;
0210     ss << "[SiStripConfigDb::" << __func__ << "]"
0211        << " Found no cached FED descriptions, therefore no upload!";
0212     edm::LogWarning(mlConfigDb_) << ss.str();
0213     return;
0214   }
0215 
0216   try {
0217     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0218     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0219     for (; iter != jter; ++iter) {
0220       if (partition.empty() || partition == iter->second.partitionName()) {
0221         FedDescriptionsRange range = feds_.find(iter->second.partitionName());
0222         if (range != feds_.emptyRange()) {
0223           FedDescriptionsV feds(range.begin(), range.end());
0224 
0225           SiStripPartition::Versions fedVersion = iter->second.fedVersion();
0226           deviceFactory(__func__)->setFed9UDescriptions(feds,
0227                                                         iter->second.partitionName(),
0228                                                         (uint16_t*)(&(fedVersion.first)),
0229                                                         (uint16_t*)(&(fedVersion.second)),
0230                                                         1);  // new major version
0231 
0232           // Some debug
0233           std::stringstream ss;
0234           ss << "[SiStripConfigDb::" << __func__ << "]"
0235              << " Uploaded " << feds.size() << " FED descriptions to database for partition \""
0236              << iter->second.partitionName() << "\"";
0237           LogTrace(mlConfigDb_) << ss.str();
0238 
0239         } else {
0240           stringstream ss;
0241           ss << "[SiStripConfigDb::" << __func__ << "]"
0242              << " Vector of FED descriptions is empty for partition \"" << iter->second.partitionName()
0243              << "\", therefore aborting upload for this partition!";
0244           edm::LogWarning(mlConfigDb_) << ss.str();
0245           continue;
0246         }
0247 
0248       } else {
0249         //    stringstream ss;
0250         //    ss << "[SiStripConfigDb::" << __func__ << "]"
0251         //       << " Cannot find partition \"" << partition
0252         //       << "\" in cached partitions list: \""
0253         //       << dbParams_.partitionNames( dbParams_.partitionNames() )
0254         //       << "\", therefore aborting upload for this partition!";
0255         //    edm::LogWarning(mlConfigDb_) << ss.str();
0256       }
0257     }
0258 
0259   } catch (...) {
0260     handleException(__func__);
0261   }
0262 
0263   allowCalibUpload_ = true;
0264 }
0265 
0266 // -----------------------------------------------------------------------------
0267 //
0268 void SiStripConfigDb::clearFedDescriptions(std::string partition) {
0269   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
0270 
0271   if (feds_.empty()) {
0272     stringstream ss;
0273     ss << "[SiStripConfigDb::" << __func__ << "]"
0274        << " Found no cached FED descriptions!";
0275     //edm::LogWarning(mlConfigDb_) << ss.str();
0276     return;
0277   }
0278 
0279   // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
0280   FedDescriptions temporary_cache;
0281   if (partition.empty()) {
0282     temporary_cache = FedDescriptions();
0283   } else {
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 != iter->second.partitionName()) {
0288         FedDescriptionsRange range = feds_.find(iter->second.partitionName());
0289         if (range != feds_.emptyRange()) {
0290           temporary_cache.loadNext(partition, FedDescriptionsV(range.begin(), range.end()));
0291         }
0292       } else {
0293         FedDescriptionsRange range = feds_.find(iter->second.partitionName());
0294         if (range != feds_.emptyRange()) {
0295           LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0296                                 << " Deleting FED descriptions for partition \"" << iter->second.partitionName()
0297                                 << "\" from local cache...";
0298         }
0299       }
0300     }
0301   }
0302 
0303   // Delete objects in local cache for specified partition (or all if not specified)
0304   FedDescriptionsRange feds;
0305   if (partition.empty()) {
0306     if (!feds_.empty()) {
0307       feds = FedDescriptionsRange(feds_.find(dbParams_.partitions().begin()->second.partitionName()).begin(),
0308                                   feds_.find((--(dbParams_.partitions().end()))->second.partitionName()).end());
0309     } else {
0310       feds = feds_.emptyRange();
0311     }
0312   } else {
0313     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0314     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0315     for (; iter != jter; ++iter) {
0316       if (partition == iter->second.partitionName()) {
0317         break;
0318       }
0319     }
0320     feds = feds_.find(iter->second.partitionName());
0321   }
0322 
0323   if (feds != feds_.emptyRange()) {
0324     FedDescriptionsV::const_iterator ifed = feds.begin();
0325     FedDescriptionsV::const_iterator jfed = feds.end();
0326     for (; ifed != jfed; ++ifed) {
0327       if (*ifed) {
0328         delete *ifed;
0329       }
0330     }
0331   } else {
0332     stringstream ss;
0333     ss << "[SiStripConfigDb::" << __func__ << "]";
0334     if (partition.empty()) {
0335       ss << " Found no FED descriptions in local cache!";
0336     } else {
0337       ss << " Found no FED descriptions in local cache for partition \"" << partition << "\"!";
0338     }
0339     edm::LogWarning(mlConfigDb_) << ss.str();
0340   }
0341 
0342   // Overwrite local cache with temporary cache
0343   feds_ = temporary_cache;
0344 }
0345 
0346 // -----------------------------------------------------------------------------
0347 //
0348 void SiStripConfigDb::printFedDescriptions(std::string partition) {
0349   std::stringstream ss;
0350   ss << "[SiStripConfigDb::" << __func__ << "]"
0351      << " Contents of FedDescriptions container:" << std::endl;
0352   ss << " Number of partitions: " << feds_.size() << std::endl;
0353 
0354   // Loop through partitions
0355   uint16_t cntr = 0;
0356   FedDescriptions::const_iterator iconn = feds_.begin();
0357   FedDescriptions::const_iterator jconn = feds_.end();
0358   for (; iconn != jconn; ++iconn) {
0359     cntr++;
0360     if (partition.empty() || partition == iconn->first) {
0361       ss << "  Partition number : " << cntr << " (out of " << feds_.size() << ")" << std::endl;
0362       ss << "  Partition name   : \"" << iconn->first << "\"" << std::endl;
0363       ss << "  Num of FED ids   : " << iconn->second.size() << std::endl;
0364 
0365       // Extract FED crates and ids
0366       std::map<uint16_t, vector<uint16_t> > feds;
0367       FedDescriptionsV::const_iterator iter = iconn->second.begin();
0368       FedDescriptionsV::const_iterator jter = iconn->second.end();
0369       for (; iter != jter; ++iter) {
0370         if (*iter) {
0371           uint16_t key = (*iter)->getCrateNumber();
0372           uint16_t data = (*iter)->getFedId();
0373           if (find(feds[key].begin(), feds[key].end(), data) == feds[key].end()) {
0374             feds[key].push_back(data);
0375           }
0376         }
0377       }
0378 
0379       // Sort contents
0380       std::map<uint16_t, std::vector<uint16_t> > tmp;
0381       std::map<uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
0382       std::map<uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
0383       for (; ii != jj; ++ii) {
0384         std::vector<uint16_t> temp = ii->second;
0385         std::sort(temp.begin(), temp.end());
0386         std::vector<uint16_t>::const_iterator iii = temp.begin();
0387         std::vector<uint16_t>::const_iterator jjj = temp.end();
0388         for (; iii != jjj; ++iii) {
0389           tmp[ii->first].push_back(*iii);
0390         }
0391       }
0392       feds.clear();
0393       feds = tmp;
0394 
0395       // Print FED crates and ids
0396       std::map<uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
0397       std::map<uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
0398       for (; ifed != jfed; ++ifed) {
0399         ss << "  Found " << std::setw(2) << ifed->second.size() << " FED ids for crate number " << std::setw(2)
0400            << ifed->first << " : ";
0401         if (!ifed->second.empty()) {
0402           uint16_t first = ifed->second.front();
0403           uint16_t last = ifed->second.front();
0404           std::vector<uint16_t>::const_iterator icrate = ifed->second.begin();
0405           std::vector<uint16_t>::const_iterator jcrate = ifed->second.end();
0406           for (; icrate != jcrate; ++icrate) {
0407             if (icrate != ifed->second.begin()) {
0408               if (*icrate != last + 1) {
0409                 ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
0410                 if (icrate != ifed->second.end()) {
0411                   first = *(icrate + 1);
0412                 }
0413               }
0414             }
0415             last = *icrate;
0416           }
0417           if (first != last) {
0418             ss << std::setw(2) << first << "->" << std::setw(2) << last;
0419           }
0420           ss << std::endl;
0421         }
0422       }
0423     }
0424   }
0425 
0426   LogTrace(mlConfigDb_) << ss.str();
0427 }
0428 
0429 // -----------------------------------------------------------------------------
0430 /** */
0431 SiStripConfigDb::FedIdsRange SiStripConfigDb::getFedIds(std::string partition) {
0432   fedIds_.clear();
0433 
0434   if ((!dbParams_.usingDbCache() && !deviceFactory(__func__)) ||
0435       (dbParams_.usingDbCache() && !databaseCache(__func__))) {
0436     return FedIdsRange(fedIds_.end(), fedIds_.end());
0437   }
0438 
0439   try {
0440     // Inhibit download of strip info
0441     bool using_strips = usingStrips_;
0442     if (factory_) {
0443       factory_->setUsingStrips(false);
0444     }
0445     FedDescriptionsRange feds = getFedDescriptions(partition);
0446     if (factory_) {
0447       factory_->setUsingStrips(using_strips);
0448     }
0449 
0450     if (!feds.empty()) {
0451       FedDescriptionsV::const_iterator ifed = feds.begin();
0452       FedDescriptionsV::const_iterator jfed = feds.end();
0453       for (; ifed != jfed; ++ifed) {
0454         if (*ifed) {
0455           fedIds_.push_back((*ifed)->getFedId());
0456         } else {
0457           edm::LogError(mlCabling_) << "[SiStripConfigDb::" << __func__ << "]"
0458                                     << " NULL pointer to FedDescription!";
0459           continue;
0460         }
0461       }
0462     }
0463 
0464   } catch (...) {
0465     handleException(__func__);
0466   }
0467 
0468   if (fedIds_.empty()) {
0469     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0470                                  << " No FED ids found!";
0471   }
0472 
0473   return FedIdsRange(fedIds_.begin(), fedIds_.end());
0474 }