File indexing completed on 2026-04-09 07:49:12
0001 #include <cstdlib>
0002 #include <filesystem>
0003 #include <iostream>
0004 #include <stdexcept>
0005 #include <string>
0006 #include <sys/stat.h>
0007 #include <vector>
0008
0009 #include <nlohmann/json.hpp>
0010 #include <cuda_runtime.h>
0011
0012 #include "sysrap/SEventConfig.hh"
0013
0014 #include "config.h"
0015 #include "config_path.h"
0016
0017 namespace gphox {
0018
0019 using namespace std;
0020
0021 constexpr const char *GPHOX_PTX_PATH_ENV = "CSGOptiX__optixpath";
0022
0023 bool FileExists(const std::string &path)
0024 {
0025 if (path.empty())
0026 return false;
0027 std::error_code ec;
0028 return std::filesystem::exists(path, ec) && !ec;
0029 }
0030
0031 Config::Config(std::string config_name) :
0032 name{std::getenv("GPHOX_CONFIG") ? std::getenv("GPHOX_CONFIG") : config_name}
0033 {
0034 ReadConfig(Locate(name + ".json"));
0035 }
0036
0037 std::string Config::PtxPath(const std::string &ptx_name)
0038 {
0039 const char *env_path = std::getenv(GPHOX_PTX_PATH_ENV);
0040 if (env_path && FileExists(env_path))
0041 return env_path;
0042
0043 std::string default_path = std::string(GPHOX_PTX_DIR) + "/" + ptx_name;
0044 if (FileExists(default_path))
0045 return default_path;
0046
0047 std::stringstream errmsg;
0048 errmsg << "Could not resolve PTX file \"" << ptx_name << "\".\n"
0049 << "Expected one of:\n"
0050 << " - " << GPHOX_PTX_PATH_ENV << "=<path-to-ptx>\n"
0051 << " - " << default_path;
0052 throw std::runtime_error(errmsg.str());
0053 }
0054
0055 std::string Config::Locate(std::string filename) const
0056 {
0057 std::vector<std::string> search_paths;
0058
0059 const std::string user_dir{std::getenv("GPHOX_CONFIG_DIR") ? std::getenv("GPHOX_CONFIG_DIR") : ""};
0060
0061 if (user_dir.empty())
0062 {
0063 std::string paths(GPHOX_CONFIG_SEARCH_PATHS);
0064
0065 size_t last = 0;
0066 size_t next = 0;
0067 while ((next = paths.find(':', last)) != std::string::npos)
0068 {
0069 search_paths.push_back(paths.substr(last, next-last));
0070 last = next + 1;
0071 }
0072
0073 search_paths.push_back(paths.substr(last));
0074 }
0075 else
0076 {
0077 search_paths.push_back(user_dir);
0078 }
0079
0080 struct stat buffer;
0081 std::string filepath{""};
0082 for (std::string path : search_paths)
0083 {
0084 std::string fpath{path + "/" + filename};
0085 if (stat(fpath.c_str(), &buffer) == 0)
0086 {
0087 filepath = fpath;
0088 break;
0089 }
0090 }
0091
0092 if (filepath.empty())
0093 {
0094 std::string errmsg{"Could not find config file \"" + filename + "\" in "};
0095 for (std::string path : search_paths) errmsg += (path + ":");
0096 throw std::runtime_error(errmsg);
0097 }
0098
0099 return filepath;
0100 }
0101
0102
0103
0104
0105
0106 void Config::ReadConfig(std::string filepath)
0107 {
0108 nlohmann::json json;
0109
0110 try {
0111 std::ifstream ifs(filepath);
0112 ifs >> json;
0113
0114 nlohmann::json torch_ = json["torch"];
0115
0116 torch = {
0117 .gentype = OpticksGenstep_::Type(torch_["gentype"]),
0118 .trackid = torch_["trackid"],
0119 .matline = torch_["matline"],
0120 .numphoton = torch_["numphoton"],
0121 .pos = make_float3(torch_["pos"][0], torch_["pos"][1], torch_["pos"][2]),
0122 .time = torch_["time"],
0123 .mom = normalize(make_float3(torch_["mom"][0], torch_["mom"][1], torch_["mom"][2])),
0124 .weight = torch_["weight"],
0125 .pol = make_float3(torch_["pol"][0], torch_["pol"][1], torch_["pol"][2]),
0126 .wavelength = torch_["wavelength"],
0127 .zenith = make_float2(torch_["zenith"][0], torch_["zenith"][1]),
0128 .azimuth = make_float2(torch_["azimuth"][0], torch_["azimuth"][1]),
0129 .radius = torch_["radius"],
0130 .distance = torch_["distance"],
0131 .mode = torch_["mode"],
0132 .type = storchtype::Type(torch_["type"])
0133 };
0134
0135 nlohmann::json event_ = json["event"];
0136
0137 SEventConfig::SetEventMode( string(event_["mode"]).c_str() );
0138 SEventConfig::SetMaxSlot( event_["maxslot"] );
0139 }
0140 catch (nlohmann::json::exception& e) {
0141 std::string errmsg{"Failed reading config parameters from " + filepath + "\n" + e.what()};
0142 throw std::runtime_error{errmsg};
0143 }
0144 }
0145
0146 }