Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:33

0001 
0002 #include "DQM/SiStripCommissioningDbClients/interface/ApvTimingHistosUsingDb.h"
0003 #include "CondFormats/SiStripObjects/interface/ApvTimingAnalysis.h"
0004 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0005 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
0006 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include <iostream>
0009 
0010 using namespace sistrip;
0011 
0012 // -----------------------------------------------------------------------------
0013 /** */
0014 ApvTimingHistosUsingDb::ApvTimingHistosUsingDb(const edm::ParameterSet& pset,
0015                                                DQMStore* bei,
0016                                                SiStripConfigDb* const db,
0017                                                edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken)
0018     : CommissioningHistograms(pset.getParameter<edm::ParameterSet>("ApvTimingParameters"), bei, sistrip::APV_TIMING),
0019       CommissioningHistosUsingDb(db, tTopoToken, sistrip::APV_TIMING),
0020       ApvTimingHistograms(pset.getParameter<edm::ParameterSet>("ApvTimingParameters"), bei) {
0021   LogTrace(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0022                          << " Constructing object...";
0023   skipFecUpdate_ = this->pset().getParameter<bool>("SkipFecUpdate");
0024   skipFedUpdate_ = this->pset().getParameter<bool>("SkipFedUpdate");
0025   if (skipFecUpdate_)
0026     LogTrace(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0027                            << " Skipping update of FEC parameters.";
0028   if (skipFedUpdate_)
0029     LogTrace(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0030                            << " Skipping update of FED parameters.";
0031 
0032   allowSelectiveUpload_ =
0033       this->pset().existsAs<bool>("doSelectiveUpload") ? this->pset().getParameter<bool>("doSelectiveUpload") : false;
0034   if (allowSelectiveUpload_)
0035     LogTrace(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0036                            << " Enabling selective update of FED parameters.";
0037 }
0038 
0039 // -----------------------------------------------------------------------------
0040 /** */
0041 ApvTimingHistosUsingDb::~ApvTimingHistosUsingDb() {
0042   LogTrace(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0043                          << " Destructing object...";
0044 }
0045 
0046 // -----------------------------------------------------------------------------
0047 /** */
0048 void ApvTimingHistosUsingDb::uploadConfigurations() {
0049   LogTrace(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]";
0050 
0051   if (!db()) {
0052     edm::LogError(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0053                                 << " NULL pointer to SiStripConfigDb interface!"
0054                                 << " Aborting upload...";
0055     return;
0056   }
0057 
0058   if (!skipFecUpdate_) {
0059     // Retrieve and update PLL device descriptions
0060     SiStripConfigDb::DeviceDescriptionsRange devices = db()->getDeviceDescriptions(PLL);
0061     bool upload = update(devices);
0062 
0063     // Check if new PLL settings are valid
0064     if (!upload) {
0065       edm::LogError(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0066                                   << " Found invalid PLL settings (coarse > 15)"
0067                                   << " Aborting update to database...";
0068       return;
0069     }
0070 
0071     // Upload PLL device descriptions
0072     if (doUploadConf()) {
0073       edm::LogVerbatim(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0074                                      << " Uploading PLL settings to DB...";
0075       db()->uploadDeviceDescriptions();
0076       edm::LogVerbatim(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0077                                      << " Upload of PLL settings to DB finished!";
0078     } else {
0079       edm::LogWarning(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0080                                     << " TEST only! No PLL settings will be uploaded to DB...";
0081     }
0082 
0083   } else {
0084     LogTrace(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0085                            << " No upload of PLL settings to DB, as defined by .cfg file!";
0086   }
0087 
0088   if (!skipFedUpdate_) {
0089     // Update FED descriptions with new ticker thresholds
0090     SiStripConfigDb::FedDescriptionsRange feds = db()->getFedDescriptions();
0091     update(feds);
0092 
0093     // Update FED descriptions with new ticker thresholds
0094     if (doUploadConf()) {
0095       edm::LogVerbatim(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0096                                      << " Uploading FED ticker thresholds to DB...";
0097       db()->uploadFedDescriptions();
0098       edm::LogVerbatim(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0099                                      << " Upload of FED ticker thresholds to DB finished!";
0100     } else {
0101       edm::LogWarning(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0102                                     << " TEST only! No FED ticker thresholds will be uploaded to DB...";
0103     }
0104 
0105   } else {
0106     LogTrace(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0107                            << " No Upload of FED ticker thresholds to DB, as defined by .cfg file!";
0108   }
0109 }
0110 
0111 // -----------------------------------------------------------------------------
0112 /** */
0113 bool ApvTimingHistosUsingDb::update(SiStripConfigDb::DeviceDescriptionsRange devices) {
0114   // Iterate through devices and update device descriptions
0115   uint16_t updated = 0;
0116   std::vector<SiStripFecKey> invalid;
0117   SiStripConfigDb::DeviceDescriptionsV::const_iterator idevice;
0118 
0119   for (idevice = devices.begin(); idevice != devices.end(); idevice++) {
0120     // Check device type
0121     if ((*idevice)->getDeviceType() != PLL) {
0122       continue;
0123     }
0124 
0125     // Cast to retrieve appropriate description object
0126     pllDescription* desc = dynamic_cast<pllDescription*>(*idevice);
0127     if (!desc) {
0128       continue;
0129     }
0130 
0131     // Retrieve device addresses from device description
0132     const SiStripConfigDb::DeviceAddress& addr = db()->deviceAddress(*desc);
0133     SiStripFecKey fec_path;
0134 
0135     // PLL delay settings
0136     uint32_t coarse = sistrip::invalid_;
0137     uint32_t fine = sistrip::invalid_;
0138 
0139     // Iterate through LLD channels
0140     for (uint16_t ichan = 0; ichan < sistrip::CHANS_PER_LLD; ichan++) {
0141       // Construct key from device description
0142       SiStripFecKey fec_key(addr.fecCrate_, addr.fecSlot_, addr.fecRing_, addr.ccuAddr_, addr.ccuChan_, ichan + 1);
0143       fec_path = fec_key;
0144 
0145       // Locate appropriate analysis object
0146       Analyses::const_iterator iter = data(allowSelectiveUpload_).find(fec_key.key());
0147       if (iter != data(allowSelectiveUpload_).end()) {
0148         ApvTimingAnalysis* anal = dynamic_cast<ApvTimingAnalysis*>(iter->second);
0149         if (!anal) {
0150           edm::LogError(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0151                                       << " NULL pointer to analysis object!";
0152           continue;
0153         }
0154 
0155         // Calculate coarse and fine delays
0156         int32_t delay = static_cast<int32_t>(rint(anal->delay()) * 24. / 25);
0157         // first set course delay
0158         coarse = static_cast<uint16_t>(desc->getDelayCoarse() + (delay / 24)) +
0159                  (static_cast<uint16_t>(desc->getDelayFine()) + (delay % 24)) / 24;
0160         delay = delay % 24;  // only bother with fine delay now
0161         if ((static_cast<uint16_t>(desc->getDelayFine()) + delay) % 24 < 0) {
0162           coarse -= 1;
0163           delay += 24;
0164         }
0165         fine = (static_cast<uint16_t>(desc->getDelayFine()) + delay) % 24;
0166 
0167         // Record PPLs maximum coarse setting
0168         if (coarse > 15) {
0169           invalid.push_back(fec_key);
0170         }
0171 
0172       } else {
0173         if (deviceIsPresent(fec_key)) {
0174           edm::LogWarning(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0175                                         << " Unable to find FEC key with params crate/FEC/ring/CCU/module/LLD: "
0176                                         << fec_key.fecCrate() << "/" << fec_key.fecSlot() << "/" << fec_key.fecRing()
0177                                         << "/" << fec_key.ccuAddr() << "/" << fec_key.ccuChan() << "/"
0178                                         << fec_key.channel();
0179         }
0180       }
0181 
0182       // Exit LLD channel loop if coarse and fine delays are known
0183       if (coarse != sistrip::invalid_ && fine != sistrip::invalid_) {
0184         break;
0185       }
0186 
0187     }  // lld channel loop
0188 
0189     // Update PLL settings
0190     if (coarse != sistrip::invalid_ && fine != sistrip::invalid_) {
0191       std::stringstream ss;
0192       if (edm::isDebugEnabled()) {
0193         ss << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0194            << " Updating coarse/fine PLL settings"
0195            << " for crate/FEC/ring/CCU/module " << fec_path.fecCrate() << "/" << fec_path.fecSlot() << "/"
0196            << fec_path.fecRing() << "/" << fec_path.ccuAddr() << "/" << fec_path.ccuChan() << " from "
0197            << static_cast<uint16_t>(desc->getDelayCoarse()) << "/" << static_cast<uint16_t>(desc->getDelayFine());
0198       }
0199       desc->setDelayCoarse(coarse);
0200       desc->setDelayFine(fine);
0201       updated++;
0202       if (edm::isDebugEnabled()) {
0203         ss << " to " << static_cast<uint16_t>(desc->getDelayCoarse()) << "/"
0204            << static_cast<uint16_t>(desc->getDelayFine());
0205         LogTrace(mlDqmClient_) << ss.str();
0206       }
0207 
0208     } else {
0209       edm::LogWarning(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0210                                     << " Invalid PLL delay settings course/fine = " << coarse << "/" << fine
0211                                     << " for crate/FEC/ring/CCU/module " << fec_path.fecCrate() << "/"
0212                                     << fec_path.fecSlot() << "/" << fec_path.fecRing() << "/" << fec_path.ccuAddr()
0213                                     << "/" << fec_path.ccuChan();
0214     }
0215   }
0216 
0217   // Check if invalid settings were found
0218   if (!invalid.empty()) {
0219     std::stringstream ss;
0220     ss << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0221        << " Found PLL coarse setting of >15"
0222        << " (not allowed!) for " << invalid.size() << " channels";
0223     ss << " (Example is crate/FEC/ring/CCU/module/LLD: " << invalid.front().fecCrate() << "/"
0224        << invalid.front().fecSlot() << "/" << invalid.front().fecRing() << "/" << invalid.front().ccuAddr() << "/"
0225        << invalid.front().ccuChan() << "/" << invalid.front().channel();
0226     edm::LogWarning(mlDqmClient_) << ss.str();
0227     return false;
0228   }
0229 
0230   edm::LogVerbatim(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0231                                  << " Updated PLL settings for " << updated << " modules";
0232   return true;
0233 }
0234 
0235 // -----------------------------------------------------------------------------
0236 /** */
0237 void ApvTimingHistosUsingDb::update(SiStripConfigDb::FedDescriptionsRange feds) {
0238   // Retrieve FED ids from cabling
0239   auto ids = cabling()->fedIds();
0240 
0241   // Iterate through feds and update fed descriptions
0242   uint16_t updated = 0;
0243   for (auto ifed = feds.begin(); ifed != feds.end(); ++ifed) {
0244     // If FED id not found in list (from cabling), then continue
0245     if (find(ids.begin(), ids.end(), (*ifed)->getFedId()) == ids.end()) {
0246       continue;
0247     }
0248 
0249     for (uint16_t ichan = 0; ichan < sistrip::FEDCH_PER_FED; ichan++) {
0250       // Build FED and FEC keys
0251       const FedChannelConnection& conn = cabling()->fedConnection((*ifed)->getFedId(), ichan);
0252       if (conn.fecCrate() == sistrip::invalid_ || conn.fecSlot() == sistrip::invalid_ ||
0253           conn.fecRing() == sistrip::invalid_ || conn.ccuAddr() == sistrip::invalid_ ||
0254           conn.ccuChan() == sistrip::invalid_ || conn.lldChannel() == sistrip::invalid_) {
0255         continue;
0256       }
0257       SiStripFedKey fed_key(conn.fedId(), SiStripFedKey::feUnit(conn.fedCh()), SiStripFedKey::feChan(conn.fedCh()));
0258       SiStripFecKey fec_key(
0259           conn.fecCrate(), conn.fecSlot(), conn.fecRing(), conn.ccuAddr(), conn.ccuChan(), conn.lldChannel());
0260 
0261       // Locate appropriate analysis object
0262       Analyses::const_iterator iter = data(allowSelectiveUpload_).find(fec_key.key());
0263       if (iter != data(allowSelectiveUpload_).end()) {
0264         ApvTimingAnalysis* anal = dynamic_cast<ApvTimingAnalysis*>(iter->second);
0265         if (!anal) {
0266           edm::LogError(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0267                                       << " NULL pointer to analysis object!";
0268           continue;
0269         }
0270 
0271         // Update frame finding threshold
0272         Fed9U::Fed9UAddress addr(ichan);
0273         uint16_t old_threshold = static_cast<uint16_t>((*ifed)->getFrameThreshold(addr));
0274         if (anal->isValid()) {
0275           (*ifed)->setFrameThreshold(addr, anal->frameFindingThreshold());
0276           updated++;
0277         }
0278         uint16_t new_threshold = static_cast<uint16_t>((*ifed)->getFrameThreshold(addr));
0279 
0280         // Debug
0281         std::stringstream ss;
0282         ss << "[ApvTimingHistosUsingDb::" << __func__ << "]";
0283         if (anal->isValid()) {
0284           ss << " Updating the frame-finding threshold"
0285              << " from " << old_threshold << " to " << new_threshold << " using tick mark base/peak/height "
0286              << anal->base() << "/" << anal->peak() << "/" << anal->height();
0287         } else {
0288           ss << " Cannot update the frame-finding threshold"
0289              << " from " << old_threshold << " to a new value using invalid analysis ";
0290         }
0291         ss << " for crate/FEC/ring/CCU/module/LLD " << fec_key.fecCrate() << "/" << fec_key.fecSlot() << "/"
0292            << fec_key.fecRing() << "/" << fec_key.ccuAddr() << "/" << fec_key.ccuChan() << fec_key.channel()
0293            << " and FED id/ch " << fed_key.fedId() << "/" << fed_key.fedChannel();
0294         anal->print(ss);
0295         //LogTrace(mlDqmClient_) << ss.str();
0296 
0297       } else {
0298         if (deviceIsPresent(fec_key)) {
0299           std::stringstream ss;
0300           ss << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0301              << " Unable to find analysis object and update ticker thresholds"
0302              << " for key/crate/FEC/ring/CCU/module/LLD " << std::hex << std::setw(8) << std::setfill('0')
0303              << fec_key.key() << std::dec << fec_key.fecCrate() << "/" << fec_key.fecSlot() << "/" << fec_key.fecRing()
0304              << "/" << fec_key.ccuAddr() << "/" << fec_key.ccuChan() << "/" << fec_key.channel()
0305              << " and FED key/id/ch " << std::hex << std::setw(8) << std::setfill('0') << fed_key.key() << std::dec
0306              << fed_key.fedId() << "/" << fed_key.fedChannel();
0307           edm::LogWarning(mlDqmClient_) << ss.str();
0308         }
0309       }
0310     }
0311   }
0312 
0313   edm::LogVerbatim(mlDqmClient_) << "[ApvTimingHistosUsingDb::" << __func__ << "]"
0314                                  << " Updated ticker thresholds for " << updated << " channels on " << ids.size()
0315                                  << " FEDs!";
0316 }
0317 
0318 // -----------------------------------------------------------------------------
0319 /** */
0320 void ApvTimingHistosUsingDb::create(SiStripConfigDb::AnalysisDescriptionsV& desc, Analysis analysis) {
0321   ApvTimingAnalysis* anal = dynamic_cast<ApvTimingAnalysis*>(analysis->second);
0322   if (!anal) {
0323     return;
0324   }
0325 
0326   SiStripFecKey fec_key(anal->fecKey());
0327   SiStripFedKey fed_key(anal->fedKey());
0328 
0329   for (uint16_t iapv = 0; iapv < 2; ++iapv) {
0330     //     std::stringstream ss;
0331     //     if ( anal->isValid() ) { ss << " TEST VALID "; }
0332     //     else { ss << " TEST INVALID "; }
0333     //     ss << std::hex << anal->fecKey() << std::dec << " "
0334     //        << anal->base() << " "
0335     //        << anal->peak() << " "
0336     //        << anal->height() << " "
0337     //        << ( anal->base() + anal->height() * ApvTimingAnalysis::frameFindingThreshold_ ) << " "
0338     //        << anal->frameFindingThreshold();
0339     //     edm::LogError("TEST") << ss.str();
0340 
0341     // Create description
0342     TimingAnalysisDescription* tmp;
0343     tmp = new TimingAnalysisDescription(anal->time(),
0344                                         anal->refTime(),
0345                                         anal->delay(),
0346                                         anal->height(),
0347                                         anal->base(),
0348                                         anal->peak(),
0349                                         anal->frameFindingThreshold(),
0350                                         anal->optimumSamplingPoint(),
0351                                         ApvTimingAnalysis::tickMarkHeightThreshold_,
0352                                         true,  //@@ APV timing analysis (not FED timing)
0353                                         fec_key.fecCrate(),
0354                                         fec_key.fecSlot(),
0355                                         fec_key.fecRing(),
0356                                         fec_key.ccuAddr(),
0357                                         fec_key.ccuChan(),
0358                                         SiStripFecKey::i2cAddr(fec_key.lldChan(), !iapv),
0359                                         db()->dbParams().partitions().begin()->second.partitionName(),
0360                                         db()->dbParams().partitions().begin()->second.runNumber(),
0361                                         anal->isValid(),
0362                                         "",
0363                                         fed_key.fedId(),
0364                                         fed_key.feUnit(),
0365                                         fed_key.feChan(),
0366                                         fed_key.fedApv());
0367 
0368     // Add comments
0369     typedef std::vector<std::string> Strings;
0370     Strings errors = anal->getErrorCodes();
0371     Strings::const_iterator istr = errors.begin();
0372     Strings::const_iterator jstr = errors.end();
0373     for (; istr != jstr; ++istr) {
0374       tmp->addComments(*istr);
0375     }
0376 
0377     // Store description
0378     desc.push_back(tmp);
0379   }
0380 }