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