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

A weighted counter. More...

#include <Counter.h>

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

Public Types

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

Public Member Functions

Constructors
 Counter (const std::string &path="", const std::string &title="")
 Default constructor.
 
 Counter (const Dbn0D &dbn, const std::string &path="", const std::string &title="")
 Constructor accepting an explicit Dbn0D.
 
 Counter (Dbn0D &&dbn, const std::string &path="", const std::string &title="")
 Constructor accepting an explicit rvalue Dbn0D.
 
 Counter (double w, const std::string &path="", const std::string &title="")
 Constructor accepting a double (treated as the weight of a single fill).
 
 Counter (const Counter &c, const std::string &path="")
 
 Counter (Counter &&c, const std::string &path="")
 Move constructor with optional new path.
 
Counteroperator= (const Counter &c) noexcept
 Assignment operator.
 
Counteroperator= (Counter &&c) noexcept
 Move operator.
 
Counter clone () const
 Make a copy on the stack.
 
Counternewclone () const
 Make a copy on the heap, via 'new'.
 
Dimensions
size_t dim () const noexcept
 Total dimension of this data object.
 
size_t fillDim () const noexcept
 Fill dimension of this data object.
 
Modifiers
virtual int fill (double weight=1.0, double fraction=1.0)
 Fill histo by value and weight.
 
virtual int fill (FillType &&, double weight=1.0, double fraction=1.0)
 
virtual void reset ()
 Reset the histogram.
 
void scaleW (double scalefactor)
 Rescale as if all fill weights had been different by factor scalefactor.
 
Data access
double numEntries (bool=false) const
 Get the number of fills.
 
double effNumEntries (bool=false) const
 Get the effective number of fills.
 
double sumW (bool=false) const
 Get the sum of weights.
 
double sumW2 (bool=false) const
 Get the sum of squared weights.
 
double val (bool=false) const
 Get the value.
 
double err () const
 
double relErr () const
 
Internal state access and modification (mainly for persistency use)
const Dbn0Ddbn () const
 Get the internal distribution object.
 
void setDbn (const Dbn0D &dbn)
 Set the internal distribution object: CAREFUL!
 
void setDbn (Dbn0D &&dbn)
 Set the internal distribution object: CAREFUL!
 
Adding and subtracting counters
Counteroperator+= (const Counter &toAdd)
 Add another counter to this.
 
Counteroperator+= (Counter &&toAdd)
 
Counteroperator-= (const Counter &toSubtract)
 Subtract another counter from this.
 
Counteroperator-= (Counter &&toSubtract)
 
Counteroperator++ ()
 
Counteroperator-- ()
 
Counteroperator*= (double s)
 Scale by a double (syntactic sugar for scaleW(s))
 
Counteroperator/= (double s)
 Inverse-scale by a double (syntactic sugar for scaleW(1/s))
 
Type reduction
Estimate0D mkEstimate (const std::string &path="", const std::string &source="") const
 
Scatter1D mkScatter (const std::string &path="") const
 
AnalysisObjectmkInert (const std::string &path="", const std::string &source="") const noexcept
 Return an inert version of the analysis object (e.g. scatter, estimate)
 
MPI (de-)serialisation
size_t lengthContent (bool=false) const noexcept
 Length of serialized content vector for MPI reduce operations.
 
std::vector< double > serializeContent (bool=false) const noexcept
 Content serialisation for MPI reduce operations.
 
void deserializeContent (const std::vector< double > &data)
 Content deserialisation for MPI reduce operations.
 
- Public Member Functions inherited from YODA::AnalysisObject
 AnalysisObject ()
 Default constructor.
 
 AnalysisObject (const std::string &type, const std::string &path, const std::string &title="")
 Constructor giving a type, a path and an optional title.
 
 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.
 
virtual ~AnalysisObject ()
 Default destructor.
 
virtual AnalysisObjectoperator= (const AnalysisObject &ao) noexcept
 Default copy assignment operator.
 
std::vector< std::string > annotations () const
 
bool hasAnnotation (const std::string &name) const
 Check if an annotation is defined.
 
const std::string & annotation (const std::string &name) const
 Get an annotation by name (as a string)
 
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.
 
template<typename T >
const T annotation (const std::string &name) const
 Get an annotation by name (copied to another type)
 
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.
 
template<typename T >
void setAnnotation (const std::string &name, const T &value)
 Add or set an annotation by name (templated for remaining types)
 
