The Gaudi Framework  v30r3 (a5ef0a68)
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  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  tmp->reset();
52  }
53  m_entity = tmp;
54  }
55 }
56 // ============================================================================
57 /* Constructor from IStatSvc,tag and value
58  *
59  * @code
60  *
61  * IStatSvc* statSvc = ... ;
62  * double eTotal = .... ;
63  *
64  * Stat stat( statSvc , "total energy" , eTotal ) ;
65  *
66  * @endcode
67  *
68  * @see IStatSvc
69  * @param svc pointer to Chrono&Stat Service
70  * @paran tag unique tag for the entry
71  * @param flag "flag"(additive quantity) to be used
72  */
73 // ============================================================================
74 Stat::Stat( IStatSvc* svc, const std::string& tag, const double flag ) : m_tag( tag ), m_group(), m_stat( svc )
75 {
76  if ( m_stat ) {
77  m_stat->stat( tag, flag );
78  // get from the service
79  m_entity = m_stat->stat( tag );
80  }
81 }
82 
83 // ============================================================================
84 // representation as string
85 // ============================================================================
87 {
89  print( ost );
90  return ost.str();
91 }
92 // ============================================================================
93 /* printout to std::ostream
94  * @param s the reference to the output stream
95  */
96 // ============================================================================
98 {
99  if ( m_group.empty() && m_tag.empty() ) {
100  return !m_entity ? ( o << "NULL" ) : ( o << m_entity );
101  }
102  if ( !m_group.empty() ) {
103  if ( m_entity ) {
104  return o << boost::format( " %|1$15s|::%|2$-15s| %|32t|%3%" ) % ( "\"" + m_group ) % ( m_tag + "\"" ) %
105  ( *m_entity );
106  } else {
107  return o << boost::format( " %|1$15s|::%|2$-15s| %|32t|%NULL%" ) % ( "\"" + m_group ) % ( m_tag + "\"" );
108  }
109  }
110  if ( m_entity ) {
111  return o << boost::format( " %|1$=30s| %|32t|%2%" ) % ( "\"" + m_tag + "\"" ) % ( *m_entity );
112  }
113  return o << boost::format( " %|1$=30s| %|32t|%NULL%" ) % ( "\"" + m_tag + "\"" );
114 }
115 // ============================================================================
116 // external operator for addition of Stat and a number
117 // ============================================================================
118 Stat operator+( const Stat& stat, const double value )
119 {
120  Stat s( stat );
121  s += value;
122  return s;
123 }
124 // ============================================================================
125 // external operator for subtraction of Stat and a number
126 // ============================================================================
127 Stat operator-( const Stat& stat, const double value )
128 {
129  Stat s( stat );
130  s -= value;
131  return s;
132 }
133 // ============================================================================
134 // external operator for addition of Stat and a number
135 // ============================================================================
136 Stat operator+( const double value, const Stat& stat )
137 {
138  Stat s( stat );
139  s += value;
140  return s;
141 }
142 // ============================================================================
143 // external operator for addition of Stat and Stat
144 Stat operator+( const Stat& stat, const Stat& value )
145 {
146  Stat s( stat );
147  s += value;
148  return s;
149 }
150 // ============================================================================
151 // external printout operator to std::ostream
152 std::ostream& operator<<( std::ostream& stream, const Stat& stat ) { return stat.print( stream ); }
153 // ============================================================================
154 
155 // ============================================================================
156 // The END
157 // ============================================================================
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:46
SmartIF< IStatSvc > m_stat
Stat service.
Definition: Stat.h:275
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:269
Stat operator+(const Stat &stat, const double value)
external operator for addition of Stat and a number
Definition: Stat.cpp:118
void reset()
Definition: Counters.h:803
STL class.
std::string m_tag
unique stat tag(name)
Definition: Stat.h:271
Stat(StatEntity *entity=0, const std::string &name="", const std::string &group="")
constructor from StatEntity, name and group :
Definition: Stat.h:65
std::string m_group
Definition: Stat.h:273
std::string toString() const
representation as string
Definition: Stat.cpp:86
string s
Definition: gaudirun.py:253
std::ostream & operator<<(std::ostream &stream, const Stat &stat)
external printout operator to std::ostream
Definition: Stat.cpp:152
"Stat"-related part of interface IChronoStatSvc
Definition: IStatSvc.h:25
backward compatible StatEntity class.
Definition: Counters.h:777
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: Stat.cpp:97
STL class.
Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition: Stat.cpp:127