10#include <sys/sendfile.h>
45 const char* geomPath = getenv(
"GEOMPATH");
46 if (geomPath ==
nullptr || geomPath[0] ==
'\0') {
47 std::cerr <<
"Error: GEOMPATH variable is not set. Please, set GEOMPATH to the storage of the local geometry "
52 std::string rootPath = geomPath;
53 if (rootPath.back() !=
'/')
62 while (storagePath.size() > 1 && storagePath.back() ==
'/')
63 storagePath.pop_back();
64 size_t last = storagePath.find_last_of(
'/');
65 return (last == std::string::npos ? std::string(
".") : storagePath.substr(0, last)) +
"/local.db";
70 return (access(name, F_OK) != -1);
75 std::map<int, TString>::iterator it =
m_modules_table.find(dbNoduleUndex);
79 std::cerr <<
"Unknown module type with id=" << dbNoduleUndex << std::endl;
86 std::map<int, TString>::iterator it;
88 if (it->second == name)
108 return it->second.idm;
126 int dbNoduleUndex = mr.
idm;
143 command +=
",kTRUE)";
147 std::cerr <<
"command:" << command << std::endl;
151 obj->SetGeometryFileName(path);
152 fRun->AddModule(obj);
162 std::ostringstream stringStream;
163 stringStream <<
"select idmr,idm,class_name,revision,active,args from module_revision;";
166 std::cout <<
"Error: SQL query was failed. "
167 "Please, check connection to the geometry database and that GEOMPATH variable is correct"
172 std::cout <<
"Error: no module revisions in the geometry database" << std::endl;
177 while ((row = resQ2->
Next()) != 0) {
203 std::ostringstream stringStream;
204 stringStream <<
"select idm,mname from module;";
207 std::cout <<
"Error: SQL query was failed. "
208 "Please, check connection to the geometry database and that GEOMPATH variable is correct"
213 std::cout <<
"Error: no modules in the geometry database" << std::endl;
218 while ((row = resQ2->
Next()) != 0) {
274 TList* l = f->GetListOfKeys();
279 while ((key = (
TKey*)next())) {
316 int source = open(in.
Data(), O_RDONLY, 0);
317 int dest = open(out.
Data(), O_WRONLY | O_CREAT , 0644);
320 struct stat stat_source;
321 fstat(source, &stat_source);
322 sendfile(dest, source, 0, stat_source.st_size);
327bool hasEnding(std::string
const& fullString, std::string
const& ending)
329 if (fullString.length() >= ending.length())
330 return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
340 std::map<int, SETUPMODULE>& moduleMove,
344 std::map<int, TString>::iterator it = loadedModule->find(idsm);
345 if (it != loadedModule->end())
348 std::ostringstream stringStream;
349 stringStream <<
"select f.idm,f.url,sm.idp,sm.tx,sm.ty,sm.tz from setupmodule sm INNER JOIN file f ON f.idf=sm.idf "
355 std::cout <<
"Error: no file for the module was found" << std::endl;
364 std::cerr <<
"idP=" << idP << std::endl;
366 res =
downloadModule(loadedModule, rootDir, db, idP, fRun, moduleMove, iVerbose);
371 std::cerr << row2->
GetField(1) << std::endl;
372 int dbModuleIndex = atoi(row2->
GetField(0));
373 stringStream.str(
"");
375 std::string ends =
".geo";
385 double mx = 0, my = 0, mz = 0;
386 std::map<int, SETUPMODULE>::iterator itr = moduleMove.find(dbModuleIndex);
388 std::cerr <<
"detector " << dbModuleIndex << std::endl;
389 if (itr != moduleMove.end()) {
391 std::cerr <<
"detector was found" << std::endl;
392 if (itr->second.enable) {
393 mx = itr->second.xMove;
394 my = itr->second.yMove;
395 mz = itr->second.zMove;
397 std::cerr <<
"Move detector " << dbModuleIndex <<
" to x:" << mx <<
" to y:" << my <<
" to z:" << mz
405 TFile* f =
new TFile(stringStream.str().c_str());
407 double tx = (double)atoi(row2->
GetField(3)) + mx;
408 double ty = (double)atoi(row2->
GetField(4)) + my;
409 double tz = (double)atoi(row2->
GetField(5)) + mz;
415 v->SetMatrix(newMatrix);
423 std::cerr <<
"Exporting geometry to ROOT file: " << out << std::endl;
429 loadedModule->insert(std::pair<int, TString>(idsm, out));
436int loadSetup(
const char* setupTag,
int subSetup,
const char* fileName = NULL,
int iVerbose = 0)
438 std::map<int, SETUPMODULE> moduleMove;
440 if (fileName != NULL) {
444 std::cerr <<
"Selected file has not XML format: " << fileName << std::endl;
473 if (chName.
CompareTo(
"setupModule") == 0) {
488 else if (aName.
CompareTo(
"moveX") == 0) {
490 }
else if (aName.
CompareTo(
"moveY") == 0) {
492 }
else if (aName.
CompareTo(
"moveZ") == 0) {
504 moduleMove.insert(std::pair<int, SETUPMODULE>(ii, sNode));
515 std::cout <<
"Created new FairRunSim" << std::endl;
517 TString rootPath = getenv(
"TMP_PATH");
518 if (rootPath.
Length() == 0) {
521 rootPath =
"/tmp/" + login +
"/";
522 system(
"mkdir -p " + rootPath);
525 std::ostringstream stringStream;
529 std::cerr <<
"Geometry Database Path: " << stringStream.str().c_str() << std::endl;
530 stringStream.str(
"");
538 <<
"select idsd,status,idma,idfi from setup where stag='" << tag
539 <<
"' order by revision desc;";
542 std::cerr <<
"SQL query was failed while searching the given tag: `" << tag <<
"`" << std::endl;
547 std::cerr <<
"No geometry setup found for the given tag: `" << tag <<
"`" << std::endl;
551 const char* idsd = row->
GetField(0);
555 stringStream.str(
"");
556 stringStream <<
"select idsm from sms where idsd=" << idsd <<
";";
560 resQ = db.
Query(stringStream.str().c_str());
562 std::cout <<
"Error: no modules found" << std::endl;
565 std::map<int, TString>* loadedModule =
new std::map<int, TString>();
566 while ((row = resQ->
Next()) != 0) {
567 stringStream.str(
"");
568 const char* idsm = row->
GetField(0);
569 int idsmI = atoi(idsm);
570 if (subSetup < 0 || subSetup == idsmI) {
571 int res =
downloadModule(loadedModule, rootPath, db, idsmI, fRun, moduleMove, iVerbose);
573 std::cout <<
"Error: no file for the module was found " << std::endl;
607 stringStream.str(
"");
608 stringStream <<
"select matag,url from material where idma=" << idma <<
";";
609 resQ = db.
Query(stringStream.str().c_str());
611 std::cout <<
"Materials did not find" << std::endl;
615 stringStream.str(
"");
618 TString work = getenv(
"GEOMPATH");
622 work = getenv(
"VMCWORKDIR");
623 Mat = work +
"/geometry/";
630 std::string workP(Mat.
Data());
632 std::cout <<
"Working geometry directory: " << workP << std::endl;
633 std::size_t found = workP.find(
'/');
634 if (found != std::string::npos) {
635 while ((found = workP.find(
'/', found + 1)) != std::string::npos) {
637 std::cout <<
"found:" << found << std::endl;
652 std::cout <<
"The setup geometry was successfully loaded" << std::endl;
661 std::srand(time(NULL));
662 int random_variable = std::rand();
663 std::cout << random_variable << std::endl;
664 std::ostringstream stringStream;
667 stringStream <<
"wget -O test2.tar \"" << url <<
"/download_file.php?type=all&tag=all&dirname=" << random_variable
669 system(stringStream.str().c_str());
670 stringStream.str(
"");
671 stringStream <<
"tar -xf test2.tar >/dev/null";
672 system(stringStream.str().c_str());
673 stringStream.str(
"");
674 stringStream <<
"rm test2.tar";
675 system(stringStream.str().c_str());
676 stringStream.str(
"");
678 stringStream <<
gSystem->pwd() <<
"/" << random_variable <<
"/storage/";
679 gSystem->Setenv(
"GEOMPATH", stringStream.str().c_str());
683 stringStream.str(
"");
684 stringStream <<
"rm -rf " << random_variable <<
"/local.db";
685 system(stringStream.str().c_str());
686 stringStream.str(
"");
687 stringStream <<
"rm -rf " << random_variable;
688 system(stringStream.str().c_str());
689 std::cout <<
"GEOMPATH=" <<
getRootPath() << std::endl;
struct SETUPMODULE setup_module
struct SETUPMODULE * setup_module_ptr
struct MODULEREVISION module_r
struct MODULEREVISION * module_r_ptr
R__EXTERN TSystem * gSystem
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
virtual Bool_t cd(const char *path="")
TGeoNode * GetNode(Int_t level) const
TGeoVolume * GetVolume() const
TGeoNode * GetDaughter(Int_t ind) const
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
virtual const char * ClassName() const
virtual void Delete(Option_t *option="")
virtual Int_t GetRowCount() const
virtual TSQLRow * Next()=0
virtual const char * GetField(Int_t field)=0
TSQLResult * Query(const char *sql) final
void Close(Option_t *opt="") final
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
static TString Format(const char *fmt,...)
TString & ReplaceAll(const char *s1, const char *s2)
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)
int downloadModule(std::map< int, TString > *loadedModule, TString &rootDir, TSQLiteServer &db, int idsm, FairRunSim *fRun, std::map< int, SETUPMODULE > &moduleMove, int iVerbose=0)
TString getModuleType(int dbModuleRev)
void copyFile(TString &in, TString &out)
int getModuleIndex(TString &name)
int loadSetup(const char *setupTag, int subSetup, const char *fileName=NULL, int iVerbose=0)
std::pair< int, TString > moduleP
MODULEREVISION getModuleRevision(int dbModuleRev)
std::string getLocalDbPath()
std::map< int, MODULEREVISION > m_modules_revision_table
bool hasEnding(std::string const &fullString, std::string const &ending)
std::map< int, TString > m_modules_table
bool loadFairModule(FairRunSim *fRun, int dbModuleRev, const char *path, double psdx, double psdz, int iVerbose=0)
TGeoVolume * constructRootGeometry(TFile *f)
bool exists_file(const char *name)
TString getModuleName(int dbNoduleUndex)
int putMudules4SQLite(TSQLiteServer &db)
int putMudulesRevision4SQLite(TSQLiteServer &db)
int getModuleId(int dbModuleRev)
std::string getRootPath()