YODA is hosted by Hepforge, IPPP Durham
YODA::Binned1D< T > Class Template Reference

1D binned container of T objects, without outflows More...

#include <Binning1D.h>

Public Member Functions

bool sameBinning (const Binned1D &other) const
 Check if two Binned1D objects have the same bin edges. More...
 
Constructors
 Binned1D ()
 Empty constructor. More...
 
 Binned1D (const std::vector< double > &binedges, const std::vector< T > &binvalues=std::vector< T >())
 Constructor accepting a list of bin edges and optional corresponding vector of values. More...
 
 Binned1D (const std::vector< std::pair< double, double > > &binedges, const std::vector< T > &binvalues=std::vector< T >())
 Constructor accepting a list of bin edge pairs and optional corresponding vector of values. More...
 
 Binned1D (size_t nbins, double lower, double upper, const std::vector< T > &binvalues=std::vector< T >())
 Constructor with the number of bins and the axis limits. More...
 
Accessor functions
size_t numBins () const
 Get the number of bins on the axis. More...
 
double xMin () const
 Return the lowest-value bin edge on the axis. More...
 
double xMax () const
 Return the highest-value bin edge on the axis. More...
 
T & valueAtIndex (size_t index)
 Return the bin value at a given index (non-const) More...
 
ssize_t indexAt (double coord) const
 Returns the index of a bin at a given coord, -1 if no bin matches. More...
 
T & valueAt (double x)
 Return the bin value at a given coordinate (non-const ref) More...
 
const T & valueAt (double x) const
 Return the bin value at a given coordinate (const ref) More...
 
valueAt (double x, const T &fallback)
 
Modifier functions
void reset ()
 Reset all the bin statistics on the axis. More...
 
void addBin (double low, double high, const T &value=T())
 Add a bin, providing its low and high edge and an optional value. More...
 
void addBins (const std::vector< double > &binedges)
 Add a contiguous set of bins to an axis, via their list of edges. More...
 
void addBins (const std::vector< std::pair< double, double > > &binpairs)
 Add a list of bins as pairs of lowEdge, highEdge. More...
 
void addBins (const Bins &bins)
 
void eraseBin (const size_t i)
 Remove a bin. More...
 
void eraseBins (const size_t from, const size_t to)
 Remove a bin range. More...
 
void scaleX (double scalefactor)
 Scale the size of an axis by a factor. More...
 

Detailed Description

template<typename T>
class YODA::Binned1D< T >

1D binned container of T objects, without outflows

This class handles most of the low-level operations on an axis of bins arranged in a 1D line (including gaps).

Definition at line 19 of file Binning1D.h.

Constructor & Destructor Documentation

template<typename T >
YODA::Binned1D< T >::Binned1D ( )
inline

Empty constructor.

Definition at line 26 of file Binning1D.h.

27  : _locked(false)
28  { }
template<typename T >
YODA::Binned1D< T >::Binned1D ( const std::vector< double > &  binedges,
const std::vector< T > &  binvalues = std::vector<T>() 
)
inline

Constructor accepting a list of bin edges and optional corresponding vector of values.

Todo:

Definition at line 31 of file Binning1D.h.

References YODA::Binned1D< T >::addBins().

32  : _locked(false)
33  {
34  addBins(binedges);
36  }
void addBins(const std::vector< double > &binedges)
Add a contiguous set of bins to an axis, via their list of edges.
Definition: Binning1D.h:133
template<typename T >
YODA::Binned1D< T >::Binned1D ( const std::vector< std::pair< double, double > > &  binedges,
const std::vector< T > &  binvalues = std::vector<T>() 
)
inline

Constructor accepting a list of bin edge pairs and optional corresponding vector of values.

Todo:

Definition at line 39 of file Binning1D.h.

References YODA::Binned1D< T >::addBins().

40  : _locked(false)
41  {
42  addBins(binedges);
44  }
void addBins(const std::vector< double > &binedges)
Add a contiguous set of bins to an axis, via their list of edges.
Definition: Binning1D.h:133
template<typename T >
YODA::Binned1D< T >::Binned1D ( size_t  nbins,
double  lower,
double  upper,
const std::vector< T > &  binvalues = std::vector<T>() 
)
inline

Constructor with the number of bins and the axis limits.

Definition at line 47 of file Binning1D.h.

References YODA::Binned1D< T >::addBins(), and YODA::linspace().

48  : _locked(false)
49  {
50  addBins(linspace(nbins, lower, upper));
51  }
std::vector< double > linspace(size_t nbins, double start, double end, bool include_end=true)
Make a list of nbins + 1 values equally spaced between start and end inclusive.
Definition: MathUtils.h:248
void addBins(const std::vector< double > &binedges)
Add a contiguous set of bins to an axis, via their list of edges.
Definition: Binning1D.h:133

Member Function Documentation

