Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-18 04:13:10

0001 #include "DQM/SiStripCommon/interface/SiStripFolderOrganizer.h"
0002 #include "DQM/SiStripMonitorClient/interface/SiStripUtility.h"
0003 #include "DQMServices/Core/interface/DQMStore.h"
0004 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
0005 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
0006 #include "CondFormats/RunInfo/interface/RunInfo.h"
0007 #include "CondFormats/DataRecord/interface/RunSummaryRcd.h"
0008 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
0009 #include "CondFormats/SiStripObjects/interface/SiStripDetVOff.h"
0010 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0011 #include "FWCore/Framework/interface/ESWatcher.h"
0012 #include "FWCore/Framework/interface/LuminosityBlock.h"
0013 #include "FWCore/Framework/interface/Run.h"
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 #include "FWCore/ServiceRegistry/interface/Service.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0019 
0020 #include <iostream>
0021 #include <iomanip>
0022 #include <cstdio>
0023 #include <sstream>
0024 #include <cmath>
0025 #include <fstream>
0026 #include <string>
0027 #include <vector>
0028 #include <map>
0029 #include <unordered_map>
0030 
0031 class SiStripDetVOff;
0032 class SiStripDetCabling;
0033 class RunInfo;
0034 
0035 class SiStripDcsInfo
0036     : public edm::one::EDAnalyzer<edm::one::SharedResources, edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
0037 public:
0038   typedef dqm::harvesting::MonitorElement MonitorElement;
0039   typedef dqm::harvesting::DQMStore DQMStore;
0040 
0041   SiStripDcsInfo(const edm::ParameterSet& ps);
0042 
0043 private:
0044   void beginJob() override;
0045   void beginRun(edm::Run const& run, edm::EventSetup const& eSetup) override;
0046   void beginLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) override;
0047   void endLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& iSetup) override;
0048   void endRun(edm::Run const& run, edm::EventSetup const& eSetup) override;
0049   void analyze(edm::Event const&, edm::EventSetup const&) override;
0050 
0051   void bookStatus(DQMStore& dqm_store);
0052   void readStatus(edm::EventSetup const&, int transition);
0053   void readCabling(edm::EventSetup const&);
0054   void addBadModules(DQMStore& dqm_store);
0055   void fillStatus(DQMStore& dqm_store);
0056   void fillDummyStatus(DQMStore& dqm_store);
0057 
0058   MonitorElement* DcsFraction_{nullptr};
0059 
0060   struct SubDetMEs {
0061     std::string folder_name;
0062     MonitorElement* DcsFractionME;
0063     int TotalDetectors;
0064     std::vector<uint32_t> FaultyDetectors;
0065     std::unordered_map<uint32_t, uint16_t> NLumiDetectorIsFaulty;
0066   };
0067 
0068   std::map<std::string, SubDetMEs> SubDetMEsMap{};
0069   unsigned long long m_cacheIDDcs_{};
0070   bool bookedStatus_{false};
0071 
0072   int nFEDConnected_{};
0073 
0074   int nLumiAnalysed_{};
0075 
0076   bool IsLumiGoodDcs_{false};
0077   int nGoodDcsLumi_{};
0078   static constexpr float MinAcceptableDcsDetFrac_{0.90};
0079   static constexpr float MaxAcceptableBadDcsLumi_{2};
0080 
0081   const SiStripDetCabling* detCabling_;
0082   edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken0_, tTopoToken1_, tTopoToken2_, tTopoToken3_;
0083   edm::ESGetToken<SiStripDetVOff, SiStripDetVOffRcd> detVOffToken0_, detVOffToken1_, detVOffToken2_;
0084   edm::ESWatcher<SiStripFedCablingRcd> fedCablingWatcher_;
0085   edm::ESGetToken<SiStripDetCabling, SiStripDetCablingRcd> detCablingToken_;  // beginRun
0086   edm::ESGetToken<RunInfo, RunInfoRcd> runInfoToken_;
0087 };
0088 
0089 //
0090 // -- Contructor
0091 //
0092 SiStripDcsInfo::SiStripDcsInfo(edm::ParameterSet const& pSet)
0093     : tTopoToken0_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
0094       tTopoToken1_(esConsumes<edm::Transition::EndLuminosityBlock>()),
0095       tTopoToken2_(esConsumes<edm::Transition::EndRun>()),
0096       tTopoToken3_(esConsumes<edm::Transition::BeginRun>()),
0097       detVOffToken0_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
0098       detVOffToken1_(esConsumes<edm::Transition::EndLuminosityBlock>()),
0099       detVOffToken2_(esConsumes<edm::Transition::EndRun>()),
0100       detCablingToken_(esConsumes<edm::Transition::BeginRun>()),
0101       runInfoToken_(esConsumes<edm::Transition::BeginRun>()) {
0102   usesResource("DQMStore");
0103   LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::Deleting SiStripDcsInfo ";
0104 }
0105 
0106 void SiStripDcsInfo::beginJob() {
0107   // Since SubDetMEs is a struct, using the brace initialization will
0108   // zero-initialize all members that are not specified in the call.
0109   SubDetMEsMap.emplace("TIB", SubDetMEs{"TIB", nullptr, 0, {}, {}});
0110   SubDetMEsMap.emplace("TOB", SubDetMEs{"TOB", nullptr, 0, {}, {}});
0111   SubDetMEsMap.emplace("TECB", SubDetMEs{"TEC/MINUS", nullptr, 0, {}, {}});
0112   SubDetMEsMap.emplace("TECF", SubDetMEs{"TEC/PLUS", nullptr, 0, {}, {}});
0113   SubDetMEsMap.emplace("TIDB", SubDetMEs{"TID/MINUS", nullptr, 0, {}, {}});
0114   SubDetMEsMap.emplace("TIDF", SubDetMEs{"TID/PLUS", nullptr, 0, {}, {}});
0115 }
0116 //
0117 // -- Begin Run
0118 //
0119 void SiStripDcsInfo::beginRun(edm::Run const& run, edm::EventSetup const& eSetup) {
0120   LogDebug("SiStripDcsInfo") << "SiStripDcsInfo:: Begining of Run";
0121   nFEDConnected_ = 0;
0122   constexpr int siStripFedIdMin{FEDNumbering::MINSiStripFEDID};
0123   constexpr int siStripFedIdMax{FEDNumbering::MAXSiStripFEDID};
0124 
0125   // Count Tracker FEDs from RunInfo
0126   //
0127   if (eSetup.tryToGet<RunInfoRcd>()) {
0128     if (auto sumFED = eSetup.getHandle(runInfoToken_)) {
0129       std::vector<int> FedsInIds = sumFED->m_fed_in;
0130       for (unsigned int it = 0; it < FedsInIds.size(); ++it) {
0131         int fedID = FedsInIds[it];
0132         if (fedID >= siStripFedIdMin && fedID <= siStripFedIdMax)
0133           ++nFEDConnected_;
0134       }
0135       LogDebug("SiStripDcsInfo") << " SiStripDcsInfo :: Connected FEDs " << nFEDConnected_;
0136     }
0137   }
0138 
0139   auto& dqm_store = *edm::Service<DQMStore>{};
0140   bookStatus(dqm_store);
0141   fillDummyStatus(dqm_store);
0142   if (nFEDConnected_ > 0)
0143     readCabling(eSetup);
0144 }
0145 
0146 void SiStripDcsInfo::analyze(edm::Event const& event, edm::EventSetup const& eSetup) {}
0147 
0148 void SiStripDcsInfo::beginLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) {
0149   LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::beginLuminosityBlock";
0150 
0151   if (nFEDConnected_ == 0)
0152     return;
0153 
0154   // initialise BadModule list
0155   for (auto& subDetME : SubDetMEsMap) {
0156     subDetME.second.FaultyDetectors.clear();
0157   }
0158   readStatus(eSetup, 0);
0159   nLumiAnalysed_++;
0160 }
0161 
0162 void SiStripDcsInfo::endLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) {
0163   LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::endLuminosityBlock";
0164 
0165   if (nFEDConnected_ == 0)
0166     return;
0167   auto& dqm_store = *edm::Service<DQMStore>{};
0168   readStatus(eSetup, 1);
0169   fillStatus(dqm_store);
0170 }
0171 
0172 void SiStripDcsInfo::endRun(edm::Run const& run, edm::EventSetup const& eSetup) {
0173   LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::EndRun";
0174 
0175   if (nFEDConnected_ == 0)
0176     return;
0177 
0178   for (auto& subDetME : SubDetMEsMap) {
0179     subDetME.second.FaultyDetectors.clear();
0180   }
0181   readStatus(eSetup, 2);
0182   auto& dqm_store = *edm::Service<DQMStore>{};
0183   addBadModules(dqm_store);
0184 }
0185 //
0186 // -- Book MEs for SiStrip Dcs Fraction
0187 //
0188 void SiStripDcsInfo::bookStatus(DQMStore& dqm_store) {
0189   if (bookedStatus_)
0190     return;
0191 
0192   std::string strip_dir = "";
0193   SiStripUtility::getTopFolderPath(dqm_store, "SiStrip", strip_dir);
0194   if (!strip_dir.empty())
0195     dqm_store.setCurrentFolder(strip_dir + "/EventInfo");
0196   else
0197     dqm_store.setCurrentFolder("SiStrip/EventInfo");
0198 
0199   auto scope = DQMStore::UseLumiScope(dqm_store);
0200 
0201   DcsFraction_ = dqm_store.bookFloat("DCSSummary");
0202 
0203   dqm_store.cd();
0204   if (!strip_dir.empty())
0205     dqm_store.setCurrentFolder(strip_dir + "/EventInfo/DCSContents");
0206   else
0207     dqm_store.setCurrentFolder("SiStrip/EventInfo/DCSContents");
0208   for (auto& [suffix, subDetME] : SubDetMEsMap) {
0209     std::string const me_name{"SiStrip_" + suffix};
0210     subDetME.DcsFractionME = dqm_store.bookFloat(me_name);
0211   }
0212   bookedStatus_ = true;
0213   dqm_store.cd();
0214 }
0215 
0216 void SiStripDcsInfo::readCabling(edm::EventSetup const& eSetup) {
0217   const auto tTopo = &eSetup.getData(tTopoToken3_);
0218 
0219   if (fedCablingWatcher_.check(eSetup)) {
0220     LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::readCabling : "
0221                                << " Change in Cache";
0222     detCabling_ = &eSetup.getData(detCablingToken_);
0223 
0224     std::vector<uint32_t> SelectedDetIds;
0225     detCabling_->addActiveDetectorsRawIds(SelectedDetIds);
0226     LogDebug("SiStripDcsInfo") << " SiStripDcsInfo::readCabling : "
0227                                << " Total Detectors " << SelectedDetIds.size();
0228 
0229     // initialise total # of detectors first
0230     for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
0231       it->second.TotalDetectors = 0;
0232     }
0233 
0234     for (std::vector<uint32_t>::const_iterator idetid = SelectedDetIds.begin(); idetid != SelectedDetIds.end();
0235          ++idetid) {
0236       uint32_t detId = *idetid;
0237       if (detId == 0 || detId == 0xFFFFFFFF)
0238         continue;
0239       std::string subdet_tag;
0240       SiStripUtility::getSubDetectorTag(detId, subdet_tag, tTopo);
0241 
0242       std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
0243       if (iPos != SubDetMEsMap.end()) {
0244         iPos->second.TotalDetectors++;
0245       }
0246     }
0247   }
0248 }
0249 //
0250 // -- Get Faulty Detectors
0251 //
0252 void SiStripDcsInfo::readStatus(edm::EventSetup const& eSetup, int transition) {
0253   const auto tTopo = &eSetup.getData(transition == 0 ? tTopoToken0_ : (transition == 1 ? tTopoToken1_ : tTopoToken2_));
0254   const auto& detVOff =
0255       eSetup.getData(transition == 0 ? detVOffToken0_ : (transition == 1 ? detVOffToken1_ : detVOffToken2_));
0256   std::vector<uint32_t> FaultyDetIds;
0257   detVOff.getDetIds(FaultyDetIds);
0258   LogDebug("SiStripDcsInfo") << " SiStripDcsInfo::readStatus : "
0259                              << " Faulty Detectors " << FaultyDetIds.size();
0260   // Read and fille bad modules
0261   for (std::vector<uint32_t>::const_iterator ihvoff = FaultyDetIds.begin(); ihvoff != FaultyDetIds.end(); ++ihvoff) {
0262     uint32_t detId_hvoff = (*ihvoff);
0263     if (!detCabling_->IsConnected(detId_hvoff))
0264       continue;
0265     std::string subdet_tag;
0266     SiStripUtility::getSubDetectorTag(detId_hvoff, subdet_tag, tTopo);
0267 
0268     std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
0269     if (iPos != SubDetMEsMap.end()) {
0270       std::vector<uint32_t>::iterator ibad =
0271           std::find(iPos->second.FaultyDetectors.begin(), iPos->second.FaultyDetectors.end(), detId_hvoff);
0272       if (ibad == iPos->second.FaultyDetectors.end())
0273         iPos->second.FaultyDetectors.push_back(detId_hvoff);
0274     }
0275   }
0276 }
0277 //
0278 // -- Fill Status
0279 //
0280 void SiStripDcsInfo::fillStatus(DQMStore& dqm_store) {
0281   if (!bookedStatus_)
0282     bookStatus(dqm_store);
0283   assert(bookedStatus_);
0284 
0285   float total_det = 0.0;
0286   float faulty_det = 0.0;
0287   float fraction;
0288   for (auto const& [name, subDetMEs] : SubDetMEsMap) {
0289     int total_subdet = subDetMEs.TotalDetectors;
0290     int faulty_subdet = subDetMEs.FaultyDetectors.size();
0291     if (nFEDConnected_ == 0 || total_subdet == 0)
0292       fraction = -1;
0293     else
0294       fraction = 1.0 - faulty_subdet * 1.0 / total_subdet;
0295     subDetMEs.DcsFractionME->Reset();
0296     subDetMEs.DcsFractionME->Fill(fraction);
0297     edm::LogInfo("SiStripDcsInfo") << " SiStripDcsInfo::fillStatus : Sub Detector " << name << " Total Number "
0298                                    << total_subdet << " Faulty ones " << faulty_subdet;
0299     total_det += total_subdet;
0300     faulty_det += faulty_subdet;
0301   }
0302   if (nFEDConnected_ == 0 || total_det == 0)
0303     fraction = -1.0;
0304   else
0305     fraction = 1 - faulty_det / total_det;
0306   DcsFraction_->Reset();
0307   DcsFraction_->Fill(fraction);
0308   IsLumiGoodDcs_ = fraction > MinAcceptableDcsDetFrac_;
0309   if (!IsLumiGoodDcs_)
0310     return;
0311 
0312   ++nGoodDcsLumi_;
0313   for (auto& pr : SubDetMEsMap) {
0314     for (auto const detId_faulty : pr.second.FaultyDetectors) {
0315       pr.second.NLumiDetectorIsFaulty[detId_faulty]++;
0316     }
0317   }
0318 }
0319 
0320 //
0321 // -- Fill with Dummy values
0322 //
0323 void SiStripDcsInfo::fillDummyStatus(DQMStore& dqm_store) {
0324   if (!bookedStatus_)
0325     bookStatus(dqm_store);
0326   assert(bookedStatus_);
0327 
0328   for (auto& pr : SubDetMEsMap) {
0329     pr.second.DcsFractionME->Reset();
0330     pr.second.DcsFractionME->Fill(-1.0);
0331   }
0332   DcsFraction_->Reset();
0333   DcsFraction_->Fill(-1.0);
0334 }
0335 
0336 void SiStripDcsInfo::addBadModules(DQMStore& dqm_store) {
0337   dqm_store.cd();
0338   std::string mdir = "MechanicalView";
0339   if (!SiStripUtility::goToDir(dqm_store, mdir)) {
0340     dqm_store.setCurrentFolder("SiStrip/" + mdir);
0341   }
0342   std::string mechanical_dir = dqm_store.pwd();
0343   std::string tag = "DCSError";
0344 
0345   for (auto const& pr : SubDetMEsMap) {
0346     auto const& lumiCountBadModules = pr.second.NLumiDetectorIsFaulty;
0347     for (auto const [ibad, nBadLumi] : lumiCountBadModules) {
0348       if (nBadLumi <= MaxAcceptableBadDcsLumi_)
0349         continue;
0350       std::string bad_module_folder = mechanical_dir + "/" + pr.second.folder_name +
0351                                       "/"
0352                                       "BadModuleList";
0353       dqm_store.setCurrentFolder(bad_module_folder);
0354 
0355       std::ostringstream detid_str;
0356       detid_str << ibad;
0357       std::string full_path = bad_module_folder + "/" + detid_str.str();
0358       MonitorElement* me = dqm_store.get(full_path);
0359       uint16_t flag = 0;
0360       if (me) {
0361         flag = me->getIntValue();
0362         me->Reset();
0363       } else
0364         me = dqm_store.bookInt(detid_str.str());
0365       SiStripUtility::setBadModuleFlag(tag, flag);
0366       me->Fill(flag);
0367     }
0368   }
0369   dqm_store.cd();
0370 }
0371 
0372 #include "FWCore/Framework/interface/MakerMacros.h"
0373 DEFINE_FWK_MODULE(SiStripDcsInfo);