g4tools  5.4.0
c2d
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_c2d
5 #define tools_histo_c2d
6 
7 #include "base_cloud"
8 
9 #include "../mnmx"
10 
11 #include "h2d"
12 
13 namespace tools {
14 namespace histo {
15 
16 class c2d : public base_cloud {
17 public:
18  static const std::string& s_class() {
19  static const std::string s_v("tools::histo::c2d");
20  return s_v;
21  }
22 public:
23  bool set_title(const std::string&);
24  unsigned int dimension() const {return 2;}
25  bool reset();
26  unsigned int entries() const { return m_histo ? m_histo->all_entries() : (unsigned int)m_ws.size();}
27 public:
28  double sum_of_weights() const { return (m_histo ? m_histo->sum_bin_heights() : m_Sw);}
29  bool convert_to_histogram();
30  bool is_converted() const {return m_histo ? true : false;}
31  bool scale(double);
32 public:
33  bool fill(double,double,double = 1);
34  double lower_edge_x() const {return m_histo ? m_histo->axis_x().lower_edge() : m_lower_x;}
35  double lower_edge_y() const { return m_histo ? m_histo->axis_y().lower_edge() : m_lower_y;}
36  double upper_edge_x() const { return m_histo ? m_histo->axis_x().upper_edge() : m_upper_x;}
37  double upper_edge_y() const { return m_histo ? m_histo->axis_y().upper_edge() : m_upper_y;}
38  double value_x(unsigned int a_index) const { return m_histo ? 0 : m_xs[a_index];}
39  double value_y(unsigned int a_index) const { return m_histo ? 0 : m_ys[a_index];}
40  double weight(unsigned int a_index) const { return m_histo ? 0 : m_ws[a_index];}
41  double mean_x() const {return m_histo ? m_histo->mean_x() : (m_Sw?m_Sxw/m_Sw:0);}
42  double mean_y() const {return m_histo ? m_histo->mean_y() : (m_Sw?m_Syw/m_Sw:0);}
43  double rms_x() const;
44  double rms_y() const;
45  bool convert(unsigned int,double,double,unsigned int,double,double);
46  bool convert(const std::vector<double>&,const std::vector<double>&);
47  const histo::h2d& histogram() const;
48  template <class HISTO>
49  bool fill_histogram(HISTO& a_histo) const {
50  size_t number = m_xs.size();
51  for(size_t index=0;index<number;index++) {
52  if(!a_histo.fill(m_xs[index],m_ys[index],m_ws[index])) return false;
53  }
54  return true;
55  }
56  bool set_conversion_parameters(unsigned int,double,double,unsigned int,double,double);
57 
58  bool set_histogram(h2d* a_histo){ //we take ownership of a_histo.
59  reset();
60  m_histo = a_histo;
61  return true;
62  }
63 public:
64  c2d();
65  c2d(const std::string&,int=base_cloud::UNLIMITED());
66  virtual ~c2d(){delete m_histo;}
67 public:
68  c2d(const c2d& a_from)
69  :base_cloud(a_from)
70  ,m_xs(a_from.m_xs)
71  ,m_ys(a_from.m_ys)
72  ,m_lower_x(a_from.m_lower_x)
73  ,m_upper_x(a_from.m_upper_x)
74  ,m_lower_y(a_from.m_lower_y)
75  ,m_upper_y(a_from.m_upper_y)
76  ,m_Sxw(a_from.m_Sxw)
77  ,m_Sx2w(a_from.m_Sx2w)
78  ,m_Syw(a_from.m_Syw)
79  ,m_Sy2w(a_from.m_Sy2w)
80  ,m_cnv_x_num(a_from.m_cnv_x_num)
81  ,m_cnv_x_min(a_from.m_cnv_x_min)
82  ,m_cnv_x_max(a_from.m_cnv_x_max)
83  ,m_cnv_y_num(a_from.m_cnv_y_num)
84  ,m_cnv_y_min(a_from.m_cnv_y_min)
85  ,m_cnv_y_max(a_from.m_cnv_y_max)
86  ,m_histo(0)
87  {
88  if(a_from.m_histo) {
89  m_histo = new histo::h2d(*a_from.m_histo);
90  }
91  }
92 
93  c2d& operator=(const c2d& a_from) {
94  base_cloud::operator=(a_from);
95  if(&a_from==this) return *this;
96  m_xs = a_from.m_xs;
97  m_ys = a_from.m_ys;
98  m_lower_x = a_from.m_lower_x;
99  m_upper_x = a_from.m_upper_x;
100  m_lower_y = a_from.m_lower_y;
101  m_upper_y = a_from.m_upper_y;
102  m_Sxw = a_from.m_Sxw;
103  m_Sx2w = a_from.m_Sx2w;
104  m_Syw = a_from.m_Syw;
105  m_Sy2w = a_from.m_Sy2w;
106  m_cnv_x_num = a_from.m_cnv_x_num;
107  m_cnv_x_min = a_from.m_cnv_x_min;
108  m_cnv_x_max = a_from.m_cnv_x_max;
109  m_cnv_y_num = a_from.m_cnv_y_num;
110  m_cnv_y_min = a_from.m_cnv_y_min;
111  m_cnv_y_max = a_from.m_cnv_y_max;
112  delete m_histo;
113  m_histo = 0;
114  if(a_from.m_histo) {
115  m_histo = new histo::h2d(*a_from.m_histo);
116  }
117  return *this;
118  }
119 public: //AIDA API
120  double lowerEdgeX() const {return lower_edge_x();}
121  double lowerEdgeY() const {return lower_edge_y();}
122  double upperEdgeX() const {return upper_edge_x();}
123  double upperEdgeY() const {return upper_edge_y();}
124  template <class HISTO>
125  bool fillHistogram(HISTO& a_histo) const {return fill_histogram<HISTO>(a_histo);}
126 protected:
127  void clear();
128 protected:
129  std::vector<double> m_xs;
130  std::vector<double> m_ys;
131  double m_lower_x;
132  double m_upper_x;
133  double m_lower_y;
134  double m_upper_y;
135  double m_Sxw;
136  double m_Sx2w;
137  double m_Syw;
138  double m_Sy2w;
139  //
140  unsigned int m_cnv_x_num;
141  double m_cnv_x_min;
142  double m_cnv_x_max;
143  unsigned int m_cnv_y_num;
144  double m_cnv_y_min;
145  double m_cnv_y_max;
147 };
148 
149 }}
150 
151 namespace tools {
152 namespace histo {
153 
154 inline
156 :base_cloud(UNLIMITED())
157 ,m_lower_x(0)
158 ,m_upper_x(0)
159 ,m_lower_y(0)
160 ,m_upper_y(0)
161 ,m_Sxw(0)
162 ,m_Sx2w(0)
163 ,m_Syw(0)
164 ,m_Sy2w(0)
165 ,m_cnv_x_num(0)
166 ,m_cnv_x_min(0)
167 ,m_cnv_x_max(0)
168 ,m_cnv_y_num(0)
169 ,m_cnv_y_min(0)
170 ,m_cnv_y_max(0)
171 ,m_histo(0)
172 {}
173 
174 inline
175 c2d::c2d(const std::string& a_title,int aLimit)
176 :base_cloud(aLimit)
177 ,m_lower_x(0)
178 ,m_upper_x(0)
179 ,m_lower_y(0)
180 ,m_upper_y(0)
181 ,m_Sxw(0)
182 ,m_Sx2w(0)
183 ,m_Syw(0)
184 ,m_Sy2w(0)
185 ,m_cnv_x_num(0)
186 ,m_cnv_x_min(0)
187 ,m_cnv_x_max(0)
188 ,m_cnv_y_num(0)
189 ,m_cnv_y_min(0)
190 ,m_cnv_y_max(0)
191 ,m_histo(0)
192 {
193  set_title(a_title);
194 }
195 
196 inline
197 void c2d::clear(){
198  m_lower_x = 0;
199  m_upper_x = 0;
200  m_lower_y = 0;
201  m_upper_y = 0;
202  m_Sw = 0;
203  m_Sxw = 0;
204  m_Sx2w = 0;
205  m_Syw = 0;
206  m_Sy2w = 0;
207  m_xs.clear();
208  m_ys.clear();
209  m_ws.clear();
210 }
211 
212 inline
214  unsigned int a_bins_x,double a_lower_edge_x,double a_upper_edge_x
215 ,unsigned int a_bins_y,double a_lower_edge_y,double a_upper_edge_y
216 ) {
217  if(m_histo) return true; // Done.
219  a_bins_x,a_lower_edge_x,a_upper_edge_x,
220  a_bins_y,a_lower_edge_y,a_upper_edge_y);
221  if(!m_histo) return false;
222  bool status = fill_histogram(*m_histo);
223  clear();
224  return status;
225 }
226 
227 inline
229  if( (m_cnv_x_num<=0) || (m_cnv_x_max<=m_cnv_x_min) ||
230  (m_cnv_y_num<=0) || (m_cnv_y_max<=m_cnv_y_min) ) {
231  double dx = 0.01 * (upper_edge_x() - lower_edge_x())/BINS();
232  double dy = 0.01 * (upper_edge_y() - lower_edge_y())/BINS();
233  return convert(BINS(),lower_edge_x(),upper_edge_x()+dx,
234  BINS(),lower_edge_y(),upper_edge_y()+dy);
235  } else {
238  }
239 }
240 
241 
242 inline
243 bool c2d::set_title(const std::string& a_title){
244  m_title = a_title;
245  if(m_histo) m_histo->set_title(a_title);
246  return true;
247 }
248 
249 inline
250 bool c2d::scale(double a_scale) {
251  if(m_histo) {
252  return m_histo->scale(a_scale);
253  } else {
254  size_t number = m_ws.size();
255  for(size_t index=0;index<number;index++) m_ws[index] *= a_scale;
256  m_Sw *= a_scale;
257  m_Sxw *= a_scale;
258  m_Sx2w *= a_scale;
259  m_Syw *= a_scale;
260  m_Sy2w *= a_scale;
261  return true;
262  }
263 }
264 
265 inline
266 bool c2d::reset() {
267  clear();
268  delete m_histo;
269  m_histo = 0;
270  return true;
271 }
272 
273 inline
274 bool c2d::fill(double aX,double aY,double aW){
275  if(!m_histo && (m_limit!=UNLIMITED()) && ((int)m_xs.size()>=m_limit)){
277  }
278 
279  if(m_histo) {
280  return m_histo->fill(aX,aY,aW);
281  } else {
282  if(m_xs.size()) {
283  m_lower_x = mn<double>(aX,m_lower_x);
284  m_upper_x = mx<double>(aX,m_upper_x);
285  } else {
286  m_lower_x = aX;
287  m_upper_x = aX;
288  }
289  if(m_ys.size()) {
290  m_lower_y = mn<double>(aY,m_lower_y);
291  m_upper_y = mx<double>(aY,m_upper_y);
292  } else {
293  m_lower_y = aY;
294  m_upper_y = aY;
295  }
296  m_xs.push_back(aX);
297  m_ys.push_back(aY);
298  m_ws.push_back(aW);
299  m_Sw += aW;
300  double xw = aX * aW;
301  m_Sxw += xw;
302  m_Sx2w += aX * xw;
303  double yw = aY * aW;
304  m_Syw += yw;
305  m_Sy2w += aY * yw;
306  return true;
307  }
308 }
309 
310 inline
311 bool c2d::convert(const std::vector<double>& a_edges_x,const std::vector<double>& a_edges_y) {
312  if(m_histo) return true;
314  a_edges_x,a_edges_y);
315  if(!m_histo) return false;
316  bool status = fill_histogram(*m_histo);
317  clear();
318  return status;
319 }
320 
321 inline
323  unsigned int aCnvXnumber,double aCnvXmin,double aCnvXmax
324 ,unsigned int aCnvYnumber,double aCnvYmin,double aCnvYmax
325 ){
326  m_cnv_x_num = aCnvXnumber;
327  m_cnv_x_min = aCnvXmin;
328  m_cnv_x_max = aCnvXmax;
329  m_cnv_y_num = aCnvYnumber;
330  m_cnv_y_min = aCnvYmin;
331  m_cnv_y_max = aCnvYmax;
332  return true;
333 }
334 
335 inline
336 const h2d& c2d::histogram() const {
337  if(!m_histo) const_cast<c2d&>(*this).convert_to_histogram();
338  return *m_histo;
339 }
340 
341 inline
342 double c2d::rms_x() const {
343  double rms = 0; //FIXME nan.
344  if(m_histo) {
345  rms = m_histo->rms_x();
346  } else {
347  if(m_Sw==0) {
348  } else {
349  double mean = m_Sxw / m_Sw;
350  rms = ::sqrt(::fabs( (m_Sx2w / m_Sw) - mean * mean));
351  }
352  }
353  return rms;
354 }
355 inline
356 double c2d::rms_y() const {
357  double rms = 0; //FIXME nan.
358  if(m_histo) {
359  rms = m_histo->rms_y();
360  } else {
361  if(m_Sw==0) {
362  } else {
363  double mean = m_Syw / m_Sw;
364  rms = ::sqrt(::fabs( (m_Sy2w / m_Sw) - mean * mean));
365  }
366  }
367  return rms;
368 }
369 
370 }}
371 
372 #endif
tools::histo::c2d::entries
unsigned int entries() const
Definition: c2d:26
h2d
tools::histo::c2d::m_ys
std::vector< double > m_ys
Definition: c2d:130
tools::histo::b2::rms_x
TC rms_x() const
Definition: b2:37
tools::histo::c2d::m_Sx2w
double m_Sx2w
Definition: c2d:136
tools::histo::c2d::m_Syw
double m_Syw
Definition: c2d:137
tools::histo::c2d::m_histo
histo::h2d * m_histo
Definition: c2d:146
tools::histo::c2d::m_cnv_x_min
double m_cnv_x_min
Definition: c2d:141
tools::histo::base_cloud::operator=
base_cloud & operator=(const base_cloud &a_from)
Definition: base_cloud:48
tools::histo::c2d::c2d
c2d(const c2d &a_from)
Definition: c2d:68
tools::histo::c2d::set_title
bool set_title(const std::string &)
Definition: c2d:243
tools::histo::base_cloud::title
const std::string & title() const
Definition: base_cloud:56
tools::histo::c2d::scale
bool scale(double)
Definition: c2d:250
tools::histo::c2d::lower_edge_x
double lower_edge_x() const
Definition: c2d:34
tools::histo::c2d::reset
bool reset()
Definition: c2d:266
tools::histo::c2d::upper_edge_x
double upper_edge_x() const
Definition: c2d:36
tools::histo::h2::scale
bool scale(TW a_factor)
Definition: h2:32
tools::histo::c2d::m_cnv_x_num
unsigned int m_cnv_x_num
Definition: c2d:140
tools::histo::h2::fill
bool fill(TC aX, TC aY, TW aWeight=1)
Definition: h2:42
tools::histo::c2d::rms_x
double rms_x() const
Definition: c2d:342
tools::histo::c2d::fill
bool fill(double, double, double=1)
Definition: c2d:274
tools::histo::c2d::m_lower_y
double m_lower_y
Definition: c2d:133
tools::histo::c2d::m_cnv_y_min
double m_cnv_y_min
Definition: c2d:144
tools::histo::base_cloud::m_Sw
double m_Sw
Definition: base_cloud:64
tools::histo::b2::mean_x
TC mean_x() const
Definition: b2:27
tools::histo::c2d
Definition: c2d:16
tools::histo::axis::upper_edge
TC upper_edge() const
Definition: axis:27
tools::histo::c2d::m_upper_y
double m_upper_y
Definition: c2d:134
tools::histo::h2d
Definition: h2d:12
tools::histo::base_cloud::m_limit
int m_limit
Definition: base_cloud:63
tools::histo::b2::mean_y
TC mean_y() const
Definition: b2:32
tools::histo::c2d::mean_y
double mean_y() const
Definition: c2d:42
tools::histo::base_cloud::UNLIMITED
static int UNLIMITED()
Definition: base_cloud:59
tools::histo::base_cloud::m_title
std::string m_title
Definition: base_cloud:62
tools::histo::c2d::clear
void clear()
Definition: c2d:197
tools::histo::c2d::weight
double weight(unsigned int a_index) const
Definition: c2d:40
tools::histo::base_cloud::m_ws
std::vector< double > m_ws
Definition: base_cloud:65
tools::histo::c2d::lowerEdgeY
double lowerEdgeY() const
Definition: c2d:121
tools::histo::c2d::rms_y
double rms_y() const
Definition: c2d:356
tools::histo::c2d::sum_of_weights
double sum_of_weights() const
Definition: c2d:28
tools::histo::c2d::lowerEdgeX
double lowerEdgeX() const
Definition: c2d:120
tools::histo::c2d::value_x
double value_x(unsigned int a_index) const
Definition: c2d:38
tools::histo::c2d::set_histogram
bool set_histogram(h2d *a_histo)
Definition: c2d:58
tools::histo::c2d::m_cnv_x_max
double m_cnv_x_max
Definition: c2d:142
tools::histo::c2d::m_xs
std::vector< double > m_xs
Definition: c2d:129
tools::histo::c2d::convert
bool convert(unsigned int, double, double, unsigned int, double, double)
Definition: c2d:213
tools::histo::b2::axis_y
const axis_t & axis_y() const
Definition: b2:148
tools::histo::base_histo::all_entries
TN all_entries() const
Definition: base_histo:95
tools::histo::c2d::m_cnv_y_num
unsigned int m_cnv_y_num
Definition: c2d:143
tools::histo::c2d::is_converted
bool is_converted() const
Definition: c2d:30
tools::histo::c2d::mean_x
double mean_x() const
Definition: c2d:41
tools::histo::c2d::operator=
c2d & operator=(const c2d &a_from)
Definition: c2d:93
tools::histo::c2d::m_upper_x
double m_upper_x
Definition: c2d:132
tools::histo::c2d::lower_edge_y
double lower_edge_y() const
Definition: c2d:35
tools::histo::base_histo::set_title
bool set_title(const std::string &a_title)
Definition: base_histo:88
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::histo::c2d::set_conversion_parameters
bool set_conversion_parameters(unsigned int, double, double, unsigned int, double, double)
Definition: c2d:322
tools::histo::c2d::upperEdgeX
double upperEdgeX() const
Definition: c2d:122
tools::histo::c2d::m_Sxw
double m_Sxw
Definition: c2d:135
tools::histo::c2d::m_lower_x
double m_lower_x
Definition: c2d:131
tools::histo::c2d::fill_histogram
bool fill_histogram(HISTO &a_histo) const
Definition: c2d:49
tools::histo::c2d::upper_edge_y
double upper_edge_y() const
Definition: c2d:37
tools::histo::c2d::c2d
c2d()
Definition: c2d:155
tools::histo::c2d::~c2d
virtual ~c2d()
Definition: c2d:66
tools::histo::c2d::m_cnv_y_max
double m_cnv_y_max
Definition: c2d:145
tools::histo::c2d::m_Sy2w
double m_Sy2w
Definition: c2d:138
tools::histo::c2d::dimension
unsigned int dimension() const
Definition: c2d:24
tools::histo::c2d::fillHistogram
bool fillHistogram(HISTO &a_histo) const
Definition: c2d:125
base_cloud
tools::histo::base_histo::sum_bin_heights
TH sum_bin_heights() const
Definition: base_histo:114
tools::histo::c2d::histogram
const histo::h2d & histogram() const
Definition: c2d:336
tools::histo::base_cloud
Definition: base_cloud:17
tools::histo::c2d::s_class
static const std::string & s_class()
Definition: c2d:18
tools::histo::c2d::value_y
double value_y(unsigned int a_index) const
Definition: c2d:39
tools::histo::axis::lower_edge
TC lower_edge() const
Definition: axis:26
tools::histo::c2d::convert_to_histogram
bool convert_to_histogram()
Definition: c2d:228
tools::histo::b2::axis_x
const axis_t & axis_x() const
Definition: b2:147
tools::histo::base_cloud::BINS
static unsigned int BINS()
Definition: base_cloud:60
tools::histo::c2d::upperEdgeY
double upperEdgeY() const
Definition: c2d:123
tools::histo::b2::rms_y
TC rms_y() const
Definition: b2:43