CbmRoot
Loading...
Searching...
No Matches
CbmGeoSetupRepoProvider.cxx
Go to the documentation of this file.
1/* Copyright (C) 2019-2025 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Evgeny Lavrik, Florian Uhlig [committer] */
4
9
11
12#include "CbmDefs.h"
13#include "TSystem.h"
14#include "TSystemDirectory.h"
15
16#include <Logger.h>
17
18#include <boost/algorithm/string.hpp>
19
20#include <fstream>
21#include <regex>
22
24
25namespace
26{ //anonymous namespace with helpers
27
28 /*
30 struct RepoInfo {
31 std::string author;
32 std::string date;
33 std::string revision;
34 };
35
37 RepoInfo GetRepoInfo(std::string fileName) {
38 std::string svnInfo = gSystem->GetFromPipe(("svn info " + fileName).c_str()).Data();
39 std::smatch match;
40 if (std::regex_search(svnInfo, match, std::regex(R"(Last Changed Author: (.*)\nLast Changed Rev: (.*)\nLast Changed Date: (.*))")))
41 return {match[1], match[2], match[3]};
42
43 return {"", "", ""};
44 }
45*/
46
48 struct DetInfo {
49 std::string tag;
50 std::string dir;
51 std::string system;
52 std::string name;
53 };
54
56 std::map<ECbmModuleId, DetInfo> detectorMap{
57 {ECbmModuleId::kCave, {"caveGeoTag", "cave", "cave", "CAVE"}},
58 {ECbmModuleId::kMagnet, {"magnetGeoTag", "magnet", "magnet", "MAGNET"}},
59 {ECbmModuleId::kPipe, {"pipeGeoTag", "pipe", "pipe", "PIPE"}},
60 {ECbmModuleId::kTarget, {"targetGeoTag", "target", "target", "TARGET"}},
61 {ECbmModuleId::kMvd, {"mvdGeoTag", "mvd", "mvd", "MVD"}},
62 {ECbmModuleId::kSts, {"stsGeoTag", "sts", "sts", "STS"}},
63 {ECbmModuleId::kRich, {"richGeoTag", "rich", "rich", "RICH"}},
64 {ECbmModuleId::kMuch, {"muchGeoTag", "much", "much", "MUCH"}},
65 {ECbmModuleId::kMust, {"mustGeoTag", "must", "must", "MUST"}},
66 {ECbmModuleId::kTrd, {"trdGeoTag", "trd", "trd", "TRD"}},
67 {ECbmModuleId::kTof, {"tofGeoTag", "tof", "tof", "TOF"}},
68 {ECbmModuleId::kPsd, {"psdGeoTag", "psd", "psd", "PSD"}},
69 {ECbmModuleId::kFsd, {"fsdGeoTag", "fsd", "fsd", "FSD"}},
70 {ECbmModuleId::kHodo, {"hodoGeoTag", "sts", "sts", "HODO"}},
71 {ECbmModuleId::kShield, {"shieldGeoTag", "much", "shield", "SHIELD"}},
72 {ECbmModuleId::kBmon, {"bmonGeoTag", "bmon", "bmon", "BMON"}},
73 {ECbmModuleId::kPlatform, {"platGeoTag", "passive", "platform", "PLATFORM"}},
74 };
75
77 std::vector<std::string> ListDirectory(std::string path, std::string searchPattern)
78 {
79 TList* fileList = TSystemDirectory("", path.c_str()).GetListOfFiles();
80
81 std::regex setupRegex(searchPattern);
82 std::smatch match;
83 std::vector<std::string> result;
84 for (const auto&& file : *fileList) {
85 std::string fileName = file->GetName();
86
87 if (std::regex_search(fileName, match, setupRegex)) result.push_back(match[1]);
88 }
89
90 delete fileList;
91
92 return result;
93 }
94} // end anonymous namespace
95
96std::vector<std::string> CbmGeoSetupRepoProvider::GetSetupTags()
97{
98 std::string path = gSystem->Getenv("VMCWORKDIR");
99 path += "/geometry/setup";
100 return ListDirectory(path, R"(setup_(.*).C)");
101};
102
104{
105 std::string path = gSystem->Getenv("VMCWORKDIR");
106 path += "/input";
107 return ListDirectory(path, R"(field_(.*).root)");
108};
109
111{
112 std::string path = gSystem->Getenv("VMCWORKDIR");
113 path += "/geometry/media";
114 return ListDirectory(path, R"(media_(.*).geo)");
115}
116
121CbmGeoSetup CbmGeoSetupRepoProvider::GetSetupByTag(std::string setupTag, std::string revision)
122{
123 if (fSetup.GetModuleMap().size()) {
124 LOG(warn) << "-W- LoadSetup " << setupTag << ": overwriting existing setup " << fSetup.GetTag();
125 }
126
127 LOG(info) << "Loading CbmGeoSetup from repository.\nSetup tag: " << setupTag
128 << " Revision: " << (revision.empty() ? "latest" : revision);
129
130 std::string base = gSystem->Getenv("VMCWORKDIR");
131 std::string geoDir = base + "/geometry/";
132 std::string fieldDir = base + "/input/";
133 std::string setupFilePath = base;
134 setupFilePath += "/geometry/setup/setup_" + setupTag + ".C";
135 std::ifstream setupFile(setupFilePath);
136 std::string fileContents((std::istreambuf_iterator<char>(setupFile)), std::istreambuf_iterator<char>());
137
138 // remove commented out-lines
139 std::regex commentRegex("/[/]+.*");
140 std::string replacementString{""};
141 fileContents = std::regex_replace(fileContents, commentRegex, replacementString);
142
143 // setup name
144 std::smatch match;
145 std::regex_search(fileContents, match, std::regex(R"(.*setup->SetTitle\‍(\"(\w+)\"\);)"));
146 std::string setupName = match[1];
147
148 // field tag
149 std::regex_search(fileContents, match, std::regex(R"(fieldTag\s+=\s+\"(\w+)\";)"));
150 std::string fieldTag = match[1];
151 std::string fieldFilePath = fieldDir + "field_" + fieldTag + ".root";
152
153 // field origin x, y, z; field scale
154 Double_t fieldX = 0., fieldY = 0., fieldZ = 40., fieldScale = 1.;
155
156 if (std::regex_search(fileContents, match, std::regex(R"(fieldX\s+=\s+([-+]?(\d+)?(\.\d+)?(\.)?);)")))
157 fieldX = std::stod(match[1]);
158 if (std::regex_search(fileContents, match, std::regex(R"(fieldY\s+=\s+([-+]?(\d+)?(\.\d+)?(\.)?);)")))
159 fieldY = std::stod(match[1]);
160 if (std::regex_search(fileContents, match, std::regex(R"(fieldZ\s+=\s+([-+]?(\d+)?(\.\d+)?(\.)?);)")))
161 fieldZ = std::stod(match[1]);
162 if (std::regex_search(fileContents, match, std::regex(R"(fieldScale\s+=\s+([-+]?(\d+)?(\.\d+)?(\.)?);)")))
163 fieldScale = std::stod(match[1]);
164
165 // media tag, if present
166 std::string mediaTag;
167 std::string mediaFilePath;
168 if (std::regex_search(fileContents, match, std::regex(R"(mediaTag\s+=\s+\"(\w+)\";)"))) {
169 mediaTag = match[1];
170 mediaFilePath = geoDir + "media/media_" + mediaTag + ".geo";
171 }
172
173 // detector tags
174 std::map<ECbmModuleId, CbmGeoSetupModule> moduleMap;
175 for (auto detector : detectorMap) {
176 // std::regex tagRegex(R"(.*)" + detector.second.tag + R"(\s+=\s+\"([a-zA-Z_0-9:]+)\";)");
177 std::regex tagRegex(R"(.*)" + detector.second.tag + R"(\s+=\s+\"([\w:]+)\";)");
178 std::regex setModuleRegex(R"(.*SetModule\‍(.*)" + detector.second.tag + R"(\);)");
179 std::string tag;
180 bool added = false;
181 if (std::regex_search(fileContents, match, tagRegex)) {
182 tag = match[1];
183 }
184 if (std::regex_search(fileContents, match, setModuleRegex)) {
185 added = true;
186 }
187
188 if (tag.size() && added) {
189 ECbmModuleId moduleId = detector.first;
190 moduleMap[moduleId] = GetModuleByTag(moduleId, tag);
191 }
192 }
193
194 // default cave, if none was found
195 if (moduleMap.find(ECbmModuleId::kCave) == moduleMap.end()) {
197 }
198
199 // field creation and configuration
200 CbmGeoSetupField field = GetFieldByTag(fieldTag);
201 field.GetMatrix().SetTranslation(fieldX, fieldY, fieldZ);
202 field.SetScale(fieldScale);
203
204 // media creation and configuration
205 CbmGeoSetupMedia media = GetMediaByTag(mediaTag);
206
207 // actual setup creation and configuration
208 // RepoInfo setupInfo = GetRepoInfo(setupFilePath);
209 CbmGeoSetup setup;
210 setup.SetName(setupName);
211 setup.SetTag(setupTag);
212 setup.SetModuleMap(moduleMap);
213 setup.SetField(field);
214 setup.SetMedia(media);
215 // setup.SetAuthor(setupInfo.author);
216 // setup.SetRevision(setupInfo.revision);
217 // setup.SetDate(setupInfo.date);
218
219 return setup;
220}
221
223{
224 std::string base = gSystem->Getenv("VMCWORKDIR");
225 std::string geoDir = base + "/geometry/";
226
227 auto detector = detectorMap[moduleId];
228 // RepoInfo info;
229 std::string full_file_path;
230 // split the input string at the character ";" which divides the string
231 // into different geometry files
232 std::vector<std::string> _geom;
233 boost::split(_geom, tag, [](char c) { return c == ':'; });
234 for (auto& string : _geom) {
235
236 std::string geoFilePath =
237 geoDir + (detector.dir.size() ? detector.dir + "/" : detector.dir) + detector.system + "_" + string + ".geo.root";
238
239 if (gSystem->AccessPathName(geoFilePath.c_str()) == kTRUE) { // doesn't exist
240 geoFilePath.erase(geoFilePath.size() - 5);
241 if (gSystem->AccessPathName(geoFilePath.c_str()) == kTRUE) {
242 LOG(error) << "Geometry file not found for " << detector.system;
243 }
244 }
245
246 // info = GetRepoInfo(geoFilePath);
247
248 // strip base path
249 if (geoFilePath.find(geoDir) != std::string::npos) geoFilePath.replace(0, geoDir.size(), "");
250
251 full_file_path += geoFilePath;
252 full_file_path += ":";
253 }
254 full_file_path.pop_back(); // Remove the last ;
255
256 CbmGeoSetupModule module;
257 module.SetName(detector.name);
258 module.SetFilePath(full_file_path);
259 module.SetTag(tag);
260 module.SetModuleId(moduleId);
261 // module.SetAuthor(info.author);
262 // module.SetRevision(info.revision);
263 // module.SetDate(info.date);
264 module.SetActive(kTRUE);
265
266 return module;
267}
268
270{
271 std::string base = std::string(gSystem->Getenv("VMCWORKDIR")) + "/";
272 std::string fieldDir = base + "input/";
273
274 std::string fieldFilePath = fieldDir + "field_" + tag + ".root";
275
276 if (gSystem->AccessPathName(fieldFilePath.c_str()) == kTRUE) { // doesn't exist
277 LOG(error) << "Field file not found for tag " << tag;
278 }
279
280 // strip base path
281 if (fieldFilePath.find(base) != std::string::npos) fieldFilePath.replace(0, base.size(), "");
282
283 CbmGeoSetupField field = fSetup.GetField();
284 field.SetTag(tag);
285 field.SetFilePath(fieldFilePath);
286 // field svn author, revision and date are not availabe
287
288 return field;
289}
290
292{
293 std::string base = std::string(gSystem->Getenv("VMCWORKDIR")) + "/";
294 std::string geoDir = base + "geometry/";
295 std::string mediaDir = base + "geometry/media/";
296
297 std::string mediaFilePath = mediaDir + "media_" + tag + ".geo";
298
299 if (gSystem->AccessPathName(mediaFilePath.c_str()) == kTRUE) { // doesn't exist
300 LOG(warn) << "Media file not found for tag " << (tag.size() ? "(empty)" : tag) << " using default media.geo";
301
302 mediaFilePath = geoDir + "media.geo";
303 }
304
305 // RepoInfo info = GetRepoInfo(mediaFilePath);
306
307 // strip base path
308 if (mediaFilePath.find(geoDir) != std::string::npos) mediaFilePath.replace(0, geoDir.size(), "");
309
310 CbmGeoSetupMedia media = fSetup.GetMedia();
311 media.SetTag(tag);
312 media.SetFilePath(mediaFilePath);
313 // media.SetAuthor(info.author);
314 // media.SetRevision(info.revision);
315 // media.SetDate(info.date);
316
317 return media;
318}
319
323void CbmGeoSetupRepoProvider::LoadSetup(std::string setupTag, std::string revision)
324{
325 fSetup = GetSetupByTag(setupTag, revision);
326}
ECbmModuleId
Enumerator for module Identifiers.
Definition CbmDefs.h:45
@ kMvd
Micro-Vertex Detector.
Definition CbmDefs.h:47
@ kPipe
Beam pipe.
Definition CbmDefs.h:63
@ kHodo
Hodoscope (for test beam times)
Definition CbmDefs.h:55
@ kCave
Cave.
Definition CbmDefs.h:66
@ kTrd
Transition Radiation Detector.
Definition CbmDefs.h:51
@ kShield
Beam pipe shielding in MUCH section.
Definition CbmDefs.h:64
@ kMagnet
Magnet.
Definition CbmDefs.h:61
@ kTof
Time-of-flight Detector.
Definition CbmDefs.h:52
@ kTarget
Target.
Definition CbmDefs.h:62
@ kPsd
Projectile spectator detector.
Definition CbmDefs.h:54
@ kSts
Silicon Tracking System.
Definition CbmDefs.h:48
@ kMust
MuSt detection system.
Definition CbmDefs.h:53
@ kPlatform
RICH rail platform.
Definition CbmDefs.h:65
@ kMuch
Muon detection system.
Definition CbmDefs.h:50
@ kFsd
Forward spectator detector.
Definition CbmDefs.h:59
@ kBmon
Bmon Counter.
Definition CbmDefs.h:57
@ kRich
Ring-Imaging Cherenkov Detector.
Definition CbmDefs.h:49
ClassImp(CbmGeoSetupRepoProvider)
TGeoTranslation & GetMatrix()
void SetScale(Double_t value)
void SetFilePath(std::string value)
void SetTag(std::string value)
void SetTag(std::string value)
void SetFilePath(std::string value)
CbmGeoSetupModule GetDefaultCaveModule()
Gets defauk cave if none was provided by the other means.
Setup provider with local (svn) repository functionality.
virtual CbmGeoSetupModule GetModuleByTag(ECbmModuleId moduleId, std::string tag)
Abstract method for constructing the module by id and tag.
virtual CbmGeoSetupField GetFieldByTag(std::string tag)
Abstract method for constructing the field by tag.
virtual std::vector< std::string > GetSetupTags()
Abstract method to get the list of setup tags.
virtual CbmGeoSetup GetSetupByTag(std::string setupTag, std::string revision)
virtual std::vector< std::string > GetFieldTags()
Abstract method to get the list of field tags.
virtual CbmGeoSetupMedia GetMediaByTag(std::string tag)
Abstract method for constructing the media by tag.
virtual std::vector< std::string > GetMediaTags()
Abstract method to get the list of media tags.
virtual void LoadSetup(std::string setupTag, std::string revision="")
Data transfer object to represent the CBM Detector setup.
Definition CbmGeoSetup.h:34
void SetName(std::string value)
Definition CbmGeoSetup.h:48
void SetField(CbmGeoSetupField value)
Definition CbmGeoSetup.h:55
void SetTag(std::string value)
Definition CbmGeoSetup.h:49
void SetMedia(CbmGeoSetupMedia value)
Definition CbmGeoSetup.h:56
void SetModuleMap(std::map< ECbmModuleId, CbmGeoSetupModule > value)
Definition CbmGeoSetup.h:54