CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMFileSaverOnline.cc
Go to the documentation of this file.
11 
12 #include "DQMFileSaverOnline.h"
13 
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <iostream>
18 #include <vector>
19 #include <string>
20 #include <fstream>
21 #include <utility>
22 #include <TString.h>
23 #include <TSystem.h>
24 
25 using namespace dqm;
26 
28  : DQMFileSaverBase(ps) {
29 
30  backupLumiCount_ = ps.getUntrackedParameter<int>("backupLumiCount", 1);
31 }
32 
34 
36  if (backupLumiCount_ > 0) {
37  if (fp.lumi_ % backupLumiCount_ == 0) {
38 
39  // actual saving is done here
40  makeSnapshot(fp, false);
41  }
42  }
43 }
44 
46  makeSnapshot(fp, true);
47 }
48 
49 void DQMFileSaverOnline::makeSnapshot(const FileParameters& fp, bool final) const {
50  int pid = getpid();
51  char hostname[64];
52  gethostname(hostname, 64);
53  hostname[63] = 0;
54 
55  char suffix[128];
56  if (!final) {
57  snprintf(suffix, 127, ".ls%08ld_host%s_pid%08d", fp.lumi_, hostname, pid);
58  } else {
59  suffix[0] = 0;
60  }
61 
62  std::string prefix = filename(fp, false);
63 
64  std::string root_fp = prefix + ".root" + suffix;
65  std::string meta_fp = prefix + ".root.origin" + suffix;
66 
67  std::string tmp_root_fp = root_fp + ".tmp";
68  std::string tmp_meta_fp = meta_fp + ".tmp";
69 
70  // run_ and lumi_ are ignored if dqmstore is not in multithread mode
72 
73  logFileAction("Writing DQM Root file: ", root_fp);
74  //logFileAction("Writing DQM Origin file: ", meta_fp);
75 
76  char rewrite[128];
77  snprintf(rewrite, 128, "\\1Run %ld/\\2/Run summary", fp.run_);
78 
79  store->save(tmp_root_fp, /* filename */
80  "", /* path */
81  "^(Reference/)?([^/]+)", /* pattern */
82  rewrite, /* rewrite */
83  store->mtEnabled() ? fp.run_ : 0, /* run */
84  0, /* lumi */
85  fp.saveReference_, /* ref */
86  fp.saveReferenceQMin_, /* ref minStatus */
87  "RECREATE", /* fileupdate */
88  false /* resetMEs */
89  );
90 
91  // write metadata
92  // format.origin: md5:d566a34b27f48d507150a332b189398b 294835 /home/dqmprolocal/output/DQM_V0001_FED_R000194224.root
93  std::ofstream meta_fd(tmp_meta_fp);
94  meta_fd << this->fillOrigin(tmp_root_fp, root_fp);
95  meta_fd.close();
96 
97  checkError("Rename failed: ", root_fp, ::rename(tmp_root_fp.c_str(), root_fp.c_str()));
98  checkError("Rename failed: ", meta_fp, ::rename(tmp_meta_fp.c_str(), meta_fp.c_str()));
99 
100  SnapshotFiles files = { root_fp, meta_fp };
101  if (final) {
102  // final will never be cleared
104 
105  saveJobReport(root_fp);
106  } else {
107  appendSnapshot(SnapshotFiles{ root_fp, meta_fp });
108  }
109 }
110 
112  std::lock_guard<std::mutex> lock(snapshots_lock_);
113 
114  while (! snapshots_.empty()) {
115  SnapshotFiles& x = snapshots_.front();
116 
117  //logFileAction("Deleting old snapshot (origin): ", x.meta);
118  checkError("Unlink failed: ", x.meta, ::unlink(x.meta.c_str()));
119 
120  logFileAction("Deleting old snapshot (root): ", x.data);
121  checkError("Unlink failed: ", x.data, ::unlink(x.data.c_str()));
122 
123  snapshots_.pop_front();
124  }
125 
126  if (! f.data.empty()) {
127  snapshots_.push_back(f);
128  }
129 }
130 
131 void DQMFileSaverOnline::checkError(const char *msg, const std::string file, int status) const {
132  if (status != 0) {
133  std::string actual_msg = msg;
134  actual_msg += std::strerror(status);
135  logFileAction(actual_msg, file);
136  }
137 }
138 
140  edm::ConfigurationDescriptions& descriptions) {
141 
143  desc.setComment("Saves histograms from DQM store, online workflow.");
144 
145  desc.addUntracked<int>("backupLumiCount", 10)->setComment(
146  "How often the backup file will be generated, in lumisections (-1 disables).");
147 
149 
150  descriptions.add("saver", desc);
151 }
152 
T getUntrackedParameter(std::string const &, T const &) const
void saveJobReport(const std::string &filename) const
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void saveRun(FileParameters fp) const
static const std::string fillOrigin(const std::string filename, const std::string final_filename)
static const std::string filename(FileParameters fp, bool useLumi=false)
void setComment(std::string const &value)
void appendSnapshot(SnapshotFiles new_snap) const
void checkError(const char *msg, const std::string file, int status) const
double f[11][100]
void logFileAction(const std::string &msg, const std::string &fileName) const
virtual void saveLumi(FileParameters fp) const
void makeSnapshot(const FileParameters &fp, bool final) const
std::list< SnapshotFiles > snapshots_
DQMStore::SaveReferenceTag saveReference_
tuple pid
Definition: sysUtil.py:22
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static void fillDescription(edm::ParameterSetDescription &d)
Definition: DDAxes.h:10
tuple status
Definition: ntuplemaker.py:245
DQMFileSaverOnline(const edm::ParameterSet &ps)