Jpp
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
JTOOLS::JAbstractHistogram::range_type
JRange< abscissa_type > range_type
Definition: JAbstractHistogram.hh:29
JTOOLS::JRange< JAbscissa_t >::getLowerLimit
JAbscissa_t getLowerLimit() const
Get lower limit.
Definition: JRange.hh:215
JTOOLS::JAbstractHistogram::operator<<
friend std::ostream & operator<<(std::ostream &out, const JAbstractHistogram< JAbscissa_t > &histogram)
Write histogram to output.
Definition: JAbstractHistogram.hh:139
JTOOLS::JRange< JAbscissa_t >::getUpperLimit
JAbscissa_t getUpperLimit() const
Get upper limit.
Definition: JRange.hh:226
JTOOLS::JAbstractHistogram::setBinWidth
void setBinWidth(const abscissa_type dx, int option=0)
Set bin width.
Definition: JAbstractHistogram.hh:88
JTOOLS::JAbstractHistogram::JAbstractHistogram
JAbstractHistogram()
Default constructor.
Definition: JAbstractHistogram.hh:35
JGrid.hh
JTOOLS::JRange< JAbscissa_t >::getLength
JAbscissa_t getLength() const
Get length (difference between upper and lower limit).
Definition: JRange.hh:302
JTOOLS::JRange
Range of values.
Definition: JRange.hh:34
JTOOLS::JAbstractHistogram::abscissa_type
JAbscissa_t abscissa_type
Definition: JAbstractHistogram.hh:28
JTOOLS::JRange< JAbscissa_t >::setLowerLimit
void setLowerLimit(argument_type x)
Set lower limit.
Definition: JRange.hh:237
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTOOLS::JAbstractHistogram::getNumberOfBins
int getNumberOfBins() const
Get number of bins.
Definition: JAbstractHistogram.hh:74
JRange.hh
JTOOLS::JAbstractHistogram::operator>>
friend std::istream & operator>>(std::istream &in, JAbstractHistogram< JAbscissa_t > &histogram)
Read histogram from input.
Definition: JAbstractHistogram.hh:126
JTOOLS::JRange< JAbscissa_t >::setUpperLimit
void setUpperLimit(argument_type y)
Set upper limit.
Definition: JRange.hh:248
JTOOLS::JGrid
Simple data structure for an abstract collection of equidistant abscissa values.
Definition: JGrid.hh:32
JTOOLS::JAbstractHistogram::is_valid
bool is_valid() const
Check validity of histogram binning.
Definition: JAbstractHistogram.hh:102
JTOOLS::JAbstractHistogram::JAbstractHistogram
JAbstractHistogram(const abscissa_type xmin, const abscissa_type xmax)
Constructor.
Definition: JAbstractHistogram.hh:62
JTOOLS::make_grid
JGrid< JAbscissa_t > make_grid(const int nx, const JAbscissa_t Xmin, const JAbscissa_t Xmax)
Helper method for JGrid.
Definition: JGrid.hh:177
JTOOLS::JAbstractHistogram
Simple data structure for histogram binning.
Definition: JAbstractHistogram.hh:24
JTOOLS::make_histogram
JAbstractHistogram< JAbscissa_t > make_histogram(const int nx, const JAbscissa_t xmin, const JAbscissa_t xmax)
Helper method for JAbstractHistogram.
Definition: JAbstractHistogram.hh:158
JTOOLS::JAbstractHistogram::number_of_bins
int number_of_bins
Definition: JAbstractHistogram.hh:145
JTOOLS
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Definition: JAbstractCollection.hh:9
JTOOLS::JAbstractHistogram::JAbstractHistogram
JAbstractHistogram(const int nx, const abscissa_type xmin, const abscissa_type xmax)
Constructor.
Definition: JAbstractHistogram.hh:48