CMS 3D CMS Logo

RamdiskMonitor.cc
Go to the documentation of this file.
1 #include <filesystem>
2 #include <map>
3 #include <vector>
4 #include <sys/stat.h>
5 
6 #include <fmt/printf.h>
7 #include <boost/algorithm/string/predicate.hpp>
8 #include <boost/range.hpp>
9 #include <boost/regex.hpp>
10 
20 
21 namespace dqm {
22  namespace rdm {
23  struct Empty {};
24  } // namespace rdm
25  class RamdiskMonitor : public DQMOneEDAnalyzer<edm::LuminosityBlockCache<rdm::Empty>> {
26  public:
28  ~RamdiskMonitor() override;
29  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
30 
31  protected:
32  void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
33  std::shared_ptr<rdm::Empty> globalBeginLuminosityBlock(edm::LuminosityBlock const &lumi,
34  edm::EventSetup const &eSetup) const override;
36  void analyze(edm::Event const &e, edm::EventSetup const &eSetup) override{};
37 
38  void analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const;
39  double getRunTimestamp() const;
40 
41  const unsigned int runNumber_;
43  const std::vector<std::string> streamLabels_;
45 
46  struct StreamME {
49 
52  };
53 
54  std::map<std::string, StreamME> streams_;
55  mutable std::set<std::string> filesSeen_;
56  mutable double global_start_ = 0.;
57 
58  static constexpr double LUMI = 23.310893056;
59  };
60 
62  : runNumber_{ps.getUntrackedParameter<unsigned int>("runNumber")},
63  runInputDir_{ps.getUntrackedParameter<std::string>("runInputDir")},
64  streamLabels_{ps.getUntrackedParameter<std::vector<std::string>>("streamLabels")},
65  runPath_{fmt::sprintf("%s/run%06d", runInputDir_, runNumber_)}
66 
67  {}
68 
70 
72  for (const auto &stream : streamLabels_) {
73  edm::LogInfo("RamdiskMonitor") << "Booking: " << stream;
74 
75  ib.cd();
76  ib.setCurrentFolder(std::string("Info/RamdiskMonitor/") + stream + "/");
77 
78  StreamME m;
79 
80  m.eventsAccepted = ib.book1D("EventAccepted", "# of accepted events per lumi", 4, 0., 4.);
81  m.eventsProcessed = ib.book1D("EventProcessed", "# of processed events per lumi", 4, 0., 4.);
82  m.deliveryDelayMTime =
83  ib.book1D("DeliveryDelayMTime", "Observed delivery delay for the data file (mtime).", 4, 0., 4.);
84  m.deliveryDelayCTime =
85  ib.book1D("DeliveryDelayCTime", "Observed delivery delay for the data file (ctime).", 4, 0., 4.);
86 
87  m.eventsAccepted->getTH1F()->SetCanExtend(TH1::kXaxis);
88  m.eventsProcessed->getTH1F()->SetCanExtend(TH1::kXaxis);
89  m.deliveryDelayMTime->getTH1F()->SetCanExtend(TH1::kXaxis);
90  m.deliveryDelayCTime->getTH1F()->SetCanExtend(TH1::kXaxis);
91 
92  m.eventsAccepted->setAxisTitle("Luminosity Section", 1);
93  m.eventsProcessed->setAxisTitle("Luminosity Section", 1);
94  m.deliveryDelayMTime->setAxisTitle("Luminosity Section", 1);
95  m.deliveryDelayCTime->setAxisTitle("Luminosity Section", 1);
96 
97  m.eventsAccepted->setAxisTitle("Number of events", 2);
98  m.eventsProcessed->setAxisTitle("Number of events", 2);
99  m.deliveryDelayMTime->setAxisTitle("Delay (s.)", 2);
100  m.deliveryDelayCTime->setAxisTitle("Delay (s.)", 2);
101 
102  streams_[stream] = m;
103  }
104  };
105 
107  if (global_start_ != 0)
108  return global_start_;
109 
110  std::string run_global = fmt::sprintf("%s/.run%06d.global", runInputDir_, runNumber_);
111  struct stat st;
112  if (::stat(run_global.c_str(), &st) != 0) {
113  edm::LogWarning("RamdiskMonitor") << "Stat failed: " << run_global;
114  return 0.;
115  }
116 
117  global_start_ = st.st_mtime;
118  edm::LogPrint("RamdiskMonitor") << "Run start timestamp: " << global_start_;
119  return global_start_;
120  };
121 
122  void RamdiskMonitor::analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const {
123  using LumiEntry = dqmservices::DQMFileIterator::LumiEntry;
124 
125  // we are disabled, at least for this stream
126  if (streams_.empty())
127  return;
128 
129  auto itStream = streams_.find(label);
130  if (itStream == streams_.end()) {
131  edm::LogPrint("RamdiskMonitor") << "Stream not monitored [" << label << "]: " << fn;
132  return;
133  }
134 
135  StreamME m = itStream->second;
136 
137  // decode json and fill in some histograms
138  LumiEntry lumi_jsn = LumiEntry::load_json(runPath_, fn, lumi, -1);
139  m.eventsAccepted->setBinContent(lumi, lumi_jsn.n_events_accepted);
140  m.eventsProcessed->setBinContent(lumi, lumi_jsn.n_events_processed);
141 
142  // collect stat struct and calculate mtimes
143  struct stat st;
144  if (::stat(fn.c_str(), &st) != 0) {
145  edm::LogWarning("RamdiskMonitor") << "Stat failed: " << fn;
146  return;
147  }
148 
149  // get start offset (from .global)
150  // abort the calculation if it does not exist
151  double start_offset = getRunTimestamp();
152  if (start_offset <= 0)
153  return;
154 
155  // check fff_dqmtools (separate repository)
156  // for calculation details
157  double mtime = st.st_mtime;
158  double ctime = st.st_ctime;
159 
160  // timeout from the begging of the run
161  double start_offset_mtime = mtime - start_offset - LUMI;
162  double start_offset_ctime = ctime - start_offset - LUMI;
163  double lumi_offset = (lumi - 1) * LUMI;
164 
165  // timeout from the time we think this lumi happenned
166  double delay_mtime = start_offset_mtime - lumi_offset;
167  double delay_ctime = start_offset_ctime - lumi_offset;
168 
169  m.deliveryDelayMTime->setBinContent(lumi, delay_mtime);
170  m.deliveryDelayCTime->setBinContent(lumi, delay_ctime);
171  };
172 
173  std::shared_ptr<dqm::rdm::Empty> RamdiskMonitor::globalBeginLuminosityBlock(edm::LuminosityBlock const &,
174  edm::EventSetup const &eSetup) const {
175  // search filesystem to find available lumi section files
176  using std::filesystem::directory_entry;
177  using std::filesystem::directory_iterator;
178 
179  directory_iterator dend;
180  for (directory_iterator di(runPath_); di != dend; ++di) {
181  const boost::regex fn_re("run(\\d+)_ls(\\d+)_([a-zA-Z0-9]+)(_.*)?\\.jsn");
182 
183  const std::string filename = di->path().filename().string();
184  const std::string fn = di->path().string();
185 
186  if (filesSeen_.find(filename) != filesSeen_.end()) {
187  continue;
188  }
189 
190  boost::smatch result;
191  if (boost::regex_match(filename, result, fn_re)) {
192  unsigned int run = std::stoi(result[1]);
193  unsigned int lumi = std::stoi(result[2]);
194  std::string label = result[3];
195 
196  filesSeen_.insert(filename);
197 
198  if (run != runNumber_)
199  continue;
200 
201  // check if this is EoR
202  if ((lumi == 0) && (label == "EoR")) {
203  // do not handle
204  continue;
205  }
206 
207  try {
208  this->analyzeFile(fn, run, lumi, label);
209  } catch (const std::exception &e) {
210  // it's likely we have read it too soon
211  filesSeen_.erase(filename);
212 
213  std::string msg("Found, tried to load the json, but failed (");
214  msg += e.what();
215  msg += "): ";
216  edm::LogWarning("RamdiskMonitor") << msg;
217  }
218  }
219  }
220 
221  // @TODO lookup info for the current lumi
222  return std::shared_ptr<dqm::rdm::Empty>();
223  }
224 
227 
228  desc.setComment(
229  "Analyses file timestams in the /fff/ramdisk and creates monitor "
230  "elements.");
231 
232  desc.addUntracked<std::vector<std::string>>("streamLabels")->setComment("List of streams to monitor.");
233 
234  desc.addUntracked<unsigned int>("runNumber")->setComment("Run number passed via configuration file.");
235 
236  desc.addUntracked<std::string>("runInputDir")->setComment("Directory where the DQM files will appear.");
237 
238  d.add("RamdiskMonitor", desc);
239  }
240 
241 } // namespace dqm
242 
244 
ConfigurationDescriptions.h
dqm::impl::MonitorElement
Definition: MonitorElement.h:98
dqm::RamdiskMonitor::runNumber_
const unsigned int runNumber_
Definition: RamdiskMonitor.cc:41
dqm::RamdiskMonitor::~RamdiskMonitor
~RamdiskMonitor() override
Definition: RamdiskMonitor.cc:69
dqm::RamdiskMonitor::getRunTimestamp
double getRunTimestamp() const
Definition: RamdiskMonitor.cc:106
dqm::RamdiskMonitor::StreamME
Definition: RamdiskMonitor.cc:46
MessageLogger.h
edm::LuminosityBlock
Definition: LuminosityBlock.h:50
edm::Run
Definition: Run.h:45
cms::cuda::stream
cudaStream_t stream
Definition: HistoContainer.h:57
dqm::RamdiskMonitor::runPath_
const std::string runPath_
Definition: RamdiskMonitor.cc:44
edm::LogPrint
Log< level::Warning, true > LogPrint
Definition: MessageLogger.h:130
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
dqm::RamdiskMonitor::StreamME::deliveryDelayMTime
MonitorElement * deliveryDelayMTime
Definition: RamdiskMonitor.cc:50
dqm::RamdiskMonitor::runInputDir_
const std::string runInputDir_
Definition: RamdiskMonitor.cc:42
DQMOneEDAnalyzer
Definition: DQMOneEDAnalyzer.h:20
DQMStore.h
DQMFileIterator.h
dqm::RamdiskMonitor::filesSeen_
std::set< std::string > filesSeen_
Definition: RamdiskMonitor.cc:55
dqm::RamdiskMonitor::bookHistograms
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
Definition: RamdiskMonitor.cc:71
mps_check.msg
tuple msg
Definition: mps_check.py:285
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
dqmservices::DQMFileIterator::LumiEntry
Definition: DQMFileIterator.h:20
dqm::RamdiskMonitor::globalBeginLuminosityBlock
std::shared_ptr< rdm::Empty > globalBeginLuminosityBlock(edm::LuminosityBlock const &lumi, edm::EventSetup const &eSetup) const override
Definition: RamdiskMonitor.cc:173
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
dqm::RamdiskMonitor::analyze
void analyze(edm::Event const &e, edm::EventSetup const &eSetup) override
Definition: RamdiskMonitor.cc:36
DQMOneEDAnalyzer.h
MakerMacros.h
dqm::RamdiskMonitor
Definition: RamdiskMonitor.cc:25
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
dqm::RamdiskMonitor::StreamME::eventsAccepted
MonitorElement * eventsAccepted
Definition: RamdiskMonitor.cc:47
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
ParameterSetDescription.h
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
dqm::RamdiskMonitor::LUMI
static constexpr double LUMI
Definition: RamdiskMonitor.cc:58
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
dqm::RamdiskMonitor::RamdiskMonitor
RamdiskMonitor(const edm::ParameterSet &ps)
Definition: RamdiskMonitor.cc:61
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
dqm::RamdiskMonitor::streamLabels_
const std::vector< std::string > streamLabels_
Definition: RamdiskMonitor.cc:43
cuy.ib
ib
Definition: cuy.py:662
edm::EventSetup
Definition: EventSetup.h:58
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
dqm::RamdiskMonitor::analyzeFile
void analyzeFile(std::string fn, unsigned int run, unsigned int lumi, std::string label) const
Definition: RamdiskMonitor.cc:122
writedatasetfile.run
run
Definition: writedatasetfile.py:27
dqm::RamdiskMonitor::globalEndLuminosityBlock
void globalEndLuminosityBlock(edm::LuminosityBlock const &lumi, edm::EventSetup const &eSetup) final
Definition: RamdiskMonitor.cc:35
dqm::RamdiskMonitor::StreamME::deliveryDelayCTime
MonitorElement * deliveryDelayCTime
Definition: RamdiskMonitor.cc:51
dqm::RamdiskMonitor::global_start_
double global_start_
Definition: RamdiskMonitor.cc:56
EventSetup.h
personalPlayback.fn
fn
Definition: personalPlayback.py:515
dqm::RamdiskMonitor::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: RamdiskMonitor.cc:225
dqm::implementation::IBooker
Definition: DQMStore.h:43
RamdiskMonitor
dqm::RamdiskMonitor RamdiskMonitor
Definition: RamdiskMonitor.cc:245
ztail.d
d
Definition: ztail.py:151
mps_fire.result
result
Definition: mps_fire.py:311
dqm
Definition: DQMStore.h:18
ParameterSet.h
edm_modernize_messagelogger.stat
stat
Definition: edm_modernize_messagelogger.py:27
edm::Event
Definition: Event.h:73
lumi
Definition: LumiSectionData.h:20
dqm::RamdiskMonitor::StreamME::eventsProcessed
MonitorElement * eventsProcessed
Definition: RamdiskMonitor.cc:48
dqm::rdm::Empty
Definition: RamdiskMonitor.cc:23
sistrip::runNumber_
static const char runNumber_[]
Definition: ConstantsForDqm.h:33
label
const char * label
Definition: PFTauDecayModeTools.cc:11
dqm::RamdiskMonitor::streams_
std::map< std::string, StreamME > streams_
Definition: RamdiskMonitor.cc:54
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37