DQMEventInfo

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/*
 * \file DQMEventInfo.h
 *
 * \author M. Zanetti - INFN Padova
 *
*/
#include "DQMServices/Core/interface/DQMOneEDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/LuminosityBlock.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Version/interface/GetReleaseVersion.h"

#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <memory>
#include <cstdio>
#include <cmath>
#include <map>

#include <sys/time.h>
#include <TSystem.h>

#include <boost/algorithm/string/join.hpp>

class DQMEventInfo : public DQMOneEDAnalyzer<> {
public:
  /// Constructor
  DQMEventInfo(const edm::ParameterSet& ps);

  /// Destructor
  ~DQMEventInfo() override = default;

  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

protected:
  /// Analyze
  void analyze(const edm::Event& e, const edm::EventSetup& c) override;
  void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
  void analyzeProvInfo(const edm::Event& e);

private:
  std::string globalTag_;
  bool globalTagRetrieved_;
  bool showHLTGlobalTag_;

  std::string eventInfoFolder_;
  std::string subsystemname_;

  double currentTime_, lastUpdateTime_, lastAvgTime_;
  double runStartTime_;
  double evtRateWindow_;
  int64_t evtRateCount_;
  int64_t pEvent_;

  //////////////////////////////////////////////////////////////////
  ///These MEs are filled with the info from the most recent event
  ///   by the module
  //////////////////////////////////////////////////////////////////
  MonitorElement* runId_;
  MonitorElement* runStartTimeStamp_;  ///UTC time of the run start
  MonitorElement* eventId_;
  MonitorElement* lumisecId_;
  MonitorElement* eventTimeStamp_;

  //////////////////////////////////////////////////////////////////
  ///These MEs are either static or updated upon each analyze() call
  //////////////////////////////////////////////////////////////////
  MonitorElement* nUpdates_;               ///Number of collector updates (TBD)
  MonitorElement* processId_;              ///The PID associated with this job
  MonitorElement* processStartTimeStamp_;  ///The UTC time of the first event processed
  MonitorElement* processTimeStamp_;       ///The UTC time of the last event
  MonitorElement* processLatency_;         ///Time elapsed since the last event
  MonitorElement* processEventRate_;       ///Avg # of events in programmable window (default: 5 min)
  MonitorElement* processEvents_;          ///# of event processed so far
  MonitorElement* hostName_;               ///Hostname of the local machine
  MonitorElement* processName_;            ///DQM "name" of the job (eg, Hcal or DT)
  MonitorElement* workingDir_;             ///Current working directory of the job
  MonitorElement* cmsswVer_;               ///CMSSW version run for this job
  MonitorElement* versGlobaltag_;          ///GlobalTag name
  MonitorElement* dqmPatch_;               ///DQM patch version for this job
  MonitorElement* errSummary_;             ///Subdetector-specific error summary (float)
  MonitorElement* errSummaryEtaPhi_;       ///Subdetector-specific etaPhi summary (float)
  MonitorElement* errSummarySegment_[10];
};

static inline double stampToReal(edm::Timestamp time) {
  return (time.value() >> 32) + 1e-6 * (time.value() & 0xffffffff);
}

static inline double stampToReal(const timeval& time) { return time.tv_sec + 1e-6 * time.tv_usec; }

DQMEventInfo::DQMEventInfo(const edm::ParameterSet& ps) {
  struct timeval now;
  gettimeofday(&now, nullptr);

  pEvent_ = 0;
  evtRateCount_ = 0;
  lastAvgTime_ = currentTime_ = stampToReal(now);

  // read config parms
  showHLTGlobalTag_ = ps.getUntrackedParameter<bool>("showHLTGlobalTag", false);
  std::string folder = ps.getUntrackedParameter<std::string>("eventInfoFolder", "EventInfo");
  subsystemname_ = ps.getUntrackedParameter<std::string>("subSystemFolder", "YourSubsystem");

  eventInfoFolder_ = subsystemname_ + "/" + folder;
  evtRateWindow_ = ps.getUntrackedParameter<double>("eventRateWindow", 0.5);
  if (evtRateWindow_ <= 0.15)
    evtRateWindow_ = 0.15;

  // Initialization of the global tag
  globalTag_ = "MODULE::DEFAULT";  // default
  globalTagRetrieved_ = false;     // set as soon as retrieved from first event
}

