16#include "TSQLResult.h"
18#include "TSQLiteServer.h"
27 std::map<Int_t, ECbmModuleId> dbToCbmModuleIdMap = {
36 std::string FieldDir() {
return "input/"; }
39 std::string DbGeoDir() {
return "db/"; }
42 std::string LocalDbPath()
44 std::string path = std::string(gSystem->Getenv(
"VMCWORKDIR")) +
"/geometry/db/local.db";
45 if (gSystem->AccessPathName(path.c_str()) == kTRUE)
46 LOG(fatal) <<
"Geometry DB file does not exist: " << path;
47 return "sqlite://" + path;
51 std::map<Int_t, std::string> GetSetupModuleTagMap(Int_t setupId)
53 TSQLiteServer db(LocalDbPath().c_str());
55 std::string query = std::string()
56 +
"select sms.idsd, sms.idsm, sm.idf, sm.smtag, f.idm "
58 "inner join setupmodule sm on sms.idsm=sm.idsm "
59 "inner join file f on sm.idf=f.idf "
61 + std::to_string(setupId);
62 TSQLResult* resQ = db.Query(query.c_str());
63 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
65 std::map<Int_t, std::string> result;
67 while ((row = resQ->Next()) != 0) {
68 result[std::atoi(row->GetField(4))] = row->GetField(3);
78 std::vector<std::string> ListTable(std::string table, std::string column)
80 TSQLiteServer db(LocalDbPath().c_str());
82 std::string query = std::string(
"select ") + column +
" from " + table;
83 TSQLResult* resQ = db.Query(query.c_str());
84 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
86 std::vector<std::string> result;
88 while ((row = resQ->Next()) != 0) {
89 result.push_back(row->GetField(0));
101 TSQLiteServer db(LocalDbPath().c_str());
103 std::string query = std::string(
"select stag,revision from setup order by revision desc");
104 TSQLResult* resQ = db.Query(query.c_str());
105 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
107 std::vector<std::string> result;
109 while ((row = resQ->Next()) != 0) {
110 result.push_back(std::string(row->GetField(0)) +
"@" + row->GetField(1));
126 if (revision.empty() && setupTag.find(
"@") != std::string::npos) {
127 auto pos = setupTag.find(
"@");
128 revision = setupTag.substr(
pos + 1, setupTag.size() -
pos - 1);
129 setupTag = setupTag.substr(0,
pos);
132 LOG(info) <<
"Loading CbmGeoSetup from geometry database.\nSetup tag: " << setupTag
133 <<
" Revision: " << (revision.size() ? revision :
"latest");
135 std::string path = LocalDbPath();
136 TSQLiteServer db(LocalDbPath().c_str());
137 std::string query =
"select s.idsd, s.stag, s.sdate, s.desc, "
138 "s.author, s.revision, s.idfi, s.idma, "
139 "f.idfi, f.tag, ma.idma, ma.matag from setup s "
140 "INNER JOIN field f ON s.idfi=f.idfi "
141 "INNER JOIN material ma ON s.idma=ma.idma "
143 + setupTag +
"' " + (revision.empty() ?
"" :
" and s.revision=" + revision)
144 +
" order by s.revision desc limit 1";
146 TSQLResult* resQ = db.Query(query.c_str());
147 TSQLRow* row = resQ ? resQ->Next() :
nullptr;
148 if (resQ ==
nullptr || row ==
nullptr || resQ->GetRowCount() == 0)
149 LOG(fatal) <<
"Geometry DB: setup not found for tag: " << setupTag
150 <<
" revision: " << (revision.size() ? revision :
"latest");
152 if (resQ->GetRowCount() > 1)
153 LOG(fatal) <<
"Geometry DB: found more than one setup with tag " << setupTag
154 <<
" revision: " << (revision.size() ? revision :
"latest");
157 setup.
SetId(std::atoi(row->GetField(0)));
158 setup.
SetName(row->GetField(1));
159 setup.
SetDate(row->GetField(2));
177 TSQLiteServer db(LocalDbPath().c_str());
179 std::string query = std::string(
"select sm.idsm, sm.smtag, sm.descr, sm.author, sm.smdate, sm.idf, "
180 "sm.r11, sm.r12, sm.r13,"
181 "sm.r21, sm.r22, sm.r23,"
182 "sm.r31, sm.r32, sm.r33,"
183 "sm.tx, sm.ty, sm.tz,"
184 "sm.sx, sm.sy, sm.sz,"
185 "sm.idf, f.idf, f.url, m.mname, m.idm "
186 "from setupmodule sm "
187 "inner join file f on sm.idf=f.idf "
188 "inner join module m on f.idm=m.idm "
190 + tag +
"' and f.idm=" + std::to_string(
static_cast<int>(moduleId)));
191 TSQLResult* resQ = db.Query(query.c_str());
192 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
194 if (resQ->GetRowCount() > 1)
195 LOG(fatal) <<
"Geometry DB: found more than one entry for moduleId " << moduleId <<
" and tag " << tag;
197 TSQLRow* row = resQ->Next();
200 module.SetModuleId(moduleId);
201 module.SetId(std::atoi(row->GetField(0)));
202 module.SetTag(row->GetField(1));
203 module.SetName(row->GetField(24));
204 module.SetDescription(row->GetField(2));
205 module.SetAuthor(row->GetField(3));
206 module.SetDate(row->GetField(4));
207 Double_t rot[9] = {std::atof(row->GetField(6)), std::atof(row->GetField(7)), std::atof(row->GetField(8)),
208 std::atof(row->GetField(9)), std::atof(row->GetField(10)), std::atof(row->GetField(11)),
209 std::atof(row->GetField(12)), std::atof(row->GetField(13)), std::atof(row->GetField(14))};
210 module.GetMatrix().SetRotation(rot);
211 Double_t trans[3] = {std::atof(row->GetField(15)), std::atof(row->GetField(16)), std::atof(row->GetField(17))};
212 module.GetMatrix().SetTranslation(trans);
213 Double_t scale[3] = {std::atof(row->GetField(18)), std::atof(row->GetField(19)), std::atof(row->GetField(20))};
214 module.GetMatrix().SetScale(scale);
215 module.SetFilePath(DbGeoDir() + row->GetField(23));
216 module.SetActive(kTRUE);
227 TSQLiteServer db(LocalDbPath().c_str());
229 std::string query = std::string(
"select fi.idfi, fi.tag, fi.date, fi.desc, fi.author, "
230 "fi.scale, fi.x, fi.y, fi.z, fi.url from field fi "
234 TSQLResult* resQ = db.Query(query.c_str());
235 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
237 if (resQ->GetRowCount() > 1) LOG(fatal) <<
"Geometry DB: found more than one field with tag " << tag;
239 TSQLRow* row = resQ->Next();
242 field.
SetId(std::atoi(row->GetField(0)));
243 field.
SetTag(row->GetField(1));
244 field.
SetDate(row->GetField(2));
247 field.
SetScale(std::atof(row->GetField(5)));
248 field.
GetMatrix().SetTranslation(std::atof(row->GetField(6)), std::atof(row->GetField(7)),
249 std::atof(row->GetField(8)));
250 std::string url = row->GetField(9);
253 if (url.empty()) { url = std::string(
"field_") + row->GetField(1) +
".root"; }
265 TSQLiteServer db(LocalDbPath().c_str());
267 std::string query = std::string(
"select ma.idma, ma.matag, ma.madata, "
268 "ma.desc, ma.author,ma.url "
272 TSQLResult* resQ = db.Query(query.c_str());
273 if (resQ == 0 || resQ->GetRowCount() == 0)
return {};
275 if (resQ->GetRowCount() > 1) LOG(fatal) <<
"Geometry DB: found more than one material with tag " << tag;
277 TSQLRow* row = resQ->Next();
280 media.
SetId(std::atoi(row->GetField(0)));
281 media.
SetTag(row->GetField(1));
282 media.
SetDate(row->GetField(2));
297 LOG(warn) <<
"-W- LoadSetup " << setupTag <<
": overwriting existing setup " <<
fSetup.
GetTag();
302 std::map<ECbmModuleId, CbmGeoSetupModule> moduleMap;
303 std::map<Int_t, std::string> moduleTags = GetSetupModuleTagMap(setup.
GetId());
304 for (
auto module : moduleTags) {
305 for (
auto moduleId : moduleMap) {
306 if (
static_cast<int>(moduleId.first) == module.first) {
307 moduleMap.at(dbToCbmModuleIdMap[module.first]) =
GetModuleByTag(moduleId.first, module.second);
@ kMvd
Micro-Vertex Detector.
@ kHodo
Hodoscope (for test beam times)
@ kTrd
Transition Radiation Detector.
@ kShield
Beam pipe shielding in MUCH section.
@ kTof
Time-of-flight Detector.
@ 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(CbmGeoSetupDbProvider)
Setup provider with database functionality.
virtual CbmGeoSetup GetSetupByTag(std::string setupTag, std::string revision)
Abstract method for constructing the setup by id and tag.
virtual CbmGeoSetupModule GetModuleByTag(ECbmModuleId moduleId, std::string tag)
Abstract method for constructing the module by id and tag.
virtual void LoadSetup(std::string setupTag, std::string revision="")
Abstract method to load the setup with a tag and revision version.
virtual CbmGeoSetupMedia GetMediaByTag(std::string tag)
Abstract method for constructing the media by 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 std::vector< std::string > GetMediaTags()
Abstract method to get the list of media tags.
virtual std::vector< std::string > GetFieldTags()
Abstract method to get the list of field tags.
TGeoTranslation & GetMatrix()
void SetDescription(std::string value)
void SetDate(std::string value)
void SetAuthor(std::string value)
void SetScale(Double_t value)
void SetFilePath(std::string value)
void SetTag(std::string value)
CbmGeoSetupModule GetDefaultCaveModule()
Gets defauk cave if none was provided by the other means.
Data transfer object to represent the CBM Detector setup.
void SetDate(std::string value)
void SetAuthor(std::string value)
void SetName(std::string value)
void SetField(CbmGeoSetupField value)
CbmGeoSetupField & GetField()
CbmGeoSetupMedia & GetMedia()
void SetTag(std::string value)
void SetRevision(std::string value)
void SetDescription(std::string value)
void SetMedia(CbmGeoSetupMedia value)
void SetModuleMap(std::map< ECbmModuleId, CbmGeoSetupModule > value)
std::map< ECbmModuleId, CbmGeoSetupModule > & GetModuleMap()