CMS 3D CMS Logo

DQMFileSaverPB.cc
Go to the documentation of this file.
5 
6 #include "DQMFileSaverPB.h"
7 
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #include <iostream>
12 #include <vector>
13 #include <string>
14 #include <fstream>
15 #include <utility>
16 #include <TString.h>
17 #include <TSystem.h>
18 
19 #include <openssl/md5.h>
20 #include <boost/property_tree/json_parser.hpp>
21 #include <boost/filesystem.hpp>
22 #include <boost/format.hpp>
23 
24 using namespace dqm;
25 
27  : DQMFileSaverBase(ps) {
28 
29  fakeFilterUnitMode_ = ps.getUntrackedParameter<bool>("fakeFilterUnitMode", false);
30  streamLabel_ = ps.getUntrackedParameter<std::string>("streamLabel", "streamDQMHistograms");
31 
33  mergeType_ = "";
34 }
35 
37 
39  if (!fakeFilterUnitMode_) {
42  }
43 }
44 
46  // get from DAQ2 services where to store the files according to their format
47  namespace bpt = boost::property_tree;
48 
49  std::string openJsonFilePathName;
50  std::string jsonFilePathName;
51  std::string openHistoFilePathName;
52  std::string histoFilePathName;
53 
54  evf::FastMonitoringService *fms = nullptr;
56 
57  // create the files names
58  if (fakeFilterUnitMode_) {
59  std::string runDir = str(boost::format("%s/run%06d") % fp.path_ % fp.run_);
60  std::string baseName = str(boost::format("%s/run%06d_ls%04d_%s") % runDir % fp.run_ % fp.lumi_ % streamLabel_ );
61 
62  boost::filesystem::create_directories(runDir);
63 
64  jsonFilePathName = baseName + ".jsn";
65  openJsonFilePathName = jsonFilePathName + ".open";
66 
67  histoFilePathName = baseName + ".pb";
68  openHistoFilePathName = histoFilePathName + ".open";
69  } else {
70  openJsonFilePathName = edm::Service<evf::EvFDaqDirector>()->getOpenOutputJsonFilePath(fp.lumi_, streamLabel_);
71  jsonFilePathName = edm::Service<evf::EvFDaqDirector>()->getOutputJsonFilePath(fp.lumi_, streamLabel_);
72 
73  openHistoFilePathName = edm::Service<evf::EvFDaqDirector>()->getOpenProtocolBufferHistogramFilePath(fp.lumi_, streamLabel_);
74  histoFilePathName = edm::Service<evf::EvFDaqDirector>()->getProtocolBufferHistogramFilePath(fp.lumi_, streamLabel_);
75 
77  }
78 
79  if (fms ? fms->getEventsProcessedForLumi(fp.lumi_) : true) {
80  // Save the file in the open directory.
81  store->savePB(openHistoFilePathName, "",
82  store->mtEnabled() ? fp.run_ : 0,
83  fp.lumi_);
84 
85  // Now move the the data and json files into the output directory.
86  ::rename(openHistoFilePathName.c_str(), histoFilePathName.c_str());
87  }
88 
89  // Write the json file in the open directory.
90  bpt::ptree pt = fillJson(fp.run_, fp.lumi_, histoFilePathName, transferDestination_, mergeType_, fms);
91  write_json(openJsonFilePathName, pt);
92  ::rename(openJsonFilePathName.c_str(), jsonFilePathName.c_str());
93 }
94 
95 void DQMFileSaverPB::saveRun(const FileParameters& fp) const {
96  // no saving for the run
97 }
98 
99 boost::property_tree::ptree
100 DQMFileSaverPB::fillJson(int run, int lumi, const std::string& dataFilePathName, const std::string& transferDestinationStr, const std::string& mergeTypeStr, evf::FastMonitoringService *fms)
101 {
102  namespace bpt = boost::property_tree;
103  namespace bfs = boost::filesystem;
104 
105  bpt::ptree pt;
106 
107  int hostnameReturn;
108  char host[32];
109  hostnameReturn = gethostname(host ,sizeof(host));
110  if (hostnameReturn == -1)
111  throw cms::Exception("fillJson")
112  << "Internal error, cannot get host name";
113 
114  int pid = getpid();
115  std::ostringstream oss_pid;
116  oss_pid << pid;
117 
118  int nProcessed = fms ? (fms->getEventsProcessedForLumi(lumi)) : -1;
119 
120  // Stat the data file: if not there, throw
121  std::string dataFileName;
122  struct stat dataFileStat;
123  dataFileStat.st_size=0;
124  if (nProcessed) {
125  if (stat(dataFilePathName.c_str(), &dataFileStat) != 0)
126  throw cms::Exception("fillJson")
127  << "Internal error, cannot get data file: "
128  << dataFilePathName;
129  // Extract only the data file name from the full path
130  dataFileName = bfs::path(dataFilePathName).filename().string();
131  }
132  // The availability test of the FastMonitoringService was done in the ctor.
133  bpt::ptree data;
134  bpt::ptree processedEvents, acceptedEvents, errorEvents, bitmask, fileList, fileSize, inputFiles, fileAdler32, transferDestination, mergeType, hltErrorEvents;
135 
136  processedEvents.put("", nProcessed); // Processed events
137  acceptedEvents.put("", nProcessed); // Accepted events, same as processed for our purposes
138 
139  errorEvents.put("", 0); // Error events
140  bitmask.put("", 0); // Bitmask of abs of CMSSW return code
141  fileList.put("", dataFileName); // Data file the information refers to
142  fileSize.put("", dataFileStat.st_size); // Size in bytes of the data file
143  inputFiles.put("", ""); // We do not care about input files!
144  fileAdler32.put("", -1); // placeholder to match output json definition
145  transferDestination.put("", transferDestinationStr); // SM Transfer destination field
146  mergeType.put("", mergeTypeStr); // SM Transfer destination field
147  hltErrorEvents.put("", 0); // Error events
148 
149  data.push_back(std::make_pair("", processedEvents));
150  data.push_back(std::make_pair("", acceptedEvents));
151  data.push_back(std::make_pair("", errorEvents));
152  data.push_back(std::make_pair("", bitmask));
153  data.push_back(std::make_pair("", fileList));
154  data.push_back(std::make_pair("", fileSize));
155  data.push_back(std::make_pair("", inputFiles));
156  data.push_back(std::make_pair("", fileAdler32));
157  data.push_back(std::make_pair("", transferDestination));
158  data.push_back(std::make_pair("", mergeType));
159  data.push_back(std::make_pair("", hltErrorEvents));
160 
161  pt.add_child("data", data);
162 
163  if (fms == nullptr) {
164  pt.put("definition", "/fakeDefinition.jsn");
165  } else {
166  // The availability test of the EvFDaqDirector Service was done in the ctor.
167  bfs::path outJsonDefName(edm::Service<evf::EvFDaqDirector>()->baseRunDir()); //we assume this file is written bu the EvF Output module
168  outJsonDefName /= (std::string("output_") + oss_pid.str() + std::string(".jsd"));
169  pt.put("definition", outJsonDefName.string());
170  }
171 
172  char sourceInfo[64]; //host and pid information
173  sprintf(sourceInfo, "%s_%d", host, pid);
174  pt.put("source", sourceInfo);
175 
176  return pt;
177 }
178 
179 
181  edm::ConfigurationDescriptions& descriptions) {
182 
184  desc.setComment("Saves histograms from DQM store, HLT->pb workflow.");
185 
186  desc.addUntracked<bool>("fakeFilterUnitMode", false)->setComment(
187  "If set, EvFDaqDirector is emulated and not used.");
188 
189  desc.addUntracked<std::string>("streamLabel", "streamDQMHistograms")->setComment(
190  "Label of the stream.");
191 
193 
194  // Changed to use addDefault instead of add here because previously
195  // DQMFileSaverOnline and DQMFileSaverPB both used the module label
196  // "saver" which caused conflicting cfi filenames to be generated.
197  // add could be used if unique module labels were given.
198  descriptions.addDefault(desc);
199 }
200 
std::string streamLabel_
T getUntrackedParameter(std::string const &, T const &) const
host
Definition: query.py:114
unsigned int getEventsProcessedForLumi(unsigned int lumi, bool *abortFlag=0)
std::string transferDestination_
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void initRun() const override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
DQMFileSaverPB(const edm::ParameterSet &ps)
void saveRun(const FileParameters &fp) const override
void saveLumi(const FileParameters &fp) const override
~DQMFileSaverPB() override
void setComment(std::string const &value)
void addDefault(ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
format
Some error handling for the usage.
inputFiles
Definition: merge.py:5
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static void fillDescription(edm::ParameterSetDescription &d)
static boost::property_tree::ptree fillJson(int run, int lumi, const std::string &dataFilePathName, const std::string &transferDestinationStr, const std::string &mergeTypeStr, evf::FastMonitoringService *fms)
std::string mergeType_