Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-29 01:21:18

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