template<typename T >
void YODA::Binned1D< T >::addBin ( double  low,
double  high,
const T &  value = T() 
)
inline

Add a bin, providing its low and high edge and an optional value.

Definition at line 126 of file Binning1D.h.

126  {
127  Bins newBins(_bins);
128  newBins.push_back(Bin(low, high));
129  _updateAxis(newBins);
130  }
template<typename T >
void YODA::Binned1D< T >::addBins ( const std::vector< double > &  binedges)
inline

Add a contiguous set of bins to an axis, via their list of edges.

Definition at line 133 of file Binning1D.h.

Referenced by YODA::Binned1D< T >::Binned1D().

133  {
134  Bins newBins(_bins);
135  if (binedges.size() == 0)
136  return;
137 
138  double low = binedges.front();
139 
140  for (size_t i = 1; i < binedges.size(); i++) {
141  double high = binedges[i];
142  newBins.push_back(Bin(low, high));
143  low = high;
144  }
145 
146  _updateAxis(newBins);
147  }
template<typename T >
void YODA::Binned1D< T >::addBins ( const std::vector< std::pair< double, double > > &  binpairs)
inline

Add a list of bins as pairs of lowEdge, highEdge.

Definition at line 150 of file Binning1D.h.

150  {
151  // Make a copy of the current binning
152  Bins newBins(_bins);
153 
154  // Iterate over given bins
155  for (size_t i = 0; i < binpairs.size(); i++) {
156  std::pair<double, double> b = binpairs[i];
157  newBins.push_back(Bin(b.first, b.second));
158  }
159  _updateAxis(newBins);
160  }
template<typename T >
void YODA::Binned1D< T >::addBins ( const Bins &  bins)
inline

Definition at line 163 of file Binning1D.h.

163  {
164  Bins newBins(_bins);
165  BOOST_FOREACH(const Bin& b, bins) newBins.push_back(b);
166  _updateAxis(newBins);
167  }
template<typename T >
void YODA::Binned1D< T >::eraseBin ( const size_t  i)
inline

Remove a bin.

Definition at line 171 of file Binning1D.h.

References YODA::Binned1D< T >::numBins().

171  {
172  // Might as well erase from the internal bins, as we can guarantee
173  // consistency.
174  if (i >= numBins())
175  throw RangeError("Bin index is out of range");
176 
177  const bool wasLocked = _locked;
178  _locked = false;
179  _bins.erase(_bins.begin() + i);
180  _updateAxis(_bins);
181  _locked = wasLocked;
182  }
size_t numBins() const
Get the number of bins on the axis.
Definition: Binning1D.h:60
template<typename T >
void YODA::Binned1D< T >::eraseBins ( const size_t  from,
const size_t  to 
)
inline

Remove a bin range.

Definition at line 186 of file Binning1D.h.

References YODA::Binned1D< T >::numBins().

186  {
187  if (from >= numBins())
188  throw RangeError("Initial index out of range");
189  if (to >= numBins())
190  throw RangeError("Final index out of range");
191  if (from > to)
192  throw RangeError("Final index is less than initial index");
193 
194  const bool wasLocked = _locked;
195  _locked = false;
196  _bins.erase(_bins.begin() + from, _bins.begin() + to + 1);
197  _updateAxis(_bins);
198  _locked = wasLocked;
199  }
size_t numBins() const
Get the number of bins on the axis.
Definition: Binning1D.h:60
template<typename T >
ssize_t YODA::Binned1D< T >::indexAt ( double  coord) const
inline

Returns the index of a bin at a given coord, -1 if no bin matches.

Definition at line 84 of file Binning1D.h.

84  {
85  // Yes, this is robust even with an empty axis: there's always at least one outflow
86  return _indexes[_binsearcher.index(coord)];
87  }
template<typename T >
size_t YODA::Binned1D< T >::numBins ( ) const
inline

Get the number of bins on the axis.

Definition at line 60 of file Binning1D.h.

Referenced by YODA::Binned1D< T >::eraseBin(), YODA::Binned1D< T >::eraseBins(), YODA::Binned1D< T >::sameBinning(), YODA::Binned1D< T >::valueAtIndex(), YODA::Binned1D< T >::xMax(), and YODA::Binned1D< T >::xMin().

60  {
61  return bins().size();
62  }
template<typename T >
void YODA::Binned1D< T >::reset ( )
inline

Reset all the bin statistics on the axis.

Definition at line 119 of file Binning1D.h.

References YODA::Bin::reset().

119  {
120  BOOST_FOREACH(Bin& bin, _bins) bin.reset();
121  _locked = false;
122  }
void reset()
Reset all the bin statistics on the axis.
Definition: Binning1D.h:119
template<typename T >
bool YODA::Binned1D< T >::sameBinning ( const Binned1D< T > &  other) const
inline

Check if two Binned1D objects have the same bin edges.

