CbmRoot
CbmGeoSetupDbProvider.cxx
Go to the documentation of this file.
1 
7 
8 #include <iostream>
9 
10 #include "TSQLResult.h"
11 #include "TSQLRow.h"
12 #include "TSQLiteServer.h"
13 
14 #include "FairLogger.h"
15 #include "TSystem.h"
16 
17 #include "CbmDefs.h"
18 
20 
21 namespace { //anonymous namespace with helpers
22  std::map<Int_t, ECbmModuleId> dbToCbmModuleIdMap = {
26  {4, ECbmModuleId::kMvd},
27  {8, ECbmModuleId::kSts},
28  {16, ECbmModuleId::kRich},
29  {32, ECbmModuleId::kTrd},
30  {64, ECbmModuleId::kTof},
31  {128, ECbmModuleId::kPsd},
33  {512, ECbmModuleId::kMuch},
34  {1024, ECbmModuleId::kHodo},
35  {2048, ECbmModuleId::kTarget},
36  {4096, ECbmModuleId::kShield},
37  };
38 
40  std::string FieldDir() { return "input/"; }
41 
43  std::string DbGeoDir() { return "db/"; }
44 
46  std::string LocalDbPath() {
47  std::string path =
48  std::string(gSystem->Getenv("VMCWORKDIR")) + "/geometry/db/local.db";
49  if (gSystem->AccessPathName(path.c_str()) == kTRUE) // does not exist
50  LOG(fatal) << "Geometry DB file does not exist: " << path;
51  return "sqlite://" + path;
52  };
53 
55  std::map<Int_t, std::string> GetSetupModuleTagMap(Int_t setupId) {
56  TSQLiteServer db(LocalDbPath().c_str());
57 
58  std::string query = std::string()
59  + "select sms.idsd, sms.idsm, sm.idf, sm.smtag, f.idm "
60  "from sms sms "
61  "inner join setupmodule sm on sms.idsm=sm.idsm "
62  "inner join file f on sm.idf=f.idf "
63  "where sms.idsd="
64  + std::to_string(setupId);
65  TSQLResult* resQ = db.Query(query.c_str());
66  if (resQ == 0 || resQ->GetRowCount() == 0) return {};
67 
68  std::map<Int_t, std::string> result;
69  TSQLRow* row;
70  while ((row = resQ->Next()) != 0) {
71  result[std::atoi(row->GetField(4))] = row->GetField(3);
72  }
73  delete resQ;
74  delete row;
75  db.Close();
76 
77  return result;
78  }
79 
81  std::vector<std::string> ListTable(std::string table, std::string column) {
82  TSQLiteServer db(LocalDbPath().c_str());
83 
84  std::string query = std::string("select ") + column + " from " + table;
85  TSQLResult* resQ = db.Query(query.c_str());
86  if (resQ == 0 || resQ->GetRowCount() == 0) return {};
87 
88  std::vector<std::string> result;
89  TSQLRow* row;
90  while ((row = resQ->Next()) != 0) {
91  result.push_back(row->GetField(0));
92  }
93  delete resQ;
94  delete row;
95  db.Close();
96 
97  return result;
98  }
99 } // end anonymous namespace
100 
101 std::vector<std::string> CbmGeoSetupDbProvider::GetSetupTags() {
102  TSQLiteServer db(LocalDbPath().c_str());
103 
104  std::string query =
105  std::string("select stag,revision from setup order by revision desc");
106  TSQLResult* resQ = db.Query(query.c_str());
107  if (resQ == 0 || resQ->GetRowCount() == 0) return {};
108 
109  std::vector<std::string> result;
110  TSQLRow* row;
111  while ((row = resQ->Next()) != 0) {
112  result.push_back(std::string(row->GetField(0)) + "@" + row->GetField(1));
113  }
114  delete resQ;
115  delete row;
116  db.Close();
117 
118  return result;
119 }
120 
121 std::vector<std::string> CbmGeoSetupDbProvider::GetFieldTags() {
122  return ListTable("field", "tag");
123 };
124 
125 std::vector<std::string> CbmGeoSetupDbProvider::GetMediaTags() {
126  return ListTable("material", "matag");
127 };
128 
129 // Get bare setup without field, media and modules loaded
131  std::string revision) {
132  if (revision.empty() && setupTag.find("@") != std::string::npos) {
133  auto pos = setupTag.find("@");
134  revision = setupTag.substr(pos + 1, setupTag.size() - pos - 1);
135  setupTag = setupTag.substr(0, pos);
136  }
137 
138  LOG(info) << "Loading CbmGeoSetup from geometry database.\nSetup tag: "
139  << setupTag
140  << " Revision: " << (revision.size() ? revision : "latest");
141 
142  std::string path = LocalDbPath();
143  TSQLiteServer db(LocalDbPath().c_str());
144  std::string query = "select s.idsd, s.stag, s.sdate, s.desc, "
145  "s.author, s.revision, s.idfi, s.idma, "
146  "f.idfi, f.tag, ma.idma, ma.matag from setup s "
147  "INNER JOIN field f ON s.idfi=f.idfi "
148  "INNER JOIN material ma ON s.idma=ma.idma "
149  "where s.stag='"
150  + setupTag + "' "
151  + (revision.empty() ? "" : " and s.revision=" + revision)
152  + " order by s.revision desc limit 1";
153 
154  TSQLResult* resQ = db.Query(query.c_str());
155  TSQLRow* row = resQ ? resQ->Next() : nullptr;
156  if (resQ == nullptr || row == nullptr || resQ->GetRowCount() == 0)
157  LOG(fatal) << "Geometry DB: setup not found for tag: " << setupTag
158  << " revision: " << (revision.size() ? revision : "latest");
159 
160  if (resQ->GetRowCount() > 1)
161  LOG(fatal) << "Geometry DB: found more than one setup with tag " << setupTag
162  << " revision: " << (revision.size() ? revision : "latest");
163 
164  CbmGeoSetup setup;
165  setup.SetId(std::atoi(row->GetField(0)));
166  setup.SetName(row->GetField(1));
167  setup.SetDate(row->GetField(2));
168  setup.SetDescription(row->GetField(3));
169  setup.SetAuthor(row->GetField(4));
170  setup.SetTag(setupTag);
171  setup.SetRevision(revision);
172 
173  setup.GetField().SetTag(row->GetField(9));
174  setup.GetMedia().SetTag(row->GetField(11));
175 
176  delete resQ;
177  delete row;
178  db.Close();
179 
180  return setup;
181 };
182 
184  std::string tag) {
185  TSQLiteServer db(LocalDbPath().c_str());
186 
187  std::string query = std::string(
188  "select sm.idsm, sm.smtag, sm.descr, sm.author, sm.smdate, sm.idf, "
189  "sm.r11, sm.r12, sm.r13,"
190  "sm.r21, sm.r22, sm.r23,"
191  "sm.r31, sm.r32, sm.r33,"
192  "sm.tx, sm.ty, sm.tz,"
193  "sm.sx, sm.sy, sm.sz,"
194  "sm.idf, f.idf, f.url, m.mname, m.idm "
195  "from setupmodule sm "
196  "inner join file f on sm.idf=f.idf "
197  "inner join module m on f.idm=m.idm "
198  "where sm.smtag='"
199  + tag + "' and f.idm=" + std::to_string(static_cast<int>(moduleId)));
200  TSQLResult* resQ = db.Query(query.c_str());
201  if (resQ == 0 || resQ->GetRowCount() == 0) return {};
202 
203  if (resQ->GetRowCount() > 1)
204  LOG(fatal) << "Geometry DB: found more than one entry for moduleId "
205  << moduleId << " and tag " << tag;
206 
207  TSQLRow* row = resQ->Next();
208 
209  CbmGeoSetupModule module;
210  module.SetModuleId(moduleId);
211  module.SetId(std::atoi(row->GetField(0)));
212  module.SetTag(row->GetField(1));
213  module.SetName(row->GetField(24));
214  module.SetDescription(row->GetField(2));
215  module.SetAuthor(row->GetField(3));
216  module.SetDate(row->GetField(4));
217  Double_t rot[9] = {std::atof(row->GetField(6)),
218  std::atof(row->GetField(7)),
219  std::atof(row->GetField(8)),
220  std::atof(row->GetField(9)),
221  std::atof(row->GetField(10)),
222  std::atof(row->GetField(11)),
223  std::atof(row->GetField(12)),
224  std::atof(row->GetField(13)),
225  std::atof(row->GetField(14))};
226  module.GetMatrix().SetRotation(rot);
227  Double_t trans[3] = {std::atof(row->GetField(15)),
228  std::atof(row->GetField(16)),
229  std::atof(row->GetField(17))};
230  module.GetMatrix().SetTranslation(trans);
231  Double_t scale[3] = {std::atof(row->GetField(18)),
232  std::atof(row->GetField(19)),
233  std::atof(row->GetField(20))};
234  module.GetMatrix().SetScale(scale);
235  module.SetFilePath(DbGeoDir() + row->GetField(23));
236  module.SetActive(kTRUE);
237 
238  delete resQ;
239  delete row;
240  db.Close();
241 
242  return module;
243 };
244 
246  TSQLiteServer db(LocalDbPath().c_str());
247 
248  std::string query =
249  std::string("select fi.idfi, fi.tag, fi.date, fi.desc, fi.author, "
250  "fi.scale, fi.x, fi.y, fi.z, fi.url from field fi "
251  "where fi.tag='"
252  + tag + "'");
253 
254  TSQLResult* resQ = db.Query(query.c_str());
255  if (resQ == 0 || resQ->GetRowCount() == 0) return {};
256 
257  if (resQ->GetRowCount() > 1)
258  LOG(fatal) << "Geometry DB: found more than one field with tag " << tag;
259 
260  TSQLRow* row = resQ->Next();
261 
262  CbmGeoSetupField field;
263  field.SetId(std::atoi(row->GetField(0)));
264  field.SetTag(row->GetField(1));
265  field.SetDate(row->GetField(2));
266  field.SetDescription(row->GetField(3));
267  field.SetAuthor(row->GetField(4));
268  field.SetScale(std::atof(row->GetField(5)));
269  field.GetMatrix().SetTranslation(std::atof(row->GetField(6)),
270  std::atof(row->GetField(7)),
271  std::atof(row->GetField(8)));
272  std::string url = row->GetField(9);
273 
274  // if url field is empty use the conventional field file name building from tag
275  if (url.empty()) { url = std::string("field_") + row->GetField(1) + ".root"; }
276  field.SetFilePath(FieldDir() + url);
277 
278  delete resQ;
279  delete row;
280  db.Close();
281 
282  return field;
283 };
284 
286  TSQLiteServer db(LocalDbPath().c_str());
287 
288  std::string query = std::string("select ma.idma, ma.matag, ma.madata, "
289  "ma.desc, ma.author,ma.url "
290  "from material ma "
291  "where ma.matag='"
292  + tag + "'");
293  TSQLResult* resQ = db.Query(query.c_str());
294  if (resQ == 0 || resQ->GetRowCount() == 0) return {};
295 
296  if (resQ->GetRowCount() > 1)
297  LOG(fatal) << "Geometry DB: found more than one material with tag " << tag;
298 
299  TSQLRow* row = resQ->Next();
300 
301  CbmGeoSetupMedia media;
302  media.SetId(std::atoi(row->GetField(0)));
303  media.SetTag(row->GetField(1));
304  media.SetDate(row->GetField(2));
305  media.SetDescription(row->GetField(3));
306  media.SetAuthor(row->GetField(4));
307  media.SetFilePath(DbGeoDir() + row->GetField(5));
308 
309  delete resQ;
310  delete row;
311  db.Close();
312 
313  return media;
314 };
315 
316 void CbmGeoSetupDbProvider::LoadSetup(std::string setupTag,
317  std::string revision) {
318  if (fSetup.GetModuleMap().size()) {
319  LOG(warn) << "-W- LoadSetup " << setupTag << ": overwriting existing setup "
320  << fSetup.GetTag();
321  }
322 
323  CbmGeoSetup setup = GetSetupByTag(setupTag, revision);
324 
325  std::map<ECbmModuleId, CbmGeoSetupModule> moduleMap;
326  std::map<Int_t, std::string> moduleTags = GetSetupModuleTagMap(setup.GetId());
327  for (auto module : moduleTags) {
328  for (auto moduleId : moduleMap) {
329  if (static_cast<int>(moduleId.first) == module.first) {
330  moduleMap.at(dbToCbmModuleIdMap[module.first]) =
331  GetModuleByTag(moduleId.first, module.second);
332  break;
333  }
334  }
335  }
336 
337  if (moduleMap.find(ECbmModuleId::kCave) == moduleMap.end()) {
339  }
340 
341  setup.SetModuleMap(moduleMap);
342  setup.SetField(GetFieldByTag(setup.GetField().GetTag()));
343  setup.SetMedia(GetMediaByTag(setup.GetMedia().GetTag()));
344 
345  fSetup = setup;
346 }
CbmGeoSetupField::GetMatrix
TGeoTranslation & GetMatrix()
Definition: CbmGeoSetupField.h:31
CbmGeoSetupProvider::fSetup
CbmGeoSetup fSetup
Definition: CbmGeoSetupProvider.h:90
ClassImp
ClassImp(CbmGeoSetupDbProvider)
CbmGeoSetupDbProvider::GetFieldTags
virtual std::vector< std::string > GetFieldTags()
Abstract method to get the list of field tags.
Definition: CbmGeoSetupDbProvider.cxx:121
CbmGeoSetupField::SetDescription
void SetDescription(std::string value)
Definition: CbmGeoSetupField.h:37
ECbmModuleId::kMagnet
@ kMagnet
Magnet.
CbmGeoSetup::SetField
void SetField(CbmGeoSetupField value)
Definition: CbmGeoSetup.h:55
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
CbmGeoSetupMedia::SetId
void SetId(Int_t value)
Definition: CbmGeoSetupMedia.h:29
ECbmModuleId::kMvd
@ kMvd
Micro-Vertex Detector.
ECbmModuleId
ECbmModuleId
Definition: CbmDefs.h:33
CbmGeoSetupDbProvider::GetMediaTags
virtual std::vector< std::string > GetMediaTags()
Abstract method to get the list of media tags.
Definition: CbmGeoSetupDbProvider.cxx:125
CbmGeoSetup::GetMedia
CbmGeoSetupMedia & GetMedia()
Definition: CbmGeoSetup.h:43
CbmGeoSetupDbProvider::LoadSetup
virtual void LoadSetup(std::string setupTag, std::string revision="")
Abstract method to load the setup with a tag and revision version.
Definition: CbmGeoSetupDbProvider.cxx:316
ECbmModuleId::kTof
@ kTof
Time-of-flight Detector.
CbmGeoSetupModule::SetAuthor
void SetAuthor(std::string value)
Definition: CbmGeoSetupModule.h:40
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::SetId
void SetId(Int_t value)
Definition: CbmGeoSetupField.h:33
CbmGeoSetup::SetId
void SetId(Int_t value)
Definition: CbmGeoSetup.h:45
CbmGeoSetupDbProvider.h
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.
CbmGeoSetupDbProvider
Setup provider with database functionality.
Definition: CbmGeoSetupDbProvider.h:21
CbmGeoSetupModule::GetMatrix
TGeoHMatrix & GetMatrix()
Definition: CbmGeoSetupModule.h:33
CbmGeoSetupField::GetTag
std::string GetTag()
Definition: CbmGeoSetupField.h:24
CbmGeoSetupMedia::SetDate
void SetDate(std::string value)
Definition: CbmGeoSetupMedia.h:32
CbmGeoSetup::SetMedia
void SetMedia(CbmGeoSetupMedia value)
Definition: CbmGeoSetup.h:56
CbmGeoSetupField::SetScale
void SetScale(Double_t value)
Definition: CbmGeoSetupField.h:40
ECbmModuleId::kRich
@ kRich
Ring-Imaging Cherenkov Detector.
CbmGeoSetupDbProvider::GetFieldByTag
virtual CbmGeoSetupField GetFieldByTag(std::string tag)
Abstract method for constructing the field by tag.
Definition: CbmGeoSetupDbProvider.cxx:245
CbmGeoSetupModule::SetDescription
void SetDescription(std::string value)
Definition: CbmGeoSetupModule.h:42
CbmGeoSetupModule::SetId
void SetId(Int_t value)
Definition: CbmGeoSetupModule.h:36
CbmGeoSetupDbProvider::GetModuleByTag
virtual CbmGeoSetupModule GetModuleByTag(ECbmModuleId moduleId, std::string tag)
Abstract method for constructing the module by id and tag.
Definition: CbmGeoSetupDbProvider.cxx:183
CbmGeoSetupField::SetAuthor
void SetAuthor(std::string value)
Definition: CbmGeoSetupField.h:35
CbmGeoSetupField::SetTag
void SetTag(std::string value)
Definition: CbmGeoSetupField.h:34
CbmGeoSetupMedia::SetDescription
void SetDescription(std::string value)
Definition: CbmGeoSetupMedia.h:33
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
CbmGeoSetupModule::SetModuleId
void SetModuleId(ECbmModuleId value)
Definition: CbmGeoSetupModule.h:37
CbmGeoSetupModule::SetFilePath
void SetFilePath(std::string value)
Definition: CbmGeoSetupModule.h:43
CbmGeoSetupMedia::SetAuthor
void SetAuthor(std::string value)
Definition: CbmGeoSetupMedia.h:31
pos
TVector3 pos
Definition: CbmMvdSensorDigiToHitTask.cxx:60
CbmGeoSetupDbProvider::GetSetupByTag
virtual CbmGeoSetup GetSetupByTag(std::string setupTag, std::string revision)
Abstract method for constructing the setup by id and tag.
Definition: CbmGeoSetupDbProvider.cxx:130
CbmGeoSetup::GetField
CbmGeoSetupField & GetField()
Definition: CbmGeoSetup.h:42
CbmGeoSetupMedia::GetTag
std::string GetTag()
Definition: CbmGeoSetupMedia.h:22
CbmGeoSetupModule
Definition: CbmGeoSetupModule.h:22
ECbmModuleId::kMuch
@ kMuch
Muon detection system.
ECbmModuleId::kPsd
@ kPsd
Projectile spectator detector.
ECbmModuleId::kPlatform
@ kPlatform
RICH rail platform.
CbmGeoSetup::GetId
Int_t GetId()
Definition: CbmGeoSetup.h:32
CbmGeoSetupModule::SetDate
void SetDate(std::string value)
Definition: CbmGeoSetupModule.h:41
CbmGeoSetupDbProvider::GetSetupTags
virtual std::vector< std::string > GetSetupTags()
Abstract method to get the list of setup tags.
Definition: CbmGeoSetupDbProvider.cxx:101
ECbmModuleId::kSts
@ kSts
Silicon Tracking System.
CbmGeoSetupMedia::SetFilePath
void SetFilePath(std::string value)
Definition: CbmGeoSetupMedia.h:34
CbmGeoSetupField::SetDate
void SetDate(std::string value)
Definition: CbmGeoSetupField.h:36
CbmGeoSetup::GetModuleMap
std::map< ECbmModuleId, CbmGeoSetupModule > & GetModuleMap()
Definition: CbmGeoSetup.h:39
CbmGeoSetup::SetModuleMap
void SetModuleMap(std::map< ECbmModuleId, CbmGeoSetupModule > value)
Definition: CbmGeoSetup.h:52
CbmGeoSetupDbProvider::GetMediaByTag
virtual CbmGeoSetupMedia GetMediaByTag(std::string tag)
Abstract method for constructing the media by tag.
Definition: CbmGeoSetupDbProvider.cxx:285
CbmDefs.h