Jpp
 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  * Set bin width.
82  *
83  * If <tt>option < 0</tt>, adjust lower limit; if <tt>option > 0</tt>, adjust upper limit; else no adjustments.
84  *
85  * \param dx bin width
86  * \param option option
87  */
88  void setBinWidth(const abscissa_type dx, int option = 0)
89  {
90  number_of_bins = (int) (this->getLength() / dx);
91 
92  if (option < 0) { this->setLowerLimit(this->getUpperLimit() - number_of_bins + dx); }
93  if (option > 0) { this->setUpperLimit(this->getLowerLimit() + number_of_bins + dx); }
94  }
95 
96 
97  /**
98  * Check validity of histogram binning.
99  *
100  * \return true if both range and number of bins are valid; else false
101  */
102  bool is_valid() const
103  {
104  return static_cast<const range_type&>(*this).is_valid() && number_of_bins > 0;
105  }
106 
107 
108  /**
109  * Type conversion operator.
110  *
111  * \return grid
112  */
113  operator JGrid<abscissa_type> () const
114  {
115  return make_grid(this->getNumberOfBins() + 1, this->getLowerLimit(), this->getUpperLimit());
116  }
117 
118 
119  /**
120  * Read histogram from input.
121  *
122  * \param in input stream
123  * \param histogram histogram
124  * \return input stream
125  */
126  friend inline std::istream& operator>>(std::istream& in, JAbstractHistogram<JAbscissa_t>& histogram)
127  {
128  return in >> histogram.number_of_bins >> static_cast<range_type&>(histogram);
129  }
130 
131 
132  /**
133  * Write histogram to output.
134  *
135  * \param out output stream
136  * \param histogram histogram
137  * \return output stream
138  */
139  friend inline std::ostream& operator<<(std::ostream& out, const JAbstractHistogram<JAbscissa_t>& histogram)
140  {
141  return out << histogram.number_of_bins << ' ' << static_cast<const range_type&>(histogram);
142  }
143 
144  protected:
146  };
147 
148 
149  /**
150  * Helper method for JAbstractHistogram.
151  *
152  * \param nx number of bins
153  * \param xmin lower limit
154  * \param xmax upper limit
155  * \return histogram
156  */
157  template<class JAbscissa_t>
159  const JAbscissa_t xmin,
160  const JAbscissa_t xmax)
161  {
162  return JAbstractHistogram<JAbscissa_t>(nx, xmin, xmax);
163  }
164 }
165 
166 #endif
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
Range of values.
Definition: JRange.hh:38
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:32
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:177
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 source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
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.