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