CMS 3D CMS Logo

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