void DQMEventInfo::bookHistograms(DQMStore::IBooker& ibooker,
                                  edm::Run const& iRun,
                                  edm::EventSetup const& /* iSetup */) {
  ibooker.setCurrentFolder(eventInfoFolder_);

  //Event specific contents
  runId_ = ibooker.bookInt("iRun");
  runId_->Fill(iRun.id().run());
  lumisecId_ = ibooker.bookInt("iLumiSection");
  lumisecId_->Fill(-1);
  eventId_ = ibooker.bookInt("iEvent");
  eventId_->Fill(-1);
  eventTimeStamp_ = ibooker.bookFloat("eventTimeStamp");

  ibooker.setCurrentFolder(eventInfoFolder_);
  //Process specific contents
  processTimeStamp_ = ibooker.bookFloat("processTimeStamp");
  processTimeStamp_->Fill(currentTime_);
  processLatency_ = ibooker.bookFloat("processLatency");
  processTimeStamp_->Fill(-1);
  processEvents_ = ibooker.bookInt("processedEvents");
  processEvents_->Fill(pEvent_);
  processEventRate_ = ibooker.bookFloat("processEventRate");
  processEventRate_->Fill(-1);
  nUpdates_ = ibooker.bookInt("processUpdates");
  nUpdates_->Fill(-1);

  //Static Contents
  processId_ = ibooker.bookInt("processID");
  processId_->Fill(getpid());
  processStartTimeStamp_ = ibooker.bookFloat("processStartTimeStamp");
  processStartTimeStamp_->Fill(currentTime_);
  runStartTimeStamp_ = ibooker.bookFloat("runStartTimeStamp");
  runStartTimeStamp_->Fill(stampToReal(iRun.beginTime()));
  char hostname[65];
  gethostname(hostname, 64);
  hostname[64] = 0;
  hostName_ = ibooker.bookString("hostName", hostname);
  processName_ = ibooker.bookString("processName", subsystemname_);
  char* pwd = getcwd(nullptr, 0);
  workingDir_ = ibooker.bookString("workingDir", pwd);
  free(pwd);
  cmsswVer_ = ibooker.bookString("CMSSW_Version", edm::getReleaseVersion());

  // Element: Globaltag
  versGlobaltag_ = ibooker.bookString("Globaltag", globalTag_);

  // Folder to be populated by sub-systems' code
  std::string subfolder = eventInfoFolder_ + "/reportSummaryContents";
  ibooker.setCurrentFolder(subfolder);

  //Online static histograms
  const edm::ParameterSet& sourcePSet =
      edm::getProcessParameterSetContainingModule(moduleDescription()).getParameterSet("@main_input");

  if (sourcePSet.getParameter<std::string>("@module_type") == "DQMStreamerReader") {
    std::string evSelection;
    std::vector<std::string> evSelectionList;
    std::string delimiter(", ");
    evSelectionList = sourcePSet.getUntrackedParameter<std::vector<std::string> >("SelectEvents");
    // add single quotes inline in the vector of HLT paths:
    // we do copy assignment, and getUntrackedParameter returns
    // a by-value copy of the vector of strings
    std::for_each(evSelectionList.begin(), evSelectionList.end(), [](std::string& s) {
      std::string squote("'");
      s = squote + s + squote;
    });
    evSelection = boost::algorithm::join(evSelectionList, delimiter);
    // if no HLT paths are specified, no selections are performed:
    // we mark this with an asterisk.
    if (evSelection.empty()) {
      evSelection = std::string("'*'");
    }
    ibooker.setCurrentFolder(eventInfoFolder_);
    ibooker.bookString("eventSelection", evSelection);
  }
}

