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