All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Stat.cpp
Go to the documentation of this file.
1 // $Id: Stat.cpp,v 1.6 2007/08/06 08:39:38 marcocle Exp $
2 // ============================================================================
3 // Include files
4 // ============================================================================
5 // STD & STL
6 // ============================================================================
7 #include <sstream>
8 // ============================================================================
9 // GaudiKernel
10 // ============================================================================
11 #include "GaudiKernel/IStatSvc.h"
13 #include "GaudiKernel/Stat.h"
14 #include "GaudiKernel/StatEntity.h"
15 // ============================================================================
16 // Boost
17 // ============================================================================
18 #include "boost/format.hpp"
19 // ============================================================================
26 // ============================================================================
27 /* Constructor from IStatSvc,tag and value
28  *
29  * @code
30  *
31  * IStatSvc* statSvc = ... ;
32  * double eTotal = .... ;
33  *
34  * Stat eTot ( statSvc , "total energy" ) ;
35  * eTot += eTotal ;
36  *
37  * @endcode
38  *
39  * @see IStatSvc
40  * @param svc pointer to Chrono&Stat Service
41  * @paran tag unique tag for the entry
42  */
43 // ============================================================================
45  const std::string& tag )
46  : m_entity ( 0 )
47  , m_tag ( tag )
48  , m_group ( )
49  , m_stat ( svc )
50  , m_counter ( 0 )
51 {
52  if ( 0 != m_stat )
53  {
54  m_stat -> addRef() ;
55  // get from the service
56  const StatEntity* tmp = m_stat->stat ( tag ) ;
57  if ( 0 == tmp )
58  {
59  // create if needed
60  m_stat->stat ( tag , 0 ) ;
61  tmp = m_stat->stat ( tag ) ;
62  StatEntity* aux = const_cast<StatEntity*>( tmp );
63  aux->reset () ;
64  }
65  m_entity = const_cast<StatEntity*> ( tmp ) ;
66  }
67 }
68 // ============================================================================
69 /* Constructor from IStatSvc,tag and value
70  *
71  * @code
72  *
73  * IStatSvc* statSvc = ... ;
74  * double eTotal = .... ;
75  *
76  * Stat stat( statSvc , "total energy" , eTotal ) ;
77  *
78  * @endcode
79  *
80  * @see IStatSvc
81  * @param svc pointer to Chrono&Stat Service
82  * @paran tag unique tag for the entry
83  * @param flag "flag"(additive quantity) to be used
84  */
85 // ============================================================================
87  const std::string& tag ,
88  const double flag )
89  : m_entity ( 0 )
90  , m_tag ( tag )
91  , m_group ( )
92  , m_stat ( svc )
93  , m_counter ( 0 )
94 {
95  if ( 0 != m_stat )
96  {
97  m_stat -> addRef() ;
98  m_stat -> stat( tag , flag ) ;
99  // get from the service
100  m_entity = const_cast<StatEntity*>( m_stat -> stat ( tag ) ) ;
101  }
102 }
103 // ============================================================================
104 /* constructor from ICounterSvc, group and name
105  * @see ICounterSvc::get
106  * @see ICounterSvc::create
107  * @param svc pointer to Counter Service
108  * @param group group name
109  * @param name counter name
110  */
111 // ============================================================================
113  const std::string& group ,
114  const std::string& name )
115  : m_entity ( 0 )
116  , m_tag ( name )
117  , m_group ( group )
118  , m_stat ( 0 )
119  , m_counter ( svc )
120 {
121  if ( 0 != m_counter )
122  {
123  m_counter -> addRef() ;
124  // get from the service
125  m_entity = m_counter -> get ( group , name ) ;
126  // create if needed:
127  if ( 0 == m_entity ) { m_counter -> create( group , name , 0 , m_entity ) ; }
128  }
129 }
130 // ============================================================================
131 // copy contructor
132 // ============================================================================
133 Stat::Stat( const Stat& right )
134  : m_entity ( right.m_entity )
135  , m_tag ( right.m_tag )
136  , m_group ( right.m_group )
137  , m_stat ( right.m_stat )
138  , m_counter ( right.m_counter )
139 {
140  if ( 0 != m_stat ) { m_stat -> addRef () ; }
141  if ( 0 != m_counter ) { m_counter -> addRef () ; }
142 }
143 // ============================================================================
144 // Assignement operator
145 // ============================================================================
146 Stat& Stat::operator=( const Stat& right)
147 {
148  if ( this == &right ) { return *this ; }
149  m_entity = right.m_entity ;
150  m_tag = right.m_tag ;
151  m_group = right.m_group ;
152  {
153  IStatSvc* stat= right.m_stat ;
154  if ( 0 != stat ) { stat -> addRef() ; }
155  if ( 0 != m_stat ) { m_stat -> release() ; m_stat = 0 ; }
156  m_stat = stat ;
157  }
158  {
159  ICounterSvc* counter= right.m_counter ;
160  if ( 0 != counter ) { counter -> addRef() ; }
161  if ( 0 != m_counter ) { m_counter -> release() ; m_counter = 0 ; }
162  m_counter = counter ;
163  }
164  return *this ;
165 }
166 // ============================================================================
167 // destructor
168 // ============================================================================
170 {
171  m_entity = 0 ;
172  if ( 0 != m_stat ) { m_stat -> release() ; m_stat = 0 ; }
173  if ( 0 != m_counter ) { m_counter -> release() ; m_counter = 0 ; }
174 }
175 // ============================================================================
176 // representation as string
177 // ============================================================================
178 std::string Stat::toString() const
179 {
180  std::ostringstream ost ;
181  print ( ost ) ;
182  return ost.str () ;
183 }
184 // ============================================================================
185 /* printout to std::ostream
186  * @param s the reference to the output stream
187  */
188 // ============================================================================
189 std::ostream& Stat::print( std::ostream& o ) const
190 {
191  if ( m_group.empty() && m_tag.empty() )
192  { return 0 == m_entity ? ( o << "NULL" ) : ( o << m_entity ) ; }
193  if ( !m_group.empty() )
194  {
195  if ( 0 != m_entity )
196  {
197  return o << boost::format(" %|1$15s|::%|2$-15s| %|32t|%3%")
198  % ( "\"" + m_group ) % ( m_tag + "\"") % (*m_entity) ;
199  }
200  else
201  {
202  return o << boost::format(" %|1$15s|::%|2$-15s| %|32t|%NULL%")
203  % ( "\"" + m_group ) % ( m_tag + "\"") ;
204  }
205  }
206  if ( 0 != m_entity )
207  {
208  return o << boost::format(" %|1$=30s| %|32t|%2%")
209  % ("\"" + m_tag + "\"" ) % (*m_entity) ;
210  }
211  return o << boost::format(" %|1$=30s| %|32t|%NULL%")
212  % ( "\"" + m_tag + "\"" ) ;
213 }
214 // ============================================================================
215 // external operator for addition of Stat and a number
216 // ============================================================================
217 Stat operator+( const Stat& stat , const double value )
218 { Stat s( stat ) ; s += value ; return s ; }
219 // ============================================================================
220 // external operator for subtraction of Stat and a number
221 // ============================================================================
222 Stat operator-( const Stat& stat , const double value )
223 { Stat s( stat ) ; s -= value ; return s ; }
224 // ============================================================================
225 // external operator for addition of Stat and a number
226 // ============================================================================
227 Stat operator+( const double value , const Stat& stat )
228 { Stat s( stat ) ; s += value ; return s ; }
229 // ============================================================================
230 // external operator for addition of Stat and Stat
231 Stat operator+( const Stat& stat , const Stat& value )
232 { Stat s( stat ) ; s += value ; return s ; }
233 // ============================================================================
234 // external printout operator to std::ostream
235 std::ostream& operator<<( std::ostream& stream , const Stat& stat )
236 { return stat.print( stream ) ; }
237 // ============================================================================
238 
239 // ============================================================================
240 // The END
241 // ============================================================================
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133
Small wrapper class for easy manipulation with generic counters and IStatSvc&ICounterSvc interface...
Definition: Stat.h:50
Create / access multi purpose counters.
Definition: ICounterSvc.h:78
StatEntity * counter() const
alternative access to underlying counter (for ICounterSvc::CounterObj)
Definition: Stat.h:282
virtual void stat(const StatTag &t, const StatFlag &f)=0
add statistical information to the entity , tagged by its name
StatEntity * m_entity
underlying counter
Definition: Stat.h:290
Stat operator+(const Stat &stat, const double value)
external operator for addition of Stat and a number
Definition: Stat.cpp:217
void reset()
reset the counters
Definition: StatEntity.cpp:209
const std::string & name() const
counter name
Definition: Stat.h:284
ICounterSvc * m_counter
Counter Service.
Definition: Stat.h:298
std::string m_tag
unique stat tag(name)
Definition: Stat.h:292
Stat(StatEntity *entity=0, const std::string &name="", const std::string &group="")
constructor from StatEntity, name and group :
Definition: Stat.h:69
std::string m_group
Definition: Stat.h:294
IStatSvc * m_stat
Stat service.
Definition: Stat.h:296
~Stat()
destructor
Definition: Stat.cpp:169
std::string toString() const
representation as string
Definition: Stat.cpp:178
string s
Definition: gaudirun.py:210
std::ostream & operator<<(std::ostream &stream, const Stat &stat)
output operator for the counter object
Definition: Stat.cpp:235
"Stat"-related part of interface IChronoStatSvc
Definition: IStatSvc.h:27
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:68
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: Stat.cpp:189
const std::string & group() const
counter group (for ICounterSvc)
Definition: Stat.h:286
Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition: Stat.cpp:222
Stat & operator=(const Stat &right)
Assignement operator.
Definition: Stat.cpp:146