CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Data1D.h
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2014 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 
33 #ifndef __DATA1D__
34 #define __DATA1D__
35 
36 #include "Data.h"
37 
38 namespace cbl {
39 
40  namespace data {
41 
50  class Data1D : public Data
51  {
52 
53  protected:
54 
59 
61  std::vector<double> m_x;
62 
64 
66  int m_xsize;
67 
68  public:
69 
74 
78  Data1D ()
79  : Data(DataType::_1D_) {}
80 
117  Data1D (const std::string input_file, const int skip_nlines=0, const std::vector<int> column={1}, const std::vector<int> column_data={}, const std::vector<int> column_errors={}, const std::vector<int> column_edges={})
119  { read(input_file, skip_nlines, column, column_data, column_errors, column_edges); }
120 
133  Data1D (const std::vector<double> x, const std::vector<double> data, const double val=-1, const std::vector<double> bin_edges_x={})
135  { (void)val; set_xx(x); if (bin_edges_x.size()>0) set_edges_xx(bin_edges_x); }
136 
152  Data1D (const std::vector<double> x, const std::vector<double> data, const std::vector<double> error, const double val=-1, const std::vector<double> bin_edges_x={})
154  { (void)val; set_xx(x); if (bin_edges_x.size()>0) set_edges_xx(bin_edges_x); }
155 
171  Data1D (const std::vector<double> x, const std::vector<double> data, const std::vector<std::vector<double>> covariance, const double val=-1, const std::vector<double> bin_edges_x={})
173  { (void)val; set_xx(x); if (bin_edges_x.size()>0) set_edges_xx(bin_edges_x); }
174 
178  virtual ~Data1D () = default;
179 
185  std::shared_ptr<Data> as_factory ();
186 
188 
193 
198  int xsize () const
199  { return m_xsize; }
200 
206  double xx (const int i) const override
207  { return m_x[i]; }
208 
213  std::vector<double> xx () const override
214  { return m_x; }
215 
225  std::vector<std::vector<double>> IndipendentVariable (const int i=-1, const int j=-1) const override
226  { (void)i; (void)j; return {m_x}; }
227 
232  void get_data (std::vector<double> &data) const override
233  { data = m_data; }
234 
239  void get_error (std::vector<double> &error) const override
240  { error = m_error; }
241 
243 
244 
249 
254  void set_xx (const std::vector<double> x);
255 
260  void set_edges_xx (const std::vector<double> edges) override { checkDim(edges, ndata()+1, "edges"); m_edges_xx = edges; }
261 
263 
264 
269 
306  void read (const std::string input_file, const int skip_nlines=0, const std::vector<int> column={1}, const std::vector<int> column_data={}, const std::vector<int> column_errors={}, const std::vector<int> column_edges={}) override;
307 
313  virtual void Print (const int precision=4) const override;
314 
331  void write (const std::string dir, const std::string file, const std::string header, const int prec=4, const int ww=8, const int rank=0) const override;
332 
342  void write_covariance (const std::string dir, const std::string file, const int precision=10) const override;
343 
345 
346 
352 
359  std::shared_ptr<Data> cut (const double xmin, const double xmax) const override;
360 
366  std::shared_ptr<Data> cut (const std::vector<bool> mask) const;
367 
369 
370  };
371 
372  }
373 }
374 
375 #endif
The class Data.
The class Data1D.
Definition: Data1D.h:51
void get_data(std::vector< double > &data) const override
get data for Data1D
Definition: Data1D.h:232
Data1D(const std::string input_file, const int skip_nlines=0, const std::vector< int > column={1}, const std::vector< int > column_data={}, const std::vector< int > column_errors={}, const std::vector< int > column_edges={})
constructor which reads the data from file
Definition: Data1D.h:117
std::vector< double > xx() const override
get the x std::vector
Definition: Data1D.h:213
int xsize() const
get the number of points along x
Definition: Data1D.h:198
double xx(const int i) const override
get the value of x at the i-th bin
Definition: Data1D.h:206
std::shared_ptr< Data > cut(const double xmin, const double xmax) const override
cut the data
Definition: Data1D.cpp:246
Data1D(const std::vector< double > x, const std::vector< double > data, const std::vector< std::vector< double >> covariance, const double val=-1, const std::vector< double > bin_edges_x={})
constructor which gets both the data and the covariance matrix from input vectors
Definition: Data1D.h:171
std::vector< std::vector< double > > IndipendentVariable(const int i=-1, const int j=-1) const override
get the independet variable, to be used in model computation
Definition: Data1D.h:225
void write(const std::string dir, const std::string file, const std::string header, const int prec=4, const int ww=8, const int rank=0) const override
write the data
Definition: Data1D.cpp:165
Data1D(const std::vector< double > x, const std::vector< double > data, const double val=-1, const std::vector< double > bin_edges_x={})
constructor which gets the data from input vectors
Definition: Data1D.h:133
void get_error(std::vector< double > &error) const override
get standard deviation for Data1D
Definition: Data1D.h:239
std::shared_ptr< Data > as_factory()
static factory used to construct objects of class Data1D
Definition: Data1D.cpp:260
virtual void Print(const int precision=4) const override
print the data on screen
Definition: Data1D.cpp:154
std::vector< double > m_x
ordered x axis points
Definition: Data1D.h:61
int m_xsize
number of points along x
Definition: Data1D.h:66
void write_covariance(const std::string dir, const std::string file, const int precision=10) const override
write the covariance
Definition: Data1D.cpp:190
virtual ~Data1D()=default
default destructor
void read(const std::string input_file, const int skip_nlines=0, const std::vector< int > column={1}, const std::vector< int > column_data={}, const std::vector< int > column_errors={}, const std::vector< int > column_edges={}) override
read the data
Definition: Data1D.cpp:56
void set_xx(const std::vector< double > x)
set interval variable m_x
Definition: Data1D.cpp:45
void set_edges_xx(const std::vector< double > edges) override
set interval variable m_edges_xx
Definition: Data1D.h:260
Data1D(const std::vector< double > x, const std::vector< double > data, const std::vector< double > error, const double val=-1, const std::vector< double > bin_edges_x={})
constructor which gets both the data and the errors from input vectors
Definition: Data1D.h:152
Data1D()
default constructor
Definition: Data1D.h:78
The class Data.
Definition: Data.h:89
virtual std::vector< double > data() const
get data
Definition: Data.h:376
std::vector< double > m_error
standard deviations
Definition: Data.h:103
std::vector< double > m_data
data values
Definition: Data.h:100
Data()=default
default constructor
std::vector< double > m_edges_xx
bin edges for the x variable
Definition: Data.h:106
virtual std::vector< double > error() const
get standard deviation
Definition: Data.h:389
virtual std::vector< std::vector< double > > covariance() const
get the m_covariance vector
Definition: Data.h:403
virtual int ndata() const
the total number of data
Definition: Data.h:363
DataType
the data type
Definition: Data.h:53
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void checkDim(const std::vector< T > vect, const int val, const std::string vector, bool equal=true)
check if the dimension of a std::vector is equal/lower than an input value
Definition: Kernel.h:1532