Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #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  void reset() { AccParent::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( std::move( 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  ( 0 == min() || 1 == min() ) && ( 0 == max() || 1 == max() ) ) {
123  // efficiency printing
124  if ( tableFormat ) {
125  if ( name.empty() ) {
126  constexpr auto fmt = "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
127  return o << boost::format{ fmt } % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
128  ( efficiencyErr() * 100 );
129  } else {
130  auto fmt = std::string{ " |*" }.append( fmtHead ).append(
131  "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |" );
132  return o << boost::format{ fmt } % ( std::string{ "\"" }.append( name ).append( "\"" ) ) %
133  BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) % ( efficiencyErr() * 100 );
134  }
135  } else {
136  constexpr auto fmt = "#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|";
137  return o << boost::format{ fmt } % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
138  ( efficiencyErr() * 100 );
139  }
140  } else {
141  // Standard printing
142  if ( tableFormat ) {
143  if ( name.empty() ) {
144  constexpr auto fmt = "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |";
145  return o << boost::format{ fmt } % nEntries() % sum() % mean() % standard_deviation() % min() % max();
146 
147  } else {
148  auto fmt = std::string{ " | " }.append( fmtHead ).append(
149  "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |" );
150  return o << boost::format{ fmt } % std::string{ "\"" }.append( name ).append( "\"" ) % nEntries() % sum() %
151  mean() % standard_deviation() % min() % max();
152  }
153  } else {
154  constexpr auto fmt = "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|";
155  return o << boost::format{ fmt } % nEntries() % sum() % mean() % standard_deviation() % min() % max();
156  }
157  }
158  }
159  std::ostream& print( std::ostream& o, bool tableFormat, std::string_view name, bool flag = true,
160  std::string_view fmtHead = "%|-48.48s|%|27t|" ) const {
161  return printImpl( o, tableFormat, name, flag, fmtHead );
162  }
163  MsgStream& print( MsgStream& o, bool tableFormat, std::string_view name, bool flag = true,
164  std::string_view fmtHead = "%|-48.48s|%|27t|" ) const {
165  return printImpl( o, tableFormat, name, flag, fmtHead );
166  }
167  virtual std::ostream& print( std::ostream& o, std::string_view tag ) const override {
168  return print( o, true, tag, true );
169  }
170  virtual MsgStream& print( MsgStream& o, std::string_view tag ) const override { return print( o, true, tag, true ); }
171  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
172  std::string emptyName;
173  return print( o, tableFormat, emptyName, true );
174  }
175  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override {
176  std::string emptyName;
177  return print( o, tableFormat, emptyName, true );
178  }
180  std::ostringstream ost;
181  print( ost );
182  return ost.str();
183  }
184  std::ostream& fillStream( std::ostream& o ) const { return print( o ); }
185  MsgStream& fillStream( MsgStream& o ) const { return print( o ); }
187  virtual nlohmann::json toJSON() const override {
188  return { { "type", typeString },
189  { "empty", this->nEntries() == 0 },
190  { "nEntries", this->nEntries() },
191  { "sum", this->sum() },
192  { "mean", this->mean() },
193  { "sum2", this->sum2() },
194  { "standard_deviation", this->standard_deviation() },
195  { "min", this->min() },
196  { "max", this->max() },
197  { "nTrueEntries", this->nTrueEntries() },
198  { "nFalseEntries", this->nFalseEntries() },
199  { "efficiency", this->efficiency() },
200  { "efficiencyErr", this->efficiencyErr() } };
201  }
202  static StatEntity fromJSON( const nlohmann::json& j ) {
203  StatEntity res;
205  j, { { { { "nEntries", "sum" }, "sum2" }, "min", "max" }, { "nTrueEntries", "nFalseEntries" } } ) );
206  return res;
207  }
208 };
StatEntity::reset
void reset()
Definition: StatEntity.h:40
Gaudi::Accumulators::PrintableCounter
An empty ancester of all counters that knows how to print themselves.
Definition: Accumulators.h:833
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:525
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.
GaudiPython.HistoUtils.nEntries
nEntries
Definition: HistoUtils.py:939
StatEntity::Sum
double Sum() const
Definition: StatEntity.h:86
StatEntity::operator=
void operator=(double by)
Definition: StatEntity.h:41
StatEntity::toJSON
virtual nlohmann::json toJSON() const override
Basic JSON export for Gaudi::Monitoring::Hub support.
Definition: StatEntity.h:187
std::move
T move(T... args)
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:494
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
StatEntity::print
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: StatEntity.h:171
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:175
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:87
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:510
StatEntity::printFormatted
MsgStream & printFormatted(MsgStream &o, const std::string &format) const
Definition: StatEntity.h:117
StatEntity::flag2
double flag2() const
Definition: StatEntity.h:97
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:202
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
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:512
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:669
StatEntity::flagMean
double flagMean() const
Definition: StatEntity.h:98
TimingHistograms.name
name
Definition: TimingHistograms.py:25
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:163
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:159
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:101
std::ostream
STL class.
Gaudi::Accumulators::StatAccumulator
AccumulatorSet< Arithmetic, Atomicity, Arithmetic, SigmaAccumulator, MinAccumulator, MaxAccumulator > StatAccumulator
StatAccumulator.
Definition: Accumulators.h:803
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:167
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
min
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:212
GaudiPython.HistoUtils.mean
mean
Definition: HistoUtils.py:904
MsgStream
Definition: MsgStream.h:34
StatEntity::toString
std::string toString() const
Definition: StatEntity.h:179
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:84
std::ostringstream
STL class.
StatEntity::fillStream
std::ostream & fillStream(std::ostream &o) const
Definition: StatEntity.h:184
StatEntity::Rms
double Rms() const
Definition: StatEntity.h:90
Accumulators.h
StatEntity::add
unsigned long add(const double v)
Definition: StatEntity.h:80
fmt
Definition: MessageSvcSink.cpp:25
StatEntity::print
virtual MsgStream & print(MsgStream &o, std::string_view tag) const override
Definition: StatEntity.h:170
StatEntity::operator<
bool operator<(const StatEntity &se) const
Definition: StatEntity.h:67
StatEntity::flagMin
double flagMin() const
Definition: StatEntity.h:101
StatEntity::operator++
StatEntity & operator++()
Definition: StatEntity.h:49
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:671
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:89
GaudiPython.HistoUtils.meanErr
meanErr
Definition: HistoUtils.py:906
StatEntity::fillStream
MsgStream & fillStream(MsgStream &o) const
Definition: StatEntity.h:185
StatEntity::operator++
StatEntity operator++(int)
Definition: StatEntity.h:53
StatEntity::Eff
double Eff() const
Definition: StatEntity.h:92