CMS 3D CMS Logo

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