CMS 3D CMS Logo

SiStripFEDMonitor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: DQM/SiStripMonitorHardware
4 // Class: SiStripFEDMonitorPlugin
5 //
10 //
11 // Original Author: Nicholas Cripps
12 // Created: 2008/09/16
13 //
14 //Modified : Anne-Marie Magnan
15 // ---- 2009/04/21 : histogram management put in separate class
16 // struct helper to simplify arguments of functions
17 // ---- 2009/04/22 : add TkHistoMap with % of bad channels per module
18 // ---- 2009/04/27 : create FEDErrors class
19 
20 #include <sstream>
21 #include <memory>
22 #include <list>
23 #include <algorithm>
24 #include <cassert>
25 
37 
44 
47 
49 
50 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
51 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
52 
54 
56 
57 //
58 // Class declaration
59 //
60 
62 public:
64  ~SiStripFEDMonitorPlugin() override;
65 
66 private:
67  void analyze(const edm::Event&, const edm::EventSetup&) override;
68  void dqmBeginLuminosityBlock(const edm::LuminosityBlock& lumiSeg, const edm::EventSetup& context) override;
69  void dqmEndLuminosityBlock(const edm::LuminosityBlock& lumiSeg, const edm::EventSetup& context) override;
70  void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
71 
72  //update the cabling if necessary
73  void updateCabling(const edm::EventSetup& eventSetup);
74 
75  static bool pairComparison(const std::pair<unsigned int, unsigned int>& pair1,
76  const std::pair<unsigned int, unsigned int>& pair2);
77 
78  void getMajority(const std::vector<std::pair<unsigned int, unsigned int> >& aFeMajVec,
79  unsigned int& aMajorityCounter,
80  std::vector<unsigned int>& afedIds);
81 
82  //tag of FEDRawData collection
86 
87  //histogram helper class
88  FEDHistograms fedHists_;
89  //folder name for histograms in DQMStore
92  //book detailed histograms even if they will be empty (for merging)
94  //do histos vs time with time=event number. Default time = orbit number (s)
96  //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
97  unsigned int printDebug_;
98  //FED cabling
99  uint32_t cablingCacheId_;
101 
102  //add parameter to save computing time if TkHistoMap/Median/FeMajCheck are not enabled
106 
107  unsigned int nEvt_;
108 
109  //FED errors
110  //need class member for lumi histograms
111  FEDErrors fedErrors_;
112  unsigned int maxFedBufferSize_;
113 
115 };
116 
117 //
118 // Constructors and destructor
119 //
120 
122  : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag", edm::InputTag("source", ""))),
123  topFolderName_(iConfig.getUntrackedParameter<std::string>("TopFolderName", "SiStrip")),
124  fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms", false)),
125  fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber", false)),
126  printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages", 1)),
127  cablingCacheId_(0),
129  fullDebugMode_(iConfig.getUntrackedParameter<bool>("FullDebugMode", false)) {
130  std::string subFolderName = iConfig.getUntrackedParameter<std::string>("HistogramFolderName", "ReadoutView");
131  folderName_ = topFolderName_ + "/" + subFolderName;
132 
133  rawDataToken_ = consumes<FEDRawDataCollection>(rawDataTag_);
134  heToken_ = consumes<EventWithHistory>(edm::InputTag("consecutiveHEs"));
135 
136  //print config to debug log
137  std::ostringstream debugStream;
138  if (printDebug_ > 1) {
139  debugStream << "[SiStripFEDMonitorPlugin]Configuration for SiStripFEDMonitorPlugin: " << std::endl
140  << "[SiStripFEDMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
141  << "[SiStripFEDMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
142  << "[SiStripFEDMonitorPlugin]\tFillAllDetailedHistograms? "
143  << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
144  << "[SiStripFEDMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
145  << "[SiStripFEDMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl;
146  }
147 
148  //don;t generate debug mesages if debug is disabled
149  std::ostringstream* pDebugStream = (printDebug_ > 1 ? &debugStream : nullptr);
150 
151  fedHists_.initialise(iConfig, pDebugStream);
152 
153  doTkHistoMap_ = fedHists_.tkHistoMapEnabled();
154 
155  doMedHists_ = fedHists_.cmHistosEnabled();
156 
157  doFEMajorityCheck_ = fedHists_.feMajHistosEnabled();
158 
159  if (printDebug_) {
160  LogTrace("SiStripMonitorHardware") << debugStream.str();
161  }
162 
163  nEvt_ = 0;
164 }
165 
167 
168 //
169 // Member functions
170 //
171 
172 // ------------ method called to for each event ------------
174  //Retrieve tracker topology from geometry
175  edm::ESHandle<TrackerTopology> tTopoHandle;
176  iSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
177  const TrackerTopology* const tTopo = tTopoHandle.product();
178 
179  //update cabling
180  updateCabling(iSetup);
181 
182  //get raw data
183  edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
184  iEvent.getByToken(rawDataToken_, rawDataCollectionHandle);
185  const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
186 
187  fedErrors_.initialiseEvent();
188 
189  //add the deltaBX value if the product exist
190 
192  iEvent.getByToken(heToken_, he);
193 
194  if (he.isValid() && !he.failedToGet()) {
195  fedErrors_.fillEventProperties(he->deltaBX());
196  }
197 
198  //initialise map of fedId/bad channel number
199  std::map<unsigned int, std::pair<unsigned short, unsigned short> > badChannelFraction;
200 
201  unsigned int lNFEDMonitoring = 0;
202  unsigned int lNFEDUnpacker = 0;
203  unsigned int lNChannelMonitoring = 0;
204  unsigned int lNChannelUnpacker = 0;
205 
206  unsigned int lNTotBadFeds = 0;
207  unsigned int lNTotBadChannels = 0;
208  unsigned int lNTotBadActiveChannels = 0;
209 
210  std::vector<std::vector<std::pair<unsigned int, unsigned int> > > lFeMajFrac;
211  const unsigned int nParts = 4;
212  if (doFEMajorityCheck_) {
213  lFeMajFrac.resize(nParts);
214  //max nFE per partition
215  lFeMajFrac[0].reserve(912);
216  lFeMajFrac[1].reserve(1080);
217  lFeMajFrac[2].reserve(768);
218  lFeMajFrac[3].reserve(760);
219  }
220 
221  maxFedBufferSize_ = 0;
222 
223  //loop over siStrip FED IDs
225  fedId++) { //loop over FED IDs
226  unsigned int lNBadChannels_perFEDID = 0;
227  const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
228 
229  //create an object to fill all errors
230  fedErrors_.initialiseFED(fedId, cabling_, tTopo);
231 
232  double aLumiSection = iEvent.orbitNumber() / 262144.0;
233 
234  //Do detailed check
235  //first check if data exists
236  bool lDataExist = fedErrors_.checkDataPresent(fedData);
237  if (!lDataExist) {
238  fedHists_.fillFEDHistograms(fedErrors_, 0, fullDebugMode_, aLumiSection, lNBadChannels_perFEDID);
239  continue;
240  }
241 
242  //check for problems and fill detailed histograms
243  fedErrors_.fillFEDErrors(fedData,
245  printDebug_,
246  lNChannelMonitoring,
247  lNChannelUnpacker,
248  doMedHists_,
249  fedHists_.cmHistPointer(false),
250  fedHists_.cmHistPointer(true),
252  lFeMajFrac);
253 
254  //check filled in previous method.
255  bool lFailUnpackerFEDcheck = fedErrors_.failUnpackerFEDCheck();
256 
257  fedErrors_.incrementFEDCounters();
258  unsigned int lSize = fedData.size();
259  if (lSize > maxFedBufferSize_) {
260  maxFedBufferSize_ = lSize;
261  }
262  //std::cout << " -- " << fedId << " " << lSize << std::endl;
263 
264  //fedHists_.fillFEDHistograms(fedErrors_,lSize,fullDebugMode_);
265 
266  bool lFailMonitoringFEDcheck = fedErrors_.failMonitoringFEDCheck();
267  if (lFailMonitoringFEDcheck)
268  lNTotBadFeds++;
269 
270  //sanity check: if something changed in the unpacking code
271  //but wasn't propagated here
272  //print only the summary, and more info if printDebug>1
273  if (lFailMonitoringFEDcheck != lFailUnpackerFEDcheck) {
274  if (printDebug_ > 1) {
275  std::ostringstream debugStream;
276  debugStream << " --- WARNING: FED " << fedId << std::endl << " ------ Monitoring FED check ";
277  if (lFailMonitoringFEDcheck)
278  debugStream << "failed." << std::endl;
279  else
280  debugStream << "passed." << std::endl;
281  debugStream << " ------ Unpacker FED check ";
282  if (lFailUnpackerFEDcheck)
283  debugStream << "failed." << std::endl;
284  else
285  debugStream << "passed." << std::endl;
286  edm::LogError("SiStripMonitorHardware") << debugStream.str();
287  }
288 
289  if (lFailMonitoringFEDcheck)
290  lNFEDMonitoring++;
291  else if (lFailUnpackerFEDcheck)
292  lNFEDUnpacker++;
293  }
294 
295  //Fill TkHistoMap:
296  //add an entry for all channels (good = 0),
297  //so that tkHistoMap knows which channels should be there.
298  if (doTkHistoMap_ && !fedHists_.tkHistoMapPointer()) {
299  edm::LogWarning("SiStripMonitorHardware")
300  << " -- Fedid " << fedId << ", TkHistoMap enabled but pointer is null." << std::endl;
301  }
302 
303  fedErrors_.fillBadChannelList(doTkHistoMap_,
304  fedHists_.tkHistoMapPointer(),
305  fedHists_.getFedvsAPVpointer(),
306  lNTotBadChannels,
307  lNTotBadActiveChannels,
308  lNBadChannels_perFEDID);
309  fedHists_.fillFEDHistograms(fedErrors_, lSize, fullDebugMode_, aLumiSection, lNBadChannels_perFEDID);
310  } //loop over FED IDs
311 
312  if (doFEMajorityCheck_) {
313  for (unsigned int iP(0); iP < nParts; ++iP) {
314  //std::cout << " -- Partition " << iP << std::endl;
315  //std::cout << " --- Number of elements in vec = " << lFeMajFrac[iP].size() << std::endl;
316  if (lFeMajFrac[iP].empty())
317  continue;
318  std::sort(lFeMajFrac[iP].begin(), lFeMajFrac[iP].end(), SiStripFEDMonitorPlugin::pairComparison);
319 
320  unsigned int lMajorityCounter = 0;
321  std::vector<unsigned int> lfedIds;
322 
323  getMajority(lFeMajFrac[iP], lMajorityCounter, lfedIds);
324  //std::cout << " -- Found " << lfedIds.size() << " unique elements not matching the majority." << std::endl;
325  fedHists_.fillMajorityHistograms(iP, static_cast<float>(lMajorityCounter) / lFeMajFrac[iP].size(), lfedIds);
326  }
327  }
328 
329  if ((lNTotBadFeds > 0 || lNTotBadChannels > 0) && printDebug_ > 1) {
330  std::ostringstream debugStream;
331  debugStream << "[SiStripFEDMonitorPlugin] --- Total number of bad feds = " << lNTotBadFeds << std::endl
332  << "[SiStripFEDMonitorPlugin] --- Total number of bad channels = " << lNTotBadChannels << std::endl
333  << "[SiStripFEDMonitorPlugin] --- Total number of bad active channels = " << lNTotBadActiveChannels
334  << std::endl;
335  edm::LogInfo("SiStripMonitorHardware") << debugStream.str();
336  }
337 
338  if ((lNFEDMonitoring > 0 || lNFEDUnpacker > 0 || lNChannelMonitoring > 0 || lNChannelUnpacker > 0) && printDebug_) {
339  std::ostringstream debugStream;
340  debugStream
341  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------"
342  << std::endl
343  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------"
344  << std::endl
345  << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at FED level : "
346  << std::endl
347  << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNFEDMonitoring
348  << std::endl
349  << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNFEDUnpacker
350  << std::endl
351  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------"
352  << std::endl
353  << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at Channel level : "
354  << std::endl
355  << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNChannelMonitoring
356  << std::endl
357  << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNChannelUnpacker
358  << std::endl
359  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------"
360  << std::endl
361  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------"
362  << std::endl;
363  edm::LogError("SiStripMonitorHardware") << debugStream.str();
364  }
365 
366  fedErrors_.getFEDErrorsCounters().nTotalBadChannels = lNTotBadChannels;
367  fedErrors_.getFEDErrorsCounters().nTotalBadActiveChannels = lNTotBadActiveChannels;
368 
369  //time in seconds since beginning of the run or event number
370  if (fillWithEvtNum_) {
371  // explicitely casting the event number unsigned long long to double here
372  double eventNumber = static_cast<double>(iEvent.id().event());
373  fedHists_.fillCountersHistograms(
374  fedErrors_.getFEDErrorsCounters(), fedErrors_.getChannelErrorsCounters(), maxFedBufferSize_, eventNumber);
375  } else {
376  double aTime = iEvent.orbitNumber() / 11223.;
377  fedHists_.fillCountersHistograms(
378  fedErrors_.getFEDErrorsCounters(), fedErrors_.getChannelErrorsCounters(), maxFedBufferSize_, aTime);
379  }
380 
381  nEvt_++;
382 
383 } //analyze method
384 
385 bool SiStripFEDMonitorPlugin::pairComparison(const std::pair<unsigned int, unsigned int>& pair1,
386  const std::pair<unsigned int, unsigned int>& pair2) {
387  return (pair1.second < pair2.second);
388 }
389 
390 void SiStripFEDMonitorPlugin::getMajority(const std::vector<std::pair<unsigned int, unsigned int> >& aFeMajVec,
391  unsigned int& aMajorityCounter,
392  std::vector<unsigned int>& afedIds) {
393  unsigned int lMajAddress = 0;
394  std::vector<std::pair<unsigned int, unsigned int> >::const_iterator lIter = aFeMajVec.begin();
395  unsigned int lMajAddr = (*lIter).second;
396  unsigned int lCounter = 0;
397 
398  //std::cout << " --- First element: addr = " << lMajAddr << " counter = " << lCounter << std::endl;
399  unsigned int iele = 0;
400  //bool foundMaj = false;
401  for (; lIter != aFeMajVec.end(); ++lIter, ++iele) {
402  //std::cout << " ---- Ele " << iele << " " << (*lIter).first << " " << (*lIter).second << " ref " << lMajAddr << std::endl;
403  if ((*lIter).second == lMajAddr) {
404  ++lCounter;
405  //std::cout << " ----- =ref: Counter = " << lCounter << std::endl;
406  } else {
407  //std::cout << " ----- !=ref: Counter = " << lCounter << " Majority = " << aMajorityCounter << std::endl;
408  if (lCounter > aMajorityCounter) {
409  //std::cout << " ------ >Majority: " << std::endl;
410  aMajorityCounter = lCounter;
411  // AV bug here??
412  lMajAddress = lMajAddr;
413  // lMajAddress = (*lIter).second;
414  //foundMaj=true;
415  }
416  lCounter = 0;
417  lMajAddr = (*lIter).second;
418  --lIter;
419  --iele;
420  }
421  }
422  // AV Bug here? The check has to be done regardless foundMaj == false or true
423  // if (!foundMaj) {
424  if (lCounter > aMajorityCounter) {
425  //std::cout << " ------ >Majority: " << std::endl;
426  aMajorityCounter = lCounter;
427  lMajAddress = lMajAddr;
428  }
429  // }
430  //std::cout << " -- found majority value for " << aMajorityCounter << " elements out of " << aFeMajVec.size() << "." << std::endl;
431  //get list of feds with address different from majority in partition:
432  lIter = aFeMajVec.begin();
433  afedIds.reserve(135);
434  for (; lIter != aFeMajVec.end(); ++lIter) {
435  if ((*lIter).second != lMajAddress) {
436  afedIds.push_back((*lIter).first);
437  } else {
438  lIter += aMajorityCounter - 1;
439  if (lIter >= aFeMajVec.end()) {
440  std::cout << "Here it is a bug: " << aMajorityCounter << " " << aFeMajVec.size() << " "
441  << lIter - aFeMajVec.end() << std::endl;
442  }
443  }
444  }
445  //std::cout << " -- Found " << lfedIds.size() << " elements not matching the majority." << std::endl;
446  if (!afedIds.empty()) {
447  std::sort(afedIds.begin(), afedIds.end());
448  std::vector<unsigned int>::iterator lIt = std::unique(afedIds.begin(), afedIds.end());
449  afedIds.erase(lIt, afedIds.end());
450  }
451 }
452 
454  const edm::Run& run,
455  const edm::EventSetup& eSetup) {
456  ibooker.setCurrentFolder(folderName_);
457 
458  edm::ESHandle<TkDetMap> tkDetMapHandle;
459  eSetup.get<TrackerTopologyRcd>().get(tkDetMapHandle);
460  const TkDetMap* tkDetMap = tkDetMapHandle.product();
461 
462  fedHists_.bookTopLevelHistograms(ibooker, tkDetMap);
463 
465  fedHists_.bookAllFEDHistograms(ibooker, fullDebugMode_);
466 }
467 
469  const edm::EventSetup& context) {
470  fedErrors_.initialiseLumiBlock();
471 }
472 
474  const edm::EventSetup& context) {
475  fedHists_.fillLumiHistograms(fedErrors_.getLumiErrors());
476 }
477 
479  uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
480  if (cablingCacheId_ != currentCacheId) {
481  edm::ESHandle<SiStripFedCabling> cablingHandle;
482  eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
483  cabling_ = cablingHandle.product();
484  cablingCacheId_ = currentCacheId;
485  }
486 }
487 
488 //
489 // Define as a plug-in
490 //
491 
size
Write out results.
EventNumber_t event() const
Definition: EventID.h:40
T getUntrackedParameter(std::string const &, T const &) const
edm::EDGetTokenT< FEDRawDataCollection > rawDataToken_
long long deltaBX(const unsigned int ev2, const unsigned int ev1) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
const SiStripFedCabling * cabling_
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:418
void analyze(const edm::Event &, const edm::EventSetup &) override
void dqmEndLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &context) override
void getMajority(const std::vector< std::pair< unsigned int, unsigned int > > &aFeMajVec, unsigned int &aMajorityCounter, std::vector< unsigned int > &afedIds)
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
SiStripFEDMonitorPlugin(const edm::ParameterSet &)
def unique(seq, keepstr=True)
Definition: tier0.py:24
void dqmBeginLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &context) override
#define end
Definition: vmac.h:39
int orbitNumber() const
Definition: EventBase.h:65
bool isValid() const
Definition: HandleBase.h:70
#define LogTrace(id)
bool failedToGet() const
Definition: HandleBase.h:72
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
edm::EventID id() const
Definition: EventBase.h:59
#define begin
Definition: vmac.h:32
HLT enums.
T get() const
Definition: EventSetup.h:73
static bool pairComparison(const std::pair< unsigned int, unsigned int > &pair1, const std::pair< unsigned int, unsigned int > &pair2)
edm::EDGetTokenT< EventWithHistory > heToken_
T const * product() const
Definition: ESHandle.h:86
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
void updateCabling(const edm::EventSetup &eventSetup)
Definition: Run.h:45