All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <iostream>
9 #include <sstream>
10 #include <string>
11 #include <cmath>
12 #include <limits>
13 // ============================================================================
14 // GaudiKernel
15 // ============================================================================
16 #include "GaudiKernel/StatEntity.h"
17 // ============================================================================
18 // Boost
19 // ============================================================================
20 #include "boost/format.hpp"
21 #include "boost/static_assert.hpp"
22 #include "boost/algorithm/string/case_conv.hpp"
23 // ============================================================================
24 
25 #ifdef __ICC
26 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
27 // A lot of comparisons are done and "meant".
28 #pragma warning(disable:1572)
29 // disable icc remark #2259: non-pointer conversion from "long double" to "double" may lose significant bits
30 // Internal operations are done with "long double", but the interface exposes only
31 // "double", so the conversions are required.
32 #pragma warning(disable:2259)
33 #endif
34 
35 
41 // ============================================================================
42 // The full contructor from all important values
43 // ============================================================================
45 ( const unsigned long entries ,
46  const double flag ,
47  const double flag2 ,
48  const double minFlag ,
49  const double maxFlag )
50  : m_se_nEntries ( entries )
51  , m_se_accumulatedFlag ( flag )
52  , m_se_accumulatedFlag2 ( flag2 )
53  , m_se_minimalFlag ( minFlag )
54  , m_se_maximalFlag ( maxFlag )
55  , m_se_nEntriesBeforeReset ( -1 )
56 {}
57 // ============================================================================
58 // The copy contructor, non default because of atomics
59 // ============================================================================
61 ( const StatEntity& other )
62  : m_se_nEntries ( other.m_se_nEntries )
63  , m_se_accumulatedFlag ( other.m_se_accumulatedFlag )
64  , m_se_accumulatedFlag2 ( other.m_se_accumulatedFlag2 )
65  , m_se_minimalFlag ( other.m_se_minimalFlag )
66  , m_se_maximalFlag ( other.m_se_maximalFlag )
67  , m_se_nEntriesBeforeReset ( other.m_se_nEntriesBeforeReset )
68 {}
69 // ============================================================================
70 // The copy contructor, non default because of atomics
71 // ============================================================================
72 StatEntity& StatEntity::operator=
73 ( const StatEntity& other ) {
74  m_se_nEntries = other.m_se_nEntries;
75  m_se_accumulatedFlag = other.m_se_accumulatedFlag;
76  m_se_accumulatedFlag2 = other.m_se_accumulatedFlag2;
77  m_se_minimalFlag = other.m_se_minimalFlag;
78  m_se_maximalFlag = other.m_se_maximalFlag;
79  m_se_nEntriesBeforeReset = other.m_se_nEntriesBeforeReset;
80  return *this;
81 }// ============================================================================
82 // the internal format description
83 // ============================================================================
85 {
86  // check for "X" or "L"
87  BOOST_STATIC_ASSERT(((sizeof(unsigned long)==4)||(sizeof(unsigned long)==8)));
88  // check for "D"
89  BOOST_STATIC_ASSERT((sizeof(double)==8)) ;
90  //
91  static const std::string s_fmt =
92  ( ( 8 == sizeof(unsigned long) ) ?"X:1;" : "L:1;" ) + std::string("D:4") ;
93  return s_fmt ;
94 }
95 // ============================================================================
96 // the actual size of published data
97 // ============================================================================
99 {
100  static const int s_size = sizeof(unsigned long) + 4 * sizeof(double) ;
101  return s_size ;
102 }
103 // ============================================================================
104 // mean value of flag
105 // ============================================================================
106 double StatEntity::mean () const
107 {
108  if ( 0 >= nEntries() ) { return 0 ;}
109  const long double f1 = m_se_accumulatedFlag ;
110  const long double f2 = m_se_nEntries ;
111  return f1 / f2 ;
112 }
113 // ============================================================================
114 // r.m.s of flag
115 // ============================================================================
116 double StatEntity::rms () const
117 {
118  if ( 0 >= nEntries() ) { return 0 ; }
119  const long double f1 = m_se_accumulatedFlag ;
120  const long double f2 = f1 / nEntries () ;
121  const long double f3 = m_se_accumulatedFlag2 ;
122  const long double f4 = f3 / nEntries () ;
123  const long double result = f4 - f2 * f2 ;
124  return ( 0 > result ) ? 0 : ::sqrtl ( result ) ;
125 }
126 // ============================================================================
127 // error in mean value of flag
128 // ============================================================================
129 double StatEntity::meanErr() const
130 {
131  if ( 0 >= nEntries () ) { return 0 ; }
132  const long double f1 = m_se_accumulatedFlag ;
133  const long double f2 = f1 / nEntries () ;
134  const long double f3 = m_se_accumulatedFlag2 ;
135  const long double f4 = f3 / nEntries () ;
136  const long double result = f4 - f2 * f2 ;
137  if ( 0 > result ) { return 0 ; }
138  //
139  return ::sqrtl ( result / nEntries () ) ;
140 }
141 // ============================================================================
142 // interprete the content as efficiency
143 // ============================================================================
144 double StatEntity::efficiency () const
145 {
146  if ( 1 > nEntries () || 0 > sum() || sum() > nEntries() ) { return -1 ; }
147  const long double fMin = min () ;
148  if ( 0 != fMin && 1 != fMin ) { return -1 ; }
149  const long double fMax = max () ;
150  if ( 0 != fMax && 1 != fMax ) { return -1 ; }
151  return mean() ;
152 }
153 // ============================================================================
154 // evaluate the binomial error in efficiency
155 // ============================================================================
157 {
158  if ( 0 > efficiency() ) { return -1 ; }
159  //
160  long double n1 = sum () ;
161  // treat properly the bins with eff=0
162  if ( 0 == n1 ) { n1 = 1 ; }
163  const long double n3 = nEntries () ;
164  long double n2 = n3 - flag () ;
165  // treat properly the bins with eff=100%
166  if ( 1 > fabsl( n2 ) ) { n2 = 1 ; }
167  //
168  return ::sqrtl( n1 * n2 / n3 ) / n3 ;
169 }
170 // ============================================================================
171 // increment with other entity
172 // ============================================================================
174 {
176  m_se_nEntries += other.m_se_nEntries ;
181  //
182  return *this ;
183 }
184 // ============================================================================
186 // ============================================================================
187 bool StatEntity::operator< ( const StatEntity& se ) const
188 {
189  if ( &se == this ) { return false ; }
190  else if ( nEntries () < se.nEntries () ) { return true ; }
191  else if ( nEntries () == se.nEntries () &&
192  sum () < se.sum () ) { return true ; }
193  else if ( nEntries () == se.nEntries () &&
194  sum () == se.sum () &&
195  min () < se.min () ) { return true ; }
196  else if ( nEntries () == se.nEntries () &&
197  sum () == se.sum () &&
198  min () == se.min () &&
199  max () < se.max () ) { return true ; }
200  else if ( nEntries () == se.nEntries () &&
201  sum () == se.flag () &&
202  min () == se.min () &&
203  max () == se.max () &&
204  sum2 () < se.sum2 () ) { return true ; }
206  return false;
207 }
208 // ============================================================================
209 // increment a flag
210 // ============================================================================
211 unsigned long StatEntity::add ( const double Flag )
212 {
214  //
216  else if ( 0 == m_se_nEntriesBeforeReset ) { reset(); }
217  m_se_accumulatedFlag += Flag ; // accumulate the flag
220  m_se_minimalFlag = std::min ( m_se_minimalFlag , Flag ) ; // evaluate min/max
221  m_se_maximalFlag = std::max ( m_se_maximalFlag , Flag ) ; // evaluate min/max
222  // accumulate statistics, but avoid FPE for small flags...
223  static const double s_min1 = 2 * ::sqrt ( std::numeric_limits<double>::min() ) ;
224  if ( s_min1 < Flag || -s_min1 > Flag )
225  { m_se_accumulatedFlag2 += Flag * Flag ; }// accumulate statistics:
226  //
227  return ++m_se_nEntries ;
228 }
229 // ============================================================================
230 // reset all quantities
231 // ============================================================================
233 {
235  //
236  m_se_nEntries = 0 ;
241  m_se_nEntriesBeforeReset = -1 ; // ?
242 }
243 // ============================================================================
245 // ============================================================================
246 void StatEntity::setnEntriesBeforeReset(unsigned long nEntriesBeforeReset )
247 { m_se_nEntriesBeforeReset = nEntriesBeforeReset; }
248 // ============================================================================
249 // representation as string
250 // ============================================================================
252 {
253  std::ostringstream ost ;
254  print ( ost ) ;
255  return ost.str () ;
256 }
257 // ============================================================================
258 // printout to std::ostream
259 // ============================================================================
261 {
262  boost::format fmt1 ("#=%|-7lu| Sum=%|-11.5g|" ) ;
263  o << fmt1 % nEntries() % sum() ;
264  boost::format fmt2 ( " Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|" ) ;
265  o << fmt2 % mean() % rms() % min() % max() ;
266  return o ;
267 }
268 // ============================================================================
269 // external operator for addition of StatEntity and a number
270 // ============================================================================
271 StatEntity operator+( const StatEntity& entity , const double value )
272 { StatEntity aux ( entity ) ; return aux+=value ; }
273 // ============================================================================
274 // external operator for addition of StatEntity and a number
275 // ============================================================================
276 StatEntity operator+( const double value , const StatEntity& entity )
277 { return entity + value ; }
278 // ============================================================================
279 // external operator for addition of StatEntity and a number
280 // ============================================================================
281 StatEntity operator-( const StatEntity& entity , const double value )
282 { StatEntity aux ( entity ) ; return aux-=value ; }
283 // ============================================================================
284 // external operator for addition of StatEntity and a number
285 // ============================================================================
286 StatEntity operator+( const StatEntity& entity , const StatEntity& value )
287 { StatEntity aux ( entity ) ; return aux+=value ; }
288 // ============================================================================
289 // external printout operator to std::ostream
290 // ============================================================================
291 std::ostream& operator<<( std::ostream& stream , const StatEntity& entity )
292 { return entity.print ( stream ) ; }
293 // ============================================================================
294 namespace
295 {
304  inline bool effCounter ( const std::string& name )
305  {
306  const std::string lower = boost::algorithm::to_lower_copy( name ) ;
307  return
308  std::string::npos != lower.find ( "eff" ) ||
309  std::string::npos != lower.find ( "acc" ) ||
310  std::string::npos != lower.find ( "filt" ) ||
311  std::string::npos != lower.find ( "fltr" ) ||
312  std::string::npos != lower.find ( "pass" ) ;
313  }
314 }
315 // ============================================================================
316 /* Format the counter in a form of the table row
317  * @param name the name associated with the counter
318  * @param counter counter to be printed
319  * @param flag use the special format for "efficiency" rows
320  * @param format1 row format for the regular rows
321  * @param format2 special row format for the "efficiency" rows
322  * @return formatted row in the table
323  */
324 // ============================================================================
326 ( const StatEntity& counter ,
327  const bool flag ,
328  const std::string& format1 ,
329  const std::string& format2 )
330 {
331  using namespace boost::io ;
332  if ( flag && 0 <= counter.eff() && 0 <= counter.effErr() )
333  {
334  boost::format fmt( format2 ) ;
335  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
336  fmt
337  % counter.nEntries ()
338  % counter.sum ()
339  % ( counter.eff () * 100 )
340  % ( counter.effErr () * 100 ) ;
341  return fmt.str() ;
342  }
343  boost::format fmt ( format1 ) ;
344  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
345  fmt
346  % counter.nEntries ()
347  % counter.sum ()
348  % counter.mean ()
349  % counter.rms ()
350  % counter.min ()
351  % counter.max () ;
352  return fmt.str() ;
353 }
354 // ============================================================================
355 /* Format the counter in a form of the table row
356  * @param name the name associated with the counter
357  * @param counter counter to be printed
358  * @param flag use the special format for efficiency rows
359  * @param format1 row format for the regular rows
360  * @param format2 the special row format for the "efficiency" rows
361  * @return formatted row in the table
362  */
363 // ============================================================================
365 ( const std::string& name ,
366  const StatEntity& counter ,
367  const bool flag ,
368  const std::string& format1 ,
369  const std::string& format2 )
370 {
371  using namespace boost::io ;
372  if ( flag && effCounter ( name ) && 0 <= counter.eff() && 0 <= counter.effErr() )
373  {
374  boost::format fmt( format2 ) ;
375  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
376  fmt
377  % ( "\"" + name + "\"" )
378  % counter.nEntries ()
379  % counter.sum ()
380  % ( counter.eff () * 100 )
381  % ( counter.effErr () * 100 ) ;
382  return fmt.str() ;
383  }
384  boost::format fmt ( format1 ) ;
385  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
386  fmt
387  % ( "\"" + name + "\"" )
388  % counter.nEntries ()
389  % counter.sum ()
390  % counter.mean ()
391  % counter.rms ()
392  % counter.min ()
393  % counter.max () ;
394  return fmt.str() ;
395 }
396 // ============================================================================
397 /* Format the counter in a form of the table row
398  * @param name the name associated with the counter
399  * @param group the group associated with the counter
400  * @param counter counter to be printed
401  * @param flag use the special format for efficiency rows
402  * @param format1 row format for the regular rows
403  * @param format2 the special row format for the "efficiency" rows
404  * @return formatted row in the table
405  */
406 // ============================================================================
408 ( const std::string& name ,
409  const std::string& group ,
410  const StatEntity& counter ,
411  const bool flag ,
412  const std::string& format1 ,
413  const std::string& format2 )
414 {
415  using namespace boost::io ;
416  if ( flag && ( effCounter ( name ) || effCounter ( group ) )
417  && 0 <= counter.eff() && 0 <= counter.effErr() )
418  {
419  boost::format fmt( format2 ) ;
420  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
421  fmt
422  % ( "\"" + name + ":" )
423  % ( ":" + group + "\"" )
424  % counter.nEntries ()
425  % counter.sum ()
426  % ( counter.eff () * 100 )
427  % ( counter.effErr () * 100 ) ;
428  return fmt.str() ;
429  }
430  boost::format fmt ( format1 ) ;
431  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
432  fmt
433  % ( "\"" + name + ":" )
434  % ( ":" + group + "\"" )
435  % counter.nEntries ()
436  % counter.sum ()
437  % counter.mean ()
438  % counter.rms ()
439  % counter.min ()
440  % counter.max () ;
441  return fmt.str() ;
442 }
443 // ============================================================================
444 
445 
446 // ============================================================================
447 // The END
448 // ============================================================================
std::string toString() const
representation as string
Definition: StatEntity.cpp:251
double effErr() const
shortcut,
Definition: StatEntity.h:188
const double & max() const
maximal value
Definition: StatEntity.h:111
unsigned long m_se_nEntries
number of calls
Definition: StatEntity.h:439
StatEntity()
the default constructor
Definition: StatEntity.h:70
const unsigned long & nEntries() const
getters
Definition: StatEntity.h:97
StatEntity operator+(const StatEntity &entity, const double value)
external operator for addition of StatEntity and a number
Definition: StatEntity.cpp:271
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
std::mutex m_mutex
Definition: StatEntity.h:448
double efficiency() const
Interpret the counter as some measure of efficiency The efficiency is calculated as the ratio of the ...
Definition: StatEntity.cpp:144
double m_se_minimalFlag
Definition: StatEntity.h:443
std::ostream & operator<<(std::ostream &stream, const StatEntity &entity)
external printout operator to std::ostream
Definition: StatEntity.cpp:291
double meanErr() const
error in mean value of counter
Definition: StatEntity.cpp:129
void reset()
reset the counters
Definition: StatEntity.cpp:232
long m_se_nEntriesBeforeReset
Definition: StatEntity.h:446
unsigned long add(const double Flag)
add a value
Definition: StatEntity.cpp:211
bool operator<(const StatEntity &se) const
comparison
Definition: StatEntity.cpp:187
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: StatEntity.cpp:260
const double & sum2() const
accumulated value**2
Definition: StatEntity.h:101
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:326
const double & sum() const
accumulated value
Definition: StatEntity.h:99
STL class.
void setnEntriesBeforeReset(unsigned long nEntriesBeforeReset)
DR specify number of entry before reset.
Definition: StatEntity.cpp:246
T min(T...args)
double eff() const
shortcut,
Definition: StatEntity.h:186
double mean() const
mean value of counter
Definition: StatEntity.cpp:106
double m_se_accumulatedFlag
accumulated flag
Definition: StatEntity.h:441
StatEntity operator-(const StatEntity &entity, const double value)
external operator for subtraction of StatEntity and a number
Definition: StatEntity.cpp:281
const double & min() const
minimal value
Definition: StatEntity.h:109
static const std::string & format()
the internal format description
Definition: StatEntity.cpp:84
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:211
T find(T...args)
static int size()
the actual size of published data
Definition: StatEntity.cpp:98
double rms() const
r.m.s of value
Definition: StatEntity.cpp:116
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:65
double m_se_maximalFlag
Definition: StatEntity.h:444
STL class.
double flag() const
accumulated "flag"
Definition: StatEntity.h:400
double efficiencyErr() const
Interpret the counter as some measure of efficiency and evaluate the uncertainty of this efficiency u...
Definition: StatEntity.cpp:156
double m_se_accumulatedFlag2
Definition: StatEntity.h:442