ndmspc 0.20250304.0
Loading...
Searching...
No Matches
Core.cxx
1#include <TSystem.h>
2#include <cstdlib>
3#include "TString.h"
4#include "Core.h"
5#include "Utils.h"
6
8ClassImp(Ndmspc::Core);
10
11namespace Ndmspc {
12
14json gCfg;
15
16bool Core::LoadConfig(std::string config, std::string userConfig, std::string & environment, std::string userConfigRaw,
17 std::string binning)
18{
22 std::string fileContent = Utils::OpenRawFile(config);
23 if (!fileContent.empty()) {
24 gCfg = json::parse(fileContent);
25 Printf("Using config file '%s' ...", config.c_str());
26 if (!userConfig.empty()) {
27 std::string fileContentUser = Ndmspc::Utils::OpenRawFile(userConfig);
28 if (!fileContentUser.empty()) {
29 json userCfg = json::parse(fileContentUser);
30 gCfg.merge_patch(userCfg);
31 Printf("User config file '%s' was merged ...", userConfig.c_str());
32 }
33 else {
34 Printf("Warning: User config '%s' was specified, but it was not open !!!", userConfig.c_str());
35 return false;
36 }
37 }
38 }
39 else {
40 Printf("Error: Problem opening config file '%s' !!! Exiting ...", config.c_str());
41 return false;
42 }
43
44 for (auto & cut : gCfg["ndmspc"]["cuts"]) {
45 if (!cut["rebin"].is_number_integer()) gCfg["ndmspc"]["cuts"][cut["axis"].get<std::string>()] = 1;
46 Int_t rebin = 1;
47 Int_t rebin_start = 1;
48 if (cut["rebin"].is_number_integer()) rebin = cut["rebin"].get<Int_t>();
49 if (cut["rebin_start"].is_number_integer()) rebin_start = cut["rebin_start"].get<Int_t>();
50 if (rebin > 1 && rebin_start >= rebin) {
51 Printf("Error: rebin_start=%d is greater than rebin=%d for axis '%s' !!! Please set rebin_start to lower then "
52 "rebin !!! Exiting ...",
53 rebin_start, rebin, cut["axis"].get<std::string>().c_str());
54 gSystem->Exit(1);
55 }
56 }
57
58 if (!environment.empty()) {
59 gCfg["ndmspc"]["environment"] = environment;
60 LoadEnvironment(environment);
61 }
62 else if (gCfg["ndmspc"]["environment"].is_string() && !gCfg["ndmspc"]["environment"].get<std::string>().empty()) {
63 environment = gCfg["ndmspc"]["environment"].get<std::string>();
64 LoadEnvironment(environment);
65 }
66
67 if (!userConfigRaw.empty()) {
68 json userCfgRaw = json::parse(userConfigRaw);
69 gCfg.merge_patch(userCfgRaw);
70 Printf("Config raw '%s' was merged...", userConfigRaw.c_str());
71 }
72 // Printf("binning=%s", binning.c_str());
73 if (!binning.empty()) {
74 std::vector<std::string> ba = Utils::Tokenize(binning.c_str(), '_');
75 int i = 0;
76 int index = -1;
77 for (auto & cut : gCfg["ndmspc"]["cuts"]) {
78 index++;
79 if (cut["enabled"].is_boolean() && cut["enabled"].get<bool>() == false) continue;
80 std::vector<std::string> b = Utils::Tokenize(ba[i].c_str(), '-');
81 if (b.size() == 2) {
82 gCfg["ndmspc"]["cuts"][index]["rebin"] = atoi(b[0].c_str());
83 gCfg["ndmspc"]["cuts"][index]["rebin_start"] = atoi(b[1].c_str());
84 }
85 i++;
86 }
87 }
88 // exit(1);
89 return true;
90}
91bool Core::LoadEnvironment(std::string environment)
92{
96 if (gCfg["ndmspc"]["environments"][environment].is_object()) {
97 Printf("Using environment '%s' ...", environment.c_str());
98 json myCfg = gCfg["ndmspc"]["environments"][environment];
99 gCfg.merge_patch(myCfg);
100 }
101 else {
102 if (!environment.compare("local")) {
103 // Printf("Warning: No environment specified !!! Setting it to 'default' !!!");
104 return true;
105 }
106 Printf("Error: Environment '%s' was not found !!! Exiting ...", environment.c_str());
107 return false;
108 }
109 return true;
110}
111bool Core::SaveConfig(json cfg, std::string filename)
112{
116 return Utils::SaveRawFile(filename, cfg.dump());
117}
118} // namespace Ndmspc
Core object.
Definition Core.h:18
static bool SaveConfig(json cfg, std::string filename)
Definition Core.cxx:111
static bool LoadEnvironment(std::string environmenti="local")
Definition Core.cxx:91
static bool LoadConfig(std::string config, std::string userConfig, std::string &environment, std::string userConfigRaw="", std::string binning="")
Definition Core.cxx:16
static std::string OpenRawFile(std::string filename)
Definition Utils.cxx:57
static std::vector< std::string > Tokenize(std::string_view input, const char delim)
Definition Utils.cxx:256
static bool SaveRawFile(std::string filename, std::string content)
Definition Utils.cxx:85