EmptyEvent_source.cc
Go to the documentation of this file.
20 #include "fhiclcpp/ParameterSet.h"
21 #include "fhiclcpp/types/Atom.h"
26 
27 #include <cassert>
28 #include <cstdint>
29 #include <memory>
30 
31 namespace art {
32  class EmptyEvent;
33 }
34 
35 using namespace fhicl;
37 using std::uint32_t;
38 
39 class art::EmptyEvent final : public DRISI {
40 public:
41  struct Config {
42 
43  Atom<std::string> module_type{Name("module_type")};
45  Atom<int> numberEventsInRun{Name("numberEventsInRun"),
46  drisi_config().maxEvents()};
47  Atom<int> numberEventsInSubRun{Name("numberEventsInSubRun"),
48  drisi_config().maxSubRuns()};
49  Atom<uint32_t> eventCreationDelay{Name("eventCreationDelay"), 0u};
50  Atom<bool> resetEventOnSubRun{Name("resetEventOnSubRun"), true};
52  OptionalAtom<SubRunNumber_t> firstSubRun{Name("firstSubRun")};
53  OptionalAtom<EventNumber_t> firstEvent{Name("firstEvent")};
54  OptionalDelegatedParameter timestampPlugin{
55  Name("timestampPlugin"),
56  Comment(
57  "The 'timestampPlugin' parameter must be a FHiCL table\n"
58  "of the form:\n\n"
59  " timestampPlugin: {\n"
60  " plugin_type: <plugin specification>\n"
61  " ...\n"
62  " }\n\n"
63  "See the notes in art/Framework/Core/EmptyEventTimestampPlugin.h\n"
64  "for more details.")};
65 
66  struct KeysToIgnore {
67  std::set<std::string>
68  operator()() const
69  {
70  return {"module_label"};
71  }
72  };
73  };
74 
76 
77  explicit EmptyEvent(Parameters const& config, InputSourceDescription& desc);
78 
79  unsigned int
81  {
82  return numberEventsInRun_;
83  }
84  unsigned int
86  {
87  return numberEventsInSubRun_;
88  }
89  unsigned int
91  {
92  return eventCreationDelay_;
93  }
94  unsigned int
96  {
97  return numberEventsInThisRun_;
98  }
99  unsigned int
101  {
102  return numberEventsInThisSubRun_;
103  }
104 
105 private:
106  art::input::ItemType getNextItemType() override;
107  void setRunAndEventInfo();
108  std::unique_ptr<EventPrincipal> readEvent_() override;
109  std::unique_ptr<SubRunPrincipal> readSubRun_() override;
110  std::unique_ptr<RunPrincipal> readRun_() override;
111 
112  std::unique_ptr<RangeSetHandler> runRangeSetHandler() override;
113  std::unique_ptr<RangeSetHandler> subRunRangeSetHandler() override;
114 
115  void skip(int offset) override;
116  void rewind_() override;
117 
118  void beginJob() override;
119  void endJob() override;
120 
121  void reallyReadEvent(bool const lastEventInSubRun);
122 
123  std::unique_ptr<EmptyEventTimestampPlugin> makePlugin_(
124  OptionalDelegatedParameter const& maybeConfig);
125 
126  unsigned int numberEventsInRun_;
127  unsigned int numberEventsInSubRun_;
128  unsigned int eventCreationDelay_; /* microseconds */
129 
130  unsigned int numberEventsInThisRun_{};
131  unsigned int numberEventsInThisSubRun_{};
132  EventID eventID_{};
133  EventID origEventID_{};
134  bool newRun_{true};
135  bool newSubRun_{true};
136  bool subRunSet_{false};
137  bool eventSet_{false};
138  bool skipEventIncrement_{true};
140  std::unique_ptr<EventPrincipal> ep_{};
141 
142  cet::BasicPluginFactory pluginFactory_{};
143  std::unique_ptr<EmptyEventTimestampPlugin> plugin_;
144  bool parentageEnabled_{true};
145  bool rangesEnabled_{true};
146 }; // EmptyEvent
147 
148 using namespace art;
149 
150 // used for defaults
151 
155  desc.moduleDescription}
156  , numberEventsInRun_{static_cast<uint32_t>(config().numberEventsInRun())}
157  , numberEventsInSubRun_{static_cast<uint32_t>(
158  config().numberEventsInSubRun())}
159  , eventCreationDelay_{config().eventCreationDelay()}
160  , resetEventOnSubRun_{config().resetEventOnSubRun()}
161  , plugin_{makePlugin_(config().timestampPlugin)}
162  , parentageEnabled_{desc.parentageEnabled_}
163  , rangesEnabled_{desc.rangesEnabled_}
164 {
165 
167  bool haveFirstRun = config().firstRun(firstRun);
168  SubRunNumber_t firstSubRun{};
169  bool haveFirstSubRun = config().firstSubRun(firstSubRun);
170  EventNumber_t firstEvent{};
171  bool haveFirstEvent = config().firstEvent(firstEvent);
172  RunID firstRunID = haveFirstRun ? RunID(firstRun) : RunID::firstRun();
173  SubRunID firstSubRunID = haveFirstSubRun ?
174  SubRunID(firstRunID.run(), firstSubRun) :
175  SubRunID::firstSubRun(firstRunID);
176  origEventID_ =
177  haveFirstEvent ?
178  EventID(firstSubRunID.run(), firstSubRunID.subRun(), firstEvent) :
179  EventID::firstEvent(firstSubRunID);
181 }
182 
183 std::unique_ptr<RunPrincipal>
185 {
186  auto ts = plugin_ ? plugin_->doBeginRunTimestamp(eventID_.runID()) :
188  RunAuxiliary const runAux{
190  newRun_ = false;
191  auto rp_ptr = std::make_unique<RunPrincipal>(
193  if (plugin_) {
195  plugin_->doBeginRun(r);
196  }
197  return rp_ptr;
198 }
199 
200 std::unique_ptr<RangeSetHandler>
202 {
204  return std::make_unique<OpenRangeSetHandler>(eventID_.run());
205 }
206 
207 std::unique_ptr<SubRunPrincipal>
209 {
210  if (processingMode() == Runs)
211  return std::unique_ptr<SubRunPrincipal>{nullptr};
212  auto ts = plugin_ ? plugin_->doBeginSubRunTimestamp(eventID_.subRunID()) :
214  SubRunAuxiliary const subRunAux{
216  auto srp_ptr = std::make_unique<SubRunPrincipal>(subRunAux,
218  nullptr,
221  if (plugin_) {
222  SubRun const sr{
224  plugin_->doBeginSubRun(sr);
225  }
226  newSubRun_ = false;
227  return srp_ptr;
228 }
229 
230 std::unique_ptr<RangeSetHandler>
232 {
234  return std::make_unique<OpenRangeSetHandler>(eventID_.run());
235 }
236 
237 std::unique_ptr<EventPrincipal>
239 {
240  assert(ep_.get() != nullptr || processingMode() != RunsSubRunsAndEvents);
241  return std::move(ep_);
242 }
243 
244 void
246 {
247  if (plugin_) {
248  plugin_->doBeginJob();
249  }
250 }
251 
252 void
254 {
255  if (plugin_) {
256  plugin_->doEndJob();
257  }
258 }
259 
260 void
261 art::EmptyEvent::reallyReadEvent(bool const lastEventInSubRun)
262 {
264  return;
265  auto timestamp = plugin_ ? plugin_->doEventTimestamp(eventID_) :
267  EventAuxiliary const eventAux{
269 
270  ep_ = std::make_unique<EventPrincipal>(eventAux,
272  nullptr,
275  std::make_shared<History>(),
276  std::make_unique<BranchMapper>(),
277  std::make_unique<NoDelayedReader>(),
278  lastEventInSubRun);
279 }
280 
281 std::unique_ptr<art::EmptyEventTimestampPlugin>
283 {
284  std::unique_ptr<art::EmptyEventTimestampPlugin> result;
285  try {
286  ParameterSet pset;
287  if (maybeConfig.get_if_present(pset)) {
288  auto const libspec = pset.get<std::string>("plugin_type");
289  auto const pluginType = pluginFactory_.pluginType(libspec);
290  if (pluginType ==
292  result =
293  pluginFactory_.makePlugin<std::unique_ptr<EmptyEventTimestampPlugin>>(
294  libspec, pset);
295  } else {
296  throw Exception(errors::Configuration, "EmptyEvent: ")
297  << "unrecognized plugin type " << pluginType << "for plugin "
298  << libspec << ".\n";
299  }
300  }
301  }
302  catch (cet::exception& e) {
303  throw Exception(errors::Configuration, "EmptyEvent: ", e)
304  << "Exception caught while processing plugin spec.\n";
305  }
306  return result;
307 }
308 
309 void
311 {
312  for (; offset < 0; ++offset) {
314  }
315  for (; offset > 0; --offset) {
316  eventID_ = eventID_.next();
317  }
318 }
319 
320 void
322 {
323  if (plugin_) {
324  plugin_->doRewind();
325  }
328  skipEventIncrement_ = true;
331  newRun_ = newSubRun_ = true;
334 }
335 
338 {
339  if (newRun_) {
340  if (!eventID_.runID().isValid()) {
341  ep_.reset();
342  return input::IsStop;
343  }
344  return input::IsRun;
345  }
346  if (newSubRun_) {
347  return input::IsSubRun;
348  }
349  if (ep_.get() != nullptr)
350  return input::IsEvent;
351  EventID oldEventID = eventID_;
352  if (!eventSet_) {
353  subRunSet_ = false;
355  eventSet_ = true;
356  }
357  if (!eventID_.runID().isValid()) {
358  ep_.reset();
359  return input::IsStop;
360  }
361  if (oldEventID.runID() != eventID_.runID()) {
362  // New Run
363  // reset these since this event is in the new run
366  newRun_ = newSubRun_ = true;
369  return input::IsRun;
370  }
371  // Same Run
372  if (oldEventID.subRunID() != eventID_.subRunID()) {
373  // New Subrun
375  newSubRun_ = true;
377  if (processingMode() != Runs) {
378  return input::IsSubRun;
379  }
380  }
383  bool const lastEventInSubRun =
385  reallyReadEvent(lastEventInSubRun);
386  if (ep_.get() == nullptr) {
387  return input::IsStop;
388  }
389  eventSet_ = false;
390  return input::IsEvent;
391 }
392 
393 void
395 {
396  // NOTE: numberEventsInRun < 0 means go forever in this run
398  // same run
399  if (!(numberEventsInSubRun_ < 1 ||
401  // new subrun
402  if (resetEventOnSubRun_) {
404  } else {
406  }
407  } else if (skipEventIncrement_) { // For first event, rewind etc.
408  skipEventIncrement_ = false;
409  } else {
410  eventID_ = eventID_.next();
411  }
412  } else {
413  // new run
414  eventID_ = EventID(
416  }
417  if (eventCreationDelay_ > 0) {
418  usleep(eventCreationDelay_);
419  }
420 }
421 
std::unique_ptr< EventPrincipal > readEvent_() override
std::unique_ptr< EmptyEventTimestampPlugin > plugin_
EmptyEvent(Parameters const &config, InputSourceDescription &desc)
RunID const & runID() const
Definition: EventID.h:92
TableFragment< DRISI::Config > drisi_config
unsigned int numberEventsInThisRun_
void reallyReadEvent(bool const lastEventInSubRun)
Timestamp const & timestamp() const
Accessor for the current time, as seen by the input source.
std::unique_ptr< RunPrincipal > readRun_() override
static EventID firstEvent()
Definition: EventID.h:190
std::unique_ptr< EmptyEventTimestampPlugin > makePlugin_(OptionalDelegatedParameter const &maybeConfig)
auto const & moduleDescription() const
Definition: InputSource.h:59
std::string pluginType(std::string const &libspec)
static cet::exempt_ptr< Consumer > non_module_context()
EventID nextSubRun(EventNumber_t first=IDNumber< Level::Event >::first()) const
Definition: EventID.h:147
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
Definition: config.py:1
RunNumber_t run() const
Definition: EventID.h:98
RunNumber_t run() const
Definition: RunID.h:62
unsigned int numberEventsInSubRun() const
EventID nextRun() const
Definition: EventID.h:153
void rewind_() override
Definition: Run.h:31
std::unique_ptr< EventPrincipal > ep_
EventID previous() const
Definition: EventID.h:159
unsigned int numberEventsInThisRun() const
unsigned int numberEventsInRun_
unsigned int numberEventsInRun() const
void beginJob()
std::unique_ptr< SubRunPrincipal > readSubRun_() override
SubRunID const & subRunID() const
Definition: EventID.h:104
RunNumber_t run() const
Definition: SubRunID.h:83
std::unique_ptr< RangeSetHandler > runRangeSetHandler() override
T get(std::string const &key) const
Definition: ParameterSet.h:231
std::unique_ptr< RangeSetHandler > subRunRangeSetHandler() override
cet::BasicPluginFactory pluginFactory_
IDNumber_t< Level::SubRun > SubRunNumber_t
Definition: IDNumber.h:118
void skip(int offset) override
auto const & processConfiguration() const
Definition: InputSource.h:64
caf::StandardRecord * sr
unsigned int eventCreationDelay_
EventID next() const
Definition: EventID.h:134
static SubRunID firstSubRun()
Definition: SubRunID.h:151
void endJob() override
void setTimestamp(Timestamp const &theTime)
To set the current time, as seen by the input source.
static constexpr Timestamp invalidTimestamp()
Definition: Timestamp.h:83
EventNumber_t event() const
Definition: EventID.h:116
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
SubRunNumber_t subRun() const
Definition: EventID.h:110
std::set< std::string > operator()() const
std::enable_if_t<!std::is_function< RESULT_TYPE >::value, RESULT_TYPE > makePlugin(std::string const &libspec, ARGS &&...args)
void beginJob() override
IDNumber_t< Level::Event > EventNumber_t
Definition: IDNumber.h:117
bool isValid() const
Definition: RunID.h:68
assert(nhit_max >=nhit_nbins)
unsigned int eventCreationDelay() const
ModuleDescription const & moduleDescription
TRandom3 r(0)
unsigned int numberEventsInThisSubRun() const
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
ProcessingMode processingMode() const
RunsSubRunsAndEvents (default), RunsAndSubRuns, or Runs.
SubRunNumber_t subRun() const
Definition: SubRunID.h:89
static RunID firstRun()
Definition: RunID.h:114
unsigned int numberEventsInThisSubRun_
Float_t e
Definition: plot.C:35
#define DEFINE_ART_INPUT_SOURCE(klass)
unsigned int numberEventsInSubRun_
art::input::ItemType getNextItemType() override
IDNumber_t< Level::Run > RunNumber_t
Definition: IDNumber.h:119