36 return (access(name, F_OK) != -1);
41 return std::tolower(
static_cast<unsigned char>(a)) == std::tolower(
static_cast<unsigned char>(b));
46static int FieldToInt(
const char* field,
int defaultValue = 0)
51 long value = strtol(field, &end, 10);
52 return (end == field) ? defaultValue :
static_cast<int>(value);
55static uint64_t
FieldToUInt64(
const char* field, uint64_t defaultValue = 0)
60 unsigned long long value = strtoull(field, &end, 10);
61 return (end == field) ? defaultValue :
static_cast<uint64_t
>(value);
69 double value = strtod(field, &end);
70 return (end == field) ? defaultValue : value;
79 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
'_' && c !=
'.' && c !=
'-')
89 escaped.reserve(value.size());
90 for (
char c : value) {
105 if (value ==
"true" || value ==
"yes" || value ==
"1") {
109 if (value ==
"false" || value ==
"no" || value ==
"0") {
114 LOG(error) <<
"Wrong value of the '" << name <<
"' attribute: '" << value
115 <<
"', the allowed values are true, yes, 1, false, no, 0";
125 double number = strtod(value.
Data(), &end);
126 if (value.
IsNull() || *end !=
'\0') {
127 LOG(error) <<
"Wrong value of the '" << name <<
"' attribute: '" << value <<
"', a number is expected";
138 map<TString, setup_module_ptr>::iterator it = edits.find(key);
139 if (it == edits.end()) {
140 edits.insert(make_pair(key, sNode));
144 LOG(warning) <<
"The setup module '" << key <<
"' is customized more than once, the latest settings are used";
163 filesystem::remove(fileName, ec);
168static const char*
const kWgetOptions =
"--tries=1 --dns-timeout=10 --connect-timeout=10";
178 unsigned long int serverRev = 0;
182 std::ifstream myFile_Handler;
184 myFile_Handler.open(fileName.
Data());
185 if (myFile_Handler.is_open()) {
187 while (getline(myFile_Handler, myLine)) {
188 serverRev = strtoul(myLine.c_str(), &ptr, 10);
191 myFile_Handler.close();
192 std::remove(fileName.
Data());
207 LOG(error) <<
"No approved geometry setup found for the tag prefix: " << pattern;
212 std::pair<int, string> res = std::make_pair<int, string>(0,
"");
215 std::string pattern(pattern_tag);
217 LOG(error) <<
"ERROR: the setup tag pattern contains forbidden characters: `" << pattern <<
"`";
222 std::remove(fileName.
Data());
224 fileName.
Data(), url, pattern.c_str());
226 std::ifstream myFile_Handler;
228 myFile_Handler.open(fileName.
Data());
229 if (myFile_Handler.is_open()) {
230 getline(myFile_Handler, myLine);
231 myFile_Handler.close();
233 std::remove(fileName.
Data());
234 std::size_t found = myLine.find(
";");
235 if (found == std::string::npos) {
238 vector<string> results;
239 istringstream f(myLine);
241 while (getline(f, token,
':')) {
242 results.push_back(token);
244 if (results.size() != 3)
246 res = std::make_pair<int, string>(1, results[0].c_str());
257 fileName.
Data(), url));
258 std::ifstream myFile_Handler;
260 std::string sTag(setup_tag);
261 myFile_Handler.open(fileName.
Data());
262 if (myFile_Handler.is_open()) {
263 while (getline(myFile_Handler, myLine)) {
264 istringstream f(myLine);
266 while (getline(f, s,
';')) {
267 vector<string> results;
269 std::istringstream input;
271 while (getline(input, token,
':')) {
272 results.push_back(token);
274 if (results.size() < 2)
276 std::string tag = results[0];
277 std::string status = results[1];
279 if (results.size() > 2) {
283 bool bb = std::equal(tag.begin(), tag.end(), sTag.begin(), sTag.end(),
ichar_equals);
284 if (tag.size() == sTag.size() && bb) {
285 myFile_Handler.close();
286 std::remove(fileName.
Data());
287 return std::make_pair(
FieldToInt(status.c_str(), -1), upTime);
292 myFile_Handler.close();
293 std::remove(fileName.
Data());
295 return std::make_pair(retval, 0);
300 std::pair<int, string> res = std::make_pair<int, string>(0,
"");
302 std::ostringstream stringStream;
303 stringStream <<
"sqlite://" << localDbPath;
305 stringStream.str(
"");
307 stringStream <<
"SELECT setup_tag, status, creation_date FROM setup where status=1 and setup_tag like '"
308 <<
SqlEscape(pattern_tag) <<
"%' order by creation_date DESC limit 1;";
313 const char* name = row->
GetField(0);
315 res = std::make_pair<int, string>(1, name);
326 unsigned long int localRev = 0;
329 std::ostringstream stringStream;
330 stringStream <<
"sqlite://" << localDbPath;
332 stringStream.str(
"");
334 stringStream <<
"select value_field from config where key_field='config_time';";
340 const char* ch_time = row->
GetField(0);
343 localRev = strtoul(ch_time, &ptr, 10);
354 return (access(path, W_OK | X_OK) == 0);
379 const char* env_p = getenv(
"DBL_CACHE_PATH");
380 if (env_p !=
nullptr) {
382 if (
gSystem->ExpandPathName(baseDir)) {
383 LOG(error) <<
"Cannot expand the DBL_CACHE_PATH value: " << env_p;
387 const char* tmp_p = getenv(
"TMPDIR");
396 LOG(error) <<
"ERROR: cannot create the folder for the geometry database: " << dirName;
400 LOG(error) <<
"ERROR: the folder for the geometry database is not writable: " << dirName;
411 if (localDbPath ==
"") {
412 LOG(error) <<
"GEOMPATH is not set. Please, set GEOMPATH as a path to the storage of the Local Geometry "
413 "Database, the database file (local.db) is located in its parent directory"
420 bool isMovedDb =
false;
422 if (
gSystem->mkdir(dirName,
true) != 0) {
423 LOG(error) <<
"ERROR: cannot create folder: " << dirName << endl;
429 LOG(info) <<
"The directory of the Local Geometry Database is read-only: " << dirName
430 <<
". The database will be installed to a writable directory";
432 if (writableDir.
Length() == 0)
440 dirName = writableDir;
441 localDbPath = dirName +
"/local.db";
447 if (setup_tag.size() == 0)
448 command =
TString::Format(
"wget %s -O %s \"%s/download_file.php?type=all&tag=all\" > /dev/null 2>&1",
451 std::string tag(setup_tag);
453 LOG(error) <<
"ERROR: the setup tag contains forbidden characters: `" << tag <<
"`";
456 command =
TString::Format(
"wget %s -O %s \"%s/download_file.php?type=s&tag=%s&rev=1\" > /dev/null 2>&1",
460 int wgetStatus =
gSystem->Exec(command);
462 LOG(error) <<
"ERROR: downloading the geometry database file failed (wget exit code " << wgetStatus / 256
463 <<
"), please, check url: " << url << endl;
469 if (file_stat.
fSize == 0) {
470 LOG(error) <<
"ERROR: downloaded geometry database file has 0 size, please, check url: " << url << endl;
477 LOG(error) <<
"ERROR: the downloaded geometry database file is not a valid archive (an incomplete download or "
478 "an error page of the server), please, check url: "
488 if (tarResult != 0) {
489 LOG(error) <<
"ERROR: cannot unpack the geometry database archive to the directory: " << dirName << endl;
494 LOG(error) <<
"ERROR: no database file found after unpacking the archive: " << localDbPath << endl;
502 previousGeomPath = getenv(
"GEOMPATH");
503 gSystem->Setenv(
"GEOMPATH", (dirName +
"/storage/").Data());
507 gSystem->Setenv(
"GEOMPATH", previousGeomPath.
Data());
511 LOG(info) <<
"GEOMPATH is changed to the storage of the installed database: " <<
GetRootPath();
512 LOG(info) <<
"The Local Geometry Database was successfully installed to the directory: " << dirName << endl;
523 if (
m_db !=
nullptr) {
541 if (fPath.size() == 0) {
542 LOG(error) <<
"Please, set GEOMPATH environment variable (the storage of the Local Geometry Database) before "
543 "using the Geometry Database";
547 LOG(error) <<
"No Database found in " << fPath;
550 ostringstream stringStream;
551 stringStream <<
"sqlite://" << fPath;
553 LOG(debug1) <<
"Connecting the Local Geometry Database at path: " << stringStream.str().c_str();
559 LOG(error) <<
"Cannot load detector geometry, please, check GEOMPATH environment variable";
563 LOG(error) <<
"Cannot load detector geometry, please, check GEOMPATH environment variable";
576 int removedCount = 0;
578 if (volume->IsAssembly() && shapes->
Remove(volume->GetShape()) !=
nullptr)
582 if (removedCount > 0) {
584 LOG(info) << removedCount <<
" assembly shapes were removed from the list of shapes of the geometry";
595 if (setup_tag.empty()) {
596 LOG(error) <<
"The tag of the geometry setup is not set";
603 LOG(error) <<
"Please, set GEOMPATH environment variable (the storage of the Local Geometry Database) before "
604 "using the Geometry Database";
612 ostringstream stringStream;
615 stringStream <<
"select setup_id,status,material_id,field_id,file_name,modified_date from setup where "
616 "lower(setup_tag)=lower('"
617 << tag <<
"') order by creation_date desc;";
619 uint64_t sUpTime = 0;
620 std::pair<int, uint64_t> status;
625 resQ =
m_db->Query(stringStream.str().c_str());
626 if (resQ ==
nullptr) {
628 stringStream.str(
"");
629 stringStream <<
"select setup_id,status,material_id,field_id,file_name, creation_date from setup where "
630 "lower(setup_tag)=lower('"
631 << tag <<
"') order by creation_date desc;";
632 resQ =
m_db->Query(stringStream.str().c_str());
634 LOG(error) <<
"SQL query was failed while searching the given tag: `" << setup_tag
635 <<
"`. Setup will download from server.";
641 if (row !=
nullptr) {
648 if (row ==
nullptr || (sStatus == 0 && status.second > sUpTime)) {
650 LOG(info) <<
"No geometry setup found in the local database for `" << setup_tag <<
"` tag";
652 LOG(info) <<
"A newer version of the not approved geometry setup `" << setup_tag
653 <<
"` is on the geometry server";
656 if (row !=
nullptr) {
660 if (status.first >= 0) {
662 if (status.first == 1) {
663 LOG(info) <<
"Approved geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST
664 <<
"`. Downloading...";
667 LOG(info) <<
"Not approved geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST
673 stringStream.str(
"");
675 <<
"select setup_id,status,material_id,field_id,file_name from setup where lower(setup_tag)=lower('"
676 << tag <<
"') order by creation_date desc;";
677 resQ =
m_db->Query(stringStream.str().c_str());
679 LOG(error) <<
"SQL query was failed while searching the given tag: `" << setup_tag <<
"`";
683 LOG(error) <<
"Cannot download the geometry setup from the server:`" <<
GEO_SERVER_HOST <<
"`.";
687 LOG(error) <<
"No geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST <<
"`.";
688 if (row ==
nullptr) {
689 LOG(error) <<
"The geometry setup `" << setup_tag <<
"` is absent in the Geometry Database";
709 if (geoFileField.
IsNull()) {
710 LOG(error) <<
"The geometry setup `" << setup_tag
711 <<
"` has no ROOT file with the full geometry in the Geometry Database";
716 TFile* geoFile =
new TFile(geoFileName,
"READ");
718 LOG(error) <<
"ERROR: could not open ROOT file with geometry: " + geoFileName;
726 LOG(error) <<
"ERROR: TGeoManager is not top element in geometry file " + geoFileName;
748 TString setup_tag =
"", unexpected_part =
"";
756 if (is_xml_file && local_settings.
IsNull())
757 local_settings = token;
758 else if (!is_xml_file && setup_tag.
IsNull())
760 else if (unexpected_part.IsNull())
761 unexpected_part = token;
766 if (local_settings.
IsNull()) {
767 LOG(warning) <<
"No XML file with a geometry customization was found after ';', the whole value is used as "
772 if (!unexpected_part.IsNull()) {
773 LOG(error) <<
"Unexpected part '" << unexpected_part <<
"' in the geometry tag: " << geometry_tag;
778 geometry_tag = setup_tag;
781 TString local_settings_path = local_settings;
782 gSystem->ExpandPathName(local_settings_path);
784 LOG(error) <<
"No XML file with a geometry customization: " << local_settings_path;
796 if (setup_tag.empty()) {
797 LOG(error) <<
"The tag of the geometry setup is not set";
803 if (localSettings !=
nullptr) {
805 TString localSettingsPath(localSettings);
806 gSystem->ExpandPathName(localSettingsPath);
809 LOG(error) <<
"The local settings of the geometry from '" << localSettingsPath
810 <<
"' cannot be applied, the setup is not loaded";
822 ostringstream stringStream;
826 <<
"select setup_id,status,material_id,field_id,modified_date from setup where lower(setup_tag)=lower('" << tag
827 <<
"') order by creation_date desc;";
832 LOG(debug1) << stringStream.str().c_str();
834 uint64_t sUpTime = 0;
835 std::pair<int, uint64_t> status;
837 resQ =
m_db->Query(stringStream.str().c_str());
838 if (resQ ==
nullptr) {
840 stringStream.str(
"");
842 <<
"select setup_id,status,material_id,field_id,creation_date from setup where lower(setup_tag)=lower('"
843 << tag <<
"') order by creation_date desc;";
844 resQ =
m_db->Query(stringStream.str().c_str());
846 LOG(error) <<
"SQL query was failed while searching the given tag: `" << setup_tag
847 <<
"`. Setup will download from server.";
854 if (row !=
nullptr) {
861 if (row ==
nullptr || (sStatus == 0 && status.second > sUpTime)) {
863 LOG(info) <<
"No geometry setup found in the local database for `" << setup_tag <<
"` tag";
865 LOG(info) <<
"A newer version of the not approved geometry setup `" << setup_tag
866 <<
"` is on the geometry server";
869 if (status.first >= 0) {
871 if (status.first == 1) {
872 LOG(info) <<
"Approved geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST
873 <<
"`. Downloading...";
876 LOG(info) <<
"Not approved geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST
877 <<
"`. Downloading...";
882 stringStream.str(
"");
884 <<
"select setup_id,status,material_id,field_id,file_name from setup where lower(setup_tag)=lower('"
885 << tag <<
"') order by creation_date desc;";
890 resQ =
m_db->Query(stringStream.str().c_str());
892 LOG(error) <<
"SQL query was failed while searching the given tag: `" << setup_tag <<
"`";
896 LOG(error) <<
"Cannot download the geometry setup from the server:`" <<
GEO_SERVER_HOST <<
"`.";
901 LOG(error) <<
"No geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST <<
"`.";
902 if (row ==
nullptr) {
903 LOG(error) <<
"The geometry setup `" << setup_tag <<
"` is absent in the Geometry Database";
910 if (fRun ==
nullptr) {
912 LOG(info) <<
"New FairRunSim instance has been created";
927 stringStream.str(
"");
928 stringStream <<
"select matag,file_name from material where material_id=" << idma <<
";";
929 resQ =
m_db->Query(stringStream.str().c_str());
932 if (resQ ==
nullptr) {
933 LOG(info) <<
"Materials were not found";
962 LOG(info) <<
"Materials were not found";
964 const char* materialFile = row->
GetField(1);
965 TString in = (materialFile !=
nullptr ? materialFile :
"");
966 fRun->SetMaterials(in.
Data());
977 LOG(info) <<
"Load geometry with tag=" << setup_tag <<
" from database";
978 LOG(debug1) <<
"setup_id = " <<
m_sid <<
"; material_id = " << idma <<
"; filed_id = " <<
m_fid << endl;
979 stringStream.str(
"");
980 stringStream <<
"select setup_module_id from sms where setup_id=" <<
m_sid <<
";";
981 resQ =
m_db->Query(stringStream.str().c_str());
982 if (resQ ==
nullptr) {
983 LOG(error) <<
"ERROR: SQL query was failed while searching the modules of the setup";
988 while ((row = resQ->
Next()) !=
nullptr) {
990 stringStream.str(
"");
991 const char* idsm = row->
GetField(0);
993 LOG(debug1) <<
"module=" << idsmI << std::endl;
1000 return (res == -3) ? -4 : -3;
1004 if (moduleCount == 0) {
1005 LOG(error) <<
"ERROR: no modules found for the setup `" << setup_tag <<
"`";
1025 if (
m_db ==
nullptr) {
1026 LOG(error) <<
"Geometry Database is not loaded";
1030 LOG(debug1) <<
"Selected Geometry Database has no magnetic field";
1037 double refVoltage = 0;
1040 ostringstream stringStream;
1041 stringStream <<
"select file_name,reference_voltage from field where field_id=" <<
m_fid <<
";";
1043 bool isRefVoltageProvided = (resQ !=
nullptr);
1044 if (!isRefVoltageProvided) {
1046 LOG(warning) <<
"The Geometry Database does not provide the reference voltage of the magnetic field. "
1047 "Please, update the Geometry Database";
1048 stringStream.str(
"");
1049 stringStream <<
"select file_name from field where field_id=" <<
m_fid <<
";";
1050 resQ =
m_db->Query(stringStream.str().c_str());
1052 if (resQ ==
nullptr) {
1053 LOG(error) <<
"SQL query was failed while searching magnetic field for id: `" <<
m_fid <<
"`";
1058 if (row ==
nullptr) {
1059 LOG(error) <<
"ERROR: no selected magnetic field in the Geometry Database for id=" <<
m_fid;
1065 if (isRefVoltageProvided)
1071 LOG(error) <<
"ERROR: no file of the magnetic field map in the Geometry Database for id=" <<
m_fid;
1075 LOG(info) <<
"\033[38;2;255;165;0mThe magnetic field map is replaced by the local settings\033[0m";
1079 LOG(info) <<
"field path=" << pBmnNewFieldMap->
GetFileName();
1090 LOG(error) <<
"Detector name for the parameter file is not set";
1094 LOG(error) <<
"The geometry setup is not loaded";
1097 if (
m_db ==
nullptr) {
1098 LOG(error) <<
"The Geometry Database is not connected";
1105 LOG(error) <<
"The detector name \'" << sname <<
"\'does not exist in the Geometry Database";
1108 LOG(debug1) <<
"The detector name \'" << sname <<
"\'exists in the Geometry Database";
1111 ostringstream stringStream;
1113 <<
"select sm.parfile_name from sms s INNER JOIN setup_module sm on s.setup_module_id=sm.setup_module_id "
1114 "INNER JOIN geometry_file f on sm.file_id=f.file_id INNER JOIN module_class mc on "
1115 "f.module_id=mc.module_class_id where s.setup_id="
1116 <<
m_sid <<
" and mc.module_id=" << index <<
";";
1117 LOG(debug1) <<
"Request \'" << stringStream.str().c_str() <<
"\'.";
1119 if (resQ ==
nullptr) {
1120 LOG(error) <<
"SQL query was failed while searching parameter file for detector `" << sname <<
"`";
1124 if (row ==
nullptr) {
1125 LOG(info) <<
"Parameter file for detector `" << sname <<
"` not found";
1131 const char* file = row->
GetField(0);
1132 string parameterFile = (file !=
nullptr ? file :
"");
1136 if (parameterFile.empty()) {
1137 LOG(info) <<
"Parameter file for detector `" << sname <<
"` does not exist in the Geometry Database";
1154 LOG(error) <<
"The geometry setup is not loaded";
1157 if (
m_db ==
nullptr) {
1158 LOG(error) <<
"The Geometry Database is not connected";
1162 ostringstream stringStream;
1163 stringStream <<
"select m.file_name from setup s INNER JOIN material m on m.material_id=s.material_id where "
1167 if (resQ ==
nullptr) {
1168 LOG(info) <<
"SQL query was failed while searching the material file of the setup";
1172 if (row ==
nullptr) {
1173 LOG(info) <<
"Material file of the setup was not found in the Geometry Database";
1179 const char* file = row->
GetField(0);
1191 const char* env_p = getenv(
"GEOMPATH");
1192 if (env_p ==
nullptr || env_p[0] ==
'\0') {
1193 LOG(debug1) <<
"GEOMPATH environment variable is not set";
1199 if (
gSystem->ExpandPathName(path)) {
1200 LOG(error) <<
"Cannot expand the GEOMPATH value: " << env_p;
1206 return string(path.
Data());
1213 if (storagePath.empty())
1216 return storagePath +
"/";
1225 if (storagePath.empty())
1228 return string(
gSystem->GetDirName(storagePath.c_str()).Data()) +
"/local.db";
1249 LOG(info) <<
"Load local settings of geometry DB from '" << fileName <<
"'";
1252 if (xmldoc ==
nullptr) {
1253 LOG(error) <<
"Selected file has not XML format: " << fileName;
1259 LOG(warning) <<
"The root element of the local settings is <" << xml.
GetNodeName(mainnode)
1260 <<
"> instead of <setup>";
1264 bool isCorrect =
true;
1267 bool isParsed =
false;
1268 if (chName ==
"editSetupModule")
1270 else if (chName ==
"addSetupModule")
1272 else if (chName ==
"editFieldMap")
1275 LOG(error) <<
"Unknown element of the local settings: <" << chName
1276 <<
">, the allowed ones are <editSetupModule>, <addSetupModule>, <editFieldMap>";
1296 bool isCorrect =
true;
1300 LOG(debug1) <<
"attr: " << aName <<
" value: " << aValue;
1301 if (aName ==
"tag") {
1304 }
else if (aName ==
"module") {
1307 }
else if (aName ==
"enable")
1309 else if (aName ==
"file_path") {
1310 gSystem->ExpandPathName(aValue);
1312 }
else if (aName ==
"moveX")
1314 else if (aName ==
"moveY")
1316 else if (aName ==
"moveZ")
1319 LOG(error) <<
"Unknown attribute '" << aName <<
"' of the <editSetupModule> element, the allowed ones are "
1320 <<
"tag, module, enable, file_path, moveX, moveY, moveZ";
1325 LOG(error) <<
"Neither 'tag' nor 'module' attribute is set for the <editSetupModule> element";
1339 if (!module_name.
IsNull())
1340 LOG(warning) <<
"Both 'tag' and 'module' attributes are set for the setup module, the tag is used: "
1343 }
else if (module_name ==
"*") {
1345 LOG(warning) <<
"All the detector modules (module=\"*\") are customized more than once, "
1346 "the latest settings are used";
1360 bool isCorrect =
true;
1364 LOG(debug1) <<
"attr: " << aName <<
" value: " << aValue;
1365 if (aName ==
"class_name")
1366 module->class_name = aValue.Strip(TString::kBoth);
1367 else if (aName ==
"object_name")
1368 module->object_name = aValue.Strip(TString::kBoth);
1369 else if (aName ==
"active")
1371 else if (aName ==
"file_path") {
1372 gSystem->ExpandPathName(aValue);
1373 module->file_path = aValue;
1374 }
else if (aName ==
"moveX")
1376 else if (aName ==
"moveY")
1378 else if (aName ==
"moveZ")
1381 LOG(error) <<
"Unknown attribute '" << aName <<
"' of the <addSetupModule> element, the allowed ones are "
1382 <<
"class_name, object_name, active, file_path, moveX, moveY, moveZ";
1387 LOG(error) <<
"The 'class_name', 'object_name' and 'file_path' attributes are required for the "
1388 "<addSetupModule> element";
1393 if (module_class ==
nullptr || !module_class->
InheritsFrom(FairModule::Class())) {
1394 LOG(error) <<
"No '" <<
module->class_name << "' class inherited from FairModule is found for the '"
1395 << module->object_name << "' module";
1399 LOG(error) <<
"Wrong object name of the module: '" <<
module->object_name
1400 << "', only letters, digits, '_', '.' and '-' are allowed";
1419 bool isCorrect =
true;
1423 if (aName ==
"file_path") {
1424 gSystem->ExpandPathName(aValue);
1427 LOG(error) <<
"Unknown attribute '" << aName <<
"' of the <editFieldMap> element, the allowed one is "
1432 if (file_path.
IsNull()) {
1433 LOG(error) <<
"The 'file_path' attribute is required for the <editFieldMap> element";
1439 LOG(error) <<
"No file of the magnetic field map is found: " << mapFilePath;
1446 LOG(warning) <<
"The magnetic field map is changed more than once, the latest one is used";
1473 vector<TString> candidates;
1474 if (
gSystem->IsAbsoluteFileName(file_path))
1475 candidates.push_back(file_path);
1478 if (!storagePath.empty())
1479 candidates.push_back(
TString(storagePath) + file_path);
1480 const char* workDir = getenv(
"VMCWORKDIR");
1481 if (workDir !=
nullptr && workDir[0] !=
'\0')
1482 candidates.push_back(
TString(workDir) +
"/geometry/" + file_path);
1486 for (
TString& candidate : candidates) {
1487 gSystem->ExpandPathName(candidate);
1488 candidate.ReplaceAll(
"//",
"/");
1493 foundPath = candidate;
1497 if (foundPath.
IsNull()) {
1498 LOG(error) <<
"No geometry file '" << file_path <<
"' is found"
1499 << (
gSystem->IsAbsoluteFileName(file_path) ?
""
1500 :
" in $GEOMPATH (the storage of the Local Geometry "
1501 "Database) and $VMCWORKDIR/geometry");
1509 bool hasGeometry =
false;
1511 if (f !=
nullptr && !f->IsZombie()) {
1512 bool isFirstKey =
true;
1513 TIter nextKey(f->GetListOfKeys());
1514 while (
TKey* key = (
TKey*)nextKey()) {
1516 if (keyClass !=
nullptr
1528 LOG(error) <<
"The geometry file is damaged or contains no geometry (a geometry manager or a volume as the "
1535 file_path = foundPath;
1547 if (!file_path.
EndsWith(
".root")) {
1548 LOG(error) <<
"ERROR: the position of the module can be changed only for the geometry in a ROOT file: "
1554 LOG(debug1) <<
"import file" << file_path << endl;
1556 if (f->IsZombie() || !f->IsOpen()) {
1557 LOG(error) <<
"ERROR: could not open ROOT file with the module geometry: " << file_path;
1564 if (node ==
nullptr) {
1565 LOG(error) <<
"ERROR: no geometry volume with a position matrix found in the file: " << file_path;
1575 matrix->
SetDx(translation[0] + xMove);
1576 matrix->
SetDy(translation[1] + yMove);
1577 matrix->
SetDz(translation[2] + zMove);
1582 static int exportCount = 0;
1585 LOG(debug1) <<
"Exporting geometry to ROOT file: " << out;
1602 if (module_class ==
nullptr || !module_class->
InheritsFrom(FairModule::Class())) {
1603 LOG(error) <<
"ERROR: no '" << class_name <<
"' class inherited from FairModule is found for the '"
1604 << module_name <<
"' module";
1610 LOG(error) <<
"ERROR: wrong name of the module: '" << module_name
1611 <<
"', only letters, digits, '_', '.' and '-' are allowed";
1617 Ssiz_t namespaceEnd = constructor_name.
Last(
':');
1618 if (namespaceEnd !=
kNPOS)
1619 constructor_name.
Remove(0, namespaceEnd + 1);
1623 command += isActive ?
",kTRUE)" :
",kFALSE)";
1626 LOG(info) <<
"The " << module_class->
GetName() <<
" class has no active flag, so the '" << module_name
1627 <<
"' module is added as a passive one";
1630 LOG(debug1) <<
"command:" << command;
1634 LOG(error) <<
"ERROR: cannot create the module by the command: " << command;
1644 map<TString, setup_module_ptr>::iterator itr =
m_moduleEdit.find(setup_module_tag);
1671 LOG(error) <<
"Unknown module type with id=" << dbModuleUndex;
1679 map<int, TString>::iterator it;
1681 if ((it->second).CompareTo(name, TString::ECaseCompare::kIgnoreCase) == 0)
1703 return it->second->idm;
1719 TList* l = f->GetListOfKeys();
1721 while ((key = (
TKey*)next())) {
1728 LOG(error) <<
"ERROR: geometry in the file " << f->GetName() <<
" has no daughter volume";
1736 if (key ==
nullptr) {
1737 LOG(error) <<
"ERROR: no keys found in the geometry file " << f->GetName();
1749 if (mr ==
nullptr) {
1750 LOG(error) <<
"ERROR: no module revision with id=" << dbModuleRev <<
" in the Geometry Database";
1770 obj->SetGeometryFileName(path);
1771 fRun->AddModule(obj);
1780 if (module ==
nullptr)
1786 bool isCave = (module_class !=
nullptr && module_class->
InheritsFrom(
"FairCave"));
1787 TIter nextModule(fRun->GetListOfModules());
1789 if (module->
object_name == runModule->GetName() || (isCave && runModule->InheritsFrom(
"FairCave"))) {
1790 LOG(error) <<
"ERROR: the '" <<
module->object_name
1791 << "' module is not added, because the setup already has the '" << runModule->GetName()
1792 << "' module (" << runModule->ClassName()
1793 << "). Use the <editSetupModule> element with the 'file_path' attribute to replace the "
1794 "geometry of a setup module";
1799 TString geo_file =
module->file_path;
1800 bool isMoved = (
module->xMove != 0 || module->yMove != 0 || module->zMove != 0);
1811 obj->SetGeometryFileName(geo_file);
1812 fRun->AddModule(obj);
1813 LOG(info) <<
"\033[0;32mAdded external detector: name=" <<
module->object_name << " file_path=" << module->file_path
1814 << " class_name=" << module->class_name << " isActive=" << module->active << "\033[0m";
1816 LOG(info) <<
"\033[0;32mThe position of the '" <<
module->object_name << "' module was shifted by ("
1817 << module->xMove << ", " << module->yMove << ", " << module->zMove << ") cm\033[0m";
1824 if (f->IsZombie() || !f->IsOpen()) {
1825 LOG(error) <<
"ERROR: could not open ROOT file with geometry: " << fileName;
1832 TGeoNode* node = (root_volume ==
nullptr) ?
nullptr : root_volume->
GetNode(0);
1840 if (row == NULL || fileName == NULL)
1852 for (
i = 0;
i < 3;
i++) {
1855 LOG(debug1) <<
" for i=" <<
i <<
" tran[i]=" << tran[
i] <<
" t=" << t << endl;
1859 for (
i = 0;
i < 9;
i++) {
1862 LOG(debug1) <<
" for i=" <<
i <<
" rot[i]=" << rot[
i] <<
" t=" << t << endl;
1880 LOG(debug1) <<
"Start load setup_module_id=" << idsm;
1881 ostringstream stringStream;
1882 stringStream <<
"select "
1883 "f.module_id,f.file_name,sm.parent_id,sm.tx,sm.ty,sm.tz,r11,r12,r13,r21,r22,r23,r31,r32,r33,sm."
1885 "from setup_module sm INNER JOIN geometry_file f ON f.file_id=sm.file_id where sm.setup_module_id="
1890 TSQLRow* row = (resQ ==
nullptr) ?
nullptr : resQ->
Next();
1891 if (row ==
nullptr) {
1892 LOG(error) <<
"ERROR: no file for the setup module (" << idsm <<
") was found";
1898 LOG(debug1) <<
"idP=" << idP;
1908 stringStream.str(
"");
1914 bool isEnable =
true;
1916 TString file_path = stringStream.str().c_str();
1920 bool isAdded =
true;
1922 bool isFind =
false;
1924 if (moduleEdit !=
nullptr) {
1925 LOG(debug1) <<
"detector was found";
1926 isEnable = moduleEdit->
enable;
1929 if (moduleEdit->
xMove != 0 || moduleEdit->
yMove != 0 || moduleEdit->
zMove != 0) {
1930 LOG(error) <<
"ERROR: the position of the module with tag=" << smTag
1931 <<
" cannot be changed by the local settings, because it is the cave or its geometry "
1932 "is in the ASCII format (.geo)";
1945 bool isDuplicateCave =
false;
1946 if (mName ==
TString(
"CAVE")) {
1947 TIter nextModule(fRun->GetListOfModules());
1948 while (
TObject* module = nextModule())
1949 if (module->InheritsFrom(
"FairCave")) {
1950 isDuplicateCave =
true;
1954 if (isDuplicateCave)
1955 LOG(warning) <<
"The cave has been already added, so the duplicate cave module with tag=" << smTag
1956 <<
" is skipped. Please, remove it from the setup in the Geometry Database";
1959 LOG(info) <<
"\033[38;2;255;165;0mEdit module with tag=" << smTag
1960 <<
" use local settings. New path: " << file_path <<
"\033[0m";
1962 LOG(info) <<
"Loading module with tag=" << smTag;
1966 LOG(info) <<
"\033[0;31mDisable module with tag=" << smTag <<
" use local settings.\033[0m";
1968 double mx = 0, my = 0, mz = 0;
1969 bool isFind =
false;
1971 LOG(debug1) <<
"detector " << dbModuleIndex;
1972 if (moduleEdit !=
nullptr) {
1974 LOG(debug1) <<
"detector was found";
1975 isEnable = moduleEdit->
enable;
1977 mx = moduleEdit->
xMove;
1978 my = moduleEdit->
yMove;
1979 mz = moduleEdit->
zMove;
1982 LOG(debug1) <<
"Move detector " << dbModuleIndex <<
" to x:" << mx <<
" to y:" << my <<
" to z:" << mz;
1987 bool isAscii = file_path.
EndsWith(
".geo");
1988 if (isEnable && isAscii && (mx != 0 || my != 0 || mz != 0)) {
1989 LOG(error) <<
"ERROR: the position of the module with tag=" << smTag
1990 <<
" cannot be changed by the local settings, because its geometry is in the ASCII format";
1997 if (isEnable && mx == 0 && my == 0 && mz == 0 && (isAscii ||
IsEqlMatrix(file_path, row))) {
1998 LOG(debug1) <<
"The same matrix for file=" << file_path << endl;
2000 LOG(info) <<
"\033[38;2;255;165;0mEdit module with tag=" << smTag
2001 <<
" use local settings. New path: " << file_path <<
"\033[0m";
2003 LOG(info) <<
"Loading module with tag=" << smTag;
2005 }
else if (isEnable) {
2009 Double_t translation[3], rotation[9];
2010 for (
int i = 0;
i < 3;
i++)
2012 for (
int i = 0;
i < 9;
i++)
2023 LOG(info) <<
"\033[38;2;255;165;0mEdit module with tag=" << smTag
2024 <<
" use local settings. New path: " << file_path <<
". The position was changed.\033[0m";
2026 LOG(info) <<
"Loading module with tag=" << smTag;
2029 LOG(info) <<
"\033[0;31mDisable module with tag=" << smTag <<
" use local settings.\033[0m";
2035 return isAdded ? 0 : -2;
2041 bool isActiveKnown =
true;
2042 TSQLResult* resQ =
m_db->Query(
"select module_id,module_name,active from module;");
2043 if (resQ ==
nullptr) {
2045 isActiveKnown =
false;
2047 <<
"The Geometry Database does not provide the 'active' flag of the modules, so the 'module=\"*\"' "
2048 "local setting of the geometry will be ignored. Please, update the Geometry Database";
2049 resQ =
m_db->Query(
"select module_id,module_name from module;");
2051 if (resQ ==
nullptr) {
2052 LOG(error) <<
"ERROR: SQL query was failed. Please, check connection to the Geometry Database and "
2053 "GEOMPATH variable is correct";
2059 while ((row = resQ->
Next()) != 0) {
2071 if (rowCount == 0) {
2072 LOG(error) <<
"ERROR: no modules in the Geometry Database";
2081 TSQLResult* resQ =
m_db->Query(
"select module_class_id,mc.module_id,class_name,revision,active,object_name from "
2082 "module_class as mc INNER JOIN module as m ON mc.module_id=m.module_id;");
2083 if (resQ ==
nullptr) {
2084 LOG(error) <<
"ERROR: SQL query was failed. Please, check connection to the Geometry Database and "
2085 "GEOMPATH variable is correct";
2091 while ((row = resQ->
Next()) != 0) {
2109 if (rowCount == 0) {
2110 LOG(error) <<
"ERROR: no module revisions in the geometry database";
static pid_t gWritableDbPid
static vector< string > gTmpGeometryFiles
static const char *const kWgetOptions
static bool AttrToBool(const TString &name, TString value, bool &result)
static int FieldToInt(const char *field, int defaultValue=0)
static void RemoveTmpGeometryFiles()
static void RemoveWritableDbDir()
static pid_t gTmpGeometryPid
static bool ichar_equals(char a, char b)
static bool AttrToDouble(const TString &name, TString value, double &result)
static uint64_t FieldToUInt64(const char *field, uint64_t defaultValue=0)
static string SqlEscape(string_view value)
static void RemoveAssemblyShapes(TGeoManager *geoManager)
static void PutModuleEdit(map< TString, setup_module_ptr > &edits, const TString &key, setup_module_ptr sNode)
static double FieldToDouble(const char *field, double defaultValue=0.)
static string gWritableDbDir
static bool IsSafeTag(const string &tag)
struct SETUPMODULE setup_module
struct SETUPMODULE * setup_module_ptr
struct MODULEREVISION module_r
struct MODULEREVISION * module_r_ptr
R__EXTERN TGeoManager * gGeoManager
Bool_t R_ISDIR(Int_t mode)
R__EXTERN TSystem * gSystem
static TString GetMapFilePath(TString mapName)
Get the expanded path to the field map file in the current installation.
const char * GetFileName()
Accessor to field map file.
void SetFileName(const char *map_file_name)
Set field map file path.
Field Map does not have any symmetries w.r.t coordinate axises.
map< TString, setup_module_ptr > m_moduleEditByName
map< TString, setup_module_ptr > m_moduleEdit
map< int, module_r_ptr > m_modules_revision_table
FairField * GetMagneticField()
Get magnetic field of the current setup geometry.
int GetModuleId(int dbModuleRev)
TGeoNode * ConstructRootGeometry(const char *fileName)
string GetParameterFile(string_view detector_name)
Get path to parameter file for the given detector name of the current setup.
void ClearLocalSettings()
bool OpenLocalGeoDatabase()
TString GetModuleName(int dbModuleUndex)
FairModule * CreateModule(const TString &class_name, const TString &module_name, bool isActive)
int LoadModuleToFairRunSim(int idsm, FairRunSim *fRun)
TString GetTmpFileName(const char *baseName)
int LoadSetupToGeoManager(string_view setup_tag)
Load selected geometry setup from the Local Geometry Database and create gGeoManager instance.
bool FindModuleGeometryFile(TString &file_path)
bool ParseEditFieldMap(TXMLEngine &xml, XMLNodePointer_t node)
bool LoadLocalSettings(const char *fileName)
string GetMaterialFile()
Get the material file of the current setup, which defines the media of the setup geometry.
map< int, TString > m_loadedModule
map< int, TString > m_modules_table
TString ExportModuleGeometry(const TString &file_path, const TString &module_name, const TGeoMatrix *placement, double xMove, double yMove, double zMove)
bool ParseEditSetupModule(TXMLEngine &xml, XMLNodePointer_t node)
std::pair< int, uint64_t > CheckTagOnServer(string_view setup_tag, const char *url=GEO_SERVER_HOST)
TGeoVolume * GetRootVolume(TFile *f)
int installLocalDB(string_view setup_tag, const char *url=GEO_SERVER_HOST)
unsigned long int GetServerRevision(const char *url=GEO_SERVER_HOST)
TString GetModuleType(int dbModuleRev)
TString PrepareWritableDbDir()
setup_module_ptr FindModuleEdit(TString setup_module_tag, TString module_name, int dbModuleRev)
module_r_ptr GetModuleRevision(int dbModuleRev)
static bool exists_file(const char *name)
static int ParseGeometryTag(TString &geometry_tag, TString &local_settings)
Parse the geometry tag, which can contain the path to an XML file with a local customization of the s...
static bool IsWritableDir(const char *path)
int GetModuleIndex(TString &name)
bool ParseAddSetupModule(TXMLEngine &xml, XMLNodePointer_t node)
bool IsEqlMatrix(const char *fileName, TSQLRow *row)
~GeoSetup()
Destructor method.
unsigned long int GetLocalRevision(TString &localDbPath)
std::pair< int, string > GetTagName4PatternLocal(string_view pattern_tag, TString &localDbPath)
bool AddModuleToFairRunSim(module_r_ptr module, FairRunSim *fRun)
std::pair< int, string > GetTagName4PatternServer(string_view pattern_tag, const char *url=GEO_SERVER_HOST)
string GetLastApprovedSetupTag(string_view tag_prefix)
Get tag for the last approved setup corresponding to the pattern.
setup_module_ptr m_moduleEditAll
vector< module_r_ptr > m_moduleAdd
map< int, bool > m_modules_active
int LoadSetupToFairRunSim(string_view setupTag, const char *localSettings=nullptr)
Load selected geometry setup from the Local or Central (defined by url) Geometry Database and add it ...
TMethod * GetMethodWithPrototype(const char *method, const char *proto, Bool_t objectIsConst=kFALSE, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
Bool_t InheritsFrom(const char *cl) const override
TList * GetListOfKeys() const override
virtual Bool_t IsOpen() const
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
void SetRotation(const Double_t *matrix)
void SetDz(Double_t dz) override
void SetDy(Double_t dy) override
void SetDx(Double_t dx) override
virtual Double_t * GetTranslation()
void SetTranslation(const Double_t *vect)
TObjArray * GetListOfVolumes() const
virtual Bool_t cd(const char *path="")
TGeoNode * GetNode(Int_t level) const
TObjArray * GetListOfShapes() const
virtual const Double_t * GetTranslation() const=0
virtual const Double_t * GetRotationMatrix() const=0
void SetMatrix(const TGeoMatrix *matrix)
TGeoMatrix * GetMatrix() const override
TGeoVolume * GetVolume() const
Int_t GetNdaughters() const
TGeoNode * GetDaughter(Int_t ind) const
virtual TGeoMatrix * GetMatrix() const=0
TGeoNode * GetNode(const char *name) const
Int_t Export(const char *filename, const char *name="", Option_t *option="")
virtual const char * GetClassName() const
virtual TObject * ReadObj()
TObject * At(Int_t idx) const override
const char * GetName() const override
Int_t GetEntriesFast() const
TObject * At(Int_t idx) const override
TObject * Remove(TObject *obj) override
R__ALWAYS_INLINE Bool_t IsZombie() const
virtual TSQLRow * Next()=0
virtual const char * GetField(Int_t field)=0
virtual Bool_t IsConnected() const
TSQLResult * Query(const char *sql) final
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
TSubString Strip(EStripType s=kTrailing, char c=' ') const
const char * Data() const
Ssiz_t Last(char c) const
TObjArray * Tokenize(const TString &delim) const
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
static TString Format(const char *fmt,...)
TString & Remove(EStripType s, char c)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
void FreeDoc(XMLDocPointer_t xmldoc)
XMLNodePointer_t DocGetRootElement(XMLDocPointer_t xmldoc)
XMLAttrPointer_t GetNextAttr(XMLAttrPointer_t xmlattr)
const char * GetAttrName(XMLAttrPointer_t xmlattr)
XMLAttrPointer_t GetFirstAttr(XMLNodePointer_t xmlnode)
const char * GetNodeName(XMLNodePointer_t xmlnode)
XMLDocPointer_t ParseFile(const char *filename, Int_t maxbuf=100000)
const char * GetAttrValue(XMLAttrPointer_t xmlattr)
XMLNodePointer_t GetNext(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
const char *const GEO_SERVER_HOST