g4tools  5.4.0
axis
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_histo_axis
5 #define tools_histo_axis
6 
7 #include <string>
8 #include <vector>
9 
10 namespace tools {
11 namespace histo {
12 
13 enum { axis_UNDERFLOW_BIN = -2, axis_OVERFLOW_BIN = -1 }; //AIDA casing.
14 
15 //TC is for a coordinate.
16 //TO is for an offset used to identify a bin.
17 
18 template <class TC,class TO>
19 class axis {
20 public:
21  typedef unsigned int bn_t;
22 public:
24 public:
25  bool is_fixed_binning() const {return m_fixed;}
26  TC lower_edge() const {return m_minimum_value;}
27  TC upper_edge() const {return m_maximum_value;}
28  bn_t bins() const {return m_number_of_bins;}
29  const std::vector<TC>& edges() const {return m_edges;}
30 
31  TC bin_width(int a_bin) const {
32  if(a_bin==UNDERFLOW_BIN) {
33  return 0; //FIXME return DBL_MAX;
34  } else if(a_bin==OVERFLOW_BIN) {
35  return 0; //FIXME return DBL_MAX;
36  } else if((a_bin<0) ||(a_bin>=(int)m_number_of_bins)) {
37  return 0;
38  } else {
39  if(m_fixed) {
40  return m_bin_width;
41  } else {
42  return (m_edges[a_bin+1]-m_edges[a_bin]);
43  }
44  }
45  }
46 
47  TC bin_lower_edge(int a_bin) const {
48  if(a_bin==UNDERFLOW_BIN) {
49  return 0; //FIXME return -DBL_MAX;
50  } else if(a_bin==OVERFLOW_BIN) {
51  return 0; //FIXME return bin_upper_edge(m_number_of_bins-1);
52  } else if((a_bin<0) ||(a_bin>=(int)m_number_of_bins)) {
53  return 0;
54  } else {
55  if(m_fixed) {
56  return (m_minimum_value + a_bin * m_bin_width);
57  } else {
58  return m_edges[a_bin];
59  }
60  }
61  }
62 
63  TC bin_upper_edge(int a_bin) const {
64  if(a_bin==UNDERFLOW_BIN) {
65  return 0; //FIXME bin_lower_edge(0)
66  } else if(a_bin==OVERFLOW_BIN) {
67  return 0; //FIXME return DBL_MAX;
68  } else if((a_bin<0) ||(a_bin>=(int)m_number_of_bins)) {
69  return 0;
70  } else {
71  if(m_fixed) {
72  return (m_minimum_value + (a_bin + 1) * m_bin_width);
73  } else {
74  return m_edges[a_bin+1];
75  }
76  }
77  }
78 
79  TC bin_center(int a_bin) const {
80  if(a_bin==UNDERFLOW_BIN) {
81  return 0; //FIXME : -INF
82  } else if(a_bin==OVERFLOW_BIN) {
83  return 0; //FIXME : +INF
84  } else if(a_bin<0) {
85  return 0; //FIXME : -INF
86  } else if(a_bin>=(int)m_number_of_bins) {
87  return 0; //FIXME : +INF
88  } else {
89  if(m_fixed) {
90  return (m_minimum_value + (a_bin + 0.5) * m_bin_width);
91  } else {
92  return (m_edges[a_bin] + m_edges[a_bin+1])/2.;
93  }
94  }
95  }
96 
97  int coord_to_index(TC a_value) const {
98  if( a_value < m_minimum_value) {
99  return UNDERFLOW_BIN;
100  } else if( a_value >= m_maximum_value) {
101  return OVERFLOW_BIN;
102  } else {
103  if(m_fixed) {
104  return (int)((a_value - m_minimum_value)/m_bin_width);
105  } else {
106  for(bn_t index=0;index<m_number_of_bins;index++) {
107  if((m_edges[index]<=a_value)&&(a_value<m_edges[index+1])) {
108  return index;
109  }
110  }
111  // Should never pass here...
112  return UNDERFLOW_BIN;
113  }
114  }
115  }
116 
117  bool coord_to_absolute_index(TC a_value,bn_t& a_index) const {
118  if( a_value < m_minimum_value) {
119  a_index = 0;
120  return true;
121  } else if( a_value >= m_maximum_value) {
122  a_index = m_number_of_bins+1;
123  return true;
124  } else {
125  if(m_fixed) {
126  a_index = (bn_t)((a_value - m_minimum_value)/m_bin_width)+1;
127  return true;
128  } else {
129  for(bn_t index=0;index<m_number_of_bins;index++) {
130  if((m_edges[index]<=a_value)&&(a_value<m_edges[index+1])) {
131  a_index = index+1;
132  return true;
133  }
134  }
135  // Should never pass here...
136  a_index = 0;
137  return false;
138  }
139  }
140  }
141 
142  bool in_range_to_absolute_index(int a_in,bn_t& a_out) const {
143  // a_in is given in in-range indexing :
144  // - [0,n-1] for in-range bins
145  // - UNDERFLOW_BIN for the iaxis underflow bin
146  // - OVERFLOW_BIN for the iaxis overflow bin
147  // Return the absolute indexing in [0,n+1].
148  if(a_in==UNDERFLOW_BIN) {
149  a_out = 0;
150  return true;
151  } else if(a_in==OVERFLOW_BIN) {
152  a_out = m_number_of_bins+1;
153  return true;
154  } else if((a_in>=0)&&(a_in<(int)m_number_of_bins)){
155  a_out = a_in + 1;
156  return true;
157  } else {
158  return false;
159  }
160  }
161 
162 public:
163  // Partition :
164  bool configure(const std::vector<TC>& a_edges) {
165  // init :
166  m_number_of_bins = 0;
167  m_minimum_value = 0;
168  m_maximum_value = 0;
169  m_fixed = true;
170  m_bin_width = 0;
171  m_edges.clear();
172  // setup :
173  if(a_edges.size()<=1) return false;
174  bn_t number = (bn_t)a_edges.size()-1;
175  for(bn_t index=0;index<number;index++) {
176  if((a_edges[index]>=a_edges[index+1])) {
177  return false;
178  }
179  }
180  m_edges = a_edges;
181  m_number_of_bins = number;
182  m_minimum_value = a_edges[0];
184  m_fixed = false;
185  return true;
186  }
187 
188  bool configure(bn_t aNumber,TC aMin,TC aMax) {
189  // init :
190  m_number_of_bins = 0;
191  m_minimum_value = 0;
192  m_maximum_value = 0;
193  m_fixed = true;
194  m_bin_width = 0;
195  m_edges.clear();
196  // setup :
197  if(aNumber<=0) return false;
198  if(aMax<=aMin) return false;
199  m_number_of_bins = aNumber;
200  m_minimum_value = aMin;
201  m_maximum_value = aMax;
202  m_bin_width = (aMax - aMin)/ aNumber;
203  m_fixed = true;
204  return true;
205  }
206 
207  bool is_compatible(const axis& a_axis) const {
208  if(m_number_of_bins!=a_axis.m_number_of_bins) return false;
209  if(m_minimum_value!=a_axis.m_minimum_value) return false;
210  if(m_maximum_value!=a_axis.m_maximum_value) return false;
211  return true;
212  }
213 
214 public:
216  :m_offset(0)
217  ,m_number_of_bins(0)
218  ,m_minimum_value(0)
219  ,m_maximum_value(0)
220  ,m_fixed(true)
221  ,m_bin_width(0)
222  {}
223 
224  virtual ~axis(){}
225 public:
226  axis(const axis& a_from)
227  :m_offset(a_from.m_offset)
231  ,m_fixed(a_from.m_fixed)
232  ,m_bin_width(a_from.m_bin_width)
233  ,m_edges(a_from.m_edges)
234  {}
235 
236  axis& operator=(const axis& a_from) {
237  if(&a_from==this) return *this;
238  m_offset = a_from.m_offset;
242  m_fixed = a_from.m_fixed;
243  m_bin_width = a_from.m_bin_width;
244  m_edges = a_from.m_edges;
245  return *this;
246  }
247 public:
248  bool equals(const axis& a_from) const {
249  if(&a_from==this) return true;
250  if(m_offset!=a_from.m_offset) return false;
251  if(m_number_of_bins!=a_from.m_number_of_bins) return false;
252  if(m_minimum_value!=a_from.m_minimum_value) return false;
253  if(m_maximum_value!=a_from.m_maximum_value) return false;
254  if(m_fixed!=a_from.m_fixed) return false;
255  if(m_bin_width!=a_from.m_bin_width) return false;
256  if(m_edges!=a_from.m_edges) return false;
257  return true;
258  }
259 
260  bool operator==(const axis& a_from) const {return equals(a_from);}
261  bool operator!=(const axis& a_from) const {return !equals(a_from);}
262 
263 public:
268  bool m_fixed;
269  // Fixed size bins :
271  // Variable size bins :
272  std::vector<TC> m_edges;
273 };
274 
275 }}
276 
277 #endif
278 
279 
280 
281 
tools::histo::axis::m_edges
std::vector< TC > m_edges
Definition: axis:272
tools::histo::axis::coord_to_index
int coord_to_index(TC a_value) const
Definition: axis:97
tools::histo::axis::m_maximum_value
TC m_maximum_value
Definition: axis:267
tools::histo::axis::m_offset
TO m_offset
Definition: axis:264
tools::histo::axis_UNDERFLOW_BIN
@ axis_UNDERFLOW_BIN
Definition: axis:13
tools::histo::axis
Definition: axis:19
tools::histo::axis::operator!=
bool operator!=(const axis &a_from) const
Definition: axis:261
tools::histo::axis::upper_edge
TC upper_edge() const
Definition: axis:27
tools::histo::axis_OVERFLOW_BIN
@ axis_OVERFLOW_BIN
Definition: axis:13
tools::histo::axis::m_bin_width
TC m_bin_width
Definition: axis:270
tools::histo::axis::bin_center
TC bin_center(int a_bin) const
Definition: axis:79
tools::histo::axis::bin_lower_edge
TC bin_lower_edge(int a_bin) const
Definition: axis:47
tools::histo::axis::~axis
virtual ~axis()
Definition: axis:224
tools::histo::axis::coord_to_absolute_index
bool coord_to_absolute_index(TC a_value, bn_t &a_index) const
Definition: axis:117
tools::histo::axis::bins
bn_t bins() const
Definition: axis:28
tools::histo::axis::is_fixed_binning
bool is_fixed_binning() const
Definition: axis:25
tools::histo::axis::in_range_to_absolute_index
bool in_range_to_absolute_index(int a_in, bn_t &a_out) const
Definition: axis:142
tools::histo::axis::bin_upper_edge
TC bin_upper_edge(int a_bin) const
Definition: axis:63
tools::histo::axis::UNDERFLOW_BIN
@ UNDERFLOW_BIN
Definition: axis:23
tools::histo::axis::axis
axis(const axis &a_from)
Definition: axis:226
tools::histo::axis::configure
bool configure(const std::vector< TC > &a_edges)
Definition: axis:164
tools::histo::axis::m_minimum_value
TC m_minimum_value
Definition: axis:266
tools::histo::axis::is_compatible
bool is_compatible(const axis &a_axis) const
Definition: axis:207
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::histo::axis::bn_t
unsigned int bn_t
Definition: axis:21
tools::histo::axis::operator==
bool operator==(const axis &a_from) const
Definition: axis:260
tools::histo::axis::configure
bool configure(bn_t aNumber, TC aMin, TC aMax)
Definition: axis:188
tools::histo::axis::m_number_of_bins
bn_t m_number_of_bins
Definition: axis:265
tools::histo::axis::OVERFLOW_BIN
@ OVERFLOW_BIN
Definition: axis:23
tools::histo::axis::equals
bool equals(const axis &a_from) const
Definition: axis:248
tools::histo::axis::axis
axis()
Definition: axis:215
tools::histo::axis::m_fixed
bool m_fixed
Definition: axis:268
tools::histo::axis::operator=
axis & operator=(const axis &a_from)
Definition: axis:236
tools::histo::axis::lower_edge
TC lower_edge() const
Definition: axis:26
tools::histo::axis::bin_width
TC bin_width(int a_bin) const
Definition: axis:31
tools::histo::axis::edges
const std::vector< TC > & edges() const
Definition: axis:29