Todo:
Be careful about using fuzzyEquals... should these be exact comparisons?

Definition at line 217 of file Binning1D.h.

References YODA::fuzzyEquals(), YODA::Binned1D< T >::numBins(), YODA::Binned1D< T >::xMax(), and YODA::Binned1D< T >::xMin().

217  {
218  if (numBins() != other.numBins()) return false;
219  for (size_t i = 0; i < numBins(); i++)
221  if (!(fuzzyEquals(bin(i).xMin(), other.bin(i).xMin()) && fuzzyEquals(bin(i).xMax(), other.bin(i).xMax())))
222  return false;
223  return true;
224  }
bool fuzzyEquals(double a, double b, double tolerance=1E-5)
Compare two floating point numbers for equality with a degree of fuzziness.
Definition: MathUtils.h:68
double xMin() const
Return the lowest-value bin edge on the axis.
Definition: Binning1D.h:65
size_t numBins() const
Get the number of bins on the axis.
Definition: Binning1D.h:60
template<typename T >
void YODA::Binned1D< T >::scaleX ( double  scalefactor)
inline

Scale the size of an axis by a factor.

Definition at line 204 of file Binning1D.h.

204  {
205  _dbn.scaleX(scalefactor);
206  _underflow.scaleX(scalefactor);
207  _overflow.scaleX(scalefactor);
208  for (size_t i = 0; i < _bins.size(); ++i)
209  _bins[i].scaleX(scalefactor);
210  _updateAxis(_bins);
211  }
void scaleX(double scalefactor)
Scale the size of an axis by a factor.
Definition: Binning1D.h:204
template<typename T >
T& YODA::Binned1D< T >::valueAt ( double  x)
inline

Return the bin value at a given coordinate (non-const ref)

Definition at line 90 of file Binning1D.h.

References YODA::Binned1D< T >::valueAtIndex().

90  {
91  const ssize_t index = binIndexAt(x);
92  if (index == -1) throw RangeError("There is no bin at the specified x");
93  return valueAtIndex(index);
94  }
T & valueAtIndex(size_t index)
Return the bin value at a given index (non-const)
Definition: Binning1D.h:78
template<typename T >
const T& YODA::Binned1D< T >::valueAt ( double  x) const
inline

Return the bin value at a given coordinate (const ref)

Definition at line 97 of file Binning1D.h.

References YODA::Binned1D< T >::valueAtIndex().

97  {
98  const ssize_t index = binIndexAt(x);
99  if (index == -1) throw RangeError("There is no bin at the specified x");
100  return valueAtIndex(index);
101  }
T & valueAtIndex(size_t index)
Return the bin value at a given index (non-const)
Definition: Binning1D.h:78
template<typename T >
T YODA::Binned1D< T >::valueAt ( double  x,
const T &  fallback 
)
inline

Return the bin value at a given coordinate, with fallback if no bin at that position

Note
Return by value rather than by reference, is less efficient than above functions.

Definition at line 106 of file Binning1D.h.

References YODA::Binned1D< T >::valueAtIndex().

106  {
107  const ssize_t index = binIndexAt(x);
108  if (index == -1) return fallback;
109  return valueAtIndex(index);
110  }
T & valueAtIndex(size_t index)
Return the bin value at a given index (non-const)
Definition: Binning1D.h:78
template<typename T >
T& YODA::Binned1D< T >::valueAtIndex ( size_t  index)
inline

Return the bin value at a given index (non-const)

Definition at line 78 of file Binning1D.h.

References YODA::Binned1D< T >::numBins().

Referenced by YODA::Binned1D< T >::valueAt().

78  {
79  if (index >= numBins()) throw RangeError("YODA::Histo1D: index out of range!");
80  return _values[index];
81  }
size_t numBins() const
Get the number of bins on the axis.
Definition: Binning1D.h:60
template<typename T >
double YODA::Binned1D< T >::xMax ( ) const
inline

Return the highest-value bin edge on the axis.

Definition at line 71 of file Binning1D.h.

References YODA::Binned1D< T >::numBins().

Referenced by YODA::Binned1D< T >::sameBinning().

71  {
72  if (numBins() == 0) throw RangeError("This axis contains no bins and so has no defined range");
73  return bins().back().xMax();
74  }
size_t numBins() const
Get the number of bins on the axis.
Definition: Binning1D.h:60
template<typename T >
double YODA::Binned1D< T >::xMin ( ) const
inline

Return the lowest-value bin edge on the axis.

Definition at line 65 of file Binning1D.h.

References YODA::Binned1D< T >::numBins().

Referenced by YODA::Binned1D< T >::sameBinning().

65  {
66  if (numBins() == 0) throw RangeError("This axis contains no bins and so has no defined range");
67  return bins().front().xMin();
68  }
size_t numBins() const
Get the number of bins on the axis.
Definition: Binning1D.h:60

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