npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
BinSummary.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_BINSUMMARY_HH_
2 #define NPSTAT_BINSUMMARY_HH_
3 
4 /*!
5 // \file BinSummary.hh
6 //
7 // \brief A type for displaying histogram bins with associated uncertainties
8 //
9 // Author: I. Volobouev
10 //
11 // October 2012
12 */
13 
14 #include <cfloat>
15 #include <stdexcept>
16 
17 #include "geners/ClassId.hh"
18 
19 namespace npstat {
20  class StatAccumulator;
21  class WeightedStatAccumulator;
22 
23  /**
24  // A type which can be used as a histogram bin in order to display bin
25  // values as mean with variance or as a five-number summary in box plots
26  */
27  class BinSummary
28  {
29  public:
30  /**
31  // Both rangeDown and rangeUp arguments in this
32  // constructor must be non-negative
33  */
34  BinSummary(double location, double rangeDown, double rangeUp);
35 
36  /**
37  // Both rangeDown and rangeUp arguments in this constructor
38  // must be non-negative. "minValue" and "maxValue" complete
39  // the five-number summary.
40  */
41  BinSummary(double location, double rangeDown, double rangeUp,
42  double minValue, double maxValue);
43 
44  /**
45  // This constructor sets both rangeDown and rangeUp
46  // to the same value (which must be non-negative)
47  */
48  BinSummary(double location, double stdev);
49 
50  /** The default constructor is equivalent to BinSummary(0.0, 0.0) */
52 
53  BinSummary(const BinSummary&);
54  BinSummary& operator=(const BinSummary&);
55 
56  /**
57  // Converting constructor from accumulator classes. If accumulators
58  // on the right side contain no data, BinSummary will look as if
59  // it was made by the default constructor.
60  */
61  template<class Accumulator>
62  BinSummary(const Accumulator& acc);
63 
64  /** Converting assignment operator from accumulator classes */
65  template<class Accumulator>
66  inline BinSummary& operator=(const Accumulator& acc)
67  {*this = BinSummary(acc); return *this;}
68 
69  //@{
70  /** Constructors from a single number (the location) */
72  BinSummary(unsigned char location);
73  BinSummary(int location);
74  BinSummary(long location);
75  BinSummary(float location);
76  BinSummary(double location);
77  //@}
78 
79  //@{
80  /** Assignment operators from a single number (the location) */
81  BinSummary& operator=(const double& location);
82  inline BinSummary& operator=(const bool& loc)
83  {return operator=(static_cast<double>(loc));}
84  inline BinSummary& operator=(const unsigned char& loc)
85  {return operator=(static_cast<double>(loc));}
86  inline BinSummary& operator=(const int& loc)
87  {return operator=(static_cast<double>(loc));}
88  inline BinSummary& operator=(const long& loc)
89  {return operator=(static_cast<double>(loc));}
90  inline BinSummary& operator=(const float& loc)
91  {return operator=(static_cast<double>(loc));}
92  //@}
93 
94  //@{
95  /** Standard plotting accessor */
96  inline double location() const {return location_;}
97  inline double rangeDown() const {return rangeDown_;}
98  inline double rangeUp() const {return rangeUp_;}
99  //@}
100 
101  /**
102  // stdev() throws std::runtime_error in case rangeDown()
103  // and rangeUp() would return different values
104  */
105  double stdev() const;
106 
107  //@{
108  /**
109  // min() and max() will throw std::runtime_error
110  // in case the corresponding quantities are not set
111  */
112  double min() const;
113  double max() const;
114  //@}
115 
116  //@{
117  /**
118  // This accessor will not throw an exception in case the corresponding
119  // quantities are undefined. Instead, it will return its argument.
120  */
121  double noThrowStdev(double valueIfNoData=0.0) const;
122  double noThrowMin(double valueIfNoData=-DBL_MAX) const;
123  double noThrowMax(double valueIfNoData=DBL_MAX) const;
124  //@}
125 
126  //@{
127  /** Check if the corresponding quantity is set */
128  bool hasStdev() const;
129  bool hasMin() const;
130  bool hasMax() const;
131  bool hasLimits() const;
132  //@}
133 
134  //@{
135  /**
136  // Modifier method (what it does is obvious from the method name).
137  // Will throw std::invalid_argument in case changing the value would
138  // result in an inconsistent object.
139  */
140  void setLocation(double newValue);
141  void setStdev(double newValue);
142  void setRangeDown(double newValue);
143  void setRangeUp(double newValue);
144  void setRanges(double newRangeDown, double newRangeUp);
145  void setMin(double newValue);
146  void setMax(double newValue);
147  void setLimits(double newMin, double newMax);
148  void setLocationAndLimits(double newLocation,
149  double newMin, double newMax);
150  //@}
151 
152  /** Shift the location of the represented distribution */
153  void shift(double delta);
154 
155  /**
156  // Change the width of the represented distribution by
157  // a certain factor
158  */
159  void scaleWidth(double scale);
160 
161  /**
162  // Symmetrize the ranges. Equivalent to calling setStdev() with the
163  // value set to the arithmetic average of the upper and lower range.
164  */
166 
167  /** In-place multiplication of all elements by a constant */
168  BinSummary& operator*=(double scaleFactor);
169 
170  /** In-place division of all elements by a constant */
171  inline BinSummary& operator/=(const double denom)
172  {
173  if (denom == 0.0) throw std::domain_error(
174  "In npstat::BinSummary::operator/=: division by zero");
175  return operator*=(1.0/denom);
176  }
177 
178  /** Multiplication of all elements by a constant */
179  inline BinSummary operator*(const double r) const
180  {
181  BinSummary tmp(*this);
182  tmp *= r;
183  return tmp;
184  }
185 
186  /** Division of all elements by a constant */
187  inline BinSummary operator/(const double r) const
188  {
189  BinSummary tmp(*this);
190  tmp /= r;
191  return tmp;
192  }
193 
194  /**
195  // Add another summary to this one assuming that both represent
196  // independent Gaussian distributions.
197  //
198  // This operator will work only in the case lower and upper
199  // ranges are symmetric in both summaries. If either min or max
200  // are undefined in any of the summaries, both of them will be
201  // undefined in this object after this operator runs. Uncertainties
202  // are added in quadrature.
203  */
205 
206  /**
207  // Subtract another summary from this one assuming that both represent
208  // independent Gaussian distributions.
209  //
210  // This operator will work only in the case lower and upper
211  // ranges are symmetric in both summaries. If either min or max
212  // are undefined in any of the summaries, both of them will be
213  // undefined in this object after this operator runs. Note that
214  // "-=" does not undo "+=": it is assumed that the correlation
215  // is 0 in both cases and uncertainties are added in quadrature.
216  */
218 
219  /** Binary addition of two summaries */
220  inline BinSummary operator+(const BinSummary& r) const
221  {
222  BinSummary tmp(*this);
223  tmp += r;
224  return tmp;
225  }
226 
227  /** Binary subtraction of two summaries */
228  inline BinSummary operator-(const BinSummary& r) const
229  {
230  BinSummary tmp(*this);
231  tmp -= r;
232  return tmp;
233  }
234 
235  /** Comparison for equality */
236  bool operator==(const BinSummary& r) const;
237 
238  /** Logical negation of operator== */
239  inline bool operator!=(const BinSummary& r) const
240  {return !(*this == r);}
241 
242  //@{
243  // Method related to "geners" I/O
244  inline gs::ClassId classId() const {return gs::ClassId(*this);}
245  bool write(std::ostream& of) const;
246  //@}
247 
248  static inline const char* classname() {return "npstat::BinSummary";}
249  static inline unsigned version() {return 1;}
250  static void restore(const gs::ClassId& id, std::istream& in,
251  BinSummary* acc);
252  private:
253  static void validateBinSummary(const char* where, double location,
254  double down, double up,
255  double min, double max);
256  double location_;
257  double rangeDown_;
258  double rangeUp_;
259  double min_;
260  double max_;
261  };
262 }
263 
264 #include "npstat/stat/BinSummary.icc"
265 
266 #endif // NPSTAT_BINSUMMARY_HH_
Definition: BinSummary.hh:28
double min() const
BinSummary & operator/=(const double denom)
Definition: BinSummary.hh:171
BinSummary(const Accumulator &acc)
void scaleWidth(double scale)
BinSummary & operator+=(const BinSummary &r)
bool hasStdev() const
double location() const
Definition: BinSummary.hh:96
bool operator!=(const BinSummary &r) const
Definition: BinSummary.hh:239
bool operator==(const BinSummary &r) const
BinSummary operator*(const double r) const
Definition: BinSummary.hh:179
double noThrowStdev(double valueIfNoData=0.0) const
BinSummary & operator=(const Accumulator &acc)
Definition: BinSummary.hh:66
BinSummary operator-(const BinSummary &r) const
Definition: BinSummary.hh:228
BinSummary(double location, double rangeDown, double rangeUp, double minValue, double maxValue)
BinSummary(bool location)
void setLocation(double newValue)
BinSummary operator/(const double r) const
Definition: BinSummary.hh:187
BinSummary(double location, double rangeDown, double rangeUp)
BinSummary & operator=(const double &location)
BinSummary & operator*=(double scaleFactor)
void shift(double delta)
BinSummary operator+(const BinSummary &r) const
Definition: BinSummary.hh:220
double stdev() const
BinSummary(double location, double stdev)
BinSummary & operator-=(const BinSummary &r)
Definition: AbsArrayProjector.hh:14