17 template <
class Rep,
class Period>
19 template <
class Rep1,
class Rep2,
class Period>
150 #include "boost/algorithm/string/predicate.hpp" 151 #include "boost/format.hpp" 158 #include <type_traits> 177 template <
typename T, T N>
179 template <
typename U>
189 template <
typename U>
190 constexpr decltype(
auto ) operator()( U&& v )
const noexcept {
191 return std::forward<U>( v );
199 template <
typename U>
200 constexpr decltype(
auto ) operator()( U&& v )
const noexcept {
208 template <
typename T,
typename =
int>
210 template <
typename T>
211 inline constexpr
bool has_fetch_add_v = Gaudi::cpp17::is_detected_v<has_fetch_add_, T>;
216 template <
typename Arithmetic,
typename Result =
double>
217 using fp_result_type = std::conditional_t<std::is_integral_v<Arithmetic>, Result, Arithmetic>;
222 template <
typename Arithmetic, atomicity Atomicity>
228 template <
typename Arithmetic>
239 template <
typename Arithmetic>
244 return v.load( std::memory_order_relaxed );
253 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
259 template <
typename Arithmetic>
270 template <
typename Arithmetic>
276 if ( DefaultValue() == b )
return;
277 if constexpr ( has_fetch_add_v<InternalType> ) {
278 a.fetch_add( b, std::memory_order_relaxed );
281 while ( !a.compare_exchange_weak( current, current + b ) )
291 template <
typename Arithmetic, atomicity Atomicity,
typename Compare, Arithmetic ( *Initial )()>
297 template <
typename Arithmetic,
typename Compare, Arithmetic ( *Initial )()>
303 if ( Compare{}( b, a ) ) a = b;
310 template <
typename Arithmetic,
typename Compare, Arithmetic ( *Initial )()>
317 while ( Compare{}( b, prev_value ) && !a.compare_exchange_weak( prev_value, b ) )
326 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
333 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
353 template <
typename InputType,
typename InnerType,
atomicity Atomicity = atomicity::full,
357 template <
typename,
typename, atomicity,
typename,
typename,
typename>
361 using OutputType = std::decay_t<std::result_of_t<OutputTransform( InnerType )>>;
367 template <
typename... Args>
371 m_value = ValueHandler::getValue( other.
m_value );
374 OutputType value()
const {
return OutputTransform{}( ValueHandler::getValue( m_value ) ); }
376 template <atomicity ato,
typename VH>
385 typename ValueHandler::InternalType m_value{ValueHandler::DefaultValue()};
393 template <
typename Arithmetic, atomicity Atomicity,
template <
typename, atomicity>
class... Bases>
400 ( Bases<Arithmetic, Atomicity>::operator+=( by ), ... );
404 void reset() { ( Bases<Arithmetic, Atomicity>::reset(), ... ); }
405 template <atomicity Ato>
407 ( Bases<Arithmetic, Atomicity>::mergeAndReset(
static_cast<Bases<Arithmetic, Ato>&&
>( other ) ), ... );
411 void reset(
const std::tuple<
typename Bases<Arithmetic, Atomicity>::OutputType...>& t ) {
412 std::apply( [
this](
const auto&... i ) { ( this->Bases<Arithmetic, Atomicity>::reset( i ), ... ); }, t );
420 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
422 :
GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Identity, Identity, Maximum<Arithmetic, Atomicity>> {
423 Arithmetic
max()
const {
return this->value(); }
430 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
432 :
GenericAccumulator<Arithmetic, Arithmetic, Atomicity, Identity, Identity, Minimum<Arithmetic, Atomicity>> {
433 Arithmetic
min()
const {
return this->value(); }
440 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
442 unsigned long nEntries()
const {
return this->value(); }
449 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
451 Arithmetic
sum()
const {
return this->value(); }
458 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
460 Arithmetic
sum2()
const {
return this->value(); };
473 template <
typename Arithmetic, atomicity Atomicity>
489 template <
typename Arithmetic, atomicity Atomicity>
500 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
502 unsigned long nEntries()
const {
return this->nTrueEntries() + this->nFalseEntries(); };
504 template <
typename Result = fp_result_type<Arithmetic>>
507 if ( 1 > nbEntries )
return Result{-1};
508 return static_cast<Result>( this->nTrueEntries() ) / nbEntries;
510 auto eff()
const {
return efficiency(); }
512 template <
typename Result = fp_result_type<Arithmetic>>
519 if ( 1 > nbEntries )
return Result{-1};
520 return sqrt( static_cast<Result>( this->nTrueEntries() * this->nFalseEntries() ) / nbEntries ) / nbEntries;
522 auto effErr()
const {
return efficiencyErr(); }
541 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
544 template <
typename Result = fp_result_type<Arithmetic>>
547 Result sum = this->sum();
548 return (
n > 0 ) ? static_cast<Result>( sum /
n ) : Result{};
556 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
559 template <
typename Result = fp_result_type<Arithmetic>>
562 Result sum = this->sum();
563 return (
n > 0 ) ? static_cast<Result>( ( this->sum2() - sum * ( sum /
n ) ) /
n ) : Result{};
566 template <
typename Result = fp_result_type<Arithmetic>>
569 Result sum = this->sum();
570 return (
n > 1 ) ? static_cast<Result>( ( this->sum2() - sum * ( sum /
n ) ) / (
n - 1 ) ) : Result{};
573 template <
typename Result = fp_result_type<Arithmetic>>
579 Result v = biased_sample_variance();
580 return ( Result{0} > v ) ? Result{} : static_cast<Result>(
sqrt( v ) );
582 [[deprecated(
"The name 'rms' has changed to standard_deviation" )]] Arithmetic
rms()
const {
583 return standard_deviation();
586 template <
typename Result = fp_result_type<Arithmetic>>
589 if ( 0 ==
n )
return Result{};
594 Result v = biased_sample_variance();
595 return ( Result{0} > v ) ? Result{} : static_cast<Result>(
sqrt( v /
n ) );
603 template <
typename Arithmetic, atomicity Atomicity = atomicity::full>
612 template <
typename Arithmetic,
template <
typename Int, atomicity Ato>
class ContainedAccumulator>
613 class Buffer :
public ContainedAccumulator<Arithmetic, atomicity::none> {
614 using prime_type = ContainedAccumulator<Arithmetic, atomicity::full>;
615 using base_type = ContainedAccumulator<Arithmetic, atomicity::none>;
621 void operator=(
const Buffer& ) =
delete;
623 void push() { m_prime.mergeAndReset( static_cast<base_type&&>( *
this ) ); }
636 template <
class OWNER>
638 o->declareCounter(
std::move( tag ), *
this );
643 template <
typename stream>
646 return print(
s,
true );
675 template <
typename Arithmetic, atomicity Atomicity,
template <
typename Int, atomicity Ato>
class Accumulator>
677 using PrintableCounter::PrintableCounter;
685 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
689 ( *this ) += Arithmetic{};
699 template <
typename stream>
700 stream&
printImpl( stream& o,
bool tableFormat )
const {
702 auto fmt = ( tableFormat ?
"|%|10d| |" :
"#=%|-7lu|" );
707 return printImpl( o, tableFormat );
717 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
723 template <
typename stream>
724 stream&
printImpl( stream& o,
bool tableFormat )
const {
725 auto fmt = ( tableFormat ?
"|%|10d| |%|11.7g| |%|#11.5g| |" :
"#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g|" );
730 return printImpl( o, tableFormat );
736 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
743 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
749 template <
typename stream>
750 stream&
printImpl( stream& o,
bool tableFormat )
const {
751 auto fmt = ( tableFormat ?
"|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |" 752 :
"#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g|" );
757 return printImpl( o, tableFormat );
767 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
772 template <
typename stream>
773 stream&
printImpl( stream& o,
bool tableFormat )
const {
774 auto fmt = ( tableFormat ?
"|%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |" 775 :
"#=%|-7lu| Sum=%|-11.5g| Mean=%|#10.4g| +- %|-#10.5g| Min/Max=%|#10.4g|/%|-#10.4g|" );
777 this->
min() % this->
max();
781 return printImpl( o, tableFormat );
791 template <
typename Arithmetic =
double, atomicity Atomicity = atomicity::full>
796 template <
typename stream>
797 stream&
printImpl( stream& o,
bool tableFormat )
const {
798 auto fmt = ( tableFormat ?
"|%|10d| |%|11.5g| |(%|#9.7g| +- %|-#8.7g|)%% |" 799 :
"#=%|-7lu| Sum=%|-11.5g| Eff=|(%|#9.7g| +- %|-#8.6g|)%%|" );
800 return o <<
boost::format{fmt} % this->
nEntries() % this->nTrueEntries() % ( this->efficiency() * 100 ) %
801 ( this->efficiencyErr() * 100 );
805 return printImpl( o, tableFormat );
809 template <
typename stream>
810 stream&
printImpl( stream& o, std::string_view tag )
const {
813 return print( o,
true );
836 template <atomicity Atomicity>
838 std::conditional_t<Atomicity == atomicity::none, unsigned long, std::atomic<unsigned long>> count = {0};
844 template <atomicity OtherAtomicity>
852 template <atomicity Atomicity>
861 auto count = ++orig.
count;
867 template <atomicity Atomicity>
874 template <MSG::Level level, atomicity Atomicity = atomicity::full>
877 details::MsgCounter::Logger,
879 details::MsgCounter::OutputTransform, details::MsgCounter::Handler<Atomicity>> {
880 template <
typename OWNER>
885 details::MsgCounter::OutputTransform, details::MsgCounter::Handler<Atomicity>>{
886 std::in_place,
this, nMax} {}
891 template <
typename stream>
892 stream&
printImpl( stream& o,
bool tableFormat )
const {
893 return o <<
boost::format{tableFormat ?
"|%|10d| |" :
"#=%|-7lu|"} % this->value();
895 using PrintableCounter::print;
906 template <
typename Counter,
typename Container,
typename Fun>
909 for (
const auto& elem : container ) b += f( elem );
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
unsigned long nEntries() const
ContainedAccumulator< Arithmetic, atomicity::none > base_type
virtual std::ostream & print(std::ostream &o, std::string_view tag) const
prints the counter to a stream in table format, with the given tag
An empty ancester of all counters that knows how to print themselves.
A counter aiming at computing sum and average.
Definition of the MsgStream class used to transmit messages.
GenericAccumulator(std::in_place_t, Args &&... args)
Generic Accumulator, templated by.
virtual MsgStream & print(MsgStream &o, std::string_view tag) const
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(...)".
Buffer is a non atomic Accumulator which, when it goes out-of-scope, updates the underlying thread-sa...
unsigned long nFalseEntries() const
static constexpr OutputType DefaultValue()
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
A functor always returning the value N.
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
A basic counter counting input values.
atomicity
Defines atomicity of the accumulators.
static Arithmetic exchange(InternalType &v, Arithmetic newv) noexcept
Buffer< Arithmetic, Accumulator > buffer()
int merge(const char *target, const char *source, bool fixup=false, bool dbg=true)
MsgCounter & operator++()
decltype(std::atomic< T >{}.fetch_add(0)) has_fetch_add_
type_traits for checking the presence of fetch_add in std::atomic<T>
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
Base type for all functors used as ValuesHandler.
stream & printImpl(stream &o, bool tableFormat) const
MsgStream & print(MsgStream &o, std::string_view tag) const override
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
MsgCounter(OWNER *o, std::string const &msg, int nMax=10)
std::conditional_t< Atomicity==atomicity::none, unsigned long, std::atomic< unsigned long > > count
stream & printImpl(stream &o, bool tableFormat) const
auto efficiencyErr() const
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
constexpr bool has_fetch_add_v
unsigned int operator()(bool v) const
void operator()(bool suppress=false) const
ValueHandler::InternalType m_value
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.
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
auto standard_deviation() const
auto biased_sample_variance() const
BinomialAccumulator & operator+=(binomial_t b)
static Arithmetic exchange(InternalType &v, Arithmetic newv) noexcept
static void merge(InternalType &a, Arithmetic b) noexcept
GAUDIPS_API Logger & logger()
Return the current logger instance.
bool toBePrinted() const override
hint whether we should print that counter or not.
unsigned int operator()(bool v) const
stream & printImpl(stream &o, bool tableFormat) const
static void merge(InternalType &a, Arithmetic b) noexcept
ContainedAccumulator< Arithmetic, atomicity::full > prime_type
void mergeAndReset(AccumulatorSet< Arithmetic, Ato, Bases... > &&other)
stream & printImpl(stream &o, std::string_view tag) const
std::string toString() const
get a string representation
GenericAccumulator & operator=(const GenericAccumulator &other)
helper functor for the TrueAccumulator
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
auto unbiased_sample_variance() const
MsgStream & msgStream() const
Return an uninitialized MsgStream.
A counter dealing with binomial data.
AccumulatorSet is an Accumulator that holds a set of Accumulators templated by same Arithmetic and At...
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
bool toBePrinted() const override
hint whether we should print that counter or not.
An Adder ValueHandler operator(a, b) means a += b.
static constexpr OutputType getValue(const InternalType &v) noexcept
auto sqrt(std::chrono::duration< Rep, Period > d)
sqrt for std::chrono::duration
bool toBePrinted() const override
hint whether we should print that counter or not.
static constexpr OutputType DefaultValue()
void reset(const std::tuple< typename Bases< Arithmetic, Atomicity >::OutputType... > &t)
stream & printImpl(stream &o, bool tableFormat) const
A counter aiming at computing average and sum2 / variance / standard deviation.
Data(Data< OtherAtomicity > const &other)
unsigned long nTrueEntries() const
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
virtual bool toBePrinted() const
hint whether we should print that counter or not.
constexpr Data(Logger const *p, unsigned long mx)
GenericAccumulator(const GenericAccumulator &other)
static constexpr OutputType getValue(const InternalType &v) noexcept
static void merge(InternalType &a, Arithmetic b) noexcept
bool toBePrinted() const override
hint whether we should print that counter or not.
GenericAccumulator operator+=(const InputType by)
static void merge(InternalType &orig, bool b)
std::ostream & print(std::ostream &o, std::string_view tag) const override
prints the counter to a stream in table format, with the given tag
void mergeAndReset(GenericAccumulator< InputType, InnerType, ato, InputTransform, OutputTransform, VH > &&other)
constexpr T operator()(U &&) const noexcept
bool toBePrinted() const override
hint whether we should print that counter or not.
stream & printImpl(stream &s, std::string_view tag) const
auto sqrt(T d)
forward declaration of sqrt for custom types
const Gaudi::Algorithm & parent
stream & printImpl(stream &o, bool tableFormat) const
std::decay_t< std::result_of_t< Identity(unsigned long)> > OutputType
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
MsgStream & print(MsgStream &o, bool tableFormat=false) const override
An empty ancester of all counters that provides a buffer method that returns a buffer on itself.
MsgStream & print(MsgStream &os, bool tableFormat) const override
A counter aiming at computing average and sum2 / variance / standard deviation.
unsigned long nEntries() const
stream & printImpl(stream &o, bool tableFormat) const
static constexpr OutputType DefaultValue()
Efficient counter implementations for Gaudi.
An Extremum ValueHandler, to be reused for Minimum and Maximum operator(a, b) means if (Compare(b,...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
static constexpr OutputType DefaultValue()
std::ostream & print(std::ostream &o, bool tableFormat=false) const override
prints the counter to a stream
static constexpr OutputType getValue(InternalType const &v) noexcept
AccumulatorSet & operator+=(const InputType by)
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...
helper functor for the FalseAccumulator
std::ostream & operator<<(std::ostream &o, const Gaudi::StringKey &key)
printout of the object reply on the native printout for the string
std::ostream & print(std::ostream &os, bool tableFormat) const override
prints the counter to a stream