CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Histogram.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  * Copyright (C) 2010 by Federico Marulli and Alfonso Veropalumbo *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful,*
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  *******************************************************************/
20 
34 #include "Histogram.h"
35 
36 using namespace std;
37 
38 using namespace cbl;
39 using namespace glob;
40 
41 
42 // ============================================================================
43 
44 
45 cbl::glob::Histogram1D::Histogram1D (const std::vector<double> var, const std::vector<double> weight, const size_t nbins, const double minVar, const double maxVar, const double shift, const BinType bin_type)
46 {
47  double _minVar = (minVar>par::defaultDouble) ? minVar : Min(var)*0.9999;
48  double _maxVar = (maxVar>par::defaultDouble) ? maxVar : Max(var)*1.0001;
49 
50  set(nbins, _minVar, _maxVar, shift, bin_type);
51  put(var, weight);
52 }
53 
54 
55 // ============================================================================
56 
57 
58 void cbl::glob::Histogram1D::set (const size_t nbins, const double minVar, const double maxVar, const double shift, const BinType bin_type, const std::vector<double> vec_edges)
59 {
60  if (shift>1 || shift<0)
61  ErrorCBL("shift must be 0<shift<1!", "set", "Histogram.cpp");
62 
63  m_nbins = nbins;
64  m_minVar = minVar;
65  m_maxVar = maxVar;
66  m_shift = shift;
67 
68  m_bins.resize(m_nbins, 0);
69  m_edges.resize(m_nbins+1, 0);
70  m_counts.resize(m_nbins, 0.);
71  m_var.resize(m_nbins, 0.);
72  m_var_err.resize(m_nbins, 0.);
73 
74  shared_ptr<gsl_histogram> histo1(gsl_histogram_alloc(m_nbins), gsl_histogram_free);
75  shared_ptr<gsl_histogram> histo2(gsl_histogram_alloc(m_nbins), gsl_histogram_free);
76 
77  m_binType = bin_type;
78 
79  if (m_binType == BinType::_linear_) {
80  m_binSize = (m_maxVar-m_minVar)/m_nbins;
81  gsl_histogram_set_ranges_uniform(histo1.get(), m_minVar, m_maxVar);
82  gsl_histogram_set_ranges_uniform(histo2.get(), m_minVar, m_maxVar);
83 
84  m_edges[0] = histo1.get()->range[0];
85  for (size_t i=0; i<m_nbins; i++){
86  m_edges[i+1] = histo1.get()->range[i+1];
87  m_bins[i] = m_edges[i]+shift*m_binSize;
88  }
89 
90  }
91  else if (m_binType == BinType::_logarithmic_) {
92 
93  m_binSize = (log10(m_maxVar)-log10(m_minVar))/nbins;
94 
95  m_edges[0] = m_minVar;
96  for (size_t i=0; i<m_nbins; i++){
97  m_edges[i+1] = pow(10., log10(m_minVar)+(i+1)*m_binSize);
98  m_bins[i] = pow(10., log10(m_edges[i])+m_shift*m_binSize);
99  }
100 
101  gsl_histogram_set_ranges(histo1.get(), m_edges.data(), m_nbins+1);
102  gsl_histogram_set_ranges(histo2.get(), m_edges.data(), m_nbins+1);
103  }
104  else if (m_binType==BinType::_custom_) {
105  if (m_nbins+1!=vec_edges.size())
106  ErrorCBL("length of vec_edges != nbins+1 !", "set", "Histrogram.cpp");
107  m_edges[0] = m_minVar;
108  for (size_t i=0; i<m_nbins; i++) {
109  m_edges[i+1] = vec_edges[i+1];
110  m_bins[i] = 0.5*(vec_edges[i]+vec_edges[i+1]);
111  }
112  gsl_histogram_set_ranges(histo1.get(), m_edges.data(), m_nbins+1);
113  gsl_histogram_set_ranges(histo2.get(), m_edges.data(), m_nbins+1);
114  }
115 
116  m_histo = histo1;
117  m_histo_error = histo2;
118 }
119 
120 
121 // ============================================================================
122 
123 
124 int cbl::glob::Histogram1D::digitize (const double var)
125 {
126  gsl_set_error_handler_off();
127 
128  size_t i;
129  int res = gsl_histogram_find(m_histo.get(), var, &i);
130 
131  if (res == GSL_EDOM)
132  return -1;
133  else
134  return i;
135 }
136 
137 
138 // ============================================================================
139 
140 
141 std::vector<int> cbl::glob::Histogram1D::digitize (const std::vector<double> var)
142 {
143  vector<int> bin(var.size());
144  for (size_t i=0; i<var.size(); i++)
145  bin[i] = digitize(var[i]);
146  return bin;
147 }
148 
149 
150 // ============================================================================
151 
152 
153 void cbl::glob::Histogram1D::put (const double var, const double weight)
154 {
155  const int bin = digitize(var);
156  if (bin>-1)
157  put(bin, weight, var);
158 }
159 
160 
161 // ============================================================================
162 
163 
164 void cbl::glob::Histogram1D::put (const std::vector<double> var, const std::vector<double> weight)
165 {
166  for (size_t i=0; i<var.size(); i++)
167  put(var[i], weight[i]);
168 }
169 
170 
171 // ============================================================================
172 
173 
174 void cbl::glob::Histogram1D::put (const int bin, const double weight, const double var)
175 {
176  m_histo.get()->bin[bin] += weight;
177  m_histo_error.get()->bin[bin] += weight*weight;
178  m_counts[bin] ++;
179 
180  double Wn = m_histo.get()->bin[bin];
181  double mean_prev = m_var[bin];
182  double var_prev = m_var_err[bin];
183  m_var[bin] = (Wn==0) ? 0 : mean_prev+weight/Wn*(var-mean_prev);
184  m_var_err[bin] = (Wn==0) ? 0 : var_prev+weight*(var-mean_prev)*(var-m_var[bin]);
185 }
186 
187 
188 // ============================================================================
189 
190 
191 void cbl::glob::Histogram1D::put (const std::vector<int> bins, const std::vector<double> weight, const std::vector<double> var)
192 {
193  for (size_t i=0; i<bins.size(); i++)
194  put(bins[i], weight[i], var[i]);
195 }
196 
197 
198 // ============================================================================
199 
200 
201 double cbl::glob::Histogram1D::normalization (const int i, const HistogramType hist_type, const double fact) const
202 {
203  double norm;
204 
205  switch (hist_type) {
206 
207  case (HistogramType::_dn_dV_):
208  norm = fact*(m_edges[i+1]-m_edges[i]) ;
209  break;
210 
211  case (HistogramType::_dn_dlogV_):
212  norm = fact*(log10(m_edges[i+1])-log10(m_edges[i])) ;
213  break;
214 
215  case (HistogramType::_dn_dlnV_):
216  norm = fact*(log(m_edges[i+1])-log(m_edges[i])) ;
217  break;
218 
219  case (HistogramType::_N_V_):
220  norm = 1.;
221  break;
222 
223  case (HistogramType::_n_V_):
224  norm = fact;
225  break;
226 
227  default:
228  ErrorCBL("no such a variable in the list!", "normalization", "Histogram1D.cpp");
229  }
230 
231  return norm;
232 }
233 
234 
235 // ============================================================================
236 
237 
238 double cbl::glob::Histogram1D::operator() (const int i, const HistogramType hist_type, const double fact) const
239 {
240  return m_histo.get()->bin[i]/normalization(i, hist_type, fact);
241 
242 }
243 
244 
245 // ============================================================================
246 
247 
248 std::vector<double> cbl::glob::Histogram1D::operator() (const HistogramType hist_type, const double fact) const
249 {
250  vector<double> hist(m_nbins, 0);
251 
252  for (size_t i=0; i<m_nbins; i++)
253  hist[i] = m_histo.get()->bin[i]/normalization(i, hist_type, fact);
254 
255  return hist;
256 }
257 
258 
259 // ============================================================================
260 
261 
262 double cbl::glob::Histogram1D::error (const int i, const HistogramType hist_type, const double fact) const
263 {
264  return sqrt(m_histo_error.get()->bin[i])/normalization(i, hist_type, fact);
265 }
266 
267 
268 // ============================================================================
269 
270 
271 std::vector<double> cbl::glob::Histogram1D::error (const HistogramType hist_type, const double fact) const
272 {
273  vector<double> hist(m_nbins, 0);
274 
275  for (size_t i=0; i<m_nbins; i++)
276  hist[i] = sqrt(m_histo_error.get()->bin[i])/normalization(i, hist_type, fact);
277 
278  return hist;
279 }
280 
281 // ============================================================================
282 
283 
284 vector<double> cbl::glob::Histogram1D::error_bins () const
285 {
286  std::vector<double> vv = m_var_err;
287  for (size_t i=0; i<m_nbins; i++)
288  vv[i] = sqrt(vv[i]/m_histo.get()->bin[i]);
289  return vv;
290 }
291 
292 
293 // ============================================================================
294 
295 
296 void cbl::glob::Histogram1D::write (const string dir, const string file, const HistogramType hist_type, const double fact) const
297 {
298  string mkdir = "mkdir -p "+dir;
299  if (system(mkdir.c_str())) {}
300 
301  string output_file = dir+file;
302  ofstream fout (output_file.c_str());
303 
304  fout << "# bin_center hist hist_err weighted_counts weighted_counts_err counts counts_err lower_edge upper_edge histogram_normalization var var_err" << endl;
305 
306  for (size_t i=0; i<m_nbins; i++) {
307  fout << bin(i) << " " << this->operator()(i, hist_type, fact) << " " << this->error(i, hist_type, fact);
308  fout << " " << m_histo.get()->bin[i] << " " << sqrt(m_histo_error.get()->bin[i]);
309  fout << " " << m_counts[i] << " " << sqrt(m_counts[i]);
310  fout << " " << edge(i) << " " << edge(i+1) << " " << normalization(i, hist_type, fact);
311  fout << " " << m_var[i] << " " << sqrt(m_var_err[i]/m_histo.get()->bin[i]) << endl;
312  }
313 
314  fout.clear(); fout.close();
315 }
316 
317 
318 // ============================================================================
319 
320 
321 cbl::glob::Histogram2D::Histogram2D (const std::vector<double> var1, const std::vector<double> var2, const std::vector<double> weight, const size_t nbins1, const size_t nbins2, const double minVar1, const double maxVar1, const double minVar2, const double maxVar2, const double shift1, const double shift2, const BinType bin_type1, const BinType bin_type2)
322 {
323  double _minVar1 = (minVar1>par::defaultDouble) ? minVar1 : Min(var1)*0.9999;
324  double _maxVar1 = (maxVar1>par::defaultDouble) ? maxVar1 : Max(var1)*1.0001;
325 
326  double _minVar2 = (minVar2>par::defaultDouble) ? minVar2 : Min(var2)*0.9999;
327  double _maxVar2 = (maxVar2>par::defaultDouble) ? maxVar2 : Max(var2)*1.0001;
328 
329  set(nbins1, nbins2, _minVar1, _maxVar1, _minVar2, _maxVar2, shift1, shift2, bin_type1, bin_type2);
330  put(var1, var2, weight);
331 }
332 
333 
334 // ============================================================================
335 
336 
337 void cbl::glob::Histogram2D::set (const size_t nbins1, const size_t nbins2, const double minVar1, const double maxVar1, const double minVar2, const double maxVar2, const double shift1, const double shift2, const BinType bin_type1, const BinType bin_type2, const std::vector<double> vec_edges1, const std::vector<double> vec_edges2)
338 {
339  if (shift1>1 || shift1<0 || shift2>1 || shift2<0)
340  ErrorCBL("shift must be in the range (0,1) !", "set", "Histrogram.cpp");
341 
342  m_nbins1 = nbins1;
343  m_minVar1 = minVar1;
344  m_maxVar1 = maxVar1;
345  m_shift1 = shift1;
346  m_bins1.resize(m_nbins1, 0);
347  m_edges1.resize(m_nbins1+1, 0);
348  m_binType1 = bin_type1;
349 
350  m_nbins2 = nbins2;
351  m_minVar2 = minVar2;
352  m_maxVar2 = maxVar2;
353  m_shift2 = shift2;
354  m_bins2.resize(m_nbins2, 0);
355  m_edges2.resize(m_nbins2+1, 0);
356  m_binType2 = bin_type2;
357 
358  m_counts.resize(m_nbins1, vector<int>(m_nbins2, 0));
359  m_var1.resize(m_nbins1, vector<double>(m_nbins2, 0));
360  m_var1_err.resize(m_nbins1, vector<double>(m_nbins2, 0));
361  m_var2.resize(m_nbins1, vector<double>(m_nbins2, 0));
362  m_var2_err.resize(m_nbins1, vector<double>(m_nbins2, 0));
363 
364  shared_ptr<gsl_histogram2d> histo(gsl_histogram2d_alloc(m_nbins1, m_nbins2), gsl_histogram2d_free);
365  shared_ptr<gsl_histogram2d> histo2(gsl_histogram2d_alloc(m_nbins1, m_nbins2), gsl_histogram2d_free);
366 
367  if (m_binType1 == BinType::_linear_ && m_binType2 == BinType::_linear_) {
368  m_binSize1 = (m_maxVar1-m_minVar1)/m_nbins1;
369  m_binSize2 = (m_maxVar2-m_minVar2)/m_nbins2;
370 
371  gsl_histogram2d_set_ranges_uniform(histo.get(), m_minVar1, m_maxVar1, m_minVar2, m_minVar2);
372  gsl_histogram2d_set_ranges_uniform(histo2.get(), m_minVar1, m_maxVar1, m_minVar2, m_minVar2);
373 
374  m_edges1[0] = histo.get()->xrange[0];
375  for (size_t i=0; i<m_nbins1; i++) {
376  m_edges1[i+1] = histo.get()->xrange[i+1];
377  m_bins1[i] = m_edges1[i]+m_shift1*m_binSize1;
378  }
379 
380  m_edges2[0] = histo.get()->yrange[0];
381  for (size_t i=0; i<m_nbins2; i++) {
382  m_edges2[i+1] = histo.get()->yrange[i+1];
383  m_bins2[i] = m_edges2[i]+m_shift2*m_binSize2;
384  }
385 
386  }
387  else {
388 
389  // set ranges for variable 1
390  if ( m_binType1==BinType::_linear_) {
391  m_binSize1 = (m_maxVar1-m_minVar1)/m_nbins1;
392  m_edges1[0] = m_minVar1;
393  for (size_t i=0; i<m_nbins1; i++) {
394  m_edges1[i+1] = m_edges1[i]+m_binSize1;
395  m_bins1[i] = m_edges1[i]+m_shift1*m_binSize1;
396  }
397  }
398  else if (m_binType1==BinType::_logarithmic_) {
399  m_binSize1 = (log10(m_maxVar1)-log10(m_minVar1))/m_nbins1;
400  m_edges1[0] = m_minVar1;
401  for (size_t i=0; i<m_nbins1; i++) {
402  m_edges1[i+1] = pow(10., log10(m_edges1[i])+m_binSize1);
403  m_bins1[i] = pow(10., log10(m_edges1[i])+m_shift1*m_binSize1);
404  }
405  }
406  else if (m_binType1==BinType::_custom_) {
407  if (m_nbins1+1!=vec_edges1.size())
408  ErrorCBL("length of vec_edges1 != nbins1+1 !", "set", "Histrogram.cpp");
409  m_edges1[0] = m_minVar1;
410  for (size_t i=0; i<m_nbins1; i++) {
411  m_edges1[i+1] = vec_edges1[i+1];
412  m_bins1[i] = 0.5*(vec_edges1[i]+vec_edges1[i+1]);
413  }
414  }
415  else
416  ErrorCBL("no such bin_type!", "set", "Histogram2D.cpp");
417 
418  // set ranges for variable 2
419  if ( m_binType2 == BinType::_linear_) {
420  m_binSize2 = (m_maxVar2-m_minVar2)/m_nbins2;
421  m_edges2[0] = m_minVar2;
422  for (size_t i=0; i<m_nbins2; i++){
423  m_edges2[i+1] = m_edges2[i]+m_binSize2;
424  m_bins2[i] = m_edges2[i]+m_shift2*m_binSize2;
425  }
426  }
427  else if (m_binType2 == BinType::_logarithmic_) {
428  m_binSize2 = (log10(m_maxVar2)-log10(m_minVar2))/m_nbins2;
429  m_edges2[0] = m_minVar2;
430  for (size_t i=0; i<m_nbins2; i++) {
431  m_edges2[i+1] = pow(10., log10(m_edges2[i])+m_binSize2);
432  m_bins2[i] = pow(10., log10(m_edges2[i])+m_shift2*m_binSize2);
433  }
434  }
435  else if (m_binType2==BinType::_custom_) {
436  if (m_nbins2+1!=vec_edges2.size())
437  ErrorCBL("length of vec_edges2 != nbins2+1 !", "set", "Histrogram.cpp");
438  m_edges2[0] = m_minVar2;
439  for (size_t i=0; i<m_nbins2; i++) {
440  m_edges2[i+1] = vec_edges2[i+1];
441  m_bins2[i] = 0.5*(vec_edges2[i]+vec_edges2[i+1]);
442  }
443  }
444  else
445  ErrorCBL("the input bin_type is not allowed!", "set", "Histrogram.cpp");
446 
447  gsl_histogram2d_set_ranges(histo.get(), m_edges1.data(), m_nbins1+1, m_edges2.data(), m_nbins2+1);
448  gsl_histogram2d_set_ranges(histo2.get(), m_edges1.data(), m_nbins1+1, m_edges2.data(), m_nbins2+1);
449  }
450 
451  m_histo = histo;
452  m_histo_error = histo2;
453 
454 }
455 
456 
457 // ============================================================================
458 
459 
460 std::vector<int> cbl::glob::Histogram2D::digitize (const double var1, const double var2)
461 {
462  size_t i, j;
463  int result = gsl_histogram2d_find(m_histo.get(), var1, var2, &i, &j);
464 
465  if (result==GSL_SUCCESS)
466  return {(int)i, (int)j};
467  return {-1, -1};
468 }
469 
470 
471 // ============================================================================
472 
473 
474 std::vector<std::vector<int>> cbl::glob::Histogram2D::digitize (const std::vector<double> var1, const std::vector<double> var2)
475 {
476  vector<vector<int>> bins (var1.size(), vector<int>(2, 0));
477 
478  for (size_t i=0; i<var1.size(); i++)
479  bins[i] = digitize(var1[i], var2[i]);
480 
481  return bins;
482 }
483 
484 
485 // ============================================================================
486 
487 
488 void cbl::glob::Histogram2D::put (const double var1, const double var2, const double weight)
489 {
490  const vector<int> bins = digitize(var1, var2);
491 
492  if (bins[0]>-1 && bins[1]>-1)
493  put(bins[0], bins[1], weight, var1, var2);
494 }
495 
496 
497 // ============================================================================
498 
499 
500 void cbl::glob::Histogram2D::put (const std::vector<double> var1, const std::vector<double> var2, const std::vector<double> weight)
501 {
502  for (size_t i=0; i<var1.size(); i++)
503  put(var1[i], var2[i], weight[i]);
504 }
505 
506 
507 // ============================================================================
508 
509 
510 void cbl::glob::Histogram2D::put (const int i, const int j, const double weight, const double var1, const double var2)
511 {
512  m_histo.get()->bin[i*m_nbins2+j] += weight;
513  m_histo_error.get()->bin[i*m_nbins2+j] += weight*weight;
514  m_counts[i][j] ++;
515 
516  double Wn = m_histo.get()->bin[i*m_nbins2+j];
517  double mean_prev = m_var1[i][j];
518  double var_prev = m_var1_err[i][j];
519 
520  m_var1[i][j] = (Wn==0) ? 0 : mean_prev+weight/Wn*(var1-mean_prev);
521  m_var1_err[i][j] = (Wn==0) ? 0 : var_prev+weight*(var1-mean_prev)*(var1-m_var1[i][j]);
522 
523  mean_prev = m_var2[i][j];
524  var_prev = m_var2_err[i][j];
525 
526  m_var2[i][j] = (Wn==0) ? 0 : mean_prev+weight/Wn*(var2-mean_prev);
527  m_var2_err[i][j] = (Wn==0) ? 0 : var_prev+weight*(var2-mean_prev)*(var2-m_var2[i][j]);
528 }
529 
530 
531 // ============================================================================
532 
533 
534 void cbl::glob::Histogram2D::put (const std::vector<std::vector<int>> bins, const std::vector<double> weight, const std::vector<double> var1, const std::vector<double> var2)
535 {
536  for (size_t i=0; i<weight.size(); i++)
537  put(bins[i][0], bins[i][1], weight[i], var1[i], var2[i]);
538 }
539 
540 
541 // ============================================================================
542 
543 
544 double cbl::glob::Histogram2D::normalization (const int i, const int j, const HistogramType hist_type, const double fact) const
545 {
546  double norm;
547 
548  switch (hist_type) {
549 
550  case (HistogramType::_dn_dV_):
551  norm = fact*(m_edges1[i+1]-m_edges1[i])*(m_edges2[j+1]-m_edges2[j]) ;
552  break;
553 
554  case (HistogramType::_dn_dlogV_):
555  norm = fact*(log10(m_edges1[i+1])-log10(m_edges1[i]))*(log10(m_edges2[j+1])-log10(m_edges2[j])) ;
556  break;
557 
558  case (HistogramType::_dn_dlnV_):
559  norm = fact*(log(m_edges1[i+1])-log(m_edges1[i]))*(log(m_edges2[j+1])-log(m_edges2[j])) ;
560  break;
561 
562  case (HistogramType::_N_V_):
563  norm = 1.;
564  break;
565 
566  case (HistogramType::_n_V_):
567  norm = fact;
568  break;
569 
570  default:
571  ErrorCBL("no such a variable in the list!", "normalization", "Histogram.cpp");
572  }
573 
574  return norm;
575 }
576 
577 
578 // ============================================================================
579 
580 
581 double cbl::glob::Histogram2D::operator() (const int i, const int j, const HistogramType hist_type, const double fact) const
582 {
583  return m_histo.get()->bin[i*m_nbins2+j]/normalization(i, j, hist_type, fact);
584 }
585 
586 
587 // ============================================================================
588 
589 
590 vector<vector<double>> cbl::glob::Histogram2D::error_bins1 () const
591 {
592  std::vector<vector<double>> vv = m_var1_err;
593  for (size_t i=0; i<m_nbins1; i++)
594  for (size_t j=0; j<m_nbins2; j++)
595  vv[i][j] = (m_histo.get()->bin[i*m_nbins2+j]>0) ? sqrt(vv[i][j]/m_histo.get()->bin[i*m_nbins2+j]) : 0.;
596  return vv;
597 }
598 
599 
600 // ============================================================================
601 
602 
603 vector<vector<double>> cbl::glob::Histogram2D::error_bins2 () const
604 {
605  std::vector<vector<double>> vv = m_var2_err;
606  for (size_t i=0; i<m_nbins1; i++)
607  for (size_t j=0; j<m_nbins2; j++)
608  vv[i][j] = (m_histo.get()->bin[i*m_nbins2+j]>0) ? sqrt(vv[i][j]/m_histo.get()->bin[i*m_nbins2+j]) : 0.;
609  return vv;
610 }
611 
612 
613 // ============================================================================
614 
615 
616 double cbl::glob::Histogram2D::error (const int i, const int j, const HistogramType hist_type, const double fact) const
617 {
618  return sqrt(m_histo_error.get()->bin[i*m_nbins2+j])/normalization(i, j, hist_type, fact);
619 }
620 
621 
622 // ============================================================================
623 
624 
625 void cbl::glob::Histogram2D::write (const string dir, const string file, const HistogramType hist_type, const double fact) const
626 {
627  string mkdir = "mkdir -p "+dir;
628  if (system(mkdir.c_str())) {}
629 
630  string output_file = dir+file;
631  ofstream fout (output_file.c_str());
632 
633  fout << "# bin1_center bin2_center hist hist_err weighted_counts weighted_counts_err counts counts_err";
634  fout << "lower_edge1 upper_edge1 lower_edge2 upper_edge2 histogram_normalization";
635  fout << "var1 var1_err var2 var2_err" << endl;
636 
637  for (size_t i=0; i<m_nbins1; i++) {
638  for (size_t j=0; j<m_nbins2; j++) {
639  fout << bin(i) << " " << bin2(j) << " " << this->operator()(i, j, hist_type, fact) << " " << this->error(i, j, hist_type, fact);
640  fout << " " << m_histo.get()->bin[i*m_nbins2+j] << " " << sqrt(m_histo_error.get()->bin[i*m_nbins2+j]);
641  fout << " " << m_counts[i][j] << " " << sqrt(m_counts[i][j]);
642  fout << " " << edge1(i) << " " << edge1(i+1) << " " << edge2(j) << " " << edge2(j+1) << " " << normalization(i, j, hist_type, fact) << endl;
643  fout << " " << m_var1[i][j] << " " << sqrt(m_var1_err[i][j]/m_histo.get()->bin[i*m_nbins2+j]) << " " << m_var2[i][j] << " " << sqrt(m_var2_err[i][j]/m_histo.get()->bin[i*m_nbins2+j]) << endl;
644  }
645  }
646 
647  fout.clear(); fout.close();
648 }
Class used to handle binned variables.
double normalization(const int i, const int j, const HistogramType hist_type, const double fact=1.) const override
return the bin normalization
Definition: Histogram.cpp:544
Histogram2D()=default
default constructor
std::vector< std::vector< double > > error_bins1() const
return the averaged bins
Definition: Histogram.cpp:590
std::vector< int > digitize(const double var1, const double var2) override
get the histogram index
Definition: Histogram.cpp:460
std::vector< std::vector< double > > error_bins2() const
return the averaged bins
Definition: Histogram.cpp:603
void write(const std::string dir, const std::string file, const HistogramType hist_type, const double fact=1.) const override
write the histogram
Definition: Histogram.cpp:625
void set(const size_t nbins1, const size_t nbins2, const double minVar1=par::defaultDouble, const double maxVar1=par::defaultDouble, const double minVar2=par::defaultDouble, const double maxVar2=par::defaultDouble, const double shift1=0.5, const double shift2=0.5, const BinType bin_type1=BinType::_linear_, const BinType bin_type2=BinType::_linear_, const std::vector< double > vec_edges1={}, const std::vector< double > vec_edges2={}) override
set the histogram variables
Definition: Histogram.cpp:337
void put(const double var1, const double var2, const double weight) override
bin the data
Definition: Histogram.cpp:488
double error(const int i, const int j, const HistogramType hist_type, const double fact=1.) const override
return the poisson error of the histogram at (i,j)
Definition: Histogram.cpp:616
double operator()(const int i, const int j, const HistogramType hist_type, const double fact=1.) const override
return the histogram at (i,j)
Definition: Histogram.cpp:581
virtual double normalization(const int i, const int j, const HistogramType hist_type, const double fact=1.) const
return the bin normalization
Definition: Histogram.h:711
virtual double error(const int i, const int j, const HistogramType hist_type, const double fact=1.) const
return the error of the histogram at (i,j)
Definition: Histogram.h:775
virtual void put(const double var1, const double var2, const double weight)
bin the data
Definition: Histogram.h:267
virtual std::vector< int > digitize(const double var1, const double var2)
get the histogram index
Definition: Histogram.h:244
virtual void set(const size_t nbins1, const size_t nbins2, const double minVar1=par::defaultDouble, const double maxVar1=par::defaultDouble, const double minVar2=par::defaultDouble, const double maxVar2=par::defaultDouble, const double shift1=0.5, const double shift2=0.5, const BinType bin_type1=BinType::_linear_, const BinType bin_type2=BinType::_linear_, const std::vector< double > vec_edges1={}, const std::vector< double > vec_edges2={})
set the histogram variables
Definition: Histogram.h:233
static const double defaultDouble
default double value
Definition: Constants.h:348
HistogramType
the histogram type
Definition: Histogram.h:49
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
T Min(const std::vector< T > vect)
minimum element of a std::vector
Definition: Kernel.h:1324
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780
T Max(const std::vector< T > vect)
maximum element of a std::vector
Definition: Kernel.h:1336
BinType
the binning type
Definition: Kernel.h:505