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

A weighted counter. More...

#include <Counter.h>

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

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

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...
 
Dimensions
size_t dim () const
 Fill dimension of this data object. More...
 
size_t fillDim () const
 Fill dimension of this data object. 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 (bool=false) const
 Get the number of fills. More...
 
double effNumEntries (bool=false) const
 Get the effective number of fills. More...
 
double sumW (bool=false) const
 Get the sum of weights. More...
 
double sumW2 (bool=false) const
 Get the sum of squared weights. More...
 
double val (bool=false) 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...
 
void parseVariations ()
 
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...
 
- Public Member Functions inherited from YODA::Fillable
virtual ~Fillable ()=default
 Virtual destructor for inheritance. More...
 

Detailed Description

A weighted counter.

Definition at line 24 of file Counter.h.

Member Typedef Documentation

◆ FillType

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

Definition at line 28 of file Counter.h.

◆ Ptr

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

Definition at line 29 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 35 of file Counter.h.

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

36  : AnalysisObject("Counter", path, title)
37  { }
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 43 of file Counter.h.

45  : AnalysisObject("Counter", path, title),
46  _dbn(dbn)
47  { }
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:168

◆ 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 53 of file Counter.h.

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

55  : AnalysisObject("Counter", path, title)
56  {
57  _dbn.fill(w);
58  }
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 75 of file Counter.h.

References Counter().

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

◆ dbn()

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

Get the internal distribution object.

Definition at line 168 of file Counter.h.

Referenced by setDbn().

168  {
169  return _dbn;
170  }

◆ dim()

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

Fill dimension of this data object.

Todo:
Change to return total dimension

Implements YODA::AnalysisObject.

Definition at line 93 of file Counter.h.

93 { return 0; }

◆ effNumEntries()

double YODA::Counter::effNumEntries ( bool  = false) const
inlinevirtual

Get the effective number of fills.

Implements YODA::Fillable.

Definition at line 137 of file Counter.h.

137 { 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 151 of file Counter.h.

References sumW2().

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

151 { return sqrt(sumW2()); }
double sumW2(bool=false) const
Get the sum of squared weights.
Definition: Counter.h:143

◆ 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 105 of file Counter.h.

Referenced by fill().

105  {
106  _dbn.fill(weight, fraction);
107  }

◆ fill() [2/2]

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

Definition at line 109 of file Counter.h.

References fill().

109  {
110  fill(weight, fraction);
111  }
virtual void fill(double weight=1.0, double fraction=1.0)
Fill histo by value and weight.
Definition: Counter.h:105

◆ fillDim()

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

Fill dimension of this data object.

Implements YODA::Fillable.

Definition at line 96 of file Counter.h.

96 { return 0; }

◆ newclone()

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

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

Implements YODA::AnalysisObject.

Definition at line 80 of file Counter.h.

References Counter().

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

◆ numEntries()

double YODA::Counter::numEntries ( bool  = false) const
inlinevirtual

Get the number of fills.

Implements YODA::Fillable.

Definition at line 134 of file Counter.h.

Referenced by YODA::efficiency().

134 { return _dbn.numEntries(); }

◆ operator*=()

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

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

Definition at line 216 of file Counter.h.

References scaleW().

216  {
217  scaleW(s);
218  return *this;
219  }
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Counter.h:122

◆ 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 203 of file Counter.h.

203  {
204  *this += 1;
205  return *this;
206  }

◆ operator+=()

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

Add another counter to this.

Definition at line 190 of file Counter.h.

190  {
191  _dbn += toAdd._dbn;
192  return *this;
193  }

◆ 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 210 of file Counter.h.

210  {
211  *this -= 1;
212  return *this;
213  }

◆ operator-=()

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

Subtract another counter from this.

Definition at line 196 of file Counter.h.

196  {
197  _dbn -= toSubtract._dbn;
198  return *this;
199  }

◆ operator/=()

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

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

Definition at line 222 of file Counter.h.

References scaleW().

222  {
223  scaleW(1/s);
224  return *this;
225  }
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition: Counter.h:122

◆ operator=()

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

Assignment operator.

Definition at line 67 of file Counter.h.

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

67  {
68  setPath(c.path());
69  setTitle(c.title());
70  _dbn = c._dbn;
71  return *this;
72  }
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 156 of file Counter.h.

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

Referenced by YODA::divide().

156  {
158  return sumW2() != 0 ? err()/sumW() : 0;
159  }
double sumW2(bool=false) const
Get the sum of squared weights.
Definition: Counter.h:143
double err() const
Definition: Counter.h:151
double sumW(bool=false) const
Get the sum of weights.
Definition: Counter.h:140

◆ 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 116 of file Counter.h.

116  {
117  _dbn.reset();
118  }

◆ scaleW()

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

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

Implements YODA::Fillable.

Definition at line 122 of file Counter.h.

References YODA::AnalysisObject::setAnnotation().

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

122  {
123  setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
124  _dbn.scaleW(scalefactor);
125  }
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 173 of file Counter.h.

References dbn().

173  {
174  _dbn = dbn;
175  }
const Dbn0D & dbn() const
Get the internal distribution object.
Definition: Counter.h:168

◆ sumW()

double YODA::Counter::sumW ( bool  = false) const
inlinevirtual

Get the sum of weights.

Implements YODA::Fillable.

Definition at line 140 of file Counter.h.

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

140 { return _dbn.sumW(); }

◆ sumW2()

double YODA::Counter::sumW2 ( bool  = false) const
inlinevirtual

Get the sum of squared weights.

Implements YODA::Fillable.

Definition at line 143 of file Counter.h.

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

143 { return _dbn.sumW2(); }

◆ val()

double YODA::Counter::val ( bool  = false) const
inline

Get the value.

Definition at line 146 of file Counter.h.

References sumW().

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

146 { return sumW(); }
double sumW(bool=false) const
Get the sum of weights.
Definition: Counter.h:140

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