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