Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Counters.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_COUNTERS_H
2 #define GAUDIKERNEL_COUNTERS_H 1
3 
129 #include "boost/algorithm/string/predicate.hpp"
130 #include "boost/format.hpp"
131 #include <atomic>
132 #include <cmath>
133 #include <initializer_list>
134 #include <iostream>
135 #include <limits>
136 #include <sstream>
137 #include <tuple>
138 #include <type_traits>
139 #include <utility>
140 
141 #include "GaudiKernel/MsgStream.h"
142 #include "GaudiKernel/apply.h"
143 #include "GaudiKernel/detected.h"
144 
145 namespace Gaudi {
146  namespace Accumulators {
147 
149  enum class atomicity { none, full };
150 
154  template <unsigned long N>
155  struct Constant {
156  template <typename U>
157  constexpr unsigned long operator()( U&& ) const noexcept {
158  return N;
159  }
160  };
161 
165  struct Identity {
166  template <typename U>
167  constexpr decltype( auto ) operator()( U&& v ) const noexcept {
168  return std::forward<U>( v );
169  }
170  };
171 
175  struct Square {
176  template <typename U>
177  constexpr decltype( auto ) operator()( U&& v ) const noexcept {
178  return v * v;
179  }
180  };
181 
185  template <typename T, typename = int>
186  using has_fetch_add_ = decltype( std::atomic<T>{}.fetch_add( 0 ) );
187  template <typename T>
188  using has_fetch_add = typename Gaudi::cpp17::is_detected<has_fetch_add_, T>::value_t;
189 
193  template <typename Arithmetic, typename Result = double>
194  using fp_result_type = std::conditional_t<std::is_integral<Arithmetic>::value, Result, Arithmetic>;
195 
199  template <typename Arithmetic, atomicity Atomicity>
201 
205  template <typename Arithmetic>
206  struct BaseValueHandler<Arithmetic, atomicity::none> {
207  using OutputType = Arithmetic;
208  using InternalType = Arithmetic;
209  static constexpr OutputType getValue( const InternalType& v ) noexcept { return v; };
210  static Arithmetic exchange( InternalType& v, Arithmetic newv ) noexcept { return std::exchange( v, newv ); }
211  };
212 
216  template <typename Arithmetic>
217  struct BaseValueHandler<Arithmetic, atomicity::full> {
218  using OutputType = Arithmetic;
220  static constexpr OutputType getValue( const InternalType& v ) noexcept {
221  return v.load( std::memory_order_relaxed );
222  };
223  static Arithmetic exchange( InternalType& v, Arithmetic newv ) noexcept { return v.exchange( newv ); }
224  };
225 
230  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
231  struct Adder;
232 
236  template <typename Arithmetic>
237  struct Adder<Arithmetic, atomicity::none> : BaseValueHandler<Arithmetic, atomicity::none> {
240  static constexpr OutputType DefaultValue() { return Arithmetic{}; }
241  static void merge( InternalType& a, Arithmetic b ) noexcept { a += b; };
242  };
243 
247  template <typename Arithmetic>
248  struct Adder<Arithmetic, atomicity::full> : BaseValueHandler<Arithmetic, atomicity::full> {
251  static constexpr OutputType DefaultValue() { return Arithmetic{}; }
252 #if __cplusplus > 201402L
253  static void merge( InternalType& a, Arithmetic b ) noexcept {
254  if ( DefaultValue() == b ) return; // avoid atomic operation if b is "0"
255  // C++ 17 version
256  if constexpr ( has_fetch_add<InternalType>::value ) {
257  a.fetch_add( b, std::memory_order_relaxed );
258  } else {
260  while ( !a.compare_exchange_weak( current, current + b ) )
261  ;
262  }
263  };
264 #else
265  // C++11 version
266  private:
267  template <typename T>
268  static void add( InternalType& a, T b, std::false_type ) {
269  auto current = a.load( std::memory_order_relaxed );
270  while ( !a.compare_exchange_weak( current, current + b ) )
271  ;
272  }
273  template <typename T>
274  static void add( InternalType& a, T b, std::true_type ) {
275  a.fetch_add( b, std::memory_order_relaxed );
276  }
277 
278  public:
279  static void merge( InternalType& a, Arithmetic b ) noexcept {
280  if ( DefaultValue() == b ) return; // avoid atomic operation if b is "0"
281  add( a, b, has_fetch_add<InternalType>{} );
282  }
283 #endif
284  };
285 
290  template <typename Arithmetic, atomicity Atomicity, typename Compare, Arithmetic ( *Initial )()>
291  struct Extremum;
292 
296  template <typename Arithmetic, typename Compare, Arithmetic ( *Initial )()>
297  struct Extremum<Arithmetic, atomicity::none, Compare, Initial> : BaseValueHandler<Arithmetic, atomicity::none> {
300  static constexpr OutputType DefaultValue() { return Initial(); }
301  static void merge( InternalType& a, Arithmetic b ) noexcept {
302  if ( Compare{}( b, a ) ) a = b;
303  };
304  };
305 
309  template <typename Arithmetic, typename Compare, Arithmetic ( *Initial )()>
310  struct Extremum<Arithmetic, atomicity::full, Compare, Initial> : BaseValueHandler<Arithmetic, atomicity::full> {
313  static constexpr OutputType DefaultValue() { return Initial(); }
314  static void merge( InternalType& a, Arithmetic b ) noexcept {
315  Arithmetic prev_value = BaseValueHandler<Arithmetic, atomicity::full>::getValue( a );
316  while ( Compare{}( b, prev_value ) && !a.compare_exchange_weak( prev_value, b ) )
317  ;
318  };
319  };
320 
325  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
327 
332  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
334 
352  template <typename InputType, typename InnerType, atomicity Atomicity = atomicity::full,
353  typename InputTransform = Identity, typename OutputTransform = Identity,
354  typename ValueHandler = Adder<InnerType, Atomicity>>
356  template <typename A, typename B, atomicity C, typename D, typename E, typename F>
357  friend class GenericAccumulator;
358 
359  public:
360  using OutputType = std::decay_t<std::result_of_t<OutputTransform( InnerType )>>;
361  GenericAccumulator operator+=( const InputType by ) {
362  ValueHandler::merge( m_value, InputTransform{}( by ) );
363  return *this;
364  }
365  GenericAccumulator() : m_value( ValueHandler::DefaultValue() ) {}
366  GenericAccumulator( const GenericAccumulator& other ) : m_value( ValueHandler::getValue( other.m_value ) ) {}
368  m_value = ValueHandler::getValue( other.m_value );
369  return *this;
370  }
371  OutputType value() const { return OutputTransform{}( ValueHandler::getValue( m_value ) ); }
372  void reset() { reset( ValueHandler::DefaultValue() ); }
373  template <atomicity ato, typename VH>
375  auto otherValue = VH::exchange( other.m_value, VH::DefaultValue() );
376  ValueHandler::merge( m_value, otherValue );
377  }
378 
379  protected:
380  void reset( InnerType in ) { m_value = std::move( in ); }
381 
382  private:
383  typename ValueHandler::InternalType m_value;
384  };
385 
391  template <typename Arithmetic, atomicity Atomicity, template <typename, atomicity> class... Bases>
392  class AccumulatorSet : public Bases<Arithmetic, Atomicity>... {
393  public:
394  using InputType = Arithmetic;
396  constexpr AccumulatorSet() = default;
398  (void)std::initializer_list<int>{( Bases<Arithmetic, Atomicity>::operator+=( by ), 0 )...};
399  return *this;
400  }
401  OutputType value() const { return std::make_tuple( Bases<Arithmetic, Atomicity>::value()... ); }
402  void reset() { (void)std::initializer_list<int>{( Bases<Arithmetic, Atomicity>::reset(), 0 )...}; }
403  template <atomicity Ato>
406  ( Bases<Arithmetic, Atomicity>::mergeAndReset( static_cast<Bases<Arithmetic, Ato>&&>( other ) ), 0 )...};
407  }
408 
409  protected:
410  void reset( const std::tuple<typename Bases<Arithmetic, Atomicity>::OutputType...>& t ) {
411  Gaudi::apply(
412  [this]( const auto&... i ) {
413  (void)std::initializer_list<int>{( this->Bases<Arithmetic, Atomicity>::reset( i ), 0 )...};
414  },
415  t );
416  }
417  };
418 
423  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
425  : GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Identity, Identity, Maximum<Arithmetic, Atomicity>> {
426  Arithmetic max() const { return this->value(); }
427  };
428 
433  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
435  : GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Identity, Identity, Minimum<Arithmetic, Atomicity>> {
436  Arithmetic min() const { return this->value(); }
437  };
438 
443  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
444  struct CountAccumulator : GenericAccumulator<Arithmetic, unsigned long, Atomicity, Constant<1>> {
445  unsigned long nEntries() const { return this->value(); }
446  };
447 
452  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
453  struct SumAccumulator : GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Identity> {
454  Arithmetic sum() const { return this->value(); }
455  };
456 
461  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
462  struct SquareAccumulator : GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Square> {
463  Arithmetic sum2() const { return this->value(); };
464  };
465 
467  struct TrueTo1 {
468  unsigned int operator()( bool v ) const { return v; }
469  };
470 
476  template <typename Arithmetic, atomicity Atomicity>
477  struct TrueAccumulator : GenericAccumulator<Arithmetic, unsigned long, Atomicity, TrueTo1> {
478  unsigned long nTrueEntries() const { return this->value(); };
479  };
480 
482  struct FalseTo1 {
483  unsigned int operator()( bool v ) const { return !v; }
484  };
485 
491  template <typename Arithmetic, atomicity Atomicity>
492  struct FalseAccumulator : GenericAccumulator<Arithmetic, unsigned long, Atomicity, FalseTo1> {
493  unsigned long nFalseEntries() const { return this->value(); };
494  };
495 
501  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
502  struct BinomialAccumulator : AccumulatorSet<bool, Atomicity, TrueAccumulator, FalseAccumulator> {
503  unsigned long nEntries() const { return this->nTrueEntries() + this->nFalseEntries(); };
504 
505  template <typename Result = fp_result_type<Arithmetic>>
506  auto efficiency() const {
507  auto nbEntries = nEntries();
508  if ( 1 > nbEntries ) return Result{-1};
509  return static_cast<Result>( this->nTrueEntries() ) / nbEntries;
510  }
511  auto eff() const { return efficiency(); }
512 
513  template <typename Result = fp_result_type<Arithmetic>>
514  auto efficiencyErr() const {
515  // Note the usage of using, aiming at using the std version of sqrt by default, without preventing
516  // more specialized versions to be used via ADL (see http://en.cppreference.com/w/cpp/language/adl)
517  using std::sqrt;
518  auto nbEntries = nEntries();
519  if ( 1 > nbEntries ) return Result{-1};
520  return sqrt( static_cast<Result>( this->nTrueEntries() * this->nFalseEntries() ) / nbEntries ) / nbEntries;
521  }
522  auto effErr() const { return efficiencyErr(); }
523  };
524 
529  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
530  struct AveragingAccumulator : AccumulatorSet<Arithmetic, Atomicity, CountAccumulator, SumAccumulator> {
531 
532  template <typename Result = fp_result_type<Arithmetic>>
533  auto mean() const {
534  auto n = this->nEntries();
535  return ( n > 0 ) ? static_cast<Result>( this->sum() ) / n : Result{};
536  }
537  };
538 
543  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
544  struct SigmaAccumulator : AccumulatorSet<Arithmetic, Atomicity, AveragingAccumulator, SquareAccumulator> {
545 
546  template <typename Result = fp_result_type<Arithmetic>>
547  auto biased_sample_variance() const {
548  auto n = this->nEntries();
549  Result sum = this->sum();
550  return ( n > 0 ) ? ( this->sum2() - sum * ( sum / n ) ) / n : Result{};
551  }
552 
553  template <typename Result = fp_result_type<Arithmetic>>
555  auto n = this->nEntries();
556  Result sum = this->sum();
557  return ( n > 1 ) ? ( this->sum2() - sum * ( sum / n ) ) / ( n - 1 ) : Result{};
558  }
559 
560  template <typename Result = fp_result_type<Arithmetic>>
561  auto standard_deviation() const {
562  // Note the usage of using, aiming at using the std version of sqrt by default, without preventing
563  // more specialized versions to be used via ADL (see http://en.cppreference.com/w/cpp/language/adl)
564  using std::sqrt;
565  Result v = biased_sample_variance();
566  return ( 0 > v ) ? Result{} : sqrt( v );
567  }
568  [[deprecated( "The name 'rms' has changed to standard_deviation" )]] Arithmetic rms() const {
569  return standard_deviation();
570  }
571 
572  template <typename Result = fp_result_type<Arithmetic>>
573  auto meanErr() const {
574  auto n = this->nEntries();
575  if ( 0 == n ) return Result{};
576  // Note the usage of using, aiming at using the std version of sqrt by default, without preventing
577  // more specialized versions to be used via ADL (see http://en.cppreference.com/w/cpp/language/adl)
578  using std::sqrt;
579  Result v = biased_sample_variance();
580  return ( 0 > v ) ? Result{} : sqrt( v / n );
581  }
582  };
583 
588  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
590 
597  template <typename Arithmetic, template <typename Int, atomicity Ato> class ContainedAccumulator>
598  class Buffer : public ContainedAccumulator<Arithmetic, atomicity::none> {
599  using prime_type = ContainedAccumulator<Arithmetic, atomicity::full>;
600  using base_type = ContainedAccumulator<Arithmetic, atomicity::none>;
601 
602  public:
603  Buffer() = delete;
604  Buffer( prime_type& p ) : base_type(), m_prime( p ) {}
605  Buffer( const Buffer& ) = delete;
606  void operator=( const Buffer& ) = delete;
607  Buffer( Buffer&& other ) : base_type( other ), m_prime( other.m_prime ) { other.reset(); }
608  void push() { m_prime.mergeAndReset( static_cast<base_type&&>( *this ) ); }
609  ~Buffer() { push(); }
610 
611  private:
613  };
614 
620  PrintableCounter() = default;
621  template <class OWNER>
622  PrintableCounter( OWNER* o, const std::string& tag ) {
623  o->declareCounter( tag, *this );
624  }
626  virtual ~PrintableCounter() = default;
627  // add tag to printout
628  template <typename stream>
629  stream& printImpl( stream& s, const std::string& tag ) const {
630  s << boost::format{" | %|-48.48s|%|50t|"} % ( "\"" + tag + "\"" );
631  return print( s, true );
632  }
634  virtual std::ostream& print( std::ostream&, bool tableFormat = false ) const = 0;
635  virtual MsgStream& print( MsgStream&, bool tableFormat = true ) const = 0;
637  virtual std::ostream& print( std::ostream& o, const std::string& tag ) const { return printImpl( o, tag ); }
638  virtual MsgStream& print( MsgStream& o, const std::string& tag ) const { return printImpl( o, tag ); }
641  virtual bool toBePrinted() const { return true; }
644  std::ostringstream ost;
645  print( ost );
646  return ost.str();
647  }
648  };
649 
653  inline std::ostream& operator<<( std::ostream& s, const PrintableCounter& counter ) { return counter.print( s ); }
654  inline MsgStream& operator<<( MsgStream& s, const PrintableCounter& counter ) { return counter.print( s ); }
660  template <typename Arithmetic, atomicity Atomicity, template <typename Int, atomicity Ato> class Accumulator>
663  Buffer<Arithmetic, Accumulator> buffer() { return {*static_cast<Accumulator<Arithmetic, Atomicity>*>( this )}; }
664  };
665 
670  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
671  struct Counter : BufferableCounter<Arithmetic, Atomicity, Counter>, CountAccumulator<Arithmetic, Atomicity> {
674  ( *this ) += Arithmetic{};
675  return *this;
676  }
678  auto copy = *this;
679  ++( *this );
680  return copy;
681  }
683 
684  template <typename stream>
685  stream& printImpl( stream& o, bool tableFormat ) const {
686  // Avoid printing empty counters in non DEBUG mode
687  auto fmt = ( tableFormat ? "|%|10d| |" : "#=%|-7lu|" );
688  o << boost::format{fmt} % this->nEntries();
689  return o;
690  }
691 
692  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
693  return printImpl( o, tableFormat );
694  }
695  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override { return printImpl( o, tableFormat ); }
696  virtual bool toBePrinted() const override {
697  return this->nEntries() > 0;
698  ;
699  }
700  };
701 
706  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
707  struct AveragingCounter : BufferableCounter<Arithmetic, Atomicity, AveragingCounter>,
708  AveragingAccumulator<Arithmetic, Atomicity> {
711 
712  template <typename stream>
713  stream& printImpl( stream& o, bool tableFormat ) const {
714  auto fmt = ( tableFormat ? "|%|10d| |%|11.7g| |%|#11.5g| |" : "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g|" );
715  return o << boost::format{fmt} % this->nEntries() % this->sum() % this->mean();
716  }
717 
718  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
719  return printImpl( o, tableFormat );
720  }
721  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override { return printImpl( o, tableFormat ); }
722 
723  virtual bool toBePrinted() const override {
724  return this->nEntries() > 0;
725  ;
726  }
727  };
728  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
730 
735  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
736  struct SigmaCounter : BufferableCounter<Arithmetic, Atomicity, SigmaCounter>,
737  SigmaAccumulator<Arithmetic, Atomicity> {
740 
741  template <typename stream>
742  stream& printImpl( stream& o, bool tableFormat ) const {
743  auto fmt = ( tableFormat ? "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |"
744  : "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g|" );
745  return o << boost::format{fmt} % this->nEntries() % this->sum() % this->mean() % this->standard_deviation();
746  }
747 
748  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
749  return printImpl( o, tableFormat );
750  }
751  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override { return printImpl( o, tableFormat ); }
752  virtual bool toBePrinted() const override {
753  return this->nEntries() > 0;
754  ;
755  }
756  };
757 
762  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
763  struct StatCounter : BufferableCounter<Arithmetic, Atomicity, StatCounter>, StatAccumulator<Arithmetic, Atomicity> {
766 
767  template <typename stream>
768  stream& printImpl( stream& o, bool tableFormat ) const {
769  auto fmt =
770  ( tableFormat ? "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |"
771  : "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|" );
772  return o << boost::format{fmt} % this->nEntries() % this->sum() % this->mean() % this->standard_deviation() %
773  this->min() % this->max();
774  }
775 
776  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
777  return printImpl( o, tableFormat );
778  }
779  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override { return printImpl( o, tableFormat ); }
780  virtual bool toBePrinted() const override { return this->nEntries() > 0; }
781  };
782 
787  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
788  struct BinomialCounter : BufferableCounter<Arithmetic, Atomicity, BinomialCounter>,
789  BinomialAccumulator<Arithmetic, Atomicity> {
791 
792  template <typename stream>
793  stream& printImpl( stream& o, bool tableFormat ) const {
794  auto fmt = ( tableFormat ? "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%% |"
795  : "#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|" );
796  return o << boost::format{fmt} % this->nEntries() % this->nTrueEntries() % ( this->efficiency() * 100 ) %
797  ( this->efficiencyErr() * 100 );
798  }
799 
800  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
801  return printImpl( o, tableFormat );
802  }
803  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override { return printImpl( o, tableFormat ); }
804 
805  template <typename stream>
806  stream& printImpl( stream& o, const std::string& tag ) const {
807  // override default print to add a '*' in from of the name
808  o << boost::format{" |*%|-48.48s|%|50t|"} % ( "\"" + tag + "\"" );
809  return print( o, true );
810  }
812  virtual std::ostream& print( std::ostream& o, const std::string& tag ) const override {
813  return printImpl( o, tag );
814  }
815  virtual MsgStream& print( MsgStream& o, const std::string& tag ) const override { return printImpl( o, tag ); }
816  };
817 
823  template <typename Counter, typename Container, typename Fun>
824  void accumulate( Counter& counter, const Container& container, Fun f = Identity{} ) {
825  auto b = counter.buffer();
826  for ( const auto& elem : container ) b += f( elem );
827  }
828 
829  } // namespace Accumulators
830 
831 } // namespace Gaudi
832 
838  public Gaudi::Accumulators::AccumulatorSet<double, Gaudi::Accumulators::atomicity::full,
839  Gaudi::Accumulators::StatAccumulator,
840  Gaudi::Accumulators::BinomialAccumulator> {
841 public:
847  using AccParent::reset;
849  StatEntity() = default;
850  template <class OWNER>
851  StatEntity( OWNER* o, const std::string& tag ) {
852  o->declareCounter( tag, *this );
853  }
854  StatEntity( const unsigned long entries, const double flag, const double flag2, const double minFlag,
855  const double maxFlag ) {
856  reset( std::make_tuple(
857  std::make_tuple( std::make_tuple( std::make_tuple( entries, flag ), flag2 ), minFlag, maxFlag ),
858  std::make_tuple( 0, 0 ) ) );
859  }
860  void reset() { AccParent::reset(); }
861  void operator=( double by ) {
862  this->reset();
863  ( *this ) += by;
864  }
865  StatEntity& operator-=( double by ) {
866  ( *this ) += ( -by );
867  return *this;
868  }
870  ( *this ) += 1.0;
871  return *this;
872  }
874  auto copy = *this;
875  ++( *this );
876  return copy;
877  }
879  ( *this ) += -1.0;
880  return *this;
881  }
883  auto copy = *this;
884  --( *this );
885  return copy;
886  }
887  bool operator<( const StatEntity& se ) const {
888  return std::make_tuple( nEntries(), sum(), min(), max(), sum2() ) <
889  std::make_tuple( se.nEntries(), se.sum(), se.min(), se.max(), se.sum2() );
890  };
891  // using AccumulatorSet::operator+=;
892  StatEntity& operator+=( double by ) {
893  this->AccumulatorSet::operator+=( by );
894  return *this;
895  }
897  mergeAndReset( std::move( by ) );
898  return *this;
899  }
900  unsigned long add( const double v ) {
901  *this += v;
902  return nEntries();
903  }
904  unsigned long addFlag( const double v ) { return add( v ); }
905  // aliases (a'la ROOT)
906  double Sum() const { return sum(); } // get sum
907  double Mean() const { return mean(); } // get mean
908  double MeanErr() const { return meanErr(); } // get error in mean
909  double rms() const { return standard_deviation(); } // get rms
910  double Rms() const { return standard_deviation(); } // get rms
911  double RMS() const { return standard_deviation(); } // get rms
912  double Eff() const { return eff(); } // get efficiency
913  double Min() const { return min(); } // get minimal value
914  double Max() const { return max(); } // get maximal value
915  // some legacy methods, to be removed ...
916  double flag() const { return sum(); }
917  double flag2() const { return sum2(); }
918  double flagMean() const { return mean(); }
919  double flagRMS() const { return standard_deviation(); }
920  double flagMeanErr() const { return meanErr(); }
921  double flagMin() const { return min(); }
922  double flagMax() const { return max(); }
923  static bool effCounter( const std::string& name ) {
924  using boost::algorithm::icontains;
925  return icontains( name, "eff" ) || icontains( name, "acc" ) || icontains( name, "filt" ) ||
926  icontains( name, "fltr" ) || icontains( name, "pass" );
927  }
928  template <typename stream>
929  stream& printFormattedImpl( stream& o, const std::string& format ) const {
930  boost::format fmt{format};
931  fmt % nEntries() % sum() % mean() % standard_deviation() % min() % max();
932  return o << fmt.str();
933  }
935  return printFormattedImpl( o, format );
936  }
937  MsgStream& printFormatted( MsgStream& o, const std::string& format ) const { return printFormattedImpl( o, format ); }
939  template <typename stream>
940  stream& printImpl( stream& o, bool tableFormat, const std::string& name, bool flag,
941  std::string const& fmtHead ) const {
942  if ( flag && effCounter( name ) && 0 <= eff() && 0 <= effErr() && sum() <= nEntries() &&
943  ( 0 == min() || 1 == min() ) && ( 0 == max() || 1 == max() ) ) {
944  // efficiency printing
945  if ( tableFormat ) {
946  if ( name.empty() ) {
947  constexpr auto fmt = "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
948  return o << boost::format{fmt} % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
949  ( efficiencyErr() * 100 );
950  } else {
951  auto fmt = " |*" + fmtHead + "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
952  return o << boost::format{fmt} % ( "\"" + name + "\"" ) % BinomialAccParent::nEntries() % sum() %
953  ( efficiency() * 100 ) % ( efficiencyErr() * 100 );
954  }
955  } else {
956  constexpr auto fmt = "#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|";
957  return o << boost::format{fmt} % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
958  ( efficiencyErr() * 100 );
959  }
960  } else {
961  // Standard printing
962  if ( tableFormat ) {
963  if ( name.empty() ) {
964  constexpr auto fmt = "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |";
965  return o << boost::format{fmt} % nEntries() % sum() % mean() % standard_deviation() % min() % max();
966 
967  } else {
968  auto fmt = " | " + fmtHead + "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |";
969  return o << boost::format{fmt} % ( "\"" + name + "\"" ) % nEntries() % sum() % mean() % standard_deviation() %
970  min() % max();
971  }
972  } else {
973  constexpr auto fmt = "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|";
974  return o << boost::format{fmt} % nEntries() % sum() % mean() % standard_deviation() % min() % max();
975  }
976  }
977  }
978  std::ostream& print( std::ostream& o, bool tableFormat, const std::string& name, bool flag = true,
979  std::string fmtHead = "%|-48.48s|%|27t|" ) const {
980  return printImpl( o, tableFormat, name, flag, fmtHead );
981  }
982  MsgStream& print( MsgStream& o, bool tableFormat, const std::string& name, bool flag = true,
983  std::string fmtHead = "%|-48.48s|%|27t|" ) const {
984  return printImpl( o, tableFormat, name, flag, fmtHead );
985  }
986  virtual std::ostream& print( std::ostream& o, const std::string& tag ) const override {
987  return print( o, true, tag, true );
988  }
989  virtual MsgStream& print( MsgStream& o, const std::string& tag ) const override {
990  return print( o, true, tag, true );
991  }
992  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override {
993  std::string emptyName;
994  return print( o, tableFormat, emptyName, true );
995  }
996  MsgStream& print( MsgStream& o, bool tableFormat = false ) const override {
997  std::string emptyName;
998  return print( o, tableFormat, emptyName, true );
999  }
1001  std::ostringstream ost;
1002  print( ost );
1003  return ost.str();
1004  }
1005  std::ostream& fillStream( std::ostream& o ) const { return print( o ); }
1006  MsgStream& fillStream( MsgStream& o ) const { return print( o ); }
1007 };
1008 
1009 #endif // GAUDIKERNEL_COUNTERS_H
constexpr unsigned long operator()(U &&) const noexcept
Definition: Counters.h:157
ContainedAccumulator< Arithmetic, atomicity::full > prime_type
Definition: Counters.h:599
virtual std::ostream & print(std::ostream &o, const std::string &tag) const override
prints the counter to a stream in table format, with the given tag
Definition: Counters.h:986
std::string toString() const
Definition: Counters.h:1000
static void merge(InternalType &a, Arithmetic b) noexcept
Definition: Counters.h:301
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
stream & printImpl(stream &o, const std::string &tag) const
Definition: Counters.h:806
static void merge(InternalType &a, Arithmetic b) noexcept
Definition: Counters.h:314
void mergeAndReset(AccumulatorSet< Arithmetic, Ato, Bases... > &&other)
Definition: Counters.h:404
T empty(T...args)
double Min() const
Definition: Counters.h:913
Buffer(prime_type &p)
Definition: Counters.h:604
unsigned long nEntries() const
Definition: Counters.h:445
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
double Mean() const
Definition: Counters.h:907
double flagRMS() const
Definition: Counters.h:919
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:776
decltype(std::atomic< T >{}.fetch_add(0)) has_fetch_add_
type_traits for checking the presence of fetch_add in std::atomic<T>
Definition: Counters.h:186
std::ostream & fillStream(std::ostream &o) const
Definition: Counters.h:1005
unsigned long nEntries() const
Definition: Counters.h:503
unsigned long nFalseEntries() const
Definition: Counters.h:493
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:800
An empty ancester of all counters that provides a buffer method that returns a buffer on itself...
Definition: Counters.h:661
double sum(double x, double y, double z)
unsigned long nTrueEntries() const
Definition: Counters.h:478
virtual bool toBePrinted() const override
hint whether we should print that counter or not.
Definition: Counters.h:780
double flagMin() const
Definition: Counters.h:921
atomicity
Defines atomicity of the accumulators.
Definition: Counters.h:149
A counter aiming at computing average and sum2 / variance / standard deviation.
Definition: Counters.h:763
typename Gaudi::cpp17::is_detected< has_fetch_add_, T >::value_t has_fetch_add
Definition: Counters.h:188
Gaudi::Accumulators::BinomialAccumulator< double, Gaudi::Accumulators::atomicity::full > BinomialAccParent
Definition: Counters.h:845
An Adder ValueHandler operator(a, b) means a += b.
Definition: Counters.h:231
Gaudi::Accumulators::AccumulatorSet< double, Gaudi::Accumulators::atomicity::full, Gaudi::Accumulators::StatAccumulator, Gaudi::Accumulators::BinomialAccumulator > AccParent
Definition: Counters.h:844
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:202
A counter aiming at computing sum and average.
Definition: Counters.h:707
struct deprecated("use MergingTransformer instead")]] ListTransformer
class MergingTransformer< Out(const vector_of_const_< In > void
decltype(auto) constexpr apply(F &&f, Tuple &&t) noexcept(noexcept( detail::apply_impl(std::forward< F >(f), std::forward< Tuple >(t), std::make_index_sequence< std::tuple_size< std::remove_reference_t< Tuple >>::value >{})))
Definition: apply.h:27
virtual std::ostream & print(std::ostream &o, const std::string &tag) const
prints the counter to a stream in table format, with the given tag
Definition: Counters.h:637
T make_tuple(T...args)
An functor always returning the value N.
Definition: Counters.h:155
void reset()
Definition: Counters.h:860
An empty ancester of all counters that knows how to print themselves.
Definition: Counters.h:619
double flagMeanErr() const
Definition: Counters.h:920
std::ostream & printFormatted(std::ostream &o, const std::string &format) const
Definition: Counters.h:934
stream & printImpl(stream &o, bool tableFormat, const std::string &name, bool flag, std::string const &fmtHead) const
Definition: Counters.h:940
bool operator<(const StatEntity &se) const
Definition: Counters.h:887
StatEntity operator++(int)
Definition: Counters.h:873
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:215
static void add(InternalType &a, T b, std::true_type)
Definition: Counters.h:274
virtual bool toBePrinted() const override
hint whether we should print that counter or not.
Definition: Counters.h:752
std::decay_t< std::result_of_t< Identity(unsigned long)>> OutputType
Definition: Counters.h:360
virtual bool toBePrinted() const override
hint whether we should print that counter or not.
Definition: Counters.h:696
static constexpr OutputType getValue(const InternalType &v) noexcept
Definition: Counters.h:220
double Max() const
Definition: Counters.h:914
double flagMax() const
Definition: Counters.h:922
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: Counters.h:695
PropertyMgr & operator=(const PropertyMgr &)=delete
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:992
STL class.
MsgStream & fillStream(MsgStream &o) const
Definition: Counters.h:1006
StatEntity & operator++()
Definition: Counters.h:869
int N
Definition: IOTest.py:99
PrintableCounter(OWNER *o, const std::string &tag)
Definition: Counters.h:622
GenericAccumulator operator+=(const InputType by)
Definition: Counters.h:361
void accumulate(Counter &counter, const Container &container, Fun f=Identity{})
A helper function for accumulating data from a container into a counter This is internally using buff...
Definition: Counters.h:824
std::conditional_t< std::is_integral< Arithmetic >::value, Result, Arithmetic > fp_result_type
type_trait for the result type of a floating point operation on the type Arithmetic ...
Definition: Counters.h:194
static void add(InternalType &a, T b, std::false_type)
Definition: Counters.h:268
virtual bool toBePrinted() const
hint whether we should print that counter or not.
Definition: Counters.h:641
static Arithmetic exchange(InternalType &v, Arithmetic newv) noexcept
Definition: Counters.h:210
double MeanErr() const
Definition: Counters.h:908
double flagMean() const
Definition: Counters.h:918
double Eff() const
Definition: Counters.h:912
unsigned int operator()(bool v) const
Definition: Counters.h:468
AccumulatorSet & operator+=(const InputType by)
Definition: Counters.h:397
StatEntity & operator-=(double by)
Definition: Counters.h:865
StatEntity(OWNER *o, const std::string &tag)
Definition: Counters.h:851
A Square functor.
Definition: Counters.h:175
Buffer is a non atomic Accumulator which, when it goes out-of-scope, updates the underlying thread-sa...
Definition: Counters.h:598
Buffer< Arithmetic, Accumulator > buffer()
Definition: Counters.h:663
virtual std::ostream & print(std::ostream &o, const std::string &tag) const override
prints the counter to a stream in table format, with the given tag
Definition: Counters.h:812
std::ostream & print(std::ostream &o, bool tableFormat, const std::string &name, bool flag=true, std::string fmtHead="%|-48.48s|%|27t|") const
Definition: Counters.h:978
virtual std::ostream & print(std::ostream &, bool tableFormat=false) const =0
prints the counter to a stream
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: Counters.h:996
virtual MsgStream & print(MsgStream &o, const std::string &tag) const override
Definition: Counters.h:989
double Sum() const
Definition: Counters.h:906
GenericAccumulator & operator=(const GenericAccumulator &other)
Definition: Counters.h:367
std::string toString() const
get a string representation
Definition: Counters.h:643
A counter aiming at computing average and sum2 / variance / standard deviation.
Definition: Counters.h:736
void mergeAndReset(GenericAccumulator< InputType, InnerType, ato, InputTransform, OutputTransform, VH > &&other)
Definition: Counters.h:374
T move(T...args)
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:748
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: Counters.h:803
AccumulatorSet is an Accumulator that holds a set of Accumulators templated by same Arithmetic and At...
Definition: Counters.h:392
stream & printFormattedImpl(stream &o, const std::string &format) const
Definition: Counters.h:929
virtual bool toBePrinted() const override
hint whether we should print that counter or not.
Definition: Counters.h:723
StatEntity & operator+=(StatEntity by)
Definition: Counters.h:896
Buffer(Buffer &&other)
Definition: Counters.h:607
stream & printImpl(stream &o, bool tableFormat) const
Definition: Counters.h:768
stream & printImpl(stream &o, bool tableFormat) const
Definition: Counters.h:793
AccumulatorSet< Arithmetic, Atomicity, SigmaAccumulator, MinAccumulator, MaxAccumulator > StatAccumulator
StatAccumulator.
Definition: Counters.h:589
stream & printImpl(stream &s, const std::string &tag) const
Definition: Counters.h:629
StatEntity & operator--()
Definition: Counters.h:878
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: Counters.h:779
void reset(const std::tuple< typename Bases< Arithmetic, Atomicity >::OutputType... > &t)
Definition: Counters.h:410
A counter dealing with binomial data.
Definition: Counters.h:788
T fetch_add(T...args)
static bool effCounter(const std::string &name)
Definition: Counters.h:923
MsgStream & printFormatted(MsgStream &o, const std::string &format) const
Definition: Counters.h:937
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
ValueHandler::InternalType m_value
Definition: Counters.h:383
stream & printImpl(stream &o, bool tableFormat) const
Definition: Counters.h:742
helper functor for the TrueAccumulator
Definition: Counters.h:467
helper functor for the FalseAccumulator
Definition: Counters.h:482
stream & printImpl(stream &o, bool tableFormat) const
Definition: Counters.h:685
An Identity functor.
Definition: Counters.h:165
unsigned long addFlag(const double v)
Definition: Counters.h:904
Base type for all functors used as ValuesHandler.
Definition: Counters.h:200
GenericAccumulator(const GenericAccumulator &other)
Definition: Counters.h:366
string s
Definition: gaudirun.py:312
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:718
A basic counter counting input values.
Definition: Counters.h:671
MsgStream & print(MsgStream &o, bool tableFormat, const std::string &name, bool flag=true, std::string fmtHead="%|-48.48s|%|27t|") const
Definition: Counters.h:982
T compare_exchange_weak(T...args)
stream & printImpl(stream &o, bool tableFormat) const
Definition: Counters.h:713
std::ostream & operator<<(std::ostream &s, const PrintableCounter &counter)
external printout operator to a stream type
Definition: Counters.h:653
double Rms() const
Definition: Counters.h:910
static void merge(InternalType &a, Arithmetic b) noexcept
Definition: Counters.h:241
static void merge(InternalType &a, Arithmetic b) noexcept
Definition: Counters.h:279
static Arithmetic exchange(InternalType &v, Arithmetic newv) noexcept
Definition: Counters.h:223
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: Counters.h:721
T sqrt(T...args)
double RMS() const
Definition: Counters.h:911
StatEntity(const unsigned long entries, const double flag, const double flag2, const double minFlag, const double maxFlag)
Definition: Counters.h:854
static constexpr OutputType getValue(const InternalType &v) noexcept
Definition: Counters.h:209
double rms() const
Definition: Counters.h:909
backward compatible StatEntity class.
Definition: Counters.h:837
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Definition: Counters.h:751
StatEntity operator--(int)
Definition: Counters.h:882
double flag2() const
Definition: Counters.h:917
StatEntity & operator+=(double by)
Definition: Counters.h:892
ContainedAccumulator< Arithmetic, atomicity::none > base_type
Definition: Counters.h:600
STL class.
An Extremum ValueHandler, to be reused for Minimum and Maximum operator(a, b) means if (Compare(b...
Definition: Counters.h:291
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:692
unsigned int operator()(bool v) const
Definition: Counters.h:483
Generic Accumulator, templated by.
Definition: Counters.h:355
Helper functions to set/get the application return code.
Definition: __init__.py:1
void operator=(double by)
Definition: Counters.h:861
double flag() const
Definition: Counters.h:916
virtual MsgStream & print(MsgStream &o, const std::string &tag) const
Definition: Counters.h:638
T load(T...args)
unsigned long add(const double v)
Definition: Counters.h:900
virtual MsgStream & print(MsgStream &o, const std::string &tag) const override
Definition: Counters.h:815