16 template <
typename,
typename>
20 template <
class Rep,
class Period>
22 template <
class Rep1,
class Rep2,
class Period>
153 #include "boost/algorithm/string/predicate.hpp" 154 #include "boost/format.hpp" 161 #include <type_traits> 180 template <
typename T, T N>
182 template <
typename U>
192 template <
typename U>
193 constexpr decltype(
auto ) operator()( U&& v )
const noexcept {
194 return std::forward<U>( v );
202 template <
typename U>
203 constexpr decltype(
auto ) operator()( U&& v )
const noexcept {
211 template <
typename T,
typename =
int>
213 template <
typename T>
214 inline constexpr
bool has_fetch_add_v = Gaudi::cpp17::is_detected_v<has_fetch_add_, T>;
219 template <
typename Arithmetic,
typename Result =
double>
220 using fp_result_type = std::conditional_t<std::is_integral_v<Arithmetic>, Result, Arithmetic>;
225 template <
typename Arithmetic, atomicity Atomicity>
231 template <
typename Arithmetic>
242 template <
typename Arithmetic>
247 return v.load( std::memory_order_relaxed );
256 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
262 template <
typename Arithmetic>
273 template <
typename Arithmetic>
279 if ( DefaultValue() == b )
return;
280 if constexpr ( has_fetch_add_v<InternalType> ) {
281 a.fetch_add( b, std::memory_order_relaxed );
284 while ( !a.compare_exchange_weak( current, current + b ) )
294 template <
typename Arithmetic, atomicity Atomicity,
typename Compare, Arithmetic ( *Initial )()>
300 template <
typename Arithmetic,
typename Compare, Arithmetic ( *Initial )()>
306 if ( Compare{}( b, a ) ) a = b;
313 template <
typename Arithmetic,
typename Compare, Arithmetic ( *Initial )()>
320 while ( Compare{}( b, prev_value ) && !a.compare_exchange_weak( prev_value, b ) )
329 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
336 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
356 template <
typename InputType,
typename InnerType,
atomicity Atomicity = atomicity::full,
360 template <
typename,
typename, atomicity,
typename,
typename,
typename>
364 using OutputType = std::decay_t<std::result_of_t<OutputTransform( InnerType )>>;
370 template <
typename... Args>
374 m_value = ValueHandler::getValue( other.
m_value );
377 OutputType value()
const {
return OutputTransform{}( ValueHandler::getValue( m_value ) ); }
379 template <atomicity ato,
typename VH>
388 typename ValueHandler::InternalType m_value{ValueHandler::DefaultValue()};
396 template <
typename Arithmetic, atomicity Atomicity,
template <
typename, atomicity>
class... Bases>
403 ( Bases<Arithmetic, Atomicity>::operator+=( by ), ... );
407 void reset() { ( Bases<Arithmetic, Atomicity>::reset(), ... ); }
408 template <atomicity Ato>
410 ( Bases<Arithmetic, Atomicity>::mergeAndReset(
static_cast<Bases<Arithmetic, Ato>&&
>( other ) ), ... );
414 void reset(
const std::tuple<
typename Bases<Arithmetic, Atomicity>::OutputType...>& t ) {
415 std::apply( [
this](
const auto&... i ) { ( this->Bases<Arithmetic, Atomicity>::reset( i ), ... ); }, t );
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(); }
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(); }
443 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
445 unsigned long nEntries()
const {
return this->value(); }
452 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
454 Arithmetic
sum()
const {
return this->value(); }
461 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
463 Arithmetic
sum2()
const {
return this->value(); };
476 template <
typename Arithmetic, atomicity Atomicity>
492 template <
typename Arithmetic, atomicity Atomicity>
503 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
505 unsigned long nEntries()
const {
return this->nTrueEntries() + this->nFalseEntries(); };
507 template <
typename Result = fp_result_type<Arithmetic>>
510 if ( 1 > nbEntries )
return Result{-1};
511 return static_cast<Result>( this->nTrueEntries() ) / nbEntries;
513 auto eff()
const {
return efficiency(); }
515 template <
typename Result = fp_result_type<Arithmetic>>
522 if ( 1 > nbEntries )
return Result{-1};
523 return sqrt( static_cast<Result>( this->nTrueEntries() * this->nFalseEntries() ) / nbEntries ) / nbEntries;
525 auto effErr()
const {
return efficiencyErr(); }
544 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
547 template <
typename Result = fp_result_type<Arithmetic>>
550 Result sum = this->sum();
551 return (
n > 0 ) ? static_cast<Result>( sum /
n ) : Result{};
559 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
562 template <
typename Result = fp_result_type<Arithmetic>>
565 Result sum = this->sum();
566 return (
n > 0 ) ? static_cast<Result>( ( this->sum2() - sum * ( sum /
n ) ) /
n ) : Result{};
569 template <
typename Result = fp_result_type<Arithmetic>>
572 Result sum = this->sum();
573 return (
n > 1 ) ? static_cast<Result>( ( this->sum2() - sum * ( sum /
n ) ) / (
n - 1 ) ) : Result{};
576 template <
typename Result = fp_result_type<Arithmetic>>
582 Result v = biased_sample_variance();
583 return ( Result{0} > v ) ? Result{} : static_cast<Result>(
sqrt( v ) );
585 [[deprecated(
"The name 'rms' has changed to standard_deviation" )]] Arithmetic
rms()
const {
586 return standard_deviation();
589 template <
typename Result = fp_result_type<Arithmetic>>
592 if ( 0 ==
n )
return Result{};
597 Result v = biased_sample_variance();
598 return ( Result{0} > v ) ? Result{} : static_cast<Result>(
sqrt( v /
n ) );
606 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
615 template <
typename Arithmetic,
template <
typename Int, atomicity Ato>
class ContainedAccumulator>
616 class Buffer :
public ContainedAccumulator<Arithmetic, atomicity::none> {
617 using prime_type = ContainedAccumulator<Arithmetic, atomicity::full>;
618 using base_type = ContainedAccumulator<Arithmetic, atomicity::none>;
624 void operator=(
const Buffer& ) =
delete;
626 void push() { m_prime.mergeAndReset( static_cast<base_type&&>( *
this ) ); }
639 template <
class OWNER>
641 o->declareCounter( tag, *
this );
646 template <
typename stream>
648 s <<
boost::format{
" | %|-48.48s|%|50t|"} % (
"\"" + tag +
"\"" );
649 return print(
s,
true );
678 template <
typename Arithmetic, atomicity Atomicity,
template <
typename Int, atomicity Ato>
class Accumulator>
680 using PrintableCounter::PrintableCounter;
688 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
692 ( *this ) += Arithmetic{};
702 template <
typename stream>
703 stream&
printImpl( stream& o,
bool tableFormat )
const {
705 auto fmt = ( tableFormat ?
"|%|10d| |" :
"#=%|-7lu|" );
710 return printImpl( o, tableFormat );
720 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
726 template <
typename stream>
727 stream&
printImpl( stream& o,
bool tableFormat )
const {
728 auto fmt = ( tableFormat ?
"|%|10d| |%|11.7g| |%|#11.5g| |" :
"#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g|" );
733 return printImpl( o, tableFormat );
739 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
746 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
752 template <
typename stream>
753 stream&
printImpl( stream& o,
bool tableFormat )
const {
754 auto fmt = ( tableFormat ?
"|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |" 755 :
"#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g|" );
760 return printImpl( o, tableFormat );
770 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
775 template <
typename stream>
776 stream&
printImpl( stream& o,
bool tableFormat )
const {
777 auto fmt = ( tableFormat ?
"|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |" 778 :
"#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|" );
780 this->
min() % this->
max();
784 return printImpl( o, tableFormat );
794 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
799 template <
typename stream>
800 stream&
printImpl( stream& o,
bool tableFormat )
const {
801 auto fmt = ( tableFormat ?
"|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%% |" 802 :
"#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|" );
803 return o <<
boost::format{fmt} % this->
nEntries() % this->nTrueEntries() % ( this->efficiency() * 100 ) %
804 ( this->efficiencyErr() * 100 );
808 return printImpl( o, tableFormat );
812 template <
typename stream>
815 o <<
boost::format{
" |*%|-48.48s|%|50t|"} % (
"\"" + tag +
"\"" );
816 return print( o,
true );
832 parent->
msgStream( lvl ) <<
"Suppressing message: " << std::quoted(
msg,
'\'' ) <<
endmsg;
839 template <atomicity Atomicity>
841 std::conditional_t<Atomicity == atomicity::none, unsigned long, std::atomic<unsigned long>>
count = {0};
847 template <atomicity OtherAtomicity>
855 template <atomicity Atomicity>
870 template <atomicity Atomicity>
877 template <MSG::Level level, atomicity Atomicity = atomicity::full>
880 details::MsgCounter::Logger,
882 details::MsgCounter::OutputTransform, details::MsgCounter::Handler<Atomicity>> {
883 template <
typename OWNER>
888 details::MsgCounter::OutputTransform, details::MsgCounter::Handler<Atomicity>>{
889 std::in_place,
this, nMax} {}
894 template <
typename stream>
895 stream&
printImpl( stream& o,
bool tableFormat )
const {
896 return o <<
boost::format{tableFormat ?
"|%|10d| |" :
"#=%|-7lu|"} % this->value();
898 using PrintableCounter::print;
909 template <
typename Counter,
typename Container,
typename Fun>
912 for (
const auto& elem : container ) b += f( elem );
void mergeAndReset(GenericAccumulator< InputType, InnerType, ato, InputTransform, OutputTransform, VH > &&other)
static void merge(InternalType &orig, bool b)
Data(Data< OtherAtomicity > const &other)
Definition of the MsgStream class used to transmit messages.
unsigned long nEntries() const
An Adder ValueHandler operator(a, b) means a += b.
Buffer is a non atomic Accumulator which, when it goes out-of-scope, updates the underlying thread-sa...
bool toBePrinted() const override
hint whether we should print that counter or not.
bool toBePrinted() const override
hint whether we should print that counter or not.
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
std::string toString() const
get a string representation
A counter aiming at computing average and sum2 / variance / standard deviation.
stream & printImpl(stream &o, const std::string &tag) const
BinomialAccumulator & operator+=(binomial_t b)
bool toBePrinted() const override
hint whether we should print that counter or not.
std::conditional_t< std::is_integral_v< Arithmetic >, Result, Arithmetic > fp_result_type
type_trait for the result type of a floating point operation on the type Arithmetic
stream & printImpl(stream &o, bool tableFormat) const
AccumulatorSet is an Accumulator that holds a set of Accumulators templated by same Arithmetic and At...
A counter dealing with binomial data.
helper functor for the FalseAccumulator
GenericAccumulator(const GenericAccumulator &other)
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
static constexpr OutputType DefaultValue()
int merge(const char *target, const char *source, bool fixup=false, bool dbg=true)
MsgStream & print(MsgStream &os, bool tableFormat) const override
constexpr Data(Logger const *p, unsigned long mx)
stream & printImpl(stream &o, bool tableFormat) const
bool toBePrinted() const override
hint whether we should print that counter or not.
std::ostream & print(std::ostream &os, bool tableFormat) const override
prints the counter to a stream
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
unsigned long nTrueEntries() const
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
helper functor for the TrueAccumulator
Generic Accumulator, templated by.
auto unbiased_sample_variance() const
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
auto operator *(const std::chrono::duration< Rep1, Period > &lhs, const std::chrono::duration< Rep2, Period > &rhs)
Multiplication of two std::chrono::duration objects with same Period.
Buffer< Arithmetic, Accumulator > buffer()
A functor always returning the value N.
An empty ancester of all counters that provides a buffer method that returns a buffer on itself.
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...
ValueHandler::InternalType m_value
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
void reset(const std::tuple< typename Bases< Arithmetic, Atomicity >::OutputType... > &t)
A counter aiming at computing sum and average.
static constexpr OutputType getValue(const InternalType &v) noexcept
GAUDIPS_API Logger & logger()
Return the current logger instance.
static void merge(InternalType &a, Arithmetic b) noexcept
stream & printImpl(stream &s, const std::string &tag) const
auto standard_deviation() const
static constexpr OutputType DefaultValue()
std::conditional_t< Atomicity==atomicity::none, unsigned long, std::atomic< unsigned long > > count
GenericAccumulator operator+=(const InputType by)
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
void mergeAndReset(AccumulatorSet< Arithmetic, Ato, Bases... > &&other)
static constexpr OutputType getValue(InternalType const &v) noexcept
Base type for all functors used as ValuesHandler.
auto efficiencyErr() const
GenericAccumulator & operator=(const GenericAccumulator &other)
ContainedAccumulator< Arithmetic, atomicity::full > prime_type
constexpr bool has_fetch_add_v
GenericAccumulator(std::in_place_t, Args &&... args)
MsgStream & msgStream() const
Return an uninitialized MsgStream.
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
static void merge(InternalType &a, Arithmetic b) noexcept
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
unsigned int operator()(bool v) const
virtual MsgStream & print(MsgStream &o, const std::string &tag) const
auto sqrt(std::chrono::duration< Rep, Period > d)
sqrt for std::chrono::duration
static void merge(InternalType &a, Arithmetic b) noexcept
stream & printImpl(stream &o, bool tableFormat) const
unsigned int operator()(bool v) const
static Arithmetic exchange(InternalType &v, Arithmetic newv) noexcept
atomicity
Defines atomicity of the accumulators.
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
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
A counter aiming at computing average and sum2 / variance / standard deviation.
constexpr T operator()(U &&) const noexcept
A basic counter counting input values.
static void merge(InternalType &a, Arithmetic b) noexcept
MsgCounter(OWNER *o, std::string const &msg, int nMax=10)
AccumulatorSet & operator+=(const InputType by)
void operator()(bool suppress=false) const
virtual bool toBePrinted() const
hint whether we should print that counter or not.
stream & printImpl(stream &o, bool tableFormat) const
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
std::decay_t< std::result_of_t< Identity(unsigned long)> > OutputType
static constexpr OutputType DefaultValue()
An empty ancester of all counters that knows how to print themselves.
stream & printImpl(stream &o, bool tableFormat) const
stream & printImpl(stream &o, bool tableFormat) const
An Extremum ValueHandler, to be reused for Minimum and Maximum operator(a, b) means if (Compare(b,...
ContainedAccumulator< Arithmetic, atomicity::none > base_type
static Arithmetic exchange(InternalType &v, Arithmetic newv) noexcept
bool toBePrinted() const override
hint whether we should print that counter or not.
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
MsgStream & print(MsgStream &o, const std::string &tag) const override
Efficient counter implementations for Gaudi.
unsigned long nEntries() const
bool toBePrinted() const override
hint whether we should print that counter or not.
MsgCounter & operator++()
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
static constexpr OutputType DefaultValue()
decltype(std::atomic< T >{}.fetch_add(0)) has_fetch_add_
type_traits for checking the presence of fetch_add in std::atomic<T>
unsigned long nFalseEntries() const
static constexpr OutputType getValue(const InternalType &v) noexcept
auto biased_sample_variance() const
std::ostream & operator<<(std::ostream &o, const Gaudi::StringKey &key)
printout of the object reply on the native printout for the string
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
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