CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 }
34 
36 
38  if (!fakeFilterUnitMode_) {
40  }
41 }
42 
44  // get from DAQ2 services where to store the files according to their format
45  namespace bpt = boost::property_tree;
46 
47  std::string openJsonFilePathName;
48  std::string jsonFilePathName;
49  std::string openHistoFilePathName;
50  std::string histoFilePathName;
51 
52  evf::FastMonitoringService *fms = nullptr;
54 
55  // create the files names
56  if (fakeFilterUnitMode_) {
57  std::string runDir = str(boost::format("%s/run%06d") % fp.path_ % fp.run_);
58  std::string baseName = str(boost::format("%s/run%06d_ls%04d_%s") % runDir % fp.run_ % fp.lumi_ % streamLabel_ );
59 
60  boost::filesystem::create_directories(runDir);
61 
62  jsonFilePathName = baseName + ".jsn";
63  openJsonFilePathName = jsonFilePathName + ".open";
64 
65  histoFilePathName = baseName + ".pb";
66  openHistoFilePathName = histoFilePathName + ".open";
67  } else {
68  openJsonFilePathName = edm::Service<evf::EvFDaqDirector>()->getOpenOutputJsonFilePath(fp.lumi_, streamLabel_);
69  jsonFilePathName = edm::Service<evf::EvFDaqDirector>()->getOutputJsonFilePath(fp.lumi_, streamLabel_);
70 
71  openHistoFilePathName = edm::Service<evf::EvFDaqDirector>()->getOpenProtocolBufferHistogramFilePath(fp.lumi_, streamLabel_);
72  histoFilePathName = edm::Service<evf::EvFDaqDirector>()->getProtocolBufferHistogramFilePath(fp.lumi_, streamLabel_);
73 
75  }
76 
77  if (fms ? fms->getEventsProcessedForLumi(fp.lumi_) : true) {
78  // Save the file in the open directory.
79  store->savePB(openHistoFilePathName, "",
80  store->mtEnabled() ? fp.run_ : 0,
81  fp.lumi_,
82  true);
83 
84  // Now move the the data and json files into the output directory.
85  ::rename(openHistoFilePathName.c_str(), histoFilePathName.c_str());
86  }
87 
88  // Write the json file in the open directory.
89  bpt::ptree pt = fillJson(fp.run_, fp.lumi_, histoFilePathName, transferDestination_, fms);
90  write_json(openJsonFilePathName, pt);
91  ::rename(openJsonFilePathName.c_str(), jsonFilePathName.c_str());
92 }
93 
95  // no saving for the run
96 }
97 
98 boost::property_tree::ptree
99 DQMFileSaverPB::fillJson(int run, int lumi, const std::string& dataFilePathName, const std::string transferDestinationStr, evf::FastMonitoringService *fms)
100 {
101  namespace bpt = boost::property_tree;
102  namespace bfs = boost::filesystem;
103 
104  bpt::ptree pt;
105 
106  int hostnameReturn;
107  char host[32];
108  hostnameReturn = gethostname(host ,sizeof(host));
109  if (hostnameReturn == -1)
110  throw cms::Exception("fillJson")
111  << "Internal error, cannot get host name";
112 
113  int pid = getpid();
114  std::ostringstream oss_pid;
115  oss_pid << pid;
116 
117  int nProcessed = fms ? (fms->getEventsProcessedForLumi(lumi)) : -1;
118 
119  // Stat the data file: if not there, throw
120  std::string dataFileName;
121  struct stat dataFileStat;
122  dataFileStat.st_size=0;
123  if (nProcessed) {
124  if (stat(dataFilePathName.c_str(), &dataFileStat) != 0)
125  throw cms::Exception("fillJson")
126  << "Internal error, cannot get data file: "
127  << dataFilePathName;
128  // Extract only the data file name from the full path
129  dataFileName = bfs::path(dataFilePathName).filename().string();
130  }
131  // The availability test of the FastMonitoringService was done in the ctor.
132  bpt::ptree data;
133  bpt::ptree processedEvents, acceptedEvents, errorEvents, bitmask, fileList, fileSize, inputFiles, fileAdler32, transferDestination;
134 
135  processedEvents.put("", nProcessed); // Processed events
136  acceptedEvents.put("", nProcessed); // Accepted events, same as processed for our purposes
137 
138  errorEvents.put("", 0); // Error events
139  bitmask.put("", 0); // Bitmask of abs of CMSSW return code
140  fileList.put("", dataFileName); // Data file the information refers to
141  fileSize.put("", dataFileStat.st_size); // Size in bytes of the data file
142  inputFiles.put("", ""); // We do not care about input files!
143  fileAdler32.put("", -1); // placeholder to match output json definition
144  transferDestination.put("", transferDestinationStr); // SM Transfer destination field
145 
146  data.push_back(std::make_pair("", processedEvents));
147  data.push_back(std::make_pair("", acceptedEvents));
148  data.push_back(std::make_pair("", errorEvents));
149  data.push_back(std::make_pair("", bitmask));
150  data.push_back(std::make_pair("", fileList));
151  data.push_back(std::make_pair("", fileSize));
152  data.push_back(std::make_pair("", inputFiles));
153  data.push_back(std::make_pair("", fileAdler32));
154  data.push_back(std::make_pair("", transferDestination));
155 
156  pt.add_child("data", data);
157 
158  if (fms == nullptr) {
159  pt.put("definition", "/fakeDefinition.jsn");
160  } else {
161  // The availability test of the EvFDaqDirector Service was done in the ctor.
162  bfs::path outJsonDefName(edm::Service<evf::EvFDaqDirector>()->baseRunDir()); //we assume this file is written bu the EvF Output module
163  outJsonDefName /= (std::string("output_") + oss_pid.str() + std::string(".jsd"));
164  pt.put("definition", outJsonDefName.string());
165  }
166 
167  char sourceInfo[64]; //host and pid information
168  sprintf(sourceInfo, "%s_%d", host, pid);
169  pt.put("source", sourceInfo);
170 
171  return pt;
172 }
173 
174 
176  edm::ConfigurationDescriptions& descriptions) {
177 
179  desc.setComment("Saves histograms from DQM store, HLT->pb workflow.");
180 
181  desc.addUntracked<bool>("fakeFilterUnitMode", false)->setComment(
182  "If set, EvFDaqDirector is emulated and not used.");
183 
184  desc.addUntracked<std::string>("streamLabel", "streamDQMHistograms")->setComment(
185  "Label of the stream.");
186 
188  descriptions.add("saver", desc);
189 }
190 
std::string streamLabel_
T getUntrackedParameter(std::string const &, T const &) const
unsigned int getEventsProcessedForLumi(unsigned int lumi, bool *abortFlag=0)
std::string transferDestination_
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
tuple lumi
Definition: fjr2json.py:35
DQMFileSaverPB(const edm::ParameterSet &ps)
string format
Some error handling for the usage.
void setComment(std::string const &value)
virtual void initRun() const override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
string host
Definition: query.py:114
static boost::property_tree::ptree fillJson(int run, int lumi, const std::string &dataFilePathName, const std::string transferDestinationStr, evf::FastMonitoringService *fms)
tuple pid
Definition: sysUtil.py:22
void add(std::string const &label, ParameterSetDescription const &psetDescription)
virtual void saveLumi(const FileParameters &fp) const override
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
tuple inputFiles
Definition: merge.py:5
virtual void saveRun(const FileParameters &fp) const override
static void fillDescription(edm::ParameterSetDescription &d)