The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
Stat.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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\***********************************************************************************/
12#include <GaudiKernel/Stat.h>
14#include <fmt/format.h>
15#include <fmt/ostream.h>
16#include <sstream>
17
24
25template <>
26struct fmt::formatter<StatEntity> : fmt::ostream_formatter {};
27
28Stat::Stat( IStatSvc* svc, const std::string& tag ) : m_tag( tag ), m_group(), m_stat( svc ) {
29 if ( m_stat ) {
30 // get from the service
31 StatEntity* tmp = m_stat->stat( tag );
32 if ( !tmp ) {
33 // create if needed
34 m_stat->stat( tag, 0 );
35 tmp = m_stat->stat( tag );
36 tmp->reset();
37 }
38 m_entity = tmp;
39 }
40}
41
42Stat::Stat( IStatSvc* svc, const std::string& tag, const double flag ) : m_tag( tag ), m_group(), m_stat( svc ) {
43 if ( m_stat ) {
44 m_stat->stat( tag, flag );
45 // get from the service
46 m_entity = m_stat->stat( tag );
47 }
48}
49
50std::string Stat::toString() const {
51 std::ostringstream ost;
52 print( ost );
53 return ost.str();
54}
55
56std::ostream& Stat::print( std::ostream& o ) const {
57 auto entity = ( m_entity ) ? fmt::format( "{}", *m_entity ) : "NULL";
58 std::string tag;
59 if ( !m_tag.empty() ) {
60 if ( !m_group.empty() ) {
61 tag = fmt::format( "\"{}::{}\"", m_group, m_tag );
62 } else {
63 tag = fmt::format( "\"{}\"", m_tag );
64 }
65 }
66 return o << fmt::format( " {:^30} {}", tag, entity );
67}
68
69Stat operator+( const Stat& stat, const double value ) {
70 Stat s( stat );
71 s += value;
72 return s;
73}
74
75Stat operator-( const Stat& stat, const double value ) {
76 Stat s( stat );
77 s -= value;
78 return s;
79}
80
81Stat operator+( const double value, const Stat& stat ) {
82 Stat s( stat );
83 s += value;
84 return s;
85}
86
87Stat operator+( const Stat& stat, const Stat& value ) {
88 Stat s( stat );
89 s += value;
90 return s;
91}
92
93std::ostream& operator<<( std::ostream& stream, const Stat& stat ) { return stat.print( stream ); }
std::ostream & operator<<(std::ostream &stream, const Stat &stat)
external printout operator to std::ostream
Definition Stat.cpp:93
Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition Stat.cpp:75
Stat operator+(const Stat &stat, const double value)
external operator for addition of Stat and a number
Definition Stat.cpp:69
"Stat"-related part of interface IChronoStatSvc
Definition IStatSvc.h:25
backward compatible StatEntity class.
Definition StatEntity.h:23
friend void reset(StatEntity &s)
Definition StatEntity.h:40
Small wrapper class for easy manipulation with generic counters and IStatSvc interface.
Definition Stat.h:48
std::string m_tag
unique stat tag(name)
Definition Stat.h:240
Stat(StatEntity *entity=0, const std::string &name="", const std::string &group="")
constructor from StatEntity, name and group :
Definition Stat.h:66
std::string toString() const
representation as string
Definition Stat.cpp:50
const StatEntity * entity() const
get the entity
Definition Stat.h:128
StatEntity * m_entity
underlying counter
Definition Stat.h:238
std::ostream & print(std::ostream &o=std::cout) const
printout to std::ostream
Definition Stat.cpp:56
SmartIF< IStatSvc > m_stat
Stat service.
Definition Stat.h:244
std::string m_group
Definition Stat.h:242