The Gaudi Framework  v36r1 (3e2fb5a8)
StatEntity.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 
19  public Gaudi::Accumulators::AccumulatorSet<double, Gaudi::Accumulators::atomicity::full, double,
20  Gaudi::Accumulators::StatAccumulator,
21  Gaudi::Accumulators::BinomialAccumulator> {
22 public:
23  inline static const std::string typeString{"statentity"};
29  using AccParent::reset;
31  StatEntity() = default;
32  StatEntity( const unsigned long entries, const double flag, const double flag2, const double minFlag,
33  const double maxFlag ) {
35  std::make_tuple( std::make_tuple( std::make_tuple( entries, flag ), flag2 ), minFlag, maxFlag ),
36  std::make_tuple( 0, 0 ) ) );
37  }
38  void reset() { AccParent::reset(); }
39  void operator=( double by ) {
40  this->reset();
41  ( *this ) += by;
42  }
43  StatEntity& operator-=( double by ) {
44  ( *this ) += ( -by );
45  return *this;
46  }
48  ( *this ) += 1.0;
49  return *this;
50  }
52  auto copy = *this;
53  ++( *this );
54  return copy;
55  }
57  ( *this ) += -1.0;
58  return *this;
59  }
61  auto copy = *this;
62  --( *this );
63  return copy;
64  }
65  bool operator<( const StatEntity& se ) const {
66  return std::make_tuple( nEntries(), sum(), min(), max(), sum2() ) <
67  std::make_tuple( se.nEntries(), se.sum(), se.min(), se.max(), se.sum2() );
68  };
69  // using AccumulatorSet::operator+=;
70  StatEntity& operator+=( double by ) {
71  this->AccumulatorSet::operator+=( by );
72  return *this;
73  }
75  mergeAndReset( std::move( by ) );
76  return *this;
77  }
78  unsigned long add( const double v ) {
79  *this += v;
80  return nEntries();
81  }
82  unsigned long addFlag( const double v ) { return add( v ); }
83  // aliases (a'la ROOT)
84  double Sum() const { return sum(); } // get sum
85  double Mean() const { return mean(); } // get mean
86  double MeanErr() const { return meanErr(); } // get error in mean
87  double rms() const { return standard_deviation(); } // get rms
88  double Rms() const { return standard_deviation(); } // get rms
89  double RMS() const { return standard_deviation(); } // get rms
90  double Eff() const { return eff(); } // get efficiency
91  double Min() const { return min(); } // get minimal value
92  double Max() const { return max(); } // get maximal value
93  // some legacy methods, to be removed ...
94  double flag() const { return sum(); }
95  double flag2() const { return sum2(); }
96  double flagMean() const { return mean(); }
97  double flagRMS() const { return standard_deviation(); }
98  double flagMeanErr() const { return meanErr(); }
99  double flagMin() const { return min(); }
100  double flagMax() const { return max(); }
101  static bool effCounter( std::string_view name ) {
102  using boost::algorithm::icontains;
103  return icontains( name, "eff" ) || icontains( name, "acc" ) || icontains( name, "filt" ) ||
104  icontains( name, "fltr" ) || icontains( name, "pass" );
105  }
106  template <typename stream>
109  fmt % nEntries() % sum() % mean() % standard_deviation() % min() % max();
110  return o << fmt.str();
111  }
113  return printFormattedImpl( o, format );
114  }
117  template <typename stream>
118  stream& printImpl( stream& o, bool tableFormat, std::string_view name, bool flag, std::string_view fmtHead ) const {
119  if ( flag && effCounter( name ) && 0 <= eff() && 0 <= effErr() && sum() <= nEntries() &&
120  ( 0 == min() || 1 == min() ) && ( 0 == max() || 1 == max() ) ) {
121  // efficiency printing
122  if ( tableFormat ) {
123  if ( name.empty() ) {
124  constexpr auto fmt = "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
125  return o << boost::format{fmt} % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
126  ( efficiencyErr() * 100 );
127  } else {
128  auto fmt = std::string{" |*"}.append( fmtHead ).append(
129  "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |" );
130  return o << boost::format{fmt} % ( std::string{"\""}.append( name ).append( "\"" ) ) %
131  BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) % ( efficiencyErr() * 100 );
132  }
133  } else {
134  constexpr auto fmt = "#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|";
135  return o << boost::format{fmt} % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
136  ( efficiencyErr() * 100 );
137  }
138  } else {
139  // Standard printing
140  if ( tableFormat ) {
141  if ( name.empty() ) {
142  constexpr auto fmt = "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |";
143  return o << boost::format{fmt} % nEntries() % sum() % mean() % standard_deviation() % min() % max();
144 
145  } else {
146  auto fmt = std::string{" | "}.append( fmtHead ).append(
147  "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |" );
148  return o << boost::format{fmt} % std::string{"\""}.append( name ).append( "\"" ) % nEntries() % sum() %
149  mean() % standard_deviation() % min() % max();
150  }
151  } else {
152  constexpr auto fmt = "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|";
153  return o << boost::format{fmt} % nEntries() % sum() % mean() % standard_deviation() % min() % max();
154  }
155  }
156  }
157  std::ostream& print( std::ostream& o, bool tableFormat, std::string_view name, bool flag = true,
158  std::string_view fmtHead = "%|-48.48s|%|27t|" ) const {
159  return printImpl( o, tableFormat, name, flag, fmtHead );
160  }
161  MsgStream& print( MsgStream& o, bool tableFormat, std::string_view name, bool flag = true,
162  std::string_view fmtHead = "%|-48.48s|%|27t|" ) const {
163  return printImpl( o, tableFormat, name, flag, fmtHead );
164  }
165  virtual std::ostream& print( std::ostream& o, std::string_view tag ) const override {
166  return print( o, true, tag, true );
167  }
168  virtual MsgStream& print( MsgStream& o, std::string_view tag ) const override { return print( o, true, tag, true ); }
169  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
170  std::string emptyName;
171  return print( o, tableFormat, emptyName, true );
172  }
173  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override {
174  std::string emptyName;
175  return print( o, tableFormat, emptyName, true );
176  }
178  std::ostringstream ost;
179  print( ost );
180  return ost.str();
181  }
182  std::ostream& fillStream( std::ostream& o ) const { return print( o ); }
183  MsgStream& fillStream( MsgStream& o ) const { return print( o ); }
185  virtual nlohmann::json toJSON() const override {
186  return {{"type", typeString},
187  {"empty", this->nEntries() == 0},
188  {"nEntries", this->nEntries()},
189  {"sum", this->sum()},
190  {"mean", this->mean()},
191  {"sum2", this->sum2()},
192  {"standard_deviation", this->standard_deviation()},
193  {"min", this->min()},
194  {"max", this->max()},
195  {"nTrueEntries", this->nTrueEntries()},
196  {"nFalseEntries", this->nFalseEntries()},
197  {"efficiency", this->efficiency()},
198  {"efficiencyErr", this->efficiencyErr()}};
199  }
200  static StatEntity fromJSON( const nlohmann::json& j ) {
201  StatEntity res;
203  j, {{{{"nEntries", "sum"}, "sum2"}, "min", "max"}, {"nTrueEntries", "nFalseEntries"}} ) );
204  return res;
205  }
206 };
StatEntity::reset
void reset()
Definition: StatEntity.h:38
Gaudi::Accumulators::PrintableCounter
An empty ancester of all counters that knows how to print themselves.
Definition: Accumulators.h:827
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:520
std::make_tuple
T make_tuple(T... args)
StatEntity::operator+=
StatEntity & operator+=(double by)
Definition: StatEntity.h:70
Write.stream
stream
Definition: Write.py:31
StatEntity::MeanErr
double MeanErr() const
Definition: StatEntity.h:86
std::string
STL class.
GaudiPython.HistoUtils.nEntries
nEntries
Definition: HistoUtils.py:921
StatEntity::Sum
double Sum() const
Definition: StatEntity.h:84
StatEntity::operator=
void operator=(double by)
Definition: StatEntity.h:39
StatEntity::toJSON
virtual nlohmann::json toJSON() const override
Basic JSON export for Gaudi::Monitoring::Hub support.
Definition: StatEntity.h:185
std::move
T move(T... args)
StatEntity::Mean
double Mean() const
Definition: StatEntity.h:85
Gaudi::Accumulators::AccumulatorSet
AccumulatorSet is an Accumulator that holds a set of Accumulators templated by same Arithmetic and At...
Definition: Accumulators.h:490
StatEntity::printFormattedImpl
stream & printFormattedImpl(stream &o, const std::string &format) const
Definition: StatEntity.h:107
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:32
StatEntity::print
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: StatEntity.h:169
StatEntity::operator+=
StatEntity & operator+=(StatEntity by)
Definition: StatEntity.h:74
StatEntity::printImpl
stream & printImpl(stream &o, bool tableFormat, std::string_view name, bool flag, std::string_view fmtHead) const
Definition: StatEntity.h:118
StatEntity
backward compatible StatEntity class.
Definition: StatEntity.h:21
StatEntity::print
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: StatEntity.h:173
StatEntity::operator--
StatEntity operator--(int)
Definition: StatEntity.h:60
StatEntity::operator-=
StatEntity & operator-=(double by)
Definition: StatEntity.h:43
Gaudi::Accumulators::AccumulatorSet< double, Gaudi::Accumulators::atomicity::full, double, Gaudi::Accumulators::StatAccumulator, Gaudi::Accumulators::BinomialAccumulator >::reset
void reset()
Definition: Accumulators.h:506
StatEntity::printFormatted
MsgStream & printFormatted(MsgStream &o, const std::string &format) const
Definition: StatEntity.h:115
StatEntity::flag2
double flag2() const
Definition: StatEntity.h:95
max
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:225
StatEntity::fromJSON
static StatEntity fromJSON(const nlohmann::json &j)
Definition: StatEntity.h:200
jsonFromLHCbLog.json
dictionary json
Definition: jsonFromLHCbLog.py:86
StatEntity::operator--
StatEntity & operator--()
Definition: StatEntity.h:56
StatEntity::flagRMS
double flagRMS() const
Definition: StatEntity.h:97
StatEntity::flag
double flag() const
Definition: StatEntity.h:94
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:508
StatEntity::flagMax
double flagMax() const
Definition: StatEntity.h:100
StatEntity::flagMeanErr
double flagMeanErr() const
Definition: StatEntity.h:98
Gaudi::Accumulators::BinomialAccumulator
BinomialAccumulator.
Definition: Accumulators.h:664
StatEntity::flagMean
double flagMean() const
Definition: StatEntity.h:96
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatEntity::RMS
double RMS() const
Definition: StatEntity.h:89
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:161
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:157
StatEntity::Min
double Min() const
Definition: StatEntity.h:91
StatEntity::printFormatted
std::ostream & printFormatted(std::ostream &o, const std::string &format) const
Definition: StatEntity.h:112
std::ostream
STL class.
Gaudi::Accumulators::StatAccumulator
AccumulatorSet< Arithmetic, Atomicity, Arithmetic, SigmaAccumulator, MinAccumulator, MaxAccumulator > StatAccumulator
StatAccumulator.
Definition: Accumulators.h:797
StatEntity::Max
double Max() const
Definition: StatEntity.h:92
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:165
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:23
min
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:212
GaudiPython.HistoUtils.mean
mean
Definition: HistoUtils.py:886
MsgStream
Definition: MsgStream.h:34
StatEntity::toString
std::string toString() const
Definition: StatEntity.h:177
std::string::append
T append(T... args)
HistoDumpEx.v
v
Definition: HistoDumpEx.py:27
StatEntity::addFlag
unsigned long addFlag(const double v)
Definition: StatEntity.h:82
std::ostringstream
STL class.
StatEntity::fillStream
std::ostream & fillStream(std::ostream &o) const
Definition: StatEntity.h:182
StatEntity::Rms
double Rms() const
Definition: StatEntity.h:88
Accumulators.h
StatEntity::add
unsigned long add(const double v)
Definition: StatEntity.h:78
fmt
StatEntity::print
virtual MsgStream & print(MsgStream &o, std::string_view tag) const override
Definition: StatEntity.h:168
StatEntity::operator<
bool operator<(const StatEntity &se) const
Definition: StatEntity.h:65
StatEntity::flagMin
double flagMin() const
Definition: StatEntity.h:99
StatEntity::operator++
StatEntity & operator++()
Definition: StatEntity.h:47
std::ostringstream::str
T str(T... args)
StatEntity::effCounter
static bool effCounter(std::string_view name)
Definition: StatEntity.h:101
Gaudi::Accumulators::BinomialAccumulator::nEntries
unsigned long nEntries() const
Definition: Accumulators.h:666
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
StatEntity::rms
double rms() const
Definition: StatEntity.h:87
GaudiPython.HistoUtils.meanErr
meanErr
Definition: HistoUtils.py:888
StatEntity::fillStream
MsgStream & fillStream(MsgStream &o) const
Definition: StatEntity.h:183
StatEntity::operator++
StatEntity operator++(int)
Definition: StatEntity.h:51
StatEntity::Eff
double Eff() const
Definition: StatEntity.h:90