CMS 3D CMS Logo

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