yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
YODA::Counter Class Reference

A weighted counter. More...

#include <Counter.h>

Inheritance diagram for YODA::Counter:
YODA::AnalysisObject

Public Types

typedef std::tuple FillType
 
typedef std::shared_ptr< CounterPtr
 
- Public Types inherited from YODA::AnalysisObject
typedef std::map< std::string, std::string > Annotations
 Collection type for annotations, as a string-string map. More...
 

Public Member Functions

size_t dim () const
 Fill dimension of this data object. More...
 
Constructors
 Counter (const std::string &path="", const std::string &title="")
 Default constructor. More...
 
 Counter (const Dbn0D &dbn, const std::string &path="", const std::string &title="")
 Constructor accepting an explicit Dbn0D. More...
 
 Counter (double w, const std::string &path="", const std::string &title="")
 Constructor accepting a double (treated as the weight of a single fill). More...
 
 Counter (const Counter &c, const std::string &path="")
 Copy constructor with optional new path. More...
 
Counteroperator= (const Counter &c)
 Assignment operator. More...
 
Counter clone () const
 Make a copy on the stack. More...
 
Counternewclone () const
 Make a copy on the heap, via 'new'. More...
 
Modifiers
virtual void fill (double weight=1.0, double fraction=1.0)
 Fill histo by value and weight. More...
 
virtual void fill (FillType, double weight=1.0, double fraction=1.0)
 
virtual void reset ()
 Reset the histogram. More...
 
void scaleW (double scalefactor)
 Rescale as if all fill weights had been different by factor scalefactor. More...
 
Data access
double numEntries () const
 Get the number of fills. More...
 
double effNumEntries () const
 Get the effective number of fills. More...
 
double sumW () const
 Get the sum of weights. More...
 
double sumW2 () const
 Get the sum of squared weights. More...
 
double val () const
 Get the value. More...
 
double err () const
 
double relErr () const
 
Internal state access and modification (mainly for persistency use)
const Dbn0Ddbn () const
 Get the internal distribution object. More...
 
void setDbn (const Dbn0D &dbn)
 Set the internal distribution object: CAREFUL! More...
 
Adding and subtracting counters
Counteroperator+= (const Counter &toAdd)
 Add another counter to this. More...
 
Counteroperator-= (const Counter &toSubtract)
 Subtract another counter from this. More...
 
Counteroperator++ ()
 
Counteroperator-- ()
 
Counteroperator*= (double s)
 Scale by a double (syntactic sugar for scaleW(s)) More...
 
Counteroperator/= (double s)
 Inverse-scale by a double (syntactic sugar for scaleW(1/s)) More...
 
- Public Member Functions inherited from YODA::AnalysisObject
 AnalysisObject ()
 Default constructor. More...
 
 AnalysisObject (const std::string &type, const std::string &path, const std::string &title="")
 Constructor giving a type, a path and an optional title. More...
 
 AnalysisObject (const std::string &type, const std::string &path, const AnalysisObject &ao, const std::string &title="")
 Constructor giving a type, a path, another AO to copy annotation from, and an optional title. More...
 
virtual ~AnalysisObject ()
 Default destructor. More...
 
virtual AnalysisObjectoperator= (const AnalysisObject &ao)
 Default copy assignment operator. More...
 
std::vector< std::string > annotations () const
 
bool hasAnnotation (const std::string &name) const
 Check if an annotation is defined. More...
 
const std::string & annotation (const std::string &name) const
 Get an annotation by name (as a string) More...
 
const std::string & annotation (const std::string &name, const std::string &defaultreturn) const
 Get an annotation by name (as a string) with a default in case the annotation is not found. More...
 
template<typename T >
const T annotation (const std::string &name) const
 Get an annotation by name (copied to another type) More...
 
template<typename T >
const T annotation (const std::string &name, const T &defaultreturn) const
 Get an annotation by name (copied to another type) with a default in case the annotation is not found. More...
 
void setAnnotation (const std::string &name, const std::string &value)
 Add or set a string-valued annotation by name. More...
 
void setAnnotation (const std::string &name, double value)
 Add or set a double-valued annotation by name. More...
 
void setAnnotation (const std::string &name, float value)
 Add or set a float-valued annotation by name. More...
 
void setAnnotation (const std::string &name, long double value)
 Add or set a long-double-valued annotation by name. More...
 
template<typename T >
void setAnnotation (const std::string &name, const T &value)
 Add or set an annotation by name (templated for remaining types) More...
 
void setAnnotations (const Annotations &anns)
 Set all annotations at once. More...
 
template<typename T >
void addAnnotation (const std::string &name, const T &value)
 Add or set an annotation by name. More...
 
void rmAnnotation (const std::string &name)
 Delete an annotation by name. More...
 
void clearAnnotations ()
 Delete an annotation by name. More...
 
const std::string title () const
 Get the AO title. More...
 
void setTitle (const std::string &title)
 Set the AO title. More...
 
const std::string path () const
 Get the AO path. More...
 
void setPath (const std::string &path)
 
const std::string name () const
 
virtual std::string type () const
 Get name of the analysis object type. More...
 

Detailed Description

A weighted counter.

Definition at line 23 of file Counter.h.

Member Typedef Documentation

◆ FillType

typedef std::tuple YODA::Counter::FillType

Definition at line 27 of file Counter.h.

◆ Ptr

typedef std::shared_ptr<Counter> YODA::Counter::Ptr

Definition at line 28 of file Counter.h.

Constructor & Destructor Documentation

◆ Counter() [1/4]

YODA::Counter::Counter ( const std::string &  path = "",
const std::string &  title = "" 
)
inline

Default constructor.

Definition at line 34 of file Counter.h.

Referenced by clone(), Counter(), and newclone().

35  : AnalysisObject("Counter", path, title)
36  { }
AnalysisObject()
Default constructor.
const std::string path() const
Get the AO path.
const std::string title() const
Get the AO title.

◆ Counter() [2/4]

YODA::Counter::Counter ( const Dbn0D dbn,
const std::string &  path = "",
const std::string &  title = "" 
)
inline

Constructor accepting an explicit Dbn0D.

Intended both for internal persistency and user use.

Definition at line 42 of file Counter.h.

44  : AnalysisObject("Counter", path, title),
45  _dbn(dbn)
46  { }
AnalysisObject()
Default constructor.
const std::string path() const
Get the AO path.
const std::string title() const
Get the AO title.
const Dbn0D & dbn() const
Get the internal distribution object.
Definition: Counter.h:157

◆ Counter() [3/4]

YODA::Counter::Counter ( double  w,
const std::string &  path = "",
const std::string &  title = "" 
)
inline

Constructor accepting a double (treated as the weight of a single fill).

Intended for user convenience only, so Counter can be treated as a number.

Definition at line 52 of file Counter.h.

References Counter(), and YODA::AnalysisObject::path().

54  : AnalysisObject("Counter", path, title)
55  {
56  _dbn.fill(w);
57  }
AnalysisObject()
Default constructor.
const std::string path() const
Get the AO path.
const std::string title() const
Get the AO title.

◆ Counter() [4/4]

YODA::Counter::Counter ( const Counter c,
const std::string &  path = "" 
)

Copy constructor with optional new path.

Copy constructor with optional new path

Todo:
Don't copy the path?

Definition at line 16 of file Counter.cc.

17  : AnalysisObject("Counter", (path.size() == 0) ? c.path() : path, c, c.title())
18  {
19  _dbn = c._dbn;
20  }
AnalysisObject()
Default constructor.
const std::string path() const
Get the AO path.

Member Function Documentation

◆ clone()

Counter YODA::Counter::clone ( ) const
inline

Make a copy on the stack.

Definition at line 74 of file Counter.h.

References Counter().

