File indexing completed on 2024-04-06 12:08:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <sstream>
0016 #include <memory>
0017 #include <list>
0018 #include <algorithm>
0019 #include <cassert>
0020
0021 #include "FWCore/Utilities/interface/EDGetToken.h"
0022 #include "FWCore/Framework/interface/Frameworkfwd.h"
0023 #include "FWCore/Framework/interface/Event.h"
0024 #include "FWCore/Framework/interface/EventSetup.h"
0025 #include "FWCore/Framework/interface/ESWatcher.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0027 #include "FWCore/Utilities/interface/InputTag.h"
0028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0029 #include "FWCore/ServiceRegistry/interface/Service.h"
0030 #include "FWCore/Utilities/interface/Exception.h"
0031
0032 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0033 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0034 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0035 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
0036 #include "DataFormats/DetId/interface/DetId.h"
0037
0038 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
0039 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0040
0041 #include "EventFilter/SiStripRawToDigi/interface/SiStripFEDBuffer.h"
0042
0043 #include "DQMServices/Core/interface/DQMStore.h"
0044
0045 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
0046 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
0047
0048 #include "DataFormats/Common/interface/DetSetVector.h"
0049 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
0050 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0051 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0052
0053 #include "DQM/SiStripMonitorHardware/interface/CMHistograms.hh"
0054
0055 #include <DQMServices/Core/interface/DQMEDAnalyzer.h>
0056
0057
0058
0059
0060
0061 class SiStripCMMonitorPlugin : public DQMEDAnalyzer {
0062 public:
0063 explicit SiStripCMMonitorPlugin(const edm::ParameterSet&);
0064 ~SiStripCMMonitorPlugin() override;
0065
0066 private:
0067 struct Statistics {
0068 float Mean;
0069 float Rms;
0070 float Counter;
0071 };
0072
0073 void analyze(const edm::Event&, const edm::EventSetup&) override;
0074 void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0075
0076 void updateCabling(const SiStripFedCablingRcd& cablingRcd);
0077
0078 void fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t, uint16_t> aMedians);
0079
0080
0081 edm::InputTag rawDataTag_;
0082 edm::EDGetTokenT<FEDRawDataCollection> rawDataToken_;
0083
0084 std::string folderName_;
0085
0086 std::vector<unsigned int> fedIdVec_;
0087
0088 bool fillAllDetailedHistograms_;
0089
0090 bool fillWithEvtNum_;
0091 bool fillWithLocalEvtNum_;
0092
0093 unsigned int printDebug_;
0094
0095 const SiStripFedCabling* cabling_;
0096
0097 edm::ESWatcher<SiStripFedCablingRcd> fedCablingWatcher_;
0098 edm::ESGetToken<SiStripFedCabling, SiStripFedCablingRcd> fedCablingToken_;
0099 edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken_;
0100 edm::ESGetToken<TkDetMap, TrackerTopologyRcd> tkDetMapToken_;
0101
0102
0103 bool doTkHistoMap_;
0104
0105 CMHistograms cmHists_;
0106
0107 std::map<unsigned int, Statistics> CommonModes_;
0108 std::map<unsigned int, Statistics> CommonModesAPV0minusAPV1_;
0109
0110 std::pair<uint16_t, uint16_t> prevMedians_[FEDNumbering::MAXSiStripFEDID + 1][sistrip::FEDCH_PER_FED];
0111
0112 edm::EventNumber_t evt_;
0113 };
0114
0115
0116
0117
0118
0119 SiStripCMMonitorPlugin::SiStripCMMonitorPlugin(const edm::ParameterSet& iConfig)
0120 : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag", edm::InputTag("source", ""))),
0121 folderName_(
0122 iConfig.getUntrackedParameter<std::string>("HistogramFolderName", "SiStrip/ReadoutView/CMMonitoring")),
0123 fedIdVec_(iConfig.getUntrackedParameter<std::vector<unsigned int> >("FedIdVec")),
0124 fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms", false)),
0125 fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber", false)),
0126 fillWithLocalEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithLocalEventNumber", false)),
0127 printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages", 1)),
0128 fedCablingWatcher_(this, &SiStripCMMonitorPlugin::updateCabling),
0129 fedCablingToken_(esConsumes<>()),
0130 tTopoToken_(esConsumes<>()),
0131 tkDetMapToken_(esConsumes<edm::Transition::BeginRun>()) {
0132 rawDataToken_ = consumes<FEDRawDataCollection>(rawDataTag_);
0133
0134 std::ostringstream debugStream;
0135 if (printDebug_ > 1) {
0136 debugStream << "[SiStripCMMonitorPlugin]Configuration for SiStripCMMonitorPlugin: " << std::endl
0137 << "[SiStripCMMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
0138 << "[SiStripCMMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
0139 << "[SiStripCMMonitorPlugin]\tFillAllDetailedHistograms? "
0140 << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
0141 << "[SiStripCMMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
0142 << "[SiStripCMMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl;
0143 }
0144
0145 std::ostringstream* pDebugStream = (printDebug_ > 1 ? &debugStream : nullptr);
0146
0147 cmHists_.initialise(iConfig, pDebugStream);
0148
0149 doTkHistoMap_ = cmHists_.tkHistoMapEnabled();
0150
0151 CommonModes_.clear();
0152 CommonModesAPV0minusAPV1_.clear();
0153
0154 for (unsigned int fedId(FEDNumbering::MINSiStripFEDID); fedId <= FEDNumbering::MAXSiStripFEDID; fedId++) {
0155 for (unsigned int iCh(0); iCh < sistrip::FEDCH_PER_FED; iCh++) {
0156 prevMedians_[fedId][iCh] = std::pair<uint16_t, uint16_t>(0, 0);
0157 }
0158 }
0159
0160 if (printDebug_)
0161 LogTrace("SiStripMonitorHardware") << debugStream.str();
0162
0163 evt_ = 0;
0164 }
0165
0166 SiStripCMMonitorPlugin::~SiStripCMMonitorPlugin() {}
0167
0168
0169
0170
0171
0172 void SiStripCMMonitorPlugin::bookHistograms(DQMStore::IBooker& ibooker,
0173 const edm::Run& run,
0174 const edm::EventSetup& eSetup) {
0175 ibooker.setCurrentFolder(folderName_);
0176
0177 const auto tkDetMap = &eSetup.getData(tkDetMapToken_);
0178 cmHists_.bookTopLevelHistograms(ibooker, tkDetMap);
0179
0180 if (fillAllDetailedHistograms_)
0181 cmHists_.bookAllFEDHistograms(ibooker);
0182 }
0183
0184
0185 void SiStripCMMonitorPlugin::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0186 const auto tTopo = &iSetup.getData(tTopoToken_);
0187
0188 fedCablingWatcher_.check(iSetup);
0189
0190
0191
0192
0193
0194 edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
0195 iEvent.getByToken(rawDataToken_, rawDataCollectionHandle);
0196 const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
0197
0198
0199 FEDErrors lFedErrors;
0200
0201
0202 for (unsigned int fedId = FEDNumbering::MINSiStripFEDID; fedId <= FEDNumbering::MAXSiStripFEDID;
0203 fedId++) {
0204 const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
0205
0206
0207 lFedErrors.initialiseFED(fedId, cabling_, tTopo);
0208
0209
0210
0211 bool lDataExist = lFedErrors.checkDataPresent(fedData);
0212 if (!lDataExist) {
0213 continue;
0214 }
0215
0216 std::unique_ptr<const sistrip::FEDBuffer> buffer;
0217
0218 if (!lFedErrors.fillFatalFEDErrors(fedData, 0)) {
0219 continue;
0220 } else {
0221
0222 const auto st_buffer = sistrip::preconstructCheckFEDBuffer(fedData, true);
0223 if (sistrip::FEDBufferStatusCode::SUCCESS != st_buffer) {
0224 throw cms::Exception("FEDBuffer") << st_buffer << " (check debug output for more details)";
0225 }
0226 auto tmp_buffer = std::make_unique<sistrip::FEDBuffer>(fedData, true);
0227 tmp_buffer->findChannels();
0228 buffer = std::move(tmp_buffer);
0229 bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
0230 bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
0231 bool feLengthsOK = buffer->checkFEUnitLengths();
0232 if (!channelLengthsOK || !channelPacketCodesOK || !feLengthsOK) {
0233 continue;
0234 }
0235 }
0236
0237 std::ostringstream infoStream;
0238
0239 if (printDebug_ > 1) {
0240 infoStream << " --- Processing FED #" << fedId << std::endl;
0241 }
0242
0243 std::vector<CMHistograms::CMvalues> values;
0244
0245 for (unsigned int iCh = 0; iCh < sistrip::FEDCH_PER_FED; iCh++) {
0246
0247 const FedChannelConnection& lConnection = cabling_->fedConnection(fedId, iCh);
0248 bool connected = lConnection.isConnected();
0249
0250
0251
0252 if (!connected) {
0253 continue;
0254 }
0255
0256 uint32_t lDetId = lConnection.detId();
0257 unsigned short nChInModule = lConnection.nApvPairs();
0258
0259 if (!lDetId || lDetId == sistrip::invalid32_)
0260 continue;
0261
0262 bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh, true) && connected;
0263
0264 if (lFailUnpackerChannelCheck) {
0265 continue;
0266 }
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292 std::ostringstream lMode;
0293 lMode << buffer->readoutMode();
0294 if (evt_ == 0 && printDebug_ > 1)
0295 std::cout << "Readout mode: " << lMode.str() << std::endl;
0296
0297 const sistrip::FEDChannel& lChannel = buffer->channel(iCh);
0298 std::pair<uint16_t, uint16_t> medians = std::pair<uint16_t, uint16_t>(0, 0);
0299
0300 if (lMode.str().find("Zero suppressed") != lMode.str().npos && lMode.str().find("lite") == lMode.str().npos)
0301 medians = std::pair<uint16_t, uint16_t>(lChannel.cmMedian(0), lChannel.cmMedian(1));
0302
0303 CMHistograms::CMvalues lVal;
0304 lVal.ChannelID = iCh;
0305 lVal.Medians = std::pair<uint16_t, uint16_t>(medians.first, medians.second);
0306 lVal.PreviousMedians = prevMedians_[fedId][iCh];
0307
0308
0309
0310
0311
0312
0313
0314
0315 if (printDebug_ > 1) {
0316 if (lChannel.length() > 7) {
0317 infoStream << "Medians for channel #" << iCh << " (length " << lChannel.length() << "): " << medians.first
0318 << ", " << medians.second << std::endl;
0319 }
0320 }
0321
0322 values.push_back(lVal);
0323
0324
0325 fillMaps(lDetId, nChInModule, medians);
0326
0327 prevMedians_[fedId][iCh] = std::pair<uint16_t, uint16_t>(medians.first, medians.second);
0328
0329 }
0330
0331 float lTime = 0;
0332 if (fillWithEvtNum_) {
0333
0334
0335 lTime = static_cast<float>(iEvent.id().event());
0336 } else {
0337 if (fillWithLocalEvtNum_) {
0338
0339
0340 lTime = static_cast<float>(evt_);
0341 } else {
0342 lTime = iEvent.orbitNumber() / 11223.;
0343 }
0344 }
0345
0346 cmHists_.fillHistograms(values, lTime, fedId);
0347
0348
0349
0350 }
0351
0352
0353
0354
0355 evt_++;
0356
0357 }
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420 void SiStripCMMonitorPlugin::updateCabling(const SiStripFedCablingRcd& cablingRcd) {
0421 cabling_ = &cablingRcd.get(fedCablingToken_);
0422 }
0423
0424 void SiStripCMMonitorPlugin::fillMaps(uint32_t aDetId,
0425 unsigned short aChInModule,
0426 std::pair<uint16_t, uint16_t> aMedians) {
0427 if (doTkHistoMap_) {
0428 std::pair<std::map<unsigned int, Statistics>::iterator, bool> alreadyThere[2];
0429
0430 Statistics lStat;
0431 lStat.Mean = (aMedians.first + aMedians.second) * 1. / (2 * aChInModule);
0432 lStat.Rms = (aMedians.first + aMedians.second) * (aMedians.first + aMedians.second) * 1. / (4 * aChInModule);
0433 lStat.Counter = 1. / aChInModule;
0434
0435 alreadyThere[0] = CommonModes_.insert(std::pair<unsigned int, Statistics>(aDetId, lStat));
0436 if (!alreadyThere[0].second) {
0437 ((alreadyThere[0].first)->second).Mean += (aMedians.first + aMedians.second) * 1. / (2 * aChInModule);
0438 ((alreadyThere[0].first)->second).Rms +=
0439 (aMedians.first + aMedians.second) * (aMedians.first + aMedians.second) * 1. / (4 * aChInModule);
0440 ((alreadyThere[0].first)->second).Counter += 1. / aChInModule;
0441 }
0442
0443 lStat.Mean = (aMedians.first - aMedians.second) * 1. / aChInModule;
0444 lStat.Rms = (aMedians.first - aMedians.second) * (aMedians.first - aMedians.second) * 1. / aChInModule;
0445 lStat.Counter = 1. / aChInModule;
0446
0447 alreadyThere[1] = CommonModesAPV0minusAPV1_.insert(std::pair<unsigned int, Statistics>(aDetId, lStat));
0448 if (!alreadyThere[1].second) {
0449 ((alreadyThere[1].first)->second).Mean += (aMedians.first - aMedians.second) * 1. / aChInModule;
0450 ((alreadyThere[1].first)->second).Rms +=
0451 (aMedians.first - aMedians.second) * (aMedians.first - aMedians.second) * 1. / aChInModule;
0452 ((alreadyThere[1].first)->second).Counter += 1. / aChInModule;
0453 }
0454 }
0455 }
0456
0457
0458
0459
0460
0461 #include "FWCore/Framework/interface/MakerMacros.h"
0462 DEFINE_FWK_MODULE(SiStripCMMonitorPlugin);