Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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