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 
38 
45 
48 
50 
51 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
52 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
53 
55 
56 
57 //
58 // Class declaration
59 //
60 
62 {
63  public:
66  private:
67  virtual void beginJob() override;
68  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
69  virtual void endJob() override;
70  virtual void beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
71  const edm::EventSetup& context) override;
72  virtual void endLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
73  const edm::EventSetup& context) override;
74 
75  //update the cabling if necessary
76  void updateCabling(const edm::EventSetup& eventSetup);
77 
78  static bool pairComparison(const std::pair<unsigned int, unsigned int> & pair1,
79  const std::pair<unsigned int, unsigned int> & pair2);
80 
81  void getMajority(const std::vector<std::pair<unsigned int,unsigned int> > & aFeMajVec,
82  unsigned int & aMajorityCounter,
83  std::vector<unsigned int> & afedIds);
84 
85  //tag of FEDRawData collection
89 
90  //histogram helper class
91  FEDHistograms fedHists_;
92  //folder name for histograms in DQMStore
95  //book detailed histograms even if they will be empty (for merging)
97  //do histos vs time with time=event number. Default time = orbit number (s)
99  //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
100  unsigned int printDebug_;
101  //bool printDebug_;
102  //write the DQMStore to a root file at the end of the job
105  //the DQMStore
107  //FED cabling
108  uint32_t cablingCacheId_;
110 
111  //add parameter to save computing time if TkHistoMap/Median/FeMajCheck are not enabled
115 
116  unsigned int nEvt_;
117 
118  //FED errors
119  //need class member for lumi histograms
120  FEDErrors fedErrors_;
121  unsigned int maxFedBufferSize_;
122 };
123 
124 
125 //
126 // Constructors and destructor
127 //
128 
130  : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
131  topFolderName_(iConfig.getUntrackedParameter<std::string>("TopFolderName","SiStrip")),
132  fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
133  fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
134  printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
135  //printDebug_(iConfig.getUntrackedParameter<bool>("PrintDebugMessages",false)),
136  writeDQMStore_(iConfig.getUntrackedParameter<bool>("WriteDQMStore",false)),
137  dqmStoreFileName_(iConfig.getUntrackedParameter<std::string>("DQMStoreFileName","DQMStore.root")),
138  dqm_(0),
139  cablingCacheId_(0),
140  maxFedBufferSize_(0)
141 {
142  std::string subFolderName = iConfig.getUntrackedParameter<std::string>("HistogramFolderName","ReadoutView");
143  folderName_ = topFolderName_ + "/" + subFolderName;
144 
145 
146  rawDataToken_ = consumes<FEDRawDataCollection>(rawDataTag_);
147  heToken_ = consumes<EventWithHistory>(edm::InputTag("consecutiveHEs") );
148 
149  //print config to debug log
150  std::ostringstream debugStream;
151  if (printDebug_>1) {
152  debugStream << "[SiStripFEDMonitorPlugin]Configuration for SiStripFEDMonitorPlugin: " << std::endl
153  << "[SiStripFEDMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
154  << "[SiStripFEDMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
155  << "[SiStripFEDMonitorPlugin]\tFillAllDetailedHistograms? " << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
156  << "[SiStripFEDMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
157  << "[SiStripFEDMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl
158  << "[SiStripFEDMonitorPlugin]\tWriteDQMStore? " << (writeDQMStore_ ? "yes" : "no") << std::endl;
159  if (writeDQMStore_) debugStream << "[SiStripFEDMonitorPlugin]\tDQMStoreFileName: " << dqmStoreFileName_ << std::endl;
160  }
161 
162  //don;t generate debug mesages if debug is disabled
163  std::ostringstream* pDebugStream = (printDebug_>1 ? &debugStream : NULL);
164 
165  fedHists_.initialise(iConfig,pDebugStream);
166 
167  doTkHistoMap_ = fedHists_.tkHistoMapEnabled();
168 
169  doMedHists_ = fedHists_.cmHistosEnabled();
170 
171  doFEMajorityCheck_ = fedHists_.feMajHistosEnabled();
172 
173  if (printDebug_) {
174  LogTrace("SiStripMonitorHardware") << debugStream.str();
175 
176  //debugStream.str("");
177 
178  //debugStream << " -- Quelle est la difference entre un canard ? " << std::endl
179  // << " -- Reponse: c'est qu'il a les deux pattes de la meme longueur, surtout la gauche." << std::endl;
180 
181  //edm::LogError("SiStripMonitorHardware") << debugStream.str();
182  }
183 
184  nEvt_ = 0;
185 
186 }
187 
189 {
190 }
191 
192 
193 //
194 // Member functions
195 //
196 
197 // ------------ method called to for each event ------------
198 void
200  const edm::EventSetup& iSetup)
201 {
202  //Retrieve tracker topology from geometry
203  edm::ESHandle<TrackerTopology> tTopoHandle;
204  iSetup.get<IdealGeometryRecord>().get(tTopoHandle);
205  const TrackerTopology* const tTopo = tTopoHandle.product();
206 
207  //update cabling
208  updateCabling(iSetup);
209 
210  //get raw data
211  edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
212  // iEvent.getByLabel(rawDataTag_,rawDataCollectionHandle);
213  iEvent.getByToken(rawDataToken_,rawDataCollectionHandle);
214  const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
215 
216  fedErrors_.initialiseEvent();
217 
218  //add the deltaBX value if the product exist
219 
221  // iEvent.getByLabel("consecutiveHEs",he);
222  iEvent.getByToken(heToken_,he);
223 
224  if(he.isValid() && !he.failedToGet()) {
225  fedErrors_.fillEventProperties(he->deltaBX());
226  }
227 
228  //initialise map of fedId/bad channel number
229  std::map<unsigned int,std::pair<unsigned short,unsigned short> > badChannelFraction;
230 
231  unsigned int lNFEDMonitoring = 0;
232  unsigned int lNFEDUnpacker = 0;
233  unsigned int lNChannelMonitoring = 0;
234  unsigned int lNChannelUnpacker = 0;
235 
236  unsigned int lNTotBadFeds = 0;
237  unsigned int lNTotBadChannels = 0;
238  unsigned int lNTotBadActiveChannels = 0;
239 
240  std::vector<std::vector<std::pair<unsigned int,unsigned int> > > lFeMajFrac;
241  const unsigned int nParts = 4;
242  if (doFEMajorityCheck_){
243  lFeMajFrac.resize(nParts);
244  //max nFE per partition
245  lFeMajFrac[0].reserve(912);
246  lFeMajFrac[1].reserve(1080);
247  lFeMajFrac[2].reserve(768);
248  lFeMajFrac[3].reserve(760);
249  }
250 
251  maxFedBufferSize_ = 0;
252 
253  //loop over siStrip FED IDs
254  for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
256  fedId++) {//loop over FED IDs
257  const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
258 
259  //create an object to fill all errors
260  fedErrors_.initialiseFED(fedId,cabling_,tTopo);
261  bool lFullDebug = false;
262 
263  //Do detailed check
264  //first check if data exists
265  bool lDataExist = fedErrors_.checkDataPresent(fedData);
266  if (!lDataExist) {
267  fedHists_.fillFEDHistograms(fedErrors_,0,lFullDebug);
268  continue;
269  }
270 
271 
272 
273  //check for problems and fill detailed histograms
274  fedErrors_.fillFEDErrors(fedData,
275  lFullDebug,
276  printDebug_,
277  lNChannelMonitoring,
278  lNChannelUnpacker,
279  doMedHists_,
280  fedHists_.cmHistPointer(false),
281  fedHists_.cmHistPointer(true),
283  lFeMajFrac
284  );
285 
286  //check filled in previous method.
287  bool lFailUnpackerFEDcheck = fedErrors_.failUnpackerFEDCheck();
288 
289  fedErrors_.incrementFEDCounters();
290  unsigned int lSize = fedData.size();
291  if (lSize > maxFedBufferSize_){
292  maxFedBufferSize_ = lSize;
293  }
294  //std::cout << " -- " << fedId << " " << lSize << std::endl;
295 
296  fedHists_.fillFEDHistograms(fedErrors_,lSize,lFullDebug);
297 
298  bool lFailMonitoringFEDcheck = fedErrors_.failMonitoringFEDCheck();
299  if (lFailMonitoringFEDcheck) lNTotBadFeds++;
300 
301 
302  //sanity check: if something changed in the unpacking code
303  //but wasn't propagated here
304  //print only the summary, and more info if printDebug>1
305  if (lFailMonitoringFEDcheck != lFailUnpackerFEDcheck) {
306  if (printDebug_>1) {
307  std::ostringstream debugStream;
308  debugStream << " --- WARNING: FED " << fedId << std::endl
309  << " ------ Monitoring FED check " ;
310  if (lFailMonitoringFEDcheck) debugStream << "failed." << std::endl;
311  else debugStream << "passed." << std::endl ;
312  debugStream << " ------ Unpacker FED check " ;
313  if (lFailUnpackerFEDcheck) debugStream << "failed." << std::endl;
314  else debugStream << "passed." << std::endl ;
315  edm::LogError("SiStripMonitorHardware") << debugStream.str();
316  }
317 
318  if (lFailMonitoringFEDcheck) lNFEDMonitoring++;
319  else if (lFailUnpackerFEDcheck) lNFEDUnpacker++;
320  }
321 
322 
323  //Fill TkHistoMap:
324  //add an entry for all channels (good = 0),
325  //so that tkHistoMap knows which channels should be there.
326  if (doTkHistoMap_ && !fedHists_.tkHistoMapPointer()) {
327  edm::LogWarning("SiStripMonitorHardware") << " -- Fedid " << fedId
328  << ", TkHistoMap enabled but pointer is null." << std::endl;
329  }
330 
331  fedErrors_.fillBadChannelList(doTkHistoMap_,
332  fedHists_.tkHistoMapPointer(),
333  fedHists_.getFedvsAPVpointer(),
334  lNTotBadChannels,
335  lNTotBadActiveChannels);
336  }//loop over FED IDs
337 
338 
339  if (doFEMajorityCheck_){
340  for (unsigned int iP(0); iP<nParts; ++iP){
341  //std::cout << " -- Partition " << iP << std::endl;
342  //std::cout << " --- Number of elements in vec = " << lFeMajFrac[iP].size() << std::endl;
343  if (lFeMajFrac[iP].size()==0) continue;
344  std::sort(lFeMajFrac[iP].begin(),lFeMajFrac[iP].end(),SiStripFEDMonitorPlugin::pairComparison);
345 
346  unsigned int lMajorityCounter = 0;
347  std::vector<unsigned int> lfedIds;
348 
349  getMajority(lFeMajFrac[iP],lMajorityCounter,lfedIds);
350  //std::cout << " -- Found " << lfedIds.size() << " unique elements not matching the majority." << std::endl;
351  fedHists_.fillMajorityHistograms(iP,static_cast<float>(lMajorityCounter)/lFeMajFrac[iP].size(),lfedIds);
352  }
353  }
354 
355  if ((lNTotBadFeds> 0 || lNTotBadChannels>0) && printDebug_>1) {
356  std::ostringstream debugStream;
357  debugStream << "[SiStripFEDMonitorPlugin] --- Total number of bad feds = "
358  << lNTotBadFeds << std::endl
359  << "[SiStripFEDMonitorPlugin] --- Total number of bad channels = "
360  << lNTotBadChannels << std::endl
361  << "[SiStripFEDMonitorPlugin] --- Total number of bad active channels = "
362  << lNTotBadActiveChannels << std::endl;
363  edm::LogInfo("SiStripMonitorHardware") << debugStream.str();
364  }
365 
366  if ((lNFEDMonitoring > 0 || lNFEDUnpacker > 0 || lNChannelMonitoring > 0 || lNChannelUnpacker > 0) && printDebug_) {
367  std::ostringstream debugStream;
368  debugStream
369  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
370  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
371  << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at FED level : " << std::endl
372  << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNFEDMonitoring << std::endl
373  << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNFEDUnpacker << std::endl
374  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
375  << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at Channel level : " << std::endl
376  << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNChannelMonitoring << std::endl
377  << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNChannelUnpacker << std::endl
378  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
379  << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl ;
380  edm::LogError("SiStripMonitorHardware") << debugStream.str();
381 
382  }
383 
384  FEDErrors::getFEDErrorsCounters().nTotalBadChannels = lNTotBadChannels;
385  FEDErrors::getFEDErrorsCounters().nTotalBadActiveChannels = lNTotBadActiveChannels;
386 
387  //fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(), nEvt_);
388  //time in seconds since beginning of the run or event number
389  if (fillWithEvtNum_) fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(),FEDErrors::getChannelErrorsCounters(),maxFedBufferSize_,iEvent.id().event());
390  else fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(),FEDErrors::getChannelErrorsCounters(),maxFedBufferSize_,iEvent.orbitNumber()/11223.);
391 
392  nEvt_++;
393 
394 }//analyze method
395 
396 
397 bool SiStripFEDMonitorPlugin::pairComparison(const std::pair<unsigned int, unsigned int> & pair1,
398  const std::pair<unsigned int, unsigned int> & pair2){
399  return (pair1.second < pair2.second) ;
400 }
401 
402 void SiStripFEDMonitorPlugin::getMajority(const std::vector<std::pair<unsigned int,unsigned int> > & aFeMajVec,
403  unsigned int & aMajorityCounter,
404  std::vector<unsigned int> & afedIds) {
405 
406 
407  unsigned int lMajAddress = 0;
408  std::vector<std::pair<unsigned int,unsigned int> >::const_iterator lIter = aFeMajVec.begin();
409  unsigned int lMajAddr = (*lIter).second;
410  unsigned int lCounter = 0;
411 
412  //std::cout << " --- First element: addr = " << lMajAddr << " counter = " << lCounter << std::endl;
413  unsigned int iele=0;
414  //bool foundMaj = false;
415  for ( ; lIter != aFeMajVec.end(); ++lIter,++iele) {
416  //std::cout << " ---- Ele " << iele << " " << (*lIter).first << " " << (*lIter).second << " ref " << lMajAddr << std::endl;
417  if ((*lIter).second == lMajAddr) {
418  ++lCounter;
419  //std::cout << " ----- =ref: Counter = " << lCounter << std::endl;
420  }
421  else {
422  //std::cout << " ----- !=ref: Counter = " << lCounter << " Majority = " << aMajorityCounter << std::endl;
423  if (lCounter > aMajorityCounter) {
424  //std::cout << " ------ >Majority: " << std::endl;
425  aMajorityCounter = lCounter;
426  // AV bug here??
427  lMajAddress = lMajAddr;
428  // lMajAddress = (*lIter).second;
429  //foundMaj=true;
430  }
431  lCounter = 0;
432  lMajAddr = (*lIter).second;
433  --lIter;
434  --iele;
435  }
436  }
437  // AV Bug here? The check has to be done regardless foundMaj == false or true
438  // if (!foundMaj) {
439  if (lCounter > aMajorityCounter) {
440  //std::cout << " ------ >Majority: " << std::endl;
441  aMajorityCounter = lCounter;
442  lMajAddress = lMajAddr;
443  }
444  // }
445  //std::cout << " -- found majority value for " << aMajorityCounter << " elements out of " << aFeMajVec.size() << "." << std::endl;
446  //get list of feds with address different from majority in partition:
447  lIter = aFeMajVec.begin();
448  afedIds.reserve(135);
449  for ( ; lIter != aFeMajVec.end(); ++lIter ) {
450  if((*lIter).second != lMajAddress) {
451  afedIds.push_back((*lIter).first);
452  }
453  else {
454  lIter += aMajorityCounter-1;
455  if(lIter >= aFeMajVec.end()) {
456  std::cout << "Here it is a bug: " << aMajorityCounter << " " << aFeMajVec.size() << " " << lIter - aFeMajVec.end() << std::endl;
457  }
458  }
459  }
460  //std::cout << " -- Found " << lfedIds.size() << " elements not matching the majority." << std::endl;
461  if (afedIds.size()>0) {
462  std::sort(afedIds.begin(),afedIds.end());
463  std::vector<unsigned int>::iterator lIt = std::unique(afedIds.begin(),afedIds.end());
464  afedIds.erase(lIt,afedIds.end());
465  }
466 
467 }
468 
469 // ------------ method called once each job just before starting event loop ------------
470 void
472 {
473  //get DQM store
476 
477  //this propagates dqm_ to the histoclass, must be called !
478  fedHists_.bookTopLevelHistograms(dqm_);
479 
480  if (fillAllDetailedHistograms_) fedHists_.bookAllFEDHistograms();
481 
482  nEvt_ = 0;
483 
484  //const unsigned int siStripFedIdMin = FEDNumbering::MINSiStripFEDID;
485  //const unsigned int siStripFedIdMax = FEDNumbering::MAXSiStripFEDID;
486 
487  //mark all channels as inactive until they have been 'locked' at least once
488  // activeChannels_.resize(siStripFedIdMax+1);
489  // for (unsigned int fedId = siStripFedIdMin;
490  // fedId <= siStripFedIdMax;
491  // fedId++) {
492  // activeChannels_[fedId].resize(sistrip::FEDCH_PER_FED,false);
493  // }
494 
495 
496 
497 
498 }
499 
500 // ------------ method called once each job just after ending the event loop ------------
501 void
503 {
505 }
506 
507 
508 
509 void
511  const edm::EventSetup& context)
512 {
513 
514  fedErrors_.initialiseLumiBlock();
515 
516 }
517 
518 
519 void
521  const edm::EventSetup& context)
522 {
523  fedHists_.fillLumiHistograms(fedErrors_.getLumiErrors());
524 }
525 
526 
527 
528 
530 {
531  uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
532  if (cablingCacheId_ != currentCacheId) {
533  edm::ESHandle<SiStripFedCabling> cablingHandle;
534  eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
535  cabling_ = cablingHandle.product();
536  cablingCacheId_ = currentCacheId;
537  }
538 }
539 
540 
541 //
542 // Define as a plug-in
543 //
544 
EventNumber_t event() const
Definition: EventID.h:44
T getUntrackedParameter(std::string const &, T const &) const
edm::EDGetTokenT< FEDRawDataCollection > rawDataToken_
virtual void endJob() override
virtual void beginLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &context) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
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:243
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
SiStripFEDMonitorPlugin(const edm::ParameterSet &)
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", const uint32_t run=0, SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE")
Definition: DQMStore.cc:2296
#define end
Definition: vmac.h:37
int orbitNumber() const
Definition: EventBase.h:63
bool isValid() const
Definition: HandleBase.h:76
#define LogTrace(id)
bool failedToGet() const
Definition: HandleBase.h:80
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
virtual void beginJob() override
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
edm::EventID id() const
Definition: EventBase.h:56
#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:121
volatile std::atomic< bool > shutdown_flag false
edm::EDGetTokenT< EventWithHistory > heToken_
tuple size
Write out results.
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:584
void updateCabling(const edm::EventSetup &eventSetup)