From 1dd07c02aed238287b9e6b70055047f8fee07d6a Mon Sep 17 00:00:00 2001 From: Oleg Kalashev Date: Mon, 16 Sep 2024 18:59:45 +0300 Subject: [PATCH] tables dir location search algorithm updated --- bin/tables/.DS_Store | Bin 6148 -> 0 bytes src/app/crbeam/CMakeLists.txt | 2 +- src/app/crbeam/CRbeam.cpp | 2 +- src/lib/Background.cpp | 4 +-- src/lib/Deflection3D.cpp | 1 + src/lib/GZK.cpp | 2 +- src/lib/MathUtils.cpp | 1 + src/lib/Stecker16Background.cpp | 5 +-- src/lib/SteckerEBL.cpp | 2 +- src/lib/Utils.cpp | 58 +++++++++++--------------------- src/lib/Utils.h | 12 +++++-- 11 files changed, 41 insertions(+), 48 deletions(-) delete mode 100644 bin/tables/.DS_Store diff --git a/bin/tables/.DS_Store b/bin/tables/.DS_Store deleted file mode 100644 index a46770735c6e3aea0a1becf7952cd7702fa16c87..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK&1%~~5Z-m%M5J%zmTUnOW&uu|I?m8jR9yLR>Pw>6ur1&#d{1s z%<~KalpQAn&L)w&@irG#%mpiCJyqlQh>J9z1o=p$H~`2J|9jIZO3c z=4A{6hJj^Z0M`c(6w%jME0jwIG eos; - aTableFile = TABLES_DIR + aTableFile; + aTableFile = tables_dir + aTableFile; std::ifstream file(aTableFile.c_str()); if(!file.good()) diff --git a/src/lib/Deflection3D.cpp b/src/lib/Deflection3D.cpp index d24de77..a0dbfd2 100644 --- a/src/lib/Deflection3D.cpp +++ b/src/lib/Deflection3D.cpp @@ -247,6 +247,7 @@ namespace Interactions { double Bgauss = 10.; MonochromaticMF mf(r,lambdaMpc,Bgauss); mf.Print(lambdaMpc); + return 0; } int Deflection3D::UnitTest() { diff --git a/src/lib/GZK.cpp b/src/lib/GZK.cpp index 01116c9..9cee22e 100644 --- a/src/lib/GZK.cpp +++ b/src/lib/GZK.cpp @@ -51,7 +51,7 @@ GZK::GZK(BackgroundIntegral* aBackground, int aRandSeed):fBackground(aBackground Function* GZK::InitSigma(ParticleType aPrim) { - std::string tablesDir = TABLES_DIR "sophia2/"; + std::string tablesDir = tables_dir + "sophia2/"; Utils::TableReader reader(tablesDir+(aPrim==Proton?"p":"n"), 2); std::vector& s = reader.getColumn(0); std::vector& sigma = reader.getColumn(1); diff --git a/src/lib/MathUtils.cpp b/src/lib/MathUtils.cpp index 1ee7664..593403d 100644 --- a/src/lib/MathUtils.cpp +++ b/src/lib/MathUtils.cpp @@ -975,6 +975,7 @@ template bool MathUtils::SampleLogscaleDistribution(const Function& aDis //Sampler4 dil; NRSampler dil; dil.UnitTest(); + return 0; } double MathUtils::Integration_qag ( diff --git a/src/lib/Stecker16Background.cpp b/src/lib/Stecker16Background.cpp index 6f27e15..bc3a312 100644 --- a/src/lib/Stecker16Background.cpp +++ b/src/lib/Stecker16Background.cpp @@ -32,11 +32,11 @@ namespace Backgrounds { Stecker16LowerBackground::Stecker16LowerBackground() : - Stecker16Background(TABLES_DIR "stecker_ebl16/comoving_enerdens_lo.csv", "Stecker_16_lower") { + Stecker16Background(tables_dir + "stecker_ebl16/comoving_enerdens_lo.csv", "Stecker_16_lower") { } Stecker16UpperBackground::Stecker16UpperBackground() : - Stecker16Background(TABLES_DIR "stecker_ebl16/comoving_enerdens_up.csv", "Stecker_16_upper") + Stecker16Background(tables_dir + "stecker_ebl16/comoving_enerdens_up.csv", "Stecker_16_upper") { } @@ -55,6 +55,7 @@ int Stecker16Background::UnitTest() BackgroundUtils::UnitTest(backgrUpper); Stecker16LowerBackground backgrLower; BackgroundUtils::UnitTest(backgrLower); + return 0; } } \ No newline at end of file diff --git a/src/lib/SteckerEBL.cpp b/src/lib/SteckerEBL.cpp index 4064ead..fec7cf3 100644 --- a/src/lib/SteckerEBL.cpp +++ b/src/lib/SteckerEBL.cpp @@ -36,7 +36,7 @@ namespace Backgrounds { #define IRO_DATA_FILE "iro_stecker2005" Stecker2005EBL::Stecker2005EBL(): - CTableWithHeaderReader(TABLES_DIR IRO_DATA_FILE), + CTableWithHeaderReader((tables_dir + IRO_DATA_FILE).c_str()), m_accuracyE(0), m_accuracyZ(0), m_curE(0) diff --git a/src/lib/Utils.cpp b/src/lib/Utils.cpp index 22fa6f6..556707a 100644 --- a/src/lib/Utils.cpp +++ b/src/lib/Utils.cpp @@ -36,52 +36,34 @@ namespace fs = boost::filesystem; namespace Utils { - const char PathSeparator = -#ifdef _WIN32 - '\\'; -#else - '/'; -#endif + const char PathSeparator = DIR_DELIMITER_STR[0]; - - std::string shared_tables_path(){ + std::string default_tables_path(){ + // On conda env installations tables are stored in shared folder + // For standalone installations tables are stored in bin folder std::string tables_folder_name("tables"); fs::path prog_path = boost::dll::program_location(); + fs::path prog_name = prog_path.stem().string(); + fs::path prog_dir = prog_path.parent_path(); + fs::path shared_tables_dir = prog_dir.parent_path() / "shared" / prog_name / tables_folder_name; + fs::path exe_tables_dir = prog_dir / tables_folder_name; - // for some reason boost::filesystem::path.parent_path() and fs::canonical didn't work properly os my Mac OS so we do it manually + if(fs::is_directory(shared_tables_dir)) + return shared_tables_dir.string(); + if(fs::is_directory(exe_tables_dir)) + return exe_tables_dir.string(); - std::stringstream path(prog_path.string()); - std::string segment; - std::vector seglist; + // we don't check for existence of './tables' and + // don't throw exception here since on some platforms fs::is_directory doesn't work properly + std::cerr << "WARNING: Neither " << shared_tables_dir.string() << " nor \n" << exe_tables_dir.string() << + " was found.\n You can download latest tables from\n" << + "https://github.com/okolo/mcray/tree/main/bin/tables\n"; - while(std::getline(path, segment, PathSeparator)) - { - seglist.push_back(segment); - } - - if(seglist.size() >= 3){ - std::string app_name = seglist.back(); - seglist.pop_back(); - seglist.pop_back(); - seglist.push_back(std::string("shared")); - seglist.push_back(app_name); - seglist.push_back(tables_folder_name); - std::ostringstream os; - for(std::string& seg : seglist){ - os << seg << PathSeparator; - } - std::string table_dir = os.str(); - table_dir.pop_back(); - std::cout << "extracted path:" << table_dir << "\n\n"; - fs::path p = fs::path(table_dir); - if(fs::exists(fs::status(p))) - return table_dir; - else - std::cout << "type " << fs::status(p).type() << "\n\n"; - } - return tables_folder_name + PathSeparator; + return tables_folder_name; } + std::string tables_dir = default_tables_path() + DIR_DELIMITER_STR; + int omp_thread_count() { int n = 0; #pragma omp parallel reduction(+:n) diff --git a/src/lib/Utils.h b/src/lib/Utils.h index b9ba358..d9323fc 100644 --- a/src/lib/Utils.h +++ b/src/lib/Utils.h @@ -43,10 +43,18 @@ namespace Utils { -std::string shared_tables_path(); +std::string default_tables_path(); + + +#ifdef _WIN32 +#define DIR_DELIMITER_STR "\\" +#else #define DIR_DELIMITER_STR "/" -#define TABLES_DIR "tables" DIR_DELIMITER_STR +#endif + +extern std::string tables_dir; +//#define TABLES_DIR "tables" DIR_DELIMITER_STR class Exception {