CMS 3D CMS Logo

DQMFileSaverPB.cc
Go to the documentation of this file.
1 #include <filesystem>
2 #include <fstream>
3 #include <iostream>
4 #include <string>
5 #include <utility>
6 #include <vector>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <boost/property_tree/json_parser.hpp>
11 #include <openssl/md5.h>
12 #include <fmt/printf.h>
13 
14 #include <google/protobuf/io/coded_stream.h>
15 #include <google/protobuf/io/gzip_stream.h>
16 #include <google/protobuf/io/zero_copy_stream_impl.h>
17 
18 #include <TString.h>
19 #include <TSystem.h>
20 #include <TBufferFile.h>
21 
22 #include "zlib.h"
28 
29 #include "DQMFileSaverPB.h"
30 
31 using namespace dqm;
32 
34  fakeFilterUnitMode_ = ps.getUntrackedParameter<bool>("fakeFilterUnitMode", false);
35  streamLabel_ = ps.getUntrackedParameter<std::string>("streamLabel", "streamDQMHistograms");
36  tag_ = ps.getUntrackedParameter<std::string>("tag", "UNKNOWN");
37 
39  mergeType_ = "";
40 
41  // If tag is set we're running in a DQM Live mode.
42  // Snapshot files will be saved for every client, then they will be merged and uploaded to the new DQM GUI.
43  if (tag_ != "UNKNOWN") {
44  streamLabel_ = "DQMLive";
45  }
46 }
47 
49 
51  if (!fakeFilterUnitMode_) {
54  }
55 
56  if (!fakeFilterUnitMode_) {
58  const std::string initFileName = daqDirector->getInitFilePath(streamLabel_);
59  std::ofstream file(initFileName);
60  file.close();
61  }
62 }
63 
65  // get from DAQ2 services where to store the files according to their format
66  namespace bpt = boost::property_tree;
67 
68  std::string openJsonFilePathName;
69  std::string jsonFilePathName;
70  std::string openHistoFilePathName;
71  std::string histoFilePathName;
72 
73  evf::FastMonitoringService* fms = nullptr;
75 
76  // create the files names
77  if (fakeFilterUnitMode_) {
78  std::string runDir = fmt::sprintf("%s/run%06d", fp.path_, fp.run_);
79  std::string baseName = "";
80  std::filesystem::create_directories(runDir);
81  // If tag is configured, append it to the name of the resulting file.
82  // This differentiates files saved by different clients.
83  // If tag is not configured, we don't add it at all to keep the old behaviour unchanged.
84  if (tag_ == "UNKNOWN") {
85  baseName = fmt::sprintf("%s/run%06d_ls%04d_%s", runDir, fp.run_, fp.lumi_, streamLabel_);
86  } else {
87  baseName = fmt::sprintf("%s/run%06d_%s_%s", runDir, fp.run_, tag_, streamLabel_);
88  }
89 
90  jsonFilePathName = baseName + ".jsn";
91  openJsonFilePathName = jsonFilePathName + ".open";
92 
93  histoFilePathName = baseName + ".pb";
94  openHistoFilePathName = histoFilePathName + ".open";
95  } else {
96  openJsonFilePathName = edm::Service<evf::EvFDaqDirector>()->getOpenOutputJsonFilePath(fp.lumi_, streamLabel_);
97  jsonFilePathName = edm::Service<evf::EvFDaqDirector>()->getOutputJsonFilePath(fp.lumi_, streamLabel_);
98 
99  openHistoFilePathName =
100  edm::Service<evf::EvFDaqDirector>()->getOpenProtocolBufferHistogramFilePath(fp.lumi_, streamLabel_);
101  histoFilePathName = edm::Service<evf::EvFDaqDirector>()->getProtocolBufferHistogramFilePath(fp.lumi_, streamLabel_);
102 
104  }
105 
106  if (fms ? fms->getEventsProcessedForLumi(fp.lumi_) : true) {
107  // Save the file in the open directory.
108  this->savePB(&*store, openHistoFilePathName, fp.run_, fp.lumi_);
109 
110  // Now move the the data and json files into the output directory.
111  ::rename(openHistoFilePathName.c_str(), histoFilePathName.c_str());
112  }
113 
114  // Write the json file in the open directory.
115  bpt::ptree pt = fillJson(fp.run_, fp.lumi_, histoFilePathName, transferDestination_, mergeType_, fms);
116  write_json(openJsonFilePathName, pt);
117  ::rename(openJsonFilePathName.c_str(), jsonFilePathName.c_str());
118 }
119 
121  // no saving for the run
122 }
123 
124 boost::property_tree::ptree DQMFileSaverPB::fillJson(int run,
125  int lumi,
126  const std::string& dataFilePathName,
127  const std::string& transferDestinationStr,
128  const std::string& mergeTypeStr,
130  namespace bpt = boost::property_tree;
131  namespace bfs = std::filesystem;
132 
133  bpt::ptree pt;
134 
135  int hostnameReturn;
136  char host[32];
137  hostnameReturn = gethostname(host, sizeof(host));
138  if (hostnameReturn == -1)
139  throw cms::Exception("fillJson") << "Internal error, cannot get host name";
140 
141  int pid = getpid();
142  std::ostringstream oss_pid;
143  oss_pid << pid;
144 
145  int nProcessed = fms ? (fms->getEventsProcessedForLumi(lumi)) : -1;
146 
147  // Stat the data file: if not there, throw
148  std::string dataFileName;
149  struct stat dataFileStat;
150  dataFileStat.st_size = 0;
151  if (nProcessed) {
152  if (stat(dataFilePathName.c_str(), &dataFileStat) != 0)
153  throw cms::Exception("fillJson") << "Internal error, cannot get data file: " << dataFilePathName;
154  // Extract only the data file name from the full path
155  dataFileName = bfs::path(dataFilePathName).filename().string();
156  }
157  // The availability test of the FastMonitoringService was done in the ctor.
158  bpt::ptree data;
159  bpt::ptree processedEvents, acceptedEvents, errorEvents, bitmask, fileList, fileSize, inputFiles, fileAdler32,
160  transferDestination, mergeType, hltErrorEvents;
161 
162  processedEvents.put("", nProcessed); // Processed events
163  acceptedEvents.put("", nProcessed); // Accepted events, same as processed for our purposes
164 
165  errorEvents.put("", 0); // Error events
166  bitmask.put("", 0); // Bitmask of abs of CMSSW return code
167  fileList.put("", dataFileName); // Data file the information refers to
168  fileSize.put("", dataFileStat.st_size); // Size in bytes of the data file
169  inputFiles.put("", ""); // We do not care about input files!
170  fileAdler32.put("", -1); // placeholder to match output json definition
171  transferDestination.put("", transferDestinationStr); // SM Transfer destination field
172  mergeType.put("", mergeTypeStr); // SM Transfer destination field
173  hltErrorEvents.put("", 0); // Error events
174 
175  data.push_back(std::make_pair("", processedEvents));
176  data.push_back(std::make_pair("", acceptedEvents));
177  data.push_back(std::make_pair("", errorEvents));
178  data.push_back(std::make_pair("", bitmask));
179  data.push_back(std::make_pair("", fileList));
180  data.push_back(std::make_pair("", fileSize));
181  data.push_back(std::make_pair("", inputFiles));
182  data.push_back(std::make_pair("", fileAdler32));
183  data.push_back(std::make_pair("", transferDestination));
184  data.push_back(std::make_pair("", mergeType));
185  data.push_back(std::make_pair("", hltErrorEvents));
186 
187  pt.add_child("data", data);
188 
189  if (fms == nullptr) {
190  pt.put("definition", "/fakeDefinition.jsn");
191  } else {
192  // The availability test of the EvFDaqDirector Service was done in the ctor.
193  bfs::path outJsonDefName{
194  edm::Service<evf::EvFDaqDirector>()->baseRunDir()}; //we assume this file is written bu the EvF Output module
195  outJsonDefName /= (std::string("output_") + oss_pid.str() + std::string(".jsd"));
196  pt.put("definition", outJsonDefName.string());
197  }
198 
199  char sourceInfo[64]; //host and pid information
200  sprintf(sourceInfo, "%s_%d", host, pid);
201  pt.put("source", sourceInfo);
202 
203  return pt;
204 }
205 
208  desc.setComment("Saves histograms from DQM store, HLT->pb workflow.");
209 
210  desc.addUntracked<bool>("fakeFilterUnitMode", false)->setComment("If set, EvFDaqDirector is emulated and not used.");
211 
212  desc.addUntracked<std::string>("streamLabel", "streamDQMHistograms")->setComment("Label of the stream.");
213 
215 
216  // Changed to use addDefault instead of add here because previously
217  // DQMFileSaverOnline and DQMFileSaverPB both used the module label
218  // "saver" which caused conflicting cfi filenames to be generated.
219  // add could be used if unique module labels were given.
220  descriptions.addDefault(desc);
221 }
222 
223 void DQMFileSaverPB::savePB(DQMStore* store, std::string const& filename, int run, int lumi) const {
224  using google::protobuf::io::FileOutputStream;
225  using google::protobuf::io::GzipOutputStream;
226  using google::protobuf::io::StringOutputStream;
227 
228  unsigned int nme = 0;
229 
230  dqmstorepb::ROOTFilePB dqmstore_message;
231 
232  // We save all histograms, indifferent of the lumi flag: even tough we save per lumi, this is a *snapshot*.
233  auto mes = store->getAllContents("");
234  for (auto const me : mes) {
235  TBufferFile buffer(TBufferFile::kWrite);
236  if (me->kind() < MonitorElement::Kind::TH1F) {
237  TObjString object(me->tagString().c_str());
238  buffer.WriteObject(&object);
239  } else {
240  buffer.WriteObject(me->getRootObject());
241  }
242  dqmstorepb::ROOTFilePB::Histo& histo = *dqmstore_message.add_histo();
243  histo.set_full_pathname(me->getFullname());
244  uint32_t flags = 0;
245  flags |= (uint32_t)me->kind();
246  if (me->getLumiFlag())
248  if (me->getEfficiencyFlag())
250  histo.set_flags(flags);
251  histo.set_size(buffer.Length());
252 
253  if (tag_ == "UNKNOWN") {
254  histo.set_streamed_histo((void const*)buffer.Buffer(), buffer.Length());
255  } else {
256  // Compress ME blob with zlib
257  int maxOutputSize = this->getMaxCompressedSize(buffer.Length());
258  char compression_output[maxOutputSize];
259  uLong total_out = this->compressME(buffer, maxOutputSize, compression_output);
260  histo.set_streamed_histo(compression_output, total_out);
261  }
262 
263  // Save quality reports
264  for (const auto& qr : me->getQReports()) {
266  // TODO: 64 is likely too short; memory corruption in the old code?
267  char buf[64];
268  std::snprintf(buf, sizeof(buf), "qr=st:%d:%.*g:", qr->getStatus(), DBL_DIG + 2, qr->getQTresult());
269  result = '<' + me->getName() + '.' + qr->getQRName() + '>';
270  result += buf;
271  result += qr->getAlgorithm() + ':' + qr->getMessage();
272  result += "</" + me->getName() + '.' + qr->getQRName() + '>';
273  TObjString str(result.c_str());
274 
275  dqmstorepb::ROOTFilePB::Histo& qr_histo = *dqmstore_message.add_histo();
276  TBufferFile qr_buffer(TBufferFile::kWrite);
277  qr_buffer.WriteObject(&str);
278  qr_histo.set_full_pathname(me->getFullname() + '.' + qr->getQRName());
279  qr_histo.set_flags(static_cast<uint32_t>(MonitorElement::Kind::STRING));
280  qr_histo.set_size(qr_buffer.Length());
281  // qr_histo.set_streamed_histo((void const*)qr_buffer.Buffer(), qr_buffer.Length());
282 
283  if (tag_ == "UNKNOWN") {
284  qr_histo.set_streamed_histo((void const*)qr_buffer.Buffer(), qr_buffer.Length());
285  } else {
286  // Compress ME blob with zlib
287  int maxOutputSize = this->getMaxCompressedSize(qr_buffer.Length());
288  char compression_output[maxOutputSize];
289  uLong total_out = this->compressME(qr_buffer, maxOutputSize, compression_output);
290  qr_histo.set_streamed_histo(compression_output, total_out);
291  }
292  }
293 
294  // Save efficiency tag, if any.
295  // XXX not supported by protobuf files.
296 
297  // Save tag if any.
298  // XXX not supported by protobuf files.
299 
300  // Count saved histograms
301  ++nme;
302  }
303 
304  int filedescriptor =
305  ::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
306  FileOutputStream file_stream(filedescriptor);
307  if (tag_ == "UNKNOWN") {
309  options.format = GzipOutputStream::GZIP;
310  options.compression_level = 1;
311  GzipOutputStream gzip_stream(&file_stream, options);
312  dqmstore_message.SerializeToZeroCopyStream(&gzip_stream);
313 
314  // Flush the internal streams
315  gzip_stream.Close();
316  file_stream.Close();
317  } else {
318  // We zlib compressed individual MEs so no need to compress the entire file again.
319  dqmstore_message.SerializeToZeroCopyStream(&file_stream);
320 
321  // Flush the internal stream
322  file_stream.Close();
323  }
324 
325  // Close the file descriptor
326  ::close(filedescriptor);
327 
328  // Maybe make some noise.
329  edm::LogInfo("DQMFileSaverPB") << "savePB: successfully wrote " << nme << " objects "
330  << "into DQM file '" << filename << "'\n";
331 }
332 
333 int DQMFileSaverPB::getMaxCompressedSize(int bufferSize) const {
334  // When input data is very badly compressable, zlib will add overhead instead of reducing the size.
335  // There is a minor amount of overhead (6 bytes overall and 5 bytes per 16K block) that is taken
336  // into consideration here to find out potential absolute maximum size of the output.
337  int n16kBlocks = (bufferSize + 16383) / 16384; // round up any fraction of a block
338  int maxOutputSize = bufferSize + 6 + (n16kBlocks * 5);
339  return maxOutputSize;
340 }
341 
342 ulong DQMFileSaverPB::compressME(const TBufferFile& buffer, int maxOutputSize, char* compression_output) const {
343  z_stream deflateStream;
344  deflateStream.zalloc = Z_NULL;
345  deflateStream.zfree = Z_NULL;
346  deflateStream.opaque = Z_NULL;
347  deflateStream.avail_in = (uInt)buffer.Length() + 1; // size of input, string + terminator
348  deflateStream.next_in = (Bytef*)buffer.Buffer(); // input array
349  deflateStream.avail_out = (uInt)maxOutputSize; // size of output
350  deflateStream.next_out = (Bytef*)compression_output; // output array, result will be placed here
351 
352  // The actual compression
353  deflateInit(&deflateStream, Z_BEST_COMPRESSION);
354  deflate(&deflateStream, Z_FINISH);
355  deflateEnd(&deflateStream);
356 
357  return deflateStream.total_out;
358 }
359 
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
dqm::DQMFileSaverBase::FileParameters
Definition: DQMFileSaverBase.h:33
Options
std::vector< std::shared_ptr< fireworks::OptionNode > > Options
Definition: FWExpressionValidator.cc:28
evf::FastMonitoringService::getEventsProcessedForLumi
unsigned int getEventsProcessedForLumi(unsigned int lumi, bool *abortFlag=nullptr)
Definition: FastMonitoringService.cc:708
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
dqmstorepb::ROOTFilePB::add_histo
::dqmstorepb::ROOTFilePB_Histo * add_histo()
Definition: ROOTFilePB.pb.h:615
LuminosityBlock.h
dqm::DQMFileSaverBase::filename
static const std::string filename(const FileParameters &fp, bool useLumi=false)
Definition: DQMFileSaverBase.cc:92
MonitorElementData::Kind::TH1F
dqm::DQMFileSaverPB::getMaxCompressedSize
int getMaxCompressedSize(int bufferSize) const
Definition: DQMFileSaverPB.cc:333
dqm::DQMFileSaverPB::DQMFileSaverPB
DQMFileSaverPB(const edm::ParameterSet &ps)
Definition: DQMFileSaverPB.cc:33
MonitorElementData::Kind::STRING
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
dqm::DQMFileSaverPB::fakeFilterUnitMode_
bool fakeFilterUnitMode_
Definition: DQMFileSaverPB.h:35
timingPdfMaker.histo
histo
Definition: timingPdfMaker.py:279
DQMStore.h
personalPlayback.fp
fp
Definition: personalPlayback.py:523
DQMFileSaverPB.h
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
dqm::implementation::IGetter::getAllContents
virtual std::vector< dqm::harvesting::MonitorElement * > getAllContents(std::string const &path) const
Definition: DQMStore.cc:609
query.host
string host
Definition: query.py:115
edmScanValgrind.buffer
buffer
Definition: edmScanValgrind.py:171
ROOTFilePB.pb.h
evf::EvFDaqDirector::getInitFilePath
std::string getInitFilePath(std::string const &stream) const
Definition: EvFDaqDirector.cc:439
options
Definition: options.py:1
evf::FastMonitoringService
Definition: FastMonitoringService.h:71
MakerMacros.h
dqmstorepb::ROOTFilePB_Histo
Definition: ROOTFilePB.pb.h:74
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
dqm::legacy::DQMStore
Definition: DQMStore.h:727
hgcalPlots.stat
stat
Definition: hgcalPlots.py:1119
Service.h
DQMNet::DQM_PROP_EFFICIENCY_PLOT
static const uint32_t DQM_PROP_EFFICIENCY_PLOT
Definition: DQMNet.h:64
str
#define str(s)
Definition: TestProcessor.cc:51
dqm::DQMFileSaverPB::savePB
void savePB(DQMStore *store, std::string const &filename, int run, int lumi) const
Definition: DQMFileSaverPB.cc:223
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
dqm::DQMFileSaverPB::tag_
std::string tag_
Definition: DQMFileSaverPB.h:37
dqm::DQMFileSaverBase
Definition: DQMFileSaverBase.h:23
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::ParameterSet
Definition: ParameterSet.h:47
dqm::DQMFileSaverPB::transferDestination_
std::string transferDestination_
Definition: DQMFileSaverPB.h:38
dqm::DQMFileSaverPB::mergeType_
std::string mergeType_
Definition: DQMFileSaverPB.h:39
dqm::DQMFileSaverPB::compressME
ulong compressME(const TBufferFile &buffer, int maxOutputSize, char *compression_output) const
Definition: DQMFileSaverPB.cc:342
edm::Service
Definition: Service.h:30
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
evf::EvFDaqDirector
Definition: EvFDaqDirector.h:62
dqm::DQMFileSaverPB
Definition: DQMFileSaverPB.h:15
dqmstorepb::ROOTFilePB
Definition: ROOTFilePB.pb.h:271
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
dqm::DQMFileSaverPB::~DQMFileSaverPB
~DQMFileSaverPB() override
dqm::DQMFileSaverPB::saveRun
void saveRun(const FileParameters &fp) const override
Definition: DQMFileSaverPB.cc:120
dqm::DQMFileSaverPB::saveLumi
void saveLumi(const FileParameters &fp) const override
Definition: DQMFileSaverPB.cc:64
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
writedatasetfile.run
run
Definition: writedatasetfile.py:27
dqm::DQMFileSaverPB::fillJson
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)
Definition: DQMFileSaverPB.cc:124
Exception
Definition: hltDiff.cc:246
AlcaSiPixelAliHarvester0T_cff.options
options
Definition: AlcaSiPixelAliHarvester0T_cff.py:42
dqm::DQMFileSaverPB::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: DQMFileSaverPB.cc:206
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
submitPVValidationJobs.inputFiles
list inputFiles
Definition: submitPVValidationJobs.py:929
DQMNet::DQM_PROP_LUMI
static const uint32_t DQM_PROP_LUMI
Definition: DQMNet.h:61
mps_fire.result
result
Definition: mps_fire.py:311
dqm::DQMFileSaverPB::streamLabel_
std::string streamLabel_
Definition: DQMFileSaverPB.h:36
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
dqm
Definition: DQMStore.h:18
ParameterSet.h
hlt_dqm_clientPB-live_cfg.me
me
Definition: hlt_dqm_clientPB-live_cfg.py:61
dqm::DQMFileSaverBase::fillDescription
static void fillDescription(edm::ParameterSetDescription &d)
Definition: DQMFileSaverBase.cc:139
HLT_FULL_cff.flags
flags
Definition: HLT_FULL_cff.py:13150
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
lumi
Definition: LumiSectionData.h:20
evf::MergeTypePB
Definition: EvFDaqDirector.h:58
dqm::DQMFileSaverPB::initRun
void initRun() const override
Definition: DQMFileSaverPB.cc:50