The Gaudi Framework  v33r0 (d5ea422b)
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 
18 class StatEntity : public Gaudi::Accumulators::PrintableCounter,
19  public Gaudi::Accumulators::AccumulatorSet<double, Gaudi::Accumulators::atomicity::full,
20  Gaudi::Accumulators::StatAccumulator,
21  Gaudi::Accumulators::BinomialAccumulator> {
22 public:
23  using AccParent = Gaudi::Accumulators::AccumulatorSet<double, Gaudi::Accumulators::atomicity::full,
25  Gaudi::Accumulators::BinomialAccumulator>;
26  using BinomialAccParent = Gaudi::Accumulators::BinomialAccumulator<double, Gaudi::Accumulators::atomicity::full>;
28  using AccParent::reset;
30  StatEntity() = default;
31  template <class OWNER>
32  StatEntity( OWNER* o, const std::string& tag ) {
33  o->declareCounter( tag, *this );
34  }
35  StatEntity( const unsigned long entries, const double flag, const double flag2, const double minFlag,
36  const double maxFlag ) {
38  std::make_tuple( std::make_tuple( std::make_tuple( entries, flag ), flag2 ), minFlag, maxFlag ),
39  std::make_tuple( 0, 0 ) ) );
40  }
41  void reset() { AccParent::reset(); }
42  void operator=( double by ) {
43  this->reset();
44  ( *this ) += by;
45  }
46  StatEntity& operator-=( double by ) {
47  ( *this ) += ( -by );
48  return *this;
49  }
51  ( *this ) += 1.0;
52  return *this;
53  }
55  auto copy = *this;
56  ++( *this );
57  return copy;
58  }
60  ( *this ) += -1.0;
61  return *this;
62  }
64  auto copy = *this;
65  --( *this );
66  return copy;
67  }
68  bool operator<( const StatEntity& se ) const {
69  return std::make_tuple( nEntries(), sum(), min(), max(), sum2() ) <
70  std::make_tuple( se.nEntries(), se.sum(), se.min(), se.max(), se.sum2() );
71  };
72  // using AccumulatorSet::operator+=;
73  StatEntity& operator+=( double by ) {
74  this->AccumulatorSet::operator+=( by );
75  return *this;
76  }
78  mergeAndReset( std::move( by ) );
79  return *this;
80  }
81  unsigned long add( const double v ) {
82  *this += v;
83  return nEntries();
84  }
85  unsigned long addFlag( const double v ) { return add( v ); }
86  // aliases (a'la ROOT)
87  double Sum() const { return sum(); } // get sum
88  double Mean() const { return mean(); } // get mean
89  double MeanErr() const { return meanErr(); } // get error in mean
90  double rms() const { return standard_deviation(); } // get rms
91  double Rms() const { return standard_deviation(); } // get rms
92  double RMS() const { return standard_deviation(); } // get rms
93  double Eff() const { return eff(); } // get efficiency
94  double Min() const { return min(); } // get minimal value
95  double Max() const { return max(); } // get maximal value
96  // some legacy methods, to be removed ...
97  double flag() const { return sum(); }
98  double flag2() const { return sum2(); }
99  double flagMean() const { return mean(); }
100  double flagRMS() const { return standard_deviation(); }
101  double flagMeanErr() const { return meanErr(); }
102  double flagMin() const { return min(); }
103  double flagMax() const { return max(); }
104  static bool effCounter( const std::string& name ) {
105  using boost::algorithm::icontains;
106  return icontains( name, "eff" ) || icontains( name, "acc" ) || icontains( name, "filt" ) ||
107  icontains( name, "fltr" ) || icontains( name, "pass" );
108  }
109  template <typename stream>
110  stream& printFormattedImpl( stream& o, const std::string& format ) const {
111  boost::format fmt{format};
112  fmt % nEntries() % sum() % mean() % standard_deviation() % min() % max();
113  return o << fmt.str();
114  }
116  return printFormattedImpl( o, format );
117  }
119  using Gaudi::Accumulators::PrintableCounter::print;
120  template <typename stream>
121  stream& printImpl( stream& o, bool tableFormat, const std::string& name, bool flag,
122  std::string const& fmtHead ) const {
123  if ( flag && effCounter( name ) && 0 <= eff() && 0 <= effErr() && sum() <= nEntries() &&
124  ( 0 == min() || 1 == min() ) && ( 0 == max() || 1 == max() ) ) {
125  // efficiency printing
126  if ( tableFormat ) {
127  if ( name.empty() ) {
128  constexpr auto fmt = "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
129  return o << boost::format{fmt} % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
130  ( efficiencyErr() * 100 );
131  } else {
132  auto fmt = " |*" + fmtHead + "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
133  return o << boost::format{fmt} % ( "\"" + name + "\"" ) % BinomialAccParent::nEntries() % sum() %
134  ( 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 = " | " + fmtHead + "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |";
150  return o << boost::format{fmt} % ( "\"" + name + "\"" ) % nEntries() % sum() % mean() % standard_deviation() %
151  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, const std::string& name, bool flag = true,
160  std::string fmtHead = "%|-48.48s|%|27t|" ) const {
161  return printImpl( o, tableFormat, name, flag, fmtHead );
162  }
163  MsgStream& print( MsgStream& o, bool tableFormat, const std::string& name, bool flag = true,
164  std::string fmtHead = "%|-48.48s|%|27t|" ) const {
165  return printImpl( o, tableFormat, name, flag, fmtHead );
166  }
167  virtual std::ostream& print( std::ostream& o, const std::string& tag ) const override {
168  return print( o, true, tag, true );
169  }
170  virtual MsgStream& print( MsgStream& o, const std::string& tag ) const override {
171  return print( o, true, tag, true );
172  }
173  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
174  std::string emptyName;
175  return print( o, tableFormat, emptyName, true );
176  }
177  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override {
178  std::string emptyName;
179  return print( o, tableFormat, emptyName, true );
180  }
182  std::ostringstream ost;
183  print( ost );
184  return ost.str();
185  }
186  std::ostream& fillStream( std::ostream& o ) const { return print( o ); }
187  MsgStream& fillStream( MsgStream& o ) const { return print( o ); }
188 };
StatEntity()=default
the constructor with automatic registration in the owner's counter map
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
double Max() const
Definition: StatEntity.h:95
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
double MeanErr() const
Definition: StatEntity.h:89
double flagMean() const
Definition: StatEntity.h:99
Gaudi::Accumulators::BinomialAccumulator< double, Gaudi::Accumulators::atomicity::full > BinomialAccParent
Definition: StatEntity.h:26
Gaudi::Accumulators::AccumulatorSet< double, Gaudi::Accumulators::atomicity::full, Gaudi::Accumulators::StatAccumulator, Gaudi::Accumulators::BinomialAccumulator > AccParent
Definition: StatEntity.h:25
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:212
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
Definition: StatEntity.h:173
stream & printImpl(stream &o, bool tableFormat, const std::string &name, bool flag, std::string const &fmtHead) const
Definition: StatEntity.h:121
double Eff() const
Definition: StatEntity.h:93
std::ostream & printFormatted(std::ostream &o, const std::string &format) const
Definition: StatEntity.h:115
T make_tuple(T... args)
void reset()
Definition: StatEntity.h:41
double rms() const
Definition: StatEntity.h:90
StatEntity operator++(int)
Definition: StatEntity.h:54
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:225
double Min() const
Definition: StatEntity.h:94
double RMS() const
Definition: StatEntity.h:92
STL class.
std::ostream & fillStream(std::ostream &o) const
Definition: StatEntity.h:186
StatEntity & operator++()
Definition: StatEntity.h:50
double Mean() const
Definition: StatEntity.h:88
double flag2() const
Definition: StatEntity.h:98
StatEntity & operator-=(double by)
Definition: StatEntity.h:46
StatEntity(OWNER *o, const std::string &tag)
Definition: StatEntity.h:32
MsgStream & print(MsgStream &o, bool tableFormat, const std::string &name, bool flag=true, std::string fmtHead="%|-48.48s|%|27t|") const
Definition: StatEntity.h:163
double flagMeanErr() const
Definition: StatEntity.h:101
T str(T... args)
double flagRMS() const
Definition: StatEntity.h:100
bool operator<(const StatEntity &se) const
Definition: StatEntity.h:68
T move(T... args)
double flagMin() const
Definition: StatEntity.h:102
double Rms() const
Definition: StatEntity.h:91
MsgStream & printFormatted(MsgStream &o, const std::string &format) const
Definition: StatEntity.h:118
StatEntity & operator+=(StatEntity by)
Definition: StatEntity.h:77
std::ostream & print(std::ostream &o, bool tableFormat, const std::string &name, bool flag=true, std::string fmtHead="%|-48.48s|%|27t|") const
Definition: StatEntity.h:159
double Sum() const
Definition: StatEntity.h:87
MsgStream & fillStream(MsgStream &o) const
Definition: StatEntity.h:187
StatEntity & operator--()
Definition: StatEntity.h:59
static bool effCounter(const std::string &name)
Definition: StatEntity.h:104
unsigned long addFlag(const double v)
Definition: StatEntity.h:85
stream & printFormattedImpl(stream &o, const std::string &format) const
Definition: StatEntity.h:110
double flagMax() const
Definition: StatEntity.h:103
double flag() const
Definition: StatEntity.h:97
StatEntity(const unsigned long entries, const double flag, const double flag2, const double minFlag, const double maxFlag)
Definition: StatEntity.h:35
backward compatible StatEntity class.
Definition: StatEntity.h:18
AccumulatorSet< Arithmetic, Atomicity, SigmaAccumulator, MinAccumulator, MaxAccumulator > StatAccumulator
StatAccumulator.
Definition: Accumulators.h:607
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: StatEntity.h:177
StatEntity operator--(int)
Definition: StatEntity.h:63
std::string toString() const
Definition: StatEntity.h:181
StatEntity & operator+=(double by)
Definition: StatEntity.h:73
STL class.
virtual std::ostream & print(std::ostream &o, const std::string &tag) const override
Definition: StatEntity.h:167
virtual MsgStream & print(MsgStream &o, const std::string &tag) const override
Definition: StatEntity.h:170
void operator=(double by)
Definition: StatEntity.h:42
unsigned long add(const double v)
Definition: StatEntity.h:81