CbmRoot
Loading...
Searching...
No Matches
sim/response/steer/Config.cxx
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Frederic Linz [committer], Volker Friese */
4
11#include "Config.h"
12
13#include <Logger.h>
14
15#include <fstream>
16
17#include <yaml-cpp/yaml.h>
18
19using std::string;
20
22{
23
24
25 // ----- Load settings from YAML file -------------------------------------
26 void Config::LoadYaml(const string& fileName)
27 {
28
29 LOG(info) << "Config: Reading configuration from " << fileName;
30 YAML::Node settings = YAML::LoadFile(fileName);
31
32 // --- Global settings
33 f_glb_logLevel = settings["global"]["log_level"].as<string>();
34 f_glb_logVerbose = settings["global"]["log_verbose"].as<string>();
35 f_glb_logColor = settings["global"]["log_color"].as<string>();
36 f_glb_mode = ToCbmDigiMode(settings["global"]["mode"].as<string>());
37 f_glb_numTs = settings["global"]["nTimeslices"].as<int>();
38 f_glb_firstTs = settings["global"]["firstTimeslice"].as<int>();
39
40 // --- Detector choice
41 f_det_deactivateAllBut = ToECbmModuleId(settings["detectors"]["deactivateAllBut"].as<string>());
42
43 YAML::Node deactivatedSystems = settings["detectors"]["deactivate"];
44 for (auto det : deactivatedSystems)
45 f_det_deactivate.push_back(ToECbmModuleId(det.as<string>()));
46
47 // --- Timeslice settings
48 f_ts_tslength = settings["timeslice"]["timeslicelength"].as<float>();
49 f_ts_storeAllTS = settings["timeslice"]["storeAllTimeslices"].as<bool>();
50 f_ts_startTime = settings["timeslice"]["startTime"].as<float>();
51 f_ts_timeDist = ToCbmSimTimeDist(settings["timeslice"]["timeDist"].as<string>());
52
53 // --- Background settings
54 f_bg_produceNoise = settings["background"]["produceNoise"].as<bool>();
55
56 // --- Input sources
57 YAML::Node sources = settings["sources"];
58
59 for (auto source : sources) {
60 f_src_id.push_back(source["id"].as<int>());
61 f_src_rate.push_back(source["rate"].as<float>());
62 f_src_treeAccessMode.push_back(ToECbmTreeAccess(source["treeAccessMode"].as<string>()));
63 f_src_embedToId.push_back(source["embedToId"].as<int>());
64 }
65 }
66 // ----------------------------------------------------------------------------
67
68
69 // ------ String to ECbmRecoMode ------------------------------------------
71 {
72 string temp = choice;
73 std::transform(temp.begin(), temp.end(), temp.begin(), [](unsigned char c) { return std::tolower(c); });
74 if (temp == "timeslice")
76 else if (temp == "event")
78 else
80 }
81 // ----------------------------------------------------------------------------
82
83
84 // ----- ECbmRecoMode to string -------------------------------------------
86 {
87 if (mode == cbm::sim::Mode::Timebased)
88 return "timeslice";
89 else if (mode == cbm::sim::Mode::EventByEvent)
90 return "event";
91 else
92 return "undefined";
93 }
94 // ----------------------------------------------------------------------------
95
96
97 // ----- String to cbm::sim::TimeDist -------------------------------------
99 {
100 string temp = choice;
101 std::transform(temp.begin(), temp.end(), temp.begin(), [](unsigned char c) { return std::tolower(c); });
102 if (temp == "poisson")
104 else if (temp == "uniform")
106 else
108 }
109 // ----------------------------------------------------------------------------
110
111
112 // ----- cbm::sim::TimeDist to string -------------------------------------
114 {
115 if (dist == cbm::sim::TimeDist::Poisson)
116 return "poisson";
117 else if (dist == cbm::sim::TimeDist::Uniform)
118 return "uniform";
119 else
120 return "undefined";
121 }
122 // ----------------------------------------------------------------------------
123
124
125 // ----- Convert std::string to ECbmModuleId --------------------------------
126 ECbmModuleId Config::ToECbmModuleId(std::string moduleString)
127 {
128 std::string temp = moduleString;
129 std::transform(temp.begin(), temp.end(), temp.begin(), [](unsigned char c) { return std::tolower(c); });
130 if (temp == "mvd")
131 return ECbmModuleId::kMvd;
132 else if (temp == "sts")
133 return ECbmModuleId::kSts;
134 else if (temp == "rich")
135 return ECbmModuleId::kRich;
136 else if (temp == "much")
137 return ECbmModuleId::kMuch;
138 else if (temp == "trd")
139 return ECbmModuleId::kTrd;
140 else if (temp == "tof")
141 return ECbmModuleId::kTof;
142 else if (temp == "psd")
143 return ECbmModuleId::kPsd;
144 else if (temp == "fsd")
145 return ECbmModuleId::kFsd;
146 else if (temp == "bmon")
147 return ECbmModuleId::kBmon;
148 else
150 }
151 // ----------------------------------------------------------------------------
152
153
154 // ----- Convert ECbmModuleId to std::string ---------------------------------
156 {
157 if (moduleId == ECbmModuleId::kMvd)
158 return "mvd";
159 else if (moduleId == ECbmModuleId::kSts)
160 return "sts";
161 else if (moduleId == ECbmModuleId::kRich)
162 return "rich";
163 else if (moduleId == ECbmModuleId::kMuch)
164 return "much";
165 else if (moduleId == ECbmModuleId::kTrd)
166 return "trd";
167 else if (moduleId == ECbmModuleId::kTof)
168 return "tof";
169 else if (moduleId == ECbmModuleId::kPsd)
170 return "psd";
171 else if (moduleId == ECbmModuleId::kFsd)
172 return "fsd";
173 else if (moduleId == ECbmModuleId::kBmon)
174 return "bmon";
175 else
176 return "";
177 }
178 // ----------------------------------------------------------------------------
179
180
181 // ----- Convert std::string to ECbmTreeAccess ------------------------------
182 ECbmTreeAccess Config::ToECbmTreeAccess(std::string treeAccessString)
183 {
184 std::string temp = treeAccessString;
185 std::transform(temp.begin(), temp.end(), temp.begin(), [](unsigned char c) { return std::tolower(c); });
186 if (temp == "random")
188 else if (temp == "repeat")
190 else
192 }
193 // ----------------------------------------------------------------------------
194
195
196 // ----- Convert ECbmTreeAccess to std::string ------------------------------
197 string Config::ToString(ECbmTreeAccess treeAccessString)
198 {
199 if (treeAccessString == ECbmTreeAccess::kRandom)
200 return "random";
201 else if (treeAccessString == ECbmTreeAccess::kRepeat)
202 return "repeat";
203 else
204 return "regular";
205 }
206 // ----------------------------------------------------------------------------
207
208
209 // ----- Save settings to YAML node ---------------------------------------
210 YAML::Node Config::ToYaml()
211 {
212 YAML::Node settings;
213
214 // --- Global settings
215 settings["global"]["log_level"] = f_glb_logLevel;
216 settings["global"]["log_verbose"] = f_glb_logVerbose;
217 settings["global"]["log_color"] = f_glb_logColor;
218 settings["global"]["mode"] = ToString(f_glb_mode);
219 settings["global"]["nTimeslices"] = f_glb_numTs;
220 settings["global"]["firstTimeslice"] = f_glb_firstTs;
221
222 // --- Detector choice
223 settings["detectors"]["deactivateAllBut"] = ToString(f_det_deactivateAllBut);
224
225 for (int i = 0; i < (int) f_det_deactivate.size(); ++i) {
226 settings["detectors"]["deactivate"][i] = ToString(f_det_deactivate.at(i));
227 }
228
229 // --- Timeslice settings
230 settings["timeslice"]["timeslicelength"] = f_ts_tslength;
231 settings["timeslice"]["storeAllTimeslices"] = f_ts_storeAllTS;
232 settings["timeslice"]["startTime"] = f_ts_startTime;
233 settings["timeslice"]["timeDist"] = ToString(f_ts_timeDist);
234
235 // --- Background settings
236 settings["background"]["produceNoise"] = f_bg_produceNoise;
237
238 // --- Input sources
239 for (int i = 0; i < (int) f_src_id.size(); ++i) {
240 settings["sources"][i]["id"] = f_src_id.at(i);
241 settings["sources"][i]["rate"] = f_src_rate.at(i);
242 settings["sources"][i]["treeAccessMode"] = ToString(f_src_treeAccessMode.at(i));
243 settings["sources"][i]["embedToId"] = f_src_embedToId.at(i);
244 }
245
246 return settings;
247 }
248 // ----------------------------------------------------------------------------
249
250} // namespace cbm::sim::digitization
ECbmTreeAccess
Mode to read entries from a ROOT TTree.
Definition CbmDefs.h:152
ECbmModuleId
Definition CbmDefs.h:39
@ kMvd
Micro-Vertex Detector.
@ kTrd
Transition Radiation Detector.
@ kTof
Time-of-flight Detector.
@ kNotExist
If not found.
@ kPsd
Projectile spectator detector.
@ kSts
Silicon Tracking System.
@ kMuch
Muon detection system.
@ kFsd
Forward spectator detector.
@ kRich
Ring-Imaging Cherenkov Detector.
std::string ToString()
String output (YAML format)
std::vector< ECbmModuleId > f_det_deactivate
YAML::Node ToYaml()
Save to YAML node.
void LoadYaml(const std::string &filename)
Load from YAML file.
ECbmTreeAccess ToECbmTreeAccess(std::string treeAccessString)
cbm::sim::TimeDist ToCbmSimTimeDist(std::string tag)
cbm::sim::Mode ToCbmDigiMode(std::string tag)
ECbmModuleId ToECbmModuleId(std::string moduleString)
std::vector< ECbmTreeAccess > f_src_treeAccessMode
TimeDist
Definition Defs.h:29