Skip to content
Snippets Groups Projects
Commit a741376c authored by Riccardo Maria Bianchi's avatar Riccardo Maria Bianchi :sunny: Committed by Johannes Junggeburth
Browse files

Adapt Read to the new DB schema, add builder classes for shapes

parent 6c744c63
Branches
Tags
1 merge request!327New schema for the GeoModel SQLite database and updated I/O
Showing
with 894 additions and 232 deletions
......@@ -11,7 +11,7 @@ file( GLOB HEADERS GeoModelCppHelpers/*.h )
# Set up the library.
add_library( GeoModelCppHelpers SHARED ${HEADERS} ${SOURCES} )
# target_link_libraries( GeoModelCppHelpers PUBLIC
# GeoModelRead GeoModelWrite )
# GeoModelHelpers )
# We link those to carry on the needed libs when including GeoModelCppHelpers,
# even if the latter is headers only
......
......@@ -13,11 +13,15 @@
#ifndef GMCPPHelper_H
#define GMCPPHelper_H
// C++ includes
#include <cstdlib> // EXIT_FAILURE
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <variant>
#include <type_traits>
namespace GeoModelIO {
......@@ -74,25 +78,97 @@ namespace GeoModelIO {
return val == NULL ? std::string("") : std::string(val);
}
static void printStdVectorVariants(const std::vector<std::variant<int, long, float, double, std::string>> vec)
{
for (const auto &item : vec)
{
if (std::holds_alternative<int>(item))
std::cout << std::get<int>(item); // INT
else if (std::holds_alternative<long>(item))
std::cout << std::get<long>(item);
else if (std::holds_alternative<float>(item))
std::cout << std::get<float>(item);
else if (std::holds_alternative<double>(item))
std::cout << std::get<double>(item);
else if (std::holds_alternative<std::string>(item))
std::cout << std::get<std::string>(item);
std::cout << ", ";
}
std::cout << std::endl;
}
// static void printStdVectorVariants(const std::vector<std::variant<int, long, float, double, std::string>> vec)
// {
// for (const auto &item : vec)
// {
// if (std::holds_alternative<int>(item))
// std::cout << std::get<int>(item); // INT
// else if (std::holds_alternative<long>(item))
// std::cout << std::get<long>(item);
// else if (std::holds_alternative<float>(item))
// std::cout << std::get<float>(item);
// else if (std::holds_alternative<double>(item))
// std::cout << std::get<double>(item);
// else if (std::holds_alternative<std::string>(item))
// std::cout << std::get<std::string>(item);
// std::cout << ", ";
// }
// std::cout << std::endl;
// }
// static std::string getFromVariant_String(const std::variant<int, long, float, double, std::string> &record, std::string_view logMsg = "")
// {
// std::string_view type{"string"};
// std::string ret;
// try
// {
// ret = std::get<std::string>(record);
// }
// catch (std::bad_variant_access const &ex)
// {
// std::cout << ex.what() << ": '" << logMsg << "' is not a 'string'!\n";
// }
// return ret;
// }
// static int getFromVariant_Int(const std::variant<int, long, float, double, std::string> &record, std::string_view logMsg = "")
// {
// std::string_view type{"int"};
// int ret;
// try
// {
// ret = std::get<int>(record);
// }
// catch (std::bad_variant_access const &ex)
// {
// std::cout << ex.what() << ": '" << logMsg << "' is not a '" << type << "'!\n";
// }
// return ret;
// }
// static int getFromVariant_Double(const std::variant<int, long, float, double, std::string> &record, std::string_view logMsg = "")
// {
// std::string_view type{"double"};
// double ret;
// try
// {
// ret = std::get<double>(record);
// }
// catch (std::bad_variant_access const &ex)
// {
// std::cout << ex.what() << ": '" << logMsg << "' is not a '" << type << "'!\n";
// }
// return ret;
// }
// static std::string getFromVariant_Type(const std::variant<int, long, float, double, std::string> &record)
// {
// std::string type;
// if (std::holds_alternative<int>(record))
// {
// type = "int";
// }
// else if (std::holds_alternative<long>(record))
// {
// type = "long";
// }
// else if (std::holds_alternative<float>(record))
// {
// type = "float";
// }
// else if (std::holds_alternative<double>(record))
// {
// type = "double";
// }
// else if (std::holds_alternative<std::string>(record))
// {
// type = "string";
// } else {
// type = "UNKOWN";
// }
// std::cout << "Variant is of type '" << type << "'" << std::endl;
// return type;
// }
};
} // namespace GeoModelIO
......
......@@ -12,7 +12,7 @@ file( GLOB HEADERS GeoModelRead/*.h GeoModelRead/*.tpp )
# Set up the library.
add_library( GeoModelRead SHARED ${HEADERS} ${SOURCES} )
target_link_libraries( GeoModelRead PUBLIC
GeoModelKernel GeoModelDBManager TFPersistification )
GeoModelKernel GeoModelDBManager TFPersistification GeoModelCppHelpers GeoModelHelpers )
target_include_directories( GeoModelRead PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> )
......
......@@ -53,8 +53,10 @@ typedef GeoModelIO::ReadGeoModel Persistifier;
// ****************************************************************
// ****************************************************************
// local includes
// GeoModel includes
#include "GeoModelDBManager/GMDBManager.h"
#include "GeoModelDBManager/definitions.h"
#include "GeoModelKernel/GeoXF.h"
// C++ includes
......@@ -86,6 +88,11 @@ class GeoGraphNode;
class GeoShapeSubtraction;
class GeoBox;
class BuildGeoShapes_Box;
class BuildGeoShapes_Tube;
class BuildGeoShapes_Pcon;
class BuildGeoShapes_Cons;
// type definitions
typedef const GeoXF::Function& TRANSFUNCTION;
// containers for boolean shapes' information
......@@ -94,6 +101,8 @@ typedef std::tuple<unsigned int /*shape ID*/, GeoShape*,
tuple_shapes_boolean_info;
typedef std::vector<tuple_shapes_boolean_info> type_shapes_boolean_info;
namespace GeoModelIO {
class ReadGeoModel {
......@@ -156,6 +165,11 @@ class ReadGeoModel {
};
private:
void buildAllShapes_Box();
void buildAllShapes_Tube();
void buildAllShapes_Pcon();
void buildAllShapes_Cons();
void buildAllShapes();
void buildAllElements();
void buildAllMaterials();
......@@ -224,7 +238,7 @@ class ReadGeoModel {
// one, with the GeoModel class as a second argument ? (RMB)
bool isBuiltShape(const unsigned int id);
void storeBuiltShape(const unsigned int, GeoShape* node);
GeoShape* getBuiltShape(const unsigned int id);
GeoShape* getBuiltShape(const unsigned int shapeId, std::string_view shapeType = "");
bool isBuiltTransform(const unsigned int id);
void storeBuiltTransform(GeoTransform* node);
......@@ -314,6 +328,14 @@ class ReadGeoModel {
// callback handles
unsigned long* m_progress;
//! builders
// std::unique_ptr<BuildGeoShapes_Box> m_builderShape_Box;
BuildGeoShapes_Box* m_builderShape_Box;
BuildGeoShapes_Tube* m_builderShape_Tube;
BuildGeoShapes_Pcon* m_builderShape_Pcon;
BuildGeoShapes_Cons* m_builderShape_Cons;
//! containers to store the list of GeoModel nodes coming from the DB
std::vector<std::vector<std::string>> m_physVols;
std::vector<std::vector<std::string>> m_fullPhysVols;
......@@ -324,12 +346,23 @@ class ReadGeoModel {
std::vector<std::vector<std::string>> m_identifierTags;
std::vector<std::vector<std::string>> m_serialTransformers;
std::vector<std::vector<std::string>> m_nameTags;
std::vector<std::vector<std::string>> m_logVols;
std::vector<std::vector<std::string>> m_materials;
std::vector<std::vector<std::string>> m_elements;
std::vector<std::vector<std::string>> m_shapes;
std::vector<std::vector<std::string>> m_allchildren;
// std::vector<std::vector<std::variant<int, long, float, double, std::string>>> m_logVols;
DBRowsList m_logVols;
// containers to store shapes' parameters
std::vector<std::vector<std::variant<int, long, float, double, std::string>>> m_shapes_Box;
std::vector<std::vector<std::variant<int, long, float, double, std::string>>> m_shapes_Tube;
std::vector<std::vector<std::variant<int, long, float, double, std::string>>> m_shapes_Pcon;
std::vector<std::vector<std::variant<int, long, float, double, std::string>>> m_shapes_Cons;
// containers to store shapes' data, for shapes with a variable number of build parameters
DBRowsList m_shapes_Pcon_data;
// std::vector<std::vector<std::string>> m_functions;
std::vector<std::vector<std::variant<int, long, float, double, std::string>>> m_functions;
std::deque<double> m_funcExprData;
......
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*/
#include "BuildGeoShapes.h"
#include "GeoModelKernel/GeoShape.h"
#include <vector>
#include <iostream>
BuildGeoShapes::BuildGeoShapes(std::string_view shapeType, const unsigned size)
{
m_shapeType = shapeType;
m_memMapShapes.reserve(size);
}
BuildGeoShapes::BuildGeoShapes(std::string_view shapeType, const unsigned size, const DBRowsList shapeData)
{
m_shapeType = shapeType;
m_memMapShapes.reserve(size);
m_shape_data = shapeData;
}
bool BuildGeoShapes::isBuiltShape(const unsigned id) {
return (!(m_memMapShapes.find(id) == m_memMapShapes.end()));
}
void BuildGeoShapes::storeBuiltShape(const unsigned id, GeoShape* nodePtr) {
m_memMapShapes[id] = nodePtr;
}
GeoShape* BuildGeoShapes::getBuiltShape(const unsigned id) {
return m_memMapShapes[id]; // this is a map, and 'id' is the key
}
void BuildGeoShapes::printBuiltShapes() {
for (unsigned id{1}; id<=m_memMapShapes.size(); ++id) {
std::cout << "shape " << m_shapeType << " -- id: " << id << ", shapePtr: " << m_memMapShapes[id] << std::endl;
}
}
\ No newline at end of file
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* BuildGeoShapes.h
*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*
*/
#ifndef GEOMODELREAD_BUILDGEOSHAPES_H
#define GEOMODELREAD_BUILDGEOSHAPES_H
#include "GeoModelDBManager/definitions.h"
#include <vector>
#include <variant>
#include <string>
#include <unordered_map>
class GeoShape;
class BuildGeoShapes
{
protected:
std::unordered_map<unsigned, GeoShape *> m_memMapShapes;
std::string m_shapeType;
DBRowsList m_shape_data;
public:
//! contructors
BuildGeoShapes(std::string_view shapeType, const unsigned size);
BuildGeoShapes(std::string_view shapeType, const unsigned size, DBRowsList shapesData);
virtual void buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row) = 0;
// --- methods for caching GeoShape nodes ---
void storeBuiltShape(const unsigned id, GeoShape *nodePtr);
bool isBuiltShape(const unsigned id);
GeoShape *getBuiltShape(const unsigned id);
// --- print the list of built/cached shapes
void printBuiltShapes();
};
#endif
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*/
#include "BuildGeoShapes_Box.h"
#include "GeoModelKernel/GeoBox.h"
#include "GeoModelHelpers/variantHelpers.h"
#include <vector>
#include <iostream>
void BuildGeoShapes_Box::buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row)
{
// === get shape numeric data from the DB row
// shape ID
const unsigned shapeId = GeoModelHelpers::variantHelper::getFromVariant_Int(row[0], "Tube:shapeID");
// shape volume
const double shapeVolume = GeoModelHelpers::variantHelper::getFromVariant_Double(row[1], "Tube:shapeVolume");
// shape parameters
const double XHalfLength = GeoModelHelpers::variantHelper::getFromVariant_Double(row[2], "Tube:XHalfLength");
const double YHalfLength = GeoModelHelpers::variantHelper::getFromVariant_Double(row[3], "Tube:YHalfLength");
const double ZHalfLength = GeoModelHelpers::variantHelper::getFromVariant_Double(row[4], "Tube:ZHalfLength");
GeoBox *shape = new GeoBox(XHalfLength, YHalfLength, ZHalfLength);
storeBuiltShape(shapeId, shape);
// return shape;
return;
}
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* BuildGeoShapes_Box.h
*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*
*/
#ifndef GEOMODELREAD_BUILDGEOSHAPES_BOX_H
#define GEOMODELREAD_BUILDGEOSHAPES_BOX_H
#include "BuildGeoShapes.h"
#include <vector>
#include <variant>
class BuildGeoShapes_Box : public BuildGeoShapes
{
public:
BuildGeoShapes_Box(const unsigned size):BuildGeoShapes("Box", size){};
void buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row) override;
};
#endif
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*/
#include "BuildGeoShapes_Cons.h"
#include "GeoModelKernel/GeoCons.h"
#include "GeoModelHelpers/variantHelpers.h"
#include <vector>
#include <iostream>
void BuildGeoShapes_Cons::buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row)
{
// === get shape numeric data from the DB row
// shape ID
const int shapeId = GeoModelHelpers::variantHelper::getFromVariant_Int(row[0], "Cons:shapeID");
// shape volume
const double shapeVolume = GeoModelHelpers::variantHelper::getFromVariant_Double(row[1], "Cons:shapeVolume");
// shape parameters
const double RMin1 = GeoModelHelpers::variantHelper::getFromVariant_Double(row[2], "Cons:RMin1");
const double RMin2 = GeoModelHelpers::variantHelper::getFromVariant_Double(row[3], "Cons:RMin2");
const double RMax1 = GeoModelHelpers::variantHelper::getFromVariant_Double(row[4], "Cons:RMax1");
const double RMax2 = GeoModelHelpers::variantHelper::getFromVariant_Double(row[4], "Cons:RMax2");
const double DZ = GeoModelHelpers::variantHelper::getFromVariant_Double(row[4], "Cons:DZ");
const double SPhi = GeoModelHelpers::variantHelper::getFromVariant_Double(row[4], "Cons:SPhi");
const double DPhi = GeoModelHelpers::variantHelper::getFromVariant_Double(row[4], "Cons:DPhi");
GeoCons *shape = new GeoCons(RMin1, RMin2, RMax1, RMax2, DZ, SPhi, DPhi);
storeBuiltShape(shapeId, shape);
return;
}
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* BuildGeoShapes_Cons.h
*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*
*/
#ifndef GEOMODELREAD_BuildGeoShapes_Cons_H
#define GEOMODELREAD_BuildGeoShapes_Cons_H
#include "BuildGeoShapes.h"
#include <vector>
#include <variant>
#include <string>
class BuildGeoShapes_Cons : public BuildGeoShapes
{
public:
BuildGeoShapes_Cons(const unsigned size):BuildGeoShapes("Cons", size){};
void buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row) override;
};
#endif
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*/
#include "BuildGeoShapes_Pcon.h"
#include "GeoModelKernel/GeoPcon.h"
#include "GeoModelHelpers/variantHelpers.h"
#include "GeoModelHelpers/throwExcept.h"
#include <vector>
#include <iostream>
void BuildGeoShapes_Pcon::buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row)
{
if (!(m_shape_data.size())) {
THROW_EXCEPTION("ERROR! GeoPcon shape has no ZPlanes data!! [m_shape_data.size() == 0]");
}
// === get shape numeric data from the DB row
// shape ID
const int shapeId = GeoModelHelpers::variantHelper::getFromVariant_Int(row[0], "Pcon:shapeID");
// shape volume
const double shapeVolume = GeoModelHelpers::variantHelper::getFromVariant_Double(row[1], "Pcon:shapeVolume");
// shape parameters
const double SPhi = GeoModelHelpers::variantHelper::getFromVariant_Double(row[2], "Pcon:SPhi");
const double DPhi = GeoModelHelpers::variantHelper::getFromVariant_Double(row[3], "Pcon:DPhi");
const int NZPlanes = GeoModelHelpers::variantHelper::getFromVariant_Int(row[4], "Pcon:NZPlanes");
// pointers to variable shape data stored in a separate table
const int dataStart = GeoModelHelpers::variantHelper::getFromVariant_Int(row[5], "Pcon:dataStart");
const int dataEnd = GeoModelHelpers::variantHelper::getFromVariant_Int(row[6], "Pcon:dataEnd");
// build the basic GeoPcon shape
GeoPcon *pcon = new GeoPcon(SPhi, DPhi);
// and now loop over the additional shape's data,
// to get the parameters of all Z planes
// get ZPlanes' data, extract subvector
// NOTE: we use (dataStart-1) to cope with the difference between the DB rows starting from '1',
// which is what the 'dataStart' stores, and the vector items, which start '0';
// also, the constructor of the sub-vector takes the element from 'begin+dataStart-1' included
// and 'begin+dataEnd' excluded.
const DBRowsList zplanesData(m_shape_data.begin() + (dataStart-1),
m_shape_data.begin() + (dataEnd) );
if (!(zplanesData.size())) {
THROW_EXCEPTION("ERROR! GeoPcon shape ZPlanes data have not been retrieved!!");
}
if (!( NZPlanes == zplanesData.size())) {
THROW_EXCEPTION("ERROR! GeoPcon shape : size of ZPlanes data does not correspond to the number of ZPlanes declared!!");
}
// loop over the data defining the ZPlanes
for (const DBRowEntry &dataRow : zplanesData)
{
const double zpos = GeoModelHelpers::variantHelper::getFromVariant_Double(dataRow[1], "Pcon:data_ZPos");
const double rmin = GeoModelHelpers::variantHelper::getFromVariant_Double(dataRow[2], "Pcon:data_RMin");
const double rmax = GeoModelHelpers::variantHelper::getFromVariant_Double(dataRow[3], "Pcon:data_RMax");
// add a Z plane to the GeoPcon
pcon->addPlane(zpos, rmin, rmax);
}
// sanity checks on the resulting Pcon shape
if (pcon->getNPlanes() != NZPlanes)
{
THROW_EXCEPTION("ERROR! GeoPcon actual number of planes: " + std::to_string(pcon->getNPlanes()) + " is not equal to the original size! --> " + std::to_string(NZPlanes));
}
if (!pcon->isValid())
{
THROW_EXCEPTION("ERROR! GeoPcon shape is not valid!!");
}
storeBuiltShape(shapeId, pcon);
return;
}
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* BuildGeoShapes_Pcon.h
*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*
*/
#ifndef GEOMODELREAD_BuildGeoShapes_Pcon_H
#define GEOMODELREAD_BuildGeoShapes_Pcon_H
#include "BuildGeoShapes.h"
#include "GeoModelDBManager/definitions.h"
#include <vector>
#include <variant>
class BuildGeoShapes_Pcon : public BuildGeoShapes
{
public:
BuildGeoShapes_Pcon(const unsigned size, DBRowsList shapeData):BuildGeoShapes("Pcon", size, shapeData){};
void buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row) override;
};
#endif
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*/
#include "BuildGeoShapes_Tube.h"
#include "GeoModelKernel/GeoTube.h"
#include "GeoModelHelpers/variantHelpers.h"
#include <vector>
#include <iostream>
void BuildGeoShapes_Tube::buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row)
{
// === get shape numeric data from the DB row
// shape ID
const int shapeId = GeoModelHelpers::variantHelper::getFromVariant_Int(row[0], "Tube:shapeID");
// shape volume
const double shapeVolume = GeoModelHelpers::variantHelper::getFromVariant_Double(row[1], "Tube:shapeVolume");
// shape parameters
const double RMin = GeoModelHelpers::variantHelper::getFromVariant_Double(row[2], "Tube:RMin");
const double RMax = GeoModelHelpers::variantHelper::getFromVariant_Double(row[3], "Tube:RMax");
const double ZHalfLength = GeoModelHelpers::variantHelper::getFromVariant_Double(row[4], "Tube:ZHalfLength");
GeoTube *shape = new GeoTube(RMin, RMax, ZHalfLength);
storeBuiltShape(shapeId, shape);
return;
}
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
/*
* BuildGeoShapes_Tube.h
*
* Created on: May 7, 2024
* Author: Riccardo Maria BIANCHI <riccardo.maria.bianchi@cern.ch>
*
*/
#ifndef GEOMODELREAD_BuildGeoShapes_Tube_H
#define GEOMODELREAD_BuildGeoShapes_Tube_H
#include "BuildGeoShapes.h"
#include <vector>
#include <variant>
class BuildGeoShapes_Tube : public BuildGeoShapes
{
public:
BuildGeoShapes_Tube(const unsigned size):BuildGeoShapes("Tube", size){};
void buildShape(const std::vector<std::variant<int, long, float, double, std::string>> row) override;
};
#endif
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment