Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-24 01:11:20

0001 // -*- C++ -*-
0002 //
0003 // Package:    CondFormats/SiPixelObjects
0004 // Class:      FastSiPixelFEDChannelContainerFromQuality
0005 //
0006 /**\class FastSiPixelFEDChannelContainerFromQuality FastSiPixelFEDChannelContainerFromQuality.cc CondFormats/SiPixelObjects/plugins/FastSiPixelFEDChannelContainerFromQuality.cc
0007  Description: class to build the SiPixelFEDChannelContainer payloads
0008 */
0009 //
0010 // Original Author:  Marco Musich
0011 //         Created:  Tue, 25 Apr 2019 12:04:36 GMT
0012 //
0013 //
0014 
0015 #include "CondCore/CondDB/interface/ConnectionPool.h"
0016 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0017 #include "CondFormats/Common/interface/Time.h"
0018 #include "CondFormats/Common/interface/TimeConversions.h"
0019 #include "CondFormats/DataRecord/interface/SiPixelFedCablingMapRcd.h"
0020 #include "CondFormats/DataRecord/interface/SiPixelQualityFromDbRcd.h"
0021 #include "CondFormats/SiPixelObjects/interface/CablingPathToDetUnit.h"
0022 #include "CondFormats/SiPixelObjects/interface/PixelROC.h"
0023 #include "CondFormats/SiPixelObjects/interface/SiPixelFEDChannelContainer.h"
0024 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingMap.h"
0025 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingTree.h"
0026 #include "CondFormats/SiPixelObjects/interface/SiPixelQuality.h"
0027 #include "FWCore/Framework/interface/ESWatcher.h"
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/Frameworkfwd.h"
0030 #include "FWCore/Framework/interface/MakerMacros.h"
0031 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "FWCore/ServiceRegistry/interface/Service.h"
0034 
0035 #include <iomanip>  // std::setw
0036 #include <iostream>
0037 #include <fstream>
0038 #include <sstream>
0039 
0040 #include <TROOT.h>
0041 #include <TSystem.h>
0042 #include <TCanvas.h>
0043 #include <TFile.h>
0044 #include <TLegend.h>
0045 #include <TGraph.h>
0046 #include <TH1.h>
0047 
0048 namespace SiPixelFEDChannelUtils {
0049   std::pair<unsigned int, unsigned int> unpack(cond::Time_t since) {
0050     auto kLowMask = 0XFFFFFFFF;
0051     auto run = (since >> 32);
0052     auto lumi = (since & kLowMask);
0053     return std::make_pair(run, lumi);
0054   }
0055 }  // namespace SiPixelFEDChannelUtils
0056 
0057 class FastSiPixelFEDChannelContainerFromQuality : public edm::one::EDAnalyzer<> {
0058 public:
0059   explicit FastSiPixelFEDChannelContainerFromQuality(const edm::ParameterSet& iConfig);
0060   ~FastSiPixelFEDChannelContainerFromQuality() override;
0061   void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) override;
0062   void endJob() override;
0063   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0064   SiPixelFEDChannelContainer::SiPixelFEDChannelCollection createFromSiPixelQuality(
0065       const SiPixelQuality& theQuality,
0066       const SiPixelFedCablingMap& theFedCabling,
0067       const SiPixelFedCablingTree& theCablingTree);
0068 
0069 private:
0070   cond::persistency::ConnectionPool m_connectionPool;
0071   const std::string m_condDbQuality;
0072   const std::string m_condDbCabling;
0073   const std::string m_QualityTagName;
0074   const std::string m_CablingTagName;
0075   const std::string m_record;
0076 
0077   // Specify output text file name. Leave empty if do not want to dump in a file
0078   const std::string m_output;
0079 
0080   // Manually specify the start/end time.
0081   unsigned long long m_startTime;
0082   unsigned long long m_endTime;
0083 
0084   const bool printdebug_;
0085   const bool isMC_;
0086   const bool removeEmptyPayloads_;
0087 
0088   std::unique_ptr<SiPixelFEDChannelContainer> myQualities;
0089 
0090   inline unsigned int closest_from_above(std::vector<unsigned int> const& vec, unsigned int value) {
0091     auto const it = std::lower_bound(vec.begin(), vec.end(), value);
0092     return vec.at(it - vec.begin() - 1);
0093   }
0094 
0095   inline unsigned int closest_from_below(std::vector<unsigned int> const& vec, unsigned int value) {
0096     auto const it = std::upper_bound(vec.begin(), vec.end(), value);
0097     return vec.at(it - vec.begin() - 1);
0098   }
0099 };
0100 
0101 FastSiPixelFEDChannelContainerFromQuality::FastSiPixelFEDChannelContainerFromQuality(const edm::ParameterSet& iConfig)
0102     : m_connectionPool(),
0103       m_condDbQuality(iConfig.getParameter<std::string>("condDBQuality")),
0104       m_condDbCabling(iConfig.getParameter<std::string>("condDBCabling")),
0105       m_QualityTagName(iConfig.getParameter<std::string>("qualityTagName")),
0106       m_CablingTagName(iConfig.getParameter<std::string>("cablingMapTagName")),
0107       m_record(iConfig.getParameter<std::string>("record")),
0108       m_output(iConfig.getParameter<std::string>("output")),
0109       m_startTime(iConfig.getParameter<unsigned long long>("startIOV")),
0110       m_endTime(iConfig.getParameter<unsigned long long>("endIOV")),
0111       printdebug_(iConfig.getUntrackedParameter<bool>("printDebug", false)),
0112       isMC_(iConfig.getUntrackedParameter<bool>("isMC", true)),
0113       removeEmptyPayloads_(iConfig.getUntrackedParameter<bool>("removeEmptyPayloads", false)) {
0114   m_connectionPool.setParameters(iConfig.getParameter<edm::ParameterSet>("DBParameters"));
0115   m_connectionPool.configure();
0116 
0117   //now do what ever initialization is needed
0118   myQualities = std::make_unique<SiPixelFEDChannelContainer>();
0119 }
0120 
0121 FastSiPixelFEDChannelContainerFromQuality::~FastSiPixelFEDChannelContainerFromQuality() = default;
0122 
0123 void FastSiPixelFEDChannelContainerFromQuality::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
0124   std::stringstream ss;
0125 
0126   cond::Time_t startIov = m_startTime;
0127   cond::Time_t endIov = m_endTime;
0128   if (startIov > endIov)
0129     throw cms::Exception("endTime must be greater than startTime!");
0130   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0131       << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
0132       << "Set start time " << startIov << "\n ... Set end time " << endIov;
0133 
0134   // open db session for the quality
0135   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0136       << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
0137       << "Query the condition database " << m_condDbQuality;
0138 
0139   cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condDbQuality);
0140   condDbSession.transaction().start(true);
0141 
0142   // query the database
0143   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0144       << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
0145       << "Reading IOVs from tag " << m_QualityTagName;
0146 
0147   // open db session for the cabling map
0148   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0149       << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
0150       << "Query the condition database " << m_condDbCabling;
0151 
0152   cond::persistency::Session condDbSession2 = m_connectionPool.createSession(m_condDbCabling);
0153   condDbSession2.transaction().start(true);
0154 
0155   // query the database
0156   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0157       << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
0158       << "Reading IOVs from tag " << m_CablingTagName;
0159 
0160   // get the list of payloads for the SiPixelQuality tag
0161   std::vector<std::tuple<cond::Time_t, cond::Hash> > m_iovs;
0162   condDbSession.readIov(m_QualityTagName).selectRange(startIov, endIov, m_iovs);
0163 
0164   const auto MIN_VAL = cond::timeTypeSpecs[cond::runnumber].beginValue;
0165   const auto MAX_VAL = cond::timeTypeSpecs[cond::runnumber].endValue;
0166 
0167   // get the list of payloads for the Cabling Map
0168   std::vector<std::tuple<cond::Time_t, cond::Hash> > m_cabling_iovs;
0169   condDbSession2.readIov(m_CablingTagName).selectRange(MIN_VAL, MAX_VAL, m_cabling_iovs);
0170 
0171   // create here the unpacked list of IOVs (run numbers)
0172   std::vector<unsigned int> listOfIOVs;
0173   std::transform(m_iovs.begin(),
0174                  m_iovs.end(),
0175                  std::back_inserter(listOfIOVs),
0176                  [](std::tuple<cond::Time_t, cond::Hash> myIOV) -> unsigned int {
0177                    return SiPixelFEDChannelUtils::unpack(std::get<0>(myIOV)).first;
0178                  });
0179 
0180   std::vector<unsigned int> listOfCablingIOVs;
0181   std::transform(m_cabling_iovs.begin(),
0182                  m_cabling_iovs.end(),
0183                  std::back_inserter(listOfCablingIOVs),
0184                  [](std::tuple<cond::Time_t, cond::Hash> myIOV2) -> unsigned int { return std::get<0>(myIOV2); });
0185 
0186   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0187       << " Number of SiPixelQuality paloyads to analyze: " << listOfIOVs.size()
0188       << " Number of SiPixelFedCablngMap payloads: " << listOfCablingIOVs.size() << std::endl;
0189 
0190   if (listOfCablingIOVs.size() > 1) {
0191     if (closest_from_below(listOfCablingIOVs, listOfIOVs.front()) !=
0192         closest_from_above(listOfCablingIOVs, listOfIOVs.back())) {
0193       throw cms::Exception("") << " The Pixel FED Cabling map does not cover all the requested SiPixelQuality IOVs in "
0194                                   "the same interval of validity \n";
0195     }
0196   } else {
0197     if (listOfIOVs.front() < listOfCablingIOVs.front()) {
0198       throw cms::Exception("") << " The Pixel FED Cabling map does not cover all the requested IOVs \n";
0199     }
0200   }
0201 
0202   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0203       << " First run covered by SiPixelQuality tag: " << listOfIOVs.front()
0204       << " / last run covered by SiPixelQuality tag: " << listOfIOVs.back() << std::endl;
0205 
0206   edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality") << " SiPixel Cabling Map IOVs in the interval: ";
0207   for (const auto& cb : m_cabling_iovs) {
0208     edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality")
0209         << " " << std::setw(6) << std::get<0>(cb) << " : " << std::get<1>(cb);
0210   }
0211   edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality") << std::endl;
0212 
0213   if (printdebug_) {
0214     edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0215         << " closest_from_above(listOfCablingIOVs,listOfIOVs.back()): "
0216         << closest_from_above(listOfCablingIOVs, listOfIOVs.back()) << std::endl;
0217     edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0218         << " closest_from_below(listOfCablingIOVs,listOfIOVs.front()): "
0219         << closest_from_below(listOfCablingIOVs, listOfIOVs.front()) << std::endl;
0220   }
0221 
0222   auto it = std::find(
0223       listOfCablingIOVs.begin(), listOfCablingIOVs.end(), closest_from_below(listOfCablingIOVs, listOfIOVs.front()));
0224   int index = std::distance(listOfCablingIOVs.begin(), it);
0225 
0226   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0227       << " using the SiPixelFedCablingMap with hash: " << std::get<1>(m_cabling_iovs.at(index)) << std::endl;
0228 
0229   auto theCablingMapPayload = condDbSession2.fetchPayload<SiPixelFedCablingMap>(std::get<1>(m_cabling_iovs.at(index)));
0230   auto theCablingTree = (*theCablingMapPayload).cablingTree();
0231 
0232   printf(
0233       "Progressing Bar                               :0%%       20%%       40%%       60%%       80%%       100%%\n");
0234   printf("Translating into SiPixelFEDChannelCollection  :");
0235   int step = (m_iovs.size() >= 50) ? m_iovs.size() / 50 : 1;
0236   int niov = 0;
0237   for (const auto& myIOV : m_iovs) {
0238     if (niov % step == 0) {
0239       printf(".");
0240       fflush(stdout);
0241     }
0242 
0243     auto payload = condDbSession.fetchPayload<SiPixelQuality>(std::get<1>(myIOV));
0244     auto runLS = SiPixelFEDChannelUtils::unpack(std::get<0>(myIOV));
0245 
0246     // print IOVs summary
0247     ss << runLS.first << "," << runLS.second << " (" << std::get<0>(myIOV) << ")"
0248        << " [hash: " << std::get<1>(myIOV) << "] \n";
0249 
0250     std::string scenario = std::to_string(runLS.first) + "_" + std::to_string(runLS.second);
0251 
0252     if (printdebug_) {
0253       edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0254           << "Found IOV:" << runLS.first << "(" << runLS.second << ")" << std::endl;
0255     }
0256 
0257     auto theSiPixelFEDChannelCollection =
0258         this->createFromSiPixelQuality(*payload, *theCablingMapPayload, *theCablingTree);
0259 
0260     if (removeEmptyPayloads_ && theSiPixelFEDChannelCollection.empty())
0261       return;
0262 
0263     myQualities->setScenario(scenario, theSiPixelFEDChannelCollection);
0264 
0265     ++niov;
0266   }
0267   //carriage return for tests
0268   printf("\n\n");
0269 
0270   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0271       << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
0272       << "Read " << niov << " IOVs from tag " << m_QualityTagName
0273       << " corresponding to the specified time interval.\n\n"
0274       << ss.str();
0275 
0276   if (printdebug_) {
0277     edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0278         << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] " << ss.str();
0279   }
0280 
0281   condDbSession.transaction().commit();
0282   condDbSession2.transaction().commit();
0283 
0284   if (!m_output.empty()) {
0285     std::ofstream fout;
0286     fout.open(m_output);
0287     fout << ss.str();
0288     fout.close();
0289   }
0290 }
0291 
0292 // ------------ method called once each job just before starting event loop  ------------
0293 SiPixelFEDChannelContainer::SiPixelFEDChannelCollection
0294 FastSiPixelFEDChannelContainerFromQuality::createFromSiPixelQuality(const SiPixelQuality& theQuality,
0295                                                                     const SiPixelFedCablingMap& theFedCabling,
0296                                                                     const SiPixelFedCablingTree& theCablingTree) {
0297   auto fedid_ = theFedCabling.det2fedMap();
0298 
0299   SiPixelFEDChannelContainer::SiPixelFEDChannelCollection theBadChannelCollection;
0300 
0301   auto theDisabledModules = theQuality.getBadComponentList();
0302   for (const auto& mod : theDisabledModules) {
0303     //mod.DetID, mod.errorType,mod.BadRocs
0304 
0305     int coded_badRocs = mod.BadRocs;
0306     std::vector<PixelFEDChannel> disabledChannelsDetSet;
0307     std::vector<sipixelobjects::CablingPathToDetUnit> path = theFedCabling.pathToDetUnit(mod.DetID);
0308     unsigned int nrocs_inLink(0);
0309     if (!path.empty()) {
0310       const sipixelobjects::PixelFEDCabling* aFed = theCablingTree.fed(path.at(0).fed);
0311       const sipixelobjects::PixelFEDLink* link = aFed->link(path.at(0).link);
0312       nrocs_inLink = link->numberOfROCs();
0313     } else {
0314       throw cms::Exception("Inconsistent data") << "could not find CablingPathToDetUnit for detId:" << mod.DetID;
0315     }
0316 
0317     std::bitset<16> bad_rocs(coded_badRocs);
0318     unsigned int n_ch = bad_rocs.size() / nrocs_inLink;
0319 
0320     for (unsigned int i_roc = 0; i_roc < n_ch; ++i_roc) {
0321       unsigned int first_idx = nrocs_inLink * i_roc;
0322       unsigned int sec_idx = nrocs_inLink * (i_roc + 1) - 1;
0323       unsigned int mask = pow(2, nrocs_inLink) - 1;
0324       unsigned int n_setbits = (coded_badRocs >> (i_roc * nrocs_inLink)) & mask;
0325 
0326       if (n_setbits == 0) {
0327         continue;
0328       }
0329 
0330       if (n_setbits != mask) {
0331         if (printdebug_) {
0332           edm::LogWarning("FastSiPixelFEDChannelContainerFromQuality")
0333               << "Mismatch! DetId: " << mod.DetID << " " << n_setbits << " " << mask << std::endl;
0334         }
0335         continue;
0336       }
0337 
0338       if (printdebug_) {
0339         edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality") << "passed" << std::endl;
0340       }
0341 
0342       unsigned int link_id = 99999;
0343       unsigned int fed_id = 99999;
0344 
0345       for (auto const& p : path) {
0346         const sipixelobjects::PixelFEDCabling* aFed = theCablingTree.fed(p.fed);
0347         const sipixelobjects::PixelFEDLink* link = aFed->link(p.link);
0348         const sipixelobjects::PixelROC* roc = link->roc(p.roc);
0349         unsigned int first_roc = roc->idInDetUnit();
0350 
0351         if (first_roc == first_idx) {
0352           link_id = p.link;
0353           fed_id = p.fed;
0354           break;
0355         }
0356       }
0357 
0358       if (printdebug_) {
0359         edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality")
0360             << " " << fed_id << " " << link_id << " " << first_idx << "  " << sec_idx << std::endl;
0361       }
0362 
0363       PixelFEDChannel ch = {fed_id, link_id, first_idx, sec_idx};
0364       disabledChannelsDetSet.push_back(ch);
0365 
0366       if (printdebug_) {
0367         edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality")
0368             << i_roc << " " << coded_badRocs << " " << first_idx << " " << sec_idx << std::endl;
0369         edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality")
0370             << "=======================================" << std::endl;
0371       }
0372     }
0373 
0374     if (!disabledChannelsDetSet.empty()) {
0375       theBadChannelCollection[mod.DetID] = disabledChannelsDetSet;
0376     }
0377   }
0378   return theBadChannelCollection;
0379 }
0380 
0381 void FastSiPixelFEDChannelContainerFromQuality::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0382   edm::ParameterSetDescription desc;
0383   desc.setComment("Writes payloads of type SiPixelFEDChannelContainer");
0384   desc.addUntracked<bool>("printDebug", false);
0385   desc.addUntracked<bool>("removeEmptyPayloads", false);
0386   desc.add<std::string>("record", "SiPixelStatusScenariosRcd");
0387   desc.add<std::string>("condDBQuality", "frontier://FrontierPrep/CMS_CONDITIONS");
0388   desc.add<std::string>("qualityTagName", "SiPixelQualityOffline_2017_threshold1percent_stuckTBM");
0389   desc.add<std::string>("condDBCabling", "frontier://FrontierProd/CMS_CONDITIONS");
0390   desc.add<std::string>("cablingMapTagName", "SiPixelFedCablingMap_v1");
0391   desc.add<unsigned long long>("startIOV", 1310841198608821);
0392   desc.add<unsigned long long>("endIOV", 1312696624480350);
0393   desc.add<std::string>("output", "summary.txt");
0394   desc.add<std::string>("connect", "");
0395 
0396   edm::ParameterSetDescription descDBParameters;
0397   descDBParameters.addUntracked<std::string>("authenticationPath", "");
0398   descDBParameters.addUntracked<int>("authenticationSystem", 0);
0399   descDBParameters.addUntracked<std::string>("security", "");
0400   descDBParameters.addUntracked<int>("messageLevel", 0);
0401 
0402   desc.add<edm::ParameterSetDescription>("DBParameters", descDBParameters);
0403   descriptions.add("FastSiPixelFEDChannelContainerFromQuality", desc);
0404 }
0405 
0406 void FastSiPixelFEDChannelContainerFromQuality::endJob() {
0407   //  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")<<"Analyzed "<<IOVcount_<<" IOVs"<<std::endl;
0408   edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
0409       << "Size of SiPixelFEDChannelContainer object " << myQualities->size() << std::endl
0410       << std::endl;
0411 
0412   if (printdebug_) {
0413     edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality") << "Content of SiPixelFEDChannelContainer " << std::endl;
0414 
0415     // use built-in method in the CondFormat
0416     myQualities->printAll();
0417   }
0418 
0419   // Form the data here
0420   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0421   if (poolDbService.isAvailable()) {
0422     cond::Time_t valid_time = poolDbService->currentTime();
0423     // this writes the payload to begin in current run defined in cfg
0424     if (!isMC_) {
0425       poolDbService->writeOneIOV(*myQualities, valid_time, m_record);
0426     } else {
0427       // for MC IOV since=1
0428       poolDbService->writeOneIOV(*myQualities, 1, m_record);
0429     }
0430   }
0431 }
0432 
0433 DEFINE_FWK_MODULE(FastSiPixelFEDChannelContainerFromQuality);