artdaq  v3_06_00
StatisticsHelper.cc
1 #include "artdaq/DAQrate/StatisticsHelper.hh"
2 
3 // This class is really nothing more than a collection of code that
4 // would be repeated throughout artdaq "application" classes if it
5 // weren't centralized here. So, we should be careful not to put
6 // too much intelligence in this class. (KAB, 07-Jan-2015)
7 
9 StatisticsHelper() : monitored_quantity_name_list_(0)
10 , primary_stat_ptr_(0)
11 , previous_reporting_index_(0)
12 , previous_stats_calc_time_(0.0) {}
13 
15 addMonitoredQuantityName(std::string const& statKey)
16 {
17  monitored_quantity_name_list_.push_back(statKey);
18 }
19 
20 void artdaq::StatisticsHelper::addSample(std::string const& statKey,
21  double value) const
22 {
23  artdaq::MonitoredQuantityPtr mqPtr =
24  artdaq::StatisticsCollection::getInstance().getMonitoredQuantity(statKey);
25  if (mqPtr.get() != 0) { mqPtr->addSample(value); }
26 }
27 
29 createCollectors(fhicl::ParameterSet const& pset, int defaultReportIntervalFragments,
30  double defaultReportIntervalSeconds, double defaultMonitorWindow,
31  std::string const& primaryStatKeyName)
32 {
33  reporting_interval_fragments_ =
34  pset.get<int>("reporting_interval_fragments", defaultReportIntervalFragments);
35  reporting_interval_seconds_ =
36  pset.get<double>("reporting_interval_seconds", defaultReportIntervalSeconds);
37 
38  double monitorWindow = pset.get<double>("monitor_window", defaultMonitorWindow);
39  double monitorBinSize =
40  pset.get<double>("monitor_binsize",
41  1.0 + static_cast<int>((monitorWindow - 1) / 100.0));
42 
43  if (monitorBinSize < 1.0) { monitorBinSize = 1.0; }
44  if (monitorWindow >= 1.0)
45  {
46  for (size_t idx = 0; idx < monitored_quantity_name_list_.size(); ++idx)
47  {
48  artdaq::MonitoredQuantityPtr
49  mqPtr(new artdaq::MonitoredQuantity(monitorBinSize,
50  monitorWindow));
51  artdaq::StatisticsCollection::getInstance().
52  addMonitoredQuantity(monitored_quantity_name_list_[idx], mqPtr);
53  }
54  }
55 
56  primary_stat_ptr_ = artdaq::StatisticsCollection::getInstance().
57  getMonitoredQuantity(primaryStatKeyName);
58  return (primary_stat_ptr_.get() != 0);
59 }
60 
62 {
63  previous_reporting_index_ = 0;
64  previous_stats_calc_time_ = 0.0;
65  for (size_t idx = 0; idx < monitored_quantity_name_list_.size(); ++idx)
66  {
67  artdaq::MonitoredQuantityPtr mqPtr = artdaq::StatisticsCollection::getInstance().
68  getMonitoredQuantity(monitored_quantity_name_list_[idx]);
69  if (mqPtr.get() != nullptr) { mqPtr->reset(); }
70  }
71 }
72 
75 {
76  if (primary_stat_ptr_.get() != 0)
77  {
78  double fullDuration = primary_stat_ptr_->getFullDuration();
79  size_t reportIndex = (size_t)(fullDuration / reporting_interval_seconds_);
80  if (reportIndex > previous_reporting_index_)
81  {
82  previous_reporting_index_ = reportIndex;
83  return true;
84  }
85  }
86 
87  return false;
88 }
89 
91 {
92  if (primary_stat_ptr_.get() != 0)
93  {
94  auto lastCalcTime = primary_stat_ptr_->getLastCalculationTime();
95  if (lastCalcTime > previous_stats_calc_time_)
96  {
97  auto now = MonitoredQuantity::getCurrentTime();
98  previous_stats_calc_time_ = std::min(lastCalcTime, now);
99  return true;
100  }
101  }
102 
103  return false;
104 }
void addMonitoredQuantityName(std::string const &statKey)
Add a MonitoredQuantity name to the list.
bool statsRollingWindowHasMoved()
Determine if the MonitoredQuantity &quot;recent&quot; window has changed since the last time this function was ...
void addSample(std::string const &statKey, double value) const
Add a sample to the MonitoredQuantity with the given name.
bool createCollectors(fhicl::ParameterSet const &pset, int defaultReportIntervalFragments, double defaultReportIntervalSeconds, double defaultMonitorWindow, std::string const &primaryStatKeyName)
Create MonitoredQuantity objects for all names registered with the StatisticsHelper.
StatisticsHelper()
StatisticsHelper default constructor.
void resetStatistics()
Reset all MonitoredQuantity instances.
bool readyToReport()
Determine if the reporting interval condition has been met.