CMS 3D CMS Logo

SiStripDcsInfo.cc
Go to the documentation of this file.
19 
20 #include <iostream>
21 #include <iomanip>
22 #include <cstdio>
23 #include <sstream>
24 #include <cmath>
25 #include <fstream>
26 #include <string>
27 #include <vector>
28 #include <map>
29 #include <unordered_map>
30 
31 class SiStripDetVOff;
32 class SiStripDetCabling;
33 class RunInfo;
34 
36  : public edm::one::EDAnalyzer<edm::one::SharedResources, edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
37 public:
40 
42 
43 private:
44  void beginJob() override;
45  void beginRun(edm::Run const& run, edm::EventSetup const& eSetup) override;
46  void beginLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) override;
47  void endLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& iSetup) override;
48  void endRun(edm::Run const& run, edm::EventSetup const& eSetup) override;
49  void analyze(edm::Event const&, edm::EventSetup const&) override;
50 
51  void bookStatus(DQMStore& dqm_store);
52  void readStatus(edm::EventSetup const&, int transition);
53  void readCabling(edm::EventSetup const&);
54  void addBadModules(DQMStore& dqm_store);
55  void fillStatus(DQMStore& dqm_store);
56  void fillDummyStatus(DQMStore& dqm_store);
57 
59 
60  struct SubDetMEs {
64  std::vector<uint32_t> FaultyDetectors;
65  std::unordered_map<uint32_t, uint16_t> NLumiDetectorIsFaulty;
66  };
67 
68  std::map<std::string, SubDetMEs> SubDetMEsMap{};
69  unsigned long long m_cacheIDDcs_{};
70  bool bookedStatus_{false};
71 
73 
75 
76  bool IsLumiGoodDcs_{false};
78  static constexpr float MinAcceptableDcsDetFrac_{0.90};
79  static constexpr float MaxAcceptableBadDcsLumi_{2};
80 
87 };
88 
89 //
90 // -- Contructor
91 //
93  : tTopoToken0_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
94  tTopoToken1_(esConsumes<edm::Transition::EndLuminosityBlock>()),
95  tTopoToken2_(esConsumes<edm::Transition::EndRun>()),
96  tTopoToken3_(esConsumes<edm::Transition::BeginRun>()),
97  detVOffToken0_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
98  detVOffToken1_(esConsumes<edm::Transition::EndLuminosityBlock>()),
99  detVOffToken2_(esConsumes<edm::Transition::EndRun>()),
100  detCablingToken_(esConsumes<edm::Transition::BeginRun>()),
101  runInfoToken_(esConsumes<edm::Transition::BeginRun>()) {
102  usesResource("DQMStore");
103  LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::Deleting SiStripDcsInfo ";
104 }
105 
107  // Since SubDetMEs is a struct, using the brace initialization will
108  // zero-initialize all members that are not specified in the call.
109  SubDetMEsMap.emplace("TIB", SubDetMEs{"TIB", nullptr, 0, {}, {}});
110  SubDetMEsMap.emplace("TOB", SubDetMEs{"TOB", nullptr, 0, {}, {}});
111  SubDetMEsMap.emplace("TECB", SubDetMEs{"TEC/MINUS", nullptr, 0, {}, {}});
112  SubDetMEsMap.emplace("TECF", SubDetMEs{"TEC/PLUS", nullptr, 0, {}, {}});
113  SubDetMEsMap.emplace("TIDB", SubDetMEs{"TID/MINUS", nullptr, 0, {}, {}});
114  SubDetMEsMap.emplace("TIDF", SubDetMEs{"TID/PLUS", nullptr, 0, {}, {}});
115 }
116 //
117 // -- Begin Run
118 //
120  LogDebug("SiStripDcsInfo") << "SiStripDcsInfo:: Begining of Run";
121  nFEDConnected_ = 0;
122  constexpr int siStripFedIdMin{FEDNumbering::MINSiStripFEDID};
123  constexpr int siStripFedIdMax{FEDNumbering::MAXSiStripFEDID};
124 
125  // Count Tracker FEDs from RunInfo
126  //
127  if (eSetup.tryToGet<RunInfoRcd>()) {
128  if (auto sumFED = eSetup.getHandle(runInfoToken_)) {
129  std::vector<int> FedsInIds = sumFED->m_fed_in;
130  for (unsigned int it = 0; it < FedsInIds.size(); ++it) {
131  int fedID = FedsInIds[it];
132  if (fedID >= siStripFedIdMin && fedID <= siStripFedIdMax)
133  ++nFEDConnected_;
134  }
135  LogDebug("SiStripDcsInfo") << " SiStripDcsInfo :: Connected FEDs " << nFEDConnected_;
136  }
137  }
138 
139  auto& dqm_store = *edm::Service<DQMStore>{};
140  bookStatus(dqm_store);
141  fillDummyStatus(dqm_store);
142  if (nFEDConnected_ > 0)
143  readCabling(eSetup);
144 }
145 
147 
149  LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::beginLuminosityBlock";
150 
151  if (nFEDConnected_ == 0)
152  return;
153 
154  // initialise BadModule list
155  for (auto& subDetME : SubDetMEsMap) {
156  subDetME.second.FaultyDetectors.clear();
157  }
158  readStatus(eSetup, 0);
159  nLumiAnalysed_++;
160 }
161 
163  LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::endLuminosityBlock";
164 
165  if (nFEDConnected_ == 0)
166  return;
167  auto& dqm_store = *edm::Service<DQMStore>{};
168  readStatus(eSetup, 1);
169  fillStatus(dqm_store);
170 }
171 
172 void SiStripDcsInfo::endRun(edm::Run const& run, edm::EventSetup const& eSetup) {
173  LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::EndRun";
174 
175  if (nFEDConnected_ == 0)
176  return;
177 
178  for (auto& subDetME : SubDetMEsMap) {
179  subDetME.second.FaultyDetectors.clear();
180  }
181  readStatus(eSetup, 2);
182  auto& dqm_store = *edm::Service<DQMStore>{};
183  addBadModules(dqm_store);
184 }
185 //
186 // -- Book MEs for SiStrip Dcs Fraction
187 //
189  if (bookedStatus_)
190  return;
191 
192  std::string strip_dir = "";
193  SiStripUtility::getTopFolderPath(dqm_store, "SiStrip", strip_dir);
194  if (!strip_dir.empty())
195  dqm_store.setCurrentFolder(strip_dir + "/EventInfo");
196  else
197  dqm_store.setCurrentFolder("SiStrip/EventInfo");
198 
199  auto scope = DQMStore::UseLumiScope(dqm_store);
200 
201  DcsFraction_ = dqm_store.bookFloat("DCSSummary");
202 
203  dqm_store.cd();
204  if (!strip_dir.empty())
205  dqm_store.setCurrentFolder(strip_dir + "/EventInfo/DCSContents");
206  else
207  dqm_store.setCurrentFolder("SiStrip/EventInfo/DCSContents");
208  for (auto& [suffix, subDetME] : SubDetMEsMap) {
209  std::string const me_name{"SiStrip_" + suffix};
210  subDetME.DcsFractionME = dqm_store.bookFloat(me_name);
211  }
212  bookedStatus_ = true;
213  dqm_store.cd();
214 }
215 
217  const auto tTopo = &eSetup.getData(tTopoToken3_);
218 
219  if (fedCablingWatcher_.check(eSetup)) {
220  LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::readCabling : "
221  << " Change in Cache";
223 
224  std::vector<uint32_t> SelectedDetIds;
225  detCabling_->addActiveDetectorsRawIds(SelectedDetIds);
226  LogDebug("SiStripDcsInfo") << " SiStripDcsInfo::readCabling : "
227  << " Total Detectors " << SelectedDetIds.size();
228 
229  // initialise total # of detectors first
230  for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
231  it->second.TotalDetectors = 0;
232  }
233 
234  for (std::vector<uint32_t>::const_iterator idetid = SelectedDetIds.begin(); idetid != SelectedDetIds.end();
235  ++idetid) {
236  uint32_t detId = *idetid;
237  if (detId == 0 || detId == 0xFFFFFFFF)
238  continue;
241 
242  std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
243  if (iPos != SubDetMEsMap.end()) {
244  iPos->second.TotalDetectors++;
245  }
246  }
247  }
248 }
249 //
250 // -- Get Faulty Detectors
251 //
252 void SiStripDcsInfo::readStatus(edm::EventSetup const& eSetup, int transition) {
253  const auto tTopo = &eSetup.getData(transition == 0 ? tTopoToken0_ : (transition == 1 ? tTopoToken1_ : tTopoToken2_));
254  const auto& detVOff =
255  eSetup.getData(transition == 0 ? detVOffToken0_ : (transition == 1 ? detVOffToken1_ : detVOffToken2_));
256  std::vector<uint32_t> FaultyDetIds;
257  detVOff.getDetIds(FaultyDetIds);
258  LogDebug("SiStripDcsInfo") << " SiStripDcsInfo::readStatus : "
259  << " Faulty Detectors " << FaultyDetIds.size();
260  // Read and fille bad modules
261  for (std::vector<uint32_t>::const_iterator ihvoff = FaultyDetIds.begin(); ihvoff != FaultyDetIds.end(); ++ihvoff) {
262  uint32_t detId_hvoff = (*ihvoff);
263  if (!detCabling_->IsConnected(detId_hvoff))
264  continue;
266  SiStripUtility::getSubDetectorTag(detId_hvoff, subdet_tag, tTopo);
267 
268  std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
269  if (iPos != SubDetMEsMap.end()) {
270  std::vector<uint32_t>::iterator ibad =
271  std::find(iPos->second.FaultyDetectors.begin(), iPos->second.FaultyDetectors.end(), detId_hvoff);
272  if (ibad == iPos->second.FaultyDetectors.end())
273  iPos->second.FaultyDetectors.push_back(detId_hvoff);
274  }
275  }
276 }
277 //
278 // -- Fill Status
279 //
281  if (!bookedStatus_)
282  bookStatus(dqm_store);
284 
285  float total_det = 0.0;
286  float faulty_det = 0.0;
287  float fraction;
288  for (auto const& [name, subDetMEs] : SubDetMEsMap) {
289  int total_subdet = subDetMEs.TotalDetectors;
290  int faulty_subdet = subDetMEs.FaultyDetectors.size();
291  if (nFEDConnected_ == 0 || total_subdet == 0)
292  fraction = -1;
293  else
294  fraction = 1.0 - faulty_subdet * 1.0 / total_subdet;
295  subDetMEs.DcsFractionME->Reset();
296  subDetMEs.DcsFractionME->Fill(fraction);
297  edm::LogInfo("SiStripDcsInfo") << " SiStripDcsInfo::fillStatus : Sub Detector " << name << " Total Number "
298  << total_subdet << " Faulty ones " << faulty_subdet;
299  total_det += total_subdet;
300  faulty_det += faulty_subdet;
301  }
302  if (nFEDConnected_ == 0 || total_det == 0)
303  fraction = -1.0;
304  else
305  fraction = 1 - faulty_det / total_det;
306  DcsFraction_->Reset();
309  if (!IsLumiGoodDcs_)
310  return;
311 
312  ++nGoodDcsLumi_;
313  for (auto& pr : SubDetMEsMap) {
314  for (auto const detId_faulty : pr.second.FaultyDetectors) {
315  pr.second.NLumiDetectorIsFaulty[detId_faulty]++;
316  }
317  }
318 }
319 
320 //
321 // -- Fill with Dummy values
322 //
324  if (!bookedStatus_)
325  bookStatus(dqm_store);
327 
328  for (auto& pr : SubDetMEsMap) {
329  pr.second.DcsFractionME->Reset();
330  pr.second.DcsFractionME->Fill(-1.0);
331  }
332  DcsFraction_->Reset();
333  DcsFraction_->Fill(-1.0);
334 }
335 
337  dqm_store.cd();
338  std::string mdir = "MechanicalView";
339  if (!SiStripUtility::goToDir(dqm_store, mdir)) {
340  dqm_store.setCurrentFolder("SiStrip/" + mdir);
341  }
342  std::string mechanical_dir = dqm_store.pwd();
343  std::string tag = "DCSError";
344 
345  for (auto const& pr : SubDetMEsMap) {
346  auto const& lumiCountBadModules = pr.second.NLumiDetectorIsFaulty;
347  for (auto const [ibad, nBadLumi] : lumiCountBadModules) {
348  if (nBadLumi <= MaxAcceptableBadDcsLumi_)
349  continue;
350  std::string bad_module_folder = mechanical_dir + "/" + pr.second.folder_name +
351  "/"
352  "BadModuleList";
353  dqm_store.setCurrentFolder(bad_module_folder);
354 
355  std::ostringstream detid_str;
356  detid_str << ibad;
357  std::string full_path = bad_module_folder + "/" + detid_str.str();
358  MonitorElement* me = dqm_store.get(full_path);
359  uint16_t flag = 0;
360  if (me) {
361  flag = me->getIntValue();
362  me->Reset();
363  } else
364  me = dqm_store.bookInt(detid_str.str());
366  me->Fill(flag);
367  }
368  }
369  dqm_store.cd();
370 }
371 
static void setBadModuleFlag(std::string &hname, uint16_t &flg)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
void endLuminosityBlock(edm::LuminosityBlock const &lumiSeg, edm::EventSetup const &iSetup) override
MonitorElement * bookFloat(TString const &name, FUNC onbooking=NOOP())
Definition: DQMStore.h:80
void bookStatus(DQMStore &dqm_store)
const SiStripDetCabling * detCabling_
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
edm::ESGetToken< SiStripDetVOff, SiStripDetVOffRcd > detVOffToken2_
SiStripDcsInfo(const edm::ParameterSet &ps)
edm::ESGetToken< SiStripDetVOff, SiStripDetVOffRcd > detVOffToken0_
void setCurrentFolder(std::string const &fullpath) override
Definition: DQMStore.h:646
std::optional< T > tryToGet() const
Definition: EventSetup.h:100
edm::ESGetToken< SiStripDetCabling, SiStripDetCablingRcd > detCablingToken_
std::string pwd() override
Definition: DQMStore.h:644
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken2_
void beginLuminosityBlock(edm::LuminosityBlock const &lumiSeg, edm::EventSetup const &eSetup) override
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
bool IsConnected(const uint32_t &det_id) const
assert(be >=bs)
edm::ESGetToken< RunInfo, RunInfoRcd > runInfoToken_
void Fill(long long x)
virtual void Reset()
Remove all data from the ME, keept the empty histogram with all its settings.
void fillStatus(DQMStore &dqm_store)
std::map< std::string, SubDetMEs > SubDetMEsMap
edm::ESWatcher< SiStripFedCablingRcd > fedCablingWatcher_
const std::string subdet_tag("SubDet")
Transition
Definition: Transition.h:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
MonitorElement * DcsFractionME
UseScope< MonitorElementData::Scope::LUMI > UseLumiScope
Definition: DQMStore.h:540
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
Log< level::Info, false > LogInfo
static constexpr float MaxAcceptableBadDcsLumi_
void readCabling(edm::EventSetup const &)
void readStatus(edm::EventSetup const &, int transition)
unsigned long long m_cacheIDDcs_
void beginRun(edm::Run const &run, edm::EventSetup const &eSetup) override
MonitorElement * bookInt(TString const &name, FUNC onbooking=NOOP())
Definition: DQMStore.h:73
MonitorElement * DcsFraction_
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:697
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
static void getSubDetectorTag(uint32_t det_id, std::string &subdet_tag, const TrackerTopology *tTopo)
void beginJob() override
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken3_
void endRun(edm::Run const &run, edm::EventSetup const &eSetup) override
HLT enums.
void analyze(edm::Event const &, edm::EventSetup const &) override
static bool goToDir(DQMStore &dqm_store, std::string const &name)
std::unordered_map< uint32_t, uint16_t > NLumiDetectorIsFaulty
edm::ESGetToken< SiStripDetVOff, SiStripDetVOffRcd > detVOffToken1_
std::vector< uint32_t > FaultyDetectors
void addBadModules(DQMStore &dqm_store)
dqm::harvesting::MonitorElement MonitorElement
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken1_
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken0_
static constexpr float MinAcceptableDcsDetFrac_
static void getTopFolderPath(DQMStore &dqm_store, std::string const &top_dir, std::string &path)
Definition: event.py:1
Definition: Run.h:45
void addActiveDetectorsRawIds(std::vector< uint32_t > &) const
void fillDummyStatus(DQMStore &dqm_store)
dqm::harvesting::DQMStore DQMStore
#define LogDebug(id)