CbmRoot
Loading...
Searching...
No Matches
CbmGeoSetupDbProvider.cxx
Go to the documentation of this file.
1/* Copyright (C) 2019-2020 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 "TSQLResult.h"
17#include "TSQLRow.h"
18#include "TSQLiteServer.h"
19#include "TSystem.h"
20
21#include <iostream>
22
24
25namespace
26{ //anonymous namespace with helpers
27 std::map<Int_t, ECbmModuleId> dbToCbmModuleIdMap = {
33 {16384, ECbmModuleId::kBmon}};
34
36 std::string FieldDir() { return "input/"; }
37
39 std::string DbGeoDir() { return "db/"; }
40
42 std::string LocalDbPath()
43 {
44 std::string path = std::string(gSystem->Getenv("VMCWORKDIR")) + "/geometry/db/local.db";
45 if (gSystem->AccessPathName(path.c_str()) == kTRUE) // does not exist
46 LOG(fatal) << "Geometry DB file does not exist: " << path;
47 return "sqlite://" + path;
48 };
49
51 std::map<Int_t, std::string> GetSetupModuleTagMap(Int_t setupId)
52 {
53 TSQLiteServer db(LocalDbPath().c_str());
54
55 std::string query = std::string()
56 + "select sms.idsd, sms.idsm, sm.idf, sm.smtag, f.idm "
57 "from sms sms "
58 "inner join setupmodule sm on sms.idsm=sm.idsm "
59 "inner join file f on sm.idf=f.idf "
60 "where sms.idsd="
61 + std::to_string(setupId);
62 TSQLResult* resQ = db.Query(query.c_str());
63 if (resQ == 0 || resQ->GetRowCount() == 0) return {};
64
65 std::map<Int_t, std::string> result;
66 TSQLRow* row;
67 while ((row = resQ->Next()) != 0) {
68 result[std::atoi(row->GetField(4))] = row->GetField(3);
69 }
70 delete resQ;
71 delete row;
72 db.Close();
73
74 return result;
75 }
76
78 std::vector<std::string> ListTable(std::string table, std::string column)
79 {
80 TSQLiteServer db(LocalDbPath().c_str());
81
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 {};
85
86 std::vector<std::string> result;
87 TSQLRow* row;
88 while ((row = resQ->Next()) != 0) {
89 result.push_back(row->GetField(0));
90 }
91 delete resQ;
92 delete row;
93 db.Close();
94
95 return result;
96 }
97} // end anonymous namespace
98
99std::vector<std::string> CbmGeoSetupDbProvider::GetSetupTags()
100{
101 TSQLiteServer db(LocalDbPath().c_str());
102
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 {};
106
107 std::vector<std::string> result;
108 TSQLRow* row;
109 while ((row = resQ->Next()) != 0) {
110 result.push_back(std::string(row->GetField(0)) + "@" + row->GetField(1));
111 }
112 delete resQ;
113 delete row;
114 db.Close();
115
116 return result;
117}
118
119std::vector<std::string> CbmGeoSetupDbProvider::GetFieldTags() { return ListTable("field", "tag"); };
120
121std::vector<std::string> CbmGeoSetupDbProvider::GetMediaTags() { return ListTable("material", "matag"); };
122
123// Get bare setup without field, media and modules loaded
124CbmGeoSetup CbmGeoSetupDbProvider::GetSetupByTag(std::string setupTag, std::string revision)
125{
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);
130 }
131
132 LOG(info) << "Loading CbmGeoSetup from geometry database.\nSetup tag: " << setupTag
133 << " Revision: " << (revision.size() ? revision : "latest");
134
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 "
142 "where s.stag='"
143 + setupTag + "' " + (revision.empty() ? "" : " and s.revision=" + revision)
144 + " order by s.revision desc limit 1";
145
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");
151
152 if (resQ->GetRowCount() > 1)
153 LOG(fatal) << "Geometry DB: found more than one setup with tag " << setupTag
154 << " revision: " << (revision.size() ? revision : "latest");
155
156 CbmGeoSetup setup;
157 setup.SetId(std::atoi(row->GetField(0)));
158 setup.SetName(row->GetField(1));
159 setup.SetDate(row->GetField(2));
160 setup.SetDescription(row->GetField(3));
161 setup.SetAuthor(row->GetField(4));
162 setup.SetTag(setupTag);
163 setup.SetRevision(revision);
164
165 setup.GetField().SetTag(row->GetField(9));
166 setup.GetMedia().SetTag(row->GetField(11));
167
168 delete resQ;
169 delete row;
170 db.Close();
171
172 return setup;
173};
174
176{
177 TSQLiteServer db(LocalDbPath().c_str());
178
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 "
189 "where sm.smtag='"
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 {};
193
194 if (resQ->GetRowCount() > 1)
195 LOG(fatal) << "Geometry DB: found more than one entry for moduleId " << moduleId << " and tag " << tag;
196
197 TSQLRow* row = resQ->Next();
198
199 CbmGeoSetupModule module;
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);
217
218 delete resQ;
219 delete row;
220 db.Close();
221
222 return module;
223};
224
226{
227 TSQLiteServer db(LocalDbPath().c_str());
228
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 "
231 "where fi.tag='"
232 + tag + "'");
233
234 TSQLResult* resQ = db.Query(query.c_str());
235 if (resQ == 0 || resQ->GetRowCount() == 0) return {};
236
237 if (resQ->GetRowCount() > 1) LOG(fatal) << "Geometry DB: found more than one field with tag " << tag;
238
239 TSQLRow* row = resQ->Next();
240
241 CbmGeoSetupField field;
242 field.SetId(std::atoi(row->GetField(0)));
243 field.SetTag(row->GetField(1));
244 field.SetDate(row->GetField(2));
245 field.SetDescription(row->GetField(3));
246 field.SetAuthor(row->GetField(4));
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);
251
252 // if url field is empty use the conventional field file name building from tag
253 if (url.empty()) { url = std::string("field_") + row->GetField(1) + ".root"; }
254 field.SetFilePath(FieldDir() + url);
255
256 delete resQ;
257 delete row;
258 db.Close();
259
260 return field;
261};
262
264{
265 TSQLiteServer db(LocalDbPath().c_str());
266
267 std::string query = std::string("select ma.idma, ma.matag, ma.madata, "
268 "ma.desc, ma.author,ma.url "
269 "from material ma "
270 "where ma.matag='"
271 + tag + "'");
272 TSQLResult* resQ = db.Query(query.c_str());
273 if (resQ == 0 || resQ->GetRowCount() == 0) return {};
274
275 if (resQ->GetRowCount() > 1) LOG(fatal) << "Geometry DB: found more than one material with tag " << tag;
276
277 TSQLRow* row = resQ->Next();
278
279 CbmGeoSetupMedia media;
280 media.SetId(std::atoi(row->GetField(0)));
281 media.SetTag(row->GetField(1));
282 media.SetDate(row->GetField(2));
283 media.SetDescription(row->GetField(3));
284 media.SetAuthor(row->GetField(4));
285 media.SetFilePath(DbGeoDir() + row->GetField(5));
286
287 delete resQ;
288 delete row;
289 db.Close();
290
291 return media;
292};
293
294void CbmGeoSetupDbProvider::LoadSetup(std::string setupTag, std::string revision)
295{
296 if (fSetup.GetModuleMap().size()) {
297 LOG(warn) << "-W- LoadSetup " << setupTag << ": overwriting existing setup " << fSetup.GetTag();
298 }
299
300 CbmGeoSetup setup = GetSetupByTag(setupTag, revision);
301
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);
308 break;
309 }
310 }
311 }
312
313 if (moduleMap.find(ECbmModuleId::kCave) == moduleMap.end()) {
315 }
316
317 setup.SetModuleMap(moduleMap);
318 setup.SetField(GetFieldByTag(setup.GetField().GetTag()));
319 setup.SetMedia(GetMediaByTag(setup.GetMedia().GetTag()));
320
321 fSetup = setup;
322}
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(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 SetId(Int_t value)
void SetTag(std::string value)
std::string GetTag()
std::string GetTag()
void SetTag(std::string value)
void SetAuthor(std::string value)
void SetId(Int_t value)
void SetDate(std::string value)
void SetDescription(std::string value)
void SetFilePath(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.
Definition CbmGeoSetup.h:34
void SetDate(std::string value)
Definition CbmGeoSetup.h:51
void SetAuthor(std::string value)
Definition CbmGeoSetup.h:50
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
void SetId(Int_t value)
Definition CbmGeoSetup.h:47
std::string GetTag()
Definition CbmGeoSetup.h:38
Int_t GetId()
Definition CbmGeoSetup.h:36
void SetTag(std::string value)
Definition CbmGeoSetup.h:49
void SetRevision(std::string value)
Definition CbmGeoSetup.h:52
void SetDescription(std::string value)
Definition CbmGeoSetup.h:53
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