artdaq  v3_01_00
RandomDelayFilter_module.cc
1 // Class: RandomDelayFilter
3 // Module Type: filter
4 // File: RandomDelayFilter_module.cc
5 //
6 // Generated at Fri Mar 4 10:45:30 2016 by Eric Flumerfelt using artmod
7 // from cetpkgsupport v1_09_00.
9 
10 #define TRACE_NAME "RandomDelayFilter"
11 
12 #include "art/Framework/Core/EDFilter.h"
13 #include "art/Framework/Core/ModuleMacros.h"
14 #include "art/Framework/Principal/Event.h"
15 #include "art/Framework/Principal/Handle.h"
16 #include "art/Framework/Principal/Run.h"
17 #include "art/Framework/Principal/SubRun.h"
18 #include "canvas/Utilities/InputTag.h"
19 #include "fhiclcpp/ParameterSet.h"
20 
21 #include "artdaq/DAQdata/Globals.hh"
22 
23 #include <memory>
24 #include <random>
25 #include <chrono>
26 
27 namespace artdaq
28 {
29  class RandomDelayFilter;
30 }
31 
39 class artdaq::RandomDelayFilter : public art::EDFilter
40 {
41 public:
56  explicit RandomDelayFilter(fhicl::ParameterSet const& p);
57 
61  RandomDelayFilter(RandomDelayFilter const&) = delete;
62 
67 
73 
79 
89  bool filter(art::Event& e) override;
90 
91 private:
92  // Random Delay Parameters (min/max for uniform, mean/sigma for normal)
93  double min_ms_;
94  double max_ms_;
95  double mean_ms_;
96  double sigma_ms_;
97 
98  int pass_factor_;
99  double load_factor_;
100 
101  // Random Engine Setup
102  std::mt19937 engine_;
103  std::unique_ptr<std::uniform_real_distribution<double>> uniform_distn_;
104  std::unique_ptr<std::normal_distribution<double>> normal_distn_;
105  std::unique_ptr<std::exponential_distribution<double>> exponential_distn_;
106  std::unique_ptr<std::uniform_int_distribution<int>> pass_distn_;
107 
108  enum class DistType {
109  Fixed,
110  Uniform,
111  Normal,
112  Exponential
113  };
114  DistType distribution_type_;
115 };
116 
117 
118 artdaq::RandomDelayFilter::RandomDelayFilter(fhicl::ParameterSet const& p)
119  : min_ms_(p.get<double>("minimum_delay_ms", 0))
120  , max_ms_(p.get<double>("maximum_delay_ms", 1000))
121  , mean_ms_(p.get<double>("mean_delay_ms", 500))
122  , sigma_ms_(p.get<double>("sigma_delay_ms", 100))
123  , pass_factor_(p.get<int>("pass_filter_percentage", 100))
124  , load_factor_(p.get<double>("cpu_load_ratio", 1.0))
125  , engine_(p.get<int64_t>("random_seed", 271828))
126  , pass_distn_(new std::uniform_int_distribution<int>(0, 100))
127 {
128  // Set limits on parameters
129  if (pass_factor_ > 100) pass_factor_ = 100;
130  if (pass_factor_ < 0) pass_factor_ = 0;
131  if (load_factor_ < 0.0) load_factor_ = 0.0;
132  if (load_factor_ > 1.0) load_factor_ = 1.0;
133 
134  if (min_ms_ < 0) min_ms_ = 0;
135  if (min_ms_ > max_ms_) max_ms_ = min_ms_;
136  if (mean_ms_ < 0) mean_ms_ = 0;
137  if (sigma_ms_ < 0) sigma_ms_ = 0;
138 
139  auto type = p.get<std::string>("delay_distribution_type", "Uniform");
140  assert(type.size() >= 1);
141  switch (type[0])
142  {
143  case 'n':
144  case 'N':
145  TLOG(TLVL_INFO) << "Generating delay times using Normal distribution with mean " << mean_ms_ << " ms, std. dev. " << sigma_ms_ << " ms, and offset " << min_ms_ << ".";
146  distribution_type_ = DistType::Normal;
147  normal_distn_.reset(new std::normal_distribution<double>(mean_ms_, sigma_ms_));
148  break;
149  case 'e':
150  case 'E':
151  TLOG(TLVL_INFO) << "Generating delay times using Exponential distribution with mean " << mean_ms_ << " ms and offset " << min_ms_ << ".";
152  distribution_type_ = DistType::Exponential;
153  if (mean_ms_ == 0) mean_ms_ = 1;
154  exponential_distn_.reset(new std::exponential_distribution<double>(1/mean_ms_));
155  break;
156  case 'U':
157  case 'u':
158  TLOG(TLVL_INFO) << "Generating delay times using Uniform distribution with min " << min_ms_ << " ms and max " << max_ms_ << " ms.";
159  distribution_type_ = DistType::Uniform;
160  uniform_distn_.reset(new std::uniform_real_distribution<double>(min_ms_, max_ms_));
161  break;
162  case 'f':
163  case 'F':
164  default:
165  TLOG(TLVL_INFO) << "Delay time set to " << min_ms_ << " ms.";
166  distribution_type_ = DistType::Fixed;
167  break;
168  }
169 }
170 
172 {
173  double delay = min_ms_;
174  switch (distribution_type_)
175  {
176  case DistType::Normal:
177  delay += (*normal_distn_)(engine_);
178  break;
179  case DistType::Exponential:
180  delay += (*exponential_distn_)(engine_);
181  break;
182  case DistType::Uniform:
183  delay = (*uniform_distn_)(engine_);
184  break;
185  case DistType::Fixed:
186  break;
187  }
188  TLOG(TLVL_DEBUG) << "Simulating processing of event " << e.event() << " by delaying " << std::to_string(delay) << "ms." ;
189 
190  usleep(1000 * (1 - load_factor_) * delay);
191 
192  auto i = 0;
193  auto now = std::chrono::steady_clock::now();
194  while (TimeUtils::GetElapsedTimeMilliseconds(now) < static_cast<size_t>(delay * load_factor_))
195  {
196  i = i + 1 % std::numeric_limits<int>::max();
197  }
198 
199  return (*pass_distn_)(engine_) < pass_factor_;
200 }
201 
202 DEFINE_ART_MODULE(artdaq::RandomDelayFilter)
RandomDelayFilter(fhicl::ParameterSet const &p)
RandomDelayFilter Constructor.
bool filter(art::Event &e) override
Filter is a required override of art::EDFilter, and is called for each event.
A filter which delays for a random amount of time, then drops a random fraction of events...
RandomDelayFilter & operator=(RandomDelayFilter const &)=delete
Copy Assignment operator is deleted.