Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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
15namespace JTOOLS {}
16namespace JPP { using namespace JTOOLS; }
17
18namespace 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),
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 {
173 return JAbstractHistogram<JAbscissa_t>(nx, xmin, xmax);
174 }
175}
176
177#endif
Auxiliary class to define a range between two values.
Range of values.
Definition JRange.hh:42
void setUpperLimit(argument_type y)
Definition JRange.hh:235
bool is_valid() const
Check validity of range.
Definition JRange.hh:311
JAbscissa_t getLength() const
Definition JRange.hh:289
JAbscissa_t getLowerLimit() const
Definition JRange.hh:202
void setLowerLimit(argument_type x)
Definition JRange.hh:224
JAbscissa_t getUpperLimit() const
Definition JRange.hh:213
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
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
Simple data structure for histogram binning.
JAbstractHistogram(const abscissa_type xmin, const abscissa_type xmax)
Constructor.
JAbstractHistogram()
Default constructor.
void setBinWidth(const abscissa_type dx, int option=0)
Set bin width.
bool is_valid() const
Check validity of histogram binning.
JRange< abscissa_type > range_type
int getNumberOfBins() const
Get number of bins.
friend std::ostream & operator<<(std::ostream &out, const JAbstractHistogram< JAbscissa_t > &histogram)
Write histogram to output.
friend std::istream & operator>>(std::istream &in, JAbstractHistogram< JAbscissa_t > &histogram)
Read histogram from input.
double getBinWidth() const
Get bin width.
JAbstractHistogram(const int nx, const abscissa_type xmin, const abscissa_type xmax)
Constructor.
Simple data structure for an abstract collection of equidistant abscissa values.
Definition JGrid.hh:40