Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:12

0001 #include "DQMServices/Core/interface/DQMStore.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/Framework/interface/Run.h"
0004 #include "FWCore/Framework/interface/LuminosityBlock.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Version/interface/GetReleaseVersion.h"
0007 #include "FWCore/ServiceRegistry/interface/Service.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/MessageLogger/interface/JobReport.h"
0010 #include "FWCore/Utilities/interface/TimeOfDay.h"
0011 #include "FWCore/Framework/interface/GetterOfProducts.h"
0012 #include "FWCore/Framework/interface/ProcessMatch.h"
0013 #include "DataFormats/Histograms/interface/DQMToken.h"
0014 
0015 #include "DQMFileSaverBase.h"
0016 
0017 #include <sys/stat.h>
0018 #include <sys/types.h>
0019 #include <unistd.h>
0020 #include <iostream>
0021 #include <vector>
0022 #include <string>
0023 #include <fstream>
0024 #include <utility>
0025 #include <filesystem>
0026 #include <TString.h>
0027 #include <TSystem.h>
0028 
0029 using namespace dqm;
0030 
0031 DQMFileSaverBase::DQMFileSaverBase(const edm::ParameterSet &ps) {
0032   FileParameters fp;
0033 
0034   fp.path_ = ps.getUntrackedParameter<std::string>("path");
0035   fp.producer_ = ps.getUntrackedParameter<std::string>("producer");
0036   fp.run_ = 0;
0037   fp.tag_ = ps.getUntrackedParameter<std::string>("tag");
0038   fp.lumi_ = 0;
0039   fp.version_ = 1;
0040   fp.child_ = "";
0041 
0042   std::unique_lock<std::mutex> lck(initial_fp_lock_);
0043   initial_fp_ = fp;
0044 
0045   runNumber_ = ps.getUntrackedParameter<int>("runNumber", 111);
0046 
0047   // This makes sure a file saver runs in a very end
0048   lumigetter_ = edm::GetterOfProducts<DQMToken>(edm::ProcessMatch("*"), this, edm::InLumi);
0049   rungetter_ = edm::GetterOfProducts<DQMToken>(edm::ProcessMatch("*"), this, edm::InRun);
0050   callWhenNewProductsRegistered([this](edm::BranchDescription const &bd) {
0051     this->lumigetter_(bd);
0052     this->rungetter_(bd);
0053   });
0054 }
0055 
0056 DQMFileSaverBase::~DQMFileSaverBase() = default;
0057 
0058 std::shared_ptr<NoCache> DQMFileSaverBase::globalBeginRun(const edm::Run &r, const edm::EventSetup &) const {
0059   this->initRun();
0060 
0061   return nullptr;
0062 }
0063 
0064 std::shared_ptr<NoCache> DQMFileSaverBase::globalBeginLuminosityBlock(const edm::LuminosityBlock &l,
0065                                                                       const edm::EventSetup &) const {
0066   return nullptr;
0067 }
0068 
0069 void DQMFileSaverBase::analyze(edm::StreamID, const edm::Event &e, const edm::EventSetup &) const {
0070   // not supported
0071 }
0072 
0073 void DQMFileSaverBase::globalEndLuminosityBlock(const edm::LuminosityBlock &iLS, const edm::EventSetup &) const {
0074   int ilumi = iLS.id().luminosityBlock();
0075   int irun = iLS.id().run();
0076 
0077   std::unique_lock<std::mutex> lck(initial_fp_lock_);
0078   FileParameters fp = initial_fp_;
0079   lck.unlock();
0080 
0081   fp.lumi_ = ilumi;
0082   fp.run_ = runNumber_ == 111 ? irun : runNumber_;
0083 
0084   this->saveLumi(fp);
0085 }
0086 
0087 void DQMFileSaverBase::globalEndRun(const edm::Run &iRun, const edm::EventSetup &) const {
0088   std::unique_lock<std::mutex> lck(initial_fp_lock_);
0089   FileParameters fp = initial_fp_;
0090   lck.unlock();
0091 
0092   fp.run_ = runNumber_ == 111 ? iRun.id().run() : runNumber_;
0093 
0094   // empty
0095   this->saveRun(fp);
0096 }
0097 
0098 const std::string DQMFileSaverBase::filename(const FileParameters &fp, bool useLumi) {
0099   char buf[256];
0100   if (useLumi) {
0101     snprintf(buf,
0102              256,
0103              "%s_V%04d_%s_R%09ld_L%09ld%s",
0104              fp.producer_.c_str(),
0105              fp.version_,
0106              fp.tag_.c_str(),
0107              fp.run_,
0108              fp.lumi_,
0109              fp.child_.c_str());
0110   } else {
0111     snprintf(buf,
0112              256,
0113              "%s_V%04d_%s_R%09ld%s",
0114              fp.producer_.c_str(),
0115              fp.version_,
0116              fp.tag_.c_str(),
0117              fp.run_,
0118              fp.child_.c_str());
0119   }
0120   buf[255] = 0;
0121 
0122   namespace fs = std::filesystem;
0123   fs::path path(fp.path_);
0124   fs::path file(buf);
0125 
0126   return (path / file).string();
0127 }
0128 
0129 void DQMFileSaverBase::saveJobReport(const std::string &filename) const {
0130   // Report the file to job report service.
0131   edm::Service<edm::JobReport> jr;
0132   if (jr.isAvailable()) {
0133     std::map<std::string, std::string> info;
0134     info["Source"] = "DQMStore";
0135     info["FileClass"] = "DQM";
0136     jr->reportAnalysisFile(filename, info);
0137   }
0138 }
0139 
0140 void DQMFileSaverBase::logFileAction(const std::string &msg, const std::string &fileName) const {
0141   edm::LogAbsolute("fileAction") << std::setprecision(0) << edm::TimeOfDay() << "  " << msg << fileName;
0142   edm::FlushMessageLog();
0143 }
0144 
0145 void DQMFileSaverBase::fillDescription(edm::ParameterSetDescription &desc) {
0146   desc.addUntracked<std::string>("tag", "UNKNOWN")->setComment("File tag, DQM_V000_<TAG>*, usually a subsytem name.");
0147 
0148   desc.addUntracked<std::string>("producer", "DQM")
0149       ->setComment("Base prefix for files, <BASE>_V000_**, either 'DQM' or 'Playback'.");
0150 
0151   desc.addUntracked<std::string>("referenceHandling", "all")->setComment("saveReference_, passed to the DQMStore");
0152 
0153   desc.addUntracked<int>("referenceRequireStatus", dqm::qstatus::STATUS_OK)
0154       ->setComment("saveReference_, passed to the DQMStore");
0155 
0156   desc.addUntracked<std::string>("path", "./")->setComment("Output path prefix.");
0157 
0158   desc.addUntracked<int>("runNumber", 111)
0159       ->setComment("Run number passed in the configuration. Will appear in output file names.");
0160 }