Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few TRestTools upgrades motivated by TRestAxionSolarFlux implementations #184

Merged
merged 8 commits into from
May 9, 2022
2 changes: 2 additions & 0 deletions source/framework/core/src/TRestMetadata.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ TRestMetadata::~TRestMetadata() {
///
Int_t TRestMetadata::LoadConfigFromFile(string cfgFileName, string sectionName) {
fConfigFileName = cfgFileName;
if (!TRestTools::fileExists(fConfigFileName)) fConfigFileName = SearchFile(fConfigFileName);

if (TRestTools::fileExists(fConfigFileName)) {
if (sectionName == "") {
sectionName = this->ClassName();
Expand Down
7 changes: 7 additions & 0 deletions source/framework/tools/inc/TRestTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ class TRestTools {
static int ReadASCIITable(std::string fName, std::vector<std::vector<Float_t>>& data,
Int_t skipLines = 0);

template <typename T>
static void TransposeTable(std::vector<std::vector<T>>& data);

template <typename T>
static int ReadBinaryTable(std::string fName, std::vector<std::vector<T>>& data, Int_t columns = -1);

static Bool_t IsBinaryFile(std::string fname);

static std::string GetFileNameExtension(std::string fullname);
static std::string GetFileNameRoot(std::string fullname);

static int GetBinaryFileColumns(std::string fname);

Expand All @@ -71,6 +75,9 @@ class TRestTools {
template <typename T>
static T GetLowestIncreaseFromTable(std::vector<std::vector<T>> data, Int_t column);

template <typename T>
static T GetIntegralFromTable(const std::vector<std::vector<T>>& data);

template <typename T>
static int PrintTable(std::vector<std::vector<T>> data, Int_t start = 0, Int_t end = 0);

Expand Down
67 changes: 64 additions & 3 deletions source/framework/tools/src/TRestTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,14 @@ Bool_t TRestTools::IsBinaryFile(string fname) {

///////////////////////////////////////////////
/// \brief It extracts the number of columns from the filename extension given by argument.
/// The file will containing a binary formatted table with a fixed number of rows and columns.
/// The file should contain a binary formatted table with a fixed number of rows and columns.
///
/// The filename extension will be : ".Nxyzf", where the number of columns is `xyz`, and the
/// last character is the type of data (f/d/i), float, double and integer respectively.
///
int TRestTools::GetBinaryFileColumns(string fname) {
string extension = GetFileNameExtension(fname);
if (extension.find("N") != 0) {
ferr << "Wrong filename extension." << endl;
ferr << "Cannot guess the number of columns" << endl;
return -1;
}

Expand All @@ -294,6 +292,28 @@ int TRestTools::GetBinaryFileColumns(string fname) {
return -1;
}

///////////////////////////////////////////////
/// \brief It transposes the std::vector<std::vector> table given in the argument.
/// It will transform rows in columns.
///
template <typename T>
void TRestTools::TransposeTable(std::vector<std::vector<T>>& data) {
if (data.size() == 0) return;

std::vector<std::vector<T>> trans_vec(data[0].size(), std::vector<T>());

for (int i = 0; i < data.size(); i++)
for (int j = 0; j < data[i].size(); j++) trans_vec[j].push_back(data[i][j]);

data = trans_vec;
}

template void TRestTools::TransposeTable<Double_t>(std::vector<std::vector<Double_t>>& data);

template void TRestTools::TransposeTable<Float_t>(std::vector<std::vector<Float_t>>& data);

template void TRestTools::TransposeTable<Int_t>(std::vector<std::vector<Int_t>>& data);

///////////////////////////////////////////////
/// \brief It returns the maximum value for a particular `column` from the table given by
/// argument. If no column is specified in the arguments, then it gets the maximum from the
Expand Down Expand Up @@ -394,6 +414,31 @@ template Float_t TRestTools::GetLowestIncreaseFromTable<Float_t>(std::vector<std
template Double_t TRestTools::GetLowestIncreaseFromTable<Double_t>(std::vector<std::vector<Double_t>> data,
Int_t column);

///////////////////////////////////////////////
/// \brief It returns the lowest increase, different from zero, between the elements of a
/// particular `column` from the table given by argument.
///
/// This method is available for tables of type Float_t, Double_t and Int_t.
///
/// \warning This method will not check every possible column element difference. It will only
/// look for consecutive elements steps.
///
template <typename T>
T TRestTools::GetIntegralFromTable(const std::vector<std::vector<T>>& data) {
if (data.size() == 0) return 0;
T sum = 0;
for (int n = 0; n < data.size(); n++) {
for (int m = 0; m < data[n].size(); m++) sum += data[n][m];
}
return sum;
}

template Int_t TRestTools::GetIntegralFromTable<Int_t>(const std::vector<std::vector<Int_t>>& data);

template Float_t TRestTools::GetIntegralFromTable<Float_t>(const std::vector<std::vector<Float_t>>& data);

template Double_t TRestTools::GetIntegralFromTable<Double_t>(const std::vector<std::vector<Double_t>>& data);

///////////////////////////////////////////////
/// \brief Reads an ASCII file containing a table with values
///
Expand Down Expand Up @@ -608,6 +653,22 @@ string TRestTools::GetFileNameExtension(string fullname) {
return fullname;
}

///////////////////////////////////////////////
/// \brief Gets the filename root as the substring found before the lastest "."
///
/// Input: "/home/jgalan/abc.txt" Output: "abc"
///
string TRestTools::GetFileNameRoot(string fullname) {
size_t pos1 = fullname.find_last_of('/', -1);
size_t pos2 = fullname.find_last_of('.', -1);

if (pos1 != string::npos && pos2 != string::npos) return fullname.substr(pos1 + 1, pos2 - pos1 - 1);

if (pos1 == string::npos && pos2 != string::npos) return fullname.substr(0, pos2);

return fullname;
}

///////////////////////////////////////////////
/// \brief Returns the input string but without multiple slashes ("/")
///
Expand Down