Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:42:05

0001 #include "DQM/EcalMonitorTasks/interface/EcalDQMonitorTask.h"
0002 
0003 #include "DQM/EcalMonitorTasks/interface/DQWorkerTask.h"
0004 
0005 #include "DQM/EcalCommon/interface/MESet.h"
0006 #include "DQM/EcalCommon/interface/EcalDQMCommonUtils.h"
0007 
0008 #include "FWCore/Framework/interface/MakerMacros.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/ConsumesCollector.h"
0011 
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0014 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 
0019 #include "DataFormats/Common/interface/Handle.h"
0020 #include "DataFormats/EcalRawData/interface/EcalRawDataCollections.h"
0021 
0022 #include <iomanip>
0023 #include <ctime>
0024 #include <bitset>
0025 #include <sstream>
0026 
0027 EcalDQMonitorTask::EcalDQMonitorTask(edm::ParameterSet const& _ps)
0028     : DQMOneEDAnalyzer<edm::LuminosityBlockCache<ecaldqm::EcalLSCache>>(),
0029       ecaldqm::EcalDQMonitor(_ps),
0030       schedule_(),
0031       skipCollections_(_ps.getUntrackedParameter<std::vector<std::string>>("skipCollections")),
0032       allowMissingCollections_(_ps.getUntrackedParameter<bool>("allowMissingCollections")),
0033       processedEvents_(0),
0034       lastResetTime_(0),
0035       resetInterval_(_ps.getUntrackedParameter<double>("resetInterval")) {
0036   ecaldqm::DependencySet dependencies;
0037   std::bitset<ecaldqm::nCollections> hasTaskToRun;
0038   edm::ConsumesCollector collector(consumesCollector());
0039 
0040   executeOnWorkers_(
0041       [&dependencies, &hasTaskToRun, &collector](ecaldqm::DQWorker* worker) {
0042         ecaldqm::DQWorkerTask* task(dynamic_cast<ecaldqm::DQWorkerTask*>(worker));
0043         if (!task)
0044           throw cms::Exception("InvalidConfiguration") << "Non-task DQWorker " << worker->getName() << " passed";
0045 
0046         task->addDependencies(dependencies);
0047         for (unsigned iCol(0); iCol < ecaldqm::nCollections; ++iCol) {
0048           if (task->analyze(nullptr, ecaldqm::Collections(iCol)))  // "dry run" mode
0049             hasTaskToRun.set(iCol);
0050         }
0051         worker->setTokens(collector);
0052         task->setTokens(collector);
0053       },
0054       "initialization");
0055 
0056   edm::ParameterSet const& collectionTags(_ps.getParameterSet("collectionTags"));
0057 
0058   for (unsigned iCol(0); iCol < ecaldqm::nCollections; iCol++) {
0059     if (hasTaskToRun[iCol])
0060       dependencies.push_back(ecaldqm::Dependency(ecaldqm::Collections(iCol)));
0061   }
0062   if (collectionTags.existsAs<edm::InputTag>("EcalRawData", false))
0063     dependencies.push_back(ecaldqm::Dependency(ecaldqm::kEcalRawData));
0064 
0065   formSchedule(dependencies.formSequence(), collectionTags);
0066 
0067   if (verbosity_ > 0) {
0068     std::stringstream ss;
0069     ss << moduleName_ << ": Using collections" << std::endl;
0070     for (unsigned iCol(0); iCol < schedule_.size(); iCol++)
0071       ss << ecaldqm::collectionName[schedule_[iCol].second] << std::endl;
0072     edm::LogInfo("EcalDQM") << ss.str();
0073   }
0074 
0075   edm::ParameterSet const& commonParams(_ps.getUntrackedParameterSet("commonParameters"));
0076   if (commonParams.getUntrackedParameter<bool>("onlineMode"))
0077     lastResetTime_ = time(nullptr);
0078 }
0079 
0080 /*static*/
0081 void EcalDQMonitorTask::fillDescriptions(edm::ConfigurationDescriptions& _descs) {
0082   edm::ParameterSetDescription desc;
0083   ecaldqm::EcalDQMonitor::fillDescriptions(desc);
0084 
0085   edm::ParameterSetDescription taskParameters;
0086   ecaldqm::DQWorkerTask::fillDescriptions(taskParameters);
0087   edm::ParameterSetDescription allWorkers;
0088   allWorkers.addNode(
0089       edm::ParameterWildcard<edm::ParameterSetDescription>("*", edm::RequireZeroOrMore, false, taskParameters));
0090   desc.addUntracked("workerParameters", allWorkers);
0091 
0092   edm::ParameterSetDescription collectionTags;
0093   collectionTags.addWildcardUntracked<edm::InputTag>("*");
0094   desc.add("collectionTags", collectionTags);
0095 
0096   desc.addUntracked<std::vector<std::string>>("skipCollections", std::vector<std::string>());
0097   desc.addUntracked<bool>("allowMissingCollections", true);
0098   desc.addUntracked<double>("resetInterval", 0.);
0099 
0100   _descs.addDefault(desc);
0101 }
0102 
0103 void EcalDQMonitorTask::bookHistograms(DQMStore::IBooker& _ibooker, edm::Run const&, edm::EventSetup const& _es) {
0104   executeOnWorkers_([&_es](ecaldqm::DQWorker* worker) { worker->setSetupObjects(_es); },
0105                     "ecaldqmGetSetupObjects",
0106                     "Getting EventSetup Objects");
0107 
0108   executeOnWorkers_([&_ibooker](ecaldqm::DQWorker* worker) { worker->bookMEs(_ibooker); }, "bookMEs", "Booking MEs");
0109 }
0110 
0111 void EcalDQMonitorTask::dqmBeginRun(edm::Run const& _run, edm::EventSetup const& _es) {
0112   ecaldqmBeginRun(_run, _es);
0113 
0114   processedEvents_ = 0;
0115 
0116   if (lastResetTime_ != 0)
0117     lastResetTime_ = time(nullptr);
0118 }
0119 
0120 void EcalDQMonitorTask::dqmEndRun(edm::Run const& _run, edm::EventSetup const& _es) {
0121   ecaldqmEndRun(_run, _es);
0122 
0123   executeOnWorkers_([](ecaldqm::DQWorker* worker) { worker->releaseMEs(); }, "releaseMEs", "releasing histograms");
0124 }
0125 
0126 std::shared_ptr<ecaldqm::EcalLSCache> EcalDQMonitorTask::globalBeginLuminosityBlock(edm::LuminosityBlock const& _lumi,
0127                                                                                     edm::EventSetup const& _es) const {
0128   std::shared_ptr<ecaldqm::EcalLSCache> tmpCache = std::make_shared<ecaldqm::EcalLSCache>();
0129   executeOnWorkers_(
0130       [&tmpCache](ecaldqm::DQWorker* worker) { (tmpCache->ByLumiPlotsResetSwitches)[worker->getName()] = true; },
0131       "globalBeginLuminosityBlock");
0132   if (this->verbosity_ > 2)
0133     edm::LogInfo("EcalDQM") << "Set LS cache.";
0134 
0135   // Reset lhcStatusSet_ to false at the beginning of each LS; when LHC status is set in some event this variable will be set to tru
0136   tmpCache->lhcStatusSet_ = false;
0137   ecaldqmBeginLuminosityBlock(_lumi, _es);
0138   return tmpCache;
0139 }
0140 
0141 void EcalDQMonitorTask::globalEndLuminosityBlock(edm::LuminosityBlock const& _lumi, edm::EventSetup const& _es) {
0142   ecaldqmEndLuminosityBlock(_lumi, _es);
0143 
0144   if (lastResetTime_ != 0 && (time(nullptr) - lastResetTime_) / 3600. > resetInterval_) {
0145     if (verbosity_ > 0)
0146       edm::LogInfo("EcalDQM") << moduleName_ << ": Soft-resetting the histograms";
0147     // TODO: soft-reset is no longer supported.
0148     lastResetTime_ = time(nullptr);
0149   }
0150 }
0151 
0152 void EcalDQMonitorTask::analyze(edm::Event const& _evt, edm::EventSetup const& _es) {
0153   if (verbosity_ > 2)
0154     edm::LogInfo("EcalDQM") << moduleName_ << "::analyze: Run " << _evt.id().run() << " Lumisection "
0155                             << _evt.id().luminosityBlock() << " Event " << _evt.id().event() << ": processed "
0156                             << processedEvents_;
0157 
0158   if (schedule_.empty())
0159     return;
0160 
0161   std::set<ecaldqm::DQWorker*> enabledTasks;
0162 
0163   bool eventTypeFiltering(false);
0164   edm::Handle<EcalRawDataCollection> dcchsHndl;
0165 
0166   if (std::find(skipCollections_.begin(), skipCollections_.end(), "EcalRawData") != skipCollections_.end()) {
0167     if (verbosity_ > 2)
0168       edm::LogInfo("EcalDQM") << "EcalRawDataCollection is being skipped. No event-type filtering will be applied";
0169 
0170   } else if (_evt.getByToken(collectionTokens_[ecaldqm::kEcalRawData], dcchsHndl)) {
0171     eventTypeFiltering = true;
0172     // determine event type (called run type in DCCHeader for some reason) for each FED
0173     std::stringstream ss;
0174     if (verbosity_ > 2)
0175       ss << moduleName_ << ": Event type ";
0176 
0177     short runType[ecaldqm::nDCC];
0178     std::fill_n(runType, ecaldqm::nDCC, -1);
0179     for (EcalRawDataCollection::const_iterator dcchItr = dcchsHndl->begin(); dcchItr != dcchsHndl->end(); ++dcchItr) {
0180       if (verbosity_ > 2)
0181         ss << dcchItr->getRunType() << " ";
0182       runType[dcchItr->id() - 1] = dcchItr->getRunType();
0183     }
0184     if (verbosity_ > 2)
0185       edm::LogInfo("EcalDQM") << ss.str();
0186 
0187     bool processEvent(false);
0188 
0189     executeOnWorkers_(
0190         [&enabledTasks, &runType, &processEvent, this](ecaldqm::DQWorker* worker) {
0191           if (static_cast<ecaldqm::DQWorkerTask*>(worker)->filterRunType(runType)) {
0192             if (this->verbosity_ > 2)
0193               edm::LogInfo("EcalDQM") << worker->getName() << " will run on this event";
0194             enabledTasks.insert(worker);
0195             processEvent = true;
0196           }
0197         },
0198         "filterRunType");
0199 
0200     if (!processEvent)
0201       return;
0202   } else {
0203     edm::LogWarning("EcalDQM") << "EcalRawDataCollection does not exist. No event-type filtering will be applied";
0204   }
0205 
0206   if (!eventTypeFiltering)
0207     executeOnWorkers_([&enabledTasks](ecaldqm::DQWorker* worker) { enabledTasks.insert(worker); }, "");
0208 
0209   ++processedEvents_;
0210 
0211   // start event processing
0212   auto lumiCache = luminosityBlockCache(_evt.getLuminosityBlock().index());
0213   executeOnWorkers_(
0214       [&_evt, &_es, &enabledTasks, &lumiCache](ecaldqm::DQWorker* worker) {
0215         if (enabledTasks.find(worker) != enabledTasks.end()) {
0216           if (worker->onlineMode())
0217             worker->setTime(time(nullptr));
0218           worker->setEventNumber(_evt.id().event());
0219           bool ByLumiResetSwitch = (lumiCache->ByLumiPlotsResetSwitches).at(worker->getName());
0220           bool lhcStatusSet = lumiCache->lhcStatusSet_;
0221           static_cast<ecaldqm::DQWorkerTask*>(worker)->beginEvent(_evt, _es, ByLumiResetSwitch, lhcStatusSet);
0222           (lumiCache->ByLumiPlotsResetSwitches)[worker->getName()] = false;
0223           lumiCache->lhcStatusSet_ = lhcStatusSet;
0224         }
0225       },
0226       "beginEvent");
0227 
0228   // run on collections
0229   for (unsigned iSch(0); iSch < schedule_.size(); iSch++) {
0230     Processor processor(schedule_[iSch].first);
0231     (this->*processor)(_evt, schedule_[iSch].second, enabledTasks);
0232   }
0233 
0234   // close event processing
0235   executeOnWorkers_(
0236       [&_evt, &_es, &enabledTasks](ecaldqm::DQWorker* worker) {
0237         if (enabledTasks.find(worker) != enabledTasks.end())
0238           static_cast<ecaldqm::DQWorkerTask*>(worker)->endEvent(_evt, _es);
0239       },
0240       "endEvent");
0241 
0242   if (verbosity_ > 2)
0243     edm::LogInfo("EcalDQM") << moduleName_ << "::analyze: Closing Event " << _evt.id().event();
0244 }
0245 
0246 DEFINE_FWK_MODULE(EcalDQMonitorTask);