Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include <ostream>
0005 
0006 using namespace std;
0007 using namespace sistrip;
0008 
0009 // -----------------------------------------------------------------------------
0010 //
0011 SiStripConfigDb::FedConnectionsRange SiStripConfigDb::getFedConnections(std::string partition) {
0012   // Check
0013   if ((!dbParams_.usingDbCache() && !deviceFactory(__func__)) ||
0014       (dbParams_.usingDbCache() && !databaseCache(__func__))) {
0015     return connections_.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           FedConnectionsRange range = connections_.find(iter->second.partitionName());
0029           if (range == connections_.emptyRange()) {
0030             FedConnectionsV tmp2;
0031 
0032             // Retrieve connections
0033             FedConnectionsV tmp1;
0034             deviceFactory(__func__)->getConnectionDescriptions(iter->second.partitionName(),
0035                                                                tmp1,
0036                                                                iter->second.cabVersion().first,
0037                                                                iter->second.cabVersion().second,
0038                                                                //#ifdef USING_DATABASE_MASKING
0039                                                                iter->second.maskVersion().first,
0040                                                                iter->second.maskVersion().second,
0041                                                                //#endif
0042                                                                false);  //@@ do not get DISABLED connections
0043 
0044             // Make local copy
0045             ConnectionFactory::vectorCopyI(tmp2, tmp1, true);
0046 
0047             // Add to cache
0048             connections_.loadNext(iter->second.partitionName(), tmp2);
0049 
0050             // Some debug
0051             FedConnectionsRange conns = connections_.find(iter->second.partitionName());
0052             std::stringstream ss;
0053             ss << "[SiStripConfigDb::" << __func__ << "]"
0054                << " Downloaded " << conns.size() << " FED connections to local cache for partition \""
0055                << iter->second.partitionName() << "\"" << std::endl;
0056             ss << "[SiStripConfigDb::" << __func__ << "]"
0057                << " Cache holds FED connections for " << connections_.size() << " partitions.";
0058             LogTrace(mlConfigDb_) << ss.str();
0059           }
0060         }
0061       }
0062 
0063     } else {  // Use database cache
0064 
0065       FedConnectionsV* tmp1 = databaseCache(__func__)->getConnections();
0066 
0067       if (tmp1) {
0068         // Make local copy
0069         FedConnectionsV tmp2;
0070         ConnectionFactory::vectorCopyI(tmp2, *tmp1, true);
0071 
0072         // Add to cache
0073         connections_.loadNext(SiStripPartition::defaultPartitionName_, tmp2);
0074 
0075       } else {
0076         edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0077                                      << " NULL pointer to FedConnections vector!";
0078       }
0079     }
0080 
0081   } catch (...) {
0082     handleException(__func__);
0083   }
0084 
0085   // Create range object
0086   uint16_t np = 0;
0087   uint16_t nc = 0;
0088   FedConnectionsRange conns;
0089   if (!partition.empty()) {
0090     conns = connections_.find(partition);
0091     np = 1;
0092     nc = conns.size();
0093   } else {
0094     if (!connections_.empty()) {
0095       conns = FedConnectionsRange(connections_.find(dbParams_.partitions().begin()->second.partitionName()).begin(),
0096                                   connections_.find((--(dbParams_.partitions().end()))->second.partitionName()).end());
0097     } else {
0098       conns = connections_.emptyRange();
0099     }
0100     np = connections_.size();
0101     nc = conns.size();
0102   }
0103 
0104   stringstream ss;
0105   ss << "[SiStripConfigDb::" << __func__ << "]"
0106      << " Found " << nc << " FED connections";
0107   if (!dbParams_.usingDb()) {
0108     ss << " in " << dbParams_.inputModuleXmlFiles().size() << " 'module.xml' file(s)";
0109   } else {
0110     if (!dbParams_.usingDbCache()) {
0111       ss << " in " << np << " database partition(s)";
0112     } else {
0113       ss << " from shared memory name '" << dbParams_.sharedMemory() << "'";
0114     }
0115   }
0116   if (connections_.empty()) {
0117     edm::LogWarning(mlConfigDb_) << ss.str();
0118   } else {
0119     LogTrace(mlConfigDb_) << ss.str();
0120   }
0121 
0122   return conns;
0123 }
0124 
0125 // -----------------------------------------------------------------------------
0126 //
0127 void SiStripConfigDb::addFedConnections(std::string partition, FedConnectionsV& conns) {
0128   if (!deviceFactory(__func__)) {
0129     return;
0130   }
0131 
0132   if (partition.empty()) {
0133     stringstream ss;
0134     ss << "[SiStripConfigDb::" << __func__ << "]"
0135        << " Partition string is empty,"
0136        << " therefore cannot add FED connections to local cache!";
0137     edm::LogWarning(mlConfigDb_) << ss.str();
0138     return;
0139   }
0140 
0141   if (conns.empty()) {
0142     stringstream ss;
0143     ss << "[SiStripConfigDb::" << __func__ << "]"
0144        << " Vector of FED connections is empty,"
0145        << " therefore cannot add FED connections to local cache!";
0146     edm::LogWarning(mlConfigDb_) << ss.str();
0147     return;
0148   }
0149 
0150   SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0151   SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0152   for (; iter != jter; ++iter) {
0153     if (partition == iter->second.partitionName()) {
0154       break;
0155     }
0156   }
0157   if (iter == dbParams_.partitions().end()) {
0158     stringstream ss;
0159     ss << "[SiStripConfigDb::" << __func__ << "]"
0160        << " Partition \"" << partition << "\" not found in partition list, "
0161        << " therefore cannot add FED connections!";
0162     edm::LogWarning(mlConfigDb_) << ss.str();
0163     return;
0164   }
0165 
0166   FedConnectionsRange range = connections_.find(partition);
0167   if (range == connections_.emptyRange()) {
0168     // Make local copy
0169     FedConnectionsV tmp;
0170     ConnectionFactory::vectorCopyI(tmp, conns, true);
0171 
0172     // Add to local cache
0173     connections_.loadNext(partition, tmp);
0174 
0175     // Some debug
0176     std::stringstream ss;
0177     ss << "[SiStripConfigDb::" << __func__ << "]"
0178        << " Added " << conns.size() << " FED connections to local cache for partition \"" << partition << "\""
0179        << std::endl;
0180     ss << "[SiStripConfigDb::" << __func__ << "]"
0181        << " Cache holds FED connections for " << connections_.size() << " partitions.";
0182     LogTrace(mlConfigDb_) << ss.str();
0183 
0184   } else {
0185     stringstream ss;
0186     ss << "[SiStripConfigDb::" << __func__ << "]"
0187        << " Partition \"" << partition << "\" already found in local cache, "
0188        << " therefore cannot add new FED connections!";
0189     edm::LogWarning(mlConfigDb_) << ss.str();
0190     return;
0191   }
0192 }
0193 
0194 // -----------------------------------------------------------------------------
0195 //
0196 void SiStripConfigDb::uploadFedConnections(std::string partition) {
0197   if (dbParams_.usingDbCache()) {
0198     edm::LogWarning(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]"
0199                                  << " Using database cache! No uploads allowed!";
0200     return;
0201   }
0202 
0203   if (!deviceFactory(__func__)) {
0204     return;
0205   }
0206 
0207   if (connections_.empty()) {
0208     stringstream ss;
0209     ss << "[SiStripConfigDb::" << __func__ << "]"
0210        << " Found no cached FED connections, therefore no upload!";
0211     edm::LogWarning(mlConfigDb_) << ss.str();
0212     return;
0213   }
0214 
0215   if (dbParams_.usingDb()) {
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           FedConnectionsRange range = connections_.find(iter->second.partitionName());
0222           if (range != connections_.emptyRange()) {
0223             FedConnectionsV conns(range.begin(), range.end());
0224 
0225             SiStripPartition::Versions cabVersion = iter->second.cabVersion();
0226             deviceFactory(__func__)->setConnectionDescriptions(conns,
0227                                                                iter->second.partitionName(),
0228                                                                &(cabVersion.first),
0229                                                                &(cabVersion.second),
0230                                                                true);  // new major version
0231 
0232             // Some debug
0233             std::stringstream ss;
0234             ss << "[SiStripConfigDb::" << __func__ << "]"
0235                << " Uploaded " << conns.size() << " FED connections 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 connections 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 
0264   allowCalibUpload_ = true;
0265 }
0266 
0267 // -----------------------------------------------------------------------------
0268 //
0269 void SiStripConfigDb::clearFedConnections(std::string partition) {
0270   LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
0271 
0272   if (connections_.empty()) {
0273     stringstream ss;
0274     ss << "[SiStripConfigDb::" << __func__ << "]"
0275        << " Found no cached FED connections!";
0276     //edm::LogWarning(mlConfigDb_) << ss.str();
0277     return;
0278   }
0279 
0280   // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
0281   FedConnections temporary_cache;
0282   if (partition.empty()) {
0283     temporary_cache = FedConnections();
0284   } else {
0285     SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
0286     SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
0287     for (; iter != jter; ++iter) {
0288       if (partition != iter->second.partitionName()) {
0289         FedConnectionsRange range = connections_.find(iter->second.partitionName());
0290         if (range != connections_.emptyRange()) {
0291           temporary_cache.loadNext(partition, FedConnectionsV(range.begin(), range.end()));
0292         } else {
0293           //      stringstream ss;
0294           //      ss << "[SiStripConfigDb::" << __func__ << "]"
0295           //         << " Cannot find partition \"" << iter->second.partitionName()
0296           //         << "\" in local cache!";
0297           //      edm::LogWarning(mlConfigDb_) << ss.str();
0298         }
0299       }
0300     }
0301   }
0302 
0303   // Delete objects in local cache for specified partition (or all if not specified)
0304   FedConnectionsRange conns;
0305   if (partition.empty()) {
0306     if (!connections_.empty()) {
0307       conns = FedConnectionsRange(connections_.find(dbParams_.partitions().begin()->second.partitionName()).begin(),
0308                                   connections_.find((--(dbParams_.partitions().end()))->second.partitionName()).end());
0309     } else {
0310       conns = connections_.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     conns = connections_.find(iter->second.partitionName());
0321   }
0322 
0323   if (conns != connections_.emptyRange()) {
0324     FedConnectionsV::const_iterator ifed = conns.begin();
0325     FedConnectionsV::const_iterator jfed = conns.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 connections in local cache!";
0336     } else {
0337       ss << " Found no FED connections in local cache for partition \"" << partition << "\"!";
0338     }
0339     edm::LogWarning(mlConfigDb_) << ss.str();
0340   }
0341 
0342   // Overwrite local cache with temporary cache
0343   connections_ = temporary_cache;
0344 }
0345 
0346 // -----------------------------------------------------------------------------
0347 //
0348 void SiStripConfigDb::printFedConnections(std::string partition) {
0349   std::stringstream ss;
0350   ss << "[SiStripConfigDb::" << __func__ << "]"
0351      << " Contents of FedConnections container:" << std::endl;
0352   ss << " Number of partitions: " << connections_.size() << std::endl;
0353 
0354   // Loop through partitions
0355   uint16_t cntr = 0;
0356   FedConnections::const_iterator iconn = connections_.begin();
0357   FedConnections::const_iterator jconn = connections_.end();
0358   for (; iconn != jconn; ++iconn) {
0359     cntr++;
0360     if (partition.empty() || partition == iconn->first) {
0361       ss << "  Partition number   : " << cntr << " (out of " << connections_.size() << ")" << std::endl;
0362       ss << "  Partition name     : \"" << iconn->first << "\"" << std::endl;
0363       ss << "  Num of connections : " << iconn->second.size() << std::endl;
0364 
0365       // Extract FED ids and channels
0366       std::map<uint16_t, vector<uint16_t> > feds;
0367       FedConnectionsV::const_iterator iter = iconn->second.begin();
0368       FedConnectionsV::const_iterator jter = iconn->second.end();
0369       for (; iter != jter; ++iter) {
0370         if (*iter) {
0371           uint16_t fed_id = (*iter)->getFedId();
0372           uint16_t fed_ch = (*iter)->getFedChannel();
0373           if (find(feds[fed_id].begin(), feds[fed_id].end(), fed_ch) == feds[fed_id].end()) {
0374             feds[fed_id].push_back(fed_ch);
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 ids and channels
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() << " channels for FED id " << std::setw(3)
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 ichan = ifed->second.begin();
0405           std::vector<uint16_t>::const_iterator jchan = ifed->second.end();
0406           for (; ichan != jchan; ++ichan) {
0407             if (ichan != ifed->second.begin()) {
0408               if (*ichan != last + 1) {
0409                 ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
0410                 if (ichan != ifed->second.end()) {
0411                   first = *(ichan + 1);
0412                 }
0413               }
0414             }
0415             last = *ichan;
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 }