74  {
75  return Counter(*this);
76  }
Counter(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Counter.h:34

◆ dbn()

const Dbn0D& YODA::Counter::dbn ( ) const
inline

Get the internal distribution object.

Definition at line 157 of file Counter.h.

Referenced by setDbn().

157  {
158  return _dbn;
159  }

◆ dim()

size_t YODA::Counter::dim ( ) const
inlinevirtual

Fill dimension of this data object.

Implements YODA::AnalysisObject.

Definition at line 87 of file Counter.h.

87 { return 0; }

◆ effNumEntries()

double YODA::Counter::effNumEntries ( ) const
inline

Get the effective number of fills.

Definition at line 126 of file Counter.h.

126 { return _dbn.effNumEntries(); }

◆ err()

double YODA::Counter::err ( ) const
inline

Get the uncertainty on the value

Todo:
Implement on Dbn0D and feed through to this and Dbn1D, 2D, etc.

Definition at line 140 of file Counter.h.

References sumW2().

Referenced by YODA::divide(), YODA::efficiency(), YODA::mkScatter(), relErr(), and YODA::WriterFLAT::writeCounter().

140 { return sqrt(sumW2()); }
double sumW2() const
Get the sum of squared weights.
Definition: Counter.h:132

◆ fill() [1/2]

virtual void YODA::Counter::fill ( double  weight = 1.0,
double  fraction = 1.0 
)
inlinevirtual

Fill histo by value and weight.

Definition at line 94 of file Counter.h.

Referenced by fill().

94  {
95  _dbn.fill(weight, fraction);
96  }

◆ fill() [2/2]

virtual void YODA::Counter::fill ( FillType  ,
double  weight = 1.0,
double  fraction = 1.0 
)
inlinevirtual

Definition at line 98 of file Counter.h.

References fill().

98  {
99  fill(weight, fraction);
100  }
virtual void fill(double weight=1.0, double fraction=1.0)
Fill histo by value and weight.
Definition: Counter.h:94

◆ newclone()

Counter* YODA::Counter::newclone ( ) const
inlinevirtual

Make a copy on the heap, via 'new'.

Implements YODA::AnalysisObject.

Definition at line 79 of file Counter.h.

References Counter().

79  {
80  return new Counter(*this);
81  }
Counter(const std::string &path="", const std::string &title="")
Default constructor.
Definition: Counter.h:34

◆ numEntries()

double YODA::Counter::numEntries ( ) const
inline

Get the number of fills.

Definition at line 123 of file Counter.h.

Referenced by YODA::efficiency().

123 { return _dbn.numEntries(); }

◆ operator*=()

Counter& YODA::Counter::operator*= ( double  s)
inline

Scale by a double (syntactic sugar for scaleW(s))

Definition at line 205 of file Counter.h.

References scaleW().

205  {
206  scaleW(s);
207  return *this;
208  }
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Counter.h:111

◆ operator++()

Counter& YODA::Counter::operator++ ( )
inline

Increment as if by a fill of weight = 1

Note
This is post-increment only, i.e. cn++ not ++cn

Definition at line 192 of file Counter.h.

192  {
193  *this += 1;
194  return *this;
195  }

◆ operator+=()

Counter& YODA::Counter::operator+= ( const Counter toAdd)
inline

Add another counter to this.

Definition at line 179 of file Counter.h.

179  {
180  _dbn += toAdd._dbn;
181  return *this;
182  }

◆ operator--()

Counter& YODA::Counter::operator-- ( )
inline

Increment as if by a fill of weight = -1

Note
This is post-decrement only, i.e. cn– not –cn

Definition at line 199 of file Counter.h.

199  {
200  *this -= 1;
201  return *this;
202  }

◆ operator-=()

Counter& YODA::Counter::operator-= ( const Counter toSubtract)
inline

Subtract another counter from this.

Definition at line 185 of file Counter.h.

185  {
186  _dbn -= toSubtract._dbn;
187  return *this;
188  }

◆ operator/=()

Counter& YODA::Counter::operator/= ( double  s)
inline

Inverse-scale by a double (syntactic sugar for scaleW(1/s))

Definition at line 211 of file Counter.h.

References scaleW().

211  {
212  scaleW(1/s);
213  return *this;
214  }
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Counter.h:111

◆ operator=()

Counter& YODA::Counter::operator= ( const Counter c)
inline

Assignment operator.

Definition at line 66 of file Counter.h.

References YODA::AnalysisObject::path(), YODA::AnalysisObject::setPath(), YODA::AnalysisObject::setTitle(), and YODA::AnalysisObject::title().

66  {
67  setPath(c.path());
68  setTitle(c.title());
69  _dbn = c._dbn;
70  return *this;
71  }
void setPath(const std::string &path)
void setTitle(const std::string &title)
Set the AO title.

◆ relErr()

double YODA::Counter::relErr ( ) const
inline

Get the relative uncertainty on the value

Todo:
Implement on Dbn0D and feed through to this and Dbn1D, 2D, etc.
Todo:
Throw excp if sumW2 is 0?

Definition at line 145 of file Counter.h.

References err(), sumW(), and sumW2().

Referenced by YODA::divide().

145  {
147  return sumW2() != 0 ? err()/sumW() : 0;
148  }
double sumW2() const
Get the sum of squared weights.
Definition: Counter.h:132
double err() const
Definition: Counter.h:140
double sumW() const
Get the sum of weights.
Definition: Counter.h:129

◆ reset()

virtual void YODA::Counter::reset ( )
inlinevirtual

Reset the histogram.

Keep the binning but set all bin contents and related quantities to zero

Implements YODA::AnalysisObject.

Definition at line 105 of file Counter.h.

105  {
106  _dbn.reset();
107  }

◆ scaleW()

void YODA::Counter::scaleW ( double  scalefactor)
inline

Rescale as if all fill weights had been different by factor scalefactor.

Definition at line 111 of file Counter.h.

References YODA::AnalysisObject::setAnnotation().

Referenced by operator*=(), and operator/=().

111  {
112  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
113  _dbn.scaleW(scalefactor);
114  }
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.

◆ setDbn()

void YODA::Counter::setDbn ( const Dbn0D dbn)
inline

Set the internal distribution object: CAREFUL!

Definition at line 162 of file Counter.h.

References dbn().

162  {
163  _dbn = dbn;
164  }
const Dbn0D & dbn() const
Get the internal distribution object.
Definition: Counter.h:157

◆ sumW()

double YODA::Counter::sumW ( ) const
inline

Get the sum of weights.

Definition at line 129 of file Counter.h.

Referenced by YODA::efficiency(), relErr(), and val().

129 { return _dbn.sumW(); }

◆ sumW2()

double YODA::Counter::sumW2 ( ) const
inline

Get the sum of squared weights.

Definition at line 132 of file Counter.h.

Referenced by YODA::efficiency(), err(), and relErr().

132 { return _dbn.sumW2(); }

◆ val()

double YODA::Counter::val ( ) const
inline

Get the value.

Definition at line 135 of file Counter.h.

References sumW().

Referenced by YODA::divide(), YODA::mkScatter(), and YODA::WriterFLAT::writeCounter().

135 { return sumW(); }
double sumW() const
Get the sum of weights.
Definition: Counter.h:129

The documentation for this class was generated from the following files: