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 #include "TBufferFile.h"
19 
20 #include <openssl/md5.h>
21 #include <boost/property_tree/json_parser.hpp>
22 #include <boost/filesystem.hpp>
23 #include <boost/format.hpp>
24 
25 #include <google/protobuf/io/coded_stream.h>
26 #include <google/protobuf/io/gzip_stream.h>
27 #include <google/protobuf/io/zero_copy_stream_impl.h>
29 
30 using namespace dqm;
31 
33  fakeFilterUnitMode_ = ps.getUntrackedParameter<bool>("fakeFilterUnitMode", false);
34  streamLabel_ = ps.getUntrackedParameter<std::string>("streamLabel", "streamDQMHistograms");
35 
37  mergeType_ = "";
38 }
39 
41 
43  if (!fakeFilterUnitMode_) {
46  }
47 
48  if (!fakeFilterUnitMode_) {
50  const std::string initFileName = daqDirector->getInitFilePath(streamLabel_);
51  std::ofstream file(initFileName);
52  file.close();
53  }
54 }
55 
57  // get from DAQ2 services where to store the files according to their format
58  namespace bpt = boost::property_tree;
59 
60  std::string openJsonFilePathName;
61  std::string jsonFilePathName;
62  std::string openHistoFilePathName;
63  std::string histoFilePathName;
64 
65  evf::FastMonitoringService* fms = nullptr;
67 
68  // create the files names
69  if (fakeFilterUnitMode_) {
70  std::string runDir = str(boost::format("%s/run%06d") % fp.path_ % fp.run_);
71  std::string baseName = str(boost::format("%s/run%06d_ls%04d_%s") % runDir % fp.run_ % fp.lumi_ % streamLabel_);
72 
73  boost::filesystem::create_directories(runDir);
74 
75  jsonFilePathName = baseName + ".jsn";
76  openJsonFilePathName = jsonFilePathName + ".open";
77 
78  histoFilePathName = baseName + ".pb";
79  openHistoFilePathName = histoFilePathName + ".open";
80  } else {
81  openJsonFilePathName = edm::Service<evf::EvFDaqDirector>()->getOpenOutputJsonFilePath(fp.lumi_, streamLabel_);
82  jsonFilePathName = edm::Service<evf::EvFDaqDirector>()->getOutputJsonFilePath(fp.lumi_, streamLabel_);
83 
84  openHistoFilePathName =
85  edm::Service<evf::EvFDaqDirector>()->getOpenProtocolBufferHistogramFilePath(fp.lumi_, streamLabel_);
86  histoFilePathName = edm::Service<evf::EvFDaqDirector>()->getProtocolBufferHistogramFilePath(fp.lumi_, streamLabel_);
87 
89  }
90 
91  if (fms ? fms->getEventsProcessedForLumi(fp.lumi_) : true) {
92  // Save the file in the open directory.
93  this->savePB(&*store, openHistoFilePathName, fp.run_, fp.lumi_);
94 
95  // Now move the the data and json files into the output directory.
96  ::rename(openHistoFilePathName.c_str(), histoFilePathName.c_str());
97  }
98 
99  // Write the json file in the open directory.
100  bpt::ptree pt = fillJson(fp.run_, fp.lumi_, histoFilePathName, transferDestination_, mergeType_, fms);
101  write_json(openJsonFilePathName, pt);
102  ::rename(openJsonFilePathName.c_str(), jsonFilePathName.c_str());
103 }
104 
106  // no saving for the run
107 }
108 
109 boost::property_tree::ptree DQMFileSaverPB::fillJson(int run,
110  int lumi,
111  const std::string& dataFilePathName,
112  const std::string& transferDestinationStr,
113  const std::string& mergeTypeStr,
115  namespace bpt = boost::property_tree;
116  namespace bfs = boost::filesystem;
117 
118  bpt::ptree pt;
119 
120  int hostnameReturn;
121  char host[32];
122  hostnameReturn = gethostname(host, sizeof(host));
123  if (hostnameReturn == -1)
124  throw cms::Exception("fillJson") << "Internal error, cannot get host name";
125 
126  int pid = getpid();
127  std::ostringstream oss_pid;
128  oss_pid << pid;
129 
130  int nProcessed = fms ? (fms->getEventsProcessedForLumi(lumi)) : -1;
131 
132  // Stat the data file: if not there, throw
133  std::string dataFileName;
134  struct stat dataFileStat;
135  dataFileStat.st_size = 0;
136  if (nProcessed) {
137  if (stat(dataFilePathName.c_str(), &dataFileStat) != 0)
138  throw cms::Exception("fillJson") << "Internal error, cannot get data file: " << dataFilePathName;
139  // Extract only the data file name from the full path
140  dataFileName = bfs::path(dataFilePathName).filename().string();
141  }
142  // The availability test of the FastMonitoringService was done in the ctor.
143  bpt::ptree data;
144  bpt::ptree processedEvents, acceptedEvents, errorEvents, bitmask, fileList, fileSize, inputFiles, fileAdler32,
145  transferDestination, mergeType, hltErrorEvents;
146 
147  processedEvents.put("", nProcessed); // Processed events
148  acceptedEvents.put("", nProcessed); // Accepted events, same as processed for our purposes
149 
150  errorEvents.put("", 0); // Error events
151  bitmask.put("", 0); // Bitmask of abs of CMSSW return code
152  fileList.put("", dataFileName); // Data file the information refers to
153  fileSize.put("", dataFileStat.st_size); // Size in bytes of the data file
154  inputFiles.put("", ""); // We do not care about input files!
155  fileAdler32.put("", -1); // placeholder to match output json definition
156  transferDestination.put("", transferDestinationStr); // SM Transfer destination field
157  mergeType.put("", mergeTypeStr); // SM Transfer destination field
158  hltErrorEvents.put("", 0); // Error events
159 
160  data.push_back(std::make_pair("", processedEvents));
161  data.push_back(std::make_pair("", acceptedEvents));
162  data.push_back(std::make_pair("", errorEvents));
163  data.push_back(std::make_pair("", bitmask));
164  data.push_back(std::make_pair("", fileList));
165  data.push_back(std::make_pair("", fileSize));
166  data.push_back(std::make_pair("", inputFiles));
167  data.push_back(std::make_pair("", fileAdler32));
168  data.push_back(std::make_pair("", transferDestination));
169  data.push_back(std::make_pair("", mergeType));
170  data.push_back(std::make_pair("", hltErrorEvents));
171 
172  pt.add_child("data", data);
173 
174  if (fms == nullptr) {
175  pt.put("definition", "/fakeDefinition.jsn");
176  } else {
177  // The availability test of the EvFDaqDirector Service was done in the ctor.
178  bfs::path outJsonDefName{
179  edm::Service<evf::EvFDaqDirector>()->baseRunDir()}; //we assume this file is written bu the EvF Output module
180  outJsonDefName /= (std::string("output_") + oss_pid.str() + std::string(".jsd"));
181  pt.put("definition", outJsonDefName.string());
182  }
183 
184  char sourceInfo[64]; //host and pid information
185  sprintf(sourceInfo, "%s_%d", host, pid);
186  pt.put("source", sourceInfo);
187 
188  return pt;
189 }
190 
193  desc.setComment("Saves histograms from DQM store, HLT->pb workflow.");
194 
195  desc.addUntracked<bool>("fakeFilterUnitMode", false)->setComment("If set, EvFDaqDirector is emulated and not used.");
196 
197  desc.addUntracked<std::string>("streamLabel", "streamDQMHistograms")->setComment("Label of the stream.");
198 
200 
201  // Changed to use addDefault instead of add here because previously
202  // DQMFileSaverOnline and DQMFileSaverPB both used the module label
203  // "saver" which caused conflicting cfi filenames to be generated.
204  // add could be used if unique module labels were given.
205  descriptions.addDefault(desc);
206 }
207 
208 void DQMFileSaverPB::savePB(DQMStore* store, std::string const& filename, int run, int lumi) const {
209  using google::protobuf::io::FileOutputStream;
210  using google::protobuf::io::GzipOutputStream;
211  using google::protobuf::io::StringOutputStream;
212 
213  unsigned int nme = 0;
214 
215  dqmstorepb::ROOTFilePB dqmstore_message;
216 
217  // We save all histograms, indifferent of the lumi flag: even tough we save per lumi, this is a *snapshot*.
218  auto mes = store->getAllContents("");
219  for (auto const me : mes) {
220  TBufferFile buffer(TBufferFile::kWrite);
221  if (me->kind() < MonitorElement::Kind::TH1F) {
222  TObjString object(me->tagString().c_str());
223  buffer.WriteObject(&object);
224  } else {
225  buffer.WriteObject(me->getRootObject());
226  }
227  dqmstorepb::ROOTFilePB::Histo& histo = *dqmstore_message.add_histo();
228  histo.set_full_pathname(me->getFullname());
229  uint32_t flags = 0;
230  flags |= (uint32_t)me->kind();
231  if (me->getLumiFlag())
233  if (me->getEfficiencyFlag())
235  histo.set_flags(flags);
236  histo.set_size(buffer.Length());
237  histo.set_streamed_histo((void const*)buffer.Buffer(), buffer.Length());
238 
239  // Save quality reports if this is not in reference section.
240  // XXX not supported by protobuf files.
241 
242  // Save efficiency tag, if any.
243  // XXX not supported by protobuf files.
244 
245  // Save tag if any.
246  // XXX not supported by protobuf files.
247 
248  // Count saved histograms
249  ++nme;
250  }
251 
252  int filedescriptor =
253  ::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
254  FileOutputStream file_stream(filedescriptor);
256  options.format = GzipOutputStream::GZIP;
257  options.compression_level = 1;
258  GzipOutputStream gzip_stream(&file_stream, options);
259  dqmstore_message.SerializeToZeroCopyStream(&gzip_stream);
260 
261  // Flush the internal streams before closing the fd.
262  gzip_stream.Close();
263  file_stream.Close();
264  ::close(filedescriptor);
265 
266  // Maybe make some noise.
267  edm::LogInfo("DQMFileSaverPB") << "savePB: successfully wrote " << nme << " objects "
268  << "into DQM file '" << filename << "'\n";
269 }
270 
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:84
MonitorElementData::Kind::TH1F
dqm::DQMFileSaverPB::DQMFileSaverPB
DQMFileSaverPB(const edm::ParameterSet &ps)
Definition: DQMFileSaverPB.cc:32
edm::LogInfo
Definition: MessageLogger.h:254
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
dqm::implementation::IGetter::getAllContents
virtual std::vector< dqm::harvesting::MonitorElement * > getAllContents(std::string const &path) const
Definition: DQMStore.cc:609
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
query.host
host
Definition: query.py:115
options
Definition: options.py:1
evf::FastMonitoringService
Definition: FastMonitoringService.h:69
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:1111
Service.h
dqm-mbProfile.format
format
Definition: dqm-mbProfile.py:16
DQMNet::DQM_PROP_EFFICIENCY_PLOT
static const uint32_t DQM_PROP_EFFICIENCY_PLOT
Definition: DQMNet.h:64
str
#define str(s)
Definition: TestProcessor.cc:48
dqm::DQMFileSaverPB::savePB
void savePB(DQMStore *store, std::string const &filename, int run, int lumi) const
Definition: DQMFileSaverPB.cc:208
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
dqm::DQMFileSaverBase
Definition: DQMFileSaverBase.h:23
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::ParameterSetDescription::addUntracked
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:100
edm::ParameterSet
Definition: ParameterSet.h:36
edm::ParameterSetDescription::setComment
void setComment(std::string const &value)
Definition: ParameterSetDescription.cc:33
dqm::DQMFileSaverPB::transferDestination_
std::string transferDestination_
Definition: DQMFileSaverPB.h:37
dqm::DQMFileSaverPB::mergeType_
std::string mergeType_
Definition: DQMFileSaverPB.h:38
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
dqm::DQMFileSaverPB::~DQMFileSaverPB
~DQMFileSaverPB() override
dqm::DQMFileSaverPB::saveRun
void saveRun(const FileParameters &fp) const override
Definition: DQMFileSaverPB.cc:105
dqm::DQMFileSaverPB::saveLumi
void saveLumi(const FileParameters &fp) const override
Definition: DQMFileSaverPB.cc:56
writedatasetfile.run
run
Definition: writedatasetfile.py:27
varParsingExample.inputFiles
inputFiles
Definition: varParsingExample.py:6
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:109
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:191
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
DQMNet::DQM_PROP_LUMI
static const uint32_t DQM_PROP_LUMI
Definition: DQMNet.h:61
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:56
dqm::DQMFileSaverBase::fillDescription
static void fillDescription(edm::ParameterSetDescription &d)
Definition: DQMFileSaverBase.cc:131
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
lumi
Definition: LumiSectionData.h:20
HLT_2018_cff.flags
flags
Definition: HLT_2018_cff.py:11758
evf::MergeTypePB
Definition: EvFDaqDirector.h:58
dqm::DQMFileSaverPB::initRun
void initRun() const override
Definition: DQMFileSaverPB.cc:42