Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-11 04:32:39

0001 #include "CondFormats/DataRecord/interface/RunSummaryRcd.h"
0002 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
0003 #include "CondFormats/RunInfo/interface/RunInfo.h"
0004 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0005 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0006 #include "DQM/SiStripMonitorClient/interface/SiStripUtility.h"
0007 #include "DQM/SiStripCommon/interface/SiStripFolderOrganizer.h"
0008 #include "DQMServices/Core/interface/DQMStore.h"
0009 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0010 #include "FWCore/Framework/interface/ESWatcher.h"
0011 #include "FWCore/Framework/interface/LuminosityBlock.h"
0012 #include "FWCore/Framework/interface/Run.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/ServiceRegistry/interface/Service.h"
0017 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0018 
0019 #include <cassert>
0020 #include <cmath>
0021 #include <cstdio>
0022 #include <iomanip>
0023 #include <iostream>
0024 #include <sstream>
0025 #include <fstream>
0026 #include <string>
0027 #include <vector>
0028 #include <map>
0029 
0030 class SiStripFedCabling;
0031 class TrackerTopology;
0032 
0033 class SiStripDaqInfo : public edm::one::EDAnalyzer<edm::one::SharedResources, edm::one::WatchRuns> {
0034 public:
0035   typedef dqm::harvesting::MonitorElement MonitorElement;
0036   typedef dqm::harvesting::DQMStore DQMStore;
0037 
0038   SiStripDaqInfo(edm::ParameterSet const& ps);
0039 
0040 private:
0041   void beginRun(edm::Run const& run, edm::EventSetup const& eSetup) override;
0042   void analyze(edm::Event const&, edm::EventSetup const&) override;
0043   void endRun(edm::Run const& run, edm::EventSetup const& eSetup) override {}
0044 
0045   void readFedIds(const SiStripFedCabling* fedcabling, edm::EventSetup const& iSetup);
0046   void readSubdetFedFractions(DQMStore& dqm_store, std::vector<int> const& fed_ids, edm::EventSetup const& iSetup);
0047   void bookStatus(DQMStore& dqm_store);
0048   void fillDummyStatus(DQMStore& dqm_store);
0049   void findExcludedModule(DQMStore& dqm_store, unsigned short fed_id, TrackerTopology const* tTopo);
0050 
0051   std::map<std::string, std::vector<unsigned short>> subDetFedMap_;
0052 
0053   MonitorElement* daqFraction_{nullptr};
0054 
0055   struct SubDetMEs {
0056     MonitorElement* daqFractionME;
0057     int connectedFeds;
0058   };
0059 
0060   std::map<std::string, SubDetMEs> subDetMEsMap_;
0061 
0062   int nFedTotal_{};
0063   bool bookedStatus_{false};
0064 
0065   const SiStripFedCabling* fedCabling_;
0066   edm::ESWatcher<SiStripFedCablingRcd> fedCablingWatcher_;
0067   edm::ESGetToken<SiStripFedCabling, SiStripFedCablingRcd> fedCablingToken_;
0068   edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken_;
0069   edm::ESGetToken<RunInfo, RunInfoRcd> runInfoToken_;
0070 };
0071 
0072 SiStripDaqInfo::SiStripDaqInfo(edm::ParameterSet const&)
0073     : fedCablingToken_{esConsumes<edm::Transition::BeginRun>()},
0074       tTopoToken_{esConsumes<edm::Transition::BeginRun>()},
0075       runInfoToken_{esConsumes<edm::Transition::BeginRun>()} {
0076   usesResource("DQMStore");
0077   edm::LogInfo("SiStripDaqInfo") << "SiStripDaqInfo::Deleting SiStripDaqInfo ";
0078 }
0079 
0080 //
0081 // -- Book MEs for SiStrip Daq Fraction
0082 //
0083 void SiStripDaqInfo::bookStatus(DQMStore& dqm_store) {
0084   edm::LogInfo("SiStripDcsInfo") << " SiStripDaqInfo::bookStatus " << bookedStatus_;
0085   if (bookedStatus_)
0086     return;
0087 
0088   dqm_store.cd();
0089   std::string strip_dir = "";
0090   SiStripUtility::getTopFolderPath(dqm_store, "SiStrip", strip_dir);
0091   if (!strip_dir.empty())
0092     dqm_store.setCurrentFolder(strip_dir + "/EventInfo");
0093   else
0094     dqm_store.setCurrentFolder("SiStrip/EventInfo");
0095 
0096   daqFraction_ = dqm_store.bookFloat("DAQSummary");
0097 
0098   dqm_store.cd();
0099   if (!strip_dir.empty())
0100     dqm_store.setCurrentFolder(strip_dir + "/EventInfo/DAQContents");
0101   else
0102     dqm_store.setCurrentFolder("SiStrip/EventInfo/DAQContents");
0103 
0104   std::vector<std::string> det_types;
0105   det_types.push_back("TIB");
0106   det_types.push_back("TOB");
0107   det_types.push_back("TIDF");
0108   det_types.push_back("TIDB");
0109   det_types.push_back("TECF");
0110   det_types.push_back("TECB");
0111 
0112   for (auto const& det : det_types) {
0113     std::string const me_name{"SiStrip_" + det};
0114     SubDetMEs local_mes{dqm_store.bookFloat(me_name), 0};
0115     subDetMEsMap_.emplace(det, local_mes);
0116   }
0117   bookedStatus_ = true;
0118   dqm_store.cd();
0119 }
0120 //
0121 // -- Fill with Dummy values
0122 //
0123 void SiStripDaqInfo::fillDummyStatus(DQMStore& dqm_store) {
0124   if (!bookedStatus_) {
0125     bookStatus(dqm_store);
0126   }
0127   assert(bookedStatus_);
0128 
0129   for (auto& pr : subDetMEsMap_) {
0130     pr.second.daqFractionME->Reset();
0131     pr.second.daqFractionME->Fill(-1.0);
0132   }
0133   daqFraction_->Reset();
0134   daqFraction_->Fill(-1.0);
0135 }
0136 
0137 void SiStripDaqInfo::beginRun(edm::Run const& run, edm::EventSetup const& eSetup) {
0138   edm::LogInfo("SiStripDaqInfo") << "SiStripDaqInfo:: Begining of Run";
0139 
0140   // Check latest Fed cabling and create TrackerMapCreator
0141   if (fedCablingWatcher_.check(eSetup)) {
0142     fedCabling_ = &eSetup.getData(fedCablingToken_);
0143     readFedIds(fedCabling_, eSetup);
0144   }
0145   auto& dqm_store = *edm::Service<DQMStore>{};
0146   if (!bookedStatus_) {
0147     bookStatus(dqm_store);
0148   }
0149   if (nFedTotal_ == 0) {
0150     fillDummyStatus(dqm_store);
0151     edm::LogInfo("SiStripDaqInfo") << " SiStripDaqInfo::No FEDs Connected!!!";
0152     return;
0153   }
0154 
0155   float nFEDConnected = 0.0;
0156   constexpr int siStripFedIdMin{FEDNumbering::MINSiStripFEDID};
0157   constexpr int siStripFedIdMax{FEDNumbering::MAXSiStripFEDID};
0158 
0159   if (!eSetup.tryToGet<RunInfoRcd>())
0160     return;
0161   auto sumFED = eSetup.getHandle(runInfoToken_);
0162   if (!sumFED)
0163     return;
0164 
0165   auto const& fedsInIds = sumFED->m_fed_in;
0166   for (auto const fedID : fedsInIds) {
0167     if (fedID >= siStripFedIdMin && fedID <= siStripFedIdMax)
0168       ++nFEDConnected;
0169   }
0170   edm::LogInfo("SiStripDaqInfo") << " SiStripDaqInfo::Total # of FEDs " << nFedTotal_ << " Connected FEDs "
0171                                  << nFEDConnected;
0172   if (nFEDConnected > 0) {
0173     daqFraction_->Reset();
0174     daqFraction_->Fill(nFEDConnected / nFedTotal_);
0175     readSubdetFedFractions(dqm_store, fedsInIds, eSetup);
0176   }
0177 }
0178 
0179 void SiStripDaqInfo::analyze(edm::Event const& event, edm::EventSetup const& eSetup) {}
0180 
0181 //
0182 // -- Read Sub Detector FEDs
0183 //
0184 void SiStripDaqInfo::readFedIds(const SiStripFedCabling* fedcabling, edm::EventSetup const& iSetup) {
0185   //Retrieve tracker topology from geometry
0186   const auto tTopo = &iSetup.getData(tTopoToken_);
0187 
0188   auto feds = fedCabling_->fedIds();
0189 
0190   nFedTotal_ = feds.size();
0191   for (auto const fed : feds) {
0192     auto fedChannels = fedCabling_->fedConnections(fed);
0193     for (auto const& conn : fedChannels) {
0194       if (!conn.isConnected())
0195         continue;
0196       uint32_t detId = conn.detId();
0197       if (detId == 0 || detId == 0xFFFFFFFF)
0198         continue;
0199       std::string subdet_tag;
0200       SiStripUtility::getSubDetectorTag(detId, subdet_tag, tTopo);
0201       subDetFedMap_[subdet_tag].push_back(fed);
0202       break;
0203     }
0204   }
0205 }
0206 //
0207 // -- Fill Subdet FEDIds
0208 //
0209 void SiStripDaqInfo::readSubdetFedFractions(DQMStore& dqm_store,
0210                                             std::vector<int> const& fed_ids,
0211                                             edm::EventSetup const& iSetup) {
0212   //Retrieve tracker topology from geometry
0213   const auto tTopo = &iSetup.getData(tTopoToken_);
0214 
0215   constexpr int siStripFedIdMin{FEDNumbering::MINSiStripFEDID};
0216   constexpr int siStripFedIdMax{FEDNumbering::MAXSiStripFEDID};
0217 
0218   // initialiase
0219   for (auto const& pr : subDetFedMap_) {
0220     auto const& name = pr.first;
0221     auto iPos = subDetMEsMap_.find(name);
0222     if (iPos == subDetMEsMap_.end())
0223       continue;
0224     iPos->second.connectedFeds = 0;
0225   }
0226   // count sub detector feds
0227 
0228   for (auto const& [name, subdetIds] : subDetFedMap_) {
0229     auto iPos = subDetMEsMap_.find(name);
0230     if (iPos == subDetMEsMap_.end())
0231       continue;
0232     iPos->second.connectedFeds = 0;
0233     for (auto const subdetId : subdetIds) {
0234       bool fedid_found = false;
0235       for (auto const fedId : fed_ids) {
0236         if (fedId < siStripFedIdMin || fedId > siStripFedIdMax)
0237           continue;
0238         if (subdetId == fedId) {
0239           fedid_found = true;
0240           iPos->second.connectedFeds++;
0241           break;
0242         }
0243       }
0244       if (!fedid_found)
0245         findExcludedModule(dqm_store, subdetId, tTopo);
0246     }
0247     if (auto nFedSubDet = subdetIds.size(); nFedSubDet > 0) {
0248       iPos->second.daqFractionME->Reset();
0249       int const nFedsConnected = iPos->second.connectedFeds;
0250       iPos->second.daqFractionME->Fill(nFedsConnected * 1.0 / nFedSubDet);
0251     }
0252   }
0253 }
0254 //
0255 // -- find Excluded Modules
0256 //
0257 void SiStripDaqInfo::findExcludedModule(DQMStore& dqm_store,
0258                                         unsigned short const fed_id,
0259                                         TrackerTopology const* tTopo) {
0260   dqm_store.cd();
0261   std::string mdir = "MechanicalView";
0262   if (!SiStripUtility::goToDir(dqm_store, mdir)) {
0263     dqm_store.setCurrentFolder("SiStrip/" + mdir);
0264   }
0265   std::string mechanical_dir = dqm_store.pwd();
0266   auto fedChannels = fedCabling_->fedConnections(fed_id);
0267   int ichannel = 0;
0268   std::string tag = "ExcludedFedChannel";
0269   std::string bad_module_folder;
0270   for (auto const& conn : fedChannels) {
0271     if (!conn.isConnected())
0272       continue;
0273     uint32_t detId = conn.detId();
0274     if (detId == 0 || detId == 0xFFFFFFFF)
0275       continue;
0276 
0277     ichannel++;
0278     if (ichannel == 1) {
0279       std::string subdet_folder;
0280       SiStripFolderOrganizer folder_organizer;
0281       folder_organizer.getSubDetFolder(detId, tTopo, subdet_folder);
0282       if (!dqm_store.dirExists(subdet_folder)) {
0283         subdet_folder = mechanical_dir + subdet_folder.substr(subdet_folder.find(mdir) + mdir.size());
0284       }
0285       bad_module_folder = subdet_folder + "/" + "BadModuleList";
0286       dqm_store.setCurrentFolder(bad_module_folder);
0287     }
0288     std::ostringstream detid_str;
0289     detid_str << detId;
0290     std::string full_path = bad_module_folder + "/" + detid_str.str();
0291     MonitorElement* me = dqm_store.get(full_path);
0292     uint16_t flag = 0;
0293     if (me) {
0294       flag = me->getIntValue();
0295       me->Reset();
0296     } else
0297       me = dqm_store.bookInt(detid_str.str());
0298     SiStripUtility::setBadModuleFlag(tag, flag);
0299     me->Fill(flag);
0300   }
0301   dqm_store.cd();
0302 }
0303 #include "FWCore/Framework/interface/MakerMacros.h"
0304 DEFINE_FWK_MODULE(SiStripDaqInfo);