The Gaudi Framework  v29r0 (ff2e7097)
StatEntity.cpp
Go to the documentation of this file.
1 // ============================================================================
2 #define GAUDIKERNEL_STATENTITY_CPP 1
3 // ============================================================================
4 // include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <cmath>
9 #include <iostream>
10 #include <limits>
11 #include <sstream>
12 #include <string>
13 // ============================================================================
14 // GaudiKernel
15 // ============================================================================
16 #include "GaudiKernel/StatEntity.h"
17 // ============================================================================
18 // Boost
19 // ============================================================================
20 #include "boost/algorithm/string/case_conv.hpp"
21 #include "boost/format.hpp"
22 // ============================================================================
23 
24 #ifdef __ICC
25 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
26 // A lot of comparisons are done and "meant".
27 #pragma warning( disable : 1572 )
28 // disable icc remark #2259: non-pointer conversion from "long double" to "double" may lose significant bits
29 // Internal operations are done with "long double", but the interface exposes only
30 // "double", so the conversions are required.
31 #pragma warning( disable : 2259 )
32 #endif
33 
39 // ============================================================================
40 // The full contructor from all important values
41 // ============================================================================
42 StatEntity::StatEntity( const unsigned long entries, const double flag, const double flag2, const double minFlag,
43  const double maxFlag )
44  : m_se{entries, flag, flag2, minFlag, maxFlag}
45 {
46 }
47 // ============================================================================
48 // The copy contructor, non default because of atomics
49 // ============================================================================
50 StatEntity::StatEntity( const StatEntity& other ) : m_se{other.m_se} {}
51 // ============================================================================
52 // The assignment operator, non default because of atomics
53 // ============================================================================
55 {
57  m_se = other.m_se;
58  return *this;
59 } // ============================================================================
60 // the internal format description
61 // ============================================================================
63 {
64  // check for "X" or "L"
65  static_assert( ( ( sizeof( unsigned long ) == 4 ) || ( sizeof( unsigned long ) == 8 ) ), "check for X or L" );
66  // check for "D"
67  static_assert( ( sizeof( double ) == 8 ), "check for D" );
68  //
69  static const std::string s_fmt = ( ( 8 == sizeof( unsigned long ) ) ? "X:1;" : "L:1;" ) + std::string( "D:4" );
70  return s_fmt;
71 }
72 // ============================================================================
73 // the actual size of published data
74 // ============================================================================
76 {
77  static const int s_size = sizeof( unsigned long ) + 4 * sizeof( double );
78  return s_size;
79 }
80 // ============================================================================
81 // t
82 // ============================================================================
83 unsigned long StatEntity::se::add( double Flag )
84 {
85  if ( 0 < nEntriesBeforeReset ) {
86  --nEntriesBeforeReset;
87  } else if ( 0 == nEntriesBeforeReset ) {
88  *this = {};
89  }
90  accumulatedFlag += Flag; // accumulate the flag
93  minimalFlag = std::min( minimalFlag, Flag ); // evaluate min/max
94  maximalFlag = std::max( maximalFlag, Flag ); // evaluate min/max
95  // accumulate statistics, but avoid FPE for small flags...
96  static double s_min1 = 2 * ::sqrt( std::numeric_limits<double>::min() );
97  if ( s_min1 < Flag || -s_min1 > Flag ) {
98  accumulatedFlag2 += Flag * Flag;
99  } // accumulate statistics:
100  //
101  return ++nEntries;
102 }
103 // ============================================================================
104 // mean value of flag
105 // ============================================================================
106 double StatEntity::se::mean() const
107 {
108  const long double f1 = accumulatedFlag;
109  const long double f2 = nEntries;
110  return nEntries > 0 ? f1 / f2 : 0;
111 }
112 // ============================================================================
113 // r.m.s of flag
114 // ============================================================================
115 double StatEntity::se::rms() const
116 {
117  if ( 0 >= nEntries ) {
118  return 0;
119  }
120  const long double f1 = accumulatedFlag;
121  const long double f2 = f1 / nEntries;
122  const long double f3 = accumulatedFlag2;
123  const long double f4 = f3 / nEntries;
124  const long double result = f4 - f2 * f2;
125  return ( 0 > result ) ? 0 : ::sqrtl( result );
126 }
127 // ============================================================================
128 // error in mean value of flag
129 // ============================================================================
131 {
132  if ( 0 >= nEntries ) {
133  return 0;
134  }
135  const long double f1 = accumulatedFlag;
136  const long double f2 = f1 / nEntries;
137  const long double f3 = accumulatedFlag2;
138  const long double f4 = f3 / nEntries;
139  const long double result = f4 - f2 * f2;
140  if ( 0 > result ) {
141  return 0;
142  }
143  //
144  return ::sqrtl( result / nEntries );
145 }
146 // ============================================================================
147 // interprete the content as efficiency
148 // ============================================================================
150 {
151  if ( 1 > nEntries || 0 > accumulatedFlag || accumulatedFlag > nEntries ) {
152  return -1;
153  }
154  const long double fMin = minimalFlag;
155  if ( 0 != fMin && 1 != fMin ) {
156  return -1;
157  }
158  const long double fMax = maximalFlag;
159  if ( 0 != fMax && 1 != fMax ) {
160  return -1;
161  }
162  return mean();
163 }
164 // ============================================================================
165 // evaluate the binomial error in efficiency
166 // ============================================================================
168 {
169  if ( 0 > efficiency() ) {
170  return -1;
171  }
172  //
173  long double n1 = accumulatedFlag;
174  // treat properly the bins with eff=0
175  if ( 0 == n1 ) {
176  n1 = 1;
177  }
178  const long double n3 = nEntries;
179  long double n2 = n3 - accumulatedFlag;
180  // treat properly the bins with eff=100%
181  if ( 1 > fabsl( n2 ) ) {
182  n2 = 1;
183  }
184  //
185  return ::sqrtl( n1 * n2 / n3 ) / n3;
186 }
187 // ============================================================================
188 // increment with other entity
189 // ============================================================================
191 {
193  m_se += other.m_se;
194  return *this;
195 }
196 // ============================================================================
197 // increment a flag
198 // ============================================================================
199 unsigned long StatEntity::add( const double flag )
200 {
202  return m_se.add( flag );
203 }
204 // ============================================================================
205 // reset all quantities
206 // ============================================================================
208 {
210  m_se = {};
211 }
212 // ============================================================================
214 // ============================================================================
215 void StatEntity::setnEntriesBeforeReset( unsigned long nEntriesBeforeReset )
216 {
218  m_se.nEntriesBeforeReset = nEntriesBeforeReset;
219 }
220 // ============================================================================
221 // representation as string
222 // ============================================================================
224 {
225  std::ostringstream ost;
226  print( ost );
227  return ost.str();
228 }
229 // ============================================================================
230 // printout to std::ostream
231 // ============================================================================
233 {
234  boost::format fmt1( "#=%|-7lu| Sum=%|-11.5g|" );
235  o << fmt1 % nEntries() % sum();
236  boost::format fmt2( " Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|" );
237  o << fmt2 % mean() % rms() % min() % max();
238  return o;
239 }
240 // ============================================================================
242 // ============================================================================
243 bool operator<( const StatEntity& lhs, const StatEntity& rhs ) { return lhs.m_se < rhs.m_se; }
244 // ============================================================================
245 // external operator for addition of StatEntity and a number
246 // ============================================================================
247 StatEntity operator+( const StatEntity& entity, const double value )
248 {
249  StatEntity aux( entity );
250  return aux += value;
251 }
252 // ============================================================================
253 // external operator for addition of StatEntity and a number
254 // ============================================================================
255 StatEntity operator+( const double value, const StatEntity& entity ) { return entity + value; }
256 // ============================================================================
257 // external operator for addition of StatEntity and a number
258 // ============================================================================
259 StatEntity operator-( const StatEntity& entity, const double value )
260 {
261  StatEntity aux( entity );
262  return aux -= value;
263 }
264 // ============================================================================
265 // external operator for addition of StatEntity and a number
266 // ============================================================================
267 StatEntity operator+( const StatEntity& entity, const StatEntity& value )
268 {
269  StatEntity aux( entity );
270  return aux += value;
271 }
272 // ============================================================================
273 // external printout operator to std::ostream
274 // ============================================================================
275 std::ostream& operator<<( std::ostream& stream, const StatEntity& entity ) { return entity.print( stream ); }
276 // ============================================================================
277 namespace
278 {
287  inline bool effCounter( const std::string& name )
288  {
289  const std::string lower = boost::algorithm::to_lower_copy( name );
290  return std::string::npos != lower.find( "eff" ) || std::string::npos != lower.find( "acc" ) ||
291  std::string::npos != lower.find( "filt" ) || std::string::npos != lower.find( "fltr" ) ||
292  std::string::npos != lower.find( "pass" );
293  }
294 }
295 // ============================================================================
296 /* Format the counter in a form of the table row
297  * @param name the name associated with the counter
298  * @param counter counter to be printed
299  * @param flag use the special format for "efficiency" rows
300  * @param format1 row format for the regular rows
301  * @param format2 special row format for the "efficiency" rows
302  * @return formatted row in the table
303  */
304 // ============================================================================
306  const std::string& format2 )
307 {
308  using namespace boost::io;
309  if ( flag && 0 <= counter.eff() && 0 <= counter.effErr() ) {
310  boost::format fmt( format2 );
311  fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
312  fmt % counter.nEntries() % counter.sum() % ( counter.eff() * 100 ) % ( counter.effErr() * 100 );
313  return fmt.str();
314  }
315  boost::format fmt( format1 );
316  fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
317  fmt % counter.nEntries() % counter.sum() % counter.mean() % counter.rms() % counter.min() % counter.max();
318  return fmt.str();
319 }
320 // ============================================================================
321 /* Format the counter in a form of the table row
322  * @param name the name associated with the counter
323  * @param counter counter to be printed
324  * @param flag use the special format for efficiency rows
325  * @param format1 row format for the regular rows
326  * @param format2 the special row format for the "efficiency" rows
327  * @return formatted row in the table
328  */
329 // ============================================================================
331  const std::string& format1, const std::string& format2 )
332 {
333  using namespace boost::io;
334  if ( flag && effCounter( name ) && 0 <= counter.eff() && 0 <= counter.effErr() ) {
335  boost::format fmt( format2 );
336  fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
337  fmt % ( "\"" + name + "\"" ) % counter.nEntries() % counter.sum() % ( counter.eff() * 100 ) %
338  ( counter.effErr() * 100 );
339  return fmt.str();
340  }
341  boost::format fmt( format1 );
342  fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
343  fmt % ( "\"" + name + "\"" ) % counter.nEntries() % counter.sum() % counter.mean() % counter.rms() % counter.min() %
344  counter.max();
345  return fmt.str();
346 }
347 // ============================================================================
348 /* Format the counter in a form of the table row
349  * @param name the name associated with the counter
350  * @param group the group associated with the counter
351  * @param counter counter to be printed
352  * @param flag use the special format for efficiency rows
353  * @param format1 row format for the regular rows
354  * @param format2 the special row format for the "efficiency" rows
355  * @return formatted row in the table
356  */
357 // ============================================================================
359  const StatEntity& counter, const bool flag, const std::string& format1,
360  const std::string& format2 )
361 {
362  using namespace boost::io;
363  if ( flag && ( effCounter( name ) || effCounter( group ) ) && 0 <= counter.eff() && 0 <= counter.effErr() ) {
364  boost::format fmt( format2 );
365  fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
366  fmt % ( "\"" + name + ":" ) % ( ":" + group + "\"" ) % counter.nEntries() % counter.sum() %
367  ( counter.eff() * 100 ) % ( counter.effErr() * 100 );
368  return fmt.str();
369  }
370  boost::format fmt( format1 );
371  fmt.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
372  fmt % ( "\"" + name + ":" ) % ( ":" + group + "\"" ) % counter.nEntries() % counter.sum() % counter.mean() %
373  counter.rms() % counter.min() % counter.max();
374  return fmt.str();
375 }
376 // ============================================================================
377 
378 // ============================================================================
379 // The END
380 // ============================================================================
double mean() const
Definition: StatEntity.cpp:106
std::string toString() const
representation as string
Definition: StatEntity.cpp:223
StatEntity()=default
the default constructor
double effErr() const
shortcut,
Definition: StatEntity.h:183
const double & max() const
maximal value
Definition: StatEntity.h:106
GAUDI_API friend std::ostream & operator<<(std::ostream &stream, const StatEntity &entity)
external printout operator to std::ostream
Definition: StatEntity.cpp:275
const unsigned long & nEntries() const
getters – no synchronization!
Definition: StatEntity.h:92
unsigned long add(double Flag)
Definition: StatEntity.cpp:83
long nEntriesBeforeReset
Definition: StatEntity.h:455
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
std::mutex m_mutex
Definition: StatEntity.h:483
double efficiency() const
Interpret the counter as some measure of efficiency The efficiency is calculated as the ratio of the ...
Definition: StatEntity.h:142
double meanErr() const
Definition: StatEntity.cpp:130
void reset()
reset the counters
Definition: StatEntity.cpp:207
unsigned long add(const double Flag)
add a value
Definition: StatEntity.cpp:199
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: StatEntity.cpp:232
double rms() const
Definition: StatEntity.cpp:115
GAUDI_API std::string formatAsTableRow(const StatEntity &counter, const bool flag, const std::string &format1=" |%|7d| |%|11.7g| |%|#11.5g| |%|#10.5g| |%|#10.5g| |%|#10.5g| |", const std::string &format2="*|%|7d| |%|11.5g| |(%|#9.7g| +- %|-#8.6g|)%%| ----- | ----- |")
print the counter in a form of the table row
Definition: StatEntity.cpp:305
GAUDI_API friend StatEntity operator+(const StatEntity &entity, const double value)
external operator for addition of StatEntity and a number
Definition: StatEntity.cpp:247
const double & sum() const
accumulated value
Definition: StatEntity.h:94
STL class.
void setnEntriesBeforeReset(unsigned long nEntriesBeforeReset)
DR specify number of entry before reset.
Definition: StatEntity.cpp:215
T min(T...args)
double eff() const
shortcut,
Definition: StatEntity.h:181
GAUDI_API friend StatEntity operator-(const StatEntity &entity, const double value)
external operator for subtraction of StatEntity and a number
Definition: StatEntity.cpp:259
double mean() const
mean value of counter
Definition: StatEntity.h:98
const double & min() const
minimal value
Definition: StatEntity.h:104
static const std::string & format()
the internal format description
Definition: StatEntity.cpp:62
T max(T...args)
StatEntity & operator+=(const double f)
General increment operator for the flag Could be used for easy manipulation with StatEntity object: ...
Definition: StatEntity.h:207
double efficiency() const
Definition: StatEntity.cpp:149
T find(T...args)
double efficiencyErr() const
Definition: StatEntity.cpp:167
struct StatEntity::se m_se
static int size()
the actual size of published data
Definition: StatEntity.cpp:75
double rms() const
r.m.s of value
Definition: StatEntity.h:100
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:65
double flag2() const
accumulated "flag squared"
Definition: StatEntity.h:398
STL class.
double flag() const
accumulated "flag"
Definition: StatEntity.h:396
GAUDI_API friend bool operator<(const StatEntity &lhs, const StatEntity &rhs)
comparison
Definition: StatEntity.cpp:243
StatEntity & operator=(const StatEntity &)
assignment operator
Definition: StatEntity.cpp:54