Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #include <algorithm>
6 #ifndef ACTSUTILS_STAT_H
7 #define ACTSUTILS_STAT_H 1
8 #include <cmath>
9 #include <iomanip>
10 #include <ostream>
11 #include <string>
12 #include <sstream>
13 #include <vector>
14 
15 namespace ActsUtils {
17 class Stat {
18 public:
21  void add(double val) {
22  ++m_n;
23  m_sum += val;
24  m_sum2 += val*val;
27  }
28  unsigned int n() const { return m_n; }
29  double min() const { return m_min; }
30  double max() const { return m_max; }
31  double mean() const { return m_n>0 ? m_sum/m_n : 0.; }
32  double rms2() const { return m_n>1 ? (m_sum2 - m_sum *m_sum/m_n)/(m_n-1) : 0.; }
33  double rms() const { return std::sqrt( rms2() ); }
34 
36  void reset() {
37  m_n=0;
38  m_sum=0.;
39  m_sum2=0.;
42  }
43 
45  Stat &operator +=(const Stat &b) {
46  m_n += b.m_n;
47  m_sum += b.m_sum;
48  m_sum2 += b.m_sum2;
49  m_min = std::min(m_min, b.m_min);
50  m_max = std::max(m_max, b.m_max);
51  return *this;
52  }
53 
54  unsigned int m_n=0;
55  double m_sum=0.;
56  double m_sum2=0.;
59 };
60 
62 template <class T_Stream>
63 inline void dumpStat(T_Stream &out, const Stat &stat) {
64  if (stat.n() > 1) {
65  out << std::setw(14) << stat.min() << " < "
66  << std::setw(14) << stat.mean() << " +- " << std::setw(14) << stat.rms() << " < "
67  << std::setw(14) << stat.max()
68  << " / " << std::setw(9) << stat.n();
69  }
70  else {
71  out << std::setw(14*4+9+3*3+4) << stat.mean();
72  }
73 }
74 
75 inline std::ostream &operator<<(std::ostream &out, const Stat &stat) {
76  dumpStat(out, stat);
77  return out;
78 }
79 
81 class StatHist : public Stat {
82 public:
84  StatHist() = default;
85 
90  StatHist(unsigned int n_bins, float xmin, float xmax)
91  {
92  setBinning(n_bins,xmin,xmax);
93  }
94 
99  void setBinning(unsigned int n_bins, float xmin, float xmax)
100  {
101  m_xmin=xmin;
102  m_scale = ( n_bins / (xmax-xmin) );
103  m_xmin -= 1./m_scale;
104  m_histogram.resize(n_bins+2,0u);
105  }
106 
109  StatHist tmp;
110  tmp.m_histogram.resize( m_histogram.size());
111  tmp.m_xmin = m_xmin;
112  tmp.m_scale = m_scale;
113  return tmp;
114  }
115 
117  void add(double val) {
118  Stat::add(val);
119  if (!m_histogram.empty()) {
120  unsigned int bin = std::min( static_cast<unsigned int>(m_histogram.size()-1),
121  static_cast<unsigned int>( std::max(0.,(val - m_xmin)*m_scale)) );
122  ++m_histogram.at(bin) ;
123  }
124  }
125 
127  void reset() {
128  Stat::reset();
129  for (unsigned int &bin : m_histogram) {
130  bin = 0u;
131  }
132  }
133 
137  if (m_histogram.size() == b.m_histogram.size()) {
138  for (unsigned int bin_i=0; bin_i< m_histogram.size(); ++bin_i) {
139  m_histogram[bin_i] += b.m_histogram[bin_i];
140  }
141  }
142  return *this;
143  }
144 
147  double lowerEdge(unsigned int i) const {
148  return m_xmin + i/m_scale;
149  }
150 
153  std::string histogramToString() const {
154  std::stringstream msg;
155  if (m_histogram.size()>2) {
156  const unsigned int max_val = *std::max_element(m_histogram.begin(), m_histogram.end());
157  const double bin_width = 1. / m_scale;
158  const unsigned int w = max_val > 0 ? static_cast<unsigned int>(log(1.*max_val) / log(10.))+1 : 1;
159  const unsigned int wtitle = std::max(10u, w);
160  msg << (m_xmin+bin_width) << " .. " << ((m_histogram.size()-2)/m_scale + m_xmin+bin_width) << " : "
161  << std::setw(wtitle) << "lower edge" << " |";
162  for (unsigned int i=1; i<m_histogram.size()-1; ++i) {
163  msg << " " << std::setw(w) << lowerEdge(i);
164  }
165  msg << " | " << std::endl;
166  msg << (m_xmin+bin_width) << " .. " << ((m_histogram.size()-2)/m_scale + m_xmin+bin_width) << " : "
167 
168  << std::setw(wtitle) << m_histogram[0] << " |";
169  for (unsigned int i=1; i<m_histogram.size()-1; ++i) {
170  msg << " " << std::setw(w) << m_histogram[i];
171  }
172  msg << " | " << std::setw(w) << m_histogram.back();
173  }
174  return msg.str();
175  }
176 
177  double m_xmin{};
178  double m_scale{1.0};
179  std::vector<unsigned int> m_histogram;
180 };
181 }
182 #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:81
ActsUtils::Stat::operator+=
Stat & operator+=(const Stat &b)
@breif Add the statistics gathered in the Stat object b
Definition: StatUtils.h:45
ActsUtils::Stat::m_max
double m_max
Definition: StatUtils.h:58
ActsUtils::StatHist::lowerEdge
double lowerEdge(unsigned int i) const
Get the lower edge of the given bin.
Definition: StatUtils.h:147
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
ActsUtils::Stat
Simple class to gather statistics : min, max, mean, rms.
Definition: StatUtils.h:17
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:21
ActsUtils::dumpStat
void dumpStat(T_Stream &out, const Stat &stat)
Dump the given statistics object to the given output stream.
Definition: StatUtils.h:63
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:28
ActsUtils::StatHist::histogramToString
std::string histogramToString() const
Create a string showing the contents of the histogram The string.
Definition: StatUtils.h:153
ActsUtils::Stat::m_sum
double m_sum
Definition: StatUtils.h:55
ActsUtils::Stat::m_n
unsigned int m_n
Definition: StatUtils.h:54
ActsUtils
Definition: StatUtils.h:15
ActsUtils::StatHist::m_scale
double m_scale
Definition: StatUtils.h:178
ActsUtils::Stat::min
double min() const
Definition: StatUtils.h:29
ActsUtils::StatHist::operator+=
StatHist & operator+=(const StatHist &b)
Add the statistucs and histogrammed data fro the given object.
Definition: StatUtils.h:135
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
ActsUtils::Stat::max
double max() const
Definition: StatUtils.h:30
beamspotman.stat
stat
Definition: beamspotman.py:266
ActsUtils::StatHist::createEmptyClone
StatHist createEmptyClone()
Definition: StatUtils.h:108
ActsUtils::StatHist::m_xmin
double m_xmin
Definition: StatUtils.h:177
ActsUtils::Stat::mean
double mean() const
Definition: StatUtils.h:31
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
ActsUtils::Stat::m_sum2
double m_sum2
Definition: StatUtils.h:56
ActsUtils::Stat::m_min
double m_min
Definition: StatUtils.h:57
ActsUtils::StatHist::StatHist
StatHist(unsigned int n_bins, float xmin, float xmax)
Set up class to also fill a histogram.
Definition: StatUtils.h:90
ActsUtils::StatHist::setBinning
void setBinning(unsigned int n_bins, float xmin, float xmax)
Define histogramm bins and enable histogramming.
Definition: StatUtils.h:99
ActsUtils::operator<<
std::ostream & operator<<(std::ostream &out, const Stat &stat)
Definition: StatUtils.h:75
ActsUtils::Stat::rms2
double rms2() const
Definition: StatUtils.h:32
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
ActsUtils::Stat::reset
void reset()
Set statistics to zero.
Definition: StatUtils.h:36
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:117
ActsUtils::StatHist::reset
void reset()
Set histogram contents and statistics to zero.
Definition: StatUtils.h:127
ActsUtils::Stat::rms
double rms() const
Definition: StatUtils.h:33
ActsUtils::StatHist::m_histogram
std::vector< unsigned int > m_histogram
Definition: StatUtils.h:179
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7