g4tools  5.4.0
b2
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_b2
5 #define tools_histo_b2
6 
7 #include "base_histo"
8 
9 #include <ostream>
10 
11 namespace tools {
12 namespace histo {
13 
14 template <class TC,class TO,class TN,class TW,class TH>
15 class b2 : public base_histo<TC,TO,TN,TW,TH> {
17 protected:
18  enum {AxisX=0,AxisY=1};
19 public:
21  typedef typename parent::axis_t axis_t;
22  typedef typename parent::bn_t bn_t;
23 public:
24  virtual TH bin_error(int,int) const = 0; //for print
25 public:
26  // Partition :
27  TC mean_x() const {
28  if(parent::m_in_range_Sw==0) return 0;
30  }
31 
32  TC mean_y() const {
33  if(parent::m_in_range_Sw==0) return 0;
35  }
36 
37  TC rms_x() const {
38  if(parent::m_in_range_Sw==0) return 0;
40  return ::sqrt(::fabs((parent::m_in_range_Sx2w[0] / parent::m_in_range_Sw) - mean * mean));
41  }
42 
43  TC rms_y() const {
44  if(parent::m_in_range_Sw==0) return 0;
46  return ::sqrt(::fabs((parent::m_in_range_Sx2w[1] / parent::m_in_range_Sw) - mean * mean));
47  }
48 
49  int coord_to_index_x(TC aCoord) const {
50  return axis_x().coord_to_index(aCoord);
51  }
52  int coord_to_index_y(TC aCoord) const {
53  return axis_y().coord_to_index(aCoord);
54  }
55 
56  // bins :
57  TN bin_entries(int aI,int aJ) const {
58  TO offset;
59  if(!_find_offset(aI,aJ,offset)) return 0;
60  return parent::m_bin_entries[offset];
61  }
62 
63  TW bin_Sw(int aI,int aJ) const {
64  TO offset;
65  if(!_find_offset(aI,aJ,offset)) return 0;
66  return parent::m_bin_Sw[offset];
67  }
68 
69  TW bin_Sw2(int aI,int aJ) const {
70  TO offset;
71  if(!_find_offset(aI,aJ,offset)) return 0;
72  return parent::m_bin_Sw2[offset];
73  }
74  TC bin_Sxw(int aI,int aJ) const {
75  TO offset;
76  if(!_find_offset(aI,aJ,offset)) return 0;
77  return parent::m_bin_Sxw[offset][AxisX];
78  }
79  TC bin_Sx2w(int aI,int aJ) const {
80  TO offset;
81  if(!_find_offset(aI,aJ,offset)) return 0;
82  return parent::m_bin_Sx2w[offset][AxisX];
83  }
84  TC bin_Syw(int aI,int aJ) const {
85  TO offset;
86  if(!_find_offset(aI,aJ,offset)) return 0;
87  return parent::m_bin_Sxw[offset][AxisY];
88  }
89  TC bin_Sy2w(int aI,int aJ) const {
90  TO offset;
91  if(!_find_offset(aI,aJ,offset)) return 0;
92  return parent::m_bin_Sx2w[offset][AxisY];
93  }
94 
95  TH bin_height(int aI,int aJ) const {
96  TO offset;
97  if(!_find_offset(aI,aJ,offset)) return 0;
98  return this->get_bin_height(offset);
99  }
100 
101  TC bin_center_x(int aI) const {
102  return parent::m_axes[0].bin_center(aI);
103  }
104  TC bin_center_y(int aJ) const {
105  return parent::m_axes[1].bin_center(aJ);
106  }
107 
108  TC bin_mean_x(int aI,int aJ) const {
109  TO offset;
110  if(!_find_offset(aI,aJ,offset)) return 0;
111  TW sw = parent::m_bin_Sw[offset];
112  if(sw==0) return 0;
113  return parent::m_bin_Sxw[offset][AxisX]/sw;
114  }
115 
116  TC bin_mean_y(int aI,int aJ) const {
117  TO offset;
118  if(!_find_offset(aI,aJ,offset)) return 0;
119  TW sw = parent::m_bin_Sw[offset];
120  if(sw==0) return 0;
121  return parent::m_bin_Sxw[offset][AxisY]/sw;
122  }
123 
124  TC bin_rms_x(int aI,int aJ) const {
125  TO offset;
126  if(!_find_offset(aI,aJ,offset)) return 0;
127  TW sw = parent::m_bin_Sw[offset];
128  if(sw==0) return 0;
129  TC sxw = parent::m_bin_Sxw[offset][AxisX];
130  TC sx2w = parent::m_bin_Sx2w[offset][AxisX];
131  TC mean = sxw/sw;
132  return ::sqrt(::fabs((sx2w / sw) - mean * mean));
133  }
134 
135  TC bin_rms_y(int aI,int aJ) const {
136  TO offset;
137  if(!_find_offset(aI,aJ,offset)) return 0;
138  TW sw = parent::m_bin_Sw[offset];
139  if(sw==0) return 0;
140  TC sxw = parent::m_bin_Sxw[offset][AxisY];
141  TC sx2w = parent::m_bin_Sx2w[offset][AxisY];
142  TC mean = sxw/sw;
143  return ::sqrt(::fabs((sx2w / sw) - mean * mean));
144  }
145 
146  // Axes :
147  const axis_t& axis_x() const {return parent::m_axes[0];}
148  const axis_t& axis_y() const {return parent::m_axes[1];}
149  axis_t& axis_x() {return parent::m_axes[0];} //touchy
150  axis_t& axis_y() {return parent::m_axes[1];} //touchy
151  // Projection :
152 
153  TN bin_entries_x(int aI) const {
154  if(!parent::m_dimension) return 0;
155  bn_t ibin;
156  if(!parent::m_axes[0].in_range_to_absolute_index(aI,ibin)) return 0;
157  bn_t ybins = parent::m_axes[1].bins()+2;
158  TO offset;
159  TN _entries = 0;
160  for(bn_t jbin=0;jbin<ybins;jbin++) {
161  offset = ibin + jbin * parent::m_axes[1].m_offset;
162  _entries += parent::m_bin_entries[offset];
163  }
164  return _entries;
165  }
166 
167  TW bin_height_x(int aI) const {
168  if(!parent::m_dimension) return 0;
169  bn_t ibin;
170  if(!parent::m_axes[0].in_range_to_absolute_index(aI,ibin)) return 0;
171  bn_t ybins = parent::m_axes[1].bins()+2;
172  TO offset;
173  TW sw = 0;
174  for(bn_t jbin=0;jbin<ybins;jbin++) {
175  offset = ibin + jbin * parent::m_axes[1].m_offset;
176  sw += this->get_bin_height(offset);
177  }
178  return sw;
179  }
180 
181  TN bin_entries_y(int aJ) const {
182  if(!parent::m_dimension) return 0;
183  bn_t jbin;
184  if(!parent::m_axes[1].in_range_to_absolute_index(aJ,jbin)) return 0;
185  bn_t xbins = parent::m_axes[0].bins()+2;
186  TO offset;
187  TN _entries = 0;
188  for(bn_t ibin=0;ibin<xbins;ibin++) {
189  offset = ibin + jbin * parent::m_axes[1].m_offset;
190  _entries += parent::m_bin_entries[offset];
191  }
192  return _entries;
193  }
194 
195  TW bin_height_y(int aJ) const {
196  if(!parent::m_dimension) return 0;
197  bn_t jbin;
198  if(!parent::m_axes[1].in_range_to_absolute_index(aJ,jbin)) return 0;
199  bn_t xbins = parent::m_axes[0].bins()+2;
200  TO offset;
201  TW sw = 0;
202  for(bn_t ibin=0;ibin<xbins;ibin++) {
203  offset = ibin + jbin * parent::m_axes[1].m_offset;
204  sw += this->get_bin_height(offset);
205  }
206  return sw;
207  }
208 
209  TC Sxyw() const {return parent::m_in_range_plane_Sxyw[0];}
210 public:
211  //NOTE : print is a Python keyword.
212  void hprint(std::ostream& a_out) {
213  // A la HPRINT.
214  a_out << parent::dimension() << parent::title() << std::endl;
215  a_out
216  << " * ENTRIES = " << parent::all_entries() << std::endl;
217 
218  // 6 | 7 | 8
219  // -----------
220  // 3 | 4 | 5
221  // -----------
222  // 0 | 1 | 2
223 
224  TW height_0 = bin_height(axis_t::UNDERFLOW_BIN,
226  TW height_2 = bin_height(axis_t::OVERFLOW_BIN,
228  TW height_6 = bin_height(axis_t::UNDERFLOW_BIN,
230  TW height_8 = bin_height(axis_t::OVERFLOW_BIN,
232 
233  bn_t i,j;
234 
235  TW height_1 = 0;
236  TW height_7 = 0;
237  for(i=0;i<axis_x().bins();i++){
238  height_1 += bin_height(i,axis_t::UNDERFLOW_BIN);
239  height_7 += bin_height(i,axis_t::OVERFLOW_BIN);
240  }
241 
242  TW height_3 = 0;
243  TW height_5 = 0;
244  for(j=0;j<axis_y().bins();j++){
245  height_3 += bin_height(axis_t::UNDERFLOW_BIN,j);
246  height_5 += bin_height(axis_t::OVERFLOW_BIN,j);
247  }
248 
249  TW height_4 = 0;
250  for(i=0;i<axis_x().bins();i++){
251  for(j=0;j<axis_y().bins();j++){
252  height_4 += bin_height(i,j);
253  }
254  }
255 
256  a_out
257  << " " << height_6 << " " << height_7 << " " << height_8 << std::endl;
258  a_out
259  << " " << height_3 << " " << height_4 << " " << height_5 << std::endl;
260  a_out
261  << " " << height_0 << " " << height_1 << " " << height_2 << std::endl;
262 
263  // Some bins :
264  bn_t xbins = axis_x().bins();
265  bn_t ybins = axis_y().bins();
266  a_out
267  << " * ENTRIES[0,0] = "
268  << bin_entries(0,0)
269  << " * HEIGHT[0,0] = "
270  << bin_height(0,0)
271  << " * ERROR[0,0] = "
272  << bin_error(0,0)
273  << std::endl;
274  a_out
275  << " * ENTRIES[N/2,N/2] = "
276  << bin_entries(xbins/2,ybins/2)
277  << " * HEIGHT[N/2,N/2] = "
278  << bin_height(xbins/2,ybins/2)
279  << " * ERROR[N/2,N/2] = "
280  << bin_error(xbins/2,ybins/2)
281  << std::endl;
282  a_out
283  << " * ENTRIES[N-1,N-1] = "
284  << bin_entries(xbins-1,ybins-1)
285  << " * HEIGHT[N-1,N-1] = "
286  << bin_height(xbins-1,ybins-1)
287  << " * ERROR[N-1,N-1] = "
288  << bin_error(xbins-1,ybins-1)
289  << std::endl;
290  }
291 protected:
292  b2(const std::string& a_title,bn_t aXnumber,TC aXmin,TC aXmax,bn_t aYnumber,TC aYmin,TC aYmax) {
293  parent::m_title = a_title;
294  std::vector<bn_t> nbins;
295  nbins.push_back(aXnumber);
296  nbins.push_back(aYnumber);
297  std::vector<TC> mins;
298  mins.push_back(aXmin);
299  mins.push_back(aYmin);
300  std::vector<TC> maxs;
301  maxs.push_back(aXmax);
302  maxs.push_back(aYmax);
303  parent::configure(2,nbins,mins,maxs);
304  }
305 
306  b2(const std::string& a_title,const std::vector<TC>& a_edges_x,const std::vector<TC>& a_edges_y) {
307  parent::m_title = a_title;
308  std::vector< std::vector<TC> > edges(2);
309  edges[0] = a_edges_x;
310  edges[1] = a_edges_y;
311  parent::configure(2,edges);
312  }
313 
314  virtual ~b2(){}
315 protected:
316  b2(const b2& a_from):parent(a_from) {}
317  b2& operator=(const b2& a_from) {parent::operator=(a_from);return *this;}
318 public:
319  bool configure(bn_t aXnumber,TC aXmin,TC aXmax,bn_t aYnumber,TC aYmin,TC aYmax){
320  std::vector<bn_t> nbins;
321  nbins.push_back(aXnumber);
322  nbins.push_back(aYnumber);
323  std::vector<TC> mins;
324  mins.push_back(aXmin);
325  mins.push_back(aYmin);
326  std::vector<TC> maxs;
327  maxs.push_back(aXmax);
328  maxs.push_back(aYmax);
329  return parent::configure(2,nbins,mins,maxs);
330  }
331 
332  bool configure(const std::vector<TC>& a_edges_x,const std::vector<TC>& a_edges_y){
333  std::vector< std::vector<TC> > edges(2);
334  edges[0] = a_edges_x;
335  edges[1] = a_edges_y;
336  return parent::configure(2,edges);
337  }
338 
339 protected:
340  bool _find_offset(int aI,int aJ,TO& a_offset) const {
341  if(parent::m_dimension!=2) {a_offset=0;return false;}
342  bn_t ibin,jbin;
343  if(!parent::m_axes[0].in_range_to_absolute_index(aI,ibin)) {a_offset=0;return false;}
344  if(!parent::m_axes[1].in_range_to_absolute_index(aJ,jbin)) {a_offset=0;return false;}
345  a_offset = ibin + jbin * parent::m_axes[1].m_offset;
346  return true;
347  }
348 };
349 
350 }}
351 
352 #endif
353 
354 
355 
356 
tools::histo::b2::bin_Sw
TW bin_Sw(int aI, int aJ) const
Definition: b2:63
tools::histo::axis::coord_to_index
int coord_to_index(TC a_value) const
Definition: axis:97
tools::histo::b2::rms_x
TC rms_x() const
Definition: b2:37
tools::histo::b2::bin_entries_y
TN bin_entries_y(int aJ) const
Definition: b2:181
tools::histo::histo_data::m_bin_Sx2w
std::vector< std::vector< TC > > m_bin_Sx2w
Definition: histo_data:179
tools::histo::histo_data::m_in_range_Sxw
std::vector< TC > m_in_range_Sxw
Definition: histo_data:190
tools::histo::b2::bin_center_y
TC bin_center_y(int aJ) const
Definition: b2:104
tools::histo::b2::AxisX
@ AxisX
Definition: b2:18
tools::histo::b2::coord_to_index_x
int coord_to_index_x(TC aCoord) const
Definition: b2:49
tools::histo::axis< double, unsigned int >
tools::histo::b2::bin_height
TH bin_height(int aI, int aJ) const
Definition: b2:95
tools::histo::b2::b2
b2(const std::string &a_title, bn_t aXnumber, TC aXmin, TC aXmax, bn_t aYnumber, TC aYmin, TC aYmax)
Definition: b2:292
tools::histo::b2::bin_Sw2
TW bin_Sw2(int aI, int aJ) const
Definition: b2:69
tools::histo::b2::bin_rms_y
TC bin_rms_y(int aI, int aJ) const
Definition: b2:135
tools::histo::b2::operator=
b2 & operator=(const b2 &a_from)
Definition: b2:317
tools::histo::b2::~b2
virtual ~b2()
Definition: b2:314
tools::histo::histo_data::m_bin_entries
std::vector< TN > m_bin_entries
Definition: histo_data:175
tools::histo::b2::bin_entries_x
TN bin_entries_x(int aI) const
Definition: b2:153
tools::histo::b2::axis_y
axis_t & axis_y()
Definition: b2:150
tools::histo::histo_data::m_bin_Sw
std::vector< TW > m_bin_Sw
Definition: histo_data:176
tools::histo::b2::mean_x
TC mean_x() const
Definition: b2:27
tools::histo::b2::mean_y
TC mean_y() const
Definition: b2:32
tools::histo::b2::bin_height_x
TW bin_height_x(int aI) const
Definition: b2:167
tools::histo::base_histo::get_bin_height
virtual TH get_bin_height(TO) const =0
tools::histo::histo_data::m_in_range_Sx2w
std::vector< TC > m_in_range_Sx2w
Definition: histo_data:191
tools::histo::b2::coord_to_index_y
int coord_to_index_y(TC aCoord) const
Definition: b2:52
tools::histo::b2
Definition: b2:15
tools::histo::b2::bin_Sxw
TC bin_Sxw(int aI, int aJ) const
Definition: b2:74
tools::histo::histo_data::m_title
std::string m_title
Definition: histo_data:171
tools::histo::b2::b2
b2(const std::string &a_title, const std::vector< TC > &a_edges_x, const std::vector< TC > &a_edges_y)
Definition: b2:306
tools::histo::b2::bin_height_y
TW bin_height_y(int aJ) const
Definition: b2:195
tools::histo::axis::bins
bn_t bins() const
Definition: axis:28
base_histo
tools::histo::histo_data::m_bin_Sxw
std::vector< std::vector< TC > > m_bin_Sxw
Definition: histo_data:178
tools::histo::b2::b2
b2(const b2 &a_from)
Definition: b2:316
tools::histo::axis::UNDERFLOW_BIN
@ UNDERFLOW_BIN
Definition: axis:23
tools::histo::b2::configure
bool configure(bn_t aXnumber, TC aXmin, TC aXmax, bn_t aYnumber, TC aYmin, TC aYmax)
Definition: b2:319
tools::histo::base_histo< double, unsigned int, unsigned int, double, double >::bn_t
axis_t::bn_t bn_t
Definition: base_histo:37
tools::histo::b2::axis_y
const axis_t & axis_y() const
Definition: b2:148
tools::histo::b2::bin_Sy2w
TC bin_Sy2w(int aI, int aJ) const
Definition: b2:89
tools::histo::base_histo::all_entries
TN all_entries() const
Definition: base_histo:95
tools::histo::b2::base_histo_t
base_histo< TC, TO, TN, TW, TH > base_histo_t
Definition: b2:20
tools::histo::histo_data::m_axes
std::vector< axis_t > m_axes
Definition: histo_data:181
tools::histo::base_histo::title
const std::string & title() const
Definition: base_histo:87
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::histo::b2::Sxyw
TC Sxyw() const
Definition: b2:209
tools::histo::base_histo::configure
bool configure(dim_t a_dim, const std::vector< bn_t > &aNumbers, const std::vector< TC > &aMins, const std::vector< TC > &aMaxs)
Definition: base_histo:293
tools::histo::b2::bin_entries
TN bin_entries(int aI, int aJ) const
Definition: b2:57
tools::histo::histo_data::m_bin_Sw2
std::vector< TW > m_bin_Sw2
Definition: histo_data:177
tools::histo::b2::axis_x
axis_t & axis_x()
Definition: b2:149
tools::histo::b2::bin_mean_y
TC bin_mean_y(int aI, int aJ) const
Definition: b2:116
tools::histo::histo_data::m_in_range_Sw
TW m_in_range_Sw
Definition: histo_data:188
tools::histo::b2::bin_center_x
TC bin_center_x(int aI) const
Definition: b2:101
tools::histo::b2::bin_Syw
TC bin_Syw(int aI, int aJ) const
Definition: b2:84
tools::histo::b2::_find_offset
bool _find_offset(int aI, int aJ, TO &a_offset) const
Definition: b2:340
tools::histo::b2::AxisY
@ AxisY
Definition: b2:18
tools::histo::axis::OVERFLOW_BIN
@ OVERFLOW_BIN
Definition: axis:23
tools::histo::base_histo::operator=
base_histo & operator=(const base_histo &a_from)
Definition: base_histo:76
tools::histo::b2::hprint
void hprint(std::ostream &a_out)
Definition: b2:212
tools::histo::b2::bin_mean_x
TC bin_mean_x(int aI, int aJ) const
Definition: b2:108
tools::histo::base_histo
Definition: base_histo:27
tools::histo::b2::axis_t
parent::axis_t axis_t
Definition: b2:21
tools::histo::histo_data::m_in_range_plane_Sxyw
std::vector< TC > m_in_range_plane_Sxyw
Definition: histo_data:183
tools::histo::histo_data::m_dimension
dim_t m_dimension
Definition: histo_data:172
tools::histo::base_histo::dimension
dim_t dimension() const
Definition: base_histo:89
tools::histo::b2::bin_error
virtual TH bin_error(int, int) const =0
tools::histo::b2::axis_x
const axis_t & axis_x() const
Definition: b2:147
tools::histo::b2::bin_rms_x
TC bin_rms_x(int aI, int aJ) const
Definition: b2:124
tools::histo::b2::configure
bool configure(const std::vector< TC > &a_edges_x, const std::vector< TC > &a_edges_y)
Definition: b2:332
tools::histo::b2::bn_t
parent::bn_t bn_t
Definition: b2:22
tools::histo::b2::bin_Sx2w
TC bin_Sx2w(int aI, int aJ) const
Definition: b2:79
tools::histo::b2::rms_y
TC rms_y() const
Definition: b2:43