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