The Gaudi Framework  master (b9786168)
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 <format>
15#include <iostream>
16
23
24Stat::Stat( IStatSvc* svc, const std::string& tag ) : m_tag( tag ), m_group(), m_stat( svc ) {
25 if ( m_stat ) {
26 // get from the service
27 StatEntity* tmp = m_stat->stat( tag );
28 if ( !tmp ) {
29 // create if needed
30 m_stat->stat( tag, 0 );
31 tmp = m_stat->stat( tag );
32 tmp->reset();
33 }
34 m_entity = tmp;
35 }
36}
37
38Stat::Stat( IStatSvc* svc, const std::string& tag, const double flag ) : m_tag( tag ), m_group(), m_stat( svc ) {
39 if ( m_stat ) {
40 m_stat->stat( tag, flag );
41 // get from the service
42 m_entity = m_stat->stat( tag );
43 }
44}
45
46std::string Stat::toString() const {
47 std::ostringstream ost;
48 print( ost );
49 return ost.str();
50}
51
52std::ostream& Stat::print( std::ostream& o ) const {
53 auto entity = ( m_entity ) ? m_entity->toString() : "NULL";
54 std::string tag;
55 if ( !m_tag.empty() ) {
56 if ( !m_group.empty() ) {
57 tag = std::format( "\"{}::{}\"", m_group, m_tag );
58 } else {
59 tag = std::format( "\"{}\"", m_tag );
60 }
61 }
62 return o << std::format( " {:^30} {}", tag, entity );
63}
64
65Stat operator+( const Stat& stat, const double value ) {
66 Stat s( stat );
67 s += value;
68 return s;
69}
70
71Stat operator-( const Stat& stat, const double value ) {
72 Stat s( stat );
73 s -= value;
74 return s;
75}
76
77Stat operator+( const double value, const Stat& stat ) {
78 Stat s( stat );
79 s += value;
80 return s;
81}
82
83Stat operator+( const Stat& stat, const Stat& value ) {
84 Stat s( stat );
85 s += value;
86 return s;
87}
88
89std::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:89
Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition Stat.cpp:71
Stat operator+(const Stat &stat, const double value)
external operator for addition of Stat and a number
Definition Stat.cpp:65
"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:46
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:52
SmartIF< IStatSvc > m_stat
Stat service.
Definition Stat.h:244
std::string m_group
Definition Stat.h:242