The Gaudi Framework  master (37c0b60a)
StatEntity.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 #include <Gaudi/Accumulators.h>
13 #include <boost/algorithm/string/predicate.hpp>
14 #include <boost/format.hpp>
15 
21  public Gaudi::Accumulators::AccumulatorSet<double, Gaudi::Accumulators::atomicity::full, double,
22  Gaudi::Accumulators::StatAccumulator,
23  Gaudi::Accumulators::BinomialAccumulator> {
24 public:
25  inline static const std::string typeString{ "statentity" };
31  using AccParent::reset;
33  StatEntity() = default;
34  StatEntity( const unsigned long entries, const double flag, const double flag2, const double minFlag,
35  const double maxFlag ) {
37  std::make_tuple( std::make_tuple( std::make_tuple( entries, flag ), flag2 ), minFlag, maxFlag ),
38  std::make_tuple( 0, 0 ) ) );
39  }
40  friend void reset( StatEntity& s ) { s.reset(); }
41  void operator=( double by ) {
42  this->reset();
43  ( *this ) += by;
44  }
45  StatEntity& operator-=( double by ) {
46  ( *this ) += ( -by );
47  return *this;
48  }
50  ( *this ) += 1.0;
51  return *this;
52  }
54  auto copy = *this;
55  ++( *this );
56  return copy;
57  }
59  ( *this ) += -1.0;
60  return *this;
61  }
63  auto copy = *this;
64  --( *this );
65  return copy;
66  }
67  bool operator<( const StatEntity& se ) const {
68  return std::make_tuple( nEntries(), sum(), min(), max(), sum2() ) <
69  std::make_tuple( se.nEntries(), se.sum(), se.min(), se.max(), se.sum2() );
70  }
71  // using AccumulatorSet::operator+=;
72  StatEntity& operator+=( double by ) {
73  this->AccumulatorSet::operator+=( by );
74  return *this;
75  }
77  mergeAndReset( by );
78  return *this;
79  }
80  unsigned long add( const double v ) {
81  *this += v;
82  return nEntries();
83  }
84  unsigned long addFlag( const double v ) { return add( v ); }
85  // aliases (a'la ROOT)
86  double Sum() const { return sum(); } // get sum
87  double Mean() const { return mean(); } // get mean
88  double MeanErr() const { return meanErr(); } // get error in mean
89  double rms() const { return standard_deviation(); } // get rms
90  double Rms() const { return standard_deviation(); } // get rms
91  double RMS() const { return standard_deviation(); } // get rms
92  double Eff() const { return eff(); } // get efficiency
93  double Min() const { return min(); } // get minimal value
94  double Max() const { return max(); } // get maximal value
95  // some legacy methods, to be removed ...
96  double flag() const { return sum(); }
97  double flag2() const { return sum2(); }
98  double flagMean() const { return mean(); }
99  double flagRMS() const { return standard_deviation(); }
100  double flagMeanErr() const { return meanErr(); }
101  double flagMin() const { return min(); }
102  double flagMax() const { return max(); }
103  static bool effCounter( std::string_view name ) {
104  using boost::algorithm::icontains;
105  return icontains( name, "eff" ) || icontains( name, "acc" ) || icontains( name, "filt" ) ||
106  icontains( name, "fltr" ) || icontains( name, "pass" );
107  }
108  template <typename stream>
111  fmt % nEntries() % sum() % mean() % standard_deviation() % min() % max();
112  return o << fmt.str();
113  }
115  return printFormattedImpl( o, format );
116  }
119  template <typename stream>
120  stream& printImpl( stream& o, bool tableFormat, std::string_view name, bool flag, std::string_view fmtHead ) const {
121  if ( flag && effCounter( name ) && 0 <= eff() && 0 <= effErr() && sum() <= nEntries() &&
122  ( essentiallyEqual( min(), 0 ) || essentiallyEqual( min(), 1 ) ) &&
123  ( essentiallyEqual( max(), 0 ) || essentiallyEqual( max(), 1 ) ) ) {
124  // efficiency printing
125  if ( tableFormat ) {
126  if ( name.empty() ) {
127  constexpr auto fmt = "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
128  return o << boost::format{ fmt } % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
129  ( efficiencyErr() * 100 );
130  } else {
131  auto fmt = std::string{ " |*" }.append( fmtHead ).append(
132  "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |" );
133  return o << boost::format{ fmt } % ( std::string{ "\"" }.append( name ).append( "\"" ) ) %
134  BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) % ( efficiencyErr() * 100 );
135  }
136  } else {
137  constexpr auto fmt = "#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|";
138  return o << boost::format{ fmt } % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
139  ( efficiencyErr() * 100 );
140  }
141  } else {
142  // Standard printing
143  if ( tableFormat ) {
144  if ( name.empty() ) {
145  constexpr auto fmt = "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |";
146  return o << boost::format{ fmt } % nEntries() % sum() % mean() % standard_deviation() % min() % max();
147 
148  } else {
149  auto fmt = std::string{ " | " }.append( fmtHead ).append(
150  "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |" );
151  return o << boost::format{ fmt } % std::string{ "\"" }.append( name ).append( "\"" ) % nEntries() % sum() %
152  mean() % standard_deviation() % min() % max();
153  }
154  } else {
155  constexpr auto fmt = "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|";
156  return o << boost::format{ fmt } % nEntries() % sum() % mean() % standard_deviation() % min() % max();
157  }
158  }
159  }
160  std::ostream& print( std::ostream& o, bool tableFormat, std::string_view name, bool flag = true,
161  std::string_view fmtHead = "%|-48.48s|%|27t|" ) const {
162  return printImpl( o, tableFormat, name, flag, fmtHead );
163  }
164  MsgStream& print( MsgStream& o, bool tableFormat, std::string_view name, bool flag = true,
165  std::string_view fmtHead = "%|-48.48s|%|27t|" ) const {
166  return printImpl( o, tableFormat, name, flag, fmtHead );
167  }
168  virtual std::ostream& print( std::ostream& o, std::string_view tag ) const override {
169  return print( o, true, tag, true );
170  }
171  virtual MsgStream& print( MsgStream& o, std::string_view tag ) const override { return print( o, true, tag, true ); }
172  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
173  std::string emptyName;
174  return print( o, tableFormat, emptyName, true );
175  }
176  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override {
177  std::string emptyName;
178  return print( o, tableFormat, emptyName, true );
179  }
181  std::ostringstream ost;
182  print( ost );
183  return ost.str();
184  }
185  std::ostream& fillStream( std::ostream& o ) const { return print( o ); }
186  MsgStream& fillStream( MsgStream& o ) const { return print( o ); }
188  friend void to_json( nlohmann::json& j, StatEntity const& s );
189  static StatEntity fromJSON( const nlohmann::json& j ) {
190  StatEntity res;
192  j, { { { { "nEntries", "sum" }, "sum2" }, "min", "max" }, { "nTrueEntries", "nFalseEntries" } } ) );
193  return res;
194  }
195 
196 private:
197  static constexpr bool essentiallyEqual( double const a, double const b ) {
198  return std::abs( a - b ) <= std::max( std::abs( a ), std::abs( b ) ) * std::numeric_limits<double>::epsilon();
199  }
200 };
Gaudi::Accumulators::PrintableCounter
An empty ancester of all counters that knows how to print themselves.
Definition: Accumulators.h:872
Gaudi::Accumulators::AccumulatorSet< double, Gaudi::Accumulators::atomicity::full, double, Gaudi::Accumulators::StatAccumulator, Gaudi::Accumulators::BinomialAccumulator >::extractJSONData
static InternalType extractJSONData(const nlohmann::json &j, const JSONStringEntriesType &entries)
Definition: Accumulators.h:562
std::make_tuple
T make_tuple(T... args)
StatEntity::operator+=
StatEntity & operator+=(double by)
Definition: StatEntity.h:72
Write.stream
stream
Definition: Write.py:32
StatEntity::MeanErr
double MeanErr() const
Definition: StatEntity.h:88
std::string
STL class.
StatEntity::Sum
double Sum() const
Definition: StatEntity.h:86
StatEntity::operator=
void operator=(double by)
Definition: StatEntity.h:41
Gaudi::Accumulators::AccumulatorSet< double, Gaudi::Accumulators::atomicity::full, double, Gaudi::Accumulators::StatAccumulator, Gaudi::Accumulators::BinomialAccumulator >::mergeAndReset
void mergeAndReset(AccumulatorSet< double, Ato, InputType, Gaudi::Accumulators::StatAccumulator ... > &other)
Definition: Accumulators.h:549
StatEntity::Mean
double Mean() const
Definition: StatEntity.h:87
Gaudi::Accumulators::AccumulatorSet
AccumulatorSet is an Accumulator that holds a set of Accumulators templated by same Arithmetic and At...
Definition: Accumulators.h:531
StatEntity::printFormattedImpl
stream & printFormattedImpl(stream &o, const std::string &format) const
Definition: StatEntity.h:109
Gaudi::Accumulators::atomicity::full
@ full
StatEntity::StatEntity
StatEntity(const unsigned long entries, const double flag, const double flag2, const double minFlag, const double maxFlag)
Definition: StatEntity.h:34
gaudirun.s
string s
Definition: gaudirun.py:346
StatEntity::print
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: StatEntity.h:172
StatEntity::operator+=
StatEntity & operator+=(StatEntity by)
Definition: StatEntity.h:76
StatEntity::printImpl
stream & printImpl(stream &o, bool tableFormat, std::string_view name, bool flag, std::string_view fmtHead) const
Definition: StatEntity.h:120
StatEntity
backward compatible StatEntity class.
Definition: StatEntity.h:23
StatEntity::print
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: StatEntity.h:176
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:86
StatEntity::operator--
StatEntity operator--(int)
Definition: StatEntity.h:62
StatEntity::operator-=
StatEntity & operator-=(double by)
Definition: StatEntity.h:45
Gaudi::Accumulators::AccumulatorSet< double, Gaudi::Accumulators::atomicity::full, double, Gaudi::Accumulators::StatAccumulator, Gaudi::Accumulators::BinomialAccumulator >::reset
void reset()
Definition: Accumulators.h:547
StatEntity::printFormatted
MsgStream & printFormatted(MsgStream &o, const std::string &format) const
Definition: StatEntity.h:117
StatEntity::flag2
double flag2() const
Definition: StatEntity.h:97
std::abs
Gaudi::ParticleID abs(const Gaudi::ParticleID &p)
Return the absolute value for a PID.
Definition: ParticleID.h:191
StatEntity::fromJSON
static StatEntity fromJSON(const nlohmann::json &j)
Definition: StatEntity.h:189
StatEntity::operator--
StatEntity & operator--()
Definition: StatEntity.h:58
StatEntity::flagRMS
double flagRMS() const
Definition: StatEntity.h:99
StatEntity::flag
double flag() const
Definition: StatEntity.h:96
StatEntity::essentiallyEqual
static constexpr bool essentiallyEqual(double const a, double const b)
Definition: StatEntity.h:197
StatEntity::flagMax
double flagMax() const
Definition: StatEntity.h:102
StatEntity::flagMeanErr
double flagMeanErr() const
Definition: StatEntity.h:100
Gaudi::Accumulators::BinomialAccumulator
BinomialAccumulator.
Definition: Accumulators.h:706
StatEntity::flagMean
double flagMean() const
Definition: StatEntity.h:98
StatEntity::RMS
double RMS() const
Definition: StatEntity.h:91
StatEntity::print
MsgStream & print(MsgStream &o, bool tableFormat, std::string_view name, bool flag=true, std::string_view fmtHead="%|-48.48s|%|27t|") const
Definition: StatEntity.h:164
StatEntity::print
std::ostream & print(std::ostream &o, bool tableFormat, std::string_view name, bool flag=true, std::string_view fmtHead="%|-48.48s|%|27t|") const
Definition: StatEntity.h:160
StatEntity::Min
double Min() const
Definition: StatEntity.h:93
StatEntity::printFormatted
std::ostream & printFormatted(std::ostream &o, const std::string &format) const
Definition: StatEntity.h:114
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::ostream
STL class.
StatEntity::to_json
friend void to_json(nlohmann::json &j, StatEntity const &s)
Basic JSON export for Gaudi::Monitoring::Hub support.
Definition: StatEntity.cpp:14
Gaudi::Accumulators::StatAccumulator
AccumulatorSet< Arithmetic, Atomicity, Arithmetic, SigmaAccumulator, MinAccumulator, MaxAccumulator > StatAccumulator
StatAccumulator.
Definition: Accumulators.h:840
StatEntity::Max
double Max() const
Definition: StatEntity.h:94
StatEntity::print
virtual std::ostream & print(std::ostream &o, std::string_view tag) const override
prints the counter to a stream in table format, with the given tag
Definition: StatEntity.h:168
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
StatEntity::typeString
static const std::string typeString
Definition: StatEntity.h:25
MsgStream
Definition: MsgStream.h:33
StatEntity::toString
std::string toString() const
Definition: StatEntity.h:180
std::numeric_limits::epsilon
T epsilon(T... args)
std::string::append
T append(T... args)
StatEntity::addFlag
unsigned long addFlag(const double v)
Definition: StatEntity.h:84
std::ostringstream
STL class.
StatEntity::fillStream
std::ostream & fillStream(std::ostream &o) const
Definition: StatEntity.h:185
StatEntity::Rms
double Rms() const
Definition: StatEntity.h:90
Accumulators.h
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
StatEntity::add
unsigned long add(const double v)
Definition: StatEntity.h:80
fmt
StatEntity::print
virtual MsgStream & print(MsgStream &o, std::string_view tag) const override
Definition: StatEntity.h:171
StatEntity::operator<
bool operator<(const StatEntity &se) const
Definition: StatEntity.h:67
StatEntity::flagMin
double flagMin() const
Definition: StatEntity.h:101
StatEntity::reset
friend void reset(StatEntity &s)
Definition: StatEntity.h:40
StatEntity::operator++
StatEntity & operator++()
Definition: StatEntity.h:49
Properties.v
v
Definition: Properties.py:122
std::ostringstream::str
T str(T... args)
StatEntity::effCounter
static bool effCounter(std::string_view name)
Definition: StatEntity.h:103
Gaudi::Accumulators::BinomialAccumulator::nEntries
unsigned long nEntries() const
Definition: Accumulators.h:708
Gaudi::Accumulators::PrintableCounter::print
virtual std::ostream & print(std::ostream &, bool tableFormat=false) const =0
prints the counter to a stream
StatEntity::StatEntity
StatEntity()=default
the constructor with automatic registration in the owner's counter map
std::max
T max(T... args)
StatEntity::rms
double rms() const
Definition: StatEntity.h:89
StatEntity::fillStream
MsgStream & fillStream(MsgStream &o) const
Definition: StatEntity.h:186
StatEntity::operator++
StatEntity operator++(int)
Definition: StatEntity.h:53
StatEntity::Eff
double Eff() const
Definition: StatEntity.h:92