CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMFileIterator.cc
Go to the documentation of this file.
1 #include "DQMFileIterator.h"
4 
5 #include <boost/regex.hpp>
6 #include <boost/format.hpp>
7 #include <boost/range.hpp>
8 #include <boost/filesystem.hpp>
9 #include <boost/algorithm/string/predicate.hpp>
10 
11 #include <memory>
12 #include <string>
13 #include <iterator>
14 #include <boost/property_tree/json_parser.hpp>
15 #include <boost/property_tree/ptree.hpp>
16 
17 namespace dqmservices {
18 
20  const std::string& filename, int lumiNumber, unsigned int datafn_position) {
21  boost::property_tree::ptree pt;
22  read_json(filename, pt);
23 
25  lumi.filename = filename;
26 
27  // We rely on n_events to be the first item on the array...
28  lumi.n_events = std::next(pt.get_child("data").begin(), 1)
29  ->second.get_value<std::size_t>();
30 
31  lumi.ls = lumiNumber;
32  lumi.datafilename = std::next(pt.get_child("data").begin(), datafn_position)
33  ->second.get_value<std::string>();
34 
35  lumi.loaded = true;
36  return lumi;
37 }
38 
39 // Contents of Eor json file are ignored for the moment.
40 // This function will not be called.
42  const std::string& filename) {
43  boost::property_tree::ptree pt;
44  read_json(filename, pt);
45 
46  EorEntry eor;
47  eor.filename = filename;
48 
49  // We rely on n_events to be the first item on the array...
50  eor.n_events = std::next(pt.get_child("data").begin(), 1)
51  ->second.get_value<std::size_t>();
52  eor.n_lumi = std::next(pt.get_child("data").begin(), 2)
53  ->second.get_value<std::size_t>();
54  eor.datafilename = std::next(pt.get_child("data").begin(), 2)
55  ->second.get_value<std::string>();
56 
57  eor.loaded = true;
58  return eor;
59 }
60 
62  : state_(EOR) {
63 
64  runNumber_ = pset.getUntrackedParameter<unsigned int>("runNumber");
65  datafnPosition_ = pset.getUntrackedParameter<unsigned int>("datafnPosition");
66  runInputDir_ = pset.getUntrackedParameter<std::string>("runInputDir");
67  streamLabel_ = pset.getUntrackedParameter<std::string>("streamLabel");
68  delayMillis_ = pset.getUntrackedParameter<uint32_t>("delayMillis");
70  pset.getUntrackedParameter<int32_t>("nextLumiTimeoutMillis");
71 
73  reset();
74 }
75 
77 
79  runPath_ = str(boost::format("%s/run%06d") % runInputDir_ % runNumber_);
80 
81  eor_.loaded = false;
82  state_ = State::OPEN;
83  currentLumi_ = 1;
84  lumiSeen_.clear();
85 
87 
88  collect(true);
89  update_state();
90 }
91 
93 
95  return lumiSeen_[currentLumi_];
96 }
97 
100 }
101 
103  if (lumiSeen_.find(currentLumi_) != lumiSeen_.end()) {
104  return true;
105  }
106 
107  return false;
108 }
109 
110 unsigned int DQMFileIterator::runNumber() { return runNumber_; }
111 
113  if (!lumiSeen_.empty()) {
114  return lumiSeen_.rbegin()->first;
115  }
116 
117  return 1;
118 }
119 
121  using boost::property_tree::ptree;
122  using boost::str;
123 
124  unsigned int prev_lumi = currentLumi_;
125 
126  currentLumi_ = lumi;
128 
129  // report the successful lumi file open
130  if (mon_.isAvailable()) {
131  ptree children;
132 
133  auto iter = lumiSeen_.begin();
134  for (; iter != lumiSeen_.end(); ++iter) {
135  children.put(std::to_string(iter->first), iter->second.filename);
136  }
137 
138  mon_->registerExtra("lumiSeen", children);
139  mon_->reportLumiSection(runNumber_, prev_lumi);
140  }
141 }
142 
144  if (boost::starts_with(lumi.datafilename, "/")) return lumi.datafilename;
145 
147  p /= lumi.datafilename;
148  return p.string();
149 }
150 
151 void DQMFileIterator::collect(bool ignoreTimers) {
152  // search filesystem to find available lumi section files
153  // or the end of run files
154 
156  auto last_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
158 
159  // don't refresh if it's too soon
160  if ((!ignoreTimers) && (last_ms < 100)) {
161  return;
162  }
163 
164  // check if directory changed
165  std::time_t t = boost::filesystem::last_write_time(runPath_);
166 
167  if ((!ignoreTimers) && (last_ms < forceFileCheckTimeoutMillis_) && (t == runPathMTime_)) {
168  //logFileAction("Directory hasn't changed.");
169  return;
170  } else {
171  //logFileAction("Directory changed, updating.");
172  }
173 
174  runPathMTime_ = t;
176 
177  using boost::filesystem::directory_iterator;
178  using boost::filesystem::directory_entry;
179 
180  std::string fn_eor;
181 
182  directory_iterator dend;
183  for (directory_iterator di(runPath_); di != dend; ++di) {
184  const boost::regex fn_re("run(\\d+)_ls(\\d+)(_.*).jsn");
185 
186  const std::string filename = di->path().filename().string();
187  const std::string fn = di->path().string();
188 
189  boost::smatch result;
190  if (boost::regex_match(filename, result, fn_re)) {
191  unsigned int run = std::stoi(result[1]);
192  unsigned int lumi = std::stoi(result[2]);
193  std::string label = result[3];
194 
195  if (run != runNumber_) continue;
196 
197  // check if this is EoR
198  // for various reasons we have to load it after all other files
199  if ((lumi == 0) && (label == "_EoR") && (!eor_.loaded)) {
200  fn_eor = fn;
201  continue;
202  }
203 
204  // check if lumi is loaded
205  if (lumiSeen_.find(lumi) != lumiSeen_.end()) {
206  continue; // already loaded
207  }
208 
209  // check if this belongs to us
210  if (label != streamLabel_) {
211  logFileAction("Found and skipped json file (stream label mismatch): ",
212  fn);
213  continue;
214  }
215 
216  LumiEntry lumi_jsn = LumiEntry::load_json(fn, lumi, datafnPosition_);
217  lumiSeen_.emplace(lumi, lumi_jsn);
218  logFileAction("Found and loaded json file: ", fn);
219  }
220  }
221 
222  if (!fn_eor.empty()) {
223  logFileAction("EoR file found: ", fn_eor);
224 
225  // @TODO load EoR files correctly
226  // eor_ = EorEntry::load_json(fn_eor);
227  // logFileAction("Loaded eor file: ", fn_eor);
228 
229  // for now , set n_lumi to the highest _found_ lumi
230  eor_.loaded = true;
231 
232  if (lumiSeen_.empty()) {
233  eor_.n_lumi = 0;
234  } else {
235  eor_.n_lumi = lumiSeen_.rbegin()->first;
236  }
237  }
238 }
239 
241  using std::chrono::high_resolution_clock;
242  using std::chrono::duration_cast;
243  using std::chrono::milliseconds;
244 
245  collect(false);
246 
247  // now update the state
248  State old_state = state_;
249 
250  if ((state_ == State::OPEN) && (eor_.loaded)) {
251  state_ = State::EOR_CLOSING;
252  }
253 
254  // special case for missing lumi files
255  // skip to the next available, but after the timeout
256  if ((state_ != State::EOR) && (nextLumiTimeoutMillis_ >= 0)) {
257  auto iter = lumiSeen_.lower_bound(currentLumi_);
258  if ((iter != lumiSeen_.end()) && iter->first != currentLumi_) {
259 
260  auto elapsed = high_resolution_clock::now() - lastLumiLoad_;
261  auto elapsed_ms = duration_cast<milliseconds>(elapsed).count();
262 
263  if (elapsed_ms >= nextLumiTimeoutMillis_) {
264  std::string msg("Timeout reached, skipping lumisection(s) ");
265  msg += std::to_string(currentLumi_) + " .. " +
266  std::to_string(iter->first - 1);
267  msg += ", currentLumi_ is now " + std::to_string(iter->first);
268 
269  logFileAction(msg);
270 
271  currentLumi_ = iter->first;
272  }
273  }
274  }
275 
276  if (state_ == State::EOR_CLOSING) {
277  // check if we parsed all lumis
278  // n_lumi is both last lumi and the number of lumi
279  // since lumis are indexed from 1
280 
281  // after all lumi have been pop()'ed
282  // current lumi will become larger than the last lumi
283  if (currentLumi_ > eor_.n_lumi) {
284  state_ = State::EOR;
285  }
286  }
287 
288  if (state_ != old_state) {
289  logFileAction("Streamer state changed: ",
290  std::to_string(old_state) + "->" + std::to_string(state_));
291  }
292 }
293 
295  const std::string& fileName) const {
296  edm::LogAbsolute("fileAction") << std::setprecision(0) << edm::TimeOfDay()
297  << " " << msg << fileName;
299 }
300 
302  const char* x = getenv("WATCHDOG_FD");
303  if (x) {
304  int fd = atoi(x);
305  write(fd, ".\n", 2);
306  }
307 }
308 
310  //logFileAction("Streamer waiting for the next LS.");
311 
312  updateWatchdog();
313  usleep(delayMillis_ * 1000);
314  updateWatchdog();
315 }
316 
318 
319  desc.addUntracked<unsigned int>("runNumber")
320  ->setComment("Run number passed via configuration file.");
321 
322  desc.addUntracked<unsigned int>("datafnPosition", 3)
323  ->setComment("Data filename position in the positional arguments array 'data' in json file.");
324 
325  desc.addUntracked<std::string>("streamLabel")
326  ->setComment("Stream label used in json discovery.");
327 
328  desc.addUntracked<uint32_t>("delayMillis")
329  ->setComment("Number of milliseconds to wait between file checks.");
330 
331  desc.addUntracked<int32_t>("nextLumiTimeoutMillis", -1)->setComment(
332  "Number of milliseconds to wait before switching to the next lumi "
333  "section if the current is missing, -1 to disable.");
334 
335  desc.addUntracked<std::string>("runInputDir")
336  ->setComment("Directory where the DQM files will appear.");
337 }
338 
339 } /* end of namespace */
T getUntrackedParameter(std::string const &, T const &) const
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void FlushMessageLog()
edm::Service< DQMMonitoringService > mon_
tuple lumi
Definition: fjr2json.py:35
std::map< unsigned int, LumiEntry > lumiSeen_
string format
Some error handling for the usage.
void advanceToLumi(unsigned int lumi)
std::string make_path_data(const LumiEntry &lumi)
U second(std::pair< T, U > const &p)
std::string to_string(const T &t)
Definition: Logger.cc:26
DQMFileIterator(edm::ParameterSet const &pset)
tuple path
else: Piece not in the list, fine.
tuple result
Definition: query.py:137
void logFileAction(const std::string &msg, const std::string &fileName="") const
static EorEntry load_json(const std::string &filename)
std::chrono::high_resolution_clock::time_point lastLumiLoad_
static LumiEntry load_json(const std::string &filename, int lumiNumber, unsigned int datafn_position)
tuple filename
Definition: lut2db_cfg.py:20
std::chrono::high_resolution_clock::time_point runPathLastCollect_
Definition: DDAxes.h:10
static void fillDescription(edm::ParameterSetDescription &d)
void collect(bool ignoreTimers)