![]() |
|
|
Generated: 24 Nov 2008 |
00001 // $Id: Stat.cpp,v 1.6 2007/08/06 08:39:38 marcocle Exp $ 00002 // ============================================================================ 00003 // Include files 00004 // ============================================================================ 00005 // STD & STL 00006 // ============================================================================ 00007 #include <sstream> 00008 // ============================================================================ 00009 // GaudiKernel 00010 // ============================================================================ 00011 #include "GaudiKernel/IStatSvc.h" 00012 #include "GaudiKernel/ICounterSvc.h" 00013 #include "GaudiKernel/Stat.h" 00014 #include "GaudiKernel/StatEntity.h" 00015 // ============================================================================ 00016 // Boost 00017 // ============================================================================ 00018 #include "boost/format.hpp" 00019 // ============================================================================ 00026 // ============================================================================ 00027 /* Constructor from IStatSvc,tag and value 00028 * 00029 * @code 00030 * 00031 * IStatSvc* statSvc = ... ; 00032 * double eTotal = .... ; 00033 * 00034 * Stat eTot ( statSvc , "total energy" ) ; 00035 * eTot += eTotal ; 00036 * 00037 * @endcode 00038 * 00039 * @see IStatSvc 00040 * @param svc pointer to Chrono&Stat Service 00041 * @paran tag unique tag for the entry 00042 */ 00043 // ============================================================================ 00044 Stat::Stat ( IStatSvc* svc , 00045 const std::string& tag ) 00046 : m_entity ( 0 ) 00047 , m_tag ( tag ) 00048 , m_group ( ) 00049 , m_stat ( svc ) 00050 , m_counter ( 0 ) 00051 { 00052 if ( 0 != m_stat ) 00053 { 00054 m_stat -> addRef() ; 00055 // get from the service 00056 const StatEntity* tmp = m_stat->stat ( tag ) ; 00057 if ( 0 == tmp ) 00058 { 00059 // create if needed 00060 m_stat->stat ( tag , 0 ) ; 00061 tmp = m_stat->stat ( tag ) ; 00062 StatEntity* aux = const_cast<StatEntity*>( tmp ); 00063 aux->reset () ; 00064 } 00065 m_entity = const_cast<StatEntity*> ( tmp ) ; 00066 } 00067 } 00068 // ============================================================================ 00069 /* Constructor from IStatSvc,tag and value 00070 * 00071 * @code 00072 * 00073 * IStatSvc* statSvc = ... ; 00074 * double eTotal = .... ; 00075 * 00076 * Stat stat( statSvc , "total energy" , eTotal ) ; 00077 * 00078 * @endcode 00079 * 00080 * @see IStatSvc 00081 * @param svc pointer to Chrono&Stat Service 00082 * @paran tag unique tag for the entry 00083 * @param flag "flag"(additive quantity) to be used 00084 */ 00085 // ============================================================================ 00086 Stat::Stat ( IStatSvc* svc , 00087 const std::string& tag , 00088 const double flag ) 00089 : m_entity ( 0 ) 00090 , m_tag ( tag ) 00091 , m_group ( ) 00092 , m_stat ( svc ) 00093 , m_counter ( 0 ) 00094 { 00095 if ( 0 != m_stat ) 00096 { 00097 m_stat -> addRef() ; 00098 m_stat -> stat( tag , flag ) ; 00099 // get from the service 00100 m_entity = const_cast<StatEntity*>( m_stat -> stat ( tag ) ) ; 00101 } 00102 } 00103 // ============================================================================ 00104 /* constructor from ICounterSvc, group and name 00105 * @see ICounterSvc::get 00106 * @see ICounterSvc::create 00107 * @param svc pointer to Counter Service 00108 * @param group group name 00109 * @param name counter name 00110 */ 00111 // ============================================================================ 00112 Stat::Stat ( ICounterSvc* svc , 00113 const std::string& group , 00114 const std::string& name ) 00115 : m_entity ( 0 ) 00116 , m_tag ( name ) 00117 , m_group ( group ) 00118 , m_stat ( 0 ) 00119 , m_counter ( svc ) 00120 { 00121 if ( 0 != m_counter ) 00122 { 00123 m_counter -> addRef() ; 00124 // get from the service 00125 m_entity = m_counter -> get ( group , name ) ; 00126 // create if needed: 00127 if ( 0 == m_entity ) { m_counter -> create( group , name , 0 , m_entity ) ; } 00128 } 00129 } 00130 // ============================================================================ 00131 // copy contructor 00132 // ============================================================================ 00133 Stat::Stat( const Stat& right ) 00134 : m_entity ( right.m_entity ) 00135 , m_tag ( right.m_tag ) 00136 , m_group ( right.m_group ) 00137 , m_stat ( right.m_stat ) 00138 , m_counter ( right.m_counter ) 00139 { 00140 if ( 0 != m_stat ) { m_stat -> addRef () ; } 00141 if ( 0 != m_counter ) { m_counter -> addRef () ; } 00142 } 00143 // ============================================================================ 00144 // Assignement operator 00145 // ============================================================================ 00146 Stat& Stat::operator=( const Stat& right) 00147 { 00148 if ( this == &right ) { return *this ; } 00149 m_entity = right.m_entity ; 00150 m_tag = right.m_tag ; 00151 m_group = right.m_group ; 00152 { 00153 IStatSvc* stat= right.m_stat ; 00154 if ( 0 != stat ) { stat -> addRef() ; } 00155 if ( 0 != m_stat ) { m_stat -> release() ; m_stat = 0 ; } 00156 m_stat = stat ; 00157 } 00158 { 00159 ICounterSvc* counter= right.m_counter ; 00160 if ( 0 != counter ) { counter -> addRef() ; } 00161 if ( 0 != m_counter ) { m_counter -> release() ; m_counter = 0 ; } 00162 m_counter = counter ; 00163 } 00164 return *this ; 00165 } 00166 // ============================================================================ 00167 // destructor 00168 // ============================================================================ 00169 Stat::~Stat() 00170 { 00171 m_entity = 0 ; 00172 if ( 0 != m_stat ) { m_stat -> release() ; m_stat = 0 ; } 00173 if ( 0 != m_counter ) { m_counter -> release() ; m_counter = 0 ; } 00174 } 00175 // ============================================================================ 00176 // representation as string 00177 // ============================================================================ 00178 std::string Stat::toString() const 00179 { 00180 std::ostringstream ost ; 00181 print ( ost ) ; 00182 return ost.str () ; 00183 } 00184 // ============================================================================ 00185 /* printout to std::ostream 00186 * @param s the reference to the output stream 00187 */ 00188 // ============================================================================ 00189 std::ostream& Stat::print( std::ostream& o ) const 00190 { 00191 if ( m_group.empty() && m_tag.empty() ) 00192 { return 0 == m_entity ? ( o << "NULL" ) : ( o << m_entity ) ; } 00193 if ( !m_group.empty() ) 00194 { 00195 if ( 0 != m_entity ) 00196 { 00197 return o << boost::format(" %|1$15s|::%|2$-15s| %|32t|%3%") 00198 % ( "\"" + m_group ) % ( m_tag + "\"") % (*m_entity) ; 00199 } 00200 else 00201 { 00202 return o << boost::format(" %|1$15s|::%|2$-15s| %|32t|%NULL%") 00203 % ( "\"" + m_group ) % ( m_tag + "\"") ; 00204 } 00205 } 00206 if ( 0 != m_entity ) 00207 { 00208 return o << boost::format(" %|1$=30s| %|32t|%2%") 00209 % ("\"" + m_tag + "\"" ) % (*m_entity) ; 00210 } 00211 return o << boost::format(" %|1$=30s| %|32t|%NULL%") 00212 % ( "\"" + m_tag + "\"" ) ; 00213 } 00214 // ============================================================================ 00215 // external operator for addition of Stat and a number 00216 // ============================================================================ 00217 Stat operator+( const Stat& stat , const double value ) 00218 { Stat s( stat ) ; s += value ; return s ; } 00219 // ============================================================================ 00220 // external operator for subtraction of Stat and a number 00221 // ============================================================================ 00222 Stat operator-( const Stat& stat , const double value ) 00223 { Stat s( stat ) ; s -= value ; return s ; } 00224 // ============================================================================ 00225 // external operator for addition of Stat and a number 00226 // ============================================================================ 00227 Stat operator+( const double value , const Stat& stat ) 00228 { Stat s( stat ) ; s += value ; return s ; } 00229 // ============================================================================ 00230 // external operator for addition of Stat and Stat 00231 Stat operator+( const Stat& stat , const Stat& value ) 00232 { Stat s( stat ) ; s += value ; return s ; } 00233 // ============================================================================ 00234 // external printout operator to std::ostream 00235 std::ostream& operator<<( std::ostream& stream , const Stat& stat ) 00236 { return stat.print( stream ) ; } 00237 // ============================================================================ 00238 00239 // ============================================================================ 00240 // The END 00241 // ============================================================================