00001
00002
00003
00004
00005
00006
00011
00012
00013
00014
00015
00016 #include <sstream>
00017 #include <memory>
00018 #include <list>
00019 #include <algorithm>
00020 #include <cassert>
00021
00022 #include "FWCore/Framework/interface/Frameworkfwd.h"
00023 #include "FWCore/Framework/interface/EDAnalyzer.h"
00024 #include "FWCore/Framework/interface/Event.h"
00025 #include "FWCore/Framework/interface/EventSetup.h"
00026 #include "FWCore/Framework/interface/ESHandle.h"
00027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00028 #include "FWCore/Utilities/interface/InputTag.h"
00029 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00030 #include "FWCore/ServiceRegistry/interface/Service.h"
00031 #include "FWCore/Utilities/interface/Exception.h"
00032
00033 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00034 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00035 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00036 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
00037 #include "DataFormats/DetId/interface/DetId.h"
00038
00039 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
00040 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00041
00042 #include "EventFilter/SiStripRawToDigi/interface/SiStripFEDBuffer.h"
00043
00044 #include "DQMServices/Core/interface/DQMStore.h"
00045 #include "DQMServices/Core/interface/MonitorElement.h"
00046
00047 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
00048 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
00049
00050 #include "DataFormats/Common/interface/DetSetVector.h"
00051 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
00052 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00053 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00054 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00055 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00056
00057 #include "DQM/SiStripMonitorHardware/interface/CMHistograms.hh"
00058
00059
00060
00061
00062
00063 class SiStripCMMonitorPlugin : public edm::EDAnalyzer
00064 {
00065 public:
00066
00067 explicit SiStripCMMonitorPlugin(const edm::ParameterSet&);
00068 ~SiStripCMMonitorPlugin();
00069 private:
00070
00071 struct Statistics {
00072 double Mean;
00073 double Rms;
00074 double Counter;
00075 };
00076
00077 virtual void beginJob();
00078 virtual void analyze(const edm::Event&, const edm::EventSetup&);
00079 virtual void endJob();
00080
00081
00082 void updateCabling(const edm::EventSetup& eventSetup);
00083
00084
00085 void fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians);
00086
00087
00088 edm::InputTag rawDataTag_;
00089
00090 std::string folderName_;
00091
00092 std::vector<unsigned int> fedIdVec_;
00093
00094 bool fillAllDetailedHistograms_;
00095
00096 bool fillWithEvtNum_;
00097 bool fillWithLocalEvtNum_;
00098
00099 unsigned int printDebug_;
00100
00101 bool writeDQMStore_;
00102 std::string dqmStoreFileName_;
00103
00104 DQMStore* dqm_;
00105
00106 uint32_t cablingCacheId_;
00107 const SiStripFedCabling* cabling_;
00108
00109
00110 bool doTkHistoMap_;
00111
00112 CMHistograms cmHists_;
00113
00114 std::map<unsigned int,Statistics> CommonModes_;
00115 std::map<unsigned int,Statistics> CommonModesAPV0minusAPV1_;
00116
00117 std::pair<uint16_t,uint16_t> prevMedians_[FEDNumbering::MAXSiStripFEDID+1][sistrip::FEDCH_PER_FED];
00118
00119 unsigned int evt_;
00120
00121 };
00122
00123
00124
00125
00126
00127
00128 SiStripCMMonitorPlugin::SiStripCMMonitorPlugin(const edm::ParameterSet& iConfig)
00129 : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
00130 folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","SiStrip/ReadoutView/CMMonitoring")),
00131 fedIdVec_(iConfig.getUntrackedParameter<std::vector<unsigned int> >("FedIdVec")),
00132 fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
00133 fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
00134 fillWithLocalEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithLocalEventNumber",false)),
00135 printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
00136 writeDQMStore_(iConfig.getUntrackedParameter<bool>("WriteDQMStore",false)),
00137 dqmStoreFileName_(iConfig.getUntrackedParameter<std::string>("DQMStoreFileName","DQMStore.root")),
00138 dqm_(0),
00139 cablingCacheId_(0)
00140
00141 {
00142
00143 std::ostringstream debugStream;
00144 if (printDebug_>1) {
00145 debugStream << "[SiStripCMMonitorPlugin]Configuration for SiStripCMMonitorPlugin: " << std::endl
00146 << "[SiStripCMMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
00147 << "[SiStripCMMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
00148 << "[SiStripCMMonitorPlugin]\tFillAllDetailedHistograms? " << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
00149 << "[SiStripCMMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
00150 << "[SiStripCMMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl
00151 << "[SiStripCMMonitorPlugin]\tWriteDQMStore? " << (writeDQMStore_ ? "yes" : "no") << std::endl;
00152 if (writeDQMStore_) debugStream << "[SiStripCMMonitorPlugin]\tDQMStoreFileName: " << dqmStoreFileName_ << std::endl;
00153 }
00154
00155 std::ostringstream* pDebugStream = (printDebug_>1 ? &debugStream : NULL);
00156
00157 cmHists_.initialise(iConfig,pDebugStream);
00158
00159 doTkHistoMap_ = cmHists_.tkHistoMapEnabled();
00160
00161 CommonModes_.clear();
00162 CommonModesAPV0minusAPV1_.clear();
00163
00164 for (unsigned int fedId(FEDNumbering::MINSiStripFEDID); fedId <= FEDNumbering::MAXSiStripFEDID; fedId++){
00165 for (unsigned int iCh(0); iCh<sistrip::FEDCH_PER_FED; iCh++){
00166 prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(0,0);
00167 }
00168 }
00169
00170
00171 if (printDebug_) {
00172 LogTrace("SiStripMonitorHardware") << debugStream.str();
00173 }
00174
00175 evt_ = 0;
00176
00177 }
00178
00179 SiStripCMMonitorPlugin::~SiStripCMMonitorPlugin()
00180 {
00181 }
00182
00183
00184
00185
00186
00187
00188
00189 void
00190 SiStripCMMonitorPlugin::analyze(const edm::Event& iEvent,
00191 const edm::EventSetup& iSetup)
00192 {
00193
00194
00195
00196
00197 updateCabling(iSetup);
00198
00199
00200 edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
00201 iEvent.getByLabel(rawDataTag_,rawDataCollectionHandle);
00202 const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
00203
00204
00205 FEDErrors lFedErrors;
00206
00207
00208 for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
00209 fedId <= FEDNumbering::MAXSiStripFEDID;
00210 fedId++) {
00211 const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
00212
00213
00214 lFedErrors.initialiseFED(fedId,cabling_);
00215
00216
00217
00218 bool lDataExist = lFedErrors.checkDataPresent(fedData);
00219 if (!lDataExist) {
00220 continue;
00221 }
00222
00223 std::auto_ptr<const sistrip::FEDBuffer> buffer;
00224
00225 if (!lFedErrors.fillFatalFEDErrors(fedData,0)) {
00226 continue;
00227 }
00228 else {
00229
00230 buffer.reset(new sistrip::FEDBuffer(fedData.data(),fedData.size(),true));
00231 bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
00232 bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
00233 bool feLengthsOK = buffer->checkFEUnitLengths();
00234 if ( !channelLengthsOK ||
00235 !channelPacketCodesOK ||
00236 !feLengthsOK ) {
00237 continue;
00238 }
00239 }
00240
00241 std::ostringstream infoStream;
00242
00243
00244 if (printDebug_ > 1) {
00245 infoStream << " --- Processing FED #" << fedId << std::endl;
00246 }
00247
00248
00249 std::vector<CMHistograms::CMvalues> values;
00250
00251 for (unsigned int iCh = 0;
00252 iCh < sistrip::FEDCH_PER_FED;
00253 iCh++) {
00254
00255 const FedChannelConnection & lConnection = cabling_->connection(fedId,iCh);
00256 bool connected = lConnection.isConnected();
00257
00258
00259
00260 if (!connected) {
00261 continue;
00262 }
00263
00264 uint32_t lDetId = lConnection.detId();
00265 unsigned short nChInModule = lConnection.nApvPairs();
00266
00267 if (!lDetId || lDetId == sistrip::invalid32_) continue;
00268
00269 bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh) && connected;
00270
00271 if (lFailUnpackerChannelCheck) {
00272 continue;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 std::ostringstream lMode;
00301 lMode << buffer->readoutMode();
00302 if (printDebug_ > 1) {
00303 static bool lFirst = true;
00304 if (lFirst) {
00305 std::cout << "Readout mode: " << lMode.str() << std::endl;
00306 lFirst = false;
00307 }
00308
00309 }
00310
00311 const sistrip::FEDChannel & lChannel = buffer->channel(iCh);
00312 std::pair<uint16_t,uint16_t> medians = std::pair<uint16_t,uint16_t>(0,0);
00313
00314 if (lMode.str().find("Zero suppressed") != lMode.str().npos && lMode.str().find("lite") == lMode.str().npos) medians = std::pair<uint16_t,uint16_t>(lChannel.cmMedian(0),lChannel.cmMedian(1));
00315
00316 CMHistograms::CMvalues lVal;
00317 lVal.ChannelID = iCh;
00318 lVal.Medians = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
00319 lVal.PreviousMedians = prevMedians_[fedId][iCh];
00320
00321
00322
00323
00324
00325
00326
00327
00328 if (printDebug_ > 1) {
00329 if (lChannel.length() > 7) {
00330 infoStream << "Medians for channel #" << iCh << " (length " << lChannel.length() << "): " << medians.first << ", " << medians.second << std::endl;
00331 }
00332 }
00333
00334 values.push_back(lVal);
00335
00336
00337 fillMaps(lDetId,nChInModule,medians);
00338
00339 prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
00340
00341 }
00342
00343 float lTime = 0;
00344 if (fillWithEvtNum_) lTime = iEvent.id().event();
00345 else if (fillWithLocalEvtNum_) lTime = evt_;
00346 else lTime = iEvent.orbitNumber()/11223.;
00347
00348 cmHists_.fillHistograms(values,lTime,fedId);
00349
00350
00351
00352
00353
00354 }
00355
00356
00357
00358
00359
00360 evt_++;
00361
00362 }
00363
00364
00365 void
00366 SiStripCMMonitorPlugin::beginJob()
00367 {
00368
00369 dqm_ = &(*edm::Service<DQMStore>());
00370 dqm_->setCurrentFolder(folderName_);
00371
00372 cmHists_.bookTopLevelHistograms(dqm_);
00373
00374 if (fillAllDetailedHistograms_) cmHists_.bookAllFEDHistograms();
00375
00376 }
00377
00378
00379 void
00380 SiStripCMMonitorPlugin::endJob()
00381 {
00382
00383 if (doTkHistoMap_) {
00384 std::map<unsigned int,Statistics>::iterator fracIter;
00385
00386
00387
00388 for (fracIter = CommonModes_.begin(); fracIter!=CommonModes_.end(); fracIter++){
00389 uint32_t detid = fracIter->first;
00390
00391
00392
00393
00394 float mean = 0;
00395 float rms = 0;
00396 Statistics lStat = fracIter->second;
00397 if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
00398 if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
00399 cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(0),detid,mean);
00400 cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(1),detid,rms);
00401
00402 if (printDebug_ > 1) {
00403 std::ostringstream message;
00404 message << "TkHistoMap CM: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
00405 edm::LogVerbatim("SiStripMonitorHardware") << message.str();
00406 }
00407
00408
00409 }
00410
00411 for (fracIter = CommonModesAPV0minusAPV1_.begin(); fracIter!=CommonModesAPV0minusAPV1_.end(); fracIter++){
00412 uint32_t detid = fracIter->first;
00413
00414
00415
00416
00417 float mean = 0;
00418 float rms = 0;
00419 Statistics lStat = fracIter->second;
00420 if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
00421 if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
00422 cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(2),detid,mean);
00423 cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(3),detid,rms);
00424
00425 if (printDebug_ > 1) {
00426 std::ostringstream message;
00427 message << "TkHistoMap APV0minusAPV1: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
00428 edm::LogVerbatim("SiStripMonitorHardware") << message.str();
00429 }
00430
00431
00432 }
00433
00434 }
00435
00436 if (writeDQMStore_) dqm_->save(dqmStoreFileName_);
00437 }
00438
00439 void SiStripCMMonitorPlugin::updateCabling(const edm::EventSetup& eventSetup)
00440 {
00441 uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
00442 if (cablingCacheId_ != currentCacheId) {
00443 edm::ESHandle<SiStripFedCabling> cablingHandle;
00444 eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
00445 cabling_ = cablingHandle.product();
00446 cablingCacheId_ = currentCacheId;
00447 }
00448 }
00449
00450
00451 void SiStripCMMonitorPlugin::fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians)
00452 {
00453
00454 if (doTkHistoMap_){
00455 std::pair<std::map<unsigned int,Statistics>::iterator,bool> alreadyThere[2];
00456
00457 Statistics lStat;
00458 lStat.Mean = (aMedians.first+aMedians.second)*1./(2*aChInModule);
00459 lStat.Rms = (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
00460 lStat.Counter = 1./aChInModule;
00461
00462 alreadyThere[0] = CommonModes_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
00463 if (!alreadyThere[0].second) {
00464 ((alreadyThere[0].first)->second).Mean += (aMedians.first+aMedians.second)*1./(2*aChInModule);
00465 ((alreadyThere[0].first)->second).Rms += (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
00466 ((alreadyThere[0].first)->second).Counter += 1./aChInModule;
00467 }
00468
00469 lStat.Mean = (aMedians.first-aMedians.second)*1./aChInModule;
00470 lStat.Rms = (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
00471 lStat.Counter = 1./aChInModule;
00472
00473 alreadyThere[1] = CommonModesAPV0minusAPV1_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
00474 if (!alreadyThere[1].second) {
00475 ((alreadyThere[1].first)->second).Mean += (aMedians.first-aMedians.second)*1./aChInModule;
00476 ((alreadyThere[1].first)->second).Rms += (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
00477 ((alreadyThere[1].first)->second).Counter += 1./aChInModule;
00478 }
00479
00480 }
00481
00482 }
00483
00484
00485
00486
00487
00488
00489 #include "FWCore/Framework/interface/MakerMacros.h"
00490 DEFINE_FWK_MODULE(SiStripCMMonitorPlugin);