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