3#include "BmnNewFieldMap.h"
7#include "TGeoBoolNode.h"
8#include "TGeoCompositeShape.h"
10#include "TGeoManager.h"
11#include "TGeoMatrix.h"
15#include "TSQLResult.h"
17#include "TSQLiteServer.h"
19#include "TXMLEngine.h"
27bool GeoSetup::exists_file(
const char* name)
29 return (access(name, F_OK) != -1);
34 return std::tolower(
static_cast<unsigned char>(a)) == std::tolower(
static_cast<unsigned char>(b));
37unsigned long int GeoSetup::GetServerRevision(
const char* url)
39 unsigned long int serverRev = 0;
40 gSystem->Exec(TString::Format(
"wget -O server_revision.txt \"%s/get_cur_timestamp.php\" > /dev/null 2>&1", url));
41 std::ifstream myFile_Handler;
43 myFile_Handler.open(
"server_revision.txt");
44 if (myFile_Handler.is_open()) {
46 while (getline(myFile_Handler, myLine)) {
47 serverRev = strtoul(myLine.c_str(), &ptr, 10);
50 myFile_Handler.close();
51 std::remove(
"server_revision.txt");
56 std::pair<int,string_view> res = GetTagName4PatternServer(pattern);
60 TString localDbPath = gSystem->ExpandPathName(getenv(
"DBL_FILE_PATH"));
61 res = GetTagName4PatternLocal(pattern,localDbPath);
65 TString result=
"Setup not found for patter:"+std::string(pattern);
66 throw std::invalid_argument(result);
68std::pair<int,string_view> GeoSetup::GetTagName4PatternServer(string_view pattern_tag,
const char* url){
69 std::pair<int,string_view> res = std::make_pair<int,string_view>(0,
"");
70 std::remove(
"setup_name.txt");
71 TString command = TString::Format(
"wget -O setup_name.txt \"%s/get_setup_name.php?tag=%s\" > /dev/null 2>&1", url,pattern_tag.data());
72 gSystem->Exec(command);
73 std::ifstream myFile_Handler;
75 myFile_Handler.open(
"setup_list.txt");
76 if (myFile_Handler.is_open()) {
77 getline(myFile_Handler, myLine);
78 std::size_t found = myLine.find(
";");
79 if(found==std::string::npos){
83 vector<string> results;
84 istringstream
f(myLine);
86 while (getline(
f, token,
':')) {
87 results.push_back(token);
91 res = std::make_pair<int,string_view>(1,results[0]);
93 myFile_Handler.close();
98std::pair<int, uint64_t> GeoSetup::CheckTagOnServer(string_view setup_tag,
const char* url)
101 gSystem->Exec(TString::Format(
"wget -O server_setup_list.txt \"%s/get_setup_list.php\" > /dev/null 2>&1", url));
102 std::ifstream myFile_Handler;
104 std::string sTag(setup_tag);
105 myFile_Handler.open(
"server_setup_list.txt");
106 if (myFile_Handler.is_open()) {
107 while (getline(myFile_Handler, myLine)) {
108 istringstream
f(myLine);
110 while (getline(
f, s,
';')) {
111 vector<string> results;
113 std::istringstream input;
116 while (getline(input, token,
':')) {
117 results.push_back(token);
122 std::string tag = results[0];
123 std::string status = results[1];
125 if(results.size()>2){
126 upTime=std::stoull(results[2],
nullptr, 10);
128 bool bb = std::equal(tag.begin(), tag.end(), sTag.begin(), sTag.end(),
ichar_equals);
129 if (tag.size() == sTag.size() && bb){
131 myFile_Handler.close();
132 std::remove(
"server_setup_list.txt");
133 return std::make_pair(std::stoi(status),upTime);
149 myFile_Handler.close();
150 std::remove(
"server_setup_list.txt");
152 return std::make_pair(retval,0);
155std::pair<int,string_view> GeoSetup::GetTagName4PatternLocal(string_view pattern_tag,TString& localDbPath){
156 std::pair<int,string_view> res = std::make_pair<int,string_view>(0,
"");
157 if (exists_file(localDbPath)) {
158 std::ostringstream stringStream;
159 stringStream <<
"sqlite://" << localDbPath;
160 TSQLiteServer db(stringStream.str().c_str());
161 stringStream.str(
"");
162 if (db.IsConnected()) {
164 stringStream <<
"SELECT setup_tag, status, creation_date FROM setup where status=1 and setup_tag like '"<<pattern_tag.data()<<
"%' order by creation_date DESC limit 1;";
165 TSQLResult* resQ = db.Query(stringStream.str().c_str());
167 TSQLRow* row = resQ->Next();
169 const char*
name = row->GetField(0);
170 res = std::make_pair<int,string_view>(1,name);
177unsigned long int GeoSetup::GetLocalRevision(TString& localDbPath)
179 unsigned long int localRev = 0;
181 if (exists_file(localDbPath)) {
182 std::ostringstream stringStream;
183 stringStream <<
"sqlite://" << localDbPath;
184 TSQLiteServer db(stringStream.str().c_str());
185 stringStream.str(
"");
186 if (db.IsConnected()) {
187 stringStream <<
"select value_field from config where key_field='config_time';";
189 TSQLResult* resQ = db.Query(stringStream.str().c_str());
191 TSQLRow* row = resQ->Next();
193 const char* ch_time = row->GetField(0);
196 localRev = strtoul(ch_time, &ptr, 10);
204int GeoSetup::installLocalDB(string_view setup_tag,
const char* url)
207 TString localDbPath = gSystem->ExpandPathName(getenv(
"DBL_FILE_PATH"));
208 if (localDbPath ==
"") {
209 LOG(error) <<
"DBL_FILE_PATH is not set. Please, set DBL_FILE_PATH as a path to the local database file (default, "
214 FileStat_t file_stat;
215 gSystem->GetPathInfo(localDbPath.Data(), file_stat);
216 if (R_ISDIR(file_stat.fMode)) {
217 LOG(error) <<
"DBL_FILE_PATH is a path to a folder. Please, set DBL_FILE_PATH as a path to the local database file "
218 "(default, 'local.db')"
223 TString dirName(gSystem->DirName(localDbPath.Data()));
224 TString fileName(gSystem->BaseName(localDbPath.Data()));
225 if (!exists_file(dirName.Data()))
226 if (gSystem->mkdir(dirName,
true) != 0) {
227 LOG(error) <<
"ERROR: cannot create folder: " << dirName << endl;
231 if (setup_tag.size() == 0)
232 command = TString::Format(
"wget -O testdb.tar \"%s/download_file.php?type=all&tag=all\" > /dev/null 2>&1", url);
234 std::string tag(setup_tag);
235 command = TString::Format(
"wget -O testdb.tar \"%s/download_file.php?type=s&tag=%s&rev=1\" > /dev/null 2>&1",
238 gSystem->Exec(command);
239 if (!exists_file(
"testdb.tar")) {
240 LOG(error) <<
"ERROR: downloading the geometry database file failed, please, check url: " <<
url << endl;
243 gSystem->GetPathInfo(
"testdb.tar", file_stat);
244 if (file_stat.fSize == 0) {
245 LOG(error) <<
"ERROR: downloaded geometry database file has 0 size, please, check url: " <<
url << endl;
249 gSystem->Exec(TString::Format(
"tar -xvf testdb.tar -C \"%s/\" > /dev/null 2>&1", dirName.Data()));
251 gSystem->Exec(
"rm -rf testdb.tar");
252 if (fileName !=
"local.db") {
253 gSystem->CopyFile(gSystem->ConcatFileName(dirName.Data(),
"local.db"),
254 gSystem->ConcatFileName(dirName.Data(), fileName.Data()), kTRUE);
255 gSystem->Unlink(gSystem->ConcatFileName(dirName.Data(),
"local.db"));
257 if (!OpenLocalGeoDatabase())
259 LOG(info) <<
"The Local Geometry Database was successfully installed to the directory: " << dirName << endl;
263void GeoSetup::CloseDB()
265 m_modules_table.clear();
266 m_modules_revision_table.clear();
267 if (m_db !=
nullptr) {
281bool GeoSetup::OpenLocalGeoDatabase()
283 const char* env_p = getenv(
"DBL_FILE_PATH");
284 if (env_p ==
nullptr) {
285 LOG(error) <<
"Please, set DBL_FILE_PATH environment variable before using the Geometry Database";
288 if (!exists_file(gSystem->ExpandPathName(env_p))) {
289 LOG(error) <<
"No Database found in " << gSystem->ExpandPathName(env_p);
292 ostringstream stringStream;
293 stringStream <<
"sqlite://" << gSystem->ExpandPathName(env_p);
295 LOG(debug1) <<
"Connecting the Local Geometry Database at path: " << stringStream.str().c_str();
297 m_db =
new TSQLiteServer(stringStream.str().c_str());
298 if (PutModules() != 0) {
299 LOG(error) <<
"Cannot load detector geometry, please, check DBL_FILE_PATH environment variable";
302 if (PutModuleRevisions() != 0) {
303 LOG(error) <<
"Cannot load detector geometry, please, check DBL_FILE_PATH environment variable";
316 const char* env_p = getenv(
"DBL_FILE_PATH");
317 if (env_p ==
nullptr) {
318 LOG(error) <<
"Please, set DBL_FILE_PATH environment variable before using the Geometry Database";
322 if (OpenLocalGeoDatabase())
324 TSQLResult* resQ =
nullptr;
325 TSQLRow* row =
nullptr;
326 ostringstream stringStream;
328 stringStream <<
"select setup_id,status,material_id,field_id,file_name,modified_date from setup where lower(setup_tag)=lower('"
329 << setup_tag <<
"') order by creation_date desc;";
332 std::pair<int, uint64_t> status;
337 resQ = m_db->Query(stringStream.str().c_str());
338 if (resQ ==
nullptr) {
339 stringStream.str(
"");
340 stringStream <<
"select setup_id,status,material_id,field_id,file_name, creation_date from setup where lower(setup_tag)=lower('" << setup_tag
341 <<
"') order by creation_date desc;";
342 resQ = m_db->Query(stringStream.str().c_str());
344 LOG(error) <<
"SQL query was failed while searching the given tag: `" << setup_tag <<
"`. Setup will download from server.";
353 sStatus = atoi(row->GetField(1));
354 sUpTime = std::stoull(row->GetField(5),
nullptr, 10);
357 status = CheckTagOnServer(setup_tag);
359 if (row ==
nullptr || (sStatus==0 && status.second>sUpTime)) {
360 LOG(error) <<
"No geometry setup found in the local database for `" << setup_tag <<
"` tag.";
369 if (status.first >= 0) {
371 if (status.first == 1) {
372 LOG(info) <<
"Approved geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST
373 <<
"`. Downloading...";
374 isUpload = installLocalDB(
"");
376 LOG(info) <<
"Not approved geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST
378 isUpload = installLocalDB(setup_tag);
382 stringStream.str(
"");
383 stringStream <<
"select setup_id,status,material_id,field_id,file_name from setup where lower(setup_tag)=lower('"
384 << setup_tag <<
"') order by creation_date desc;";
385 resQ = m_db->Query(stringStream.str().c_str());
388 LOG(error) <<
"Cannot download the geometry setup from the server:`" <<
GEO_SERVER_HOST <<
"`.";
392 LOG(error) <<
"No geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST <<
"`.";
397 m_sid = atoi(row->GetField(0));
399 m_fid = atoi(row->GetField(3));
400 TString geoFileName = GetRootPath() + row->GetField(4);
404 TFile* geoFile =
new TFile(geoFileName,
"READ");
405 if (!geoFile->IsOpen()) {
406 LOG(error) <<
"ERROR: could not open ROOT file with geometry: " + geoFileName;
409 TList* keyList = geoFile->GetListOfKeys();
411 TKey* key = (TKey*)next();
412 TString className(key->GetClassName());
413 if (className.BeginsWith(
"TGeoManager"))
416 LOG(error) <<
"ERROR: TGeoManager is not top element in geometry file " + geoFileName;
419 m_setupTag=setup_tag;
426 if (localSettings !=
nullptr)
427 LoadLocalSettings(localSettings);
457 const char* env_p = getenv(
"DBL_FILE_PATH");
458 if (env_p ==
nullptr) {
459 LOG(error) <<
"Please, set DBL_FILE_PATH environment variable before using the Geometry Database";
463 if (OpenLocalGeoDatabase())
465 TSQLResult* resQ =
nullptr;
466 TSQLRow* row =
nullptr;
468 ostringstream stringStream;
470 stringStream <<
"select setup_id,status,material_id,field_id,modified_date from setup where lower(setup_tag)=lower('" << setup_tag
471 <<
"') order by creation_date desc;";
476 LOG(debug1) << stringStream.str().c_str();
479 std::pair<int, uint64_t> status;
481 resQ = m_db->Query(stringStream.str().c_str());
482 if (resQ ==
nullptr) {
484 stringStream.str(
"");
485 stringStream <<
"select setup_id,status,material_id,field_id,creation_date from setup where lower(setup_tag)=lower('" << setup_tag
486 <<
"') order by creation_date desc;";
487 resQ = m_db->Query(stringStream.str().c_str());
489 LOG(error) <<
"SQL query was failed while searching the given tag: `" << setup_tag <<
"`. Setup will download from server.";
498 sStatus = atoi(row->GetField(1));
499 sUpTime = std::stoull(row->GetField(4),
nullptr, 10);
502 status = CheckTagOnServer(setup_tag);
504 if (row ==
nullptr || (sStatus==0 && status.second>sUpTime)) {
505 LOG(error) <<
"No geometry setup found in the local database for `" << setup_tag <<
"` tag."<<status.first;
508 if (status.first >= 0) {
510 if (status.first == 1) {
511 LOG(info) <<
"Approved geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST
512 <<
"`. Downloading...";
513 isUpload = installLocalDB(
"");
515 LOG(info) <<
"Not approved geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST
516 <<
"`. Downloading...";
517 isUpload = installLocalDB(setup_tag);
521 stringStream.str(
"");
522 stringStream <<
"select setup_id,status,material_id,field_id,file_name from setup where lower(setup_tag)=lower('"
523 << setup_tag <<
"') order by creation_date desc;";
525 resQ = m_db->Query(stringStream.str().c_str());
528 LOG(error) <<
"Cannot download the geometry setup from the server:`" <<
GEO_SERVER_HOST <<
"`.";
532 LOG(error) <<
"No geometry setup found on the geometry server:`" <<
GEO_SERVER_HOST <<
"`.";
537 FairRunSim* fRun = FairRunSim::Instance();
538 if (fRun ==
nullptr) {
539 fRun =
new FairRunSim();
540 LOG(info) <<
"New FairRunSim instance has been created";
543 m_sid = atoi(row->GetField(0));
544 int idma = atoi(row->GetField(2));
545 m_fid = atoi(row->GetField(3));
550 stringStream.str(
"");
551 stringStream <<
"select matag,file_name from material where material_id=" << idma <<
";";
552 resQ = m_db->Query(stringStream.str().c_str());
554 if (resQ == 0 || resQ->GetRowCount() == 0)
555 LOG(info) <<
"Materials were not found";
558 const char* media_path = row->GetField(1);
560 TString Mat = getenv(
"GEOMPATH");
562 Mat = ((TString)getenv(
"VMCWORKDIR")) +
"/geometry/";
563 Mat.ReplaceAll(
"//",
"/");
565 Mat.ReplaceAll(
"//",
"/");
566 if (!Mat.EndsWith(
"/"))
570 string workP(Mat.Data());
571 LOG(debug1) <<
"Working geometry directory: " << workP;
573 size_t found = workP.find(
'/');
575 if (found != string::npos) {
576 while ((found = workP.find(
'/', found + 1)) != string::npos) {
577 LOG(debug1) <<
"found:" << found;
581 in += GetRootPath() + media_path;
594 LOG(info)<<
"Load geometry with tag="<<setup_tag<<
" from database";
595 LOG(debug1) <<
"setup_id = " << m_sid <<
"; material_id = " << idma <<
"; filed_id = " << m_fid << endl;
596 stringStream.str(
"");
597 stringStream <<
"select setup_module_id from sms where setup_id=" << m_sid <<
";";
598 resQ = m_db->Query(stringStream.str().c_str());
599 if (resQ == 0 || resQ->GetRowCount() == 0) {
600 LOG(error) <<
"ERROR: no modules found";
603 while ((row = resQ->Next()) !=
nullptr) {
604 stringStream.str(
"");
605 const char* idsm = row->GetField(0);
606 int idsmI = atoi(idsm);
607 LOG(debug1) <<
"module=" << idsmI << std::endl;
609 int res = LoadModuleToFairRunSim(idsmI, fRun);
611 LOG(error) <<
"ERROR: no file for the setup module " << idsmI <<
" was found";
617 for (
const auto& item : m_moduleAdd) {
618 AddModuleToFairRunSim(item,fRun);
634 m_setupTag=setup_tag;
642 if (m_field !=
nullptr)
645 if (m_db ==
nullptr) {
646 LOG(error) <<
"Geometry Database is not loaded";
650 LOG(debug1) <<
"Selected Geometry Database has no magnetic field";
655 ostringstream stringStream;
656 stringStream <<
"select file_name,scale,x,y,z from field where field_id=" << m_fid <<
";";
657 TSQLResult* resQ = m_db->Query(stringStream.str().c_str());
658 if (resQ ==
nullptr) {
659 LOG(error) <<
"SQL query was failed while searching magnetic field for id: `" << m_fid <<
"`";
662 if (resQ->GetRowCount() == 0) {
663 LOG(error) <<
"ERROR: no selected magnetic field in the Geometry Database for id=" << m_fid;
668 TSQLRow* row = resQ->Next();
669 if (row ==
nullptr) {
670 LOG(error) <<
"ERROR: no selected magnetic field in the Geometry Database for id=" << m_fid;
676 stringStream.str(
"");
677 stringStream << GetRootPath().c_str() << row->GetField(0);
678 if (access(stringStream.str().c_str(), F_OK) == -1) {
679 LOG(error) <<
"ERROR: File with the magnetic field was not found. The url=" << stringStream.str().c_str();
684 double sc = atof(row->GetField(1));
690 pBmnNewFieldMap->
SetFileName(stringStream.str().c_str());
697 m_field = pBmnNewFieldMap;
705 LOG(error) <<
"Detector name for the parameter file is not set";
708 if (m_db ==
nullptr) {
709 LOG(error) <<
"The Geometry Database is not connected";
714 int index = GetModuleIndex(sname);
716 LOG(error) <<
"The detector name \'" << sname <<
"\'does not exist in the Geometry Database";
719 LOG(debug1) <<
"The detector name \'" << sname <<
"\'exists in the Geometry Database";
721 ostringstream stringStream;
723 <<
"select sm.fpar from sms s INNER JOIN setup_module sm on s.idsm=sm.idsm INNER JOIN file f on sm.idf=f.idf "
724 "INNER JOIN module_revision mr on f.idm=mr.idmr where idsd="
725 << m_sid <<
" and mr.idm=" << index <<
";";
726 LOG(debug1) <<
"Request \'" << stringStream.str().c_str() <<
"\'.";
727 TSQLResult* resQ = m_db->Query(stringStream.str().c_str());
728 if (resQ ==
nullptr) {
729 LOG(info) <<
"SQL query was failed while searching parameter file for detector `" << sname <<
"`";
732 TSQLRow* row = resQ->Next();
733 if (row ==
nullptr) {
734 LOG(info) <<
"Parameter file for detector `" << sname <<
"` not found";
738 const char* file = row->GetField(0);
742 if (file ==
nullptr) {
743 LOG(info) <<
"Parameter file for detector `" << sname <<
"` does not exist in the Geometry Database";
751string GeoSetup::GetRootPath()
753 string rootPath = gSystem->ExpandPathName(getenv(
"DBL_FILE_PATH"));
756 if (stat(rootPath.c_str(), &st) == 0)
757 if ((st.st_mode & S_IFDIR) != 0) {
758 LOG(error) <<
"ERROR: DBL_FILE_PATH variable points to the folder. Please, set DBL_FILE_PATH to file path "
759 "with local geometry database";
763 int last = rootPath.find_last_of(
"/\\");
767 rootPath = rootPath.substr(0, last);
768 rootPath +=
"/storage/";
774TString& GeoSetup::GetTmpPath()
776 if (m_tmpFolder.Length() != 0)
779 UserGroup_t* user = gSystem->GetUserInfo();
780 string login(user->fUser.Data());
781 m_tmpFolder =
"/tmp/" + login +
"/";
782 system(
"mkdir -p " + m_tmpFolder);
788bool GeoSetup::LoadLocalSettings(
const char* fileName)
790 LOG(info)<<
"Load local settings of geometry DB from '"<<fileName<<
"'";
791 TXMLEngine* xml =
new TXMLEngine;
792 XMLDocPointer_t xmldoc = xml->ParseFile(fileName);
794 LOG(error) <<
"Selected file has not XML format: " << fileName;
799 m_moduleEdit.clear();
801 XMLNodePointer_t mainnode = xml->DocGetRootElement(xmldoc);
802 XMLNodePointer_t child = xml->GetChild(mainnode);
804 TString chName = xml->GetNodeName(child);
805 if (chName.CompareTo(
"editSetupModule") == 0) {
811 sNode->
type = TString(
"");
813 XMLAttrPointer_t attr = xml->GetFirstAttr(child);
815 TString aName = xml->GetAttrName(attr);
816 TString aValue = xml->GetAttrValue(attr);
817 if (aName.CompareTo(
"tag") == 0){
819 sNode->
type = aValue;
821 else if (aName.CompareTo(
"moveX") == 0) {
822 sNode->
xMove = aValue.Atof();
823 }
else if (aName.CompareTo(
"moveY") == 0) {
824 sNode->
yMove = aValue.Atof();
825 }
else if (aName.CompareTo(
"moveZ") == 0) {
826 sNode->
zMove = aValue.Atof();
828 else if (aName.CompareTo(
"file_path") == 0){
829 gSystem->ExpandPathName(aValue);
832 else if ((aName.CompareTo(
"enable") == 0)
833 && (aValue.CompareTo(
"false") == 0 || aValue.CompareTo(
"False") == 0
834 || aValue.CompareTo(
"FALSE") == 0))
837 LOG(debug1) <<
"attr: " << xml->GetAttrName(attr) <<
" value: " << xml->GetAttrValue(attr);
839 attr = xml->GetNextAttr(attr);
845 m_moduleEdit.insert(pair<TString, setup_module_ptr>(sNode->
type, sNode));
846 }
else if(chName.CompareTo(
"addModule") == 0){
848 XMLAttrPointer_t attr = xml->GetFirstAttr(child);
850 TString aName = xml->GetAttrName(attr);
851 TString aValue = xml->GetAttrValue(attr);
852 if (aName.CompareTo(
"class_name") == 0)
853 module->class_name = aValue;
854 else if (aName.CompareTo(
"file_path") == 0){
855 gSystem->ExpandPathName(aValue);
856 module->file_path = aValue;
858 else if (aName.CompareTo(
"args") == 0)
859 module->args = aValue;
860 if ((aName.CompareTo(
"active") == 0)
861 && (aValue.CompareTo(
"false") == 0 || aValue.CompareTo(
"False") == 0
862 || aValue.CompareTo(
"FALSE") == 0))
863 module->active = false;
864 attr = xml->GetNextAttr(attr);
866 m_moduleAdd.push_back(module);
868 child = xml->GetNext(child);
871 xml->FreeDoc(xmldoc);
877TString GeoSetup::GetModuleName(
int dbModuleUndex)
879 map<int, TString>::iterator it = m_modules_table.find(dbModuleUndex);
880 if (it != m_modules_table.end())
883 LOG(error) <<
"Unknown module type with id=" << dbModuleUndex;
884 return TString(
"Unknown");
889int GeoSetup::GetModuleIndex(TString& name)
891 map<int, TString>::iterator it;
892 for (it = m_modules_table.begin(); it != m_modules_table.end(); ++it)
893 if ((it->second).CompareTo(name, TString::ECaseCompare::kIgnoreCase) == 0)
900module_r_ptr GeoSetup::GetModuleRevision(
int dbModuleRev)
902 map<int, module_r_ptr>::iterator it = m_modules_revision_table.find(dbModuleRev);
903 if (it != m_modules_revision_table.end())
911int GeoSetup::GetModuleId(
int dbModuleRev)
913 map<int, module_r_ptr>::iterator it = m_modules_revision_table.find(dbModuleRev);
914 if (it != m_modules_revision_table.end())
915 return it->second->idm;
921TString GeoSetup::GetModuleType(
int dbModuleRev)
923 return GetModuleName(GetModuleId(dbModuleRev));
927TGeoVolume* GeoSetup::GetRootVolume(TFile*
f)
930 TGeoManager* geoManager =
nullptr;
931 TList* l =
f->GetListOfKeys();
933 while ((key = (TKey*)next())) {
934 if (strcmp(
key->GetClassName(),
"TGeoManager") != 0)
936 geoManager = (TGeoManager*)
key->ReadObj();
938 return (TGeoVolume*)geoManager->GetNode(0)->GetDaughter(0)->GetVolume();
942 key = (TKey*)l->At(0);
943 TGeoVolume* volume =
dynamic_cast<TGeoVolume*
>(
key->ReadObj());
949bool GeoSetup::AddModuleToFairRunSim(
int dbModuleRev,
const char* path, FairRunSim* fRun)
958 if (mr->
args.Length() > 0) {
959 type_name = mr->
args;
962 type_name = GetModuleType(dbModuleRev);
966 TString command =
"new " + mr->
class_name +
"(\"" + type_name +
"\"";
968 command +=
",kTRUE)";
971 LOG(debug1) <<
"command:" << command;
973 FairModule* obj = (FairModule*)gInterpreter->Calc(command);
975 obj->SetGeometryFileName(path);
976 fRun->AddModule(obj);
981bool GeoSetup::AddModuleToFairRunSim(
module_r_ptr module, FairRunSim* fRun){
982 TString command=
"new " +
module->class_name + "(\"" + module->args + "\"";
984 command +=
",kTRUE)";
987 FairModule* obj = (FairModule*)gInterpreter->Calc(command);
988 obj->SetGeometryFileName(module->
file_path);
989 fRun->AddModule(obj);
990 LOG(info)<<
"\033[0;32mAdded external detector: file_path="<<
module->file_path<<" class_name="<<module->class_name<<" isActive="<<module->active<<"\033[0m";
994TGeoNode* GeoSetup::ConstructRootGeometry(
const char* fileName)
996 TGeoManager* NewGeo = 0;
997 TGeoVolume* volume = 0;
999 TFile*
f =
new TFile(fileName);
1000 TList* l =
f->GetListOfKeys();
1005 while ((key = (TKey*)next())) {
1006 if (strcmp(
key->GetClassName(),
"TGeoManager") != 0) {
1009 NewGeo = (TGeoManager*)
key->ReadObj();
1014 n = NewGeo->GetNode(0);
1019 key = (TKey*)l->At(0);
1020 volume =
dynamic_cast<TGeoVolume*
>(
key->ReadObj());
1024 return volume->GetNode(0);
1032bool GeoSetup::IsEqlMatrix(
const char* fileName, TSQLRow* row)
1034 if (row == NULL || fileName == NULL)
1039 TGeoNode* node = ConstructRootGeometry(fileName);
1042 const Double_t* tran = node->GetMatrix()->GetTranslation();
1044 const Double_t* rot = node->GetMatrix()->GetRotationMatrix();
1046 for (
i = 0;
i < 3;
i++) {
1047 double t = (double)atof(row->GetField(3 +
i));
1049 LOG(debug1) <<
" for i=" <<
i <<
" tran[i]=" << tran[
i] <<
" t=" << t << endl;
1053 for (
i = 0;
i < 9;
i++) {
1054 double t = (double)atof(row->GetField(6 +
i));
1056 LOG(debug1) <<
" for i=" <<
i <<
" rot[i]=" << rot[
i] <<
" t=" << t << endl;
1066int GeoSetup::LoadModuleToFairRunSim(
int idsm, FairRunSim* fRun)
1068 map<int, TString>::iterator it = m_loadedModule.find(idsm);
1069 if (it != m_loadedModule.end())
1072 LOG(debug1) <<
"Start load idsm=" << idsm;
1073 LOG(debug1) <<
"Start load setup_module_id=" << idsm;
1074 ostringstream stringStream;
1075 stringStream <<
"select 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.setup_module_tag "
1076 "from setup_module sm INNER JOIN geometry_file f ON f.file_id=sm.file_id where sm.setup_module_id="
1080 TSQLResult* resQ = m_db->Query(stringStream.str().c_str());
1081 if (resQ == 0 || resQ->GetRowCount() == 0) {
1082 LOG(error) <<
"ERROR: no file for the setup module (" << idsm <<
") was found";
1087 TSQLRow* row = resQ->Next();
1088 int dbModuleIndex = atoi(row->GetField(0));
1089 int idP = atoi(row->GetField(2));
1090 LOG(debug1) <<
"idP=" << idP;
1092 int res = LoadModuleToFairRunSim(idP, fRun);
1097 stringStream.str(
"");
1098 LOG(debug1) << row->GetField(1);
1099 stringStream << GetRootPath().c_str() << row->GetField(1);
1102 TString mName = GetModuleType(dbModuleIndex), out =
"";
1105 if (mName == TString(
"CAVE") || ((TString)stringStream.str()).EndsWith(
".geo"))
1106 AddModuleToFairRunSim(dbModuleIndex, stringStream.str().c_str(), fRun);
1108 double mx = 0, my = 0, mz = 0;
1109 TString smTag=row->GetField(15);
1110 TString file_path=stringStream.str().c_str();
1112 map<TString, setup_module_ptr>::iterator itr = m_moduleEdit.find(smTag);
1113 LOG(debug1) <<
"detector " << dbModuleIndex;
1114 if (itr != m_moduleEdit.end()) {
1115 LOG(debug1) <<
"detector was found";
1116 isEnable=itr->second->enable;
1118 mx = itr->second->xMove;
1119 my = itr->second->yMove;
1120 mz = itr->second->zMove;
1121 if(itr->second->file_path.Length()>0)
1122 file_path=itr->second->file_path;
1123 LOG(debug1) <<
"Move detector " << dbModuleIndex <<
" to x:" << mx <<
" to y:" << my <<
" to z:" << mz;
1127 if (isEnable && mx == 0 && my == 0 && mz == 0 && IsEqlMatrix(stringStream.str().c_str(), row)) {
1128 LOG(debug1) <<
"The same matrix for file=" << stringStream.str().c_str() << endl;
1129 LOG(info)<<
"Loading module with tag="<<row->GetField(15);
1130 AddModuleToFairRunSim(dbModuleIndex, file_path, fRun);
1131 }
else if(isEnable){
1132 TGeoManager* geom =
new TGeoManager(mName, TString::Format(
"Geometry of the %s module", mName.Data()));
1133 LOG(debug1) <<
"import file" << file_path << endl;
1135 TFile*
f =
new TFile(file_path);
1136 TGeoVolume* root_volume = GetRootVolume(
f);
1137 double tx = (double)atof(row->GetField(3)) + mx;
1138 double ty = (double)atof(row->GetField(4)) + my;
1139 double tz = (double)atof(row->GetField(5)) + mz;
1141 TGeoGenTrans* newMatrix =
new TGeoGenTrans(tx, ty, tz, 1, 1, 1, &rotM);
1142 if (strcmp(root_volume->GetNode(0)->ClassName(),
"TGeoNodeMatrix") == 0) {
1143 TGeoNodeMatrix*
v = (TGeoNodeMatrix*)root_volume->GetNode(0);
1145 v->SetMatrix(newMatrix);
1149 out = GetTmpPath() + GetModuleName(GetModuleId(dbModuleIndex)) +
".root";
1150 root_volume->Export(out.Data());
1151 LOG(info)<<
"Loading module with tag="<<row->GetField(15);
1153 AddModuleToFairRunSim(dbModuleIndex, out.Data(), fRun);
1154 LOG(debug1) <<
"Exporting geometry to ROOT file: " << out;
1161 LOG(info)<<
"\033[0;31mDisable module with tag="<<row->GetField(15)<<
" use local settings.\033[0m";
1163 m_loadedModule.insert(pair<int, TString>(idsm, out));
1171int GeoSetup::PutModules()
1173 TSQLResult* resQ = m_db->Query(
"select module_id,module_name from module;");
1174 if (resQ ==
nullptr) {
1175 LOG(error) <<
"ERROR: SQL query was failed. Please, check connection to the Geometry Database and "
1176 "DBL_FILE_PATH variable is correct";
1179 if (resQ->GetRowCount() == 0) {
1180 LOG(error) <<
"ERROR: no modules in the Geometry Database";
1186 while ((row = resQ->Next()) != 0) {
1187 int id = atoi(row->GetField(0));
1188 TString
name(row->GetField(1));
1189 m_modules_table.insert(make_pair(
id, name));
1198int GeoSetup::PutModuleRevisions()
1200 TSQLResult* resQ = m_db->Query(
"select module_class_id,mc.module_id,class_name,revision,active,object_name from "
1201 "module_class as mc INNER JOIN module as m ON mc.module_id=m.module_id;");
1202 if (resQ ==
nullptr) {
1203 LOG(error) <<
"ERROR: SQL query was failed. Please, check connection to the Geometry Database and "
1204 "DBL_FILE_PATH variable is correct";
1207 if (resQ->GetRowCount() == 0) {
1208 LOG(error) <<
"ERROR: no module revisions in the geometry database";
1214 while ((row = resQ->Next()) != 0) {
1216 mr->
idmr = atoi(row->GetField(0));
1217 mr->
idm = atoi(row->GetField(1));
1219 mr->
rev = atoi(row->GetField(3));
1220 if (atoi(row->GetField(4)) == 0)
1224 mr->
args = row->GetField(5);
1226 m_modules_revision_table.insert(make_pair(mr->
idmr, mr));
bool ichar_equals(char a, char b)
void SetFileName(const char *map_file_name)
void SetScale(Double_t factor)
int LoadSetupToGeoManager(string_view setup_tag)
FairField * GetMagneticField(double scale=0)
string_view GetParameterFile(string_view detector_name)
string_view GetLastApprovedSetupTag(string_view tag_prefix)
int LoadSetupToFairRunSim(string_view setupTag, const char *localSettings=nullptr)
const char *const GEO_SERVER_HOST
struct SETUPMODULE setup_module
struct MODULEREVISION module_r