tttrlib
A library for time-tagged time resolved data
Loading...
Searching...
No Matches
HistogramAxis.h
Go to the documentation of this file.
1#ifndef TTTRLIB_HISTOGRAMAXIS_H
2#define TTTRLIB_HISTOGRAMAXIS_H
3
4#include <algorithm>
5#include <vector>
6#include <cstdio>
7#include <string>
8#include <map>
9#include <cmath>
10
11template <typename T>
12inline void linspace(double start, double stop, T *bin_edges, int n_bins){
13 double bin_width = (stop - start) / n_bins;
14 for(int i=0; i<n_bins; i++){
15 bin_edges[i] = start + ((T) i) * bin_width;
16 }
17}
18
19
20template <typename T>
21inline void logspace(double start, double stop, T *bin_edges, int n_bins){
22 linspace(std::log(start), std::log(stop), bin_edges, n_bins);
23 for(int i=0; i<n_bins; i++){
24 bin_edges[i] = std::pow(10.0, bin_edges[i]);
25 }
26}
27
42template <typename T>
43inline int search_bin_idx(T value, T *bin_edges, int n_bins){
44 int b, e, m;
45
46 // ignore values outside of the bounds
47 if ((value < bin_edges[0]) || (value > bin_edges[n_bins - 2])) {
48 return -1;
49 }
50
51 b = 0;
52 e = n_bins;
53 do {
54 m = (e - b) / 2 + b;
55 if (value > bin_edges[m]) {
56 b = m;
57 } else {
58 e = m;
59 }
60 } while ((value < bin_edges[m]) || (value >= bin_edges[m + 1]));
61 return m;
62}
63
64
74template <typename T>
75inline int calc_bin_idx(T begin, T bin_width, T value){
76 return ((value - begin) / bin_width);
77}
78
79template<class T>
81
82private:
83 std::string name;
84 double begin;
85 double end;
86 int n_bins;
87 double bin_width; // for logarithmic spacing the bin_width in logarithms
88 std::vector<T> bin_edges;
89 int axis_type;
90
91public:
92
96 void update(){
97 bin_edges.resize(n_bins);
98 switch (HistogramAxis::axis_type){
99 case 0:
100 // linear axis
101 bin_width = (end - begin) / n_bins;
102 linspace(begin, end, bin_edges.data(), bin_edges.size());
103 break;
104 case 1:
105 // logarithmic axis
106 bin_width = (log(end) - log(begin)) / n_bins;
107 logspace(begin, end, bin_edges.data(), bin_edges.size());
108 break;
109 }
110 }
111
112 void setAxisType(const std::string &axis_type) {
113 if(axis_type == "log10")
114 HistogramAxis::axis_type = 1;
115 if(axis_type == "lin")
116 HistogramAxis::axis_type = 0;
117 }
118
120 return n_bins;
121 }
122
123 int getBinIdx(T value){
124 switch (axis_type){
125 case 0:
126 // linear
127 return calc_bin_idx(begin, bin_width, value);
128 case 1:
129 // logarithm
130 return calc_bin_idx(begin, bin_width, std::log10(value));
131 default:
132 return search_bin_idx(value, bin_edges.data(), bin_edges.size());
133 }
134 }
135
137 return bin_edges.data();
138 }
139
140 void getBins(T* bin_edges, int n_bins){
141 for(int i = 0; i < n_bins; i++){
142 bin_edges[i] = this->bin_edges[i];
143 }
144 }
145
146 const std::string &getName() const {
147 return name;
148 }
149
150 void setName(const std::string &name) {
151 HistogramAxis::name = name;
152 }
153
154 HistogramAxis() = default;
156 std::string name,
157 T begin,
158 T end,
159 int n_bins,
160 std::string axis_type
161 ){
162
163 // make sure that begin < end
164 if(begin > end){
165 T temp = begin;
166 begin = end;
167 end = temp;
168 }
169
170 HistogramAxis::begin = begin;
171 HistogramAxis::end = end;
172 HistogramAxis::n_bins = n_bins;
174 HistogramAxis::name = name;
176 }
177};
178
179#endif //TTTRLIB_HISTOGRAMAXIS_H
int search_bin_idx(T value, T *bin_edges, int n_bins)
Definition HistogramAxis.h:43
void logspace(double start, double stop, T *bin_edges, int n_bins)
Definition HistogramAxis.h:21
void linspace(double start, double stop, T *bin_edges, int n_bins)
Definition HistogramAxis.h:12
int calc_bin_idx(T begin, T bin_width, T value)
Definition HistogramAxis.h:75
Definition HistogramAxis.h:80
void setAxisType(const std::string &axis_type)
Definition HistogramAxis.h:112
const std::string & getName() const
Definition HistogramAxis.h:146
int getNumberOfBins()
Definition HistogramAxis.h:119
HistogramAxis(std::string name, T begin, T end, int n_bins, std::string axis_type)
Definition HistogramAxis.h:155
void getBins(T *bin_edges, int n_bins)
Definition HistogramAxis.h:140
void setName(const std::string &name)
Definition HistogramAxis.h:150
void update()
Definition HistogramAxis.h:96
T * getBins()
Definition HistogramAxis.h:136
HistogramAxis()=default
int getBinIdx(T value)
Definition HistogramAxis.h:123