npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
ArrayProjectors.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ARRAYPROJECTORS_HH_
2 #define NPSTAT_ARRAYPROJECTORS_HH_
3 
4 /*!
5 // \file ArrayProjectors.hh
6 //
7 // \brief Helper templates for making lower-dimensional array projections
8 //
9 // Author: I. Volobouev
10 //
11 // March 2010
12 */
13 
14 #include <vector>
15 #include <stdexcept>
16 
17 #include "npstat/nm/AbsVisitor.hh"
18 #include "npstat/nm/PreciseType.hh"
19 
20 namespace npstat {
21  /** Maximum value projector */
22  template <typename T>
23  class ArrayMaxProjector : public AbsVisitor<T, T>
24  {
25  public:
26  inline ArrayMaxProjector() : isClear_(true) {}
27  inline virtual ~ArrayMaxProjector() {}
28 
29  inline void clear() {isClear_ = true;}
30  inline T result()
31  {
32  if (isClear_) throw std::runtime_error(
33  "In npstat::ArrayMaxProjector: no data processed");
34  return v_;
35  }
36  void process(const T& value);
37 
38  private:
39  T v_;
40  bool isClear_;
41  };
42 
43  /** Minimum value projector */
44  template <typename T>
45  class ArrayMinProjector : public AbsVisitor<T, T>
46  {
47  public:
48  inline ArrayMinProjector() : isClear_(true) {}
49  inline virtual ~ArrayMinProjector() {}
50 
51  inline void clear() {isClear_ = true;}
52  inline T result()
53  {
54  if (isClear_) throw std::runtime_error(
55  "In npstat::ArrayMinProjector: no data processed");
56  return v_;
57  }
58  void process(const T& value);
59 
60  private:
61  T v_;
62  bool isClear_;
63  };
64 
65  /** Sum projector */
66  template
67  <
68  typename T,
69  typename Result,
70  typename Accumulator=typename PreciseType<T>::type
71  >
72  class ArraySumProjector : public AbsVisitor<T, Result>
73  {
74  public:
75  inline ArraySumProjector() : sum_(Accumulator()), n_(0UL) {}
76  inline virtual ~ArraySumProjector() {}
77 
78  inline void clear() {sum_ = Accumulator(); n_ = 0UL;}
79  inline virtual Result result() {return static_cast<Result>(sum_);}
80  inline void process(const T& value) {sum_ += value; ++n_;}
81 
82  protected:
83  Accumulator sum_;
84  unsigned long n_;
85  };
86 
87  /** Mean projector */
88  template
89  <
90  typename T,
91  typename Result,
92  typename Accumulator=typename PreciseType<T>::type
93  >
94  class ArrayMeanProjector : public ArraySumProjector<T, Result, Accumulator>
95  {
96  public:
97  inline ArrayMeanProjector() :
99  inline virtual ~ArrayMeanProjector() {}
100 
101  inline Result result()
102  {
103  if (!this->n_) throw std::runtime_error(
104  "In npstat::ArrayMeanProjector: no data processed");
105  return static_cast<Result>(this->sum_/(1.0*this->n_));
106  }
107  };
108 
109  /** Median projector */
110  template <typename T>
111  class ArrayMedianProjector : public AbsVisitor<T, T>
112  {
113  public:
114  inline ArrayMedianProjector() {}
115  inline virtual ~ArrayMedianProjector() {}
116 
117  inline void clear() {v_.clear();}
118  inline void process(const T& value) {v_.push_back(value);}
119  virtual T result();
120 
121  protected:
122  T medSorted(const T *array, unsigned long n);
123  std::vector<T> v_;
124  };
125 
126  /**
127  // Range projector. Range is defined here as the difference
128  // between 75th and 25th percentiles scaled so that it equals
129  // sigma for Gaussian distribution.
130  */
131  template <typename T>
133  {
134  public:
136  inline virtual ~ArrayRangeProjector() {}
137  T result();
138  };
139 
140  /** Standard deviation projector using an accurate two-pass algorithm */
141  template <typename T, typename Result>
142  class ArrayStdevProjector : public AbsVisitor<T, Result>
143  {
144  public:
145  inline virtual ~ArrayStdevProjector() {}
146 
147  inline void clear() {v_.clear();}
148  inline void process(const T& value) {v_.push_back(value);}
149  Result result();
150 
151  private:
152  std::vector<T> v_;
153  };
154 }
155 
156 #include "npstat/stat/ArrayProjectors.icc"
157 
158 #endif // NPSTAT_ARRAYPROJECTORS_HH_
Interface for piecemeal processing of a data collection.
Compile-time deduction of an appropriate precise numeric type.
Definition: ArrayProjectors.hh:24
T result()
Definition: ArrayProjectors.hh:30
void process(const T &value)
void clear()
Definition: ArrayProjectors.hh:29
Definition: ArrayProjectors.hh:95
Result result()
Definition: ArrayProjectors.hh:101
Definition: ArrayProjectors.hh:112
void process(const T &value)
Definition: ArrayProjectors.hh:118
void clear()
Definition: ArrayProjectors.hh:117
Definition: ArrayProjectors.hh:46
void process(const T &value)
T result()
Definition: ArrayProjectors.hh:52
void clear()
Definition: ArrayProjectors.hh:51
Definition: ArrayProjectors.hh:133
Definition: ArrayProjectors.hh:143
void process(const T &value)
Definition: ArrayProjectors.hh:148
void clear()
Definition: ArrayProjectors.hh:147
Definition: ArrayProjectors.hh:73
void clear()
Definition: ArrayProjectors.hh:78
virtual Result result()
Definition: ArrayProjectors.hh:79
void process(const T &value)
Definition: ArrayProjectors.hh:80
Definition: AbsArrayProjector.hh:14
Definition: AbsVisitor.hh:20