The Gaudi Framework  v30r2 (9eca68f7)
Stat.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // STD & STL
5 // ============================================================================
6 #include <sstream>
7 // ============================================================================
8 // GaudiKernel
9 // ============================================================================
10 #include "GaudiKernel/IStatSvc.h"
11 #include "GaudiKernel/Stat.h"
12 #include "GaudiKernel/StatEntity.h"
13 // ============================================================================
14 // Boost
15 // ============================================================================
16 #include "boost/format.hpp"
17 // ============================================================================
24 // ============================================================================
25 /* Constructor from IStatSvc,tag and value
26  *
27  * @code
28  *
29  * IStatSvc* statSvc = ... ;
30  * double eTotal = .... ;
31  *
32  * Stat eTot ( statSvc , "total energy" ) ;
33  * eTot += eTotal ;
34  *
35  * @endcode
36  *
37  * @see IStatSvc
38  * @param svc pointer to Chrono&Stat Service
39  * @paran tag unique tag for the entry
40  */
41 // ============================================================================
42 Stat::Stat( IStatSvc* svc, const std::string& tag ) : m_tag( tag ), m_group(), m_stat( svc )
43 {
44  if ( m_stat ) {
45  // get from the service
46  const StatEntity* tmp = m_stat->stat( tag );
47  if ( !tmp ) {
48  // create if needed
49  m_stat->stat( tag, 0 );
50  tmp = m_stat->stat( tag );
51  StatEntity* aux = const_cast<StatEntity*>( tmp );
52  aux->reset();
53  }
54  m_entity = const_cast<StatEntity*>( tmp );
55  }
56 }
57 // ============================================================================
58 /* Constructor from IStatSvc,tag and value
59  *
60  * @code
61  *
62  * IStatSvc* statSvc = ... ;
63  * double eTotal = .... ;
64  *
65  * Stat stat( statSvc , "total energy" , eTotal ) ;
66  *
67  * @endcode
68  *
69  * @see IStatSvc
70  * @param svc pointer to Chrono&Stat Service
71  * @paran tag unique tag for the entry
72  * @param flag "flag"(additive quantity) to be used
73  */
74 // ============================================================================
75 Stat::Stat( IStatSvc* svc, const std::string& tag, const double flag ) : m_tag( tag ), m_group(), m_stat( svc )
76 {
77  if ( m_stat ) {
78  m_stat->stat( tag, flag );
79  // get from the service
80  m_entity = const_cast<StatEntity*>( m_stat->stat( tag ) );
81  }
82 }
83 
84 // ============================================================================
85 // representation as string
86 // ============================================================================
88 {
90  print( ost );
91  return ost.str();
92 }
93 // ============================================================================
94 /* printout to std::ostream
95  * @param s the reference to the output stream
96  */
97 // ============================================================================
99 {
100  if ( m_group.empty() && m_tag.empty() ) {
101  return !m_entity ? ( o << "NULL" ) : ( o << m_entity );
102  }
103  if ( !m_group.empty() ) {
104  if ( m_entity ) {
105  return o << boost::format( " %|1$15s|::%|2$-15s| %|32t|%3%" ) % ( "\"" + m_group ) % ( m_tag + "\"" ) %
106  ( *m_entity );
107  } else {
108  return o << boost::format( " %|1$15s|::%|2$-15s| %|32t|%NULL%" ) % ( "\"" + m_group ) % ( m_tag + "\"" );
109  }
110  }
111  if ( m_entity ) {
112  return o << boost::format( " %|1$=30s| %|32t|%2%" ) % ( "\"" + m_tag + "\"" ) % ( *m_entity );
113  }
114  return o << boost::format( " %|1$=30s| %|32t|%NULL%" ) % ( "\"" + m_tag + "\"" );
115 }
116 // ============================================================================
117 // external operator for addition of Stat and a number
118 // ============================================================================
119 Stat operator+( const Stat& stat, const double value )
120 {
121  Stat s( stat );
122  s += value;
123  return s;
124 }
125 // ============================================================================
126 // external operator for subtraction of Stat and a number
127 // ============================================================================
128 Stat operator-( const Stat& stat, const double value )
129 {
130  Stat s( stat );
131  s -= value;
132  return s;
133 }
134 // ============================================================================
135 // external operator for addition of Stat and a number
136 // ============================================================================
137 Stat operator+( const double value, const Stat& stat )
138 {
139  Stat s( stat );
140  s += value;
141  return s;
142 }
143 // ============================================================================
144 // external operator for addition of Stat and Stat
145 Stat operator+( const Stat& stat, const Stat& value )
146 {
147  Stat s( stat );
148  s += value;
149  return s;
150 }
151 // ============================================================================
152 // external printout operator to std::ostream
153 std::ostream& operator<<( std::ostream& stream, const Stat& stat ) { return stat.print( stream ); }
154 // ============================================================================
155 
156 // ============================================================================
157 // The END
158 // ============================================================================
T empty(T...args)
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
Small wrapper class for easy manipulation with generic counters and IStatSvc interface.
Definition: Stat.h:45
SmartIF< IStatSvc > m_stat
Stat service.
Definition: Stat.h:274
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:268
Stat operator+(const Stat &stat, const double value)
external operator for addition of Stat and a number
Definition: Stat.cpp:119
void reset()
reset the counters
Definition: StatEntity.cpp:207
STL class.
std::string m_tag
unique stat tag(name)
Definition: Stat.h:270
Stat(StatEntity *entity=0, const std::string &name="", const std::string &group="")
constructor from StatEntity, name and group :
Definition: Stat.h:64
std::string m_group
Definition: Stat.h:272
std::string toString() const
representation as string
Definition: Stat.cpp:87
string s
Definition: gaudirun.py:253
std::ostream & operator<<(std::ostream &stream, const Stat &stat)
external printout operator to std::ostream
Definition: Stat.cpp:153
"Stat"-related part of interface IChronoStatSvc
Definition: IStatSvc.h:25
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:65
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: Stat.cpp:98
STL class.
Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition: Stat.cpp:128