11 #include "GaudiKernel/ICounterSvc.h"
12 #include "GaudiKernel/ISvcLocator.h"
13 #include "GaudiKernel/MsgStream.h"
14 #include "GaudiKernel/Service.h"
15 #include "GaudiKernel/HashMap.h"
16 #include "GaudiKernel/Stat.h"
34 ,
m_header (
" Counter :: Group | # | sum | mean/eff^* | rms/err^* | min | max |")
36 ,
m_format1 (
" %|15.15s|%|-15.15s|%|32t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |" )
38 ,
m_format2 (
"*%|15.15s|%|-15.15s|%|32t||%|10d| |%|11.5g| |(%|#9.7g| +- %|-#9.7g|)%%| ------- | ------- |" )
43 declareProperty (
"PrintStat" ,
m_print ) ;
47 "The header row for the output Stat-table" ) ;
51 "The format for the regular row in the output Stat-table" ) ;
55 "The format for the regular row in the outptu Stat-table" ) ;
59 "Use the special format for printout of efficiency counters" ) ;
80 (
const std::string& group ,
81 const std::string&
name )
const;
97 (
const std::string& group ,
98 const std::string&
name ,
100 Counter*& refpCounter ) ;
112 virtual CountObject
create
113 (
const std::string& group ,
114 const std::string&
name ,
126 (
const std::string& group ,
127 const std::string&
name ) ;
134 (
const std::string& group ) ;
145 (
const std::string& group,
146 const std::string&
name,
147 Printout& printer)
const;
156 (
const std::string& group,
157 Printout& printer)
const;
165 (
const Counter* pCounter,
166 Printout& printer)
const;
174 (
const CountObject& pCounter,
175 Printout& printer)
const;
183 const Counter* pCounter)
const ;
186 inline std::pair<std::string,std::string>
_find (
const Counter*
c )
const
190 auto j = std::find_if(
i.second.begin(),
i.second.end(),
192 return k.second ==
c;
194 if (j!=
i.second.end())
return {
i.first, j->first } ;
200 inline size_t num ()
const
204 return r + std::count_if(
i.second.begin(),
i.second.end(),
210 void print ()
const ;
235 ( const
std::
string& grp ,
236 const
std::
string& nam )
const
238 auto i = m_counts.find ( grp ) ;
239 if ( m_counts.end() ==
i ) {
return nullptr ; }
240 auto j =
i->second.find ( nam ) ;
241 if (
i->second.end() == j ) {
return nullptr ; }
252 std::transform(
i->second.begin(),
i->second.end(),
253 std::back_inserter(result),
255 return CountObject{ j.second,
i->first, j.first };
265 #pragma warning(push)
266 #pragma warning(disable:2259)
269 (
const std::string& grp ,
270 const std::string& nam ,
272 Counter*& refpCounter )
275 refpCounter =
get ( grp , nam ) ;
276 if ( refpCounter ) {
return COUNTER_EXISTS ; }
278 Counter* newc =
new Counter() ;
280 if ( 0 != initial_value ) {
281 refpCounter->addFlag ( static_cast<double>(initial_value) ) ;
284 auto i = m_counts.find ( grp ) ;
286 if ( m_counts.end() ==
i )
287 {
i = m_counts.emplace ( grp ,
NameMap() ).first ; }
289 i->second.emplace( nam , newc );
300 (
const std::string& group ,
301 const std::string&
name ,
304 Counter* p =
nullptr;
305 StatusCode sc = create ( group, name, initial_value, p ) ;
306 if ( sc.
isSuccess() && 0 != p ) {
return CountObject ( p , group , name ) ; }
307 throw std::runtime_error(
"CounterSvc::Counter('"+group+
"::"+name+
"') exists already!");
313 (
const std::string& grp ,
314 const std::string& nam )
316 auto i = m_counts.find ( grp ) ;
317 if ( m_counts.end() ==
i ) {
return COUNTER_NOT_PRESENT ; }
318 auto j =
i->second.find ( nam ) ;
319 if (
i->second.end() == j ) {
return COUNTER_NOT_PRESENT ; }
321 i->second.erase ( j ) ;
330 if (
m_counts.
end() ==
i ) {
return COUNTER_NOT_PRESENT ; }
331 for (
auto& j :
i->second )
delete j.second ;
341 for (
auto&
i :
m_counts )
remove (
i.first ).ignore () ;
349 (
const std::string& grp,
350 const std::string& nam,
351 Printout& printer)
const
353 const Counter*
c =
get( grp , nam ) ;
354 if ( !c ) {
return COUNTER_NOT_PRESENT ; }
357 return printer ( log , c ) ;
362 class conditionalPrint {
364 CounterSvc::Printout *printer;
367 conditionalPrint(CounterSvc::Printout &_p,
MsgStream &_l): printer(&_p), log(&_l) {}
369 void operator() (
const Pair &p) {
371 (*printer)(*
log, p.second).ignore();
380 (
const std::string& grp ,
381 Printout& printer )
const
383 auto i = m_counts.find ( grp ) ;
384 if ( m_counts.end() ==
i ) {
return COUNTER_NOT_PRESENT ; }
388 std::map<std::string, Counter*> sorted_map(
i->second.begin(),
i->second.end());
389 std::for_each(sorted_map.begin(), sorted_map.end(),
390 conditionalPrint(printer, log));
397 (
const Counter* pCounter,
398 Printout& printer )
const
401 return printer ( log , pCounter ) ;
407 (
const CountObject& refCounter,
408 Printout& printer)
const
409 {
return print( refCounter.counter() , printer ) ; }
417 std::map<std::pair<std::string,std::string>, Counter*> sorted_map;
418 for (
const auto&
i :
m_counts )
for (
const auto& j :
i.second )
419 sorted_map[ {
i.first, j.first } ] = j.second;
420 std::for_each(sorted_map.begin(), sorted_map.end(),
421 conditionalPrint(printer, log));
429 const Counter*
c )
const
432 auto p = _find ( c ) ;
435 << CountObject( const_cast<Counter*>(c) , p.first , p.second )
447 const auto _num =
num() ;
451 <<
"Number of counters : " << _num <<
endmsg
455 std::map<std::pair<std::string,std::string>, Counter*> sorted_map;
457 for (
const auto& j :
i.second ) {
458 if ( j.second ) sorted_map[ {
i.first, j.first} ] = j.second;
461 for (
const auto&
i : sorted_map ) {
std::pair< std::string, std::string > _find(const Counter *c) const
GaudiUtils::HashMap< std::string, NameMap > CountMap
Definition of the MsgStream class used to transmit messages.
virtual StatusCode remove()
Remove all known counter objects.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
virtual StatusCode create(const std::string &group, const std::string &name, longlong initial_value, Counter *&refpCounter)
Create a new counter object.
std::string m_header
the header row
StatusCode finalize() override
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
bool isSuccess() const
Test for a status code of SUCCESS.
bool m_useEffFormat
flag to use the special "efficiency" format
bool m_print
boolean flag to print statistics
std::vector< CountObject > Counters
the actual type of vectors of initialized counters
std::string m_format2
format for "efficiency" statistical printout rows
Simple implementation of the abstract interface ICounterSvc.
GaudiUtils::HashMap< std::string, Counter * > NameMap
void print() const
"standard" printout a'la GaudiCommon
#define DECLARE_COMPONENT(type)
This class is used for returning status codes from appropriate routines.
std::pair< const K, T > value_type
CountMap m_counts
the actual map of counters
CounterSvc(const std::string &name, ISvcLocator *svcLoc)
Standard Constructor.
iterator find(const key_type &key)
virtual ~CounterSvc()
Standard destructor.
std::string m_format1
format for regular statistical printout rows
Base class used to extend a class implementing other interfaces.
virtual StatusCode defaultPrintout(MsgStream &log, const Counter *pCounter) const
Default Printout for counters.
GAUDI_API std::string formatAsTableRow(const StatEntity &counter, const bool flag, const std::string &format1=" |%|7d| |%|11.7g| |%|#11.5g| |%|#10.5g| |%|#10.5g| |%|#10.5g| |", const std::string &format2="*|%|7d| |%|11.5g| |(%|#9.7g| +- %|-#8.6g|)%%| ----- | ----- |")
print the counter in a form of the table row
Common class providing an architecture-independent hash map.
virtual Counter * get(const std::string &group, const std::string &name) const
Access an existing counter object.
virtual StatusCode finalize()
Finalization.