00001 #ifndef __GAUDI_CHRONOSTATSVC_STATENTITY_H__
00002 #define __GAUDI_CHRONOSTATSVC_STATENTITY_H__
00003
00004 #include <iomanip>
00005 #include <iostream>
00006 #include <strstream>
00007 #include <cmath>
00008 #include <algorithm>
00009 #include <functional>
00010
00011 #include "GaudiKernel/IChronoStatSvc.h"
00012
00013
00014 #ifdef linux
00015 #define __ADJUST_THE_STREAM__(s,w,p) s.precision(p); \
00016 s << std::setw(w); \
00017 s.setf( std::ios::left , std::ios::adjustfield ); \
00018 s.setf( std::ios::scientific , std::ios::floatfield );
00019 #else
00020 #define __ADJUST_THE_STREAM__(s,w,p) ;
00021 #endif
00022
00023 class ChronoStatSvc;
00024
00033 class StatEntity
00034 {
00036 public:
00039 StatEntity()
00040 : m_se_nEntries ( 0 )
00041 , m_se_accumulatedFlag ( 0 )
00042 , m_se_minimalFlag ( 5000000 )
00043 , m_se_maximalFlag ( -5000000 )
00044 , m_se_accumulatedFlag2 ( 0 )
00045 , m_se_accumulatedWeight ( 1 )
00046 , m_se_minimalWeight ( 5000000 )
00047 , m_se_maximalWeight ( -5000000 )
00048 {};
00050 ~StatEntity(){};
00052 public:
00055 inline const unsigned long & nEntries () const ;
00058 inline const IChronoStatSvc::StatFlag & flag () const ;
00060 inline const IChronoStatSvc::StatFlag & flag2 () const ;
00062 inline const IChronoStatSvc::StatFlag flagMean () const ;
00064 inline const IChronoStatSvc::StatFlag flagRMS () const ;
00066 inline const IChronoStatSvc::StatFlag flagMeanErr() const ;
00068 inline const IChronoStatSvc::StatFlag & flagMin () const ;
00070 inline const IChronoStatSvc::StatFlag & flagMax () const ;
00072 inline const IChronoStatSvc::StatWeight & weight () const ;
00074 inline const IChronoStatSvc::StatWeight & weightMin () const ;
00076 inline const IChronoStatSvc::StatWeight & weightMax () const ;
00078 inline const IChronoStatSvc::StatWeight weightHarmonicMean () const ;
00080 inline unsigned long addFlagAndWeight ( const IChronoStatSvc::StatFlag & Flag ,
00081 const IChronoStatSvc::StatWeight& Weight ) ;
00083 inline const std::string stringOutPut() const;
00085 inline bool operator< ( const StatEntity& se ) const ;
00086 inline bool operator<=( const StatEntity& se ) const ;
00087 inline bool operator> ( const StatEntity& se ) const ;
00088 inline bool operator>=( const StatEntity& se ) const ;
00090 public:
00092 class ComparePairOfStatEntityAndStatTag;
00093 private:
00095 unsigned long m_se_nEntries ;
00097 IChronoStatSvc::StatFlag m_se_accumulatedFlag ;
00098 IChronoStatSvc::StatFlag m_se_minimalFlag ;
00099 IChronoStatSvc::StatFlag m_se_maximalFlag ;
00100 IChronoStatSvc::StatFlag m_se_accumulatedFlag2 ;
00102 IChronoStatSvc::StatWeight m_se_accumulatedWeight ;
00103 IChronoStatSvc::StatWeight m_se_minimalWeight ;
00104 IChronoStatSvc::StatWeight m_se_maximalWeight ;
00106 };
00108 inline const unsigned long & StatEntity::nEntries () const { return m_se_nEntries ; } ;
00110 inline const IChronoStatSvc::StatFlag & StatEntity::flag () const { return m_se_accumulatedFlag ; } ;
00112 inline const IChronoStatSvc::StatFlag & StatEntity::flag2 () const { return m_se_accumulatedFlag2 ; } ;
00114 inline const IChronoStatSvc::StatFlag StatEntity::flagMean () const
00115 { return ( 0 >= nEntries() ) ? 0 : flag() / nEntries() ; } ;
00117 inline const IChronoStatSvc::StatFlag StatEntity::flagRMS () const
00118 {
00119 IChronoStatSvc::StatFlag Flag = ( 0 >= nEntries() ) ? 0 : flag2() / nEntries() - flagMean() * flagMean() ;
00120 return ( 0 > Flag ) ? 0 : sqrt( Flag );
00121 }
00123 inline const IChronoStatSvc::StatFlag StatEntity::flagMeanErr() const
00124 { return ( 0 >= nEntries() ) ? 0 : flagRMS() / sqrt( (IChronoStatSvc::StatFlag) nEntries() ) ; } ;
00126 inline const IChronoStatSvc::StatFlag & StatEntity::flagMin () const { return m_se_minimalFlag ; } ;
00128 inline const IChronoStatSvc::StatFlag & StatEntity::flagMax () const { return m_se_maximalFlag ; } ;
00130 inline const IChronoStatSvc::StatWeight & StatEntity::weight () const { return m_se_accumulatedWeight; } ;
00132 inline const IChronoStatSvc::StatWeight & StatEntity::weightMin () const { return m_se_minimalWeight ; } ;
00134 inline const IChronoStatSvc::StatWeight & StatEntity::weightMax () const { return m_se_maximalWeight ; } ;
00136 inline const IChronoStatSvc::StatWeight StatEntity::weightHarmonicMean () const
00137 { return ( 0 >= nEntries() || weight() <= 0 ) ? 0 : exp( log( weight() ) / ( IChronoStatSvc::StatWeight) nEntries() ) ; }
00139 inline bool StatEntity::operator< ( const StatEntity& se ) const
00140 {
00143 if( nEntries() < se.nEntries() ) { return true; }
00145 if( nEntries() == se.nEntries() && flag() < se.flag() ) { return true; }
00147 if( nEntries() == se.nEntries() && flag() == se.flag() && weight() < se.weight() ) { return true; };
00149 return false;
00150 }
00152 class StatEntity::ComparePairOfStatEntityAndStatTag
00153 : public std::binary_function < const std::pair<const StatEntity*,const IChronoStatSvc::StatTag*>& ,
00154 const std::pair<const StatEntity*,const IChronoStatSvc::StatTag*>& , bool >
00155 {
00156 public:
00157 inline bool operator() ( const std::pair<const StatEntity*,const IChronoStatSvc::StatTag*>& p1,
00158 const std::pair<const StatEntity*,const IChronoStatSvc::StatTag*>& p2 ) const
00159 {
00160 const StatEntity* se1 = p1.first;
00161 const StatEntity* se2 = p2.first;
00162 return ( 0 == se1 || 0 == se2 ) ? true : (*se1)<(*se2) ;
00163 };
00164
00165 };
00167 inline unsigned long StatEntity::addFlagAndWeight ( const IChronoStatSvc::StatFlag & Flag ,
00168 const IChronoStatSvc::StatWeight& Weight )
00169 {
00170 m_se_accumulatedFlag += Flag ; ;
00171 m_se_minimalFlag = ( m_se_minimalFlag < Flag ) ? m_se_minimalFlag : Flag ;
00172 m_se_maximalFlag = ( m_se_maximalFlag > Flag ) ? m_se_maximalFlag : Flag ;
00173 m_se_accumulatedFlag2 += Flag * Flag ;
00174 m_se_accumulatedWeight *= Weight ;
00175 m_se_minimalWeight = ( m_se_minimalWeight < Weight ) ? m_se_minimalWeight : Weight ;
00176 m_se_maximalWeight = ( m_se_maximalWeight > Weight ) ? m_se_maximalWeight : Weight ;
00177 return ++m_se_nEntries ;
00178 };
00181 inline const std::string StatEntity::stringOutPut() const
00182 {
00186 const int buffer_size = 1024;
00187 char buffer[ buffer_size ] = { 0 , 0 };
00188 std::ostrstream ost( buffer , buffer_size);
00190 ost << " #=" << std::setw(5) << nEntries() ;
00192 if( 0 == flag() && ( 0 == flagRMS() || flagMin() == flagMax() ) ) { }
00193 else
00194 {
00195 ost << " Flag=";
00196 __ADJUST_THE_STREAM__(ost,8,2);
00197 ost << flag();
00198 ost << " Mean=";
00199 __ADJUST_THE_STREAM__(ost,8,2);
00200 ost << flagMean ();
00201 if( 0 != flagRMS() || flagMin() != flagMax() )
00202 {
00203 ost << "+-" ;
00204 __ADJUST_THE_STREAM__(ost,8,2);
00205 ost << flagRMS() ;
00206 ost << " Min=" ;
00207 __ADJUST_THE_STREAM__(ost,8,2);
00208 ost << flagMin() ;
00209 ost << " Max=" ;
00210 __ADJUST_THE_STREAM__(ost,8,2);
00211 ost << flagMax() ;
00212 }
00213
00214 }
00216 if( 1 == weight() && ( weightMin() == weightMax() ) ) { }
00217 else
00218 {
00219 ost << " Weight=" ;
00220 __ADJUST_THE_STREAM__(ost,8,2);
00221 ost << weight() ;
00222 ost << " 'Mean'=" ;
00223 __ADJUST_THE_STREAM__(ost,8,2);
00224 ost << weightHarmonicMean() ;
00225 if( weightMin() != weightMax() )
00226 {
00227 ost << "\tMin=" ;
00228 __ADJUST_THE_STREAM__(ost,8,2);
00229 ost << weightMin() ;
00230 ost << "\tMax=" ;
00231 __ADJUST_THE_STREAM__(ost,8,2);
00232 ost << weightMax() ;
00233 }
00234 }
00238 unsigned int len = strlen(ost.str());
00239 char *resstr = new char[len+1];
00240 strncpy(resstr,ost.str(),len);
00241 resstr[len] = 0;
00242 std::string res( resstr );
00243 delete [] resstr;
00244 return res;
00246 };
00248 #endif // __GAUDI_CHRONOSTATSVC_STATENTITY_H__
00249
00250
00251
00252
00253