LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
BlockingPrescaler_module.cc
Go to the documentation of this file.
1 // 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.
11 
13 #include "art/Framework/Core/fwd.h"
14 #include "fhiclcpp/types/Atom.h"
15 
16 #include <mutex>
17 
18 using namespace fhicl;
19 
20 namespace art {
21  class BlockingPrescaler;
22 }
23 
24 // ======================================================================
25 
27 public:
28  struct Config {
29  Atom<size_t> blockSize{Name("blockSize"), 1};
30  Atom<size_t> stepSize{
31  Name("stepSize"),
32  Comment(
33  "The value of 'stepSize' cannot be less than that of 'blockSize'.")};
34  Atom<size_t> offset{Name("offset"), 0};
35  };
36 
38  explicit BlockingPrescaler(Parameters const&, ProcessingFrame const&);
39 
40 private:
41  bool filter(Event&, ProcessingFrame const&) override;
42 
43  size_t count_{};
44  size_t const m_; // accept m in n (sequentially).
45  size_t const n_;
46  size_t const offset_; // First accepted event is 1 + offset.
47  std::mutex mutex_{};
48 }; // BlockingPrescaler
49 
50 // ======================================================================
51 
53  ProcessingFrame const&)
54  : SharedFilter{config}
55  , m_{config().blockSize()}
56  , n_{config().stepSize()}
57  , offset_{config().offset()}
58 {
59  if (n_ < m_) {
61  "There was an error configuring Blocking Prescaler.\n"}
62  << "The specified step size (" << n_ << ") is less than the block size ("
63  << m_ << ")\n";
64  }
65  async<InEvent>();
66 }
67 
68 bool
70 {
71  // This sequence of operations/comparisons must be serialized.
72  // Changing 'count_' to be of type std::atomic<size_t> will not
73  // help. Using a mutex here is cheaper than calling serialize(),
74  // since that will also serialize any of the module-level service
75  // callbacks invoked before and after this function is called.
76  std::lock_guard lock{mutex_};
77  bool const result{(count_ >= offset_) && ((count_ - offset_) % n_) < m_};
78  ++count_;
79  return result;
80 }
81 
BlockingPrescaler(Parameters const &, ProcessingFrame const &)
Framework includes.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
parameter set interface
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
Definition: MVAAlg.h:12
bool filter(Event &, ProcessingFrame const &) override