artdaq  v3_09_01
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 
10  : monitored_quantity_name_list_(0)
11  , primary_stat_ptr_(nullptr) {}
12 
14  addMonitoredQuantityName(std::string const& statKey)
15 {
16  monitored_quantity_name_list_.push_back(statKey);
17 }
18 
19 void artdaq::StatisticsHelper::addSample(std::string const& statKey,
20  double value) const
21 {
22  artdaq::MonitoredQuantityPtr mqPtr =
23  artdaq::StatisticsCollection::getInstance().getMonitoredQuantity(statKey);
24  if (mqPtr.get() != nullptr) { mqPtr->addSample(value); }
25 }
26 
28  createCollectors(fhicl::ParameterSet const& pset, int defaultReportIntervalFragments,
29  double defaultReportIntervalSeconds, double defaultMonitorWindow,
30  std::string const& primaryStatKeyName)
31 {
32  reporting_interval_fragments_ =
33  pset.get<int>("reporting_interval_fragments", defaultReportIntervalFragments);
34  reporting_interval_seconds_ =
35  pset.get<double>("reporting_interval_seconds", defaultReportIntervalSeconds);
36 
37  auto monitorWindow = pset.get<double>("monitor_window", defaultMonitorWindow);
38  auto monitorBinSize =
39  pset.get<double>("monitor_binsize",
40  1.0 + static_cast<int>((monitorWindow - 1) / 100.0));
41 
42  if (monitorBinSize < 1.0) { monitorBinSize = 1.0; }
43  if (monitorWindow >= 1.0)
44  {
45  for (const auto& idx : monitored_quantity_name_list_)
46  {
47  artdaq::MonitoredQuantityPtr
48  mqPtr(new artdaq::MonitoredQuantity(monitorBinSize,
49  monitorWindow));
50  artdaq::StatisticsCollection::getInstance().addMonitoredQuantity(idx, mqPtr);
51  }
52  }
53 
54  primary_stat_ptr_ = artdaq::StatisticsCollection::getInstance().getMonitoredQuantity(primaryStatKeyName);
55  return (primary_stat_ptr_.get() != nullptr);
56 }
57 
59 {
60  previous_reporting_index_ = 0;
61  previous_stats_calc_time_ = 0.0;
62  for (const auto& idx : monitored_quantity_name_list_)
63  {
64  artdaq::MonitoredQuantityPtr mqPtr = artdaq::StatisticsCollection::getInstance().getMonitoredQuantity(idx);
65  if (mqPtr.get() != nullptr) { mqPtr->reset(); }
66  }
67 }
68 
71 {
72  if (primary_stat_ptr_.get() != nullptr)
73  {
74  double fullDuration = primary_stat_ptr_->getFullDuration();
75  auto reportIndex = static_cast<size_t>(fullDuration / reporting_interval_seconds_);
76  if (reportIndex > previous_reporting_index_)
77  {
78  previous_reporting_index_ = reportIndex;
79  return true;
80  }
81  }
82 
83  return false;
84 }
85 
87 {
88  if (primary_stat_ptr_.get() != nullptr)
89  {
90  auto lastCalcTime = primary_stat_ptr_->getLastCalculationTime();
91  if (lastCalcTime > previous_stats_calc_time_)
92  {
93  auto now = MonitoredQuantity::getCurrentTime();
94  previous_stats_calc_time_ = std::min(lastCalcTime, now);
95  return true;
96  }
97  }
98 
99  return false;
100 }
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.