CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMStreamerReader.cc
Go to the documentation of this file.
9 
13 #include "DQMStreamerReader.h"
14 
15 #include <fstream>
16 #include <queue>
17 #include <cstdlib>
18 #include <boost/regex.hpp>
19 #include <boost/format.hpp>
20 #include <boost/range.hpp>
21 #include <boost/filesystem.hpp>
22 #include <boost/algorithm/string.hpp>
23 
25 
26 namespace dqmservices {
27 
29  edm::InputSourceDescription const& desc)
30  : StreamerInputSource(pset, desc),
31  fiterator_(pset),
32  streamReader_() {
33 
34  runNumber_ = pset.getUntrackedParameter<unsigned int>("runNumber");
35  runInputDir_ = pset.getUntrackedParameter<std::string>("runInputDir");
36  hltSel_ =
37  pset.getUntrackedParameter<std::vector<std::string> >("SelectEvents");
38 
39  minEventsPerLs_ = pset.getUntrackedParameter<int>("minEventsPerLumi");
40  flagSkipFirstLumis_ = pset.getUntrackedParameter<bool>("skipFirstLumis");
41  flagEndOfRunKills_ = pset.getUntrackedParameter<bool>("endOfRunKills");
42  flagDeleteDatFiles_ = pset.getUntrackedParameter<bool>("deleteDatFiles");
43 
44  triggerSel();
45 
46  reset_();
47 }
48 
50 
52  // We have to load at least a single header,
53  // so the ProductRegistry gets initialized.
54  //
55  // This must happen here (inside the constructor),
56  // as ProductRegistry gets frozen after we initialize:
57  // https://cmssdt.cern.ch/SDT/lxr/source/FWCore/Framework/src/Schedule.cc#441
58 
60  "Waiting for the first lumi in order to initialize.");
61 
63 
64  // Fast-forward to the last open file.
65  if (flagSkipFirstLumis_) {
66  unsigned int l = fiterator_.lastLumiFound();
67  if (l > 1) {
69  }
70  }
71 
72  for (;;) {
73  bool next = prepareNextFile();
74 
75  // check for end of run
76  if (!next) {
78  "End of run reached before DQMStreamerReader was initialised.");
79  return;
80  }
81 
82  // check if we have a file openned
83  if (streamReader_.get() != nullptr) {
84  // we are now initialised
85  break;
86  }
87 
88  // wait
89  fiterator_.delay();
90  }
91 
92  fiterator_.logFileAction("DQMStreamerReader initialised.");
93 }
94 
95 void DQMStreamerReader::openFile_(std::string newStreamerFile_) {
97  edm::ParameterSet pset;
98 
99  streamReader_ = std::unique_ptr<edm::StreamerInputFile>(
100  new edm::StreamerInputFile(newStreamerFile_));
101 
102  InitMsgView const* header = getHeaderMsg();
103  deserializeAndMergeWithRegistry(*header, false);
104 
105  // dump the list of HLT trigger name from the header
106  // dumpInitHeader(header);
107 
108  // if specific trigger selection is requested, check if the requested triggers
109  // match with trigger paths in the header file
110  if (!acceptAllEvt_){
111  Strings tnames;
112  header->hltTriggerNames(tnames);
113 
114  pset.addParameter<Strings>("SelectEvents", hltSel_);
115  eventSelector_.reset(new TriggerSelector(pset, tnames));
116 
117  // check if any trigger path name requested matches with trigger name in the header file
118  matchTriggerSel(tnames);
119  }
120 
121  // our initialization
123 
124  if (flagDeleteDatFiles_) {
125  // unlink the file
126  unlink(newStreamerFile_.c_str());
127  }
128 }
129 
131  if (streamReader_.get() != nullptr) {
132  streamReader_->closeStreamerFile();
133  streamReader_ = nullptr;
134  }
135 }
136 
138  closeFile_();
139 
142 
143  if (boost::filesystem::exists(p)) {
144  openFile_(p);
145  return true;
146  } else {
147  /* dat file missing */
148  fiterator_.logFileAction("Data file (specified in json) is missing:", p);
149  fiterator_.logLumiState(currentLumi_, "error: data file missing");
150 
151  return false;
152  }
153 }
154 
156  InitMsgView const* header = streamReader_->startMessage();
157 
158  if (header->code() != Header::INIT) { // INIT Msg
159  throw edm::Exception(edm::errors::FileReadError, "DQMStreamerReader::readHeader")
160  << "received wrong message type: expected INIT, got " << header->code()
161  << "\n";
162  }
163 
164  return header;
165 }
166 
168  if (!streamReader_->next()) {
169  return nullptr;
170  }
171 
172  EventMsgView const* msg = streamReader_->currentRecord();
173 
174  // if (msg != nullptr) dumpEventView(msg);
175  return msg;
176 }
177 
189  typedef DQMFileIterator::State State;
190 
191  for (;;) {
193 
194  // check for end of run file and force quit
195  if (flagEndOfRunKills_ && (fiterator_.state() != State::OPEN)) {
196  closeFile_();
197  return false;
198  }
199 
200  // check for end of run and quit if everything has been processed.
201  // this clean exit
202  if ((streamReader_.get() == nullptr) && (!fiterator_.lumiReady()) &&
203  (fiterator_.state() == State::EOR)) {
204 
205  closeFile_();
206  return false;
207  }
208 
209  // if this is end of run and no more files to process
210  // close it
212  (!fiterator_.lumiReady()) && (fiterator_.state() == State::EOR)) {
213 
214  closeFile_();
215  return false;
216  }
217 
218  // skip to the next file if we have no files openned yet
219  if (streamReader_.get() == nullptr) {
220  if (fiterator_.lumiReady()) {
221  openNextFile_();
222  // we might need to open once more (if .dat is missing)
223  continue;
224  }
225  }
226 
227  // or if there is a next file and enough eventshas been processed.
229  openNextFile_();
230  // we might need to open once more (if .dat is missing)
231  continue;
232  }
233 
234  return true;
235  }
236 }
237 
243  EventMsgView const* eview = nullptr;
244  typedef DQMFileIterator::State State;
245 
246  // wait for the next event
247  for (;;) {
248  // edm::LogAbsolute("DQMStreamerReader")
249  // << "State loop.";
250  bool next = prepareNextFile();
251  if (!next) return nullptr;
252 
253  // sleep
254  if (streamReader_.get() == nullptr) {
255  // the reader does not exist
256  fiterator_.delay();
257  } else {
258  // our reader exists, try to read out an event
259  eview = getEventMsg();
260 
261  if (eview == nullptr) {
262  // read unsuccessful
263  // this means end of file, so close the file
264  closeFile_();
265  } else {
266  if (!acceptEvent(eview)) {
267  continue;
268  } else {
269  return eview;
270  }
271  }
272  }
273  }
274  return eview;
275 }
276 
281  EventMsgView const* eview = prepareNextEvent();
282  if (eview == nullptr) {
283  return false;
284  }
285 
286  // this is reachable only if eview is set
287  // and the file is openned
288  if (streamReader_->newHeader()) {
289  // A new file has been opened and we must compare Headers here !!
290  // Get header/init from reader
291  InitMsgView const* header = getHeaderMsg();
292  deserializeAndMergeWithRegistry(*header, true);
293  }
294 
296  deserializeEvent(*eview);
297 
298  return true;
299 }
300 
305  acceptAllEvt_ = false;
306  for (Strings::const_iterator i(hltSel_.begin()), end(hltSel_.end());
307  i!=end; ++i){
308  std::string hltPath(*i);
309  boost::erase_all(hltPath, " \t");
310  if (hltPath == "*") acceptAllEvt_ = true;
311  }
312  return acceptAllEvt_;
313 }
314 
319  matchTriggerSel_ = false;
320  for (Strings::const_iterator i(hltSel_.begin()), end(hltSel_.end());
321  i!=end; ++i){
322  std::string hltPath(*i);
323  boost::erase_all(hltPath, " \t");
324  std::vector<Strings::const_iterator> matches = edm::regexMatch(tnames, hltPath);
325  if (matches.empty()){
326  edm::LogWarning("Trigger selection does not match any trigger path!!!") << std::endl;
327  matchTriggerSel_ = false;
328  }else{
329  matchTriggerSel_ = true;
330  }
331  }
332  return matchTriggerSel_;
333 }
334 
339 
340  if (acceptAllEvt_) return true;
341  if (!matchTriggerSel_) return false;
342 
343  std::vector<unsigned char> hltTriggerBits_;
344  int hltTriggerCount_ = evtmsg->hltCount();
345  if (hltTriggerCount_ > 0) {
346  hltTriggerBits_.resize(1 + (hltTriggerCount_ - 1) / 4);
347  }
348  evtmsg->hltTriggerBits(&hltTriggerBits_[0]);
349 
350  if (eventSelector_->wantAll() ||
351  eventSelector_->acceptEvent(&hltTriggerBits_[0], evtmsg->hltCount())) {
352  return true;
353  }else{
354  return false;
355  }
356 }
357 
358 void DQMStreamerReader::skip(int toSkip) {
359  for (int i = 0; i != toSkip; ++i) {
360  EventMsgView const* evMsg = prepareNextEvent();
361 
362  if (evMsg == nullptr) {
363  return;
364  }
365  }
366 }
367 
369  edm::ConfigurationDescriptions& descriptions) {
370 
372  desc.setComment("Reads events from streamer files.");
373 
374  desc.addUntracked<std::vector<std::string> >("SelectEvents")
375  ->setComment("HLT path to select events ");
376 
377  desc.addUntracked<int>("minEventsPerLumi", 1)->setComment(
378  "Minimum number of events to process per lumisection, "
379  "before switching to a new input file. If the next file "
380  "does not yet exist, "
381  "the number of processed events will be bigger.");
382 
383  desc.addUntracked<bool>("skipFirstLumis", false)->setComment(
384  "Skip (and ignore the minEventsPerLumi parameter) for the files "
385  "which have been available at the begining of the processing. "
386  "If set to true, the reader will open last available file for "
387  "processing.");
388 
389  desc.addUntracked<bool>("deleteDatFiles", false)->setComment(
390  "Delete data files after they have been closed, in order to "
391  "save disk space.");
392 
393  desc.addUntracked<bool>("endOfRunKills", false)->setComment(
394  "Kill the processing as soon as the end-of-run file appears, even if "
395  "there are/will be unprocessed lumisections.");
396 
397  // desc.addUntracked<unsigned int>("skipEvents", 0U)
398  // ->setComment("Skip the first 'skipEvents' events that otherwise would "
399  // "have been processed.");
400 
401  // This next parameter is read in the base class, but its default value
402  // depends on the derived class, so it is set here.
403  desc.addUntracked<bool>("inputFileTransitionsEachEvent", false);
404 
408 
409  descriptions.add("source", desc);
410 }
411 
412 } // end of namespace
413 
416 
T getUntrackedParameter(std::string const &, T const &) const
static void fillDescription(ParameterSetDescription &description)
int i
Definition: DBlmapReader.cc:9
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void hltTriggerBits(uint8 *put_here) const
void logLumiState(const LumiEntry &lumi, const std::string &msg)
std::shared_ptr< TriggerSelector > eventSelector_
EventMsgView const * getEventMsg()
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
DQMFileIterator::LumiEntry currentLumi_
dqmservices::DQMStreamerReader DQMStreamerReader
std::unique_ptr< edm::StreamerInputFile > streamReader_
void advanceToLumi(unsigned int lumi)
std::string make_path_data(const LumiEntry &lumi)
void setComment(std::string const &value)
virtual void skip(int toSkip)
#define DEFINE_FWK_INPUT_SOURCE(type)
void openFile_(std::string filename)
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:142
void deserializeEvent(EventMsgView const &eventView)
void logFileAction(const std::string &msg, const std::string &fileName="") const
#define end
Definition: vmac.h:37
std::vector< std::vector< std::string >::const_iterator > regexMatch(std::vector< std::string > const &strings, boost::regex const &regexp)
Definition: RegexMatch.cc:30
DQMStreamerReader(edm::ParameterSet const &pset, edm::InputSourceDescription const &desc)
std::vector< std::string > Strings
bool acceptEvent(const EventMsgView *)
InitMsgView const * getHeaderMsg()
uint32 hltCount() const
Definition: EventMessage.h:95
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescription(ParameterSetDescription &desc)
uint32 code() const
Definition: InitMessage.h:70
void deserializeAndMergeWithRegistry(InitMsgView const &initView, bool subsequent=false)
bool matchTriggerSel(Strings const &tnames)
EventMsgView const * prepareNextEvent()
static void fillDescription(edm::ParameterSetDescription &d)