00001
00002
00003
00004
00005
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <sstream>
00022 #include <memory>
00023 #include <list>
00024 #include <algorithm>
00025 #include <cassert>
00026
00027 #include "FWCore/Framework/interface/Frameworkfwd.h"
00028 #include "FWCore/Framework/interface/EDAnalyzer.h"
00029 #include "FWCore/Framework/interface/Event.h"
00030 #include "FWCore/Framework/interface/EventSetup.h"
00031 #include "FWCore/Framework/interface/ESHandle.h"
00032 #include "FWCore/Framework/interface/LuminosityBlock.h"
00033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00034 #include "FWCore/Utilities/interface/InputTag.h"
00035 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00036 #include "FWCore/ServiceRegistry/interface/Service.h"
00037 #include "FWCore/Utilities/interface/Exception.h"
00038
00039 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00040 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00041 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00042 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
00043
00044 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
00045 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00046
00047 #include "DQMServices/Core/interface/DQMStore.h"
00048
00049 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
00050 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
00051
00052 #include "DPGAnalysis/SiStripTools/interface/EventWithHistory.h"
00053
00054
00055
00056
00057
00058
00059 class SiStripFEDMonitorPlugin : public edm::EDAnalyzer
00060 {
00061 public:
00062 explicit SiStripFEDMonitorPlugin(const edm::ParameterSet&);
00063 ~SiStripFEDMonitorPlugin();
00064 private:
00065 virtual void beginJob();
00066 virtual void analyze(const edm::Event&, const edm::EventSetup&);
00067 virtual void endJob();
00068 virtual void beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00069 const edm::EventSetup& context);
00070 virtual void endLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00071 const edm::EventSetup& context);
00072
00073
00074 void updateCabling(const edm::EventSetup& eventSetup);
00075
00076 static bool pairComparison(const std::pair<unsigned int, unsigned int> & pair1,
00077 const std::pair<unsigned int, unsigned int> & pair2);
00078
00079 void getMajority(const std::vector<std::pair<unsigned int,unsigned int> > & aFeMajVec,
00080 unsigned int & aMajorityCounter,
00081 std::vector<unsigned int> & afedIds);
00082
00083
00084 edm::InputTag rawDataTag_;
00085
00086 FEDHistograms fedHists_;
00087
00088 std::string folderName_;
00089
00090 bool fillAllDetailedHistograms_;
00091
00092 bool fillWithEvtNum_;
00093
00094 unsigned int printDebug_;
00095
00096
00097 bool writeDQMStore_;
00098 std::string dqmStoreFileName_;
00099
00100 DQMStore* dqm_;
00101
00102 uint32_t cablingCacheId_;
00103 const SiStripFedCabling* cabling_;
00104
00105
00106 bool doTkHistoMap_;
00107 bool doMedHists_;
00108 bool doFEMajorityCheck_;
00109
00110 unsigned int nEvt_;
00111
00112
00113
00114 FEDErrors fedErrors_;
00115 unsigned int maxFedBufferSize_;
00116 };
00117
00118
00119
00120
00121
00122
00123 SiStripFEDMonitorPlugin::SiStripFEDMonitorPlugin(const edm::ParameterSet& iConfig)
00124 : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
00125 folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","SiStrip/ReadoutView/FedSummary")),
00126 fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
00127 fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
00128 printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
00129
00130 writeDQMStore_(iConfig.getUntrackedParameter<bool>("WriteDQMStore",false)),
00131 dqmStoreFileName_(iConfig.getUntrackedParameter<std::string>("DQMStoreFileName","DQMStore.root")),
00132 dqm_(0),
00133 cablingCacheId_(0),
00134 maxFedBufferSize_(0)
00135 {
00136
00137 std::ostringstream debugStream;
00138 if (printDebug_>1) {
00139 debugStream << "[SiStripFEDMonitorPlugin]Configuration for SiStripFEDMonitorPlugin: " << std::endl
00140 << "[SiStripFEDMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
00141 << "[SiStripFEDMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
00142 << "[SiStripFEDMonitorPlugin]\tFillAllDetailedHistograms? " << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
00143 << "[SiStripFEDMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
00144 << "[SiStripFEDMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl
00145 << "[SiStripFEDMonitorPlugin]\tWriteDQMStore? " << (writeDQMStore_ ? "yes" : "no") << std::endl;
00146 if (writeDQMStore_) debugStream << "[SiStripFEDMonitorPlugin]\tDQMStoreFileName: " << dqmStoreFileName_ << std::endl;
00147 }
00148
00149
00150 std::ostringstream* pDebugStream = (printDebug_>1 ? &debugStream : NULL);
00151
00152 fedHists_.initialise(iConfig,pDebugStream);
00153
00154 doTkHistoMap_ = fedHists_.tkHistoMapEnabled();
00155
00156 doMedHists_ = fedHists_.cmHistosEnabled();
00157
00158 doFEMajorityCheck_ = fedHists_.feMajHistosEnabled();
00159
00160 if (printDebug_) {
00161 LogTrace("SiStripMonitorHardware") << debugStream.str();
00162
00163
00164
00165
00166
00167
00168
00169 }
00170
00171 nEvt_ = 0;
00172
00173 }
00174
00175 SiStripFEDMonitorPlugin::~SiStripFEDMonitorPlugin()
00176 {
00177 }
00178
00179
00180
00181
00182
00183
00184
00185 void
00186 SiStripFEDMonitorPlugin::analyze(const edm::Event& iEvent,
00187 const edm::EventSetup& iSetup)
00188 {
00189
00190 updateCabling(iSetup);
00191
00192
00193 edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
00194 iEvent.getByLabel(rawDataTag_,rawDataCollectionHandle);
00195 const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
00196
00197 fedErrors_.initialiseEvent();
00198
00199
00200
00201 edm::Handle<EventWithHistory> he;
00202 iEvent.getByLabel("consecutiveHEs",he);
00203
00204 if(he.isValid() && !he.failedToGet()) {
00205 fedErrors_.fillEventProperties(he->deltaBX());
00206 }
00207
00208
00209 std::map<unsigned int,std::pair<unsigned short,unsigned short> > badChannelFraction;
00210
00211 unsigned int lNFEDMonitoring = 0;
00212 unsigned int lNFEDUnpacker = 0;
00213 unsigned int lNChannelMonitoring = 0;
00214 unsigned int lNChannelUnpacker = 0;
00215
00216 unsigned int lNTotBadFeds = 0;
00217 unsigned int lNTotBadChannels = 0;
00218 unsigned int lNTotBadActiveChannels = 0;
00219
00220 std::vector<std::vector<std::pair<unsigned int,unsigned int> > > lFeMajFrac;
00221 const unsigned int nParts = 4;
00222 if (doFEMajorityCheck_){
00223 lFeMajFrac.resize(nParts);
00224
00225 lFeMajFrac[0].reserve(912);
00226 lFeMajFrac[1].reserve(1080);
00227 lFeMajFrac[2].reserve(768);
00228 lFeMajFrac[3].reserve(760);
00229 }
00230
00231 maxFedBufferSize_ = 0;
00232
00233
00234 for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
00235 fedId <= FEDNumbering::MAXSiStripFEDID;
00236 fedId++) {
00237 const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
00238
00239
00240 fedErrors_.initialiseFED(fedId,cabling_);
00241 bool lFullDebug = false;
00242
00243
00244
00245 bool lDataExist = fedErrors_.checkDataPresent(fedData);
00246 if (!lDataExist) {
00247 fedHists_.fillFEDHistograms(fedErrors_,0,lFullDebug);
00248 continue;
00249 }
00250
00251
00252
00253
00254 fedErrors_.fillFEDErrors(fedData,
00255 lFullDebug,
00256 printDebug_,
00257 lNChannelMonitoring,
00258 lNChannelUnpacker,
00259 doMedHists_,
00260 fedHists_.cmHistPointer(false),
00261 fedHists_.cmHistPointer(true),
00262 doFEMajorityCheck_,
00263 lFeMajFrac
00264 );
00265
00266
00267 bool lFailUnpackerFEDcheck = fedErrors_.failUnpackerFEDCheck();
00268
00269 fedErrors_.incrementFEDCounters();
00270 unsigned int lSize = fedData.size();
00271 if (lSize > maxFedBufferSize_){
00272 maxFedBufferSize_ = lSize;
00273 }
00274
00275
00276 fedHists_.fillFEDHistograms(fedErrors_,lSize,lFullDebug);
00277
00278 bool lFailMonitoringFEDcheck = fedErrors_.failMonitoringFEDCheck();
00279 if (lFailMonitoringFEDcheck) lNTotBadFeds++;
00280
00281
00282
00283
00284
00285 if (lFailMonitoringFEDcheck != lFailUnpackerFEDcheck) {
00286 if (printDebug_>1) {
00287 std::ostringstream debugStream;
00288 debugStream << " --- WARNING: FED " << fedId << std::endl
00289 << " ------ Monitoring FED check " ;
00290 if (lFailMonitoringFEDcheck) debugStream << "failed." << std::endl;
00291 else debugStream << "passed." << std::endl ;
00292 debugStream << " ------ Unpacker FED check " ;
00293 if (lFailUnpackerFEDcheck) debugStream << "failed." << std::endl;
00294 else debugStream << "passed." << std::endl ;
00295 edm::LogError("SiStripMonitorHardware") << debugStream.str();
00296 }
00297
00298 if (lFailMonitoringFEDcheck) lNFEDMonitoring++;
00299 else if (lFailUnpackerFEDcheck) lNFEDUnpacker++;
00300 }
00301
00302
00303
00304
00305
00306 if (doTkHistoMap_ && !fedHists_.tkHistoMapPointer()) {
00307 edm::LogWarning("SiStripMonitorHardware") << " -- Fedid " << fedId
00308 << ", TkHistoMap enabled but pointer is null." << std::endl;
00309 }
00310
00311 fedErrors_.fillBadChannelList(doTkHistoMap_,
00312 fedHists_.tkHistoMapPointer(),
00313 fedHists_.getFedvsAPVpointer(),
00314 lNTotBadChannels,
00315 lNTotBadActiveChannels);
00316 }
00317
00318
00319 if (doFEMajorityCheck_){
00320 for (unsigned int iP(0); iP<nParts; ++iP){
00321
00322
00323 if (lFeMajFrac[iP].size()==0) continue;
00324 std::sort(lFeMajFrac[iP].begin(),lFeMajFrac[iP].end(),SiStripFEDMonitorPlugin::pairComparison);
00325
00326 unsigned int lMajorityCounter = 0;
00327 std::vector<unsigned int> lfedIds;
00328
00329 getMajority(lFeMajFrac[iP],lMajorityCounter,lfedIds);
00330
00331 fedHists_.fillMajorityHistograms(iP,static_cast<float>(lMajorityCounter)/lFeMajFrac[iP].size(),lfedIds);
00332 }
00333 }
00334
00335 if ((lNTotBadFeds> 0 || lNTotBadChannels>0) && printDebug_>1) {
00336 std::ostringstream debugStream;
00337 debugStream << "[SiStripFEDMonitorPlugin] --- Total number of bad feds = "
00338 << lNTotBadFeds << std::endl
00339 << "[SiStripFEDMonitorPlugin] --- Total number of bad channels = "
00340 << lNTotBadChannels << std::endl
00341 << "[SiStripFEDMonitorPlugin] --- Total number of bad active channels = "
00342 << lNTotBadActiveChannels << std::endl;
00343 edm::LogInfo("SiStripMonitorHardware") << debugStream.str();
00344 }
00345
00346 if ((lNFEDMonitoring > 0 || lNFEDUnpacker > 0 || lNChannelMonitoring > 0 || lNChannelUnpacker > 0) && printDebug_) {
00347 std::ostringstream debugStream;
00348 debugStream
00349 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
00350 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
00351 << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at FED level : " << std::endl
00352 << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNFEDMonitoring << std::endl
00353 << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNFEDUnpacker << std::endl
00354 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
00355 << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at Channel level : " << std::endl
00356 << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNChannelMonitoring << std::endl
00357 << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNChannelUnpacker << std::endl
00358 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
00359 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl ;
00360 edm::LogError("SiStripMonitorHardware") << debugStream.str();
00361
00362 }
00363
00364 FEDErrors::getFEDErrorsCounters().nTotalBadChannels = lNTotBadChannels;
00365 FEDErrors::getFEDErrorsCounters().nTotalBadActiveChannels = lNTotBadActiveChannels;
00366
00367
00368
00369 if (fillWithEvtNum_) fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(),FEDErrors::getChannelErrorsCounters(),maxFedBufferSize_,iEvent.id().event());
00370 else fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(),FEDErrors::getChannelErrorsCounters(),maxFedBufferSize_,iEvent.orbitNumber()/11223.);
00371
00372 nEvt_++;
00373
00374 }
00375
00376
00377 bool SiStripFEDMonitorPlugin::pairComparison(const std::pair<unsigned int, unsigned int> & pair1,
00378 const std::pair<unsigned int, unsigned int> & pair2){
00379 return (pair1.second < pair2.second) ;
00380 }
00381
00382 void SiStripFEDMonitorPlugin::getMajority(const std::vector<std::pair<unsigned int,unsigned int> > & aFeMajVec,
00383 unsigned int & aMajorityCounter,
00384 std::vector<unsigned int> & afedIds) {
00385
00386
00387 unsigned int lMajAddress = 0;
00388 std::vector<std::pair<unsigned int,unsigned int> >::const_iterator lIter = aFeMajVec.begin();
00389 unsigned int lMajAddr = (*lIter).second;
00390 unsigned int lCounter = 0;
00391
00392
00393 unsigned int iele=0;
00394
00395 for ( ; lIter != aFeMajVec.end(); ++lIter,++iele) {
00396
00397 if ((*lIter).second == lMajAddr) {
00398 ++lCounter;
00399
00400 }
00401 else {
00402
00403 if (lCounter > aMajorityCounter) {
00404
00405 aMajorityCounter = lCounter;
00406
00407 lMajAddress = lMajAddr;
00408
00409
00410 }
00411 lCounter = 0;
00412 lMajAddr = (*lIter).second;
00413 --lIter;
00414 --iele;
00415 }
00416 }
00417
00418
00419 if (lCounter > aMajorityCounter) {
00420
00421 aMajorityCounter = lCounter;
00422 lMajAddress = lMajAddr;
00423 }
00424
00425
00426
00427 lIter = aFeMajVec.begin();
00428 afedIds.reserve(135);
00429 for ( ; lIter != aFeMajVec.end(); ++lIter ) {
00430 if((*lIter).second != lMajAddress) {
00431 afedIds.push_back((*lIter).first);
00432 }
00433 else {
00434 lIter += aMajorityCounter-1;
00435 if(lIter >= aFeMajVec.end()) {
00436 std::cout << "Here it is a bug: " << aMajorityCounter << " " << aFeMajVec.size() << " " << lIter - aFeMajVec.end() << std::endl;
00437 }
00438 }
00439 }
00440
00441 if (afedIds.size()>0) {
00442 std::sort(afedIds.begin(),afedIds.end());
00443 std::vector<unsigned int>::iterator lIt = std::unique(afedIds.begin(),afedIds.end());
00444 afedIds.erase(lIt,afedIds.end());
00445 }
00446
00447 }
00448
00449
00450 void
00451 SiStripFEDMonitorPlugin::beginJob()
00452 {
00453
00454 dqm_ = &(*edm::Service<DQMStore>());
00455 dqm_->setCurrentFolder(folderName_);
00456
00457
00458 fedHists_.bookTopLevelHistograms(dqm_);
00459
00460 if (fillAllDetailedHistograms_) fedHists_.bookAllFEDHistograms();
00461
00462 nEvt_ = 0;
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478 }
00479
00480
00481 void
00482 SiStripFEDMonitorPlugin::endJob()
00483 {
00484 if (writeDQMStore_) dqm_->save(dqmStoreFileName_);
00485 }
00486
00487
00488
00489 void
00490 SiStripFEDMonitorPlugin::beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00491 const edm::EventSetup& context)
00492 {
00493
00494 fedErrors_.initialiseLumiBlock();
00495
00496 }
00497
00498
00499 void
00500 SiStripFEDMonitorPlugin::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00501 const edm::EventSetup& context)
00502 {
00503 fedHists_.fillLumiHistograms(fedErrors_.getLumiErrors());
00504 }
00505
00506
00507
00508
00509 void SiStripFEDMonitorPlugin::updateCabling(const edm::EventSetup& eventSetup)
00510 {
00511 uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
00512 if (cablingCacheId_ != currentCacheId) {
00513 edm::ESHandle<SiStripFedCabling> cablingHandle;
00514 eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
00515 cabling_ = cablingHandle.product();
00516 cablingCacheId_ = currentCacheId;
00517 }
00518 }
00519
00520
00521
00522
00523
00524
00525 #include "FWCore/Framework/interface/MakerMacros.h"
00526 DEFINE_FWK_MODULE(SiStripFEDMonitorPlugin);