BlockingPrescaler_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // BlockingPrescaler.
3 //
4 // Accept m in n events with offset. So, for blockSize (m) = 5,
5 // stepSize (n) = 7 and offset = 4, the filter will accept events 5-9
6 // inclusive, 12-16 inclusive, 19-23 inclusive, etc.
7 //
8 // Note that BlockingPrescaler prescales based on the number of events
9 // seen by this module, *not* the event number as recorded by EventID.
10 ////////////////////////////////////////////////////////////////////////
11 
15 #include "fhiclcpp/types/Atom.h"
16 
17 #include <mutex>
18 
19 using namespace fhicl;
20 
21 namespace art {
22  class BlockingPrescaler;
23 }
24 
25 // ======================================================================
26 
28 public:
29  struct Config {
30  Atom<size_t> blockSize{Name("blockSize"), 1};
31  Atom<size_t> stepSize{
32  Name("stepSize"),
33  Comment(
34  "The value of 'stepSize' cannot be less than that of 'blockSize'.")};
35  Atom<size_t> offset{Name("offset"), 0};
36  };
37 
39  explicit BlockingPrescaler(Parameters const&, ProcessingFrame const&);
40 
41 private:
42  bool filter(Event&, ProcessingFrame const&) override;
43 
44  size_t count_{};
45  size_t const m_; // accept m in n (sequentially).
46  size_t const n_;
47  size_t const offset_; // First accepted event is 1 + offset.
48  std::mutex mutex_{};
49 }; // BlockingPrescaler
50 
51 // ======================================================================
52 
54  ProcessingFrame const&)
55  : SharedFilter{config}
56  , m_{config().blockSize()}
57  , n_{config().stepSize()}
58  , offset_{config().offset()}
59 {
60  if (n_ < m_) {
62  "There was an error configuring Blocking Prescaler.\n"}
63  << "The specified step size (" << n_ << ") is less than the block size ("
64  << m_ << ")\n";
65  }
66  async<InEvent>();
67 }
68 
69 bool
71 {
72  // This sequence of operations/comparisons must be serialized.
73  // Changing 'count_' to be of type std::atomic<size_t> will not
74  // help. Using a mutex here is cheaper than calling serialize(),
75  // since that will also serialize any of the module-level service
76  // callbacks invoked before and after this function is called.
77  std::lock_guard lock{mutex_};
78  bool const result{(count_ >= offset_) && ((count_ - offset_) % n_) < m_};
79  ++count_;
80  return result;
81 }
82 
static QCString result
BlockingPrescaler(Parameters const &, ProcessingFrame const &)
ChannelGroupService::Name Name
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
#define Comment
bool filter(Event &, ProcessingFrame const &) override