void DQMEventInfo::analyze(const edm::Event& e, const edm::EventSetup& c) {
  //Filling lumi here guarantees that the lumi number corresponds to the event when
  // using multiple concurrent lumis in a job
  lumisecId_->Fill(e.id().luminosityBlock());
  eventId_->Fill(e.id().event());  // Handing edm::EventNumber_t to Fill method which will handle further casting
  eventTimeStamp_->Fill(stampToReal(e.time()));

  pEvent_++;
  evtRateCount_++;
  processEvents_->Fill(pEvent_);

  struct timeval now;
  gettimeofday(&now, nullptr);
  lastUpdateTime_ = currentTime_;
  currentTime_ = stampToReal(now);

  processTimeStamp_->Fill(currentTime_);
  processLatency_->Fill(currentTime_ - lastUpdateTime_);

  double delta = currentTime_ - lastAvgTime_;
  if (delta >= (evtRateWindow_ * 60.0)) {
    processEventRate_->Fill(evtRateCount_ / delta);
    evtRateCount_ = 0;
    lastAvgTime_ = currentTime_;
  }

  analyzeProvInfo(e);

  return;
}

void DQMEventInfo::analyzeProvInfo(const edm::Event& event) {
  // Only trying to retrieve the global tag for the first event we ever
  // encounter.
  if (!globalTagRetrieved_) {
    // Initialize processName to an empty string
    std::string processName;

    if (showHLTGlobalTag_) {
      // Getting all process names
      std::vector<std::string> pnames;
      for (const auto& p : event.processHistory()) {
        pnames.push_back(p.processName());
      }

      // Iterate through the process names in reverse to find the last one that contains "HLT"
      for (auto it = pnames.rbegin(); it != pnames.rend(); ++it) {
        if (it->find("HLT") != std::string::npos) {
          processName = *it;
          break;  // Exit the loop once the last matching process name is found
        }
      }

      // Print the process name containing "HLT"
      if (processName.empty()) {
        edm::LogError("DQMEventInfo") << "Could not find any processName containing 'HLT' even if 'showHLTGlobalTag' "
                                         "was chosen.\n Falling back to current processing!"
                                      << std::endl;
        processName = event.processHistory()[event.processHistory().size() - 1].processName();
      }
    } else {
      processName = event.processHistory()[event.processHistory().size() - 1].processName();
    }

    // Getting parameters for that process
    edm::ParameterSet ps;
    event.getProcessParameterSet(processName, ps);

    // Check if the 'PoolDBESSource@GlobalTag' ParameterSet exists
    if (ps.exists("PoolDBESSource@GlobalTag")) {
      // Getting the global tag
      globalTag_ = ps.getParameterSet("PoolDBESSource@GlobalTag").getParameter<std::string>("globaltag");
    } else {
      // Handle the case where 'PoolDBESSource@GlobalTag' is missing
      // You can set a default value or take some other action
      edm::LogInfo("Configuration") << "ParameterSet 'PoolDBESSource@GlobalTag' not found. Using default global tag.";
    }

    versGlobaltag_->Fill(globalTag_);
    // Finaly: Setting globalTagRetrieved_ to true, since we got it now
    globalTagRetrieved_ = true;
  }
}

void DQMEventInfo::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  desc.addUntracked<bool>("showHLTGlobalTag", false);
  desc.addUntracked<std::string>("eventInfoFolder", "EventInfo");
  desc.addUntracked<std::string>("subSystemFolder", "YourSubsystem");
  desc.addUntracked<double>("eventRateWindow", 0.5);
  descriptions.addWithDefaultLabel(desc);
}

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(DQMEventInfo);