The Gaudi Framework  v33r0 (d5ea422b)
Stat.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
12 // Include files
13 // ============================================================================
14 // STD & STL
15 // ============================================================================
16 #include <sstream>
17 // ============================================================================
18 // GaudiKernel
19 // ============================================================================
20 #include "GaudiKernel/IStatSvc.h"
21 #include "GaudiKernel/Stat.h"
22 #include "GaudiKernel/StatEntity.h"
23 // ============================================================================
24 // Boost
25 // ============================================================================
26 #include "boost/format.hpp"
27 // ============================================================================
34 // ============================================================================
35 /* Constructor from IStatSvc,tag and value
36  *
37  * @code
38  *
39  * IStatSvc* statSvc = ... ;
40  * double eTotal = .... ;
41  *
42  * Stat eTot ( statSvc , "total energy" ) ;
43  * eTot += eTotal ;
44  *
45  * @endcode
46  *
47  * @see IStatSvc
48  * @param svc pointer to Chrono&Stat Service
49  * @paran tag unique tag for the entry
50  */
51 // ============================================================================
52 Stat::Stat( IStatSvc* svc, const std::string& tag ) : m_tag( tag ), m_group(), m_stat( svc ) {
53  if ( m_stat ) {
54  // get from the service
55  StatEntity* tmp = m_stat->stat( tag );
56  if ( !tmp ) {
57  // create if needed
58  m_stat->stat( tag, 0 );
59  tmp = m_stat->stat( tag );
60  tmp->reset();
61  }
62  m_entity = tmp;
63  }
64 }
65 // ============================================================================
66 /* Constructor from IStatSvc,tag and value
67  *
68  * @code
69  *
70  * IStatSvc* statSvc = ... ;
71  * double eTotal = .... ;
72  *
73  * Stat stat( statSvc , "total energy" , eTotal ) ;
74  *
75  * @endcode
76  *
77  * @see IStatSvc
78  * @param svc pointer to Chrono&Stat Service
79  * @paran tag unique tag for the entry
80  * @param flag "flag"(additive quantity) to be used
81  */
82 // ============================================================================
83 Stat::Stat( IStatSvc* svc, const std::string& tag, const double flag ) : m_tag( tag ), m_group(), m_stat( svc ) {
84  if ( m_stat ) {
85  m_stat->stat( tag, flag );
86  // get from the service
87  m_entity = m_stat->stat( tag );
88  }
89 }
90 
91 // ============================================================================
92 // representation as string
93 // ============================================================================
96  print( ost );
97  return ost.str();
98 }
99 // ============================================================================
100 /* printout to std::ostream
101  * @param s the reference to the output stream
102  */
103 // ============================================================================
105  if ( m_group.empty() && m_tag.empty() ) { return !m_entity ? ( o << "NULL" ) : ( o << m_entity ); }
106  if ( !m_group.empty() ) {
107  if ( m_entity ) {
108  return o << boost::format( " %|1$15s|::%|2$-15s| %|32t|%3%" ) % ( "\"" + m_group ) % ( m_tag + "\"" ) %
109  ( *m_entity );
110  } else {
111  return o << boost::format( " %|1$15s|::%|2$-15s| %|32t|%NULL%" ) % ( "\"" + m_group ) % ( m_tag + "\"" );
112  }
113  }
114  if ( m_entity ) { return o << boost::format( " %|1$=30s| %|32t|%2%" ) % ( "\"" + m_tag + "\"" ) % ( *m_entity ); }
115  return o << boost::format( " %|1$=30s| %|32t|%NULL%" ) % ( "\"" + m_tag + "\"" );
116 }
117 // ============================================================================
118 // external operator for addition of Stat and a number
119 // ============================================================================
120 Stat operator+( const Stat& stat, const double value ) {
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  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  Stat s( stat );
138  s += value;
139  return s;
140 }
141 // ============================================================================
142 // external operator for addition of Stat and Stat
143 Stat operator+( const Stat& stat, const Stat& value ) {
144  Stat s( stat );
145  s += value;
146  return s;
147 }
148 // ============================================================================
149 // external printout operator to std::ostream
150 std::ostream& operator<<( std::ostream& stream, const Stat& stat ) { return stat.print( stream ); }
151 // ============================================================================
152 
153 // ============================================================================
154 // The END
155 // ============================================================================
T empty(T... args)
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
Small wrapper class for easy manipulation with generic counters and IStatSvc interface.
Definition: Stat.h:56
SmartIF< IStatSvc > m_stat
Stat service.
Definition: Stat.h:256
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:250
Stat operator+(const Stat &stat, const double value)
external operator for addition of Stat and a number
Definition: Stat.cpp:120
void reset()
Definition: StatEntity.h:41
STL class.
std::string m_tag
unique stat tag(name)
Definition: Stat.h:252
Stat(StatEntity *entity=0, const std::string &name="", const std::string &group="")
constructor from StatEntity, name and group :
Definition: Stat.h:74
std::string m_group
Definition: Stat.h:254
T str(T... args)
string s
Definition: gaudirun.py:328
std::ostream & operator<<(std::ostream &stream, const Stat &stat)
external printout operator to std::ostream
Definition: Stat.cpp:150
"Stat"-related part of interface IChronoStatSvc
Definition: IStatSvc.h:35
backward compatible StatEntity class.
Definition: StatEntity.h:18
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: Stat.cpp:104
STL class.
Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition: Stat.cpp:128
std::string toString() const
representation as string
Definition: Stat.cpp:94