yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.2
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!
 
void set (const double numEntries, const double sumW, const double sumW2)
 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 346 of file Counter.h.

346 {
347 _dbn._deserializeContent(data);
348 }

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

338 {
339 return _dbn._lengthContent();
340 }

◆ mkEstimate()

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

Definition at line 302 of file Counter.h.

302 {
303 Estimate0D rtn;
304 for (const std::string& a : annotations()) {
305 if (a != "Type") rtn.setAnnotation(a, annotation(a));
306 }
307 rtn.setAnnotation("Path", path);
308
309 rtn.setVal(val());
310 if (numEntries()) { // only set uncertainty for filled Dbns
311 rtn.setErr(err(), source);
312 }
313 return rtn;
314 }
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::AnalysisObject::setAnnotation(), YODA::Estimate::setErr(), YODA::Estimate::setVal(), 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 328 of file Counter.h.

329 {
330 return mkEstimate(path, source).newclone();
331 }
Estimate0D mkEstimate(const std::string &path="", const std::string &source="") const
Definition Counter.h:302
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 316 of file Counter.h.

316 {
317 Scatter1D rtn;
318 for (const std::string& a : annotations()) {
319 if (a != "Type") rtn.setAnnotation(a, annotation(a));
320 }
321 rtn.setAnnotation("Path", path);
322
323 rtn.addPoint(Point1D(val(), err()));
324 return rtn;
325 }
PointND< 1 > Point1D
User-familiar alias.
Definition Point.h:715
ScatterND< 1 > Scatter1D
Definition Scatter.h:902

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(), set(), and YODA::WriterYODA1::writeCounter().

◆ operator*=()

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

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

Definition at line 284 of file Counter.h.

284 {
285 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
286 scaleW(s);
287 return *this;
288 }
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 269 of file Counter.h.

269 {
270 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
271 *this += 1;
272 return *this;
273 }

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

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

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

◆ operator+=() [2/2]

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

Definition at line 248 of file Counter.h.

248 {
249 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
250 _dbn += std::move(toAdd._dbn);
251 return *this;
252 }

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

277 {
278 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
279 *this -= 1;
280 return *this;
281 }

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

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

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

◆ operator-=() [2/2]

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

Definition at line 261 of file Counter.h.

261 {
262 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
263 _dbn -= std::move(toSubtract._dbn);
264 return *this;
265 }

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

291 {
292 if (AO::hasAnnotation("ScaledBy")) AO::rmAnnotation("ScaledBy");
293 scaleW(1/s);
294 return *this;
295 }

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

342 {
343 return _dbn._serializeContent();
344 }

◆ set()

void YODA::Counter::set ( const double  numEntries,
const double  sumW,
const double  sumW2 
)
inline

Set the internal distribution object: CAREFUL!

Definition at line 225 of file Counter.h.

225 {
226 _dbn.set(numEntries, sumW, sumW2);
227 }

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

◆ 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(), set(), 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(), set(), 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: