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 lNTotBadChannels,
00303 lNTotBadActiveChannels);
00304 }
00305
00306
00307 if (doFEMajorityCheck_){
00308 for (unsigned int iP(0); iP<nParts; ++iP){
00309
00310
00311 if (lFeMajFrac[iP].size()==0) continue;
00312 std::sort(lFeMajFrac[iP].begin(),lFeMajFrac[iP].end(),SiStripFEDMonitorPlugin::pairComparison);
00313
00314 unsigned int lMajorityCounter = 0;
00315 std::vector<unsigned int> lfedIds;
00316
00317 getMajority(lFeMajFrac[iP],lMajorityCounter,lfedIds);
00318
00319 fedHists_.fillMajorityHistograms(iP,static_cast<float>(lMajorityCounter)/lFeMajFrac[iP].size(),lfedIds);
00320 }
00321 }
00322
00323 if ((lNTotBadFeds> 0 || lNTotBadChannels>0) && printDebug_>1) {
00324 std::ostringstream debugStream;
00325 debugStream << "[SiStripFEDMonitorPlugin] --- Total number of bad feds = "
00326 << lNTotBadFeds << std::endl
00327 << "[SiStripFEDMonitorPlugin] --- Total number of bad channels = "
00328 << lNTotBadChannels << std::endl
00329 << "[SiStripFEDMonitorPlugin] --- Total number of bad active channels = "
00330 << lNTotBadActiveChannels << std::endl;
00331 edm::LogInfo("SiStripMonitorHardware") << debugStream.str();
00332 }
00333
00334 if ((lNFEDMonitoring > 0 || lNFEDUnpacker > 0 || lNChannelMonitoring > 0 || lNChannelUnpacker > 0) && printDebug_) {
00335 std::ostringstream debugStream;
00336 debugStream
00337 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
00338 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
00339 << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at FED level : " << std::endl
00340 << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNFEDMonitoring << std::endl
00341 << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNFEDUnpacker << std::endl
00342 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
00343 << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at Channel level : " << std::endl
00344 << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNChannelMonitoring << std::endl
00345 << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNChannelUnpacker << std::endl
00346 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl
00347 << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl ;
00348 edm::LogError("SiStripMonitorHardware") << debugStream.str();
00349
00350 }
00351
00352 FEDErrors::getFEDErrorsCounters().nTotalBadChannels = lNTotBadChannels;
00353 FEDErrors::getFEDErrorsCounters().nTotalBadActiveChannels = lNTotBadActiveChannels;
00354
00355
00356
00357 if (fillWithEvtNum_) fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(),FEDErrors::getChannelErrorsCounters(),maxFedBufferSize_,iEvent.id().event());
00358 else fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(),FEDErrors::getChannelErrorsCounters(),maxFedBufferSize_,iEvent.orbitNumber()/11223.);
00359
00360 nEvt_++;
00361
00362 }
00363
00364
00365 bool SiStripFEDMonitorPlugin::pairComparison(const std::pair<unsigned int, unsigned int> & pair1,
00366 const std::pair<unsigned int, unsigned int> & pair2){
00367 return (pair1.second < pair2.second) ;
00368 }
00369
00370 void SiStripFEDMonitorPlugin::getMajority(const std::vector<std::pair<unsigned int,unsigned int> > & aFeMajVec,
00371 unsigned int & aMajorityCounter,
00372 std::vector<unsigned int> & afedIds) {
00373
00374
00375 unsigned int lMajAddress = 0;
00376 std::vector<std::pair<unsigned int,unsigned int> >::const_iterator lIter = aFeMajVec.begin();
00377 unsigned int lMajAddr = (*lIter).second;
00378 unsigned int lCounter = 0;
00379
00380
00381 unsigned int iele=0;
00382 bool foundMaj = false;
00383 for ( ; lIter != aFeMajVec.end(); ++lIter,++iele) {
00384
00385 if ((*lIter).second == lMajAddr) {
00386 ++lCounter;
00387
00388 }
00389 else {
00390
00391 if (lCounter > aMajorityCounter) {
00392
00393 aMajorityCounter = lCounter;
00394 lMajAddress = (*lIter).second;
00395 foundMaj=true;
00396 }
00397 lCounter = 0;
00398 lMajAddr = (*lIter).second;
00399 --lIter;
00400 --iele;
00401 }
00402 }
00403 if (!foundMaj) {
00404 if (lCounter > aMajorityCounter) {
00405
00406 aMajorityCounter = lCounter;
00407 lMajAddress = lMajAddr;
00408 }
00409 }
00410
00411
00412 lIter = aFeMajVec.begin();
00413 afedIds.reserve(135);
00414 for ( ; lIter != aFeMajVec.end(); ++lIter ) {
00415 if((*lIter).second != lMajAddress) {
00416 afedIds.push_back((*lIter).first);
00417 }
00418 else {
00419 lIter += aMajorityCounter-1;
00420 }
00421 }
00422
00423 if (afedIds.size()>0) {
00424 std::sort(afedIds.begin(),afedIds.end());
00425 std::vector<unsigned int>::iterator lIt = std::unique(afedIds.begin(),afedIds.end());
00426 afedIds.erase(lIt,afedIds.end());
00427 }
00428
00429 }
00430
00431
00432 void
00433 SiStripFEDMonitorPlugin::beginJob()
00434 {
00435
00436 dqm_ = &(*edm::Service<DQMStore>());
00437 dqm_->setCurrentFolder(folderName_);
00438
00439
00440 fedHists_.bookTopLevelHistograms(dqm_);
00441
00442 if (fillAllDetailedHistograms_) fedHists_.bookAllFEDHistograms();
00443
00444 nEvt_ = 0;
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 }
00461
00462
00463 void
00464 SiStripFEDMonitorPlugin::endJob()
00465 {
00466 if (writeDQMStore_) dqm_->save(dqmStoreFileName_);
00467 }
00468
00469
00470
00471 void
00472 SiStripFEDMonitorPlugin::beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00473 const edm::EventSetup& context)
00474 {
00475
00476 fedErrors_.initialiseLumiBlock();
00477
00478 }
00479
00480
00481 void
00482 SiStripFEDMonitorPlugin::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg,
00483 const edm::EventSetup& context)
00484 {
00485 fedHists_.fillLumiHistograms(fedErrors_.getLumiErrors());
00486 }
00487
00488
00489
00490
00491 void SiStripFEDMonitorPlugin::updateCabling(const edm::EventSetup& eventSetup)
00492 {
00493 uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
00494 if (cablingCacheId_ != currentCacheId) {
00495 edm::ESHandle<SiStripFedCabling> cablingHandle;
00496 eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
00497 cabling_ = cablingHandle.product();
00498 cablingCacheId_ = currentCacheId;
00499 }
00500 }
00501
00502
00503
00504
00505
00506
00507 #include "FWCore/Framework/interface/MakerMacros.h"
00508 DEFINE_FWK_MODULE(SiStripFEDMonitorPlugin);