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"
31 ,
m_header (
" Counter :: Group | # | sum | mean/eff^* | rms/err^* | min | max |")
33 ,
m_format1 (
" %|15.15s|%|-15.15s|%|32t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |" )
35 ,
m_format2 (
"*%|15.15s|%|-15.15s|%|32t||%|10d| |%|11.5g| |(%|#9.7g| +- %|-#9.7g|)%%| ------- | ------- |" )
38 declareProperty (
"PrintStat" ,
m_print ) ;
42 "The header row for the output Stat-table" ) ;
46 "The format for the regular row in the output Stat-table" ) ;
50 "The format for the regular row in the outptu Stat-table" ) ;
54 "Use the special format for printout of efficiency counters" ) ;
75 (
const std::string& group ,
76 const std::string&
name )
const override;
92 (
const std::string& group ,
93 const std::string&
name ,
95 Counter*& refpCounter )
override ;
108 (
const std::string& group ,
109 const std::string&
name ,
110 longlong initial_value = 0 )
override ;
121 (
const std::string& group ,
122 const std::string&
name )
override ;
129 (
const std::string& group )
override ;
140 (
const std::string& group,
141 const std::string&
name,
142 Printout& printer)
const override;
151 (
const std::string& group,
152 Printout& printer)
const override;
160 (
const Counter* pCounter,
161 Printout& printer)
const override;
169 (
const CountObject& pCounter,
170 Printout& printer)
const override;
178 const Counter* pCounter)
const override ;
181 inline std::pair<std::string,std::string>
_find (
const Counter*
c )
const
185 auto j = std::find_if(
i.second.begin(),
i.second.end(),
187 return k.second ==
c;
189 if (j!=
i.second.end())
return {
i.first, j->first } ;
195 inline size_t num ()
const
199 return r + std::count_if(
i.second.begin(),
i.second.end(),
205 void print ()
const ;
230 ( const
std::
string& grp ,
231 const
std::
string& nam )
const
233 auto i = m_counts.find ( grp ) ;
234 if ( m_counts.end() ==
i ) {
return nullptr ; }
235 auto j =
i->second.find ( nam ) ;
236 if (
i->second.end() == j ) {
return nullptr ; }
247 std::transform(
i->second.begin(),
i->second.end(),
248 std::back_inserter(result),
250 return CountObject{ j.second,
i->first, j.first };
260 #pragma warning(push)
261 #pragma warning(disable:2259)
264 (
const std::string& grp ,
265 const std::string& nam ,
267 Counter*& refpCounter )
270 refpCounter =
get ( grp , nam ) ;
271 if ( refpCounter ) {
return COUNTER_EXISTS ; }
273 auto newc =
new Counter() ;
275 if ( 0 != initial_value ) {
276 refpCounter->addFlag ( static_cast<double>(initial_value) ) ;
279 auto i = m_counts.find ( grp ) ;
281 if ( m_counts.end() ==
i )
282 {
i = m_counts.emplace ( grp ,
NameMap() ).first ; }
284 i->second.emplace( nam , newc );
295 (
const std::string& group ,
296 const std::string&
name ,
299 Counter* p =
nullptr;
300 StatusCode sc = create ( group, name, initial_value, p ) ;
301 if ( sc.
isSuccess() && p ) {
return CountObject ( p , group , name ) ; }
302 throw std::runtime_error(
"CounterSvc::Counter('"+group+
"::"+name+
"') exists already!");
308 (
const std::string& grp ,
309 const std::string& nam )
311 auto i = m_counts.find ( grp ) ;
312 if ( m_counts.end() ==
i ) {
return COUNTER_NOT_PRESENT ; }
313 auto j =
i->second.find ( nam ) ;
314 if (
i->second.end() == j ) {
return COUNTER_NOT_PRESENT ; }
316 i->second.erase ( j ) ;
325 if (
m_counts.
end() ==
i ) {
return COUNTER_NOT_PRESENT ; }
326 for (
auto& j :
i->second )
delete j.second ;
336 for (
auto&
i :
m_counts )
remove (
i.first ).ignore () ;
344 (
const std::string& grp,
345 const std::string& nam,
346 Printout& printer)
const
348 const Counter*
c =
get( grp , nam ) ;
349 if ( !c ) {
return COUNTER_NOT_PRESENT ; }
352 return printer ( log , c ) ;
357 class conditionalPrint {
359 CounterSvc::Printout *printer;
362 conditionalPrint(CounterSvc::Printout &_p,
MsgStream &_l): printer(&_p), log(&_l) {}
364 void operator() (
const Pair &p) {
366 (*printer)(*
log, p.second).ignore();
375 (
const std::string& grp ,
376 Printout& printer )
const
378 auto i = m_counts.find ( grp ) ;
379 if ( m_counts.end() ==
i ) {
return COUNTER_NOT_PRESENT ; }
383 std::map<std::string, Counter*> sorted_map(
i->second.begin(),
i->second.end());
384 std::for_each(sorted_map.begin(), sorted_map.end(),
385 conditionalPrint(printer, log));
392 (
const Counter* pCounter,
393 Printout& printer )
const
396 return printer ( log , pCounter ) ;
402 (
const CountObject& refCounter,
403 Printout& printer)
const
404 {
return print( refCounter.counter() , printer ) ; }
412 std::map<std::pair<std::string,std::string>, Counter*> sorted_map;
413 for (
const auto&
i :
m_counts )
for (
const auto& j :
i.second )
414 sorted_map[ {
i.first, j.first } ] = j.second;
415 std::for_each(sorted_map.begin(), sorted_map.end(),
416 conditionalPrint(printer, log));
424 const Counter*
c )
const
427 auto p = _find ( c ) ;
430 << CountObject( const_cast<Counter*>(c) , p.first , p.second )
442 const auto _num =
num() ;
446 <<
"Number of counters : " << _num <<
endmsg
450 std::map<std::pair<std::string,std::string>, Counter*> sorted_map;
452 for (
const auto& j :
i.second ) {
453 if ( j.second ) sorted_map[ {
i.first, j.first} ] = j.second;
456 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...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
std::string m_header
the header row
StatusCode finalize() override
bool isSuccess() const
Test for a status code of SUCCESS.
Counter * get(const std::string &group, const std::string &name) const override
Access an existing counter object.
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.
StatusCode create(const std::string &group, const std::string &name, longlong initial_value, Counter *&refpCounter) override
Create a new counter object.
GaudiUtils::HashMap< std::string, Counter * > NameMap
void print() const
"standard" printout a'la GaudiCommon
This class is used for returning status codes from appropriate routines.
std::pair< const K, T > value_type
#define DECLARE_COMPONENT(type)
CountMap m_counts
the actual map of counters
CounterSvc(const std::string &name, ISvcLocator *svcLoc)
Standard Constructor.
iterator find(const key_type &key)
std::string m_format1
format for regular statistical printout rows
StatusCode defaultPrintout(MsgStream &log, const Counter *pCounter) const override
Default Printout for counters.
Base class used to extend a class implementing other interfaces.
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.
~CounterSvc() override
Standard destructor.
StatusCode finalize() override
Finalization.