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  true);
85 
86  // Now move the the data and json files into the output directory.
87  ::rename(openHistoFilePathName.c_str(), histoFilePathName.c_str());
88  }
89 
90  // Write the json file in the open directory.
91  bpt::ptree pt = fillJson(fp.run_, fp.lumi_, histoFilePathName, transferDestination_, mergeType_, fms);
92  write_json(openJsonFilePathName, pt);
93  ::rename(openJsonFilePathName.c_str(), jsonFilePathName.c_str());
94 }
95 
96 void DQMFileSaverPB::saveRun(const FileParameters& fp) const {
97  // no saving for the run
98 }
99 
100 boost::property_tree::ptree
101 DQMFileSaverPB::fillJson(int run, int lumi, const std::string& dataFilePathName, const std::string transferDestinationStr, const std::string mergeTypeStr, evf::FastMonitoringService *fms)
102 {
103  namespace bpt = boost::property_tree;
104  namespace bfs = boost::filesystem;
105 
106  bpt::ptree pt;
107 
108  int hostnameReturn;
109  char host[32];
110  hostnameReturn = gethostname(host ,sizeof(host));
111  if (hostnameReturn == -1)
112  throw cms::Exception("fillJson")
113  << "Internal error, cannot get host name";
114 
115  int pid = getpid();
116  std::ostringstream oss_pid;
117  oss_pid << pid;
118 
119  int nProcessed = fms ? (fms->getEventsProcessedForLumi(lumi)) : -1;
120 
121  // Stat the data file: if not there, throw
122  std::string dataFileName;
123  struct stat dataFileStat;
124  dataFileStat.st_size=0;
125  if (nProcessed) {
126  if (stat(dataFilePathName.c_str(), &dataFileStat) != 0)
127  throw cms::Exception("fillJson")
128  << "Internal error, cannot get data file: "
129  << dataFilePathName;
130  // Extract only the data file name from the full path
131  dataFileName = bfs::path(dataFilePathName).filename().string();
132  }
133  // The availability test of the FastMonitoringService was done in the ctor.
134  bpt::ptree data;
135  bpt::ptree processedEvents, acceptedEvents, errorEvents, bitmask, fileList, fileSize, inputFiles, fileAdler32, transferDestination, mergeType, hltErrorEvents;
136 
137  processedEvents.put("", nProcessed); // Processed events
138  acceptedEvents.put("", nProcessed); // Accepted events, same as processed for our purposes
139 
140  errorEvents.put("", 0); // Error events
141  bitmask.put("", 0); // Bitmask of abs of CMSSW return code
142  fileList.put("", dataFileName); // Data file the information refers to
143  fileSize.put("", dataFileStat.st_size); // Size in bytes of the data file
144  inputFiles.put("", ""); // We do not care about input files!
145  fileAdler32.put("", -1); // placeholder to match output json definition
146  transferDestination.put("", transferDestinationStr); // SM Transfer destination field
147  mergeType.put("", mergeTypeStr); // SM Transfer destination field
148  hltErrorEvents.put("", 0); // Error events
149 
150  data.push_back(std::make_pair("", processedEvents));
151  data.push_back(std::make_pair("", acceptedEvents));
152  data.push_back(std::make_pair("", errorEvents));
153  data.push_back(std::make_pair("", bitmask));
154  data.push_back(std::make_pair("", fileList));
155  data.push_back(std::make_pair("", fileSize));
156  data.push_back(std::make_pair("", inputFiles));
157  data.push_back(std::make_pair("", fileAdler32));
158  data.push_back(std::make_pair("", transferDestination));
159  data.push_back(std::make_pair("", mergeType));
160  data.push_back(std::make_pair("", hltErrorEvents));
161 
162  pt.add_child("data", data);
163 
164  if (fms == nullptr) {
165  pt.put("definition", "/fakeDefinition.jsn");
166  } else {
167  // The availability test of the EvFDaqDirector Service was done in the ctor.
168  bfs::path outJsonDefName(edm::Service<evf::EvFDaqDirector>()->baseRunDir()); //we assume this file is written bu the EvF Output module
169  outJsonDefName /= (std::string("output_") + oss_pid.str() + std::string(".jsd"));
170  pt.put("definition", outJsonDefName.string());
171  }
172 
173  char sourceInfo[64]; //host and pid information
174  sprintf(sourceInfo, "%s_%d", host, pid);
175  pt.put("source", sourceInfo);
176 
177  return pt;
178 }
179 
180 
182  edm::ConfigurationDescriptions& descriptions) {
183 
185  desc.setComment("Saves histograms from DQM store, HLT->pb workflow.");
186 
187  desc.addUntracked<bool>("fakeFilterUnitMode", false)->setComment(
188  "If set, EvFDaqDirector is emulated and not used.");
189 
190  desc.addUntracked<std::string>("streamLabel", "streamDQMHistograms")->setComment(
191  "Label of the stream.");
192 
194 
195  // Changed to use addDefault instead of add here because previously
196  // DQMFileSaverOnline and DQMFileSaverPB both used the module label
197  // "saver" which caused conflicting cfi filenames to be generated.
198  // add could be used if unique module labels were given.
199  descriptions.addDefault(desc);
200 }
201 
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)
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)
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)
std::string mergeType_