LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Prescaler_module.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // Prescaler_plugin
4 //
5 // ======================================================================
6 
9 #include "fhiclcpp/types/Atom.h"
10 
11 #include <mutex>
12 
13 namespace art {
14  class Prescaler;
15 }
16 using namespace fhicl;
17 using art::Prescaler;
18 
19 // ======================================================================
20 
21 class art::Prescaler : public SharedFilter {
22 public:
23  struct Config {
24  Atom<size_t> prescaleFactor{Name("prescaleFactor")};
25  Atom<size_t> prescaleOffset{Name("prescaleOffset")};
26  };
27 
29  explicit Prescaler(Parameters const&, ProcessingFrame const&);
30 
31 private:
32  bool filter(Event&, ProcessingFrame const&) override;
33 
34  size_t count_{};
35  // Accept one in n events.
36  size_t const n_;
37  // An offset is allowed--i.e. sequence of events does not have to
38  // start at first event.
39  size_t const offset_;
40  std::mutex mutex_{};
41 
42 }; // Prescaler
43 
44 // ======================================================================
45 
47  : SharedFilter{config}
48  , n_{config().prescaleFactor()}
49  , offset_{config().prescaleOffset()}
50 {
51  async<InEvent>();
52 }
53 
54 bool
56 {
57  // The combination of incrementing, modulo dividing, and equality
58  // comparing must be synchronized. Changing count_ to the type
59  // std::atomic<size_t> would not help since the entire combination
60  // of operations must be atomic. Using a mutex here is cheaper than
61  // calling serialize(), since that will also serialize any of the
62  // module-level service callbacks invoked before and after this
63  // function is called.
64  std::lock_guard lock{mutex_};
65  ++count_;
66  return count_ % n_ == offset_;
67 }
68 
size_t const n_
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
parameter set interface
size_t const offset_
Prescaler(Parameters const &, ProcessingFrame const &)
Definition: MVAAlg.h:12
bool filter(Event &, ProcessingFrame const &) override