Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-06-25 22:34:53

0001 /*
0002  * \file DQMEventInfo.h
0003  *
0004  * \author M. Zanetti - INFN Padova
0005  *
0006 */
0007 #include "DQMServices/Core/interface/DQMOneEDAnalyzer.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "FWCore/Framework/interface/LuminosityBlock.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/Framework/interface/Run.h"
0012 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0015 #include "FWCore/ServiceRegistry/interface/Service.h"
0016 #include "FWCore/Version/interface/GetReleaseVersion.h"
0017 
0018 #include <algorithm>
0019 #include <iostream>
0020 #include <sstream>
0021 #include <fstream>
0022 #include <string>
0023 #include <vector>
0024 #include <memory>
0025 #include <cstdio>
0026 #include <cmath>
0027 #include <map>
0028 
0029 #include <sys/time.h>
0030 #include <TSystem.h>
0031 
0032 #include <boost/algorithm/string/join.hpp>
0033 
0034 class DQMEventInfo : public DQMOneEDAnalyzer<> {
0035 public:
0036   /// Constructor
0037   DQMEventInfo(const edm::ParameterSet& ps);
0038 
0039   /// Destructor
0040   ~DQMEventInfo() override = default;
0041 
0042   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0043 
0044 protected:
0045   /// Analyze
0046   void analyze(const edm::Event& e, const edm::EventSetup& c) override;
0047   void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
0048   void analyzeProvInfo(const edm::Event& e);
0049 
0050 private:
0051   std::string globalTag_;
0052   bool globalTagRetrieved_;
0053   bool showHLTGlobalTag_;
0054 
0055   std::string eventInfoFolder_;
0056   std::string subsystemname_;
0057 
0058   double currentTime_, lastUpdateTime_, lastAvgTime_;
0059   double runStartTime_;
0060   double evtRateWindow_;
0061   int64_t evtRateCount_;
0062   int64_t pEvent_;
0063 
0064   //////////////////////////////////////////////////////////////////
0065   ///These MEs are filled with the info from the most recent event
0066   ///   by the module
0067   //////////////////////////////////////////////////////////////////
0068   MonitorElement* runId_;
0069   MonitorElement* runStartTimeStamp_;  ///UTC time of the run start
0070   MonitorElement* eventId_;
0071   MonitorElement* lumisecId_;
0072   MonitorElement* eventTimeStamp_;
0073 
0074   //////////////////////////////////////////////////////////////////
0075   ///These MEs are either static or updated upon each analyze() call
0076   //////////////////////////////////////////////////////////////////
0077   MonitorElement* nUpdates_;               ///Number of collector updates (TBD)
0078   MonitorElement* processId_;              ///The PID associated with this job
0079   MonitorElement* processStartTimeStamp_;  ///The UTC time of the first event processed
0080   MonitorElement* processTimeStamp_;       ///The UTC time of the last event
0081   MonitorElement* processLatency_;         ///Time elapsed since the last event
0082   MonitorElement* processEventRate_;       ///Avg # of events in programmable window (default: 5 min)
0083   MonitorElement* processEvents_;          ///# of event processed so far
0084   MonitorElement* hostName_;               ///Hostname of the local machine
0085   MonitorElement* processName_;            ///DQM "name" of the job (eg, Hcal or DT)
0086   MonitorElement* workingDir_;             ///Current working directory of the job
0087   MonitorElement* cmsswVer_;               ///CMSSW version run for this job
0088   MonitorElement* versGlobaltag_;          ///GlobalTag name
0089   MonitorElement* dqmPatch_;               ///DQM patch version for this job
0090   MonitorElement* errSummary_;             ///Subdetector-specific error summary (float)
0091   MonitorElement* errSummaryEtaPhi_;       ///Subdetector-specific etaPhi summary (float)
0092   MonitorElement* errSummarySegment_[10];
0093 };
0094 
0095 static inline double stampToReal(edm::Timestamp time) {
0096   return (time.value() >> 32) + 1e-6 * (time.value() & 0xffffffff);
0097 }
0098 
0099 static inline double stampToReal(const timeval& time) { return time.tv_sec + 1e-6 * time.tv_usec; }
0100 
0101 DQMEventInfo::DQMEventInfo(const edm::ParameterSet& ps) {
0102   struct timeval now;
0103   gettimeofday(&now, nullptr);
0104 
0105   pEvent_ = 0;
0106   evtRateCount_ = 0;
0107   lastAvgTime_ = currentTime_ = stampToReal(now);
0108 
0109   // read config parms
0110   showHLTGlobalTag_ = ps.getUntrackedParameter<bool>("showHLTGlobalTag", false);
0111   std::string folder = ps.getUntrackedParameter<std::string>("eventInfoFolder", "EventInfo");
0112   subsystemname_ = ps.getUntrackedParameter<std::string>("subSystemFolder", "YourSubsystem");
0113 
0114   eventInfoFolder_ = subsystemname_ + "/" + folder;
0115   evtRateWindow_ = ps.getUntrackedParameter<double>("eventRateWindow", 0.5);
0116   if (evtRateWindow_ <= 0.15)
0117     evtRateWindow_ = 0.15;
0118 
0119   // Initialization of the global tag
0120   globalTag_ = "MODULE::DEFAULT";  // default
0121   globalTagRetrieved_ = false;     // set as soon as retrieved from first event
0122 }
0123 
0124 void DQMEventInfo::bookHistograms(DQMStore::IBooker& ibooker,
0125                                   edm::Run const& iRun,
0126                                   edm::EventSetup const& /* iSetup */) {
0127   ibooker.setCurrentFolder(eventInfoFolder_);
0128 
0129   //Event specific contents
0130   runId_ = ibooker.bookInt("iRun");
0131   runId_->Fill(iRun.id().run());
0132   lumisecId_ = ibooker.bookInt("iLumiSection");
0133   lumisecId_->Fill(-1);
0134   eventId_ = ibooker.bookInt("iEvent");
0135   eventId_->Fill(-1);
0136   eventTimeStamp_ = ibooker.bookFloat("eventTimeStamp");
0137 
0138   ibooker.setCurrentFolder(eventInfoFolder_);
0139   //Process specific contents
0140   processTimeStamp_ = ibooker.bookFloat("processTimeStamp");
0141   processTimeStamp_->Fill(currentTime_);
0142   processLatency_ = ibooker.bookFloat("processLatency");
0143   processTimeStamp_->Fill(-1);
0144   processEvents_ = ibooker.bookInt("processedEvents");
0145   processEvents_->Fill(pEvent_);
0146   processEventRate_ = ibooker.bookFloat("processEventRate");
0147   processEventRate_->Fill(-1);
0148   nUpdates_ = ibooker.bookInt("processUpdates");
0149   nUpdates_->Fill(-1);
0150 
0151   //Static Contents
0152   processId_ = ibooker.bookInt("processID");
0153   processId_->Fill(getpid());
0154   processStartTimeStamp_ = ibooker.bookFloat("processStartTimeStamp");
0155   processStartTimeStamp_->Fill(currentTime_);
0156   runStartTimeStamp_ = ibooker.bookFloat("runStartTimeStamp");
0157   runStartTimeStamp_->Fill(stampToReal(iRun.beginTime()));
0158   char hostname[65];
0159   gethostname(hostname, 64);
0160   hostname[64] = 0;
0161   hostName_ = ibooker.bookString("hostName", hostname);
0162   processName_ = ibooker.bookString("processName", subsystemname_);
0163   char* pwd = getcwd(nullptr, 0);
0164   workingDir_ = ibooker.bookString("workingDir", pwd);
0165   free(pwd);
0166   cmsswVer_ = ibooker.bookString("CMSSW_Version", edm::getReleaseVersion());
0167 
0168   // Element: Globaltag
0169   versGlobaltag_ = ibooker.bookString("Globaltag", globalTag_);
0170 
0171   // Folder to be populated by sub-systems' code
0172   std::string subfolder = eventInfoFolder_ + "/reportSummaryContents";
0173   ibooker.setCurrentFolder(subfolder);
0174 
0175   //Online static histograms
0176   const edm::ParameterSet& sourcePSet =
0177       edm::getProcessParameterSetContainingModule(moduleDescription()).getParameterSet("@main_input");
0178 
0179   if (sourcePSet.getParameter<std::string>("@module_type") == "DQMStreamerReader") {
0180     std::string evSelection;
0181     std::vector<std::string> evSelectionList;
0182     std::string delimiter(", ");
0183     evSelectionList = sourcePSet.getUntrackedParameter<std::vector<std::string> >("SelectEvents");
0184     // add single quotes inline in the vector of HLT paths:
0185     // we do copy assignment, and getUntrackedParameter returns
0186     // a by-value copy of the vector of strings
0187     std::for_each(evSelectionList.begin(), evSelectionList.end(), [](std::string& s) {
0188       std::string squote("'");
0189       s = squote + s + squote;
0190     });
0191     evSelection = boost::algorithm::join(evSelectionList, delimiter);
0192     // if no HLT paths are specified, no selections are performed:
0193     // we mark this with an asterisk.
0194     if (evSelection.empty()) {
0195       evSelection = std::string("'*'");
0196     }
0197     ibooker.setCurrentFolder(eventInfoFolder_);
0198     ibooker.bookString("eventSelection", evSelection);
0199   }
0200 }
0201 
0202 void DQMEventInfo::analyze(const edm::Event& e, const edm::EventSetup& c) {
0203   //Filling lumi here guarantees that the lumi number corresponds to the event when
0204   // using multiple concurrent lumis in a job
0205   lumisecId_->Fill(e.id().luminosityBlock());
0206   eventId_->Fill(e.id().event());  // Handing edm::EventNumber_t to Fill method which will handle further casting
0207   eventTimeStamp_->Fill(stampToReal(e.time()));
0208 
0209   pEvent_++;
0210   evtRateCount_++;
0211   processEvents_->Fill(pEvent_);
0212 
0213   struct timeval now;
0214   gettimeofday(&now, nullptr);
0215   lastUpdateTime_ = currentTime_;
0216   currentTime_ = stampToReal(now);
0217 
0218   processTimeStamp_->Fill(currentTime_);
0219   processLatency_->Fill(currentTime_ - lastUpdateTime_);
0220 
0221   double delta = currentTime_ - lastAvgTime_;
0222   if (delta >= (evtRateWindow_ * 60.0)) {
0223     processEventRate_->Fill(evtRateCount_ / delta);
0224     evtRateCount_ = 0;
0225     lastAvgTime_ = currentTime_;
0226   }
0227 
0228   analyzeProvInfo(e);
0229 
0230   return;
0231 }
0232 
0233 void DQMEventInfo::analyzeProvInfo(const edm::Event& event) {
0234   // Only trying to retrieve the global tag for the first event we ever
0235   // encounter.
0236   if (!globalTagRetrieved_) {
0237     // Initialize processName to an empty string
0238     std::string processName;
0239 
0240     if (showHLTGlobalTag_) {
0241       // Getting all process names
0242       std::vector<std::string> pnames;
0243       for (const auto& p : event.processHistory()) {
0244         pnames.push_back(p.processName());
0245       }
0246 
0247       // Iterate through the process names in reverse to find the last one that contains "HLT"
0248       for (auto it = pnames.rbegin(); it != pnames.rend(); ++it) {
0249         if (it->find("HLT") != std::string::npos) {
0250           processName = *it;
0251           break;  // Exit the loop once the last matching process name is found
0252         }
0253       }
0254 
0255       // Print the process name containing "HLT"
0256       if (processName.empty()) {
0257         edm::LogError("DQMEventInfo") << "Could not find any processName containing 'HLT' even if 'showHLTGlobalTag' "
0258                                          "was chosen.\n Falling back to current processing!"
0259                                       << std::endl;
0260         processName = event.processHistory()[event.processHistory().size() - 1].processName();
0261       }
0262     } else {
0263       processName = event.processHistory()[event.processHistory().size() - 1].processName();
0264     }
0265 
0266     // Getting parameters for that process
0267     edm::ParameterSet ps;
0268     event.getProcessParameterSet(processName, ps);
0269 
0270     // Check if the 'PoolDBESSource@GlobalTag' ParameterSet exists
0271     if (ps.exists("PoolDBESSource@GlobalTag")) {
0272       // Getting the global tag
0273       globalTag_ = ps.getParameterSet("PoolDBESSource@GlobalTag").getParameter<std::string>("globaltag");
0274     } else {
0275       // Handle the case where 'PoolDBESSource@GlobalTag' is missing
0276       // You can set a default value or take some other action
0277       edm::LogInfo("Configuration") << "ParameterSet 'PoolDBESSource@GlobalTag' not found. Using default global tag.";
0278     }
0279 
0280     versGlobaltag_->Fill(globalTag_);
0281     // Finaly: Setting globalTagRetrieved_ to true, since we got it now
0282     globalTagRetrieved_ = true;
0283   }
0284 }
0285 
0286 void DQMEventInfo::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0287   edm::ParameterSetDescription desc;
0288   desc.addUntracked<bool>("showHLTGlobalTag", false);
0289   desc.addUntracked<std::string>("eventInfoFolder", "EventInfo");
0290   desc.addUntracked<std::string>("subSystemFolder", "YourSubsystem");
0291   desc.addUntracked<double>("eventRateWindow", 0.5);
0292   descriptions.addWithDefaultLabel(desc);
0293 }
0294 
0295 #include "FWCore/Framework/interface/MakerMacros.h"
0296 DEFINE_FWK_MODULE(DQMEventInfo);