The Gaudi Framework  v38r1p1 (ae26267b)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #include "GaudiKernel/Stat.h"
12 #include "GaudiKernel/IStatSvc.h"
13 #include "GaudiKernel/StatEntity.h"
14 #include <fmt/format.h>
15 #include <fmt/ostream.h>
16 #include <sstream>
17 // ============================================================================
25 #if FMT_VERSION >= 90000
26 template <>
27 struct fmt::formatter<StatEntity> : fmt::ostream_formatter {};
28 #endif
29 
30 // ============================================================================
31 /* Constructor from IStatSvc,tag and value
32  *
33  * @code
34  *
35  * IStatSvc* statSvc = ... ;
36  * double eTotal = .... ;
37  *
38  * Stat eTot ( statSvc , "total energy" ) ;
39  * eTot += eTotal ;
40  *
41  * @endcode
42  *
43  * @see IStatSvc
44  * @param svc pointer to Chrono&Stat Service
45  * @paran tag unique tag for the entry
46  */
47 // ============================================================================
48 Stat::Stat( IStatSvc* svc, const std::string& tag ) : m_tag( tag ), m_group(), m_stat( svc ) {
49  if ( m_stat ) {
50  // get from the service
51  StatEntity* tmp = m_stat->stat( tag );
52  if ( !tmp ) {
53  // create if needed
54  m_stat->stat( tag, 0 );
55  tmp = m_stat->stat( tag );
56  tmp->reset();
57  }
58  m_entity = tmp;
59  }
60 }
61 // ============================================================================
62 /* Constructor from IStatSvc,tag and value
63  *
64  * @code
65  *
66  * IStatSvc* statSvc = ... ;
67  * double eTotal = .... ;
68  *
69  * Stat stat( statSvc , "total energy" , eTotal ) ;
70  *
71  * @endcode
72  *
73  * @see IStatSvc
74  * @param svc pointer to Chrono&Stat Service
75  * @paran tag unique tag for the entry
76  * @param flag "flag"(additive quantity) to be used
77  */
78 // ============================================================================
79 Stat::Stat( IStatSvc* svc, const std::string& tag, const double flag ) : m_tag( tag ), m_group(), m_stat( svc ) {
80  if ( m_stat ) {
81  m_stat->stat( tag, flag );
82  // get from the service
83  m_entity = m_stat->stat( tag );
84  }
85 }
86 
87 // ============================================================================
88 // representation as string
89 // ============================================================================
92  print( ost );
93  return ost.str();
94 }
95 // ============================================================================
96 /* printout to std::ostream
97  * @param o the reference to the output stream
98  */
99 // ============================================================================
101  auto entity = ( m_entity ) ? fmt::format( "{}", *m_entity ) : "NULL";
102  std::string tag;
103  if ( !m_tag.empty() ) {
104  if ( !m_group.empty() ) {
105  tag = fmt::format( "\"{}::{}\"", m_group, m_tag );
106  } else {
107  tag = fmt::format( "\"{}\"", m_tag );
108  }
109  }
110  return o << fmt::format( " {:^30} {}", tag, entity );
111 }
112 // ============================================================================
113 // external operator for addition of Stat and a number
114 // ============================================================================
115 Stat operator+( const Stat& stat, const double value ) {
116  Stat s( stat );
117  s += value;
118  return s;
119 }
120 // ============================================================================
121 // external operator for subtraction of Stat and a number
122 // ============================================================================
123 Stat operator-( const Stat& stat, const double value ) {
124  Stat s( stat );
125  s -= value;
126  return s;
127 }
128 // ============================================================================
129 // external operator for addition of Stat and a number
130 // ============================================================================
131 Stat operator+( const double value, const Stat& stat ) {
132  Stat s( stat );
133  s += value;
134  return s;
135 }
136 // ============================================================================
137 // external operator for addition of Stat and Stat
138 Stat operator+( const Stat& stat, const Stat& value ) {
139  Stat s( stat );
140  s += value;
141  return s;
142 }
143 // ============================================================================
144 // external printout operator to std::ostream
145 std::ostream& operator<<( std::ostream& stream, const Stat& stat ) { return stat.print( stream ); }
146 // ============================================================================
147 
148 // ============================================================================
149 // The END
150 // ============================================================================
Stat::print
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: Stat.cpp:100
Write.stream
stream
Definition: Write.py:32
std::string
STL class.
operator+
Stat operator+(const Stat &stat, const double value)
external operator for addition of Stat and a number
Definition: Stat.cpp:115
gaudirun.s
string s
Definition: gaudirun.py:346
operator<<
std::ostream & operator<<(std::ostream &stream, const Stat &stat)
external printout operator to std::ostream
Definition: Stat.cpp:145
StatEntity
backward compatible StatEntity class.
Definition: StatEntity.h:23
StatEntity.h
Stat::entity
const StatEntity * entity() const
get the entity
Definition: Stat.h:137
Stat::m_stat
SmartIF< IStatSvc > m_stat
Stat service.
Definition: Stat.h:256
IStatSvc
Definition: IStatSvc.h:35
Stat::m_group
std::string m_group
Definition: Stat.h:254
Stat::m_tag
std::string m_tag
unique stat tag(name)
Definition: Stat.h:252
Stat::m_entity
StatEntity * m_entity
underlying counter
Definition: Stat.h:250
operator-
Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition: Stat.cpp:123
std::ostream
STL class.
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
Stat::Stat
Stat(StatEntity *entity=0, const std::string &name="", const std::string &group="")
constructor from StatEntity, name and group :
Definition: Stat.h:74
IStatSvc.h
Stat.h
Stat
Definition: Stat.h:56
std::ostringstream
STL class.
StatEntity::reset
friend void reset(StatEntity &s)
Definition: StatEntity.h:40
std::string::empty
T empty(T... args)
std::ostringstream::str
T str(T... args)
Stat::toString
std::string toString() const
representation as string
Definition: Stat.cpp:90