Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:11:57

0001 
0002 // -*- C++ -*-
0003 //
0004 // Package:    DQM/SiStripMonitorHardware
0005 // Class:      SiStripCMMonitorPlugin
0006 //
0007 /**\class SiStripCMMonitorPlugin SiStripCMMonitor.cc DQM/SiStripMonitorHardware/plugins/SiStripCMMonitor.cc
0008 
0009  Description: DQM source application to monitor common mode for SiStrip data
0010 */
0011 //
0012 //         Created:  2009/07/22
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 // Class declaration
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   //update the cabling if necessary
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   //tag of FEDRawData collection
0081   edm::InputTag rawDataTag_;
0082   edm::EDGetTokenT<FEDRawDataCollection> rawDataToken_;
0083   //folder name for histograms in DQMStore
0084   std::string folderName_;
0085   //vector of fedIDs which will have detailed histograms made
0086   std::vector<unsigned int> fedIdVec_;
0087   //book detailed histograms even if they will be empty (for merging)
0088   bool fillAllDetailedHistograms_;
0089   //do histos vs time with time=event number. Default time = orbit number (s)
0090   bool fillWithEvtNum_;
0091   bool fillWithLocalEvtNum_;
0092   //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
0093   unsigned int printDebug_;
0094   //FED cabling
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   //add parameter to save computing time if TkHistoMap are not filled
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 // Constructors and destructor
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   //print config to debug log
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 // Member functions
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 // ------------ method called to for each event  ------------
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   //static bool firstEvent = true;
0191   //static bool isBeingFilled = false;
0192 
0193   //get raw data
0194   edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
0195   iEvent.getByToken(rawDataToken_, rawDataCollectionHandle);
0196   const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
0197 
0198   //FED errors
0199   FEDErrors lFedErrors;
0200 
0201   //loop over siStrip FED IDs
0202   for (unsigned int fedId = FEDNumbering::MINSiStripFEDID; fedId <= FEDNumbering::MAXSiStripFEDID;
0203        fedId++) {  //loop over FED IDs
0204     const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
0205 
0206     //create an object to fill all errors
0207     lFedErrors.initialiseFED(fedId, cabling_, tTopo);
0208 
0209     //Do detailed check
0210     //first check if data exists
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       //need to construct full object to go any further
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);  // const now
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++) {  //loop on channels
0246 
0247       const FedChannelConnection& lConnection = cabling_->fedConnection(fedId, iCh);
0248       bool connected = lConnection.isConnected();
0249 
0250       //std::cout << "FedID " << fedId << ", ch " << iCh << ", nAPVPairs " << lConnection.nApvPairs() << " apvPairNumber " << lConnection.apvPairNumber() << std::endl;
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       //short lAPVPair = lConnection.apvPairNumber();
0269       //short lSubDet = DetId(lDetId).subdetId();
0270 
0271       //       if (firstEvent){
0272       //    infoStream << "Subdet " << lSubDet << ", " ;
0273       //    if (lSubDet == 3) {
0274       //
0275       //      infoStream << "TIB layer " << tTopo->tibLayer(lDetId)  << ", fedID " << fedId << ", channel " << iCh << std::endl;
0276       //    }
0277       //    else if (lSubDet == 4) {
0278       //
0279       //      infoStream << "TID side " << tTopo->tibSide(lDetId)  << " wheel " << tTopo->tibWheel(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
0280       //    }
0281       //    else if (lSubDet == 5) {
0282       //
0283       //      infoStream << "TOB side " << tTopo->tibRod(lDetId)[0]  << " layer " << tTopo->tibLayer(lDetId) << ", rod " << tTopo->tibRodNumber(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
0284       //    }
0285       //    else if (lSubDet == 6) {
0286       //
0287       //      infoStream << "TEC side " << tTopo->tibSide(lDetId)  << " wheel " << tTopo->tibWheel(lDetId) << ", petal " << tTopo->tibPetalNumber(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
0288       //    }
0289       //    isBeingFilled=true;
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       //       if (medians.second-medians.first > 26){
0309       //    std::ostringstream info;
0310       //    if (medians.second-medians.first > 44) info << " --- Second bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
0311       //    else info << " --- First bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
0312       //    edm::LogVerbatim("SiStripMonitorHardware") << info.str();
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       //if (iEvent.id().event() > 1000)
0325       fillMaps(lDetId, nChInModule, medians);
0326 
0327       prevMedians_[fedId][iCh] = std::pair<uint16_t, uint16_t>(medians.first, medians.second);
0328 
0329     }  //loop on channels
0330 
0331     float lTime = 0;
0332     if (fillWithEvtNum_) {
0333       // casting from unsigned long long to a float here
0334       // doing it explicitely
0335       lTime = static_cast<float>(iEvent.id().event());
0336     } else {
0337       if (fillWithLocalEvtNum_) {
0338         // casting from unsigned long long to a float here
0339         // doing it explicitely
0340         lTime = static_cast<float>(evt_);
0341       } else {
0342         lTime = iEvent.orbitNumber() / 11223.;
0343       }
0344     }
0345 
0346     cmHists_.fillHistograms(values, lTime, fedId);
0347 
0348     //if (printDebug_ > 0 && isBeingFilled && firstEvent) edm::LogVerbatim("SiStripMonitorHardware") << infoStream.str();
0349 
0350   }  //loop on FEDs
0351 
0352   //if (isBeingFilled)
0353   //firstEvent = false;
0354 
0355   evt_++;
0356 
0357 }  //analyze method
0358 
0359 // ------------ method called once each job just after ending the event loop  ------------
0360 /* //to be moved to harvesting step
0361 void 
0362 SiStripCMMonitorPlugin::endJob()
0363 {
0364 
0365   if (doTkHistoMap_) {//if TkHistoMap is enabled
0366     std::map<unsigned int,Statistics>::iterator fracIter;
0367 
0368     //int ele = 0;
0369     //int nBadChannels = 0;
0370     for (fracIter = CommonModes_.begin(); fracIter!=CommonModes_.end(); fracIter++){
0371       uint32_t detid = fracIter->first;
0372       //if ((fracIter->second).second != 0) {
0373       //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
0374       //nBadChannels++;
0375       //}
0376       float mean = 0;
0377       float rms = 0;
0378       Statistics lStat = fracIter->second;
0379       if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
0380       if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
0381       cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(0),detid,mean);
0382       cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(1),detid,rms);
0383 
0384       if (printDebug_ > 1) {
0385     std::ostringstream message;
0386     message << "TkHistoMap CM: Detid " << detid << ", mean = " <<  mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
0387     edm::LogVerbatim("SiStripMonitorHardware") << message.str();
0388       }
0389 
0390       //ele++;
0391     }
0392 
0393     for (fracIter = CommonModesAPV0minusAPV1_.begin(); fracIter!=CommonModesAPV0minusAPV1_.end(); fracIter++){
0394       uint32_t detid = fracIter->first;
0395       //if ((fracIter->second).second != 0) {
0396       //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
0397       //nBadChannels++;
0398       //}
0399       float mean = 0;
0400       float rms = 0;
0401       Statistics lStat = fracIter->second;
0402       if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
0403       if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
0404       cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(2),detid,mean);
0405       cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(3),detid,rms);
0406 
0407       if (printDebug_ > 1) {
0408     std::ostringstream message;
0409     message << "TkHistoMap APV0minusAPV1: Detid " << detid << ", mean = " <<  mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
0410     edm::LogVerbatim("SiStripMonitorHardware") << message.str();
0411       }
0412 
0413       //ele++;
0414     }
0415 
0416   }//if TkHistoMap is enabled
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_) {  //if TkHistMap is enabled
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 // Define as a plug-in
0459 //
0460 
0461 #include "FWCore/Framework/interface/MakerMacros.h"
0462 DEFINE_FWK_MODULE(SiStripCMMonitorPlugin);