The Gaudi Framework  master (37c0b60a)
Stat.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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/IStatSvc.h>
12 #include <GaudiKernel/Stat.h>
13 #include <GaudiKernel/StatEntity.h>
14 #include <fmt/format.h>
15 #include <fmt/ostream.h>
16 #include <sstream>
17 // ============================================================================
25 template <>
26 struct fmt::formatter<StatEntity> : fmt::ostream_formatter {};
27 
28 // ============================================================================
29 /* Constructor from IStatSvc,tag and value
30  *
31  * @code
32  *
33  * IStatSvc* statSvc = ... ;
34  * double eTotal = .... ;
35  *
36  * Stat eTot ( statSvc , "total energy" ) ;
37  * eTot += eTotal ;
38  *
39  * @endcode
40  *
41  * @see IStatSvc
42  * @param svc pointer to Chrono&Stat Service
43  * @paran tag unique tag for the entry
44  */
45 // ============================================================================
46 Stat::Stat( IStatSvc* svc, const std::string& tag ) : m_tag( tag ), m_group(), m_stat( svc ) {
47  if ( m_stat ) {
48  // get from the service
49  StatEntity* tmp = m_stat->stat( tag );
50  if ( !tmp ) {
51  // create if needed
52  m_stat->stat( tag, 0 );
53  tmp = m_stat->stat( tag );
54  tmp->reset();
55  }
56  m_entity = tmp;
57  }
58 }
59 // ============================================================================
60 /* Constructor from IStatSvc,tag and value
61  *
62  * @code
63  *
64  * IStatSvc* statSvc = ... ;
65  * double eTotal = .... ;
66  *
67  * Stat stat( statSvc , "total energy" , eTotal ) ;
68  *
69  * @endcode
70  *
71  * @see IStatSvc
72  * @param svc pointer to Chrono&Stat Service
73  * @paran tag unique tag for the entry
74  * @param flag "flag"(additive quantity) to be used
75  */
76 // ============================================================================
77 Stat::Stat( IStatSvc* svc, const std::string& tag, const double flag ) : m_tag( tag ), m_group(), m_stat( svc ) {
78  if ( m_stat ) {
79  m_stat->stat( tag, flag );
80  // get from the service
81  m_entity = m_stat->stat( tag );
82  }
83 }
84 
85 // ============================================================================
86 // representation as string
87 // ============================================================================
90  print( ost );
91  return ost.str();
92 }
93 // ============================================================================
94 /* printout to std::ostream
95  * @param o the reference to the output stream
96  */
97 // ============================================================================
99  auto entity = ( m_entity ) ? fmt::format( "{}", *m_entity ) : "NULL";
100  std::string tag;
101  if ( !m_tag.empty() ) {
102  if ( !m_group.empty() ) {
103  tag = fmt::format( "\"{}::{}\"", m_group, m_tag );
104  } else {
105  tag = fmt::format( "\"{}\"", m_tag );
106  }
107  }
108  return o << fmt::format( " {:^30} {}", tag, entity );
109 }
110 // ============================================================================
111 // external operator for addition of Stat and a number
112 // ============================================================================
113 Stat operator+( const Stat& stat, const double value ) {
114  Stat s( stat );
115  s += value;
116  return s;
117 }
118 // ============================================================================
119 // external operator for subtraction of Stat and a number
120 // ============================================================================
121 Stat operator-( const Stat& stat, const double value ) {
122  Stat s( stat );
123  s -= value;
124  return s;
125 }
126 // ============================================================================
127 // external operator for addition of Stat and a number
128 // ============================================================================
129 Stat operator+( const double value, const Stat& stat ) {
130  Stat s( stat );
131  s += value;
132  return s;
133 }
134 // ============================================================================
135 // external operator for addition of Stat and Stat
136 Stat operator+( const Stat& stat, const Stat& value ) {
137  Stat s( stat );
138  s += value;
139  return s;
140 }
141 // ============================================================================
142 // external printout operator to std::ostream
143 std::ostream& operator<<( std::ostream& stream, const Stat& stat ) { return stat.print( stream ); }
144 // ============================================================================
145 
146 // ============================================================================
147 // The END
148 // ============================================================================
Stat::print
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition: Stat.cpp:98
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:113
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:143
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:121
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:88