CMS 3D CMS Logo

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