CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMFileSaver.cc
Go to the documentation of this file.
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include <iostream>
16 #include <vector>
17 #include <string>
18 #include <TString.h>
19 #include <TSystem.h>
20 
21 //--------------------------------------------------------
22 static void
23 getAnInt(const edm::ParameterSet &ps, int &value, const std::string &name)
24 {
25  value = ps.getUntrackedParameter<int>(name, value);
26  if (value < 1 && value != -1)
27  throw cms::Exception("DQMFileSaver")
28  << "Invalid '" << name << "' parameter '" << value
29  << "'. Must be -1 or >= 1.";
30 }
31 
32 void
33 DQMFileSaver::saveForOffline(const std::string &workflow, int run, int lumi)
34 {
35 
36  char suffix[64];
37  sprintf(suffix, "R%09d", run);
38 
39  char rewrite[128];
40  if (lumi == 0) // save for run
41  sprintf(rewrite, "\\1Run %d/\\2/Run summary", run);
42  else
43  sprintf(rewrite, "\\1Run %d/\\2/By Lumi Section %d-%d", irun_, ilumi_, ilumi_);
44 
45  size_t pos = 0;
46  std::string wflow;
47  wflow.reserve(workflow.size() + 3);
48  wflow = workflow;
49  while ((pos = wflow.find('/', pos)) != std::string::npos)
50  wflow.replace(pos++, 1, "__");
51 
52  std::string filename = fileBaseName_ + suffix + wflow + child_ + ".root";
53 
54  if (lumi == 0) // save for run
55  {
56  // set run end flag
57  dbe_->cd();
58  dbe_->setCurrentFolder("Info/ProvInfo");
59 
60  // do this, because ProvInfo is not yet run in offline DQM
61  MonitorElement* me = dbe_->get("Info/ProvInfo/CMSSW");
62  if (!me) me = dbe_->bookString("CMSSW",edm::getReleaseVersion().c_str() );
63 
64  me = dbe_->get("Info/ProvInfo/runIsComplete");
65  if (!me) me = dbe_->bookFloat("runIsComplete");
66 
67  if (me)
68  {
69  if (runIsComplete_)
70  me->Fill(1.);
71  else
72  me->Fill(0.);
73  }
74 
75  dbe_->save(filename,
76  "",
77  "^(Reference/)?([^/]+)",
78  rewrite,
81  fileUpdate_);
82  }
83  else // save EventInfo folders for luminosity sections
84  {
85  std::vector<std::string> systems = (dbe_->cd(), dbe_->getSubdirs());
86 
87  std::cout << " DQMFileSaver: storing EventInfo folders for Run: "
88  << irun_ << ", Lumi Section: " << ilumi_ << ", Subsystems: " ;
89 
90  for (size_t i = 0, e = systems.size(); i != e; ++i) {
91  if (systems[i] != "Reference") {
92  dbe_->cd();
93  std::cout << systems[i] << " " ;
94  dbe_->save(filename,
95  systems[i]+"/EventInfo", "^(Reference/)?([^/]+)", rewrite,
98  fileUpdate_);
99  // from now on update newly created file
100  if (fileUpdate_=="RECREATE") fileUpdate_="UPDATE";
101  }
102  }
103  std::cout << "\n";
104  }
105 
106  if (pastSavedFiles_.size() == 0)
107  {
108  // save JobReport upon creation of file (once per job)
109  saveJobReport(filename);
110  pastSavedFiles_.push_back(filename);
111  }
112 
113 }
114 
115 static void
116 doSaveForOnline(std::list<std::string> &pastSavedFiles,
117  size_t numKeepSavedFiles,
118  DQMStore *store,
119  const std::string &filename,
120  const std::string &directory,
121  const std::string &rxpat,
122  const std::string &rewrite,
124  int saveRefQMin)
125 {
126  store->save(filename, directory , rxpat,
127  rewrite, saveref, saveRefQMin);
128  pastSavedFiles.push_back(filename);
129  if (pastSavedFiles.size() > numKeepSavedFiles)
130  {
131  remove(pastSavedFiles.front().c_str());
132  pastSavedFiles.pop_front();
133  }
134 }
135 
136 void
137 DQMFileSaver::saveForOnline(const std::string &suffix, const std::string &rewrite)
138 {
139  std::vector<std::string> systems = (dbe_->cd(), dbe_->getSubdirs());
140 
141  for (size_t i = 0, e = systems.size(); i != e; ++i)
142  {
143  if (systems[i] != "Reference")
144  {
145  dbe_->cd();
146  if (MonitorElement* me = dbe_->get(systems[i] + "/EventInfo/processName"))
147  {
149  fileBaseName_ + me->getStringValue() + suffix + child_ + ".root",
150  "", "^(Reference/)?([^/]+)", rewrite,
153  return;
154  }
155  }
156  }
157 
158  // look for EventInfo folder in an unorthodox location
159  for (size_t i = 0, e = systems.size(); i != e; ++i)
160  if (systems[i] != "Reference")
161  {
162  dbe_->cd();
163  std::vector<MonitorElement*> pNamesVector = dbe_->getMatchingContents("^" + systems[i] + "/.*/EventInfo/processName",lat::Regexp::Perl);
164  if (pNamesVector.size() > 0){
166  fileBaseName_ + systems[i] + suffix + child_ + ".root",
167  "", "^(Reference/)?([^/]+)", rewrite,
170  pNamesVector.clear();
171  return;
172  }
173  }
174 
175  // if no EventInfo Folder is found, then store subsystem wise
176  for (size_t i = 0, e = systems.size(); i != e; ++i)
177  if (systems[i] != "Reference")
179  fileBaseName_ + systems[i] + suffix + child_ + ".root",
180  systems[i], "^(Reference/)?([^/]+)", rewrite,
183 }
184 
185 void
187 {
188 
189  // Report the file to job report service.
191  if (jr.isAvailable())
192  {
193  std::map<std::string, std::string> info;
194  info["Source"] = "DQMStore";
195  info["FileClass"] = "DQM";
196  jr->reportAnalysisFile(filename, info);
197  }
198 
199 }
200 
201 //--------------------------------------------------------
203  : convention_ (Offline),
204  workflow_ (""),
205  producer_ ("DQM"),
206  dirName_ ("."),
207  child_ (""),
208  version_ (1),
209  runIsComplete_ (false),
210  saveByLumiSection_ (-1),
211  saveByEvent_ (-1),
212  saveByMinute_ (-1),
213  saveByTime_ (-1),
214  saveByRun_ (1),
215  saveAtJobEnd_ (false),
216  saveReference_ (DQMStore::SaveWithReference),
217  saveReferenceQMin_ (dqm::qstatus::STATUS_OK),
218  forceRunNumber_ (-1),
219  fileBaseName_ (""),
220  fileUpdate_ ("RECREATE"),
221  dbe_ (&*edm::Service<DQMStore>()),
222  irun_ (-1),
223  ilumi_ (-1),
224  ilumiprev_ (-1),
225  ievent_ (-1),
226  nrun_ (0),
227  nlumi_ (0),
228  nevent_ (0),
229  numKeepSavedFiles_ (5)
230 {
231  // Determine the file saving convention, and adjust defaults accordingly.
232  std::string convention = ps.getUntrackedParameter<std::string>("convention", "Offline");
233  if (convention == "Offline")
235  else if (convention == "Online")
237  else
238  throw cms::Exception("DQMFileSaver")
239  << "Invalid 'convention' parameter '" << convention << "'."
240  << " Expected one of 'Online' or 'Offline'.";
241 
242  // If this isn't online convention, check workflow.
243  if (convention_ != Online)
244  {
245  workflow_ = ps.getUntrackedParameter<std::string>("workflow", workflow_);
246  if (workflow_.empty()
247  || workflow_[0] != '/'
248  || *workflow_.rbegin() == '/'
249  || std::count(workflow_.begin(), workflow_.end(), '/') != 3
250  || workflow_.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
251  "abcdefghijklmnopqrstuvwxyz"
252  "0123456789"
253  "-_/") != std::string::npos)
254  throw cms::Exception("DQMFileSaver")
255  << "Invalid 'workflow' parameter '" << workflow_
256  << "'. Expected '/A/B/C'.";
257  }
258  else if (! ps.getUntrackedParameter<std::string>("workflow", "").empty())
259  throw cms::Exception("DQMFileSaver")
260  << "The 'workflow' parameter must be empty in 'Online' convention.";
261  else // for online set parameters
262  {
263  workflow_="/Global/Online/P5";
264  }
265 
266  // Allow file producer to be set to specific values in certain conditions.
267  producer_ = ps.getUntrackedParameter<std::string>("producer", producer_);
268  if (convention_ == Online
269  && producer_ != "DQM"
270  && producer_ != "HLTDQM"
271  && producer_ != "Playback")
272  {
273  throw cms::Exception("DQMFileSaver")
274  << "Invalid 'producer' parameter '" << producer_
275  << "'. Expected 'DQM', 'HLTDQM' or 'Playback'.";
276  }
277  else if (convention_ != Online && producer_ != "DQM")
278  {
279  throw cms::Exception("DQMFileSaver")
280  << "Invalid 'producer' parameter '" << producer_
281  << "'. Expected 'DQM'.";
282  }
283 
284  // version number to be used in filename
285  version_ = ps.getUntrackedParameter<int>("version", version_);
286  // flag to signal that file contains data from complete run
287  runIsComplete_ = ps.getUntrackedParameter<bool>("runIsComplete", runIsComplete_);
288 
289  // Check how we should save the references.
290  std::string refsave = ps.getUntrackedParameter<std::string>("referenceHandling", "default");
291  if (refsave == "default")
292  ;
293  else if (refsave == "skip")
294  {
296  // std::cout << "skip saving all references" << std::endl;
297  }
298  else if (refsave == "all")
299  {
301  // std::cout << "saving all references" << std::endl;
302  }
303  else if (refsave == "qtests")
304  {
306  // std::cout << "saving qtest references" << std::endl;
307  }
308  else
309  throw cms::Exception("DQMFileSaver")
310  << "Invalid 'referenceHandling' parameter '" << refsave
311  << "'. Expected 'default', 'skip', 'all' or 'qtests'.";
312 
313  // Check minimum required quality test result for which reference is saved.
314  saveReferenceQMin_ = ps.getUntrackedParameter<int>("referenceRequireStatus", saveReferenceQMin_);
315 
316  // Get and check the output directory.
317  struct stat s;
318  dirName_ = ps.getUntrackedParameter<std::string>("dirName", dirName_);
319  if (dirName_.empty() || stat(dirName_.c_str(), &s) == -1)
320  throw cms::Exception("DQMFileSaver")
321  << "Invalid 'dirName' parameter '" << dirName_ << "'.";
322 
323  // Find out when and how to save files. The following contraints apply:
324  // - For online, allow files to be saved at event and time intervals.
325  // - For online and offline, allow files to be saved per run, lumi and job end
326  // - For offline allow run number to be overridden (for mc data).
327  if (convention_ == Online)
328  {
329  getAnInt(ps, saveByEvent_, "saveByEvent");
330  getAnInt(ps, saveByMinute_, "saveByMinute");
331  getAnInt(ps, saveByTime_, "saveByTime");
332  getAnInt(ps, numKeepSavedFiles_, "maxSavedFilesCount");
333  }
334 
335  if (convention_ == Online || convention_ == Offline)
336  {
337  getAnInt(ps, saveByRun_, "saveByRun");
338  getAnInt(ps, saveByLumiSection_, "saveByLumiSection");
339  }
340 
341  if (convention_ != Online)
342  {
343  getAnInt(ps, forceRunNumber_, "forceRunNumber");
344  saveAtJobEnd_ = ps.getUntrackedParameter<bool>("saveAtJobEnd", saveAtJobEnd_);
345  }
346 
347  if (saveAtJobEnd_ && forceRunNumber_ < 1)
348  throw cms::Exception("DQMFileSaver")
349  << "If saving at the end of the job, the run number must be"
350  << " overridden to a specific value using 'forceRunNumber'.";
351 
352 
353  // Set up base file name and determine the start time.
354  char version[8];
355  sprintf(version, "_V%04d_", int(version_));
356  version[7]='\0';
357  fileBaseName_ = dirName_ + "/" + producer_ + version;
358  gettimeofday(&start_, 0);
359  saved_ = start_;
360 
361  // Log some information what we will do.
362  edm::LogInfo("DQMFileSaver")
363  << "DQM file saving settings:\n"
364  << " using base file name '" << fileBaseName_ << "'\n"
365  << " forcing run number " << forceRunNumber_ << "\n"
366  << " saving every " << saveByLumiSection_ << " lumi section(s)\n"
367  << " saving every " << saveByEvent_ << " event(s)\n"
368  << " saving every " << saveByMinute_ << " minute(s)\n"
369  << " saving every 2^n*" << saveByTime_ << " minutes \n"
370  << " saving every " << saveByRun_ << " run(s)\n"
371  << " saving at job end: " << (saveAtJobEnd_ ? "yes" : "no") << "\n"
372  << " keeping at most " << numKeepSavedFiles_ << " files\n";
373 }
374 
375 //--------------------------------------------------------
376 void
378 {
379  irun_ = ilumi_ = ilumiprev_ = ievent_ = -1;
380  nrun_ = nlumi_ = nevent_ = 0;
381 }
382 
383 void
385 {
386  irun_ = (forceRunNumber_ == -1 ? r.id().run() : forceRunNumber_);
387  ++nrun_;
388 }
389 
390 void
392 {
393  ilumi_ = l.id().luminosityBlock();
394  if (ilumiprev_ == -1) ilumiprev_ = ilumi_;
395  ++nlumi_;
396 }
397 
399 {
400  ++nevent_;
401 
402  ievent_ = e.id().event();
403 
404  // Check if we should save for this event.
405  char suffix[64];
406  if (ievent_ > 0 && saveByEvent_ > 0 && nevent_ == saveByEvent_)
407  {
408  if (convention_ != Online)
409  throw cms::Exception("DQMFileSaver")
410  << "Internal error, can save files by event"
411  << " only in Online mode.";
412 
413  sprintf(suffix, "_R%09d_E%08d", irun_, ievent_);
414  saveForOnline(suffix, "\\1\\2");
415  nevent_ = 0;
416  }
417 
418  // Check if we should save due to elapsed time.
419  if ( ievent_ > 0 && ( saveByMinute_ > 0 || saveByTime_ > 0 ) )
420  {
421  if (convention_ != Online)
422  throw cms::Exception("DQMFileSaver")
423  << "Internal error, can save files by time"
424  << " only in Online mode.";
425 
426  // Compute elapsed time in minutes.
427  struct timeval tv;
428  gettimeofday(&tv, 0);
429 
430  double totalelapsed = ((tv.tv_sec + tv.tv_usec*1e-6)
431  - (start_.tv_sec + start_.tv_usec*1e-6)) / 60;
432  double elapsed = ((tv.tv_sec + tv.tv_usec*1e-6)
433  - (saved_.tv_sec + saved_.tv_usec*1e-6)) / 60;
434 
435  // Save if enough time has elapsed since the last save.
436  if ( (saveByMinute_ > 0 && elapsed > saveByMinute_ ) ||
437  (saveByTime_ > 0 && totalelapsed > saveByTime_ ) )
438  {
439  if ( saveByTime_ > 0 ) saveByTime_ *= 2;
440  saved_ = tv;
441  sprintf(suffix, "_R%09d_T%08d", irun_, int(totalelapsed));
442  char rewrite[64]; sprintf(rewrite, "\\1Run %d/\\2/Run summary", irun_);
443  saveForOnline(suffix, rewrite);
444  }
445  }
446 }
447 
448 void
450 {
451 
452  if (ilumi_ > 0 && saveByLumiSection_ > 0 )
453  {
454  if (convention_ != Online && convention_ != Offline )
455  throw cms::Exception("DQMFileSaver")
456  << "Internal error, can save files at end of lumi block"
457  << " only in Online or Offline mode.";
458 
459  if (convention_ == Online && nlumi_ == saveByLumiSection_) // insist on lumi section ordering
460  {
461  char suffix[64];
462  char rewrite[128];
463  sprintf(suffix, "_R%09d_L%06d", irun_, ilumi_);
464  sprintf(rewrite, "\\1Run %d/\\2/By Lumi Section %d-%d", irun_, ilumiprev_, ilumi_);
465  saveForOnline(suffix, rewrite);
466  ilumiprev_ = -1;
467  nlumi_ = 0;
468  }
469  if (convention_ == Offline)
471  }
472 }
473 
474 void
476 {
477  if (irun_ > 0 && saveByRun_ > 0 && nrun_ == saveByRun_)
478  {
479  if (convention_ == Online)
480  {
481  char suffix[64]; sprintf(suffix, "_R%09d", irun_);
482  char rewrite[64]; sprintf(rewrite, "\\1Run %d/\\2/Run summary", irun_);
483  saveForOnline(suffix, rewrite);
484  }
485  else if (convention_ == Offline)
487  else
488  throw cms::Exception("DQMFileSaver")
489  << "Internal error. Can only save files in endRun()"
490  << " in Online and Offline modes.";
491 
492  nrun_ = 0;
493  }
494 }
495 
496 void
498 {
499  if (saveAtJobEnd_)
500  {
501  if (convention_ == Offline && forceRunNumber_ > 0)
503  else
504  throw cms::Exception("DQMFileSaver")
505  << "Internal error. Can only save files at the end of the"
506  << " job in Offline mode with run number overridden.";
507  }
508 
509 }
510 
511 void
512 DQMFileSaver::postForkReacquireResources(unsigned int childIndex, unsigned int numberOfChildren)
513 {
514  // this is copied from IOPool/Output/src/PoolOutputModule.cc, for consistency
515  unsigned int digits = 0;
516  while (numberOfChildren != 0) {
517  ++digits;
518  numberOfChildren /= 10;
519  }
520  // protect against zero numberOfChildren
521  if (digits == 0) {
522  digits = 3;
523  }
524 
525  char buffer[digits + 2];
526  snprintf(buffer, digits + 2, "_%0*d", digits, childIndex);
527  child_ = std::string(buffer);
528 }
LuminosityBlockID id() const
EventNumber_t event() const
Definition: EventID.h:44
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
DQMFileSaver(const edm::ParameterSet &ps)
int forceRunNumber_
Definition: DQMFileSaver.h:52
int saveReferenceQMin_
Definition: DQMFileSaver.h:51
RunID const & id() const
Definition: RunBase.h:41
std::vector< std::string > getSubdirs(void) const
Definition: DQMStore.cc:1419
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
RunNumber_t run() const
Definition: RunID.h:44
std::string fileUpdate_
Definition: DQMFileSaver.h:55
void saveForOnline(const std::string &suffix, const std::string &rewrite)
Convention convention_
Definition: DQMFileSaver.h:36
void cd(void)
go to top directory (ie. root)
Definition: DQMStore.cc:406
tuple lumi
Definition: fjr2json.py:35
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE")
Definition: DQMStore.cc:2113
void reportAnalysisFile(std::string const &fileName, std::map< std::string, std::string > const &fileData)
Definition: JobReport.cc:685
std::list< std::string > pastSavedFiles_
Definition: DQMFileSaver.h:70
std::vector< MonitorElement * > getMatchingContents(const std::string &pattern, lat::Regexp::Syntax syntaxType=lat::Regexp::Wildcard) const
Definition: DQMStore.cc:1691
DQMStore * dbe_
Definition: DQMFileSaver.h:57
std::string dirName_
Definition: DQMFileSaver.h:39
void saveJobReport(const std::string &filename)
std::string fileBaseName_
Definition: DQMFileSaver.h:54
static void doSaveForOnline(std::list< std::string > &pastSavedFiles, size_t numKeepSavedFiles, DQMStore *store, const std::string &filename, const std::string &directory, const std::string &rxpat, const std::string &rewrite, DQMStore::SaveReferenceTag saveref, int saveRefQMin)
MonitorElement * bookFloat(const char *name)
Book float.
Definition: DQMStore.cc:654
SaveReferenceTag
Definition: DQMStore.h:70
void Fill(long long x)
virtual void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
int saveByLumiSection_
Definition: DQMFileSaver.h:44
static void getAnInt(const edm::ParameterSet &ps, int &value, const std::string &name)
Definition: DQMFileSaver.cc:23
MonitorElement * bookString(const char *name, const char *value)
Book string.
Definition: DQMStore.cc:683
bool isAvailable() const
Definition: Service.h:47
bool runIsComplete_
Definition: DQMFileSaver.h:42
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1468
timeval start_
Definition: DQMFileSaver.h:66
DQMStore * dbe_
std::string workflow_
Definition: DQMFileSaver.h:37
std::string getReleaseVersion()
std::string producer_
Definition: DQMFileSaver.h:38
std::string child_
Definition: DQMFileSaver.h:40
int saveReference_
Definition: DQMFileSaver.h:50
LuminosityBlockNumber_t luminosityBlock() const
virtual void endLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &)
edm::EventID id() const
Definition: EventBase.h:56
bool saveAtJobEnd_
Definition: DQMFileSaver.h:49
void saveForOffline(const std::string &workflow, int run, int lumi)
Definition: DQMFileSaver.cc:33
virtual void analyze(const edm::Event &e, const edm::EventSetup &)
tuple filename
Definition: lut2db_cfg.py:20
timeval saved_
Definition: DQMFileSaver.h:67
static const int STATUS_OK
virtual void postForkReacquireResources(unsigned int childIndex, unsigned int numberOfChildren)
virtual void endRun(const edm::Run &, const edm::EventSetup &)
tuple cout
Definition: gather_cfg.py:121
virtual void beginJob(void)
virtual void endJob(void)
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:429
Definition: Run.h:33
int numKeepSavedFiles_
Definition: DQMFileSaver.h:69