The Gaudi Framework  v30r3 (a5ef0a68)
Counters.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_COUNTERS_H
2 #define GAUDIKERNEL_COUNTERS_H 1
3 
127 #include "boost/algorithm/string/predicate.hpp"
128 #include "boost/format.hpp"
129 #include <atomic>
130 #include <cmath>
131 #include <initializer_list>
132 #include <iostream>
133 #include <limits>
134 #include <sstream>
135 #include <tuple>
136 #include <type_traits>
137 #include <utility>
138 
139 #include "GaudiKernel/apply.h"
140 #include "GaudiKernel/detected.h"
141 
142 namespace Gaudi
143 {
144  namespace Accumulators
145  {
146 
148  enum class atomicity { none, full };
149 
153  template <unsigned long N>
154  struct Constant {
155  template <typename U>
156  constexpr unsigned long operator()( U&& ) const noexcept
157  {
158  return N;
159  }
160  };
161 
165  struct Identity {
166  template <typename U>
167  constexpr decltype( auto ) operator()( U&& v ) const noexcept
168  {
169  return std::forward<U>( v );
170  }
171  };
172 
176  struct Square {
177  template <typename U>
178  constexpr decltype( auto ) operator()( U&& v ) const noexcept
179  {
180  return v * v;
181  }
182  };
183 
187  template <typename T, typename = int>
188  using has_fetch_add_ = decltype( std::atomic<T>{}.fetch_add( 0 ) );
189  template <typename T>
190  using has_fetch_add = typename Gaudi::cpp17::is_detected<has_fetch_add_, T>::value_t;
191 
195  template <typename Arithmetic, atomicity Atomicity>
197 
201  template <typename Arithmetic>
202  struct BaseValueHandler<Arithmetic, atomicity::none> {
203  using OutputType = Arithmetic;
204  using InternalType = Arithmetic;
205  static constexpr OutputType getValue( const InternalType& v ) noexcept { return v; };
206  static Arithmetic exchange( InternalType& v, Arithmetic newv ) noexcept { return std::exchange( v, newv ); }
207  };
208 
212  template <typename Arithmetic>
213  struct BaseValueHandler<Arithmetic, atomicity::full> {
214  using OutputType = Arithmetic;
216  static constexpr OutputType getValue( const InternalType& v ) noexcept
217  {
218  return v.load( std::memory_order_relaxed );
219  };
220  static Arithmetic exchange( InternalType& v, Arithmetic newv ) noexcept { return v.exchange( newv ); }
221  };
222 
227  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
228  struct Adder;
229 
233  template <typename Arithmetic>
234  struct Adder<Arithmetic, atomicity::none> : BaseValueHandler<Arithmetic, atomicity::none> {
237  static constexpr OutputType DefaultValue() { return Arithmetic{}; }
238  static void merge( InternalType& a, Arithmetic b ) noexcept { a += b; };
239  };
240 
244  template <typename Arithmetic>
245  struct Adder<Arithmetic, atomicity::full> : BaseValueHandler<Arithmetic, atomicity::full> {
248  static constexpr OutputType DefaultValue() { return Arithmetic{}; }
249 #if __cplusplus > 201402L
250  static void merge( InternalType& a, Arithmetic b ) noexcept
251  {
252  if ( DefaultValue() == b ) return; // avoid atomic operation if b is "0"
253  // C++ 17 version
254  if
255  constexpr( has_fetch_add<InternalType>::value ) { a.fetch_add( b, std::memory_order_relaxed ); }
256  else {
258  while ( !a.compare_exchange_weak( current, current + b ) )
259  ;
260  }
261  };
262 #else
263  // C++11 version
264  private:
265  template <typename T>
266  static void add( InternalType& a, T b, std::false_type )
267  {
268  auto current = a.load( std::memory_order_relaxed );
269  while ( !a.compare_exchange_weak( current, current + b ) )
270  ;
271  }
272  template <typename T>
273  static void add( InternalType& a, T b, std::true_type )
274  {
275  a.fetch_add( b, std::memory_order_relaxed );
276  }
277 
278  public:
279  static void merge( InternalType& a, Arithmetic b ) noexcept
280  {
281  if ( DefaultValue() == b ) return; // avoid atomic operation if b is "0"
282  add( a, b, has_fetch_add<InternalType>{} );
283  }
284 #endif
285  };
286 
291  template <typename Arithmetic, atomicity Atomicity, typename Compare, Arithmetic ( *Initial )()>
292  struct Extremum;
293 
297  template <typename Arithmetic, typename Compare, Arithmetic ( *Initial )()>
298  struct Extremum<Arithmetic, atomicity::none, Compare, Initial> : BaseValueHandler<Arithmetic, atomicity::none> {
301  static constexpr OutputType DefaultValue() { return Initial(); }
302  static void merge( InternalType& a, Arithmetic b ) noexcept
303  {
304  if ( Compare{}( b, a ) ) a = b;
305  };
306  };
307 
311  template <typename Arithmetic, typename Compare, Arithmetic ( *Initial )()>
312  struct Extremum<Arithmetic, atomicity::full, Compare, Initial> : BaseValueHandler<Arithmetic, atomicity::full> {
315  static constexpr OutputType DefaultValue() { return Initial(); }
316  static void merge( InternalType& a, Arithmetic b ) noexcept
317  {
318  Arithmetic prev_value = BaseValueHandler<Arithmetic, atomicity::full>::getValue( a );
319  while ( Compare{}( b, prev_value ) && !a.compare_exchange_weak( prev_value, b ) )
320  ;
321  };
322  };
323 
328  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
330 
335  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
337 
355  template <typename InputType, typename InnerType, atomicity Atomicity = atomicity::full,
356  typename InputTransform = Identity, typename OutputTransform = Identity,
357  typename ValueHandler = Adder<InnerType, Atomicity>>
359  {
360  template <typename A, typename B, atomicity C, typename D, typename E, typename F>
361  friend class GenericAccumulator;
362 
363  public:
364  using OutputType = std::decay_t<std::result_of_t<OutputTransform( InnerType )>>;
365  GenericAccumulator operator+=( const InputType by )
366  {
367  ValueHandler::merge( m_value, InputTransform{}( by ) );
368  return *this;
369  }
370  GenericAccumulator() : m_value( ValueHandler::DefaultValue() ) {}
371  GenericAccumulator( const GenericAccumulator& other ) : m_value( ValueHandler::getValue( other.m_value ) ) {}
373  {
374  m_value = ValueHandler::getValue( other.m_value );
375  return *this;
376  }
377  OutputType value() const { return OutputTransform{}( ValueHandler::getValue( m_value ) ); }
378  void reset() { reset( ValueHandler::DefaultValue() ); }
379  template <atomicity ato, typename VH>
381  {
382  auto otherValue = VH::exchange( other.m_value, VH::DefaultValue() );
383  ValueHandler::merge( m_value, otherValue );
384  }
385 
386  protected:
387  void reset( InnerType in ) { m_value = std::move( in ); }
388  private:
389  typename ValueHandler::InternalType m_value;
390  };
391 
396  template <typename Arithmetic, atomicity Atomicity, template <typename, atomicity> class... Bases>
397  class AccumulatorSet : public Bases<Arithmetic, Atomicity>...
398  {
399  public:
400  using InputType = Arithmetic;
402  constexpr AccumulatorSet() = default;
404  {
405  (void)std::initializer_list<int>{( Bases<Arithmetic, Atomicity>::operator+=( by ), 0 )...};
406  return *this;
407  }
408  OutputType value() const { return std::make_tuple( Bases<Arithmetic, Atomicity>::value()... ); }
409  void reset() { (void)std::initializer_list<int>{( Bases<Arithmetic, Atomicity>::reset(), 0 )...}; }
410  template <atomicity Ato>
412  {
414  ( Bases<Arithmetic, Atomicity>::mergeAndReset( static_cast<Bases<Arithmetic, Ato>&&>( other ) ), 0 )...};
415  }
416 
417  protected:
418  void reset( const std::tuple<typename Bases<Arithmetic, Atomicity>::OutputType...>& t )
419  {
420  Gaudi::apply(
421  [this]( const auto&... i ) {
422  (void)std::initializer_list<int>{( this->Bases<Arithmetic, Atomicity>::reset( i ), 0 )...};
423  },
424  t );
425  }
426  };
427 
431  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
433  : GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Identity, Identity, Maximum<Arithmetic, Atomicity>> {
434  Arithmetic max() const { return this->value(); }
435  };
436 
440  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
442  : GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Identity, Identity, Minimum<Arithmetic, Atomicity>> {
443  Arithmetic min() const { return this->value(); }
444  };
445 
449  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
450  struct CountAccumulator : GenericAccumulator<Arithmetic, unsigned long, Atomicity, Constant<1>> {
451  unsigned long nEntries() const { return this->value(); }
452  };
453 
457  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
458  struct SumAccumulator : GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Identity> {
459  Arithmetic sum() const { return this->value(); }
460  };
461 
465  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
466  struct SquareAccumulator : GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Square> {
467  Arithmetic sum2() const { return this->value(); };
468  };
469 
471  struct TrueTo1 {
472  unsigned int operator()( bool v ) const { return v; }
473  };
474 
479  template <typename Arithmetic, atomicity Atomicity>
480  struct TrueAccumulator : GenericAccumulator<Arithmetic, unsigned long, Atomicity, TrueTo1> {
481  unsigned long nTrueEntries() const { return this->value(); };
482  };
483 
485  struct FalseTo1 {
486  unsigned int operator()( bool v ) const { return !v; }
487  };
488 
493  template <typename Arithmetic, atomicity Atomicity>
494  struct FalseAccumulator : GenericAccumulator<Arithmetic, unsigned long, Atomicity, FalseTo1> {
495  unsigned long nFalseEntries() const { return this->value(); };
496  };
497 
502  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
503  struct BinomialAccumulator : AccumulatorSet<bool, Atomicity, TrueAccumulator, FalseAccumulator> {
504  unsigned long nEntries() const { return this->nTrueEntries() + this->nFalseEntries(); };
505  Arithmetic efficiency() const
506  {
507  auto nbEntries = nEntries();
508  if ( 1 > nbEntries ) return -1;
509  return static_cast<Arithmetic>( this->nTrueEntries() ) / static_cast<Arithmetic>( nbEntries );
510  }
511  Arithmetic eff() const { return efficiency(); }
512  Arithmetic efficiencyErr() const
513  {
514  // Note the usage of using, aiming at using the std version of sqrt by default, without preventing
515  // more specialized versions to be used via ADL (see http://en.cppreference.com/w/cpp/language/adl)
516  using std::sqrt;
517  Arithmetic nbEntries = static_cast<Arithmetic>( nEntries() );
518  if ( 1 > nbEntries ) return -1;
519  return sqrt( static_cast<Arithmetic>( this->nTrueEntries() * this->nFalseEntries() ) / nbEntries ) / nbEntries;
520  }
521  Arithmetic effErr() const { return efficiencyErr(); }
522  };
523 
527  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
528  struct AveragingAccumulator : AccumulatorSet<Arithmetic, Atomicity, CountAccumulator, SumAccumulator> {
529  Arithmetic mean() const
530  {
531  auto n = this->nEntries();
532  return n ? this->sum() / n : Arithmetic{};
533  }
534  };
535 
539  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
540  struct SigmaAccumulator : AccumulatorSet<Arithmetic, Atomicity, AveragingAccumulator, SquareAccumulator> {
541  Arithmetic biased_sample_variance() const
542  {
543  auto count = this->nEntries();
544  auto sum = this->sum();
545  return count ? ( this->sum2() - sum * ( sum / count ) ) / count : Arithmetic{};
546  };
547  Arithmetic unbiased_sample_variance() const
548  {
549  auto count = this->nEntries();
550  auto sum = this->sum();
551  return count ? ( this->sum2() - sum * ( sum / count ) ) / ( count - 1 ) : Arithmetic{};
552  };
553  Arithmetic standard_deviation() const
554  {
555  // Note the usage of using, aiming at using the std version of sqrt by default, without preventing
556  // more specialized versions to be used via ADL (see http://en.cppreference.com/w/cpp/language/adl)
557  using std::sqrt;
558  auto v = biased_sample_variance();
559  return ( 0 > v ) ? Arithmetic{} : sqrt( v );
560  }
561  [[deprecated( "The name 'rms' has changed to standard_deviation" )]] Arithmetic rms() const
562  {
563  return standard_deviation();
564  }
565  Arithmetic meanErr() const
566  {
567  auto n = this->nEntries();
568  if ( 0 == n ) return Arithmetic{};
569  // Note the usage of using, aiming at using the std version of sqrt by default, without preventing
570  // more specialized versions to be used via ADL (see http://en.cppreference.com/w/cpp/language/adl)
571  using std::sqrt;
572  auto v = biased_sample_variance();
573  if ( 0 > v ) return Arithmetic{};
574  return sqrt( v / n );
575  }
576  };
577 
581  template <typename Arithmetic, atomicity Atomicity = atomicity::full>
583 
589  template <typename Arithmetic, template <typename Int, atomicity Ato> class ContainedAccumulator>
590  class Buffer : public ContainedAccumulator<Arithmetic, atomicity::none>
591  {
592  using prime_type = ContainedAccumulator<Arithmetic, atomicity::full>;
593  using base_type = ContainedAccumulator<Arithmetic, atomicity::none>;
594 
595  public:
596  Buffer() = delete;
597  Buffer( prime_type& p ) : base_type(), m_prime( p ) {}
598  Buffer( const Buffer& ) = delete;
599  void operator=( const Buffer& ) = delete;
600  Buffer( Buffer&& other ) : base_type( other ), m_prime( other.m_prime ) { other.reset(); }
601  void push() { m_prime.mergeAndReset( static_cast<base_type&&>( *this ) ); }
602  ~Buffer() { push(); }
603  private:
605  };
606 
611  PrintableCounter() = default;
612  template <class OWNER>
613  PrintableCounter( OWNER* o, const std::string& tag )
614  {
615  o->registerCounter( tag, *this );
616  }
618  virtual ~PrintableCounter() = default;
620  virtual std::ostream& print( std::ostream&, bool tableFormat = false ) const = 0;
622  virtual std::ostream& print( std::ostream& o, const std::string& tag ) const
623  {
624  o << boost::format{" | %|-48.48s|%|50t|"} % ( "\"" + tag + "\"" );
625  return print( o, true );
626  }
629  {
630  std::ostringstream ost;
631  print( ost );
632  return ost.str();
633  }
634  };
635 
640  {
641  return counter.print( stream );
642  }
643 
648  template <typename Arithmetic, atomicity Atomicity, template <typename Int, atomicity Ato> class Accumulator>
651  Buffer<Arithmetic, Accumulator> buffer() { return {*static_cast<Accumulator<Arithmetic, Atomicity>*>( this )}; }
652  };
653 
657  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
658  struct Counter : BufferableCounter<Arithmetic, Atomicity, Counter>, CountAccumulator<Arithmetic, Atomicity> {
661  {
662  ( *this ) += Arithmetic{};
663  return *this;
664  }
666  {
667  auto copy = *this;
668  ++( *this );
669  return copy;
670  }
672  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override
673  {
674  std::string fmt( "#=%|-7lu|" );
675  if ( tableFormat ) {
676  fmt = "|%|7d| |";
677  }
678  o << boost::format{fmt} % this->nEntries();
679  return o;
680  }
681  };
682 
686  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
687  struct AveragingCounter : BufferableCounter<Arithmetic, Atomicity, AveragingCounter>,
688  AveragingAccumulator<Arithmetic, Atomicity> {
691  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override
692  {
693  std::string fmt;
694  if ( tableFormat ) {
695  fmt = "|%|7d| |%|11.7g| |%|#11.5g| |";
696  } else {
697  fmt = "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g|";
698  }
699  return o << boost::format{fmt} % this->nEntries() % this->sum() % this->mean();
700  }
701  };
702  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
704 
708  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
709  struct SigmaCounter : BufferableCounter<Arithmetic, Atomicity, SigmaCounter>,
710  SigmaAccumulator<Arithmetic, Atomicity> {
713  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override
714  {
715  std::string fmt;
716  if ( tableFormat ) {
717  fmt = "|%|7d| |%|11.7g| |%|#11.5g| |%|#10.5g| |";
718  } else {
719  fmt = "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g|";
720  }
721  return o << boost::format{fmt} % this->nEntries() % this->sum() % this->mean() % this->standard_deviation();
722  }
723  };
724 
728  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
729  struct StatCounter : BufferableCounter<Arithmetic, Atomicity, StatCounter>, StatAccumulator<Arithmetic, Atomicity> {
732  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override
733  {
734  std::string fmt;
735  if ( tableFormat ) {
736  fmt = "|%|7d| |%|11.7g| |%|#11.5g| |%|#10.5g| |%|#10.5g| |%|#10.5g| |";
737  } else {
738  fmt = "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|";
739  }
740  return o << boost::format{fmt} % this->nEntries() % this->sum() % this->mean() % this->standard_deviation() %
741  this->min() % this->max();
742  }
743  };
744 
745  template <typename Arithmetic = double, atomicity Atomicity = atomicity::full>
746  struct BinomialCounter : BufferableCounter<bool, Atomicity, BinomialCounter>,
747  BinomialAccumulator<Arithmetic, Atomicity> {
749  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override
750  {
751  std::string fmt;
752  if ( tableFormat ) {
753  fmt = "|%|7d| |%|11.5g| |(%|#9.7g| +- %|-#8.6g|)%%|";
754  } else {
755  fmt = "#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|";
756  }
757  return o << boost::format{fmt} % this->nEntries() % this->nTrueEntries() % ( this->efficiency() * 100 ) %
758  ( this->efficiencyErr() * 100 );
759  }
761  virtual std::ostream& print( std::ostream& o, const std::string& tag ) const override
762  {
763  // override default print to add a '*' in from of the name
764  o << boost::format{"*| %|-48.48s|%|50t|"} % ( "\"" + tag + "\"" );
765  return print( o, true );
766  }
767  };
768 
769  } // namespace Accumulators
770 
771 } // namespace Gaudi
772 
778  public Gaudi::Accumulators::AccumulatorSet<double, Gaudi::Accumulators::atomicity::full,
779  Gaudi::Accumulators::StatAccumulator,
780  Gaudi::Accumulators::BinomialAccumulator>
781 {
782 public:
788  using AccParent::reset;
790  StatEntity() = default;
791  template <class OWNER>
792  StatEntity( OWNER* o, const std::string& tag )
793  {
794  o->registerCounter( tag, *this );
795  }
796  StatEntity( const unsigned long entries, const double flag, const double flag2, const double minFlag,
797  const double maxFlag )
798  {
799  reset( std::make_tuple(
800  std::make_tuple( std::make_tuple( std::make_tuple( entries, flag ), flag2 ), minFlag, maxFlag ),
801  std::make_tuple( 0, 0 ) ) );
802  }
803  void reset() { AccParent::reset(); }
804  void operator=( double by )
805  {
806  this->reset();
807  ( *this ) += by;
808  }
809  StatEntity& operator-=( double by )
810  {
811  ( *this ) += ( -by );
812  return *this;
813  }
815  {
816  ( *this ) += 1.0;
817  return *this;
818  }
820  {
821  auto copy = *this;
822  ++( *this );
823  return copy;
824  }
826  {
827  ( *this ) += -1.0;
828  return *this;
829  }
831  {
832  auto copy = *this;
833  --( *this );
834  return copy;
835  }
836  bool operator<( const StatEntity& se ) const
837  {
838  return std::make_tuple( nEntries(), sum(), min(), max(), sum2() ) <
839  std::make_tuple( se.nEntries(), se.sum(), se.min(), se.max(), se.sum2() );
840  };
841  // using AccumulatorSet::operator+=;
842  StatEntity& operator+=( double by )
843  {
844  this->AccumulatorSet::operator+=( by );
845  return *this;
846  }
848  {
849  mergeAndReset( std::move( by ) );
850  return *this;
851  }
852  unsigned long add( const double v )
853  {
854  *this += v;
855  return nEntries();
856  }
857  unsigned long addFlag( const double v ) { return add( v ); }
858  // aliases (a'la ROOT)
859  double Sum() const { return sum(); } // get sum
860  double Mean() const { return mean(); } // get mean
861  double MeanErr() const { return meanErr(); } // get error in mean
862  double rms() const { return standard_deviation(); } // get rms
863  double Rms() const { return standard_deviation(); } // get rms
864  double RMS() const { return standard_deviation(); } // get rms
865  double Eff() const { return eff(); } // get efficiency
866  double Min() const { return min(); } // get minimal value
867  double Max() const { return max(); } // get maximal value
868  // some legacy methods, to be removed ...
869  double flag() const { return sum(); }
870  double flag2() const { return sum2(); }
871  double flagMean() const { return mean(); }
872  double flagRMS() const { return standard_deviation(); }
873  double flagMeanErr() const { return meanErr(); }
874  double flagMin() const { return min(); }
875  double flagMax() const { return max(); }
876  static bool effCounter( const std::string& name )
877  {
878  using boost::algorithm::icontains;
879  return icontains( name, "eff" ) || icontains( name, "acc" ) || icontains( name, "filt" ) ||
880  icontains( name, "fltr" ) || icontains( name, "pass" );
881  }
883  {
884  boost::format fmt{format};
885  fmt % nEntries() % sum() % mean() % standard_deviation() % min() % max();
886  return o << fmt.str();
887  }
889  std::ostream& print( std::ostream& o, bool tableFormat, const std::string& name, bool flag = true,
890  std::string fmtHead = "%|-48.48s|%|27t|" ) const
891  {
892  if ( flag && effCounter( name ) && 0 <= eff() && 0 <= effErr() && sum() <= nEntries() &&
893  ( 0 == min() || 1 == min() ) && ( 0 == max() || 1 == max() ) ) {
894  // efficiency printing
895  std::string fmt;
896  if ( tableFormat ) {
897  if ( name.empty() ) {
898  fmt = "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
899  return o << boost::format{fmt} % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
900  ( efficiencyErr() * 100 );
901  } else {
902  fmt = "*" + fmtHead + "|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%%| ------- | ------- |";
903  return o << boost::format{fmt} % ( "\"" + name + "\"" ) % BinomialAccParent::nEntries() % sum() %
904  ( efficiency() * 100 ) % ( efficiencyErr() * 100 );
905  }
906  } else {
907  fmt = "#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|";
908  return o << boost::format{fmt} % BinomialAccParent::nEntries() % sum() % ( efficiency() * 100 ) %
909  ( efficiencyErr() * 100 );
910  }
911  } else {
912  // Standard printing
913  std::string fmt;
914  if ( tableFormat ) {
915  if ( name.empty() ) {
916  fmt = "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |";
917  return o << boost::format{fmt} % nEntries() % sum() % mean() % standard_deviation() % min() % max();
918 
919  } else {
920  fmt = " " + fmtHead + "|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |";
921  return o << boost::format{fmt} % ( "\"" + name + "\"" ) % nEntries() % sum() % mean() % standard_deviation() %
922  min() % max();
923  }
924  } else {
925  fmt = "#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|";
926  return o << boost::format{fmt} % nEntries() % sum() % mean() % standard_deviation() % min() % max();
927  }
928  }
929  }
930  virtual std::ostream& print( std::ostream& o, const std::string& tag ) const override
931  {
932  return print( o, true, tag, true );
933  }
934  std::ostream& print( std::ostream& o, bool tableFormat = false ) const override
935  {
936  std::string emptyName;
937  return print( o, tableFormat, emptyName, true );
938  }
940  {
941  std::ostringstream ost;
942  print( ost );
943  return ost.str();
944  }
945  std::ostream& fillStream( std::ostream& o ) const { return print( o ); }
946 };
947 
948 #endif // GAUDIKERNEL_COUNTERS_H
constexpr unsigned long operator()(U &&) const noexcept
Definition: Counters.h:156
ContainedAccumulator< Arithmetic, atomicity::full > prime_type
Definition: Counters.h:592
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:930
std::string toString() const
Definition: Counters.h:939
static void merge(InternalType &a, Arithmetic b) noexcept
Definition: Counters.h:302
static void merge(InternalType &a, Arithmetic b) noexcept
Definition: Counters.h:316
void mergeAndReset(AccumulatorSet< Arithmetic, Ato, Bases... > &&other)
Definition: Counters.h:411
T empty(T...args)
double Min() const
Definition: Counters.h:866
Buffer(prime_type &p)
Definition: Counters.h:597
unsigned long nEntries() const
Definition: Counters.h:451
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
double Mean() const
Definition: Counters.h:860
double flagRMS() const
Definition: Counters.h:872
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:732
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:188
std::ostream & fillStream(std::ostream &o) const
Definition: Counters.h:945
unsigned long nEntries() const
Definition: Counters.h:504
unsigned long nFalseEntries() const
Definition: Counters.h:495
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:749
An empty ancester of all counters that provides a buffer method that returns a buffer on itself...
Definition: Counters.h:649
double sum(double x, double y, double z)
unsigned long nTrueEntries() const
Definition: Counters.h:481
double flagMin() const
Definition: Counters.h:874
atomicity
Defines atomicity of the accumulators.
Definition: Counters.h:148
A counter aiming at computing average and sum2 / variance / standard deviation.
Definition: Counters.h:729
typename Gaudi::cpp17::is_detected< has_fetch_add_, T >::value_t has_fetch_add
Definition: Counters.h:190
Gaudi::Accumulators::BinomialAccumulator< double, Gaudi::Accumulators::atomicity::full > BinomialAccParent
Definition: Counters.h:786
An Adder ValueHandler operator(a, b) means a += b.
Definition: Counters.h:228
Gaudi::Accumulators::AccumulatorSet< double, Gaudi::Accumulators::atomicity::full, Gaudi::Accumulators::StatAccumulator, Gaudi::Accumulators::BinomialAccumulator > AccParent
Definition: Counters.h:785
A counter aiming at computing sum and average.
Definition: Counters.h:687
Arithmetic unbiased_sample_variance() const
Definition: Counters.h:547
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:31
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:622
T make_tuple(T...args)
An functor always returning the value N.
Definition: Counters.h:154
void reset()
Definition: Counters.h:803
An empty ancester of all counters that knows how to print themselves.
Definition: Counters.h:610
double flagMeanErr() const
Definition: Counters.h:873
std::ostream & printFormatted(std::ostream &o, const std::string &format) const
Definition: Counters.h:882
bool operator<(const StatEntity &se) const
Definition: Counters.h:836
StatEntity operator++(int)
Definition: Counters.h:819
Arithmetic standard_deviation() const
Definition: Counters.h:553
static void add(InternalType &a, T b, std::true_type)
Definition: Counters.h:273
std::decay_t< std::result_of_t< Identity(unsigned long)>> OutputType
Definition: Counters.h:364
static constexpr OutputType getValue(const InternalType &v) noexcept
Definition: Counters.h:216
double Max() const
Definition: Counters.h:867
double flagMax() const
Definition: Counters.h:875
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:934
STL class.
StatEntity & operator++()
Definition: Counters.h:814
int N
Definition: IOTest.py:101
PrintableCounter(OWNER *o, const std::string &tag)
Definition: Counters.h:613
GenericAccumulator operator+=(const InputType by)
Definition: Counters.h:365
static void add(InternalType &a, T b, std::false_type)
Definition: Counters.h:266
static Arithmetic exchange(InternalType &v, Arithmetic newv) noexcept
Definition: Counters.h:206
double MeanErr() const
Definition: Counters.h:861
double flagMean() const
Definition: Counters.h:871
double Eff() const
Definition: Counters.h:865
unsigned int operator()(bool v) const
Definition: Counters.h:472
AccumulatorSet & operator+=(const InputType by)
Definition: Counters.h:403
StatEntity & operator-=(double by)
Definition: Counters.h:809
StatEntity(OWNER *o, const std::string &tag)
Definition: Counters.h:792
A Square functor.
Definition: Counters.h:176
Buffer is a non atomic Accumulator which, when it goes out-of-scope, updates the underlying thread-sa...
Definition: Counters.h:590
Buffer< Arithmetic, Accumulator > buffer()
Definition: Counters.h:651
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:761
Arithmetic biased_sample_variance() const
Definition: Counters.h:541
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:889
virtual std::ostream & print(std::ostream &, bool tableFormat=false) const =0
prints the counter to a stream
double Sum() const
Definition: Counters.h:859
GenericAccumulator & operator=(const GenericAccumulator &other)
Definition: Counters.h:372
std::string toString() const
get a string representation
Definition: Counters.h:628
A counter aiming at computing average and sum2 / variance / standard deviation.
Definition: Counters.h:709
void mergeAndReset(GenericAccumulator< InputType, InnerType, ato, InputTransform, OutputTransform, VH > &&other)
Definition: Counters.h:380
T move(T...args)
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:713
AccumulatorSet is an Accumulator that holds a set of Accumulators templated by same Arithmetic and At...
Definition: Counters.h:397
StatEntity & operator+=(StatEntity by)
Definition: Counters.h:847
Buffer(Buffer &&other)
Definition: Counters.h:600
AccumulatorSet< Arithmetic, Atomicity, SigmaAccumulator, MinAccumulator, MaxAccumulator > StatAccumulator
StatAccumulator.
Definition: Counters.h:582
StatEntity & operator--()
Definition: Counters.h:825
void reset(const std::tuple< typename Bases< Arithmetic, Atomicity >::OutputType... > &t)
Definition: Counters.h:418
T fetch_add(T...args)
static bool effCounter(const std::string &name)
Definition: Counters.h:876
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
ValueHandler::InternalType m_value
Definition: Counters.h:389
std::ostream & operator<<(std::ostream &stream, const PrintableCounter &counter)
external printout operator to std::ostream
Definition: Counters.h:639
helper functor for the TrueAccumulator
Definition: Counters.h:471
helper functor for the FalseAccumulator
Definition: Counters.h:485
An Identity functor.
Definition: Counters.h:165
unsigned long addFlag(const double v)
Definition: Counters.h:857
Base type for all functors used as ValuesHandler.
Definition: Counters.h:196
GenericAccumulator(const GenericAccumulator &other)
Definition: Counters.h:371
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:691
A basic counter counting input values.
Definition: Counters.h:658
T compare_exchange_weak(T...args)
double Rms() const
Definition: Counters.h:863
static void merge(InternalType &a, Arithmetic b) noexcept
Definition: Counters.h:238
static void merge(InternalType &a, Arithmetic b) noexcept
Definition: Counters.h:279
static Arithmetic exchange(InternalType &v, Arithmetic newv) noexcept
Definition: Counters.h:220
T sqrt(T...args)
double RMS() const
Definition: Counters.h:864
StatEntity(const unsigned long entries, const double flag, const double flag2, const double minFlag, const double maxFlag)
Definition: Counters.h:796
static constexpr OutputType getValue(const InternalType &v) noexcept
Definition: Counters.h:205
double rms() const
Definition: Counters.h:862
backward compatible StatEntity class.
Definition: Counters.h:777
StatEntity operator--(int)
Definition: Counters.h:830
double flag2() const
Definition: Counters.h:870
StatEntity & operator+=(double by)
Definition: Counters.h:842
ContainedAccumulator< Arithmetic, atomicity::none > base_type
Definition: Counters.h:593
STL class.
An Extremum ValueHandler, to be reused for Minimum and Maximum operator(a, b) means if (Compare(b...
Definition: Counters.h:292
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
Definition: Counters.h:672
unsigned int operator()(bool v) const
Definition: Counters.h:486
Generic Accumulator, templated by.
Definition: Counters.h:358
Helper functions to set/get the application return code.
Definition: __init__.py:1
void operator=(double by)
Definition: Counters.h:804
double flag() const
Definition: Counters.h:869
T load(T...args)
unsigned long add(const double v)
Definition: Counters.h:852