CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMFileSaverBase.cc
Go to the documentation of this file.
12 
13 #include "DQMFileSaverBase.h"
14 
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <iostream>
19 #include <vector>
20 #include <string>
21 #include <fstream>
22 #include <utility>
23 #include <TString.h>
24 #include <TSystem.h>
25 
26 #include <openssl/md5.h>
27 #include <boost/iostreams/device/mapped_file.hpp>
28 #include <boost/filesystem.hpp>
29 
30 using namespace dqm;
31 
34 
35  fp.path_ = ps.getUntrackedParameter<std::string>("path");
36  fp.producer_ = ps.getUntrackedParameter<std::string>("producer");
37  fp.run_ = 0;
38  fp.tag_ = ps.getUntrackedParameter<std::string>("tag");
39  fp.lumi_ = 0;
40  fp.version_ = 1;
41  fp.child_ = "";
42 
44  // Check how we should save the references.
45  std::string refsave = ps.getUntrackedParameter<std::string>("referenceHandling", "all");
46  if (refsave == "skip")
47  {
49  }
50  else if (refsave == "all")
51  {
53  }
54  else if (refsave == "qtests")
55  {
57  }
58  else {
59  //edm::LogInfo("DQMFileSaverBase")
60  std::cerr
61  << "Invalid 'referenceHandling' parameter '" << refsave
62  << "'. Expected 'skip', 'all' or 'qtests'.";
63 
64  }
65 
66  // Check minimum required quality test result for which reference is saved.
67  fp.saveReferenceQMin_ = ps.getUntrackedParameter<int>("referenceRequireStatus", dqm::qstatus::STATUS_OK);
68 
69  std::unique_lock<std::mutex> lck(initial_fp_lock_);
70  initial_fp_ = fp;
71 }
72 
74 
76 
77 std::shared_ptr<NoCache> DQMFileSaverBase::globalBeginRun(
78  const edm::Run &r, const edm::EventSetup &) const {
79 
80  return nullptr;
81 }
82 
84  const edm::LuminosityBlock &l, const edm::EventSetup &) const {
85 
86  return nullptr;
87 }
88 
90  const edm::EventSetup &) const {
91  // not supported
92 }
93 
95  const edm::EventSetup &) const {
96  int ilumi = iLS.id().luminosityBlock();
97  int irun = iLS.id().run();
98 
99  std::unique_lock<std::mutex> lck(initial_fp_lock_);
101  lck.unlock();
102 
103  fp.lumi_ = ilumi;
104  fp.run_ = irun;
105 
107 
108  this->saveLumi(fp);
109 
110  store->markForDeletion(store->mtEnabled() ? irun : 0, ilumi);
111 }
112 
114  const edm::EventSetup &) const {
115 
116  std::unique_lock<std::mutex> lck(initial_fp_lock_);
118  lck.unlock();
119 
120  fp.run_ = iRun.id().run();
121 
122  // empty
123  this->saveRun(fp);
124 }
125 
127 
129  unsigned int childIndex, unsigned int numberOfChildren) {
130  // this is copied from IOPool/Output/src/PoolOutputModule.cc, for consistency
131  unsigned int digits = 0;
132  while (numberOfChildren != 0) {
133  ++digits;
134  numberOfChildren /= 10;
135  }
136  // protect against zero numberOfChildren
137  if (digits == 0) {
138  digits = 3;
139  }
140 
141  char buffer[digits + 2];
142  snprintf(buffer, digits + 2, "_F%0*d", digits, childIndex);
143 
144  std::unique_lock<std::mutex> lck(initial_fp_lock_);
145  initial_fp_.child_ = std::string(buffer);
146 }
147 
149  char buf[256];
150  if (useLumi) {
151  snprintf(buf, 256, "%s_V%04d_%s_R%09ld_L%09ld%s", fp.producer_.c_str(),
152  fp.version_, fp.tag_.c_str(), fp.run_, fp.lumi_,
153  fp.child_.c_str());
154  } else {
155  snprintf(buf, 256, "%s_V%04d_%s_R%09ld%s", fp.producer_.c_str(), fp.version_,
156  fp.tag_.c_str(), fp.run_, fp.child_.c_str());
157  }
158  buf[255] = 0;
159 
160  namespace fs = boost::filesystem;
161  fs::path path(fp.path_);
162  fs::path file(buf);
163 
164  return (path / file).string();
165 }
166 
167 // file metadata saving stuff
168 boost::property_tree::ptree
169 DQMFileSaverBase::fillJson(int run, int lumi, const std::string& dataFilePathName, const std::string transferDestinationStr, evf::FastMonitoringService *fms)
170 {
171  namespace bpt = boost::property_tree;
172  namespace bfs = boost::filesystem;
173 
174  bpt::ptree pt;
175 
176  int hostnameReturn;
177  char host[32];
178  hostnameReturn = gethostname(host ,sizeof(host));
179  if (hostnameReturn == -1)
180  throw cms::Exception("fillJson")
181  << "Internal error, cannot get host name";
182 
183  int pid = getpid();
184  std::ostringstream oss_pid;
185  oss_pid << pid;
186 
187  // Stat the data file: if not there, throw
188  struct stat dataFileStat;
189  if (stat(dataFilePathName.c_str(), &dataFileStat) != 0)
190  throw cms::Exception("fillJson")
191  << "Internal error, cannot get data file: "
192  << dataFilePathName;
193  // Extract only the data file name from the full path
194  std::string dataFileName = bfs::path(dataFilePathName).filename().string();
195  // The availability test of the FastMonitoringService was done in the ctor.
196  bpt::ptree data;
197  bpt::ptree processedEvents, acceptedEvents, errorEvents, bitmask, fileList, fileSize, inputFiles, fileAdler32, transferDestination;
198 
199  processedEvents.put("", fms ? (fms->getEventsProcessedForLumi(lumi)) : -1); // Processed events
200  acceptedEvents.put("", fms ? (fms->getEventsProcessedForLumi(lumi)) : -1); // Accepted events, same as processed for our purposes
201 
202  errorEvents.put("", 0); // Error events
203  bitmask.put("", 0); // Bitmask of abs of CMSSW return code
204  fileList.put("", dataFileName); // Data file the information refers to
205  fileSize.put("", dataFileStat.st_size); // Size in bytes of the data file
206  inputFiles.put("", ""); // We do not care about input files!
207  fileAdler32.put("", -1); // placeholder to match output json definition
208  transferDestination.put("", transferDestinationStr); // SM Transfer destination field
209 
210  data.push_back(std::make_pair("", processedEvents));
211  data.push_back(std::make_pair("", acceptedEvents));
212  data.push_back(std::make_pair("", errorEvents));
213  data.push_back(std::make_pair("", bitmask));
214  data.push_back(std::make_pair("", fileList));
215  data.push_back(std::make_pair("", fileSize));
216  data.push_back(std::make_pair("", inputFiles));
217  data.push_back(std::make_pair("", fileAdler32));
218  data.push_back(std::make_pair("", transferDestination));
219 
220  pt.add_child("data", data);
221 
222  if (fms == nullptr) {
223  pt.put("definition", "/fakeDefinition.jsn");
224  } else {
225  // The availability test of the EvFDaqDirector Service was done in the ctor.
226  bfs::path outJsonDefName(edm::Service<evf::EvFDaqDirector>()->baseRunDir()); //we assume this file is written bu the EvF Output module
227  outJsonDefName /= (std::string("output_") + oss_pid.str() + std::string(".jsd"));
228  pt.put("definition", outJsonDefName.string());
229  }
230 
231  char sourceInfo[64]; //host and pid information
232  sprintf(sourceInfo, "%s_%d", host, pid);
233  pt.put("source", sourceInfo);
234 
235  return pt;
236 }
237 
239  const std::string final_filename) {
240 
241  // format.origin (one line):
242  // md5:d566a34b27f48d507150a332b189398b 294835 final_filename.root
243 
244  unsigned char md5[MD5_DIGEST_LENGTH];
245 
246  boost::iostreams::mapped_file_source fp(filename);
247 
248  MD5((unsigned char *)fp.data(), fp.size(), md5);
249 
250  std::ostringstream hash;
251  for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
252  hash << std::hex << std::setfill('0') << std::setw(2) << (int)(md5[i]);
253  }
254 
255  std::ostringstream out;
256  out << "md5:" << hash.str() << " " << fp.size() << " " << final_filename;
257  return out.str();
258 }
259 
261 {
262  // Report the file to job report service.
264  if (jr.isAvailable())
265  {
266  std::map<std::string, std::string> info;
267  info["Source"] = "DQMStore";
268  info["FileClass"] = "DQM";
269  jr->reportAnalysisFile(filename, info);
270  }
271 
272 }
273 
275  edm::LogAbsolute("fileAction") << std::setprecision(0) << edm::TimeOfDay()
276  << " " << msg << fileName;
278 }
279 
281  desc.addUntracked<std::string>("tag", "UNKNOWN")
282  ->setComment("File tag, DQM_V000_<TAG>*, usually a subsytem name.");
283 
284  desc.addUntracked<std::string>("producer", "DQM")
285  ->setComment("Base prefix for files, <BASE>_V000_**, either 'DQM' or 'Playback'.");
286 
287  desc.addUntracked<std::string>("referenceHandling", "all")
288  ->setComment("saveReference_, passed to the DQMStore");
289 
290  desc.addUntracked<int>("referenceRequireStatus", dqm::qstatus::STATUS_OK)
291  ->setComment("saveReference_, passed to the DQMStore");
292 
293  desc.addUntracked<std::string>("path", "./")->setComment(
294  "Output path prefix.");
295 }
DQMFileSaverBase(const edm::ParameterSet &ps)
LuminosityBlockID id() const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
unsigned int getEventsProcessedForLumi(unsigned int lumi, bool *abortFlag=0)
static const TGPicture * info(bool iBackgroundIsBlack)
void saveJobReport(const std::string &filename) const
RunID const & id() const
Definition: RunBase.h:41
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
RunNumber_t run() const
Definition: RunID.h:39
void FlushMessageLog()
tuple lumi
Definition: fjr2json.py:35
static const std::string fillOrigin(const std::string filename, const std::string final_filename)
static const std::string filename(FileParameters fp, bool useLumi=false)
virtual void endJob(void)
virtual void analyze(edm::StreamID, const edm::Event &e, const edm::EventSetup &) const
virtual void beginJob(void)
virtual void globalEndRun(const edm::Run &, const edm::EventSetup &) const
tuple path
else: Piece not in the list, fine.
virtual void postForkReacquireResources(unsigned int childIndex, unsigned int numberOfChildren)
bool isAvailable() const
Definition: Service.h:46
RunNumber_t run() const
virtual std::shared_ptr< NoCache > globalBeginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) const
void logFileAction(const std::string &msg, const std::string &fileName) const
virtual std::shared_ptr< NoCache > globalBeginRun(const edm::Run &, const edm::EventSetup &) const
tuple out
Definition: dbtoconf.py:99
string host
Definition: query.py:114
DQMStore::SaveReferenceTag saveReference_
tuple pid
Definition: sysUtil.py:22
LuminosityBlockNumber_t luminosityBlock() const
virtual void saveLumi(FileParameters fp) const
FileParameters initial_fp_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void reportAnalysisFile(std::string const &fileName, std::map< std::string, std::string > const &fileData)
Definition: JobReport.cc:559
virtual void saveRun(FileParameters fp) const
tuple filename
Definition: lut2db_cfg.py:20
static boost::property_tree::ptree fillJson(int run, int lumi, const std::string &dataFilePathName, const std::string transferDestinationStr, evf::FastMonitoringService *fms)
static const int STATUS_OK
tuple inputFiles
Definition: merge.py:5
static void fillDescription(edm::ParameterSetDescription &d)
virtual void globalEndLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) const
Definition: Run.h:41