LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
EmptyEvent_source.cc
Go to the documentation of this file.
19 #include "cetlib/BasicPluginFactory.h"
20 #include "fhiclcpp/ParameterSet.h"
21 #include "fhiclcpp/types/Atom.h"
26 
27 #include <cstdint>
28 #include <memory>
29 
30 namespace art {
31  class EmptyEvent;
32 }
33 
34 using namespace fhicl;
36 using std::uint32_t;
37 
38 class art::EmptyEvent final : public DRISI {
39 public:
40  struct Config {
41 
42  Atom<std::string> module_type{Name("module_type")};
44  Atom<int> numberEventsInRun{Name("numberEventsInRun"),
45  drisi_config().maxEvents()};
46  Atom<int> numberEventsInSubRun{Name("numberEventsInSubRun"),
47  drisi_config().maxSubRuns()};
48  Atom<uint32_t> eventCreationDelay{Name("eventCreationDelay"), 0u};
49  Atom<bool> resetEventOnSubRun{Name("resetEventOnSubRun"), true};
50  OptionalAtom<RunNumber_t> firstRun{Name("firstRun")};
51  OptionalAtom<SubRunNumber_t> firstSubRun{Name("firstSubRun")};
52  OptionalAtom<EventNumber_t> firstEvent{Name("firstEvent")};
53  OptionalDelegatedParameter timestampPlugin{
54  Name("timestampPlugin"),
55  Comment(
56  "The 'timestampPlugin' parameter must be a FHiCL table\n"
57  "of the form:\n\n"
58  " timestampPlugin: {\n"
59  " plugin_type: <plugin specification>\n"
60  " ...\n"
61  " }\n\n"
62  "See the notes in art/Framework/Core/EmptyEventTimestampPlugin.h\n"
63  "for more details.")};
64 
65  struct KeysToIgnore {
66  std::set<std::string>
67  operator()() const
68  {
69  return {"module_label"};
70  }
71  };
72  };
73 
75 
76  explicit EmptyEvent(Parameters const& config, InputSourceDescription& desc);
77 
78  unsigned int
80  {
81  return numberEventsInRun_;
82  }
83  unsigned int
85  {
86  return numberEventsInSubRun_;
87  }
88  unsigned int
90  {
91  return eventCreationDelay_;
92  }
93  unsigned int
95  {
96  return numberEventsInThisRun_;
97  }
98  unsigned int
100  {
101  return numberEventsInThisSubRun_;
102  }
103 
104 private:
105  art::input::ItemType getNextItemType() override;
106  void setRunAndEventInfo();
107  std::unique_ptr<EventPrincipal> readEvent_() override;
108  std::unique_ptr<SubRunPrincipal> readSubRun_() override;
109  std::unique_ptr<RunPrincipal> readRun_() override;
110 
111  std::unique_ptr<RangeSetHandler> runRangeSetHandler() override;
112  std::unique_ptr<RangeSetHandler> subRunRangeSetHandler() override;
113 
114  void skip(int offset) override;
115  void rewind_() override;
116 
117  void beginJob() override;
118  void endJob() override;
119 
120  void reallyReadEvent(bool const lastEventInSubRun);
121 
122  std::unique_ptr<EmptyEventTimestampPlugin> makePlugin_(
123  OptionalDelegatedParameter const& maybeConfig);
124 
125  unsigned int numberEventsInRun_;
126  unsigned int numberEventsInSubRun_;
127  unsigned int eventCreationDelay_; /* microseconds */
128 
129  unsigned int numberEventsInThisRun_{};
130  unsigned int numberEventsInThisSubRun_{};
131  EventID eventID_{};
132  EventID origEventID_{};
133  bool newRun_{true};
134  bool newSubRun_{true};
135  bool subRunSet_{false};
136  bool eventSet_{false};
137  bool skipEventIncrement_{true};
139  std::unique_ptr<EventPrincipal> ep_{};
140 
141  cet::BasicPluginFactory pluginFactory_{};
142  std::unique_ptr<EmptyEventTimestampPlugin> plugin_;
143 }; // EmptyEvent
144 
145 using namespace art;
146 
147 // used for defaults
148 
151  : DecrepitRelicInputSourceImplementation{config().drisi_config,
152  desc.moduleDescription}
153  , numberEventsInRun_{static_cast<uint32_t>(config().numberEventsInRun())}
154  , numberEventsInSubRun_{static_cast<uint32_t>(
155  config().numberEventsInSubRun())}
156  , eventCreationDelay_{config().eventCreationDelay()}
157  , resetEventOnSubRun_{config().resetEventOnSubRun()}
158  , plugin_{makePlugin_(config().timestampPlugin)}
159 {
160 
161  RunNumber_t firstRun{};
162  bool haveFirstRun = config().firstRun(firstRun);
163  SubRunNumber_t firstSubRun{};
164  bool haveFirstSubRun = config().firstSubRun(firstSubRun);
165  EventNumber_t firstEvent{};
166  bool haveFirstEvent = config().firstEvent(firstEvent);
167  RunID firstRunID = haveFirstRun ? RunID(firstRun) : RunID::firstRun();
168  SubRunID firstSubRunID = haveFirstSubRun ?
169  SubRunID(firstRunID.run(), firstSubRun) :
170  SubRunID::firstSubRun(firstRunID);
171  origEventID_ =
172  haveFirstEvent ?
173  EventID(firstSubRunID.run(), firstSubRunID.subRun(), firstEvent) :
174  EventID::firstEvent(firstSubRunID);
176 }
177 
178 std::unique_ptr<RunPrincipal>
180 {
181  auto ts = plugin_ ? plugin_->doBeginRunTimestamp(eventID_.runID()) :
183  RunAuxiliary const runAux{
185  newRun_ = false;
186  auto rp_ptr =
187  std::make_unique<RunPrincipal>(runAux, processConfiguration(), nullptr);
188  if (plugin_) {
189  Run const r{*rp_ptr, moduleDescription(), Consumer::non_module_context()};
190  plugin_->doBeginRun(r);
191  }
192  return rp_ptr;
193 }
194 
195 std::unique_ptr<RangeSetHandler>
197 {
198  return std::make_unique<OpenRangeSetHandler>(eventID_.run());
199 }
200 
201 std::unique_ptr<SubRunPrincipal>
203 {
204  if (processingMode() == Runs)
205  return std::unique_ptr<SubRunPrincipal>{nullptr};
206  auto ts = plugin_ ? plugin_->doBeginSubRunTimestamp(eventID_.subRunID()) :
208  SubRunAuxiliary const subRunAux{
210  auto srp_ptr = std::make_unique<SubRunPrincipal>(
211  subRunAux, processConfiguration(), nullptr);
212  if (plugin_) {
213  SubRun const sr{
215  plugin_->doBeginSubRun(sr);
216  }
217  newSubRun_ = false;
218  return srp_ptr;
219 }
220 
221 std::unique_ptr<RangeSetHandler>
223 {
224  return std::make_unique<OpenRangeSetHandler>(eventID_.run());
225 }
226 
227 std::unique_ptr<EventPrincipal>
229 {
230  assert(ep_.get() != nullptr || processingMode() != RunsSubRunsAndEvents);
231  return std::move(ep_);
232 }
233 
234 void
236 {
237  if (plugin_) {
238  plugin_->doBeginJob();
239  }
240 }
241 
242 void
244 {
245  if (plugin_) {
246  plugin_->doEndJob();
247  }
248 }
249 
250 void
251 art::EmptyEvent::reallyReadEvent(bool const lastEventInSubRun)
252 {
254  return;
255  auto timestamp = plugin_ ? plugin_->doEventTimestamp(eventID_) :
257  EventAuxiliary const eventAux{
259 
260  ep_ = std::make_unique<EventPrincipal>(eventAux,
262  nullptr,
263  std::make_shared<History>(),
264  std::make_unique<BranchMapper>(),
265  std::make_unique<NoDelayedReader>(),
266  lastEventInSubRun);
267 }
268 
269 std::unique_ptr<art::EmptyEventTimestampPlugin>
271 {
272  std::unique_ptr<art::EmptyEventTimestampPlugin> result;
273  try {
274  ParameterSet pset;
275  if (maybeConfig.get_if_present(pset)) {
276  auto const libspec = pset.get<std::string>("plugin_type");
277  auto const pluginType = pluginFactory_.pluginType(libspec);
278  if (pluginType ==
280  result =
281  pluginFactory_.makePlugin<std::unique_ptr<EmptyEventTimestampPlugin>>(
282  libspec, pset);
283  } else {
284  throw Exception(errors::Configuration, "EmptyEvent: ")
285  << "unrecognized plugin type " << pluginType << "for plugin "
286  << libspec << ".\n";
287  }
288  }
289  }
290  catch (cet::exception& e) {
291  throw Exception(errors::Configuration, "EmptyEvent: ", e)
292  << "Exception caught while processing plugin spec.\n";
293  }
294  return result;
295 }
296 
297 void
299 {
300  for (; offset < 0; ++offset) {
302  }
303  for (; offset > 0; --offset) {
304  eventID_ = eventID_.next();
305  }
306 }
307 
308 void
310 {
311  if (plugin_) {
312  plugin_->doRewind();
313  }
316  skipEventIncrement_ = true;
319  newRun_ = newSubRun_ = true;
322 }
323 
326 {
327  if (newRun_) {
328  if (!eventID_.runID().isValid()) {
329  ep_.reset();
330  return input::IsStop;
331  }
332  return input::IsRun;
333  }
334  if (newSubRun_) {
335  return input::IsSubRun;
336  }
337  if (ep_.get() != nullptr)
338  return input::IsEvent;
339  EventID oldEventID = eventID_;
340  if (!eventSet_) {
341  subRunSet_ = false;
343  eventSet_ = true;
344  }
345  if (!eventID_.runID().isValid()) {
346  ep_.reset();
347  return input::IsStop;
348  }
349  if (oldEventID.runID() != eventID_.runID()) {
350  // New Run
351  // reset these since this event is in the new run
354  newRun_ = newSubRun_ = true;
357  return input::IsRun;
358  }
359  // Same Run
360  if (oldEventID.subRunID() != eventID_.subRunID()) {
361  // New Subrun
363  newSubRun_ = true;
365  if (processingMode() != Runs) {
366  return input::IsSubRun;
367  }
368  }
371  bool const lastEventInSubRun =
373  reallyReadEvent(lastEventInSubRun);
374  if (ep_.get() == nullptr) {
375  return input::IsStop;
376  }
377  eventSet_ = false;
378  return input::IsEvent;
379 }
380 
381 void
383 {
384  // NOTE: numberEventsInRun < 0 means go forever in this run
386  // same run
387  if (!(numberEventsInSubRun_ < 1 ||
389  // new subrun
390  if (resetEventOnSubRun_) {
392  } else {
394  }
395  } else if (skipEventIncrement_) { // For first event, rewind etc.
396  skipEventIncrement_ = false;
397  } else {
398  eventID_ = eventID_.next();
399  }
400  } else {
401  // new run
402  eventID_ = EventID(
404  }
405  if (eventCreationDelay_ > 0) {
406  usleep(eventCreationDelay_);
407  }
408 }
409 
EventID previous() const
Definition: EventID.h:160
RunID const & runID() const
Definition: EventID.h:93
SubRunID const & subRunID() const
Definition: EventID.h:105
std::unique_ptr< EventPrincipal > readEvent_() override
std::unique_ptr< EmptyEventTimestampPlugin > plugin_
EventID next() const
Definition: EventID.h:135
EmptyEvent(Parameters const &config, InputSourceDescription &desc)
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
std::unique_ptr< EmptyEventTimestampPlugin > makePlugin_(OptionalDelegatedParameter const &maybeConfig)
auto const & moduleDescription() const
Definition: InputSource.h:59
static cet::exempt_ptr< Consumer > non_module_context()
Definition: Consumer.cc:76
RunNumber_t run() const
Definition: RunID.h:63
unsigned int numberEventsInSubRun() const
RunNumber_t run() const
Definition: EventID.h:99
void rewind_() override
Definition: Run.h:30
std::unique_ptr< EventPrincipal > ep_
unsigned int numberEventsInThisRun() const
unsigned int numberEventsInRun_
unsigned int numberEventsInRun() const
void beginJob()
Definition: Breakpoints.cc:14
std::unique_ptr< SubRunPrincipal > readSubRun_() override
parameter set interface
RunNumber_t run() const
Definition: SubRunID.h:84
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
EventID nextSubRun(EventNumber_t first=IDNumber< Level::Event >::first()) const
Definition: EventID.h:148
unsigned int eventCreationDelay_
static SubRunID firstSubRun()
Definition: SubRunID.h:152
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
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string value(boost::any const &)
std::set< std::string > operator()() const
void beginJob() override
IDNumber_t< Level::Event > EventNumber_t
Definition: IDNumber.h:117
bool isValid() const
Definition: RunID.h:69
unsigned int eventCreationDelay() const
ModuleDescription const & moduleDescription
unsigned int numberEventsInThisSubRun() const
HLT enums.
EventNumber_t event() const
Definition: EventID.h:117
ProcessingMode processingMode() const
RunsSubRunsAndEvents (default), RunsAndSubRuns, or Runs.
SubRunNumber_t subRun() const
Definition: SubRunID.h:90
static RunID firstRun()
Definition: RunID.h:115
EventID nextRun() const
Definition: EventID.h:154
unsigned int numberEventsInThisSubRun_
Float_t e
Definition: plot.C:34
static EventID firstEvent()
Definition: EventID.h:191
#define DEFINE_ART_INPUT_SOURCE(klass)
unsigned int numberEventsInSubRun_
SubRunNumber_t subRun() const
Definition: EventID.h:111
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
art::input::ItemType getNextItemType() override
IDNumber_t< Level::Run > RunNumber_t
Definition: IDNumber.h:119