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

This example shows how to construct a catalogue of extragalactic objects

// ===================================================================
// Example code: how to construct a catalogue of extragalactic objects
// ===================================================================
#include "Catalogue.h"
int main () {
try {
std::string file_catalogue = "cat.dat";
// -----------------------------------------------------------------------------------------
// ------- method I : construct a galaxy catalogue directly by reading an input file -------
// -----------------------------------------------------------------------------------------
// standard
std::cout << "The coordinates of the first galaxy in the catalogue are: " <<
catalogue1.xx(0) << ", " << catalogue1.yy(0) << ", " << catalogue1.zz(0) << std::endl;
// with vector syntax
std::cout << "The coordinates of the first galaxy in the catalogue are: " <<
catalogue1[0]->xx() << ", " << catalogue1[0]->yy() << ", " << catalogue1[0]->zz() << std::endl;
// -----------------------------------------------------------------------------------------------------------------------
// ------- method II : construct a galaxy catalogue directly from a file, choosing which quantity has to be read ---------
// -----------------------------------------------------------------------------------------------------------------------
// std::vector containing the quantities to be read
std::vector<cbl::catalogue::Var> attribute = {cbl::catalogue::Var::_X_, cbl::catalogue::Var::_Y_, cbl::catalogue::Var::_Z_};
// std::vector containing the columns corresponding to each quantity
std::vector<int> column = {1, 2, 3};
// -----------------------------------------------------------------------------------------------------------------
// --------- method III : construct a galaxy catalogue using std::vectors to specify the galaxy properties ---------
// -----------------------------------------------------------------------------------------------------------------
std::ifstream fin; fin.open(file_catalogue.c_str()); cbl::checkIO(fin, file_catalogue);
double X, Y, Z;
std::vector<double> x, y, z;
while (fin >> X >> Y >> Z) {
x.emplace_back(X);
y.emplace_back(Y);
z.emplace_back(Z);
}
fin.clear();
// ------------------------------------------------------------------------------------------------------
// -------- method IV : construct a std::vector of galaxies and add them into an empty catalogue --------
// ------------------------------------------------------------------------------------------------------
std::vector<std::shared_ptr<cbl::catalogue::Object>> object;
fin.seekg(std::ios::beg);
while (fin >> X >> Y >> Z) {
cbl::comovingCoordinates coord = {X, Y, Z};
auto galaxy = std::make_shared<cbl::catalogue::Galaxy>(coord);
object.emplace_back(galaxy);
}
fin.clear(); fin.close();
cbl::catalogue::Catalogue catalogue4; catalogue4.add_objects(object);
// --------------------------------------------------------------
// -------- method V : subsample a catalogue with a mask --------
// --------------------------------------------------------------
cbl::catalogue::mask_function mask = [&] (const std::shared_ptr<cbl::catalogue::Object> obj)
{
if (obj->xx()>-70 && obj->xx()<10)
return true;
return false;
};
cbl::catalogue::Catalogue catalogue5 = catalogue1.sub_catalogue(mask);
std::cout << "The number of galaxy in catalogue1 is " << catalogue1.nObjects() << std::endl;
std::cout << "The number of galaxy in catalogue2 is " << catalogue2.nObjects() << std::endl;
std::cout << "The number of galaxy in catalogue3 is " << catalogue3.nObjects() << std::endl;
std::cout << "The number of galaxy in catalogue4 is " << catalogue4.nObjects() << std::endl;
std::cout << "The number of galaxy in catalogue5 is " << catalogue5.nObjects() << std::endl;
}
catch(cbl::glob::Exception &exc) { std::cerr << exc.what() << std::endl; exit(1); }
return 0;
}
The class Catalogue
int main()
main function to create the logo of the CosmoBolognaLib
Definition: Logo.cpp:41
The class Catalogue.
Definition: Catalogue.h:654
void add_objects(std::vector< std::shared_ptr< Object > > sample)
add some objects to the catalogue
Definition: Catalogue.h:2654
Catalogue sub_catalogue(const Var var_name, const double down, const double up, const bool excl=false) const
create a sub-catalogue
Definition: Catalogue.cpp:1890
size_t nObjects() const
get the number of objects of the catalogue
Definition: Catalogue.h:2264
The class Exception.
Definition: Exception.h:111
const char * what() const noexcept override
the error description
Definition: Exception.h:203
std::function< bool(const std::shared_ptr< Object > obj)> mask_function
Definition of a new type to manage mask function.
Definition: Catalogue.h:266
@ _Z_
coordinate z
@ _Y_
coordinate y
@ _X_
coordinate x
@ _comoving_
comoving coordinates (x, y, z)
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160