artdaq  v3_07_02
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 <chrono>
24 #include <memory>
25 #include <random>
26 
27 namespace artdaq {
28 class RandomDelayFilter;
29 }
30 
38 class artdaq::RandomDelayFilter : public art::EDFilter
39 {
40 public:
55  explicit RandomDelayFilter(fhicl::ParameterSet const& p);
56 
60  RandomDelayFilter(RandomDelayFilter const&) = delete;
61 
66 
72 
78 
88  bool filter(art::Event& e) override;
89 
90 private:
91  // Random Delay Parameters (min/max for uniform, mean/sigma for normal)
92  double min_ms_;
93  double max_ms_;
94  double mean_ms_;
95  double sigma_ms_;
96 
97  int pass_factor_;
98  double load_factor_;
99 
100  // Random Engine Setup
101  std::mt19937 engine_;
102  std::unique_ptr<std::uniform_real_distribution<double>> uniform_distn_;
103  std::unique_ptr<std::normal_distribution<double>> normal_distn_;
104  std::unique_ptr<std::exponential_distribution<double>> exponential_distn_;
105  std::unique_ptr<std::uniform_int_distribution<int>> pass_distn_;
106 
107  enum class DistType
108  {
109  Fixed,
110  Uniform,
111  Normal,
112  Exponential
113  };
114  DistType distribution_type_;
115 };
116 
117 artdaq::RandomDelayFilter::RandomDelayFilter(fhicl::ParameterSet const& p)
118  :
119 #if ART_HEX_VERSION >= 0x30200
120  art::EDFilter(p)
121  ,
122 #endif
123  min_ms_(p.get<double>("minimum_delay_ms", 0))
124  , max_ms_(p.get<double>("maximum_delay_ms", 1000))
125  , mean_ms_(p.get<double>("mean_delay_ms", 500))
126  , sigma_ms_(p.get<double>("sigma_delay_ms", 100))
127  , pass_factor_(p.get<int>("pass_filter_percentage", 100))
128  , load_factor_(p.get<double>("cpu_load_ratio", 0.5))
129  , engine_(p.get<int64_t>("random_seed", time(0)))
130  , pass_distn_(new std::uniform_int_distribution<int>(0, 100))
131 {
132  // Set limits on parameters
133  if (pass_factor_ > 100) pass_factor_ = 100;
134  if (pass_factor_ < 0) pass_factor_ = 0;
135  if (load_factor_ < 0.0) load_factor_ = 0.0;
136  if (load_factor_ > 1.0) load_factor_ = 1.0;
137 
138  if (min_ms_ < 0) min_ms_ = 0;
139  if (min_ms_ > max_ms_) max_ms_ = min_ms_;
140  if (mean_ms_ < 0) mean_ms_ = 0;
141  if (sigma_ms_ < 0) sigma_ms_ = 0;
142 
143  auto type = p.get<std::string>("distribution_type", "Uniform");
144  assert(type.size() >= 1);
145  switch (type[0])
146  {
147  case 'n':
148  case 'N':
149  TLOG(TLVL_INFO) << "Generating delay times using Normal distribution with mean " << mean_ms_ << " ms, std. dev. " << sigma_ms_ << " ms, min " << min_ms_ << " ms and max " << max_ms_ << "ms.";
150  distribution_type_ = DistType::Normal;
151  if (mean_ms_ < min_ms_)
152  {
153  TLOG(TLVL_WARNING) << "Mean is smaller than min, setting to min";
154  mean_ms_ = min_ms_;
155  }
156  mean_ms_ -= min_ms_; // When we sample the distribution, we offset by min_ms_
157  normal_distn_.reset(new std::normal_distribution<double>(mean_ms_, sigma_ms_));
158  break;
159  case 'e':
160  case 'E':
161  TLOG(TLVL_INFO) << "Generating delay times using Exponential distribution with mean " << mean_ms_ << " ms, min " << min_ms_ << " ms and max " << max_ms_ << " ms.";
162  distribution_type_ = DistType::Exponential;
163  if (mean_ms_ < min_ms_)
164  {
165  TLOG(TLVL_WARNING) << "Mean is smaller than min, setting to min";
166  mean_ms_ = min_ms_;
167  }
168  mean_ms_ -= min_ms_; // When we sample the distribution, we offset by min_ms_
169  if (mean_ms_ == 0) mean_ms_ = 1;
170  exponential_distn_.reset(new std::exponential_distribution<double>(1 / mean_ms_));
171  break;
172  case 'U':
173  case 'u':
174  TLOG(TLVL_INFO) << "Generating delay times using Uniform distribution with min " << min_ms_ << " ms and max " << max_ms_ << " ms.";
175  distribution_type_ = DistType::Uniform;
176  uniform_distn_.reset(new std::uniform_real_distribution<double>(min_ms_, max_ms_));
177  break;
178  case 'f':
179  case 'F':
180  default:
181  TLOG(TLVL_INFO) << "Delay time set to " << min_ms_ << " ms.";
182  distribution_type_ = DistType::Fixed;
183  break;
184  }
185 }
186 
188 {
189  double delay = min_ms_;
190  do
191  {
192  switch (distribution_type_)
193  {
194  case DistType::Normal:
195  delay += (*normal_distn_)(engine_);
196  break;
197  case DistType::Exponential:
198  delay += (*exponential_distn_)(engine_);
199  break;
200  case DistType::Uniform:
201  delay = (*uniform_distn_)(engine_);
202  break;
203  case DistType::Fixed:
204  break;
205  }
206  } while (delay > max_ms_ && delay < min_ms_);
207  TLOG(TLVL_DEBUG) << "Simulating processing of event " << e.event() << " by delaying " << delay << "ms.";
208 
209  usleep(1000 * (1 - load_factor_) * delay);
210 
211  auto i = 0;
212  auto now = std::chrono::steady_clock::now();
213  while (TimeUtils::GetElapsedTimeMilliseconds(now) < static_cast<size_t>(delay * load_factor_))
214  {
215  i = i + 1 % std::numeric_limits<int>::max();
216  }
217 
218  return (*pass_distn_)(engine_) < pass_factor_;
219 }
220 
221 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.