The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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> {
24public:
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 ) {
36 reset( std::make_tuple(
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>
109 stream& printFormattedImpl( stream& o, const std::string& format ) const {
110 boost::format fmt{ format };
111 fmt % nEntries() % sum() % mean() % standard_deviation() % min() % max();
112 return o << fmt.str();
113 }
114 std::ostream& printFormatted( std::ostream& o, const std::string& format ) const {
115 return printFormattedImpl( o, format );
116 }
117 MsgStream& printFormatted( MsgStream& o, const std::string& format ) const { return printFormattedImpl( o, format ); }
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 }
180 std::string toString() const {
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
196private:
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_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
AccumulatorSet is an Accumulator that holds a set of Accumulators templated by same Arithmetic and At...
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
backward compatible StatEntity class.
Definition StatEntity.h:23
StatEntity & operator+=(StatEntity by)
Definition StatEntity.h:76
double Eff() const
Definition StatEntity.h:92
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition StatEntity.h:176
double Min() const
Definition StatEntity.h:93
unsigned long addFlag(const double v)
Definition StatEntity.h:84
double rms() const
Definition StatEntity.h:89
static StatEntity fromJSON(const nlohmann::json &j)
Definition StatEntity.h:189
void operator=(double by)
Definition StatEntity.h:41
double flag() const
Definition StatEntity.h:96
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
stream & printFormattedImpl(stream &o, const std::string &format) const
Definition StatEntity.h:109
MsgStream & printFormatted(MsgStream &o, const std::string &format) const
Definition StatEntity.h:117
StatEntity & operator--()
Definition StatEntity.h:58
friend void reset(StatEntity &s)
Definition StatEntity.h:40
std::ostream & printFormatted(std::ostream &o, const std::string &format) const
Definition StatEntity.h:114
double flagRMS() const
Definition StatEntity.h:99
double flagMax() const
Definition StatEntity.h:102
double Sum() const
Definition StatEntity.h:86
double Max() const
Definition StatEntity.h:94
StatEntity & operator++()
Definition StatEntity.h:49
double MeanErr() const
Definition StatEntity.h:88
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 operator++(int)
Definition StatEntity.h:53
double flag2() const
Definition StatEntity.h:97
MsgStream & fillStream(MsgStream &o) const
Definition StatEntity.h:186
double Rms() const
Definition StatEntity.h:90
double RMS() const
Definition StatEntity.h:91
bool operator<(const StatEntity &se) const
Definition StatEntity.h:67
Gaudi::Accumulators::BinomialAccumulator< Gaudi::Accumulators::atomicity::full, double > BinomialAccParent
Definition StatEntity.h:29
StatEntity(const unsigned long entries, const double flag, const double flag2, const double minFlag, const double maxFlag)
Definition StatEntity.h:34
double flagMin() const
Definition StatEntity.h:101
double flagMeanErr() const
Definition StatEntity.h:100
StatEntity & operator+=(double by)
Definition StatEntity.h:72
StatEntity()=default
the constructor with automatic registration in the owner's counter map
static const std::string typeString
Definition StatEntity.h:25
stream & printImpl(stream &o, bool tableFormat, std::string_view name, bool flag, std::string_view fmtHead) const
Definition StatEntity.h:120
static bool effCounter(std::string_view name)
Definition StatEntity.h:103
StatEntity operator--(int)
Definition StatEntity.h:62
Gaudi::Accumulators::AccumulatorSet< double, Gaudi::Accumulators::atomicity::full, double, Gaudi::Accumulators::StatAccumulator, Gaudi::Accumulators::BinomialAccumulator > AccParent
Definition StatEntity.h:26
friend void to_json(nlohmann::json &j, StatEntity const &s)
Basic JSON export for Gaudi::Monitoring::Hub support.
unsigned long add(const double v)
Definition StatEntity.h:80
StatEntity & operator-=(double by)
Definition StatEntity.h:45
static constexpr bool essentiallyEqual(double const a, double const b)
Definition StatEntity.h:197
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition StatEntity.h:172
virtual MsgStream & print(MsgStream &o, std::string_view tag) const override
Definition StatEntity.h:171
std::ostream & fillStream(std::ostream &o) const
Definition StatEntity.h:185
double flagMean() const
Definition StatEntity.h:98
double Mean() const
Definition StatEntity.h:87
std::string toString() const
Definition StatEntity.h:180
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
AccumulatorSet< Arithmetic, Atomicity, Arithmetic, SigmaAccumulator, MinAccumulator, MaxAccumulator > StatAccumulator
StatAccumulator.
Gaudi::ParticleID abs(const Gaudi::ParticleID &p)
Return the absolute value for a PID.
Definition ParticleID.h:191
An empty ancester of all counters that knows how to print themselves.
virtual std::ostream & print(std::ostream &, bool tableFormat=false) const =0
prints the counter to a stream