CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EvFOutputModule.cc
Go to the documentation of this file.
2 
4 
8 
12 
16 
19 
20 #include <sys/stat.h>
21 #include <filesystem>
22 #include <boost/algorithm/string.hpp>
23 
24 namespace evf {
25 
28  std::string const& streamLabel,
29  std::string const& moduleLabel)
30  : streamerCommon_(ps, selections, moduleLabel),
31  processed_(0),
32  accepted_(0),
33  errorEvents_(0),
34  retCodeMask_(0),
35  filelist_(),
36  filesize_(0),
37  inputFiles_(),
38  fileAdler32_(1),
39  hltErrorEvents_(0) {
40  transferDestination_ = edm::Service<evf::EvFDaqDirector>()->getStreamDestinations(streamLabel);
41  mergeType_ = edm::Service<evf::EvFDaqDirector>()->getStreamMergeType(streamLabel, evf::MergeTypeDAT);
42 
43  std::string baseRunDir = edm::Service<evf::EvFDaqDirector>()->baseRunDir();
44  LogDebug("EvFOutputModule") << "writing .dat files to -: " << baseRunDir;
45 
46  edm::Service<evf::EvFDaqDirector>()->createRunOpendirMaybe();
47 
48  processed_.setName("Processed");
49  accepted_.setName("Accepted");
50  errorEvents_.setName("ErrorEvents");
51  retCodeMask_.setName("ReturnCodeMask");
52  filelist_.setName("Filelist");
53  filesize_.setName("Filesize");
54  inputFiles_.setName("InputFiles");
55  fileAdler32_.setName("FileAdler32");
56  transferDestination_.setName("TransferDestination");
57  mergeType_.setName("MergeType");
58  hltErrorEvents_.setName("HLTErrorEvents");
59 
69  outJsonDef_.addLegendItem("TransferDestination", "string", jsoncollector::DataPointDefinition::SAME);
72 
73  std::stringstream tmpss, ss;
74  tmpss << baseRunDir << "/open/"
75  << "output_" << getpid() << ".jsd";
76  ss << baseRunDir << "/"
77  << "output_" << getpid() << ".jsd";
78  std::string outTmpJsonDefName = tmpss.str();
79  std::string outJsonDefName = ss.str();
80 
81  edm::Service<evf::EvFDaqDirector>()->lockInitLock();
82  struct stat fstat;
83  if (stat(outJsonDefName.c_str(), &fstat) != 0) { //file does not exist
84  LogDebug("EvFOutputModule") << "writing output definition file -: " << outJsonDefName;
87  jsoncollector::FileIO::writeStringToFile(outTmpJsonDefName, content);
88  std::filesystem::rename(outTmpJsonDefName, outJsonDefName);
89  }
90  edm::Service<evf::EvFDaqDirector>()->unlockInitLock();
91 
93  jsonMonitor_->setDefPath(outJsonDefName);
94  jsonMonitor_->registerGlobalMonitorable(&processed_, false);
95  jsonMonitor_->registerGlobalMonitorable(&accepted_, false);
96  jsonMonitor_->registerGlobalMonitorable(&errorEvents_, false);
97  jsonMonitor_->registerGlobalMonitorable(&retCodeMask_, false);
98  jsonMonitor_->registerGlobalMonitorable(&filelist_, false);
99  jsonMonitor_->registerGlobalMonitorable(&filesize_, false);
100  jsonMonitor_->registerGlobalMonitorable(&inputFiles_, false);
101  jsonMonitor_->registerGlobalMonitorable(&fileAdler32_, false);
102  jsonMonitor_->registerGlobalMonitorable(&transferDestination_, false);
103  jsonMonitor_->registerGlobalMonitorable(&mergeType_, false);
104  jsonMonitor_->registerGlobalMonitorable(&hltErrorEvents_, false);
105  jsonMonitor_->commit(nullptr);
106  }
107 
109  : edm::one::OutputModuleBase(ps),
111  ps_(ps),
112  streamLabel_(ps.getParameter<std::string>("@module_label")),
113  trToken_(consumes<edm::TriggerResults>(edm::InputTag("TriggerResults"))),
114  psetToken_(consumes<edm::SendJobHeader::ParameterSetMap, edm::InRun>(
115  ps.getUntrackedParameter<edm::InputTag>("psetMap"))) {
116  //replace hltOutoputA with stream if the HLT menu uses this convention
117  std::string testPrefix = "hltOutput";
118  if (streamLabel_.find(testPrefix) == 0)
119  streamLabel_ = std::string("stream") + streamLabel_.substr(testPrefix.size());
120 
121  if (streamLabel_.find('_') != std::string::npos) {
122  throw cms::Exception("EvFOutputModule") << "Underscore character is reserved can not be used for stream names in "
123  "FFF, but was detected in stream name -: "
124  << streamLabel_;
125  }
126 
127  std::string streamLabelLow = streamLabel_;
128  boost::algorithm::to_lower(streamLabelLow);
129  auto streampos = streamLabelLow.rfind("stream");
130  if (streampos != 0 && streampos != std::string::npos)
131  throw cms::Exception("EvFOutputModule")
132  << "stream (case-insensitive) sequence was found in stream suffix. This is reserved and can not be used for "
133  "names in FFF based HLT, but was detected in stream name";
134 
136  }
137 
139 
144  desc.addUntracked<edm::InputTag>("psetMap", {"hltPSetMap"})
145  ->setComment("Optionally allow the map of ParameterSets to be calculated externally.");
146  descriptions.add("evfOutputModule", desc);
147  }
148 
150  //create run Cache holding JSON file writer and variables
151  jsonWriter_ = std::make_unique<EvFOutputJSONWriter>(
153 
154  //output INI file (non-const). This doesn't require globalBeginRun to be finished
155  const std::string openIniFileName = edm::Service<evf::EvFDaqDirector>()->getOpenInitFilePath(streamLabel_);
156  edm::LogInfo("EvFOutputModule") << "beginRun init stream -: " << openIniFileName;
157 
158  StreamerOutputFile stream_writer_preamble(openIniFileName);
159  uint32 preamble_adler32 = 1;
160  edm::BranchIDLists const* bidlPtr = branchIDLists();
161 
162  auto psetMapHandle = run.getHandle(psetToken_);
163 
164  std::unique_ptr<InitMsgBuilder> init_message =
165  jsonWriter_->streamerCommon_.serializeRegistry(*jsonWriter_->streamerCommon_.getSerializerBuffer(),
166  *bidlPtr,
171  psetMapHandle.isValid() ? psetMapHandle.product() : nullptr);
172 
173  //Let us turn it into a View
174  InitMsgView view(init_message->startAddress());
175 
176  //output header
177  stream_writer_preamble.write(view);
178  preamble_adler32 = stream_writer_preamble.adler32();
179  stream_writer_preamble.close();
180 
181  struct stat istat;
182  stat(openIniFileName.c_str(), &istat);
183  //read back file to check integrity of what was written
184  off_t readInput = 0;
185  uint32_t adlera = 1, adlerb = 0;
186  FILE* src = fopen(openIniFileName.c_str(), "r");
187 
188  //allocate buffer to write INI file
189  unsigned char* outBuf = new unsigned char[1024 * 1024];
190  while (readInput < istat.st_size) {
191  size_t toRead = readInput + 1024 * 1024 < istat.st_size ? 1024 * 1024 : istat.st_size - readInput;
192  fread(outBuf, toRead, 1, src);
193  cms::Adler32((const char*)outBuf, toRead, adlera, adlerb);
194  readInput += toRead;
195  }
196  fclose(src);
197 
198  //clear serialization buffers
199  jsonWriter_->streamerCommon_.getSerializerBuffer()->clearHeaderBuffer();
200 
201  //free output buffer needed only for the file write
202  delete[] outBuf;
203  outBuf = nullptr;
204 
205  uint32_t adler32c = (adlerb << 16) | adlera;
206  if (adler32c != preamble_adler32) {
207  throw cms::Exception("EvFOutputModule") << "Checksum mismatch of ini file -: " << openIniFileName
208  << " expected:" << preamble_adler32 << " obtained:" << adler32c;
209  } else {
210  LogDebug("EvFOutputModule") << "Ini file checksum -: " << streamLabel_ << " " << adler32c;
211  std::filesystem::rename(openIniFileName, edm::Service<evf::EvFDaqDirector>()->getInitFilePath(streamLabel_));
212  }
213  }
214 
216  edm::EventForOutput const& e) const {
217  Trig result;
219  return result;
220  }
221 
222  std::shared_ptr<EvFOutputEventWriter> EvFOutputModule::globalBeginLuminosityBlock(
223  edm::LuminosityBlockForOutput const& iLB) const {
224  auto openDatFilePath = edm::Service<evf::EvFDaqDirector>()->getOpenDatFilePath(iLB.luminosityBlock(), streamLabel_);
225 
226  return std::make_shared<EvFOutputEventWriter>(openDatFilePath);
227  }
228 
231 
232  //auto lumiWriter = const_cast<EvFOutputEventWriter*>(luminosityBlockCache(e.getLuminosityBlock().index() ));
233  auto lumiWriter = luminosityBlockCache(e.getLuminosityBlock().index());
234  std::unique_ptr<EventMsgBuilder> msg = jsonWriter_->streamerCommon_.serializeEvent(
235  *jsonWriter_->streamerCommon_.getSerializerBuffer(), e, triggerResults, selectorConfig());
236  lumiWriter->incAccepted();
237  lumiWriter->doOutputEvent(*msg); //msg is written and discarded at this point
238  }
239 
241  auto lumiWriter = luminosityBlockCache(iLB.index());
242  //close dat file
243  lumiWriter->close();
244 
245  jsonWriter_->fileAdler32_.value() = lumiWriter->get_adler32();
246  jsonWriter_->accepted_.value() = lumiWriter->getAccepted();
247 
248  bool abortFlag = false;
249  jsonWriter_->processed_.value() = fms_->getEventsProcessedForLumi(iLB.luminosityBlock(), &abortFlag);
250  if (abortFlag) {
251  edm::LogInfo("EvFOutputModule") << "Abort flag has been set. Output is suppressed";
252  return;
253  }
254 
255  if (jsonWriter_->processed_.value() != 0) {
256  struct stat istat;
257  std::filesystem::path openDatFilePath = lumiWriter->getFilePath();
258  stat(openDatFilePath.string().c_str(), &istat);
259  jsonWriter_->filesize_ = istat.st_size;
260  std::filesystem::rename(openDatFilePath.string().c_str(),
262  jsonWriter_->filelist_ = openDatFilePath.filename().string();
263  } else {
264  //remove empty file when no event processing has occurred
265  remove(lumiWriter->getFilePath().c_str());
266  jsonWriter_->filesize_ = 0;
267  jsonWriter_->filelist_ = "";
268  jsonWriter_->fileAdler32_.value() = -1; //no files in signed long
269  }
270 
271  //produce JSON file
272  jsonWriter_->jsonMonitor_->snap(iLB.luminosityBlock());
273  const std::string outputJsonNameStream =
274  edm::Service<evf::EvFDaqDirector>()->getOutputJsonFilePath(iLB.luminosityBlock(), streamLabel_);
275  jsonWriter_->jsonMonitor_->outputFullJSON(outputJsonNameStream, iLB.luminosityBlock());
276  }
277 
278 } // namespace evf
void addLegendItem(std::string const &name, std::string const &type, std::string const &operation)
ParameterSetID const & mainParameterSetID() const
EvFOutputModule(edm::ParameterSet const &ps)
std::unique_ptr< evf::EvFOutputJSONWriter > jsonWriter_
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
edm::ParameterSet const & ps_
jsoncollector::IntJ fileAdler32_
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::shared_ptr< EvFOutputEventWriter > globalBeginLuminosityBlock(edm::LuminosityBlockForOutput const &iLB) const override
static bool serialize(JsonSerializable *pObj, std::string &output)
void write(const InitMsgBuilder &)
std::string to_lower(const std::string &s)
jsoncollector::DataPointDefinition outJsonDef_
jsoncollector::IntJ hltErrorEvents_
std::string const & moduleLabel() const
jsoncollector::StringJ inputFiles_
void beginRun(edm::RunForOutput const &run) override
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
tuple result
Definition: mps_fire.py:311
LuminosityBlockIndex index() const
std::vector< std::pair< BranchDescription const *, EDGetToken > > SelectedProducts
LuminosityBlockForOutput const & getLuminosityBlock() const
LuminosityBlockNumber_t luminosityBlock() const
jsoncollector::IntJ retCodeMask_
std::string streamLabel_
static void fillDescription(ParameterSetDescription &desc)
virtual void setName(std::string name)
ModuleDescription const & description() const
void globalEndLuminosityBlock(edm::LuminosityBlockForOutput const &iLB) override
static std::string const triggerResults
Definition: EdmProvDump.cc:44
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
static void writeStringToFile(std::string const &filename, std::string &content)
Definition: FileIO.cc:21
unsigned int uint32
Definition: MsgTools.h:13
jsoncollector::IntJ processed_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
BranchIDLists const * branchIDLists() const
Log< level::Info, false > LogInfo
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
void write(edm::EventForOutput const &e) override
uint32 adler32() const
ParameterSetID selectorConfig() const
edm::EDGetTokenT< edm::TriggerResults > trToken_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
tuple msg
Definition: mps_check.py:285
BasicHandle getByToken(EDGetToken token, TypeID const &typeID) const
std::map< ParameterSetID, ParameterSetBlob > ParameterSetMap
jsoncollector::IntJ filesize_
jsoncollector::StringJ filelist_
jsoncollector::StringJ transferDestination_
bool isValid() const
Definition: Hash.h:141
edm::EDGetTokenT< edm::SendJobHeader::ParameterSetMap > psetToken_
std::shared_ptr< jsoncollector::FastMonitor > jsonMonitor_
EvFOutputJSONWriter(edm::ParameterSet const &ps, edm::SelectedProducts const *selections, std::string const &streamLabel, std::string const &moduleLabel)
SelectedProductsForBranchType const & keptProducts() const
const ModuleDescription & moduleDescription() const
unsigned int getEventsProcessedForLumi(unsigned int lumi, bool *abortFlag=nullptr)
void setDefaultGroup(std::string const &group)
jsoncollector::IntJ accepted_
jsoncollector::StringJ mergeType_
static void fillDescription(ParameterSetDescription &desc, std::vector< std::string > const &iDefaultOutputCommands=ProductSelectorRules::defaultSelectionStrings())
jsoncollector::IntJ errorEvents_
Trig getTriggerResults(edm::EDGetTokenT< edm::TriggerResults > const &token, edm::EventForOutput const &e) const
evf::FastMonitoringService * fms_
#define LogDebug(id)