void setAnnotations (const Annotations &anns)
 Set all annotations at once.
 
template<typename T >
void addAnnotation (const std::string &name, const T &value)
 Add or set an annotation by name.
 
void rmAnnotation (const std::string &name)
 Delete an annotation by name.
 
void clearAnnotations ()
 Delete an annotation by name.
 
const std::string title () const
 Get the AO title.
 
void setTitle (const std::string &title)
 Set the AO title.
 
const std::string path () const
 Get the AO path.
 
void setPath (const std::string &path)
 
const std::string name () const
 
virtual std::string type () const
 Get name of the analysis object type.
 
size_t lengthMeta (const bool skipPath=true, const bool skipTitle=true) const noexcept
 Length of serialized meta-data vector for MPI reduce operations.
 
std::vector< std::string > serializeMeta (const bool skipPath=true, const bool skipTitle=true) const noexcept
 Mate-data serialisation for MPI reduce operations.
 
virtual void deserializeMeta (const std::vector< std::string > &data, const bool resetPath=false, const bool resetTitle=false)
 Mate-data deserialisation for MPI reduce operations.
 
- Public Member Functions inherited from YODA::Fillable
virtual ~Fillable ()=default
 Virtual destructor for inheritance.
 

Detailed Description

A weighted counter.

Definition at line 26 of file Counter.h.

Member Typedef Documentation

◆ FillType

using YODA::Counter::FillType = std::tuple<>

Definition at line 30 of file Counter.h.

◆ Ptr

using YODA::Counter::Ptr = std::shared_ptr<Counter>

Definition at line 31 of file Counter.h.

Constructor & Destructor Documentation

◆ Counter() [1/6]

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

Default constructor.

Definition at line 38 of file Counter.h.

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

◆ Counter() [2/6]

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

47 : AnalysisObject("Counter", path, title), _dbn(dbn) { }
const Dbn0D & dbn() const
Get the internal distribution object.
Definition Counter.h:210

◆ Counter() [3/6]

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

Constructor accepting an explicit rvalue Dbn0D.

Intended both for internal persistency and user use.

Definition at line 53 of file Counter.h.

55 : AnalysisObject("Counter", path, title), _dbn(std::move(dbn)) { }

◆ Counter() [4/6]

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

63 : AnalysisObject("Counter", path, title) { _dbn.fill(w); }

◆ Counter() [5/6]

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

Copy constructor with optional new path

Todo:
Don't copy the path?

Definition at line 68 of file Counter.h.

69 : AnalysisObject("Counter", (path.size() == 0) ? c.path() : path, c, c.title()),
70 _dbn(c._dbn) { }

◆ Counter() [6/6]

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

Move constructor with optional new path.

Definition at line 73 of file Counter.h.

74 : AnalysisObject("Counter", (path.size() == 0) ? c.path() : path, c, c.title()),
75 _dbn(std::move(c._dbn)) { }

Member Function Documentation

◆ clone()

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

Make a copy on the stack.

Definition at line 97 of file Counter.h.

97 {
98 return Counter(*this);
99 }
Counter(const std::string &path="", const std::string &title="")
Default constructor.
Definition Counter.h:38

◆ dbn()

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

Get the internal distribution object.

Definition at line 210 of file Counter.h.

210 {
211 return _dbn;
212 }

Referenced by setDbn(), and setDbn().

◆ deserializeContent()

void YODA::Counter::deserializeContent ( const std::vector< double > &  data)
inlinevirtual

Content deserialisation for MPI reduce operations.

Implements YODA::AnalysisObject.

Definition at line 340 of file Counter.h.

340 {
341 _dbn._deserializeContent(data);
342 }

◆ dim()

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

Total dimension of this data object.

Implements YODA::AnalysisObject.

Definition at line 135 of file Counter.h.

135{ return 1; }

◆ effNumEntries()

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

Get the effective number of fills.

Implements YODA::Fillable.

Definition at line 180 of file Counter.h.

