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