CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
data1D.cpp

this example shows how to construct an object of class Data1D, used to handle 1D datasets of any type

// ================================================================================================
// Example code: how to construct an object of class Data1D, used to handle 1D datasets of any type
// ================================================================================================
#include "Data1D.h"
using namespace std;
int main () {
try {
// the input file
const string file = "data.dat";
// header lines to skip
const int n_lines_header = 2;
// --- method I: providing just the name of the input file ---
// with default columns
const cbl::data::Data1D data1(file, n_lines_header);
data1.Print(); cout << endl;
// specifying that the x column is the second one (the y and error
// columns will be the third and fourth ones, by default)
const cbl::data::Data1D data2(file, n_lines_header, {2});
data2.Print(); cout << endl;
// specifying all the columns (the 4th argument indicates
// the bin edges column, not present in the file considered here)
const cbl::data::Data1D data3(file, n_lines_header, {2}, {3}, {4});
data3.Print(); cout << endl;
// reading two datasets (that will be added one after the other in
// a single dataset)
const cbl::data::Data1D data4(file, n_lines_header, {2}, {3, 5}, {4, 6});
data4.Print(); cout << endl;
// --- method II: providing the x, y and error vectors ---
std::ifstream fin(file.c_str()); cbl::checkIO(fin, file);
double X, Y, ERROR, N;
std::vector<double> x, y, error;
string line;
for (int i=0; i<n_lines_header; ++i)
getline(fin, line);
while (fin >> X >> Y >> ERROR >> N >> N >> N) {
x.emplace_back(X);
y.emplace_back(Y);
error.emplace_back(ERROR);
}
fin.clear();
const cbl::data::Data1D data5(x, y, error);
data5.Print(); cout << endl;
}
catch(cbl::glob::Exception &exc) { cerr << exc.what() << endl; exit(1); }
return 0;
}
The class Data1D.
int main()
main function to create the logo of the CosmoBolognaLib
Definition: Logo.cpp:41
The class Data1D.
Definition: Data1D.h:51
virtual void Print(const int precision=4) const override
print the data on screen
Definition: Data1D.cpp:154
The class Exception.
Definition: Exception.h:111
const char * what() const noexcept override
the error description
Definition: Exception.h:203
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160