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