diff --git a/GeoModelDBManager/GeoModelDBManager/GMDBManager.h b/GeoModelDBManager/GeoModelDBManager/GMDBManager.h index aa864bd642c60bffadd64b88ad3032097e612381..8743447184056a06d40e10912e8bd56af10ced45 100644 --- a/GeoModelDBManager/GeoModelDBManager/GMDBManager.h +++ b/GeoModelDBManager/GeoModelDBManager/GMDBManager.h @@ -51,6 +51,10 @@ public: ~GMDBManager(); void DestructorGMDBManagerStd(); + bool initDB(); + + int loadGeoNodeTypesAndBuildCache(); + /** * @brief Returns true if the DB is correctly open @@ -145,35 +149,14 @@ public: int execQuery(std::string queryStr); - // ADD methods - /* single-item adders are not efficient, so they are not used at the moment. We might want to enable them again later if we'll have specific use-cases. - QVariant addShape(const QString &type, const QString ¶meters); - QVariant addMaterial(const QString &name, const QString &density, const QString &elements); - QVariant addElement(const QString &name, const QString &symbol, const QString &elZ, const QString &elA); - QVariant addLogVol(const QString &name, const QVariant &shapeId, const QVariant &materialId); - QVariant addPhysVol(const QVariant &logVolId, const QVariant &parentPhysVolId, bool isRootVolume = false); - QVariant addFullPhysVol(const QVariant &logVolId, const QVariant &parentPhysVolId, bool isRootVolume = false); - QVariant addSerialDenominator(const QString &baseName); - QVariant addFunction(const QString expression); - QVariant addSerialTransformer(const unsigned int &funcId, const unsigned int &physvolId, const std::string &volType, const unsigned int &copies); - QVariant addTransform(QVector<double> parameters); - QVariant addAlignableTransform(QVector<double> parameters); - QVariant addNameTag(const QString &name); - */ - -// bool addListOfRecords(const QString geoType, const std::vector<QStringList> records); bool addListOfRecords(const std::string geoType, const std::vector<std::vector<std::string>> records); -// bool addListOfRecordsToTable(const QString tableName, const std::vector<QStringList> records); -// bool addListOfRecordsToTableStd(const std::string tableName, const std::vector<std::vector<std::string>> records); bool addListOfRecordsToTable(const std::string tableName, const std::vector<std::vector<std::string>> records); // bool addListOfRecordsToTableOld(const QString tableName, const std::vector<QStringList> records); // for the old SQlite only bool addListOfChildrenPositions(const std::vector<std::vector<std::string>> &records); bool addRootVolume(const std::vector<std::string> &values); -// void addChildPosition(const unsigned int &parentId, const std::string &parentType, const unsigned int &childId, const unsigned int &parentCopyNumber, const unsigned int &childPos, const std::string &childType, const unsigned int &childCopyN); - // GET methods @@ -181,9 +164,6 @@ public: std::vector<std::string> getRootPhysVol(); -// std::vector<std::string> getItem(std::string geoType, unsigned int id); -// std::vector<std::string> getItem(unsigned int tableId, unsigned int id); -// std::vector<std::string> getItemFromTableName(std::string tableName, unsigned int id); @@ -191,13 +171,10 @@ public: std::string getNodeTypeFromTableId(unsigned int id); -// QMap<unsigned int, QStringList> getVPhysVolChildren(const unsigned int &id, const std::string &nodeType, const unsigned int ©N); /// methods to dump the DB -// QHash<QString, QMap<unsigned int, QStringList>> getChildrenTable(); std::vector<std::vector<std::string>> getChildrenTable(); -// QHash<unsigned int, QStringList> getTableFromNodeType(QString nodeType); std::vector<std::vector<std::string>> getTableFromNodeType(std::string nodeType); std::unordered_map<unsigned int, std::string> getAll_TableIDsNodeTypes(); @@ -208,7 +185,6 @@ public: private: - bool initDB(); bool createTables(); @@ -218,17 +194,11 @@ private: // void loadTestData(); // for debug only -// void loadTableNamesFromDB(); -// std::vector<std::string> getTableColNamesFromDB(std::string tableName) const; - - int loadGeoNodeTypesAndBuildCache(); - std::string getTableNameFromTableId(unsigned int tabId); unsigned int getTableIdFromNodeType(const std::string &nodeType); void storeNodeType(std::string nodeType, std::string tableName); -// QString getTableNameFromNodeType(QString nodeType); // TODO: to be removed std::string getTableNameFromNodeType(const std::string &nodeType); sqlite3_stmt* selectAllFromTable(std::string tableName) const; @@ -238,7 +208,6 @@ private: void storeTableColumnNames(std::vector<std::string> input); std::vector<std::string> getTableColumnNames(const std::string &tableName); -// void printTableColNamesFromDB(const std::string &tableName) const; void printAllRecords(const std::string &tableName) const; diff --git a/GeoModelDBManager/src/GMDBManager.cpp b/GeoModelDBManager/src/GMDBManager.cpp index d6fa34244f609c81817ff20e0fa9643aa94d58bd..b3d798c78d1ca918f1cb6700e62dad03e6659e39 100644 --- a/GeoModelDBManager/src/GMDBManager.cpp +++ b/GeoModelDBManager/src/GMDBManager.cpp @@ -3,7 +3,7 @@ * author: Riccardo.Maria.Bianchi@cern.ch - 2017 * major updates: * - Aug 2018, R.M.Bianchi -* - May 2020, R.M.Bianchi +* - Jun 2020, R.M.Bianchi */ #include <GeoModelDBManager/GMDBManager.h> @@ -38,6 +38,13 @@ std::vector<std::string> toStdVectorStrings(QStringList qlist) return vec; } +// FIXME: TODO: move to an utility class +std::string getEnvVar( std::string const & key ) +{ + char * val = std::getenv( key.c_str() ); + return val == NULL ? std::string("") : std::string(val); +} + // FIXME: should go to an utility class std::string joinVectorStrings(std::vector<std::string> vec, std::string sep="") { std::string s; @@ -54,55 +61,6 @@ std::string joinVectorStrings(std::vector<std::string> vec, std::string sep="") } - -//GMDBManager::GMDBManager(const std::string &path) : m_dbpath(path), m_SQLiteErrMsg(0), m_dbIsOK(false), m_debug(false) -//{ -// // TODO: move to a more organic way -// #ifdef GEOREAD_DEEP_DEBUG -// m_debug = true; -// #endif -// -// m_dbIsOK = false; -// -// m_db = QSqlDatabase::addDatabase("QSQLITE"); -// m_db.setDatabaseName( QString::fromStdString(path)); -// -// if (!m_db.open()) -// { -// std::cout << "Error: connection with database failed!\n"; -// showError( m_db.lastError() ); -// m_dbIsOK = false; -// } -// else -// { -// m_dbIsOK = true; -// } -// -//// // check if DB has tables, if not create them -// if (m_dbIsOK) { -// if ( ! (initDB()) ) { -// std::cout << "Error: database initialization failed" << std::endl; -// m_dbIsOK = false; -// } -// } -// -// // populate DB with fake data, only for debug -// //loadTestData(); // TODO: we should move initDB() here, only for debug and Write -//} - -//GMDBManager::~GMDBManager() -//{ -// m_db.close(); -//} - - -//bool GMDBManager::isOpen() const -//{ -// if (! m_dbIsOK ) -// return false; -// return m_db.isOpen(); -//} - GMDBManager::GMDBManager(const std::string &path) : m_dbpath(path), m_dbSqlite(nullptr), m_SQLiteErrMsg(0), m_dbIsOK(false), m_debug(false) { // Check if the user asked for running in serial or multi-threading mode @@ -126,12 +84,12 @@ GMDBManager::GMDBManager(const std::string &path) : m_dbpath(path), m_dbSqlite(n m_dbIsOK = false; } - if (m_dbIsOK) { - if ( ! (initDB()) ) { - std::cout << "Error: database initialization failed" << std::endl; - m_dbIsOK = false; - } - } +// if (m_dbIsOK) { +// if ( ! (initDB()) ) { +// std::cout << "Error: database initialization failed" << std::endl; +// m_dbIsOK = false; +// } +// } // populate DB with fake data, only for debug //loadTestData(); // TODO: we should move initDB() here, only for debug and Write @@ -233,7 +191,7 @@ void GMDBManager::printAllRecords(const std::string &tableName) const } std::cout << std::endl; } - std::cout << "---" << endl; + std::cout << "---" << std::endl; // TODO: I want to have a symbol like '---' to mean empty line when query gives 0 results. } @@ -293,134 +251,12 @@ std::vector<std::vector<std::string>> GMDBManager::getTableFromNodeType(std::str std::string tableName = getTableNameFromNodeType(nodeType); return getTableRecords(tableName); - -// // QHash<unsigned int, QStringList> records; -// // QStringList nodeParams; -// std::vector<std::vector<std::string>> records; -// std::vector<std::string> nodeParams; -// -// int nCols = (m_tableNames.at(tableName)).size(); -// -// QSqlQuery query = selectAllFromTable(tableName); // sorted by ID -// while (query.next()) { -// nodeParams.clear(); -//// unsigned int nodeId = query.value(0).toUInt(); -// -// for( int ii=0; ii<nCols; ++ii) { -// // nodeParams << query.value(ii).toString(); -// nodeParams.push_back( query.value(ii).toString().toStdString() ); -// } -// // records[nodeId] = nodeParams; -// records.push_back(nodeParams); -// } -//return records; -} - - -void GMDBManager::showError(const QSqlError &err) const -{ - qWarning() << "Unable to initialize Database" << "Error initializing database: " + err.text(); } -//QVariant GMDBManager::addPhysVol(const QVariant &logVolId, const QVariant &parentPhysVolId, bool isRootVolume) +// TODO: simplify error reporting for SQLite +//void GMDBManager::showError(const QSqlError &err) const //{ -// if (m_debug) std::cout << "GMDBManager::addPhysVol() - is root?" << isRootVolume << std::endl; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into PhysVols(logvol, parent) values(?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(logVolId); -// q.addBindValue(parentPhysVolId); -// q.exec(); -// -// QVariant lastInserted = q.lastInsertId(); -// -// if (isRootVolume) { -// storeRootVolume(lastInserted.toUInt(), "GeoPhysVol"); -// } -// -// return lastInserted; -// -//} - -//QVariant GMDBManager::addFullPhysVol(const QVariant &logVolId, const QVariant &parentPhysVolId, bool isRootVolume) -//{ -// if (m_debug) std::cout << "GMDBManager::addFullPhysVol() - is root?" << isRootVolume << std::endl; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into FullPhysVols(logvol, parent) values(?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(logVolId); -// q.addBindValue(parentPhysVolId); -// q.exec(); -// -// QVariant lastInserted = q.lastInsertId(); -// -// if (isRootVolume) { -// storeRootVolume(lastInserted.toUInt(), "GeoFullPhysVol"); -// } -// -// return lastInserted; -// -//} - - - -//QVariant GMDBManager::addLogVol(const QString &name, const QVariant &shapeId, const QVariant &materialId) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addLogVol()"; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into LogVols(name, shape, material) values(?, ?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(name); -// q.addBindValue(shapeId); -// q.addBindValue(materialId); -// q.exec(); -// return q.lastInsertId(); -//} - -//QVariant GMDBManager::addMaterial(const QString &name, const QString &density, const QString &elements) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addMaterial()"; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into Materials(name, density, elements) values(?, ?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// q.addBindValue(name); -// q.addBindValue(density); -// q.addBindValue(elements); -// q.exec(); -// return q.lastInsertId(); -//} - - -//QVariant GMDBManager::addElement(const QString &name, const QString &symbol, const QString &elZ, const QString &elA) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addElement()"; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into Elements(name, symbol, Z, A) values(?, ?, ?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// q.addBindValue(name); -// q.addBindValue(symbol); -// q.addBindValue(elZ); -// q.addBindValue(elA); -// q.exec(); -// return q.lastInsertId(); +// qWarning() << "Unable to initialize Database" << "Error initializing database: " + err.text(); //} @@ -432,7 +268,6 @@ bool GMDBManager::addListOfChildrenPositions(const std::vector<std::vector<std:: } - bool GMDBManager::addListOfRecords(const std::string geoType, const std::vector<std::vector<std::string>> records) { // if (m_debug) qDebug() << "GMDBManager::addListOfRecords():" << geoType; @@ -450,74 +285,11 @@ bool GMDBManager::addListOfRecords(const std::string geoType, const std::vector< return addListOfRecordsToTable(tableName, records); // better, but needs SQLite >= 3.7.11 //return addListOfRecordsToTableOld(tableName, records); // old SQLite versions } - else - std::cout << "Info: no records to save for geoType '" << geoType << "'. Skipping..." << std::endl; - return true; -} - - - -// ***Note*** -// the syntax built here below is more convenient -// but it is supported only in SQLite >= 3.7.11 -// (Note: old SLC6-based Athena releases uses SQLite 3.7.9 from LCG! -// here we build a query like this: -// queryStr = QString("INSERT INTO Materials (id, name) VALUES (1,'Air'), (2,'Silicon'), (368,'ShieldSteel');"); -// -/* -bool GMDBManager::addListOfRecordsToTable(const std::string tableName, const std::vector<std::vector<std::string>> records) -//bool addListOfRecordsToTable(const std::string tableName, const std::vector<std::vector<std::string>> records) -{ - // get table columns and format them for query - QString tableColString = "(" + QString::fromStdString(joinVectorStrings(m_tableNames.at(tableName), ", ")) + ")"; - - qWarning() << "tableColString:" << tableColString; - - unsigned int nRecords = records.size(); - std::cout << "number of " << tableName << " records to insert into the DB:" << nRecords << std::endl; - - // preparing the SQL query - QString queryStr("INSERT INTO %1 %2 VALUES "); - queryStr = queryStr.arg(QString::fromStdString(tableName)); // insert table name - queryStr = queryStr.arg(tableColString); // insert table columns - - unsigned int nMat = nRecords; - unsigned int id = 0; -// foreach(QStringList rec, records) { - for( const std::vector<std::string>& rec : records) { - //qDebug() << "rec:" << rec; - - ++id; -// QStringList items; - std::vector<std::string> items; - - for ( const std::string& item : rec) { - items.push_back("'" + item + "'"); - } -// QString values = items.join(","); - std::string values = joinVectorStrings(items, ","); - queryStr += " (" + QString::number(id) + "," + QString::fromStdString(values) + ")"; - if (id != nMat) - queryStr += ","; - else - queryStr += ";"; - - } - - qWarning() << "Query string:" << queryStr; - - // executing the SQL query - QSqlQuery q; - if (!q.exec(queryStr)) { - qWarning() << "ERROR!!! SQL error:"; - showError(q.lastError()); - return false; + else { + if(m_debug) std::cout << "Info: no records to save for geoType '" << geoType << "'. Skipping..." << std::endl; } - return true; } -*/ - // ***Note*** @@ -532,7 +304,7 @@ bool GMDBManager::addListOfRecordsToTable(const std::string tableName, const std { // get table columns and format them for query std::string tableColString = "(" + joinVectorStrings(m_tableNames.at(tableName), ", ") + ")"; - //std::cout << "tableColString:" << tableColString << std::endl; // debug + if(m_debug) std::cout << "tableColString:" << tableColString << std::endl; unsigned int nRecords = records.size(); std::cout << "Info: number of " << tableName << " records to dump into the DB:" << nRecords << std::endl; @@ -564,7 +336,7 @@ bool GMDBManager::addListOfRecordsToTable(const std::string tableName, const std return true; } - +// TODO: this is for the old SQLite. Not needed anymore, I guess. ==> Just put a requirement on the newer version of SQLite3 in CMakeLists.txt. Perhaps, also check that GeoModelIO can run smoothly on older ATLAS releases, like 21.9 by taking a newer SQLite3 from LCG. // ***Note*** // old syntax, for SQLite in SLC6 // here below we build the syntax for multiple INSERT @@ -748,184 +520,6 @@ bool GMDBManager::addListOfRecordsToTableOld(const QString tableName, const std: */ - - - -//QVariant GMDBManager::addShape(const QString &type, const QString ¶meters) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addShape()"; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into Shapes(type, parameters) values(?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(type); -// q.addBindValue(parameters); -// q.exec(); -// return q.lastInsertId(); -//} -// -//QVariant GMDBManager::addSerialDenominator(const QString &baseName) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addSerialDenominator("+baseName+")"; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into SerialDenominators(baseName) values(?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(baseName); -// q.exec(); -// return q.lastInsertId(); -//} - -//QVariant GMDBManager::addNameTag(const QString &name) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addNameTag("+name+")"; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into NameTags(name) values(?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// q.addBindValue(name); -// q.exec(); -// return q.lastInsertId(); -//} -// -//QVariant GMDBManager::addFunction(const QString expression) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addFunction()"; -// -// // TEST -// std::cout << "Function - expression string len: " << expression.length(); -// std::cout << "Function - expression: " << expression.toStdString() << std::endl << std::endl; -// //----- -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into Functions(expression) values(?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(expression); -// q.exec(); -// return q.lastInsertId(); -// -// -//} - -//QVariant GMDBManager::addSerialTransformer(const unsigned int &funcId, const unsigned int &physvolId, const std::string &physvolType, const unsigned int &copies) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addSerialTransformer()" << funcId << physvolId << copies; -// -// const unsigned int volTableId = getTableIdFromNodeType(physvolType); -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into SerialTransformers(funcId, volId, volTable, copies) values(?, ?, ?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(funcId); -// q.addBindValue(physvolId); -// q.addBindValue(volTableId); -// q.addBindValue(copies); -// q.exec(); -// return q.lastInsertId(); -//} - -//QVariant GMDBManager::addTransform(QVector<double> params) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addTransform()"; -// -// // get the 12 matrix elements -// double xx = params[0]; -// double xy = params[1]; -// double xz = params[2]; -// -// double yx = params[3]; -// double yy = params[4]; -// double yz = params[5]; -// -// double zx = params[6]; -// double zy = params[7]; -// double zz = params[8]; -// -// double dx = params[9]; -// double dy = params[10]; -// double dz = params[11]; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into Transforms(xx, xy, xz, yx, yy, yz, zx, zy, zz, dx, dy, dz) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(xx); -// q.addBindValue(xy); -// q.addBindValue(xz); -// q.addBindValue(yx); -// q.addBindValue(yy); -// q.addBindValue(yz); -// q.addBindValue(zx); -// q.addBindValue(zy); -// q.addBindValue(zz); -// q.addBindValue(dx); -// q.addBindValue(dy); -// q.addBindValue(dz); -// q.exec(); -// return q.lastInsertId(); -//} -// -// -//QVariant GMDBManager::addAlignableTransform(QVector<double> params) -//{ -//// if (m_debug) qDebug() << "GMDBManager::addAlignableTransform()"; -// -// // get the 12 matrix elements -// double xx = params[0]; -// double xy = params[1]; -// double xz = params[2]; -// -// double yx = params[3]; -// double yy = params[4]; -// double yz = params[5]; -// -// double zx = params[6]; -// double zy = params[7]; -// double zz = params[8]; -// -// double dx = params[9]; -// double dy = params[10]; -// double dz = params[11]; -// -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into AlignableTransforms(xx, xy, xz, yx, yy, yz, zx, zy, zz, dx, dy, dz) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))) { -// showError(q.lastError()); -// return QVariant(); -// } -// -// q.addBindValue(xx); -// q.addBindValue(xy); -// q.addBindValue(xz); -// q.addBindValue(yx); -// q.addBindValue(yy); -// q.addBindValue(yz); -// q.addBindValue(zx); -// q.addBindValue(zy); -// q.addBindValue(zz); -// q.addBindValue(dx); -// q.addBindValue(dy); -// q.addBindValue(dz); -// q.exec(); -// return q.lastInsertId(); -//} - - bool GMDBManager::addRootVolume(const std::vector<std::string> &values) { if (values.size() > 0 ) { @@ -933,58 +527,13 @@ bool GMDBManager::addRootVolume(const std::vector<std::string> &values) const std::string nodeType = values[1]; return storeRootVolume( volId, nodeType ); } - else - std::cout << "WARNING! No records to save for RootVolume!" << std::endl; - return false; + else { + std::cout << "ERROR! No records to save for RootVolume! All GeoModel trees must have a 'World'/'Root' volume! Exiting..." << std::endl; + exit(EXIT_FAILURE); + } } -/* -// insert a single parent-child relationship for a given parent volume -void GMDBManager::addChildPosition(const unsigned int &parentId, const std::string &parentType, const unsigned int &childId, const unsigned int &parentCopyNumber, const unsigned int &childPos, const std::string &childType, const unsigned int &childCopyN) -{ -// if (m_debug) qDebug() << "GMDBManager::addChildPosition(" << parentId << "," << parentType << "," << QString::number(parentCopyNumber) << "," <<childPos << "," << childType << ", " << childId << ", " << QString::number(childCopyN) << ")"; - - // get table name -- which the child ID refers to -- based on the child type - const unsigned int parentTableId = getTableIdFromNodeType(parentType); - const unsigned int childTableId = getTableIdFromNodeType(childType); - - std::string tableName = "ChildrenPositions"; - std::vector<std::string> cols = getTableColumnNames(tableName); - - QString queryStr = QString("insert into %1(%2, %3, %4, %5, %6, %7, %8) values(?, ?, ?, ?, ?, ?, ?)").arg( QString::fromStdString(tableName) ); - for (int i=0; i < cols.size(); ++i) { - if (i != 0) // skip the "id" column - queryStr = queryStr.arg( QString::fromStdString( cols.at(i)) ); - } -// if (m_debug) qDebug() << "queryStr:" << queryStr; - QSqlQuery q; - if (!q.prepare(queryStr)) { - showError(q.lastError()); - return; - } - q.addBindValue(parentId); - q.addBindValue(parentTableId); - q.addBindValue(parentCopyNumber); - q.addBindValue(childPos); - q.addBindValue(childTableId); - q.addBindValue(childId); - q.addBindValue(childCopyN); - q.exec(); - return; -} -*/ -//void GMDBManager::addDBversion(const QString version) -//{ -// QSqlQuery q; -// if (!q.prepare(QLatin1String("insert into dbversion(version) values(?)"))) { -// showError(q.lastError()); -// return; -// } -// q.addBindValue(version); -// q.exec(); -// return; -//} void GMDBManager::addDBversion(std::string version) { checkIsDBOpen(); @@ -993,13 +542,13 @@ void GMDBManager::addDBversion(std::string version) std::string sql = "INSERT INTO dbversion(version) VALUES(?)"; rc = sqlite3_prepare_v2( m_dbSqlite, sql.c_str(), -1, &st, NULL); if (rc != SQLITE_OK) { - printf( "[SQLite ERR] : Error msg: %s\n", sqlite3_errmsg(m_dbSqlite) ); + printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); exit(EXIT_FAILURE); } rc = sqlite3_bind_text(st, 1, version.c_str(), version.length(), SQLITE_TRANSIENT); rc = sqlite3_step( st ); if (rc != SQLITE_DONE) { - printf( "[SQLite ERR] : Error msg: %s\n", sqlite3_errmsg(m_dbSqlite) ); + printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); exit(EXIT_FAILURE); } // finalize @@ -1018,23 +567,17 @@ bool GMDBManager::checkIsDBOpen() const } - - std::vector<std::string> GMDBManager::getItemAndType(unsigned int tableId, unsigned int id) { std::vector<std::string> results; - std::string tableName = getTableNameFromTableId(tableId); std::string nodeType = getNodeTypeFromTableId(tableId); std::vector<std::string> item = getItemFromTableName(tableName, id); - // add the type results.push_back( nodeType ); // add the item results.insert(results.end(), item.begin(), item.end()); - return results; - } @@ -1052,7 +595,7 @@ std::vector<std::string> GMDBManager::getItemFromTableName(std::string tableName int rc = -1; rc = sqlite3_prepare_v2( m_dbSqlite, sql.c_str(), -1, &stmt, NULL); if (rc != SQLITE_OK) { - printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); + printf( "[SQLite ERR] 'prepare' (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); exit(EXIT_FAILURE); } // bind the parameters @@ -1084,10 +627,11 @@ std::vector<std::string> GMDBManager::getItemFromTableName(std::string tableName } } } - if (rc != SQLITE_DONE) { - printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); - exit(EXIT_FAILURE); - } + // TODO: do we need that error check here?? +// if (rc != SQLITE_DONE) { +// printf( "[SQLite ERR] 'step' (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); +//// exit(EXIT_FAILURE); +// } // finalize sqlite3_finalize(stmt); @@ -1097,150 +641,13 @@ std::vector<std::string> GMDBManager::getItemFromTableName(std::string tableName } return item; - - /* - QString queryStr = QString("SELECT * FROM %1 WHERE id = (?)").arg(QString::fromStdString(tableName)); - // prepare the query - QSqlQuery q; - if (!q.prepare( queryStr )) { - showError(q.lastError()); -// return QStringList(); - exit(EXIT_FAILURE); - } - q.addBindValue(id); - q.exec(); - - std::vector<std::string> params; - - // get the number of columns of the DB table - int nCols = (m_tableNames.at(tableName)).size(); - - while (q.next()) { - - for( int ii=0; ii<nCols; ++ii) - params.push_back( q.value(ii).toString().toStdString() ); - } - if (params.size()==0) { - std::cout << "WARNING!!" << "Item" << id << "does not exist in table" << tableName << "!!"; - } - return params; - */ } -/* -// get the list of children for a single GeoVPhysVol (i.e., GeoPhysVol or GeoFullPhysVol) -QMap<unsigned int, QStringList> GMDBManager::getVPhysVolChildren(const unsigned int &id, const std::string &nodeType, const unsigned int &parentCopyNumber) -{ - if (m_debug) std::cout << "GMDBManager::getVPhysVolChildren() - id:" << id << "- type:" << nodeType << "- copyN:" << parentCopyNumber; - - const unsigned int tableId = getTableIdFromNodeType(nodeType); - - // get children and their positions - QSqlQuery q; - if (!q.prepare(QLatin1String("SELECT * FROM ChildrenPositions WHERE parentId = (?) AND parentTable = (?) AND parentCopyNumber = (?)"))) { - showError(q.lastError()); - return QMap<unsigned int, QStringList>(); - } - q.addBindValue(id); - q.addBindValue(tableId); - q.addBindValue(parentCopyNumber); - q.exec(); - - QMap<unsigned int, QStringList> children; - QStringList childParams; - - // get the number of columns of the DB table - int nCols = m_tableNames["ChildrenPositions"].size(); - - while (q.next()) { - - childParams.clear(); - - unsigned int childPos = q.value(3).toUInt(); - - for( int ii=0; ii<nCols; ++ii) - childParams << q.value(ii).toString(); - - children[childPos] = childParams; - } - - return children; -} - */ - - -/* -// Get all parent-children data from the database in one go -QHash<QString, QMap<unsigned int, QStringList>> GMDBManager::getChildrenTable() -{ - QSqlQuery q = selectAllFromTable("ChildrenPositions"); - - QHash< QString, QMap<unsigned int, QStringList> > all_children; // to store all children - // QMap<unsigned int, QStringList> children; // to temporarily store the children of one parent - QStringList childParams; // to temporarily store the children parameters - - // get the number of columns of the DB table - int nCols = m_tableNames["ChildrenPositions"].size(); - - // loop over all children's positions stored in the DB - while (q.next()) { - - childParams.clear(); - - QString parentId = q.value(1).toString(); - QString parentTable = q.value(2).toString(); - QString parentCopyNumber = q.value(3).toString(); - unsigned int childPos = q.value(4).toUInt(); - - QString key = parentId + ":" + parentTable + ":" + parentCopyNumber; - - for( int ii=0; ii<nCols; ++ii) { - childParams << q.value(ii).toString(); - } - - all_children[key][childPos] = childParams; - } - return all_children; -} -*/ // Get all parent-children data from the database in one go std::vector<std::vector<std::string>> GMDBManager::getChildrenTable() { return getTableRecords("ChildrenPositions"); - - /* - QSqlQuery q; - QString queryStr = QString("SELECT * FROM ChildrenPositions ORDER BY parentTable, parentId, parentCopyNumber, position"); - if (!q.prepare(queryStr)) { - showError(q.lastError()); - } - q.exec(); - - std::vector<std::vector<std::string>> all_children; // to store all children - std::vector<std::string> childParams; // to temporarily store the children parameters - - // get the number of columns of the DB table - int nCols = m_tableNames["ChildrenPositions"].size(); - // std::cout << "num of columns in childrenPos table" << nCols << std::endl; // debug - - // loop over all children's positions stored in the DB - while (q.next()) { - - childParams.clear(); - - for( int ii=0; ii<nCols; ++ii) { - childParams.push_back( q.value(ii).toString().toStdString() ); - } - -// // debug -// if(childParams[2]=="8920") -// std::cout << "CoolingTube parent:"; print childParams; - - all_children.push_back(childParams); - } - return all_children; - */ } @@ -1248,23 +655,6 @@ std::vector<std::vector<std::string>> GMDBManager::getChildrenTable() unsigned int GMDBManager::getTableIdFromNodeType(const std::string &nodeType) { return m_cache_nodeType_tableID[nodeType]; - - /* - QSqlQuery q; - if (!q.prepare(QLatin1String("SELECT id FROM GeoNodesTypes WHERE nodeType = (?)"))) { - showError(q.lastError()); - exit(EXIT_FAILURE); - } - q.addBindValue(QString::fromStdString(nodeType)); - q.exec(); - - QVariant id; - while (q.next()) { - id = q.value(0); - } - - return id.toUInt(); - */ } @@ -1274,27 +664,6 @@ std::string GMDBManager::getTableNameFromNodeType(const std::string &nodeType) } - -/* -QString GMDBManager::getTableNameFromTableId(unsigned int tabId) -{ - QSqlQuery q; - if (!q.prepare(QLatin1String("SELECT tableName FROM GeoNodesTypes WHERE id = (?)"))) { - showError(q.lastError()); - return QString(); - } - q.addBindValue(tabId); - q.exec(); - - QString tableName; - while (q.next()) { - tableName = q.value(0).toString(); - } - - return tableName; -} - */ - // build the GeoNodeTypes cache int GMDBManager::loadGeoNodeTypesAndBuildCache() { @@ -1311,7 +680,7 @@ int GMDBManager::loadGeoNodeTypesAndBuildCache() // prepare the query rc = sqlite3_prepare_v2( m_dbSqlite, sql.c_str(), -1, &st, NULL); if (rc != SQLITE_OK) { - printf( "[SQLite ERR] : Error msg: %s\n", sqlite3_errmsg(m_dbSqlite) ); + printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); exit(EXIT_FAILURE); } // execute the statement until all selected records are processed @@ -1320,7 +689,7 @@ int GMDBManager::loadGeoNodeTypesAndBuildCache() id = sqlite3_column_int(st, 0); nodeType = std::string(reinterpret_cast<const char*>(sqlite3_column_text(st, 1))); tableName = std::string(reinterpret_cast<const char*>(sqlite3_column_text(st, 2))); -// std::cout << "row: " << id << "," << nodeType << "," << tableName << std::endl; // debug + if(m_debug) std::cout << "row: " << id << "," << nodeType << "," << tableName << std::endl; // fill the caches m_cache_tableId_tableName.insert( std::pair<unsigned int, std::string>(id, tableName)); m_cache_tableId_nodeType.insert( std::pair<unsigned int, std::string>(id, nodeType)); @@ -1337,6 +706,7 @@ int GMDBManager::loadGeoNodeTypesAndBuildCache() return rc; } +//FIXME: should throw an error if the method `loadGeoNodeTypesAndBuildCache()` was not called before calling this method std::string GMDBManager::getTableNameFromTableId(unsigned int tabId) { return m_cache_tableId_tableName[tabId]; @@ -1368,45 +738,20 @@ sqlite3_stmt* GMDBManager::selectAllFromTable(std::string tableName) const sqlite3_stmt* GMDBManager::selectAllFromTableSortBy(std::string tableName, std::string sortColumn) const { checkIsDBOpen(); - if ("" == sortColumn || 0 == sortColumn.size()) { sortColumn = "id"; } - - sqlite3_stmt * st = nullptr; - int rc = -1; + sqlite3_stmt * st = nullptr; // SQLite statement to be returned + int rc = -1; // SQLite return code //set the SQL query string std::string sql = fmt::format("SELECT * FROM {0} ORDER BY {1}", tableName, sortColumn); // prepare the query rc = sqlite3_prepare_v2( m_dbSqlite, sql.c_str(), -1, &st, NULL); if (rc != SQLITE_OK) { - printf( "[SQLite ERR] : Error msg: %s\n", sqlite3_errmsg(m_dbSqlite) ); + printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); exit(EXIT_FAILURE); } return st; - -// QSqlQuery q; -// -// if ("" == sortColumn || 0 == sortColumn.size()) { -// sortColumn = "id"; -// } -// QString qsortColumn = QString::fromStdString(sortColumn); -// -// QStringList args; -// args << QString::fromStdString(tableName) << qsortColumn; -// -// QString queryStr = QString("SELECT * FROM %1 ORDER BY %2"); -//// queryStr = queryStr.arg(tableName); -// for (int i=0; i < args.size(); ++i) { -// queryStr = queryStr.arg( args.at(i) ); -// } -// -// if (!q.prepare(queryStr)) { -// showError(q.lastError()); -// return QSqlQuery(); -// } -// q.exec(); -// return q; } @@ -1420,7 +765,7 @@ sqlite3_stmt* GMDBManager::selectAllFromTableChildrenPositions() const // prepare the query rc = sqlite3_prepare_v2( m_dbSqlite, sql.c_str(), -1, &st, NULL); if (rc != SQLITE_OK) { - printf( "[SQLite ERR] : Error msg: %s\n", sqlite3_errmsg(m_dbSqlite) ); + printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); exit(EXIT_FAILURE); } return st; @@ -1433,19 +778,18 @@ bool GMDBManager::initDB() bool tablesOK = createTables(); // store DB version addDBversion(dbversion); - // build caches - loadGeoNodeTypesAndBuildCache(); return tablesOK; - } + + void GMDBManager::printAllDBTables() { - if(m_cache_tables.size() == 0) - getAllDBTables(); - + if(m_cache_tables.size() == 0) { + getAllDBTables(); // load tables and build the cache + } for( auto& str : m_cache_tables ) std::cout << str << std::endl; } @@ -1767,7 +1111,6 @@ void GMDBManager::storeTableColumnNames(std::vector<std::string> input) std::vector<std::string> GMDBManager::getTableColumnNames(const std::string &tableName) { - // qDebug() << "GMDBManager::getTableColumnNames()"; return m_tableNames.at(tableName); } @@ -1781,7 +1124,7 @@ void GMDBManager::storeNodeType(std::string nodeType, std::string tableName) std::string sql = "INSERT INTO GeoNodesTypes(nodeType, tableName) VALUES(?, ?)"; rc = sqlite3_prepare_v2( m_dbSqlite, sql.c_str(), -1, &st, NULL); if (rc != SQLITE_OK) { - printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); // TODO: add __func__ to all error messages, as I did here + printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); exit(EXIT_FAILURE); } if(m_debug) std::cout << "storeNodeType - Query string:" << sql << std::endl; // debug @@ -1791,29 +1134,16 @@ void GMDBManager::storeNodeType(std::string nodeType, std::string tableName) // execute the query rc = sqlite3_step( st ); if (rc != SQLITE_DONE) { - printf( "[SQLite ERR] : Error msg: %s\n", sqlite3_errmsg(m_dbSqlite) ); + printf( "[SQLite ERR] (%s) : Error msg: %s\n", __func__, sqlite3_errmsg(m_dbSqlite) ); exit(EXIT_FAILURE); } // finalize sqlite3_finalize(st); return; - - /* - QSqlQuery q; - if (!q.prepare(QLatin1String("insert into GeoNodesTypes(nodeType, tableName) values(?, ?)"))) { - showError(q.lastError()); - return; - } - - q.addBindValue(QString::fromStdString(nodeType)); - q.addBindValue(QString::fromStdString(tableName)); - q.exec(); - return; - */ } - +// TODO: move to sqlite3 library //void GMDBManager::loadTestData() //{ // // create test data @@ -1842,31 +1172,6 @@ void GMDBManager::storeNodeType(std::string nodeType, std::string tableName) //} -//std::vector<std::string> GMDBManager::getTableColNamesFromDB(std::string tableName) const -//{ -// std::vector<std::string> colNames; -// colNames. push_back( tableName ); -// -// // fetch the driver -// QSqlDriver* driver = m_db.driver(); -// QSqlRecord record = driver->record(QString::fromStdString(tableName)); -// unsigned int colN = record.count(); -// for (unsigned int i = 0; i < colN; ++i) -// colNames.push_back( record.fieldName(i).toStdString() ); -// return colNames; -//} - - - - - -//void GMDBManager::printTableColNamesFromDB(const std::string &tableName) const -//{ -// // JFB commented: qDebug() << "GMDBManager::printTableColNames:" << tableName; -// -// // fetch the driver -// // JFB commented: qDebug() << getTableColNamesFromDB(tableName); -//} bool GMDBManager::storeRootVolume(const unsigned int &id, const std::string &nodeType) { @@ -1899,46 +1204,14 @@ bool GMDBManager::storeRootVolume(const unsigned int &id, const std::string &nod } // finalize sqlite3_finalize(st); - - /* - // prepare the query string - QString queryStr = QString("insert into %1(%2, %3) values(?, ?)").arg( QString::fromStdString(tableName) ); - for (int i=0; i < cols.size(); ++i) { - if (i != 0) // skip the "id" column - queryStr = queryStr.arg( QString::fromStdString(cols.at(i)) ); - } - //qDebug() << "queryStr:" << queryStr; - // prepare the query - QSqlQuery q; - if (!q.prepare(queryStr)) { - showError(q.lastError()); - return false; - } - q.addBindValue(id); - q.addBindValue(typeId); - // run the query - q.exec(); - */ - + + if (rc != 0) { + return false; + } return true; } -/* -std::vector<std::string> GMDBManager::getRootPhysVol() -{ - // get the ID of the ROOT vol from the table "RootVolume" - // QSqlQuery query("SELECT * FROM RootVolume"); - QSqlQuery query = selectAllFromTable("RootVolume"); - - QVariant id; - QVariant typeId; - while (query.next()) { - id = query.value(1); - typeId = query.value(2); - } - return getItemAndType(typeId.toUInt(), id.toUInt()); -} -*/ + std::vector<std::string> GMDBManager::getRootPhysVol() { @@ -1972,7 +1245,7 @@ std::string GMDBManager::getDBFilePath() return m_dbpath; } -// TODO: move to an utility class +// FIXME: TODO: move to an utility class int lastIndexOf(std::vector<std::string> v, std::string str, int pos = 0) { auto it = std::find(std::next(v.rbegin(), v.size() - pos), v.rend(), str); if (it != v.rend()) diff --git a/GeoModelRead/src/ReadGeoModel.cpp b/GeoModelRead/src/ReadGeoModel.cpp index 1ef613cb69913d22c51f4e84a82e40c85628d423..626bad84a2e69fd6e85059f2d7510be6b8d1bbac 100644 --- a/GeoModelRead/src/ReadGeoModel.cpp +++ b/GeoModelRead/src/ReadGeoModel.cpp @@ -103,7 +103,7 @@ ReadGeoModel::ReadGeoModel(GMDBManager* db, unsigned long* progress) : m_deepDeb std::cout << "You defined the GEOMODEL_IO_READ_DEBUG_VERBOSE variable, so you will see a verbose output." << std::endl; } // Check if the user asked for timing output - if ( "" != getEnvVar("GEOMODEL_ENV_IO_TIMING")) { + if ( "" != getEnvVar("GEOMODEL_ENV_IO_READ_TIMING")) { m_timing = true; std::cout << "You defined the GEOMODEL_IO_READ_TIMING variable, so you will see a timing measurement in the output." << std::endl; } @@ -112,30 +112,33 @@ ReadGeoModel::ReadGeoModel(GMDBManager* db, unsigned long* progress) : m_deepDeb m_progress = progress; } - // set the geometry file + // open the geometry file m_dbManager = db; - if (m_dbManager->isOpen()) { - if (m_deepDebug) std::cout << "OK! Database is open!"; + if (m_dbManager->checkIsDBOpen()) { + if (m_debug) std::cout << "OK! Database is open!"; } else { std::cout << "ERROR!! Database is NOT open!"; return; } + // build caches + m_dbManager->loadGeoNodeTypesAndBuildCache(); + // Check if the user asked for running in serial or multi-threading mode if ( "" != getEnvVar("GEOMODEL_ENV_IO_NTHREADS")) { int nThreads = std::stoi(getEnvVar("GEOMODEL_ENV_IO_NTHREADS")); if (nThreads == 0) { - std::cout << "\nYou set the GEOMODEL_ENV_IO_NTHREADS to " << nThreads << "; thus, GeoModelIO will be run in serial mode." << std::endl; + std::cout << "Info: You set the GEOMODEL_ENV_IO_NTHREADS to '" << nThreads << "'; thus, GeoModelIO will be run in serial mode." << std::endl; m_runMultithreaded_nThreads = nThreads; m_runMultithreaded = false; } else if( nThreads > 0 ) { - std::cout << "\nYou set the GEOMODEL_ENV_IO_NTHREADS to " << nThreads << "; thus, GeoModelIO will use that number of worker threads." << std::endl; + std::cout << "Info: You set the GEOMODEL_ENV_IO_NTHREADS to '" << nThreads << "'; thus, GeoModelIO will use that number of worker threads." << std::endl; m_runMultithreaded_nThreads = nThreads; m_runMultithreaded = true; } else if (nThreads == -1) { - std::cout << "\nYou set the GEOMODEL_ENV_IO_NTHREADS to " << nThreads << "; thus, GeoModelIO will use the number of threads supported by the platform." << std::endl; + std::cout << "Info: You set the GEOMODEL_ENV_IO_NTHREADS to '" << nThreads << "'; thus, GeoModelIO will use the number of threads supported by the platform." << std::endl; m_runMultithreaded_nThreads = nThreads; m_runMultithreaded = true; } @@ -151,7 +154,7 @@ ReadGeoModel::~ReadGeoModel() { // FIXME: some cleaning...?? } - + // FIXME: TODO: move to an utility class std::string ReadGeoModel::getEnvVar( std::string const & key ) const { char * val = std::getenv( key.c_str() ); @@ -180,7 +183,7 @@ GeoPhysVol* ReadGeoModel::buildGeoModel() GeoPhysVol* ReadGeoModel::buildGeoModelPrivate() { // *** get all data from the DB *** - + std::chrono::system_clock::time_point start = std::chrono::system_clock::now(); // timing: get start time // get all GeoModel nodes from the DB m_logVols = m_dbManager->getTableFromNodeType("GeoLogVol"); m_shapes = m_dbManager->getTableFromNodeType("GeoShape"); @@ -194,27 +197,27 @@ GeoPhysVol* ReadGeoModel::buildGeoModelPrivate() m_serialDenominators = m_dbManager->getTableFromNodeType("GeoSerialDenominator"); m_serialTransformers = m_dbManager->getTableFromNodeType("GeoSerialTransformer"); m_nameTags = m_dbManager->getTableFromNodeType("GeoNameTag"); - // get the children table from DB - m_allchildren = m_dbManager->getChildrenTableStd(); - + m_allchildren = m_dbManager->getChildrenTable(); // get the root volume data -// m_root_vol_data = m_dbManager->getRootPhysVol(); m_root_vol_data = m_dbManager->getRootPhysVol(); - // get DB metadata m_tableID_toTableName = m_dbManager->getAll_TableIDsNodeTypes(); m_tableName_toTableID = m_dbManager->getAll_NodeTypesTableIDs(); - + + auto end = std::chrono::system_clock::now(); // timing: get end time + auto diff = std::chrono::duration_cast < std::chrono::seconds > (end - start).count(); + if (m_timing || m_debug || m_deepDebug) { + std::cout << "*** Time taken to fetch GeoModel data from the database: " << diff << " [s]" << std::endl; + } // *** build all nodes *** - + start = std::chrono::system_clock::now(); // timing: get start time // parallel mode: - if (m_runMultithreaded) { std::cout << "Building nodes concurrently..." << std::endl; std::thread t2(&ReadGeoModel::buildAllElements, this); -// std::thread t7(&ReadGeoModel::buildAllFunctions, this); + // std::thread t7(&ReadGeoModel::buildAllFunctions, this); // FIXME: implement cache for Functions std::thread t8(&ReadGeoModel::buildAllTransforms, this); std::thread t9(&ReadGeoModel::buildAllAlignableTransforms, this); @@ -266,7 +269,21 @@ GeoPhysVol* ReadGeoModel::buildGeoModelPrivate() buildAllFullPhysVols(); buildAllSerialTransformers(); } + end = std::chrono::system_clock::now(); // timing: get end time + diff = std::chrono::duration_cast < std::chrono::seconds > (end - start).count(); + if (m_timing || m_debug || m_deepDebug) { + std::cout << "*** Time taken to build all GeoModel nodes: " << diff << " [s]" << std::endl; + } + + // *** recreate all mother-daughter relatioships between nodes *** + start = std::chrono::system_clock::now(); // timing: get start time loopOverAllChildrenInBunches(); + end = std::chrono::system_clock::now(); // timing: get end time + diff = std::chrono::duration_cast < std::chrono::seconds > (end - start).count(); + if (m_timing || m_debug || m_deepDebug) { + std::cout << "*** Time taken to recreate all mother-daughter relationships between nodes of the GeoModel tree: " << diff << " [s]" << std::endl; + } + return getRootVolume(); } @@ -284,22 +301,22 @@ GeoPhysVol* ReadGeoModel::buildGeoModelPrivate() muxCout.unlock(); } - // Get Start Time - std::chrono::system_clock::time_point start = std::chrono::system_clock::now(); +// // Get Start Time +// std::chrono::system_clock::time_point start = std::chrono::system_clock::now(); for ( auto& record : records ) { processParentChild( record ); } - // Get End Time - auto end = std::chrono::system_clock::now(); - auto diff = std::chrono::duration_cast < std::chrono::seconds > (end - start).count(); - - if (m_timing || m_debug || m_deepDebug) { - muxCout.lock(); - std::cout << "Time Taken to process " << nChildrenRecords << " parent-child relationships = " << diff << " Seconds" << std::endl; - muxCout.unlock(); - } +// // Get End Time +// auto end = std::chrono::system_clock::now(); +// auto diff = std::chrono::duration_cast < std::chrono::seconds > (end - start).count(); +// +// if (m_timing || m_debug || m_deepDebug) { +// muxCout.lock(); +// std::cout << "Time Taken to process " << nChildrenRecords << " parent-child relationships = " << diff << " Seconds" << std::endl; +// muxCout.unlock(); +// } } @@ -470,6 +487,7 @@ void ReadGeoModel::buildAllSerialTransformers() if (nSize>0) std::cout << "All " << nSize << " SerialTransformers have been built!\n"; } + // FIXME: implement build function and cache for Functions // //! Iterate over the list of nodes, build them all, and store their pointers // void ReadGeoModel::buildAllFunctions() // { @@ -599,7 +617,7 @@ void ReadGeoModel::loopOverAllChildrenInBunches() const unsigned int parentCopyN = std::stoi(parentchild[3]); // get the child's position in the parent's children list - const unsigned int position = std::stoi(parentchild[4]); + //const unsigned int position = std::stoi(parentchild[4]); // unused // get the child's details const unsigned int childTableId = std::stoi(parentchild[5]); @@ -815,7 +833,7 @@ GeoPhysVol* ReadGeoModel::getRootVolume() std::cout << "ReadGeoModel::getRootVolume()" << std::endl; muxCout.unlock(); } - const unsigned int id = std::stoi(m_root_vol_data[1]); // TODO: GetRoot() should return integers instead of strings... + const unsigned int id = std::stoi(m_root_vol_data[1]); // TODO: GeoModel GetRoot() should return integers instead of strings... const unsigned int tableId = std::stoi(m_root_vol_data[2]); const unsigned int copyNumber = 1; // the Root volume has only one copy by definition GeoPhysVol* root = dynamic_cast<GeoPhysVol*>(buildVPhysVolInstance(id, tableId, copyNumber)); @@ -837,11 +855,6 @@ GeoMaterial* ReadGeoModel::buildMaterial(const unsigned int id) } std::vector<std::string> values = m_materials[id-1]; -// if (m_deepDebug) { -// muxCout.lock(); -// std::cout << "mat values=" << values << std::endl; -// muxCout.unlock(); -// } const unsigned int matId = std::stoi(values[0]); const std::string matName = values[1]; double matDensity = std::stod(values[2]); @@ -936,7 +949,7 @@ std::string ReadGeoModel::getShapeType(const unsigned int shapeId) - + //TODO: move shapes in different files, so code here is more managable /// Recursive function, to build GeoShape nodes GeoShape* ReadGeoModel::buildShape(const unsigned int shapeId, type_shapes_boolean_info* shapes_info_sub) { @@ -1118,7 +1131,8 @@ GeoShape* ReadGeoModel::buildShape(const unsigned int shapeId, type_shapes_boole if(error) { muxCout.lock(); - // FIXME: std::cout << "ERROR! GeoPcon 'ZRmin' and 'ZRmax' values are not at the right place! --> " << shapePars << std::endl; + std::cout << "ERROR! GeoPcon 'ZRmin' and 'ZRmax' values are not at the right place! --> "; + printStdVectorStrings(shapePars); muxCout.unlock(); } @@ -1127,7 +1141,8 @@ GeoShape* ReadGeoModel::buildShape(const unsigned int shapeId, type_shapes_boole } else { error = 1; muxCout.lock(); - // FIXME: std::cout << "ERROR! GeoPcon 'ZPos' value is not at the right place! --> " << shapePars << std::endl; + std::cout << "ERROR! GeoPcon 'ZPos' value is not at the right place! --> "; + printStdVectorStrings(shapePars); muxCout.unlock(); } } @@ -1857,7 +1872,7 @@ GeoShape* ReadGeoModel::buildShape(const unsigned int shapeId, type_shapes_boole // otherwise, build the operands else { - // TODO: IMPORTANT!!! --> check how the transf used in shift are saved into the DB, because they are bare transf and not GeoTransform nodes... I fear thay are wrongly stored... + // TODO: IMPORTANT!!! --> check how the transf used in shift are saved into the DB, because they are bare transf and not GeoTransform nodes... // first, check if the transform is built; // if so, use that; @@ -2305,6 +2320,9 @@ std::vector<std::string> ReadGeoModel::splitString(const std::string& s, const c // } // return vec; //} + + + // TODO: move this to utility class/file void ReadGeoModel::printStdVectorStrings(std::vector<std::string> vec) { @@ -2404,21 +2422,12 @@ GeoLogVol* ReadGeoModel::buildLogVol(const unsigned int id) // get logVol properties from the DB std::vector<std::string> values = m_logVols[id-1]; - if (m_deepDebug) { - muxCout.lock(); - //FIXME: std::cout << "buildLogVol() - params:" << values << std::endl; - muxCout.unlock(); - } // get the parameters to build the GeoLogVol node std::string logVolName = values[1]; // build the referenced GeoShape node const unsigned int shapeId = std::stoi(values[2]); -// type_shapes_boolean_info shapes_info_sub; // tuple to store the boolean shapes to complete at a second stage -// GeoShape* shape = buildShape(shapeId, &shapes_info_sub); - // now, create the missing operand shapes for boolean/operator shapes -// createBooleanShapeOperands(&shapes_info_sub); GeoShape* shape = getBuiltShape(shapeId); if(!shape) { std::cout << "ERROR!! While building a LogVol, Shape is NULL! Exiting..." <<std::endl; @@ -2432,7 +2441,6 @@ GeoLogVol* ReadGeoModel::buildLogVol(const unsigned int id) std::cout << "buildLogVol() - material Id:" << matId << std::endl; muxCout.unlock(); } -// GeoMaterial* mat = buildMaterial(matId); GeoMaterial* mat = getBuiltMaterial(matId); if(!mat) { std::cout << "ERROR!! While building a LogVol, Material is NULL! Exiting..." <<std::endl; diff --git a/GeoModelWrite/src/WriteGeoModel.cpp b/GeoModelWrite/src/WriteGeoModel.cpp index 64c60b062a33a65a460ce691610a01c722ba1d4c..cda91242f44be35b723a7c9e0a3811006e146b99 100644 --- a/GeoModelWrite/src/WriteGeoModel.cpp +++ b/GeoModelWrite/src/WriteGeoModel.cpp @@ -92,7 +92,6 @@ namespace GeoModelIO { std::unordered_map<std::string, unsigned int>::iterator it = m_volumeCopiesMap.find(key); if ( it == m_volumeCopiesMap.end() ) { -// if ( ! (m_volumeCopiesMap.contains(key)) ) { m_volumeCopiesMap[key] = 1; } else { ++m_volumeCopiesMap[key]; @@ -103,13 +102,11 @@ namespace GeoModelIO { unsigned int WriteGeoModel::getLatestParentCopyNumber(const unsigned int &parentId, const std::string &parentType) { - //JFB Commented out: qDebug() << "WriteGeoModel::getLatestParentCopyNumber()"; const unsigned int tableId = getIdFromNodeType(parentType); std::string key = std::to_string(tableId) + ":" + std::to_string(parentId); std::unordered_map<std::string, unsigned int>::iterator it = m_volumeCopiesMap.find(key); if ( it == m_volumeCopiesMap.end() ) { -// if ( ! (m_volumeCopiesMap.contains(key)) ) { std::cout << "ERROR!!! Something's wrong in storing the number of copies!" << std::endl; } return m_volumeCopiesMap[key]; @@ -118,23 +115,18 @@ namespace GeoModelIO { void WriteGeoModel::handlePhysVol (const GeoPhysVol *vol) { - //JFB Commented out: qDebug() << "\nWriteGeoModel::handlePhysVol(GeoPhysVol*)"; handleVPhysVolObjects( vol ); } void WriteGeoModel::handleFullPhysVol (const GeoFullPhysVol *vol) { - //JFB Commented out: qDebug() << "\nWriteGeoModel::handleFullPhysVol( GeoFullPhysVol* )"; handleVPhysVolObjects( vol ); } void WriteGeoModel::handleVPhysVolObjects(const GeoVPhysVol* vol) { - //JFB Commented out: qDebug() << "WriteGeoModel::handleVPhysVolObjects( GeoVPhysVol* )"; - - // get the address string for the current volume std::string address = getAddressStringFromPointer( vol ); @@ -166,8 +158,7 @@ void WriteGeoModel::handleVPhysVolObjects(const GeoVPhysVol* vol) if (len > 1) { doGetParentNode = true; // TODO: is that needed???? } else{ - //JFB Commented out: qDebug() << "This is the Root volume!"; - isRootVolume = true; + isRootVolume = true; m_rootVolumeFound = true; storeRootVolume = true; doGetParentNode = false; @@ -181,11 +172,11 @@ void WriteGeoModel::handleVPhysVolObjects(const GeoVPhysVol* vol) // If we do, the ID of the parent of the SerialTransformer is returned, which is wrong. if (m_unconnectedTree) { doGetParentNode = false; - //JFB Commented out: qDebug() << "Handling an unconnected tree: we skip the parent-finding step..."; + //qDebug() << "Handling an unconnected tree: we skip the parent-finding step..."; // now, we reset the status, // otherwise all the children of the first referenced, unconnected volume // will be treated as unconnected as well - //JFB Commented out: qDebug() << "setting 'unconnected' to: false"; + //qDebug() << "setting 'unconnected' to: false"; m_unconnectedTree = false; } @@ -204,13 +195,13 @@ void WriteGeoModel::handleVPhysVolObjects(const GeoVPhysVol* vol) if (doGetParentNode) { // bool isShared = vol->isShared(); - //JFB Commented out: qDebug() << "is this node shared?" << isShared; + //qDebug() << "is this node shared?" << isShared; //if (isShared) parentNode = upperVol; //else // parentNode = dynamic_cast<const GeoVPhysVol*>( &(*(vol->getParent() ))); // this cannot be trust for shared nodes - //JFB Commented out: qDebug() << "parentNode address" << parentNode; + //qDebug() << "parentNode address" << parentNode; if (parentNode) { std::string parentAddress = getAddressStringFromPointer(parentNode); @@ -221,24 +212,24 @@ void WriteGeoModel::handleVPhysVolObjects(const GeoVPhysVol* vol) // else // qFatal("FATAL ERROR!!! - The parent node of this child should has been stored in the DB already, but it was not found!!"); - //JFB Commented out: qDebug() << "==> parent's LogVol name:" << QString::fromStdString(parentNode->getLogVol()->getName()); + //qDebug() << "==> parent's LogVol name:" << QString::fromStdString(parentNode->getLogVol()->getName()); } else { - //JFB Commented out: qDebug() << "NULL parent node!! --> this node appeared unconnected."; + //qDebug() << "NULL parent node!! --> this node appeared unconnected."; } } // counting children // unsigned int nChildren = vol->getNChildVols(); - //JFB Commented out: qDebug() << "number of child physical volumes:" << nChildren; - //JFB Commented out: qDebug() << "[number of PhysVol and SerialTransformer child nodes:" << vol->getNChildVolAndST() << "]"; + //qDebug() << "number of child physical volumes:" << nChildren; + //qDebug() << "[number of PhysVol and SerialTransformer child nodes:" << vol->getNChildVolAndST() << "]"; //// for debug // GeoCountVolAction cv; // cv.setDepthLimit(1); // vol->exec(&cv); // int nChildCount = cv.getCount(); - // //JFB Commented out: qDebug() << "number of child volumes:" << nChildCount; + // qDebug() << "number of child volumes:" << nChildCount; @@ -285,12 +276,10 @@ void WriteGeoModel::handleVPhysVolObjects(const GeoVPhysVol* vol) logvolId = storeObj(logVol, logName, shapeId, matId); if (dynamic_cast<const GeoPhysVol*>(vol)) { - //JFB Commented out: qDebug() << "New PhysVol, storing it..."; const GeoPhysVol* physVol = dynamic_cast<const GeoPhysVol*>(vol); // store the PhysVol volume into the DB physId = storeObj(physVol, logvolId, parentId, storeRootVolume); // with parent info volTypeStr = "GeoPhysVol"; - //JFB Commented out: qDebug() << "PhysVol stored. Id:" << physId.toString(); } else if (dynamic_cast<const GeoFullPhysVol*>(vol)) { const GeoFullPhysVol* fullVol = dynamic_cast<const GeoFullPhysVol*>(vol); @@ -313,16 +302,13 @@ void WriteGeoModel::handleVPhysVolObjects(const GeoVPhysVol* vol) //JFB Commented out: qDebug() << "physId: " << physId << "- volume copy number: " << volCopyN; if ( isRootVolume || parentId == 0) { - //JFB Commented out: qDebug() << "This is the RootVolume or the volume has 'NULL' parent (unconnected subtree?) - So, we do not store the child position for this volume!"; + //qDebug() << "This is the RootVolume or the volume has 'NULL' parent (unconnected subtree?) - So, we do not store the child position for this volume!"; } else { // store the parent-child relationship in the DB std::string parentType = getGeoTypeFromVPhysVol(parentNode); - // get the copy number of the parent - const unsigned int parentCopyN = getLatestParentCopyNumber(parentId, parentType); - //JFB Commented out: qDebug() << "PhysVol Id:" << physId << " - copyNumber:" << QString::number(parentCopyN); - + // get the copy number of the parent + const unsigned int parentCopyN = getLatestParentCopyNumber(parentId, parentType); std::string childType = getGeoTypeFromVPhysVol(vol); - storeChildPosition(parentId, parentType, physId, parentCopyN, getChildPosition( parentId, parentType, parentCopyN), childType, volCopyN); } } @@ -376,8 +362,6 @@ void WriteGeoModel::handleSerialDenominator (const GeoSerialDenominator *node) void WriteGeoModel::handleSerialTransformer (const GeoSerialTransformer *node) { - //JFB Commented out: qDebug() << "\nWriteGeoModel::handleSerialTransformer(GeoSerialTransformer*)"; - std::string address = getAddressStringFromPointer( node ); // variables used to persistify the object @@ -457,10 +441,8 @@ void WriteGeoModel::handleSerialTransformer (const GeoSerialTransformer *node) * ==> So, we need to persitify it as a new tree, to get all its children visited and persistified * */ - //JFB Commented out: qDebug() << "Handling the referenced VPhysVol, going into the sub-tree, if any..."; handleReferencedVPhysVol(vol); - //JFB Commented out: qDebug() << "Storing the referenced VPhysVol..."; std::string physvolAddress = getAddressStringFromPointer(vol); physvolId = getStoredIdFromAddress(physvolAddress); @@ -470,14 +452,11 @@ void WriteGeoModel::handleSerialTransformer (const GeoSerialTransformer *node) */ // store the SerialTransformer volume in the DB stId = storeObj(node, functionId, physvolId, volType, nCopies); - //JFB Commented out: qDebug() << "SerialTransformer stored. Id:" << stId.toString(); - - } else { + } + else { stId = getStoredIdFromAddress(address); - //JFB Commented out: qDebug() << "SerialTransformer already stored in the DB. Id:" << stId.toString(); } - //JFB Commented out: qDebug() << "Storing:" << parentId << parentType << stId; storeChildPosition(parentId, parentType, stId, parentCopyN, getChildPosition( parentId, parentType, parentCopyN ), "GeoSerialTransformer", 0); // TODO: Check if the copyN=0 at the end is OK for nodes as Transforms, which cannot be used as parents, only as children! } @@ -486,8 +465,6 @@ void WriteGeoModel::handleSerialTransformer (const GeoSerialTransformer *node) void WriteGeoModel::handleTransform(const GeoTransform* node) { - //JFB Commented out: qDebug() << "\nWriteGeoModel::handleTransform(GeoTransform*)"; - std::string address = getAddressStringFromPointer( node ); // get the parent volume @@ -507,11 +484,9 @@ void WriteGeoModel::handleTransform(const GeoTransform* node) // Store the child-parent relationship if ( dynamic_cast<const GeoAlignableTransform*>(node) ) { - //JFB Commented out: qDebug() << "Storing a GeoAlignableTransform..."; storeChildPosition(parentId, parentType, trId, parentCopyN, getChildPosition( parentId, parentType, parentCopyN ), "GeoAlignableTransform", 0); // TODO: Check if the copyN=0 at the end is OK for nodes as Transforms, which cannot be used as parents, only as children! } else if ( dynamic_cast<const GeoTransform*>(node) ) { - //JFB Commented out: qDebug() << "Storing a GeoTransform..."; storeChildPosition(parentId, parentType, trId, parentCopyN, getChildPosition( parentId, parentType, parentCopyN ), "GeoTransform", 0); // TODO: Check if the copyN=0 at the end is OK for nodes as Transforms, which cannot be used as parents, only as children! } else { @@ -587,13 +562,11 @@ void WriteGeoModel::handleNameTag(const GeoNameTag* node) parentId = getStoredIdFromAddress(parentAddress); } else { - // qFatal("FATAL ERROR!!! - The parent node of this child should has been stored in the DB already, but it was not found!!"); std::cout << "The parent node of this child node seems to not having be stored in the DB yet! [It is normal if it is the root volume or a transformation node used for example only in the definition of a 'GeoShapeShift' instance]" << std::endl; } } } else{ - // qWarning() << "ERROR!! Len == 1, but this cannot be the Root volume!"; std::cout << "WARNING!! Len == 0, but this cannot be the Root volume!" << std::endl; } @@ -610,14 +583,10 @@ unsigned int WriteGeoModel::storeShape(const GeoShape* shape) // QString shapeType = QString::fromStdString(shape->type()); std::string shapeType = shape->type(); - - //JFB Commented out: qDebug() << "storeShape() - shape name:" << shapeType << ", address:" << shape; - - // LArCustomShape is deprecated. Write it out as a GeoUnidentifiedShape; - if (shapeType=="CustomShape") shapeType="UnidentifiedShape"; + // LArCustomShape is deprecated. Write it out as a GeoUnidentifiedShape; + if (shapeType=="CustomShape") shapeType="UnidentifiedShape"; - // get shape parameters -// QString shapePars = getShapeParameters(shape); + // get shape parameters std::string shapePars = getShapeParameters(shape); // store the shape in the DB and returns the ID @@ -632,14 +601,6 @@ unsigned int WriteGeoModel::storeMaterial(const GeoMaterial* mat) const double matDensity = mat->getDensity(); //The density of the material. const unsigned int numElements = mat->getNumElements(); -// const QString matID = QString::number(mat->getID()); //Gives an integral identifier for the material.For convenience. -// const QString matNumElements = QString::number(numElements); - //JFB Commented out: qDebug() << "storeMaterial() - material name:" << matName - // << ", address:" << mat - // << ", matID:" << matID - // << ", matDensity:" << matDensity - // << ", matNumElements:" << matNumElements; - // loop over the elements composing the material std::string matElements; std::vector<std::string> matElementsList; @@ -659,8 +620,6 @@ unsigned int WriteGeoModel::storeMaterial(const GeoMaterial* mat) } matElements = joinVectorStrings(matElementsList, ";"); - //JFB Commented out: qDebug() << "\t==> material's elements:" << matElements; - // store the material in the DB and returns the ID return storeObj(mat, matName, matDensity, matElements); } @@ -700,9 +659,6 @@ unsigned int WriteGeoModel::storeTranform(const GeoTransform* node) // check if this object has been stored already if (! isAddressStored(address)) { - //JFB Commented out: qDebug() << "New Transform, storing it..."; - - // TODO: simplify and put common code in a separate class /* @@ -755,7 +711,6 @@ unsigned int WriteGeoModel::storeTranform(const GeoTransform* node) tr(3,3)=1; std::vector<double> params = getTransformParameters(tr); - //JFB Commented out: qDebug() << "Transform parameters:" << QVector<double>::fromStdVector(params); /* * STORE THE OBJECT IN THE DB @@ -764,11 +719,9 @@ unsigned int WriteGeoModel::storeTranform(const GeoTransform* node) // store the object in the DB if ( dynamic_cast<const GeoAlignableTransform*>(node) ) { trId = storeObj(dynamic_cast<const GeoAlignableTransform*>(node), params); - //JFB Commented out: qDebug() << "AlignableTransform stored. Id:" << trId.toString(); } else if ( dynamic_cast<const GeoTransform*>(node) ) { trId = storeObj(dynamic_cast<const GeoTransform*>(node), params); - //JFB Commented out: qDebug() << "Transform stored. Id:" << trId.toString(); } else { std::cout << "ERROR!!! - Unknown type of transformation! Exiting..." << std::endl; @@ -788,8 +741,6 @@ unsigned int WriteGeoModel::storeTranform(const GeoTransform* node) void WriteGeoModel::handleReferencedVPhysVol (const GeoVPhysVol *vol) { - //JFB Commented out: qDebug() << "\nWriteGeoModel::handleReferencedVPhysVol(GeoVPhysVol*)"; - // qDebug() << "PhysVol's LogVol name:" << QString::fromStdString(vol->getLogVol()->getName()); // get the address string for the current volume @@ -797,7 +748,6 @@ void WriteGeoModel::handleReferencedVPhysVol (const GeoVPhysVol *vol) unsigned int parentId = 0; - //JFB Commented out: qDebug() << "setting 'unconnected' to: false"; m_unconnectedTree = false; // get the parent volume, if this is not the Root volume @@ -815,11 +765,11 @@ void WriteGeoModel::handleReferencedVPhysVol (const GeoVPhysVol *vol) // else // qFatal("FATAL ERROR!!! - The parent node of this child should has been stored in the DB already, but it was not found!!"); - //JFB Commented out: qDebug() << "--> parent's LogVol name:" << QString::fromStdString(parentNode->getLogVol()->getName()); + //qDebug() << "--> parent's LogVol name:" << QString::fromStdString(parentNode->getLogVol()->getName()); } else { - //JFB Commented out: qDebug() << "NULL parent node!! --> it seems to be an unconnected subtree."; - //JFB Commented out: qDebug() << "setting 'unconnected' to: true"; + //qDebug() << "NULL parent node!! --> it seems to be an unconnected subtree."; + //qDebug() << "setting 'unconnected' to: true"; m_unconnectedTree = true; } @@ -840,16 +790,16 @@ void WriteGeoModel::handleReferencedVPhysVol (const GeoVPhysVol *vol) // check if this object has been stored already if (! isAddressStored(address)) { - //JFB Commented out: qDebug() << "This is a new root PhysVol node of an 'unconnected' tree, so we start another action on it to dump it into the DB..."; + //qDebug() << "This is a new root PhysVol node of an 'unconnected' tree, so we start another action on it to dump it into the DB..."; // Dump the tree volumes into the DB vol->exec(this); // TODO: check if the new action overwrites the id of the volumes already in the DB...!!! } else { - //JFB Commented out: qDebug() << "The referenced volume has been stored already. Skipping..."; + //qDebug() << "The referenced volume has been stored already. Skipping..."; } // at the end, we make sure we reset the status - //JFB Commented out: qDebug() << "setting 'unconnected' to: false"; + //qDebug() << "setting 'unconnected' to: false"; m_unconnectedTree = false; } @@ -858,13 +808,11 @@ void WriteGeoModel::handleReferencedVPhysVol (const GeoVPhysVol *vol) std::string WriteGeoModel::getShapeParameters(const GeoShape* shape) { const std::string shapeType = shape->type(); - //JFB Commented out: qDebug() << "shapeType:" << shapeType; std::string shapePars = ""; std::vector<std::string> pars; if (shapeType == "Box") { - //JFB Commented out: qDebug() << "get GeoBox parameters"; const GeoBox* box = dynamic_cast<const GeoBox*>(shape); pars.push_back("XHalfLength=" + std::to_string(box->getXHalfLength())) ; pars.push_back("YHalfLength=" + std::to_string(box->getYHalfLength())) ; @@ -1114,29 +1062,29 @@ std::vector<double> WriteGeoModel::getTransformParameters(GeoTrf::Transform3D tr WriteGeoModel::WriteGeoModel(GMDBManager &db) { - //JFB Commented out: qDebug() << "WriteGeoModel: constructor"; - // init variables m_len = 0; m_len_nChild = 0; m_rootVolumeFound = false; m_unconnectedTree = false; - // init anche check the database handle + // get a handle to the database and check it m_dbManager = &db; if (m_dbManager->checkIsDBOpen()) { - //JFB Commented out: qDebug() << "OK! Database is open!"; +// if(m_debug) std::cout << "OK! Database is open!" << std::endl; } else { - //JFB Commented out: qDebug() << "Database is not open!"; + //qDebug() << "Database is not open!"; } - - m_dbpath = m_dbManager->getDBFilePath(); + // initialize the database (create tables, ...) + m_dbManager->initDB(); + // build caches + m_dbManager->loadGeoNodeTypesAndBuildCache(); + // get the database file path + m_dbpath = m_dbManager->getDBFilePath(); // get DB metadata m_memMap_Tables = m_dbManager->getAll_NodeTypesTableIDs(); - // qDebug() << "m_memMap_Tables:" << m_memMap_Tables; - } WriteGeoModel::~WriteGeoModel() @@ -1147,8 +1095,6 @@ WriteGeoModel::~WriteGeoModel() void WriteGeoModel::showMemoryMap() { - //JFB Commented out: qDebug() << "WriteGeoModel::showMemoryMap()"; - std::unordered_map<std::string, unsigned int>::const_iterator it = m_memMap.begin(); while (it != m_memMap.end()) { std::cout << it->first << ": " << it->second << std::endl; @@ -1160,23 +1106,16 @@ void WriteGeoModel::showMemoryMap() unsigned int WriteGeoModel::storeObj(const GeoMaterial* pointer, const std::string &name, const double &density, const std::string &elements) { - //JFB Commented out: qDebug() << "WriteGeoModel::storeObj(GeoMaterial*) - name:" << name << "- address:" << pointer << "- density:" << density << "- elements:" << elements; - std::string address = getAddressStringFromPointer( pointer ); unsigned int materialId; if (! isAddressStored(address)) { - //JFB Commented out: qDebug() << "New Material! Storing it..."; - materialId = addMaterial(name, density, elements); - storeAddress( address, materialId ); } else { - //JFB Commented out: qDebug() << "Material node stored already. Getting ID from the memory map..."; materialId = getStoredIdFromAddress(address); } - //JFB Commented out: qDebug() << "materialId:" << materialId; return materialId; } @@ -1187,17 +1126,12 @@ void WriteGeoModel::showMemoryMap() unsigned int elementId; if (! isAddressStored(address)) { - //JFB Commented out: qDebug() << "New Element! Storing it..."; - elementId = addElement(name, symbol, elZ, elA); - storeAddress( address, elementId ); } else { - //JFB Commented out: qDebug() << "Element node stored already. Getting ID from the memory map..."; elementId = getStoredIdFromAddress(address); } - //JFB Commented out: qDebug() << "elementId:" << elementId; return elementId; } @@ -1208,25 +1142,17 @@ void WriteGeoModel::showMemoryMap() unsigned int shapeId; if (! isAddressStored(address)) { - //JFB Commented out: qDebug() << "New Shape! Storing it..."; - - // shapeId = m_dbManager->addShape(name, parameters); shapeId = addShape(name, parameters); - storeAddress( address, shapeId); } else { - //JFB Commented out: qDebug() << "Shape node stored already. Getting ID from the memory map..."; shapeId = getStoredIdFromAddress(address); } - //JFB Commented out: qDebug() << "shapeId:" << shapeId; return shapeId; } unsigned int WriteGeoModel::storeObj(const GeoLogVol* pointer, const std::string &name, const unsigned int &shapeId, const unsigned int &materialId) { - //JFB Commented out: qDebug() << "WriteGeoModel::storeObj(GeoLogVol*) - name:" << name << "address:" << pointer; - std::string address = getAddressStringFromPointer( pointer ); unsigned int logvolId; @@ -1243,8 +1169,6 @@ unsigned int WriteGeoModel::storeObj(const GeoLogVol* pointer, const std::string unsigned int WriteGeoModel::storeObj(const GeoPhysVol* pointer, const unsigned int &logvolId, const unsigned int parentId, const bool isRootVolume) { - //JFB Commented out: qDebug() << "WriteGeoModel::storeObj(GeoPhysVol*) - address:" << pointer << "- is root volume?" << isRootVolume; - std::string address = getAddressStringFromPointer( pointer ); unsigned int physvolId; @@ -1281,9 +1205,7 @@ unsigned int WriteGeoModel::storeObj(const GeoFullPhysVol* pointer, const unsign unsigned int id; if (! isAddressStored(address)) { - // id = m_dbManager->addSerialDenominator(baseName); id = addSerialDenominator(baseName); - storeAddress( address, id ); } else { @@ -1400,7 +1322,6 @@ unsigned int WriteGeoModel::storeObj(const GeoAlignableTransform* pointer, const } -//unsigned int WriteGeoModel::addRecord(std::vector<QStringList>* container, const QStringList values) const unsigned int WriteGeoModel::addRecord(std::vector<std::vector<std::string>>* container, const std::vector<std::string> values) const { container->push_back(values); @@ -1491,7 +1412,6 @@ unsigned int WriteGeoModel::addTransform(const std::vector<double> ¶ms) const unsigned int volTypeID = getIdFromNodeType(volType); std::vector<std::string> values; -// values << funcId.toString() << physvolId.toString() << volTypeID << QString::number(copies); values.insert( values.begin(), {std::to_string(funcId), std::to_string(physvolId), std::to_string(volTypeID), std::to_string(copies)} ); return addRecord(container, values); @@ -1511,7 +1431,7 @@ unsigned int WriteGeoModel::addPhysVol(const unsigned int &logVolId, const unsig { std::vector<std::vector<std::string>>* container = &m_physVols; std::vector<std::string> values; - values.push_back( std::to_string(logVolId) ); //<< parentPhysVolId; // TODO: we should remove the parent info: it's not complete because the type is missing (PhysVol or FullPhysVol) and it's redundant, because we store the childrenPositions. It's only useful for quick visual debug, by dumping the PhysVol DB table + values.push_back( std::to_string(logVolId) ); unsigned int idx = addRecord(container, values); if (isRootVolume) { std::vector<std::string> rootValues; @@ -1526,7 +1446,7 @@ unsigned int WriteGeoModel::addFullPhysVol(const unsigned int &logVolId, const u { std::vector<std::vector<std::string>>* container = &m_fullPhysVols; std::vector<std::string> values; - values.push_back( std::to_string(logVolId) ); // << parentPhysVolId.toString(); // TODO: we should remove the parent info: it's not complete because the type is missing (PhysVol or FullPhysVol) and it's redundant, because we store the childrenPositions. It's only useful for quick visual debug, by dumping the PhysVol DB table + values.push_back( std::to_string(logVolId) ); unsigned int idx = addRecord(container, values); if (isRootVolume) { std::vector<std::string> rootValues; @@ -1593,7 +1513,6 @@ void WriteGeoModel::saveToDB() void WriteGeoModel::storeAddress(const std::string &address, const unsigned int &id) { m_memMap.insert( std::pair<std::string,unsigned int>(address, id) ); -// m_memMap.insert(address, id); } bool WriteGeoModel::isAddressStored(const std::string &address) @@ -1612,7 +1531,6 @@ void WriteGeoModel::saveToDB() // get pointer string std::string WriteGeoModel::getAddressStringFromPointer(const GeoMaterial* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoMaterial*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); @@ -1620,7 +1538,6 @@ void WriteGeoModel::saveToDB() // get pointer string std::string WriteGeoModel::getAddressStringFromPointer(const GeoElement* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoElement*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); @@ -1628,7 +1545,6 @@ std::string WriteGeoModel::getAddressStringFromPointer(const GeoElement* pointer // get pointer string std::string WriteGeoModel::getAddressStringFromPointer(const GeoShape* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoShape*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); @@ -1636,7 +1552,6 @@ std::string WriteGeoModel::getAddressStringFromPointer(const GeoShape* pointer) // get pointer string std::string WriteGeoModel::getAddressStringFromPointer(const GeoLogVol* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoLogVol*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); @@ -1644,14 +1559,12 @@ std::string WriteGeoModel::getAddressStringFromPointer(const GeoLogVol* pointer) // get pointer string std::string WriteGeoModel::getAddressStringFromPointer(const GeoPhysVol* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoPhysVol*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); } std::string WriteGeoModel::getAddressStringFromPointer(const GeoVPhysVol* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoVPhysVol*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); @@ -1659,21 +1572,18 @@ std::string WriteGeoModel::getAddressStringFromPointer(const GeoVPhysVol* pointe // get pointer string std::string WriteGeoModel::getAddressStringFromPointer(const GeoSerialDenominator* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoSerialDenominator*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); } std::string WriteGeoModel::getAddressStringFromPointer(const GeoSerialTransformer* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoSerialTransformer*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); } std::string WriteGeoModel::getAddressStringFromPointer(const GeoXF::Function* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoXF::Function*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss); @@ -1681,7 +1591,6 @@ std::string WriteGeoModel::getAddressStringFromPointer(const GeoXF::Function* po std::string WriteGeoModel::getAddressStringFromPointer(const GeoTransform* pointer) { - //JFB Commented out: qDebug() << "WriteGeoModel::getAddressStringFromPointer(GeoTransform*)"; std::ostringstream oss; oss << pointer; return getQStringFromOss(oss);