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
4 changes: 4 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 Down
42 changes: 39 additions & 3 deletions source/framework/tools/src/TRestTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,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 @@ -292,6 +290,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 @@ -606,6 +626,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) {
int pos1 = fullname.find_last_of('/', -1);
int pos2 = fullname.find_last_of('.', -1);
juanangp marked this conversation as resolved.
Show resolved Hide resolved
jgalan marked this conversation as resolved.
Show resolved Hide resolved

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