180{ 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 193 of file Counter.h.

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

References sumW2().

Referenced by mkEstimate(), YODA::mkScatter(), mkScatter(), and relErr().

◆ fill() [1/2]

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

Fill histo by value and weight.

Definition at line 147 of file Counter.h.

147 {
148 _dbn.fill(weight, fraction);
149 return 0;
150 }

Referenced by fill().

◆ fill() [2/2]

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

Definition at line 152 of file Counter.h.

152 {
153 return fill(weight, fraction);
154 }
virtual int fill(double weight=1.0, double fraction=1.0)
Fill histo by value and weight.
Definition Counter.h:147

References fill().

◆ fillDim()

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

Fill dimension of this data object.

Implements YODA::Fillable.

Definition at line 138 of file Counter.h.

138{ return 0; }

◆ lengthContent()

size_t YODA::Counter::lengthContent ( bool  fixed_length = false) const
inlinevirtualnoexcept

Length of serialized content vector for MPI reduce operations.

Implements YODA::AnalysisObject.

Definition at line 332 of file Counter.h.

332 {
333 return _dbn._lengthContent();
334 }

◆ mkEstimate()

Estimate0D YODA::Counter::mkEstimate ( const std::string &  path = "",
const std::string &  source = "" 
) const
inline

Definition at line 297 of file Counter.h.

297 {
298 Estimate0D rtn;
299 for (const std::string& a : annotations()) {
300 if (a != "Type") rtn.setAnnotation(a, annotation(a));
301 }
302 rtn.setAnnotation("Path", path);
303
304 if (numEntries()) {
305 rtn.set(val(), err(), source);
306 }
307 return rtn;
308 }
std::vector< std::string > annotations() const
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
double err() const
Definition Counter.h:193
double numEntries(bool=false) const
Get the number of fills.
Definition Counter.h:177
double val(bool=false) const
Get the value.
Definition Counter.h:189

References YODA::AnalysisObject::annotation(), YODA::AnalysisObject::annotations(), err(), numEntries(), YODA::AnalysisObject::path(), YODA::Estimate::set(), YODA::AnalysisObject::setAnnotation(), and val().

Referenced by mkInert().

◆ mkInert()

AnalysisObject * YODA::Counter::mkInert ( const std::string &  path = "",
const std::string &  source = "" 
) const
inlinevirtualnoexcept

Return an inert version of the analysis object (e.g. scatter, estimate)

Reimplemented from YODA::AnalysisObject.

Definition at line 322 of file Counter.h.

323 {
324 return mkEstimate(path, source).newclone();
325 }
Estimate0D mkEstimate(const std::string &path="", const std::string &source="") const
Definition Counter.h:297
Estimate0D * newclone() const noexcept
Make a copy on the heap.
Definition Estimate0D.h:83

References mkEstimate(), YODA::Estimate0D::newclone(), and YODA::AnalysisObject::path().

◆ mkScatter()

Scatter1D YODA::Counter::mkScatter ( const std::string &  path = "") const
inline

Definition at line 310 of file Counter.h.

310 {
311 Scatter1D rtn;
312 for (const std::string& a : annotations()) {
313 if (a != "Type") rtn.setAnnotation(a, annotation(a));
314 }
315 rtn.setAnnotation("Path", path);
316
317 rtn.addPoint(Point1D(val(), err()));
318 return rtn;
319 }
PointND< 1 > Point1D
User-familiar alias.
Definition Point.h:704
ScatterND< 1 > Scatter1D
Definition Scatter.h:881

References YODA::ScatterND< N >::addPoint(), YODA::AnalysisObject::annotation(), YODA::AnalysisObject::annotations(), err(), YODA::AnalysisObject::path(), YODA::AnalysisObject::setAnnotation(), and val().

◆ newclone()

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

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

Implements YODA::AnalysisObject.

Definition at line 102 of file Counter.h.

102 {
103 return new Counter(*this);
104 }

◆ numEntries()

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

Get the number of fills.

Implements YODA::Fillable.

Definition at line 177 of file Counter.h.

177{ return _dbn.numEntries(); }

Referenced by YODA::efficiency(), mkEstimate(), and YODA::WriterYODA1::writeCounter().

◆ operator*=()

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

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

Definition at line 279 of file Counter.h.

279 {
280 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
281 scaleW(s);
282 return *this;
283 }
void rmAnnotation(const std::string &name)
Delete an annotation by name.
bool hasAnnotation(const std::string &name) const
Check if an annotation is defined.
void scaleW(double scalefactor)
Rescale as if all fill weights had been different by factor scalefactor.
Definition Counter.h:165

References YODA::AnalysisObject::hasAnnotation(), YODA::AnalysisObject::rmAnnotation(), and scaleW().

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

264 {
265 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
266 *this += 1;
267 return *this;
268 }

References YODA::AnalysisObject::hasAnnotation(), and YODA::AnalysisObject::rmAnnotation().

◆ operator+=() [1/2]

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

Add another counter to this.

Definition at line 237 of file Counter.h.

237 {
238 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
239 _dbn += toAdd._dbn;
240 return *this;
241 }

References YODA::AnalysisObject::hasAnnotation(), and YODA::AnalysisObject::rmAnnotation().

◆ operator+=() [2/2]

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

Definition at line 243 of file Counter.h.

243 {
244 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
245 _dbn += std::move(toAdd._dbn);
246 return *this;
247 }

References YODA::AnalysisObject::hasAnnotation(), and YODA::AnalysisObject::rmAnnotation().

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

272 {
273 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
274 *this -= 1;
275 return *this;
276 }

References YODA::AnalysisObject::hasAnnotation(), and YODA::AnalysisObject::rmAnnotation().

◆ operator-=() [1/2]

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

Subtract another counter from this.

Definition at line 250 of file Counter.h.

250 {
251 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
252 _dbn -= toSubtract._dbn;
253 return *this;
254 }

References YODA::AnalysisObject::hasAnnotation(), and YODA::AnalysisObject::rmAnnotation().

◆ operator-=() [2/2]

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

Definition at line 256 of file Counter.h.

256 {
257 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
258 _dbn -= std::move(toSubtract._dbn);
259 return *this;
260 }

References YODA::AnalysisObject::hasAnnotation(), and YODA::AnalysisObject::rmAnnotation().

◆ operator/=()

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

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

Definition at line 286 of file Counter.h.

286 {
287 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
288 scaleW(1/s);
289 return *this;
290 }

References YODA::AnalysisObject::hasAnnotation(), YODA::AnalysisObject::rmAnnotation(), and scaleW().

◆ operator=() [1/2]

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

Assignment operator.

Definition at line 79 of file Counter.h.

79 {
80 if (this != &c) {
82 _dbn = c._dbn;
83 }
84 return *this;
85 }
virtual AnalysisObject & operator=(const AnalysisObject &ao) noexcept
Default copy assignment operator.

References YODA::AnalysisObject::operator=().

◆ operator=() [2/2]

Counter & YODA::Counter::operator= ( Counter &&  c)
inlinenoexcept

Move operator.

Definition at line 88 of file Counter.h.

88 {
89 if (this != &c) {
91 _dbn = std::move(c._dbn);
92 }
93 return *this;
94 }

References YODA::AnalysisObject::operator=().

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

198 {
200 return sumW2() != 0 ? err()/sumW() : 0;
201 }
double sumW(bool=false) const
Get the sum of weights.
Definition Counter.h:183

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

Referenced by YODA::divide().

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

159 {
160 _dbn.reset();
161 }

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

165 {
166 setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
167 _dbn.scaleW(scalefactor);
168 }
void setAnnotation(const std::string &name, const T &value)
Add or set an annotation by name (templated for remaining types)

References YODA::AnalysisObject::setAnnotation().

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

◆ serializeContent()

std::vector< double > YODA::Counter::serializeContent ( bool  fixed_length = false) const
inlinevirtualnoexcept

Content serialisation for MPI reduce operations.

Implements YODA::AnalysisObject.

Definition at line 336 of file Counter.h.

336 {
337 return _dbn._serializeContent();
338 }

◆ setDbn() [1/2]

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

Set the internal distribution object: CAREFUL!

Definition at line 215 of file Counter.h.

215 {
216 _dbn = dbn;
217 }

References dbn().

◆ setDbn() [2/2]

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

Set the internal distribution object: CAREFUL!

Definition at line 220 of file Counter.h.

220 {
221 _dbn = std::move(dbn);
222 }

References dbn().

◆ sumW()

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

Get the sum of weights.

Implements YODA::Fillable.

Definition at line 183 of file Counter.h.

183{ return _dbn.sumW(); }

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

◆ sumW2()

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

Get the sum of squared weights.

Implements YODA::Fillable.

Definition at line 186 of file Counter.h.

186{ return _dbn.sumW2(); }

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

◆ val()

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

Get the value.

Definition at line 189 of file Counter.h.

189{ return sumW(); }

References sumW().

Referenced by YODA::divide(), mkEstimate(), YODA::mkScatter(), and mkScatter().


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