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