Jpp  18.0.1-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAbstractHistogram.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JABSTRACTHISTOGRAM__
2 #define __JTOOLS__JABSTRACTHISTOGRAM__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "JTools/JRange.hh"
8 #include "JTools/JGrid.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JTOOLS {}
16 namespace JPP { using namespace JTOOLS; }
17 
18 namespace JTOOLS {
19 
20  /**
21  * Simple data structure for histogram binning.
22  */
23  template<class JAbscissa_t>
25  public JRange<JAbscissa_t>
26  {
27 
28  typedef JAbscissa_t abscissa_type;
30 
31 
32  /**
33  * Default constructor.
34  */
36  JRange<JAbscissa_t>(),
38  {}
39 
40 
41  /**
42  * Constructor.
43  *
44  * \param nx number of bins
45  * \param xmin lower limit
46  * \param xmax upper limit
47  */
48  JAbstractHistogram(const int nx,
49  const abscissa_type xmin,
50  const abscissa_type xmax) :
51  JRange<JAbscissa_t>(xmin, xmax),
52  number_of_bins(nx)
53  {}
54 
55 
56  /**
57  * Constructor.
58  *
59  * \param xmin lower limit
60  * \param xmax upper limit
61  */
63  const abscissa_type xmax) :
64  JRange<JAbscissa_t>(xmin, xmax),
66  {}
67 
68 
69  /**
70  * Get number of bins.
71  *
72  * \return number of bins
73  */
74  int getNumberOfBins() const
75  {
76  return number_of_bins;
77  }
78 
79 
80  /**
81  * Get bin width.
82  *
83  * \return bin width
84  */
85  double getBinWidth() const
86  {
87  return this->getLength() / this->getNumberOfBins();
88  }
89 
90 
91  /**
92  * Set bin width.
93  *
94  * If <tt>option < 0</tt>, adjust lower limit; if <tt>option > 0</tt>, adjust upper limit; else no adjustments.
95  *
96  * \param dx bin width
97  * \param option option
98  */
99  void setBinWidth(const abscissa_type dx, int option = 0)
100  {
101  number_of_bins = (int) (this->getLength() / dx);
102 
103  if (option < 0) { this->setLowerLimit(this->getUpperLimit() - number_of_bins + dx); }
104  if (option > 0) { this->setUpperLimit(this->getLowerLimit() + number_of_bins + dx); }
105  }
106 
107 
108  /**
109  * Check validity of histogram binning.
110  *
111  * \return true if both range and number of bins are valid; else false
112  */
113  bool is_valid() const
114  {
115  return static_cast<const range_type&>(*this).is_valid() && number_of_bins > 0;
116  }
117 
118 
119  /**
120  * Type conversion operator.
121  *
122  * \return grid
123  */
124  operator JGrid<abscissa_type> () const
125  {
126  return make_grid(this->getNumberOfBins() + 1, this->getLowerLimit(), this->getUpperLimit());
127  }
128 
129 
130  /**
131  * Read histogram from input.
132  *
133  * \param in input stream
134  * \param histogram histogram
135  * \return input stream
136  */
137  friend inline std::istream& operator>>(std::istream& in, JAbstractHistogram<JAbscissa_t>& histogram)
138  {
139  return in >> histogram.number_of_bins >> static_cast<range_type&>(histogram);
140  }
141 
142 
143  /**
144  * Write histogram to output.
145  *
146  * \param out output stream
147  * \param histogram histogram
148  * \return output stream
149  */
150  friend inline std::ostream& operator<<(std::ostream& out, const JAbstractHistogram<JAbscissa_t>& histogram)
151  {
152  return out << histogram.number_of_bins << ' ' << static_cast<const range_type&>(histogram);
153  }
154 
155  protected:
157  };
158 
159 
160  /**
161  * Helper method for JAbstractHistogram.
162  *
163  * \param nx number of bins
164  * \param xmin lower limit
165  * \param xmax upper limit
166  * \return histogram
167  */
168  template<class JAbscissa_t>
170  const JAbscissa_t xmin,
171  const JAbscissa_t xmax)
172  {
174  }
175 }
176 
177 #endif
const double xmax
Definition: JQuadrature.cc:24
JRange< abscissa_type > range_type
JAbscissa_t getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
JAbstractHistogram()
Default constructor.
void setBinWidth(const abscissa_type dx, int option=0)
Set bin width.
Simple data structure for histogram binning.
int getNumberOfBins() const
Get number of bins.
void setUpperLimit(argument_type y)
Set upper limit.
Definition: JRange.hh:235
JAbscissa_t getLength() const
Get length (difference between upper and lower limit).
Definition: JRange.hh:289
friend std::istream & operator>>(std::istream &in, JAbstractHistogram< JAbscissa_t > &histogram)
Read histogram from input.
JAbscissa_t getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
double getBinWidth() const
Get bin width.
Range of values.
Definition: JRange.hh:38
const double xmin
Definition: JQuadrature.cc:23
bool is_valid() const
Check validity of range.
Definition: JRange.hh:311
bool is_valid() const
Check validity of histogram binning.
Auxiliary class to define a range between two values.
Simple data structure for an abstract collection of equidistant abscissa values.
Definition: JGrid.hh:38
JAbstractHistogram(const abscissa_type xmin, const abscissa_type xmax)
Constructor.
JAbstractHistogram< JAbscissa_t > make_histogram(const int nx, const JAbscissa_t xmin, const JAbscissa_t xmax)
Helper method for JAbstractHistogram.
JGrid< JAbscissa_t > make_grid(const int nx, const JAbscissa_t Xmin, const JAbscissa_t Xmax)
Helper method for JGrid.
Definition: JGrid.hh:209
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
void setLowerLimit(argument_type x)
Set lower limit.
Definition: JRange.hh:224
JAbstractHistogram(const int nx, const abscissa_type xmin, const abscissa_type xmax)
Constructor.