artdaq  v2_03_02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
StatisticsHelper.cc
1 #include "artdaq/Application/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 
74 readyToReport(size_t currentCount)
75 {
76  if (primary_stat_ptr_.get() != 0 &&
77  (currentCount % reporting_interval_fragments_) == 0)
78  {
79  double fullDuration = primary_stat_ptr_->getFullDuration();
80  size_t reportIndex = (size_t)(fullDuration / reporting_interval_seconds_);
81  if (reportIndex > previous_reporting_index_)
82  {
83  previous_reporting_index_ = reportIndex;
84  return true;
85  }
86  }
87 
88  return false;
89 }
90 
92 {
93  if (primary_stat_ptr_.get() != 0)
94  {
95  auto lastCalcTime = primary_stat_ptr_->getLastCalculationTime();
96  if (lastCalcTime > previous_stats_calc_time_)
97  {
98  auto now = MonitoredQuantity::getCurrentTime();
99  previous_stats_calc_time_ = std::min(lastCalcTime, now);
100  return true;
101  }
102  }
103 
104  return false;
105 }
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 ...
bool readyToReport(size_t currentCount)
Determine if the reporting interval conditions have been met.
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.