Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:11:33

0001 #include "DQM/SiStripCommissioningDbClients/interface/CommissioningHistosUsingDb.h"
0002 #include "CalibFormats/SiStripObjects/interface/NumberOfDevices.h"
0003 #include "CalibFormats/SiStripObjects/interface/SiStripFecCabling.h"
0004 #include "CondFormats/SiStripObjects/interface/CommissioningAnalysis.h"
0005 #include "CondFormats/SiStripObjects/interface/FedChannelConnection.h"
0006 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0007 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0008 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
0009 #include "OnlineDB/SiStripESSources/interface/SiStripFedCablingBuilderFromDb.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include <iostream>
0012 
0013 using namespace sistrip;
0014 
0015 // -----------------------------------------------------------------------------
0016 /** */
0017 CommissioningHistosUsingDb::CommissioningHistosUsingDb(SiStripConfigDb* const db,
0018                                                        edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken,
0019                                                        sistrip::RunType type)
0020     : CommissioningHistograms(),
0021       runType_(type),
0022       db_(db),
0023       cabling_(nullptr),
0024       detInfo_(),
0025       uploadAnal_(true),
0026       uploadConf_(false),
0027       tTopoToken_{tTopoToken} {
0028   LogTrace(mlDqmClient_) << "[" << __PRETTY_FUNCTION__ << "]"
0029                          << " Constructing object...";
0030 }
0031 
0032 // -----------------------------------------------------------------------------
0033 /** */
0034 CommissioningHistosUsingDb::CommissioningHistosUsingDb()
0035     : CommissioningHistograms(),
0036       runType_(sistrip::UNDEFINED_RUN_TYPE),
0037       db_(nullptr),
0038       cabling_(nullptr),
0039       detInfo_(),
0040       uploadAnal_(false),
0041       uploadConf_(false) {
0042   LogTrace(mlDqmClient_) << "[" << __PRETTY_FUNCTION__ << "]"
0043                          << " Constructing object..." << endl;
0044 }
0045 
0046 // -----------------------------------------------------------------------------
0047 /** */
0048 CommissioningHistosUsingDb::~CommissioningHistosUsingDb() {
0049   if (db_) {
0050     delete db_;
0051   }
0052   LogTrace(mlDqmClient_) << "[" << __PRETTY_FUNCTION__ << "]"
0053                          << " Destructing object...";
0054 }
0055 
0056 void CommissioningHistosUsingDb::configure(const edm::ParameterSet&, const edm::EventSetup& setup) {
0057   if (!db_) {
0058     edm::LogError(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0059                                 << " NULL pointer to SiStripConfigDb interface!"
0060                                 << " Cannot configure...";
0061   } else {
0062     // Build FEC cabling object from connections found in DB
0063     SiStripFecCabling fec_cabling;
0064     if (runType_ == sistrip::FAST_CABLING) {
0065       SiStripFedCablingBuilderFromDb::buildFecCablingFromDevices(db_, fec_cabling);
0066     } else {
0067       SiStripFedCablingBuilderFromDb::buildFecCabling(db_, fec_cabling);
0068     }
0069 
0070     // Build FED cabling from FEC cabling
0071     cabling_ = new SiStripFedCabling();
0072     SiStripFedCablingBuilderFromDb::getFedCabling(fec_cabling, *cabling_);
0073     std::stringstream ss;
0074     ss << "[CommissioningHistosUsingDb::" << __func__ << "]"
0075        << " Terse print out of FED cabling:" << std::endl;
0076     cabling_->terse(ss);
0077     LogTrace(mlDqmClient_) << ss.str();
0078 
0079     const auto& tTopo = setup.getData(tTopoToken_);
0080     std::stringstream sss;
0081     sss << "[CommissioningHistosUsingDb::" << __func__ << "]"
0082         << " Summary of FED cabling:" << std::endl;
0083     cabling_->summary(sss, &tTopo);
0084     edm::LogVerbatim(mlDqmClient_) << sss.str();
0085   }
0086 }
0087 
0088 // -----------------------------------------------------------------------------
0089 /** */
0090 void CommissioningHistosUsingDb::uploadToConfigDb() {
0091   buildDetInfo();
0092   addDcuDetIds();
0093   uploadConfigurations();
0094   uploadAnalyses();
0095 }
0096 
0097 // -----------------------------------------------------------------------------
0098 /** */
0099 void CommissioningHistosUsingDb::uploadAnalyses() {
0100   if (!db_) {
0101     edm::LogError(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0102                                 << " NULL pointer to SiStripConfigDb interface!"
0103                                 << " Aborting upload...";
0104     return;
0105   }
0106 
0107   db_->clearAnalysisDescriptions();
0108   SiStripDbParams::SiStripPartitions::const_iterator ip = db_->dbParams().partitions().begin();
0109   SiStripDbParams::SiStripPartitions::const_iterator jp = db_->dbParams().partitions().end();
0110   for (; ip != jp; ++ip) {
0111     edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0112                                    << " Starting from partition " << ip->first << " with versions:\n"
0113                                    << std::dec << "   Conn: " << ip->second.cabVersion().first << "."
0114                                    << ip->second.cabVersion().second << "\n"
0115                                    << "   FED:  " << ip->second.fedVersion().first << "."
0116                                    << ip->second.fedVersion().second << "\n"
0117                                    << "   FEC:  " << ip->second.fecVersion().first << "."
0118                                    << ip->second.fecVersion().second << "\n"
0119                                    << "   Mask: " << ip->second.maskVersion().first << "."
0120                                    << ip->second.maskVersion().second;
0121 
0122     // Upload commissioning analysis results
0123     SiStripConfigDb::AnalysisDescriptionsV anals;
0124     createAnalyses(anals);
0125 
0126     edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0127                                    << " Created analysis descriptions for " << anals.size() << " devices";
0128 
0129     // Update analysis descriptions with new commissioning results
0130     if (uploadAnal_) {
0131       if (uploadConf_) {
0132         edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0133                                        << " Uploading major version of analysis descriptions to DB"
0134                                        << " (will be used for physics)...";
0135       } else {
0136         edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0137                                        << " Uploading minor version of analysis descriptions to DB"
0138                                        << " (will not be used for physics)...";
0139       }
0140       db_->clearAnalysisDescriptions(ip->second.partitionName());
0141       db_->addAnalysisDescriptions(ip->second.partitionName(), anals);
0142       db_->uploadAnalysisDescriptions(uploadConf_, ip->second.partitionName());
0143       edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0144                                      << " Upload of analysis descriptions to DB finished!";
0145     } else {
0146       edm::LogWarning(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0147                                     << " TEST! No analysis descriptions will be uploaded to DB...";
0148     }
0149 
0150     if (uploadConf_) {
0151       SiStripDbParams::SiStripPartitions::const_iterator ip = db_->dbParams().partitions().begin();
0152       SiStripDbParams::SiStripPartitions::const_iterator jp = db_->dbParams().partitions().end();
0153       for (; ip != jp; ++ip) {
0154         DeviceFactory* df = db_->deviceFactory();
0155         tkStateVector states = df->getCurrentStates();
0156         tkStateVector::const_iterator istate = states.begin();
0157         tkStateVector::const_iterator jstate = states.end();
0158         while (istate != jstate) {
0159           if (*istate && ip->first == (*istate)->getPartitionName()) {
0160             break;
0161           }
0162           istate++;
0163         }
0164         // Set versions if state was found
0165         if (istate != states.end()) {
0166           edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0167                                          << " Created new version for partition " << ip->first << ". Current state:\n"
0168                                          << std::dec << "   Conn: " << (*istate)->getConnectionVersionMajorId() << "."
0169                                          << (*istate)->getConnectionVersionMinorId() << "\n"
0170                                          << "   FED:  " << (*istate)->getFedVersionMajorId() << "."
0171                                          << (*istate)->getFedVersionMinorId() << "\n"
0172                                          << "   FEC:  " << (*istate)->getFecVersionMajorId() << "."
0173                                          << (*istate)->getFecVersionMinorId() << "\n"
0174                                          << "   Mask: " << (*istate)->getMaskVersionMajorId() << "."
0175                                          << (*istate)->getMaskVersionMinorId();
0176         }
0177       }
0178     }
0179   }
0180 }
0181 
0182 // -----------------------------------------------------------------------------
0183 /** */
0184 void CommissioningHistosUsingDb::addDcuDetIds() {
0185   if (!cabling_) {
0186     edm::LogWarning(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0187                                   << " NULL pointer to SiStripFedCabling object!";
0188     return;
0189   }
0190 
0191   Analyses::iterator ianal = data().begin();
0192   Analyses::iterator janal = data().end();
0193   for (; ianal != janal; ++ianal) {
0194     CommissioningAnalysis* anal = ianal->second;
0195 
0196     if (!anal) {
0197       edm::LogWarning(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0198                                     << " NULL pointer to CommissioningAnalysis object!";
0199       return;
0200     }
0201 
0202     SiStripFedKey fed_key = anal->fedKey();
0203     SiStripFecKey fec_key = anal->fecKey();
0204 
0205     FedChannelConnection conn = cabling_->fedConnection(fed_key.fedId(), fed_key.fedChannel());
0206 
0207     SiStripFedKey fed(conn.fedId(), SiStripFedKey::feUnit(conn.fedCh()), SiStripFedKey::feChan(conn.fedCh()));
0208 
0209     SiStripFecKey fec(
0210         conn.fecCrate(), conn.fecSlot(), conn.fecRing(), conn.ccuAddr(), conn.ccuChan(), conn.lldChannel());
0211 
0212     if (fed_key.path() != fed.path()) {
0213       std::stringstream ss;
0214       ss << "[CommissioningHistosUsingDb::" << __func__ << "]"
0215          << " Cannot set DCU and DetId values in commissioning analysis object!" << std::endl
0216          << " Incompatible FED key retrieved from cabling!" << std::endl
0217          << " FED key from analysis object  : " << fed_key.path() << std::endl
0218          << " FED key from cabling object   : " << fed.path() << std::endl
0219          << " FED id/ch from analysis object: " << fed_key.fedId() << "/" << fed_key.fedChannel() << std::endl
0220          << " FED id/ch from cabling object : " << conn.fedId() << "/" << conn.fedCh();
0221       edm::LogWarning(mlDqmClient_) << ss.str();
0222 
0223     } else if (fec_key.path() != fec.path()) {
0224       std::stringstream ss;
0225       ss << "[CommissioningHistosUsingDb::" << __func__ << "]"
0226          << " Cannot set DCU and DetId values in commissioning analysis object!" << std::endl
0227          << " Incompatible FEC key retrieved from cabling!" << std::endl
0228          << " FEC key from analysis object : " << fec_key.path() << std::endl
0229          << " FEC key from cabling object  : " << fec.path();
0230       edm::LogWarning(mlDqmClient_) << ss.str();
0231 
0232     } else {
0233       anal->dcuId(conn.dcuId());
0234       anal->detId(conn.detId());
0235     }
0236   }
0237 }
0238 
0239 // -----------------------------------------------------------------------------
0240 //
0241 void CommissioningHistosUsingDb::createAnalyses(SiStripConfigDb::AnalysisDescriptionsV& desc) {
0242   LogTrace(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0243                          << " Creating AnalysisDescriptions...";
0244 
0245   desc.clear();
0246 
0247   Analyses::iterator ianal = data().begin();
0248   Analyses::iterator janal = data().end();
0249   for (; ianal != janal; ++ianal) {
0250     // create analysis description
0251     create(desc, ianal);
0252   }
0253 }
0254 
0255 // -----------------------------------------------------------------------------
0256 //
0257 void CommissioningHistosUsingDb::buildDetInfo() {
0258   detInfo_.clear();
0259 
0260   if (!db()) {
0261     edm::LogError(mlDqmClient_) << "[CommissioningHistosUsingDb::" << __func__ << "]"
0262                                 << " NULL pointer to SiStripConfigDb interface!";
0263     return;
0264   }
0265 
0266   SiStripDbParams::SiStripPartitions::const_iterator ii = db_->dbParams().partitions().begin();
0267   SiStripDbParams::SiStripPartitions::const_iterator jj = db_->dbParams().partitions().end();
0268   for (; ii != jj; ++ii) {
0269     // Retrieve DCUs and DetIds for given partition
0270     std::string pp = ii->second.partitionName();
0271     SiStripConfigDb::DeviceDescriptionsRange dcus = db()->getDeviceDescriptions(DCU, pp);
0272     SiStripConfigDb::DcuDetIdsRange dets = db()->getDcuDetIds(pp);
0273 
0274     // Iterate through DCUs
0275     SiStripConfigDb::DeviceDescriptionsV::const_iterator idcu = dcus.begin();
0276     SiStripConfigDb::DeviceDescriptionsV::const_iterator jdcu = dcus.end();
0277     for (; idcu != jdcu; ++idcu) {
0278       // Extract DCU-FEH description
0279       dcuDescription* dcu = dynamic_cast<dcuDescription*>(*idcu);
0280       if (!dcu) {
0281         continue;
0282       }
0283       if (dcu->getDcuType() != "FEH") {
0284         continue;
0285       }
0286 
0287       // S.L. 29/1/2010
0288       // HARDCODED!!! We have a broken module, known from Pisa integration tests
0289       // We could really use a better solutin for this than hardcode it!!!
0290       if (dcu->getDcuHardId() == 16448250)
0291         continue;  // fake dcu (0xfafafa)
0292 
0293       // Find TkDcuInfo object corresponding to given DCU description
0294       SiStripConfigDb::DcuDetIdsV::const_iterator idet = dets.end();
0295       idet = SiStripConfigDb::findDcuDetId(dets.begin(), dets.end(), dcu->getDcuHardId());
0296       if (idet == dets.begin()) {
0297         continue;
0298       }
0299 
0300       // Extract TkDcuInfo object
0301       TkDcuInfo* det = idet->second;
0302       if (!det) {
0303         continue;
0304       }
0305 
0306       // Build FEC key
0307       const SiStripConfigDb::DeviceAddress& addr = db()->deviceAddress(*dcu);
0308       SiStripFecKey fec_key(addr.fecCrate_, addr.fecSlot_, addr.fecRing_, addr.ccuAddr_, addr.ccuChan_);
0309 
0310       // Build DetInfo object
0311       DetInfo info;
0312       info.dcuId_ = det->getDcuHardId();
0313       info.detId_ = det->getDetId();
0314       info.pairs_ = det->getApvNumber() / 2;
0315 
0316       // Add it to map
0317       if (fec_key.isValid()) {
0318         detInfo_[pp][fec_key.key()] = info;
0319       }
0320     }
0321   }
0322 
0323   // Debug
0324   if (edm::isDebugEnabled()) {
0325     std::stringstream ss;
0326     ss << "[CommissioningHistosUsingDb::" << __func__ << "]"
0327        << " List of modules for " << detInfo_.size()
0328        << " partitions, with their DCUids, DetIds, and nApvPairs: " << std::endl;
0329     std::map<std::string, DetInfos>::const_iterator ii = detInfo_.begin();
0330     std::map<std::string, DetInfos>::const_iterator jj = detInfo_.end();
0331     for (; ii != jj; ++ii) {
0332       ss << " Partition \"" << ii->first << "\" has " << ii->second.size() << " modules:" << std::endl;
0333       DetInfos::const_iterator iii = ii->second.begin();
0334       DetInfos::const_iterator jjj = ii->second.end();
0335       for (; iii != jjj; ++iii) {
0336         SiStripFecKey key = iii->first;
0337         ss << "  module= " << key.fecCrate() << "/" << key.fecSlot() << "/" << key.fecRing() << "/" << key.ccuAddr()
0338            << "/" << key.ccuChan() << ", " << std::hex << " DCUid= " << std::setw(8) << std::setfill('0')
0339            << iii->second.dcuId_ << " DetId= " << std::setw(8) << std::setfill('0') << iii->second.detId_ << std::dec
0340            << " nPairs= " << iii->second.pairs_ << std::endl;
0341       }
0342     }
0343     //LogTrace(mlDqmClient_) << ss.str();
0344   }
0345 }
0346 
0347 // -----------------------------------------------------------------------------
0348 //
0349 std::pair<std::string, CommissioningHistosUsingDb::DetInfo> CommissioningHistosUsingDb::detInfo(
0350     const SiStripFecKey& key) {
0351   SiStripFecKey tmp(key, sistrip::CCU_CHAN);
0352   if (tmp.isInvalid()) {
0353     return std::make_pair("", DetInfo());
0354   }
0355   std::map<std::string, DetInfos>::const_iterator ii = detInfo_.begin();
0356   std::map<std::string, DetInfos>::const_iterator jj = detInfo_.end();
0357   for (; ii != jj; ++ii) {
0358     DetInfos::const_iterator iii = ii->second.find(tmp.key());
0359     if (iii != ii->second.end()) {
0360       return std::make_pair(ii->first, iii->second);
0361     }
0362   }
0363   return std::make_pair("", DetInfo());
0364 }
0365 
0366 // -----------------------------------------------------------------------------
0367 //
0368 bool CommissioningHistosUsingDb::deviceIsPresent(const SiStripFecKey& key) {
0369   SiStripFecKey tmp(key, sistrip::CCU_CHAN);
0370   std::pair<std::string, DetInfo> info = detInfo(key);
0371   if (info.second.dcuId_ != sistrip::invalid32_) {
0372     if (key.channel() == 2 && info.second.pairs_ == 2) {
0373       return false;
0374     } else {
0375       return true;
0376     }
0377   } else {
0378     std::stringstream ss;
0379     ss << "[CommissioningHistosUsingDb::" << __func__ << "]"
0380        << " Cannot find module (crate/FEC/ring/CCU/module): " << tmp.fecCrate() << "/" << tmp.fecSlot() << "/"
0381        << tmp.fecRing() << "/" << tmp.ccuAddr() << "/" << tmp.ccuChan() << "!";
0382     edm::LogWarning(mlDqmClient_) << ss.str();
0383     return true;
0384   }
0385 }