ATLAS Offline Software
StatUtils.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ACTSUTILS_STAT_H
6 #define ACTSUTILS_STAT_H 1
7 #include <cmath>
8 #include <iomanip>
9 #include <ostream>
10 #include <string>
11 #include <sstream>
12 #include <vector>
13 
14 namespace ActsUtils {
16 class Stat {
17 public:
20  void add(double val) {
21  ++m_n;
22  m_sum += val;
23  m_sum2 += val*val;
26  }
27  unsigned int n() const { return m_n; }
28  double min() const { return m_min; }
29  double max() const { return m_max; }
30  double mean() const { return m_n>0 ? m_sum/m_n : 0.; }
31  double rms2() const { return m_n>1 ? (m_sum2 - m_sum *m_sum/m_n)/(m_n-1) : 0.; }
32  double rms() const { return std::sqrt( rms2() ); }
33 
35  void reset() {
36  m_n=0;
37  m_sum=0.;
38  m_sum2=0.;
41  }
42 
44  Stat &operator +=(const Stat &b) {
45  m_n += b.m_n;
46  m_sum += b.m_sum;
47  m_sum2 += b.m_sum2;
48  m_min = std::min(m_min, b.m_min);
49  m_max = std::max(m_max, b.m_max);
50  return *this;
51  }
52 
53  unsigned int m_n=0;
54  double m_sum=0.;
55  double m_sum2=0.;
58 };
59 
61 template <class T_Stream>
62 inline void dumpStat(T_Stream &out, const Stat &stat) {
63  if (stat.n() > 1) {
64  out << std::setw(14) << stat.min() << " < "
65  << std::setw(14) << stat.mean() << " +- " << std::setw(14) << stat.rms() << " < "
66  << std::setw(14) << stat.max()
67  << " / " << std::setw(9) << stat.n();
68  }
69  else {
70  out << std::setw(14*4+9+3*3+4) << stat.mean();
71  }
72 }
73 
74 inline std::ostream &operator<<(std::ostream &out, const Stat &stat) {
75  dumpStat(out, stat);
76  return out;
77 }
78 
80 class StatHist : public Stat {
81 public:
83  StatHist() = default;
84 
89  StatHist(unsigned int n_bins, float xmin, float xmax)
90  {
91  setBinning(n_bins,xmin,xmax);
92  }
93 
98  void setBinning(unsigned int n_bins, float xmin, float xmax)
99  {
100  m_xmin=xmin;
101  m_scale = ( n_bins / (xmax-xmin) );
102  m_xmin -= 1./m_scale;
103  m_histogram.resize(n_bins+2,0u);
104  }
105 
108  StatHist tmp;
109  tmp.m_histogram.resize( m_histogram.size());
110  tmp.m_xmin = m_xmin;
111  tmp.m_scale = m_scale;
112  return tmp;
113  }
114 
116  void add(double val) {
117  Stat::add(val);
118  if (!m_histogram.empty()) {
119  unsigned int bin = std::min( static_cast<unsigned int>(m_histogram.size()-1),
120  static_cast<unsigned int>( std::max(0.,(val - m_xmin)*m_scale)) );
121  ++m_histogram.at(bin) ;
122  }
123  }
124 
126  void reset() {
127  Stat::reset();
128  for (unsigned int &bin : m_histogram) {
129  bin = 0u;
130  }
131  }
132 
136  if (m_histogram.size() == b.m_histogram.size()) {
137  for (unsigned int bin_i=0; bin_i< m_histogram.size(); ++bin_i) {
138  m_histogram[bin_i] += b.m_histogram[bin_i];
139  }
140  }
141  return *this;
142  }
143 
146  double lowerEdge(unsigned int i) const {
147  return m_xmin + i/m_scale;
148  }
149 
152  std::string histogramToString() const {
153  std::stringstream msg;
154  if (m_histogram.size()>2) {
155  unsigned int max_val = 0;
156  for (const auto &count : m_histogram) {
157  max_val = std::max(max_val, count);
158  }
159  double bin_width=1./m_scale;
160  unsigned int w = static_cast<unsigned int>(log(1.*max_val) / log(10.))+1;
161  unsigned int wtitle = std::max(10u,w);;
162  msg << (m_xmin+bin_width) << " .. " << ((m_histogram.size()-2)/m_scale + m_xmin+bin_width) << " : "
163  << std::setw(wtitle) << "lower edge" << " |";
164  for (unsigned int i=1; i<m_histogram.size()-1; ++i) {
165  msg << " " << std::setw(w) << lowerEdge(i);
166  }
167  msg << " | " << std::endl;
168  msg << (m_xmin+bin_width) << " .. " << ((m_histogram.size()-2)/m_scale + m_xmin+bin_width) << " : "
169 
170  << std::setw(wtitle) << m_histogram[0] << " |";
171  for (unsigned int i=1; i<m_histogram.size()-1; ++i) {
172  msg << " " << std::setw(w) << m_histogram[i];
173  }
174  msg << " | " << std::setw(w) << m_histogram.back();
175  }
176  return msg.str();
177  }
178 
179  double m_xmin{};
180  double m_scale{1.0};
181  std::vector<unsigned int> m_histogram;
182 };
183 }
184 #endif
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
CheckAppliedSFs.bin_width
bin_width
Definition: CheckAppliedSFs.py:242
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
bin
Definition: BinsDiffFromStripMedian.h:43
ActsUtils::StatHist
Extend Stat helper by an equidistant binned histogram.
Definition: StatUtils.h:80
ActsUtils::Stat::operator+=
Stat & operator+=(const Stat &b)
@breif Add the statistics gathered in the Stat object b
Definition: StatUtils.h:44
ActsUtils::Stat::m_max
double m_max
Definition: StatUtils.h:57
ActsUtils::StatHist::lowerEdge
double lowerEdge(unsigned int i) const
Get the lower edge of the given bin.
Definition: StatUtils.h:146
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
ActsUtils::Stat
Simple class to gather statistics : min, max, mean, rms.
Definition: StatUtils.h:16
ActsUtils::Stat::add
void add(double val)
@bruef Gather a new value will update min, max and the sums to compute mean and rms
Definition: StatUtils.h:20
ActsUtils::dumpStat
void dumpStat(T_Stream &out, const Stat &stat)
Dump the given statistics object to the given output stream.
Definition: StatUtils.h:62
ActsUtils::StatHist::StatHist
StatHist()=default
The default constructor will disable histogramming.
lumiFormat.i
int i
Definition: lumiFormat.py:85
xmin
double xmin
Definition: listroot.cxx:60
ActsUtils::Stat::n
unsigned int n() const
Definition: StatUtils.h:27
ActsUtils::StatHist::histogramToString
std::string histogramToString() const
Create a string showing the contents of the histogram The string.
Definition: StatUtils.h:152
ActsUtils::Stat::m_sum
double m_sum
Definition: StatUtils.h:54
ActsUtils::Stat::m_n
unsigned int m_n
Definition: StatUtils.h:53
ActsUtils
Definition: StatUtils.h:14
ActsUtils::StatHist::m_scale
double m_scale
Definition: StatUtils.h:180
ActsUtils::Stat::min
double min() const
Definition: StatUtils.h:28
ActsUtils::StatHist::operator+=
StatHist & operator+=(const StatHist &b)
Add the statistucs and histogrammed data fro the given object.
Definition: StatUtils.h:134
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
ActsUtils::Stat::max
double max() const
Definition: StatUtils.h:29
beamspotman.stat
stat
Definition: beamspotman.py:266
ActsUtils::StatHist::createEmptyClone
StatHist createEmptyClone()
Definition: StatUtils.h:107
ActsUtils::StatHist::m_xmin
double m_xmin
Definition: StatUtils.h:179
ActsUtils::Stat::mean
double mean() const
Definition: StatUtils.h:30
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
ActsUtils::Stat::m_sum2
double m_sum2
Definition: StatUtils.h:55
ActsUtils::Stat::m_min
double m_min
Definition: StatUtils.h:56
ActsUtils::StatHist::StatHist
StatHist(unsigned int n_bins, float xmin, float xmax)
Set up class to also fill a histogram.
Definition: StatUtils.h:89
ActsUtils::StatHist::setBinning
void setBinning(unsigned int n_bins, float xmin, float xmax)
Define histogramm bins and enable histogramming.
Definition: StatUtils.h:98
ActsUtils::operator<<
std::ostream & operator<<(std::ostream &out, const Stat &stat)
Definition: StatUtils.h:74
ActsUtils::Stat::rms2
double rms2() const
Definition: StatUtils.h:31
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
ActsUtils::Stat::reset
void reset()
Set statistics to zero.
Definition: StatUtils.h:35
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
xmax
double xmax
Definition: listroot.cxx:61
ActsUtils::StatHist::add
void add(double val)
Gather statistics and fill the histogram if not disabled.
Definition: StatUtils.h:116
ActsUtils::StatHist::reset
void reset()
Set histogram contents and statistics to zero.
Definition: StatUtils.h:126
ActsUtils::Stat::rms
double rms() const
Definition: StatUtils.h:32
ActsUtils::StatHist::m_histogram
std::vector< unsigned int > m_histogram
Definition: StatUtils.h:181
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7