56 void cbl::data::Data1D::read (
const string input_file,
const int skip_nlines,
const vector<int> column,
const vector<int> column_data,
const vector<int> column_errors,
const vector<int> column_edges)
58 if (column_data.size()!=column_errors.size())
ErrorCBL(
"the sizes of column_data and columt_errors must be equal!",
"read",
"Data1D.cpp");
61 int column_data_default = column[0]+1;
64 int column_error_default = column[0]+2;
67 int column_edges_default = column[0]+3;
69 ifstream fin(input_file.c_str());
checkIO(fin, input_file);
76 const int cl_max = (column_data.size()<2) ? 1 : column_data.size();
77 for (
int cl=0; cl<cl_max; ++cl) {
81 for (
int i=0; i<skip_nlines; ++i)
86 while (getline(fin, line)) {
88 stringstream ss(line);
89 vector<double> num;
double NUM;
90 while (ss>>NUM) num.emplace_back(NUM);
92 if (num.size()<2)
ErrorCBL(
"there are less than 2 columns in the input file!",
"read",
"Data1D.cpp");
96 size_t ind_x = max(0, column[0]-1);
97 checkDim(num, ind_x+1,
"num",
false);
98 m_x.emplace_back(num[ind_x]);
102 const size_t ind_data = (column_data.size()==0) ? column_data_default-1 : column_data[cl]-1;
103 checkDim(num, ind_data+1,
"num",
false);
104 m_data.emplace_back(num[ind_data]);
110 const size_t ind_error = (column_errors.size()==0) ? column_error_default-1 : column_errors[cl]-1;
111 if (num.size()<ind_error+1 && column_errors.size()>1)
112 WarningMsgCBL(
"the errors cannot be retrieved from the provided input file, and will be set to 1",
"read",
"Data1D.cpp");
113 m_error.emplace_back((num.size()<ind_error+1) ? 1 : num[ind_error]);
117 const size_t ind_edges = (column_edges.size()==0) ? column_edges_default-1 : column_edges[cl]-1;
118 if (num.size()>ind_edges+1) {
119 m_edges_xx.emplace_back(num[ind_edges]);
120 last_bin_edge = num[ind_edges+1];
127 m_edges_xx.emplace_back(last_bin_edge);
130 fin.clear(); fin.seekg(ios::beg);
132 column_data_default += 2;
133 column_error_default += 2;
136 fin.clear(); fin.close();
141 m_xsize = m_data.size();
142 m_ndata = m_data.size();
144 m_covariance.resize(m_ndata, vector<double>(m_ndata, 0));
145 for (
int i=0; i<m_ndata; i++)
146 m_covariance[i][i] = pow(m_error[i], 2);
156 for (
size_t i=0; i<m_x.size(); i++)
157 coutCBL << std::fixed << setprecision(precision) << setw(8) << right << m_x[i]
158 <<
" " << std::fixed << setprecision(precision) << setw(8) << right << m_data[i]
159 <<
" " << std::fixed << setprecision(precision) << setw(8) << right << m_error[i] << endl;
165 void cbl::data::Data1D::write (
const string dir,
const string file,
const string header,
const int prec,
const int ww,
int rank)
const
169 string file_out = dir+file;
170 ofstream fout(file_out.c_str());
checkIO(fout, file_out);
173 fout <<
"### " << header <<
" ###" << endl;
175 const int bp = std::cout.precision();
177 for (
size_t i=0; i<m_x.size(); i++) {
178 cbl::Print(m_x[i], prec, ww,
"",
" ",
false, fout);
179 cbl::Print(m_data[i], prec, ww,
"",
" ",
false, fout);
180 cbl::Print(m_error[i], prec, ww,
"",
"\n",
false, fout);
183 fout.close(); cout << endl;
coutCBL <<
"I wrote the file: " << file_out << endl;
192 checkDim(m_covariance, m_ndata, m_ndata,
"covariance",
false);
194 string file_out = dir+file;
195 ofstream fout(file_out.c_str());
checkIO(fout, file_out);
197 fout <<
"### [1] r1 # [2] r2 # [3] covariance # [4] correlation # [5] index1 # [6] index2 ### " << endl;
199 for (
int i=0; i<m_ndata; ++i)
200 for (
int j=0; j<m_ndata; ++j)
201 fout << setiosflags(ios::fixed) << setprecision(precision) << setw(15) << right << m_x[i]
202 <<
" " << setiosflags(ios::fixed) << setprecision(precision) << setw(15) << right << m_x[j]
203 <<
" " << setiosflags(ios::fixed) << setprecision(precision) << setw(15) << right << m_covariance[i][j]
204 <<
" " << setiosflags(ios::fixed) << setprecision(precision) << setw(15) << right << m_covariance[i][j]/sqrt(m_covariance[i][i]*m_covariance[j][j])
205 <<
" " << setiosflags(ios::fixed) << setprecision(precision) << setw(5) << right << i
206 <<
" " << setiosflags(ios::fixed) << setprecision(precision) << setw(5) << right << j << endl;
208 fout.close(); cout << endl;
coutCBL <<
"I wrote the file: " << file_out << endl;
219 vector<double> xx, edges_xx;
221 for (
size_t i=0; i<mask.size(); i++){
223 xx.push_back(m_x[i]);
224 if (m_edges_xx.size() > 0)
225 edges_xx.push_back(m_edges_xx[i]);
229 if (m_edges_xx.size() > 0)
230 edges_xx.push_back(m_edges_xx[last_index+1]);
232 vector<double> data, error;
233 vector<vector<double>> covariance;
234 Data::cut(mask, data, error, covariance);
236 shared_ptr<Data> dd = make_shared<Data1D>(
Data1D(xx, data, covariance, -1, edges_xx));
248 vector<bool> mask(m_ndata,
true);
249 for (
int i=0; i<m_ndata; i++)
250 if ((m_x[i] < xmin) || (m_x[i]>xmax))
262 shared_ptr<Data> dd = make_shared<Data1D>(
Data1D(m_x, m_data, m_covariance));
#define coutCBL
CBL print message.
std::shared_ptr< Data > cut(const double xmin, const double xmax) const override
cut the data
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
std::shared_ptr< Data > as_factory()
static factory used to construct objects of class Data1D
virtual void Print(const int precision=4) const override
print the data on screen
void write_covariance(const std::string dir, const std::string file, const int precision=10) const override
write the covariance
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
void set_xx(const std::vector< double > x)
set interval variable m_x
static const std::string defaultString
default std::string value
static const double defaultDouble
default double value
The global namespace of the CosmoBolognaLib
void Print(const T value, const int prec, const int ww, const std::string header="", const std::string end="\n", const bool use_coutCBL=true, std::ostream &stream=std::cout, const std::string colour=cbl::par::col_default)
function to print values with a proper homegenised format
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
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
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
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message