g4tools  5.4.0
histo_data
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_histo_data
5 #define tools_histo_histo_data
6 
7 #include <vector>
8 #include <map> //for annotations
9 
10 #include "axes"
11 #include "../eqT"
12 //#include "../vmanip" //vequ
13 
14 namespace tools {
15 namespace histo {
16 
17 inline unsigned int dim_planes(unsigned int a_dim) {
18  // m_dim = 1 -> 0
19  // m_dim = 2 -> 0+1=1
20  // m_dim = 3 -> 0+1+2=3
21  // m_dim = 4 -> 0+1+2+3=6
22  typedef unsigned int dim_t;
23  dim_t n = 0;
24  for(dim_t i=0;i<a_dim;i++) n += i;
25  return n;
26 }
27 
28 //TC is for a coordinate.
29 //TO is for an offset used to identify a bin.
30 //TN is for a number of entries.
31 //TW is for a weight.
32 
33 template <class TC,class TO,class TN,class TW>
34 class histo_data {
35 public:
37  typedef unsigned int dim_t;
38  typedef std::map<std::string,std::string> annotations_t;
39 public:
41  :m_dimension(0)
42  ,m_bin_number(0)
43  ,m_all_entries(0)
45  ,m_in_range_Sw(0)
46  ,m_in_range_Sw2(0)
47  {}
48 public:
49  histo_data(const histo_data& a_from)
50  :m_title(a_from.m_title)
51  ,m_dimension(a_from.m_dimension)
52  ,m_bin_number(a_from.m_bin_number)
54  ,m_bin_Sw(a_from.m_bin_Sw)
55  ,m_bin_Sw2(a_from.m_bin_Sw2)
56  ,m_bin_Sxw(a_from.m_bin_Sxw)
57  ,m_bin_Sx2w(a_from.m_bin_Sx2w)
58  ,m_axes(a_from.m_axes)
67  {}
68 
69  histo_data& operator=(const histo_data& a_from) {
70  if(&a_from==this) return *this;
71  m_title = a_from.m_title;
72  m_dimension = a_from.m_dimension;
73  m_bin_number = a_from.m_bin_number;
75  m_bin_Sw = a_from.m_bin_Sw;
76  m_bin_Sw2 = a_from.m_bin_Sw2;
77  m_bin_Sxw = a_from.m_bin_Sxw;
78  m_bin_Sx2w = a_from.m_bin_Sx2w;
79  m_axes = a_from.m_axes;
88  return *this;
89  }
90 
91  virtual ~histo_data(){}
92 protected:
94  //m_in_range_plane_Sxyw is not a fast getter.
95  m_all_entries = 0;
97  m_in_range_Sw = 0;
98  m_in_range_Sw2 = 0;
99  m_in_range_Sxw.assign(m_dimension,0);
100  m_in_range_Sx2w.assign(m_dimension,0);
101  }
102 public:
105  {for(TO ibin=0;ibin<m_bin_number;ibin++) {
106  if(!histo::is_out(m_axes,ibin)) {
108  m_in_range_Sw += m_bin_Sw[ibin];
109  m_in_range_Sw2 += m_bin_Sw2[ibin];
110  for(dim_t iaxis=0;iaxis<m_dimension;iaxis++) {
111  m_in_range_Sxw[iaxis] += m_bin_Sxw[ibin][iaxis];
112  m_in_range_Sx2w[iaxis] += m_bin_Sx2w[ibin][iaxis];
113  }
114  }
115  m_all_entries += m_bin_entries[ibin];
116  }}
117  }
118 
119  bool equals(const histo_data& a_from,const TW& a_prec,TW(*a_fabs)(TW)) const {
120  if(&a_from==this) return true;
121  if(m_title!=a_from.m_title) return false;
122  if(m_dimension!=a_from.m_dimension) return false;
123  if(m_bin_number!=a_from.m_bin_number) return false;
124  if(m_bin_entries!=a_from.m_bin_entries) return false;
125  //if(!vequ(m_bin_entries,a_from.m_bin_entries)) return false;
126  if(!vectors_are_equal(m_bin_Sw,a_from.m_bin_Sw,a_prec,a_fabs)) return false;
127  if(!vectors_are_equal(m_bin_Sw2,a_from.m_bin_Sw2,a_prec,a_fabs)) return false;
128  if(!vecvecs_are_equal(m_bin_Sxw,a_from.m_bin_Sxw,a_prec,a_fabs)) return false;
129  if(!vecvecs_are_equal(m_bin_Sx2w,a_from.m_bin_Sx2w,a_prec,a_fabs)) return false;
130  if(m_axes!=a_from.m_axes) return false;
131  if(!vectors_are_equal(m_in_range_plane_Sxyw,a_from.m_in_range_plane_Sxyw,a_prec,a_fabs)) return false;
132  if(m_annotations!=a_from.m_annotations) return false;
133 
134  if(m_all_entries!=a_from.m_all_entries) return false;
135  if(m_in_range_entries!=a_from.m_in_range_entries) return false;
136 
137  if(!numbers_are_equal(m_in_range_Sw,a_from.m_in_range_Sw,a_prec,a_fabs)) return false;
138  if(!numbers_are_equal(m_in_range_Sw2,a_from.m_in_range_Sw2,a_prec,a_fabs)) return false;
139  if(!vectors_are_equal(m_in_range_Sxw,a_from.m_in_range_Sxw,a_prec,a_fabs)) return false;
140  if(!vectors_are_equal(m_in_range_Sx2w,a_from.m_in_range_Sx2w,a_prec,a_fabs)) return false;
141 
142  return true;
143  }
144  bool equals_TH(const histo_data& a_from,const TW& a_prec,TW(*a_fabs)(TW),bool a_cmp_bin_Sw2) const {
145  // used to compare with an histo built from a TH stream out from a CERN-ROOT file.
146  if(&a_from==this) return true;
147  if(m_title!=a_from.m_title) return false;
148  if(m_dimension!=a_from.m_dimension) return false;
149  if(m_bin_number!=a_from.m_bin_number) return false;
150  //if(m_bin_entries!=a_from.m_bin_entries) return false;
151  if(!vectors_are_equal(m_bin_Sw,a_from.m_bin_Sw,a_prec,a_fabs)) return false;
152  if(a_cmp_bin_Sw2) if(!vectors_are_equal(m_bin_Sw2,a_from.m_bin_Sw2,a_prec,a_fabs)) return false;
153  //if(!vecvecs_are_equal(m_bin_Sxw,a_from.m_bin_Sxw,a_prec,a_fabs)) return false;
154  //if(!vecvecs_are_equal(m_bin_Sx2w,a_from.m_bin_Sx2w,a_prec,a_fabs)) return false;
155  if(m_axes!=a_from.m_axes) return false;
156  if(!vectors_are_equal(m_in_range_plane_Sxyw,a_from.m_in_range_plane_Sxyw,a_prec,a_fabs)) return false;
157  //if(m_annotations!=a_from.m_annotations) return false;
158 
159  if(m_all_entries!=a_from.m_all_entries) return false;
160  //if(m_in_range_entries!=a_from.m_in_range_entries) return false;
161 
162  if(!numbers_are_equal(m_in_range_Sw,a_from.m_in_range_Sw,a_prec,a_fabs)) return false;
163  if(!numbers_are_equal(m_in_range_Sw2,a_from.m_in_range_Sw2,a_prec,a_fabs)) return false;
164  //if(!vectors_are_equal(m_in_range_Sxw,a_from.m_in_range_Sxw,a_prec,a_fabs)) return false;
165  //if(!vectors_are_equal(m_in_range_Sx2w,a_from.m_in_range_Sx2w,a_prec,a_fabs)) return false;
166 
167  return true;
168  }
169 public:
170  // General :
171  std::string m_title;
173  // Bins :
175  std::vector<TN> m_bin_entries;
176  std::vector<TW> m_bin_Sw;
177  std::vector<TW> m_bin_Sw2;
178  std::vector< std::vector<TC> > m_bin_Sxw;
179  std::vector< std::vector<TC> > m_bin_Sx2w;
180  // Axes :
181  std::vector<axis_t> m_axes;
182  // etc :
183  std::vector<TC> m_in_range_plane_Sxyw; // ill-defined relative to slicing, sub, div, mult operations. Handled because of CERN-ROOT.
184  std::map<std::string,std::string> m_annotations;
185  // fast getters :
186  TN m_all_entries; //used if reading from a ROOT file.
190  std::vector<TC> m_in_range_Sxw;
191  std::vector<TC> m_in_range_Sx2w;
192 };
193 
194 }}
195 
196 #endif
tools::histo::histo_data::m_in_range_Sw2
TW m_in_range_Sw2
Definition: histo_data:189
tools::histo::histo_data::m_annotations
std::map< std::string, std::string > m_annotations
Definition: histo_data:184
tools::histo::histo_data::update_fast_getters
void update_fast_getters()
Definition: histo_data:103
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::histo_data::equals
bool equals(const histo_data &a_from, const TW &a_prec, TW(*a_fabs)(TW)) const
Definition: histo_data:119
tools::histo::axis
Definition: axis:19
tools::histo::histo_data::m_bin_entries
std::vector< TN > m_bin_entries
Definition: histo_data:175
tools::histo::histo_data::m_bin_Sw
std::vector< TW > m_bin_Sw
Definition: histo_data:176
tools::histo::histo_data::histo_data
histo_data()
Definition: histo_data:40
tools::vecvecs_are_equal
bool vecvecs_are_equal(const VECVEC &a_1, const VECVEC &a_2, const PREC &a_prec, PREC(*a_fabs)(const PREC &))
Definition: eqT:40
tools::histo::histo_data::m_in_range_entries
TN m_in_range_entries
Definition: histo_data:187
tools::vectors_are_equal
bool vectors_are_equal(const VEC &a_1, const VEC &a_2, const PREC &a_prec, PREC(*a_fabs)(const PREC &))
Definition: eqT:23
tools::histo::histo_data::dim_t
unsigned int dim_t
Definition: histo_data:37
tools::histo::histo_data::m_in_range_Sx2w
std::vector< TC > m_in_range_Sx2w
Definition: histo_data:191
tools::histo::histo_data::m_title
std::string m_title
Definition: histo_data:171
tools::histo::histo_data::~histo_data
virtual ~histo_data()
Definition: histo_data:91
tools::histo::histo_data::histo_data
histo_data(const histo_data &a_from)
Definition: histo_data:49
tools::histo::is_out
bool is_out(const std::vector< axis< TC, TO > > &a_axes, TO a_offset)
Definition: axes:16
tools::histo::dim_planes
unsigned int dim_planes(unsigned int a_dim)
Definition: histo_data:17
tools::histo::histo_data::annotations_t
std::map< std::string, std::string > annotations_t
Definition: histo_data:38
tools::histo::histo_data::operator=
histo_data & operator=(const histo_data &a_from)
Definition: histo_data:69
tools::histo::histo_data::m_bin_Sxw
std::vector< std::vector< TC > > m_bin_Sxw
Definition: histo_data:178
tools::histo::histo_data
Definition: histo_data:34
tools::histo::histo_data::reset_fast_getters
void reset_fast_getters()
Definition: histo_data:93
tools::histo::histo_data::m_axes
std::vector< axis_t > m_axes
Definition: histo_data:181
axes
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::histo::histo_data::m_bin_Sw2
std::vector< TW > m_bin_Sw2
Definition: histo_data:177
tools::histo::histo_data::m_all_entries
TN m_all_entries
Definition: histo_data:186
tools::histo::histo_data::m_in_range_Sw
TW m_in_range_Sw
Definition: histo_data:188
tools::numbers_are_equal
bool numbers_are_equal(const NUMBER &a_left, const NUMBER &a_right, const PREC &a_prec, PREC(*a_fabs)(const NUMBER &))
Definition: eqT:10
tools::histo::histo_data::m_bin_number
TO m_bin_number
Definition: histo_data:174
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::histo_data::equals_TH
bool equals_TH(const histo_data &a_from, const TW &a_prec, TW(*a_fabs)(TW), bool a_cmp_bin_Sw2) const
Definition: histo_data:144
tools::histo::histo_data::axis_t
axis< TC, TO > axis_t
Definition: histo_data:36