10 #include "TSQLResult.h"
12 #include "TSQLiteServer.h"
14 #include "FairLogger.h"
22 std::map<Int_t, ECbmModuleId> dbToCbmModuleIdMap = {
40 std::string FieldDir() {
return "input/"; }
43 std::string DbGeoDir() {
return "db/"; }
46 std::string LocalDbPath() {
48 std::string(gSystem->Getenv(
"VMCWORKDIR")) +
"/geometry/db/local.db";
49 if (gSystem->AccessPathName(path.c_str()) == kTRUE)
50 LOG(fatal) <<
"Geometry DB file does not exist: " << path;
51 return "sqlite://" + path;
55 std::map<Int_t, std::string> GetSetupModuleTagMap(Int_t setupId) {
56 TSQLiteServer db(LocalDbPath().c_str());
58 std::string query = std::string()
59 +
"select sms.idsd, sms.idsm, sm.idf, sm.smtag, f.idm "
61 "inner join setupmodule sm on sms.idsm=sm.idsm "
62 "inner join file f on sm.idf=f.idf "
64 + std::to_string(setupId);
65 TSQLResult* resQ = db.Query(query.c_str());
66 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
68 std::map<Int_t, std::string> result;
70 while ((row = resQ->Next()) != 0) {
71 result[std::atoi(row->GetField(4))] = row->GetField(3);
81 std::vector<std::string> ListTable(std::string table, std::string column) {
82 TSQLiteServer db(LocalDbPath().c_str());
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 {};
88 std::vector<std::string> result;
90 while ((row = resQ->Next()) != 0) {
91 result.push_back(row->GetField(0));
102 TSQLiteServer db(LocalDbPath().c_str());
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 {};
109 std::vector<std::string> result;
111 while ((row = resQ->Next()) != 0) {
112 result.push_back(std::string(row->GetField(0)) +
"@" + row->GetField(1));
122 return ListTable(
"field",
"tag");
126 return ListTable(
"material",
"matag");
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);
138 LOG(info) <<
"Loading CbmGeoSetup from geometry database.\nSetup tag: "
140 <<
" Revision: " << (revision.size() ? revision :
"latest");
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 "
151 + (revision.empty() ?
"" :
" and s.revision=" + revision)
152 +
" order by s.revision desc limit 1";
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");
160 if (resQ->GetRowCount() > 1)
161 LOG(fatal) <<
"Geometry DB: found more than one setup with tag " << setupTag
162 <<
" revision: " << (revision.size() ? revision :
"latest");
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);
173 setup.GetField().SetTag(row->GetField(9));
174 setup.GetMedia().SetTag(row->GetField(11));
185 TSQLiteServer db(LocalDbPath().c_str());
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 "
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 {};
203 if (resQ->GetRowCount() > 1)
204 LOG(fatal) <<
"Geometry DB: found more than one entry for moduleId "
205 << moduleId <<
" and tag " << tag;
207 TSQLRow* row = resQ->Next();
211 module.
SetId(std::atoi(row->GetField(0)));
212 module.
SetTag(row->GetField(1));
213 module.
SetName(row->GetField(24));
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))};
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))};
235 module.
SetFilePath(DbGeoDir() + row->GetField(23));
246 TSQLiteServer db(LocalDbPath().c_str());
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 "
254 TSQLResult* resQ = db.Query(query.c_str());
255 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
257 if (resQ->GetRowCount() > 1)
258 LOG(fatal) <<
"Geometry DB: found more than one field with tag " << tag;
260 TSQLRow* row = resQ->Next();
263 field.
SetId(std::atoi(row->GetField(0)));
264 field.
SetTag(row->GetField(1));
265 field.
SetDate(row->GetField(2));
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);
275 if (url.empty()) { url = std::string(
"field_") + row->GetField(1) +
".root"; }
286 TSQLiteServer db(LocalDbPath().c_str());
288 std::string query = std::string(
"select ma.idma, ma.matag, ma.madata, "
289 "ma.desc, ma.author,ma.url "
293 TSQLResult* resQ = db.Query(query.c_str());
294 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
296 if (resQ->GetRowCount() > 1)
297 LOG(fatal) <<
"Geometry DB: found more than one material with tag " << tag;
299 TSQLRow* row = resQ->Next();
302 media.
SetId(std::atoi(row->GetField(0)));
303 media.
SetTag(row->GetField(1));
304 media.
SetDate(row->GetField(2));
317 std::string revision) {
319 LOG(warn) <<
"-W- LoadSetup " << setupTag <<
": overwriting existing setup "
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]) =