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 internal format description
59 // ============================================================================
61 {
62  // check for "X" or "L"
63  BOOST_STATIC_ASSERT(((sizeof(unsigned long)==4)||(sizeof(unsigned long)==8)));
64  // check for "D"
65  BOOST_STATIC_ASSERT((sizeof(double)==8)) ;
66  //
67  static const std::string s_fmt =
68  ( ( 8 == sizeof(unsigned long) ) ?"X:1;" : "L:1;" ) + std::string("D:4") ;
69  return s_fmt ;
70 }
71 // ============================================================================
72 // the actual size of published data
73 // ============================================================================
75 {
76  static const int s_size = sizeof(unsigned long) + 4 * sizeof(double) ;
77  return s_size ;
78 }
79 // ============================================================================
80 // mean value of flag
81 // ============================================================================
82 double StatEntity::mean () const
83 {
84  if ( 0 >= nEntries() ) { return 0 ;}
85  const long double f1 = m_se_accumulatedFlag ;
86  const long double f2 = m_se_nEntries ;
87  return f1 / f2 ;
88 }
89 // ============================================================================
90 // r.m.s of flag
91 // ============================================================================
92 double StatEntity::rms () const
93 {
94  if ( 0 >= nEntries() ) { return 0 ; }
95  const long double f1 = m_se_accumulatedFlag ;
96  const long double f2 = f1 / nEntries () ;
97  const long double f3 = m_se_accumulatedFlag2 ;
98  const long double f4 = f3 / nEntries () ;
99  const long double result = f4 - f2 * f2 ;
100  return ( 0 > result ) ? 0 : ::sqrtl ( result ) ;
101 }
102 // ============================================================================
103 // error in mean value of flag
104 // ============================================================================
105 double StatEntity::meanErr() const
106 {
107  if ( 0 >= nEntries () ) { return 0 ; }
108  const long double f1 = m_se_accumulatedFlag ;
109  const long double f2 = f1 / nEntries () ;
110  const long double f3 = m_se_accumulatedFlag2 ;
111  const long double f4 = f3 / nEntries () ;
112  const long double result = f4 - f2 * f2 ;
113  if ( 0 > result ) { return 0 ; }
114  //
115  return ::sqrtl ( result / nEntries () ) ;
116 }
117 // ============================================================================
118 // interprete the content as efficiency
119 // ============================================================================
120 double StatEntity::efficiency () const
121 {
122  if ( 1 > nEntries () || 0 > sum() || sum() > nEntries() ) { return -1 ; }
123  const long double fMin = min () ;
124  if ( 0 != fMin && 1 != fMin ) { return -1 ; }
125  const long double fMax = max () ;
126  if ( 0 != fMax && 1 != fMax ) { return -1 ; }
127  return mean() ;
128 }
129 // ============================================================================
130 // evaluate the binomial error in efficiency
131 // ============================================================================
133 {
134  if ( 0 > efficiency() ) { return -1 ; }
135  //
136  long double n1 = sum () ;
137  // treat properly the bins with eff=0
138  if ( 0 == n1 ) { n1 = 1 ; }
139  const long double n3 = nEntries () ;
140  long double n2 = n3 - flag () ;
141  // treat properly the bins with eff=100%
142  if ( 1 > fabsl( n2 ) ) { n2 = 1 ; }
143  //
144  return ::sqrtl( n1 * n2 / n3 ) / n3 ;
145 }
146 // ============================================================================
147 // increment with other entity
148 // ============================================================================
150 {
151  m_se_nEntries += other.m_se_nEntries ;
156  //
157  return *this ;
158 }
159 // ============================================================================
161 // ============================================================================
162 bool StatEntity::operator< ( const StatEntity& se ) const
163 {
164  if ( &se == this ) { return false ; }
165  else if ( nEntries () < se.nEntries () ) { return true ; }
166  else if ( nEntries () == se.nEntries () &&
167  sum () < se.sum () ) { return true ; }
168  else if ( nEntries () == se.nEntries () &&
169  sum () == se.sum () &&
170  min () < se.min () ) { return true ; }
171  else if ( nEntries () == se.nEntries () &&
172  sum () == se.sum () &&
173  min () == se.min () &&
174  max () < se.max () ) { return true ; }
175  else if ( nEntries () == se.nEntries () &&
176  sum () == se.flag () &&
177  min () == se.min () &&
178  max () == se.max () &&
179  sum2 () < se.sum2 () ) { return true ; }
181  return false;
182 }
183 // ============================================================================
184 // increment a flag
185 // ============================================================================
186 unsigned long StatEntity::add ( const double Flag )
187 {
188  //
190  else if ( 0 == m_se_nEntriesBeforeReset ) { reset(); }
191  m_se_accumulatedFlag += Flag ; // accumulate the flag
194  m_se_minimalFlag = std::min ( m_se_minimalFlag , Flag ) ; // evaluate min/max
195  m_se_maximalFlag = std::max ( m_se_maximalFlag , Flag ) ; // evaluate min/max
196  // accumulate statistics, but avoid FPE for small flags...
197  static const double s_min1 = 2 * ::sqrt ( std::numeric_limits<double>::min() ) ;
198  if ( s_min1 < Flag || -s_min1 > Flag )
199  { m_se_accumulatedFlag2 += Flag * Flag ; }// accumulate statistics:
200  //
201  return ++m_se_nEntries ;
202 }
203 // ============================================================================
204 // reset all quantities
205 // ============================================================================
207 {
208  //
209  m_se_nEntries = 0 ;
214  m_se_nEntriesBeforeReset = -1 ; // ?
215 }
216 // ============================================================================
218 // ============================================================================
219 void StatEntity::setnEntriesBeforeReset(unsigned long nEntriesBeforeReset )
220 { m_se_nEntriesBeforeReset = nEntriesBeforeReset; }
221 // ============================================================================
222 // representation as string
223 // ============================================================================
225 {
226  std::ostringstream ost ;
227  print ( ost ) ;
228  return ost.str () ;
229 }
230 // ============================================================================
231 // printout to std::ostream
232 // ============================================================================
234 {
235  boost::format fmt1 ("#=%|-7lu| Sum=%|-11.5g|" ) ;
236  o << fmt1 % nEntries() % sum() ;
237  boost::format fmt2 ( " Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|" ) ;
238  o << fmt2 % mean() % rms() % min() % max() ;
239  return o ;
240 }
241 // ============================================================================
242 // external operator for addition of StatEntity and a number
243 // ============================================================================
244 StatEntity operator+( const StatEntity& entity , const double value )
245 { StatEntity aux ( entity ) ; return aux+=value ; }
246 // ============================================================================
247 // external operator for addition of StatEntity and a number
248 // ============================================================================
249 StatEntity operator+( const double value , const StatEntity& entity )
250 { return entity + value ; }
251 // ============================================================================
252 // external operator for addition of StatEntity and a number
253 // ============================================================================
254 StatEntity operator-( const StatEntity& entity , const double value )
255 { StatEntity aux ( entity ) ; return aux-=value ; }
256 // ============================================================================
257 // external operator for addition of StatEntity and a number
258 // ============================================================================
259 StatEntity operator+( const StatEntity& entity , const StatEntity& value )
260 { StatEntity aux ( entity ) ; return aux+=value ; }
261 // ============================================================================
262 // external printout operator to std::ostream
263 // ============================================================================
264 std::ostream& operator<<( std::ostream& stream , const StatEntity& entity )
265 { return entity.print ( stream ) ; }
266 // ============================================================================
267 namespace
268 {
277  inline bool effCounter ( const std::string& name )
278  {
279  const std::string lower = boost::algorithm::to_lower_copy( name ) ;
280  return
281  std::string::npos != lower.find ( "eff" ) ||
282  std::string::npos != lower.find ( "acc" ) ||
283  std::string::npos != lower.find ( "filt" ) ||
284  std::string::npos != lower.find ( "fltr" ) ||
285  std::string::npos != lower.find ( "pass" ) ;
286  }
287 }
288 // ============================================================================
289 /* Format the counter in a form of the table row
290  * @param name the name associated with the counter
291  * @param counter counter to be printed
292  * @param flag use the special format for "efficiency" rows
293  * @param format1 row format for the regular rows
294  * @param format2 special row format for the "efficiency" rows
295  * @return formatted row in the table
296  */
297 // ============================================================================
299 ( const StatEntity& counter ,
300  const bool flag ,
301  const std::string& format1 ,
302  const std::string& format2 )
303 {
304  using namespace boost::io ;
305  if ( flag && 0 <= counter.eff() && 0 <= counter.effErr() )
306  {
307  boost::format fmt( format2 ) ;
308  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
309  fmt
310  % counter.nEntries ()
311  % counter.sum ()
312  % ( counter.eff () * 100 )
313  % ( counter.effErr () * 100 ) ;
314  return fmt.str() ;
315  }
316  boost::format fmt ( format1 ) ;
317  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
318  fmt
319  % counter.nEntries ()
320  % counter.sum ()
321  % counter.mean ()
322  % counter.rms ()
323  % counter.min ()
324  % counter.max () ;
325  return fmt.str() ;
326 }
327 // ============================================================================
328 /* Format the counter in a form of the table row
329  * @param name the name associated with the counter
330  * @param counter counter to be printed
331  * @param flag use the special format for efficiency rows
332  * @param format1 row format for the regular rows
333  * @param format2 the special row format for the "efficiency" rows
334  * @return formatted row in the table
335  */
336 // ============================================================================
338 ( const std::string& name ,
339  const StatEntity& counter ,
340  const bool flag ,
341  const std::string& format1 ,
342  const std::string& format2 )
343 {
344  using namespace boost::io ;
345  if ( flag && effCounter ( name ) && 0 <= counter.eff() && 0 <= counter.effErr() )
346  {
347  boost::format fmt( format2 ) ;
348  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
349  fmt
350  % ( "\"" + name + "\"" )
351  % counter.nEntries ()
352  % counter.sum ()
353  % ( counter.eff () * 100 )
354  % ( counter.effErr () * 100 ) ;
355  return fmt.str() ;
356  }
357  boost::format fmt ( format1 ) ;
358  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
359  fmt
360  % ( "\"" + name + "\"" )
361  % counter.nEntries ()
362  % counter.sum ()
363  % counter.mean ()
364  % counter.rms ()
365  % counter.min ()
366  % counter.max () ;
367  return fmt.str() ;
368 }
369 // ============================================================================
370 /* Format the counter in a form of the table row
371  * @param name the name associated with the counter
372  * @param group the group associated with the counter
373  * @param counter counter to be printed
374  * @param flag use the special format for efficiency rows
375  * @param format1 row format for the regular rows
376  * @param format2 the special row format for the "efficiency" rows
377  * @return formatted row in the table
378  */
379 // ============================================================================
381 ( const std::string& name ,
382  const std::string& group ,
383  const StatEntity& counter ,
384  const bool flag ,
385  const std::string& format1 ,
386  const std::string& format2 )
387 {
388  using namespace boost::io ;
389  if ( flag && ( effCounter ( name ) || effCounter ( group ) )
390  && 0 <= counter.eff() && 0 <= counter.effErr() )
391  {
392  boost::format fmt( format2 ) ;
393  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
394  fmt
395  % ( "\"" + name + ":" )
396  % ( ":" + group + "\"" )
397  % counter.nEntries ()
398  % counter.sum ()
399  % ( counter.eff () * 100 )
400  % ( counter.effErr () * 100 ) ;
401  return fmt.str() ;
402  }
403  boost::format fmt ( format1 ) ;
404  fmt.exceptions ( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) ) ;
405  fmt
406  % ( "\"" + name + ":" )
407  % ( ":" + group + "\"" )
408  % counter.nEntries ()
409  % counter.sum ()
410  % counter.mean ()
411  % counter.rms ()
412  % counter.min ()
413  % counter.max () ;
414  return fmt.str() ;
415 }
416 // ============================================================================
417 
418 
419 // ============================================================================
420 // The END
421 // ============================================================================
std::string toString() const
representation as string
Definition: StatEntity.cpp:224
double effErr() const
shortcut,
Definition: StatEntity.h:183
const double & max() const
maximal value
Definition: StatEntity.h:106
unsigned long m_se_nEntries
number of calls
Definition: StatEntity.h:434
StatEntity()
the default constructor
Definition: StatEntity.h:69
const unsigned long & nEntries() const
getters
Definition: StatEntity.h:92
StatEntity operator+(const StatEntity &entity, const double value)
external operator for addition of StatEntity and a number
Definition: StatEntity.cpp:244
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
double efficiency() const
Interpret the counter as some measure of efficiency The efficiency is calculated as the ratio of the ...
Definition: StatEntity.cpp:120
double m_se_minimalFlag
Definition: StatEntity.h:438
std::ostream & operator<<(std::ostream &stream, const StatEntity &entity)
external printout operator to std::ostream
Definition: StatEntity.cpp:264
double meanErr() const
error in mean value of counter
Definition: StatEntity.cpp:105
void reset()
reset the counters
Definition: StatEntity.cpp:206
long m_se_nEntriesBeforeReset
Definition: StatEntity.h:441
unsigned long add(const double Flag)
add a value
Definition: StatEntity.cpp:186
bool operator<(const StatEntity &se) const
comparison
Definition: StatEntity.cpp:162
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: StatEntity.cpp:233
const double & sum2() const
accumulated value**2
Definition: StatEntity.h:96
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:299
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:219
T min(T...args)
double eff() const
shortcut,
Definition: StatEntity.h:181
double mean() const
mean value of counter
Definition: StatEntity.cpp:82
double m_se_accumulatedFlag
accumulated flag
Definition: StatEntity.h:436
StatEntity operator-(const StatEntity &entity, const double value)
external operator for subtraction of StatEntity and a number
Definition: StatEntity.cpp:254
const double & min() const
minimal value
Definition: StatEntity.h:104
static const std::string & format()
the internal format description
Definition: StatEntity.cpp:60
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:206
T find(T...args)
static int size()
the actual size of published data
Definition: StatEntity.cpp:74
double rms() const
r.m.s of value
Definition: StatEntity.cpp:92
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:64
double m_se_maximalFlag
Definition: StatEntity.h:439
STL class.
double flag() const
accumulated "flag"
Definition: StatEntity.h:395
double efficiencyErr() const
Interpret the counter as some measure of efficiency and evaluate the uncertainty of this efficiency u...
Definition: StatEntity.cpp:132
double m_se_accumulatedFlag2
Definition: StatEntity.h:437