15#include <fairlogger/Logger.h>
16#include <TCollection.h>
18#include <TGeoMatrix.h>
19#include <TGeoNavigator.h>
21#include <TGeoPhysicalNode.h>
34#include "TGeo2VecGeom/RootGeoManager.h"
35#include <VecGeom/base/Version.h>
36#include <VecGeom/management/GeoManager.h>
37#include <VecGeom/management/ABBoxManager.h>
38#include <VecGeom/management/BVHManager.h>
39#include <VecGeom/navigation/GlobalLocator.h>
40#include <VecGeom/navigation/NavigationState.h>
41#include <VecGeom/navigation/NewSimpleNavigator.h>
42#include <VecGeom/navigation/BVHNavigator.h>
43#include <VecGeom/navigation/SimpleLevelLocator.h>
44#if VECGEOM_VERSION >= 0x020000
45#include <VecGeom/navigation/SimpleABBoxLevelLocator.h>
47#include <VecGeom/navigation/BVHLevelLocator.h>
49#include <VecGeom/navigation/VNavigator.h>
50#include <VecGeom/volumes/LogicalVolume.h>
53#if __has_include(<VecGeom/navigation/BVHNavigatorV.h>)
54#define O2_VECGEOM_HAS_BVH_VNAVIGATOR
55#include <VecGeom/navigation/BVHNavigatorV.h>
56#include <VecGeom/navigation/BVHLevelLocator.h>
57#include <VecGeom/navigation/BVHSafetyEstimator.h>
58#include <VecGeom/navigation/SimpleSafetyEstimator.h>
68std::mutex GeometryManager::sTGMutex;
74 if (!gGeoManager || !gGeoManager->IsClosed()) {
75 LOG(
error) <<
"No active geometry or geometry not yet closed!";
79 std::lock_guard<std::mutex> guard(sTGMutex);
80 if (!gGeoManager->GetListOfPhysicalNodes()) {
81 LOG(warning) <<
"gGeoManager doesn't contain any aligned nodes!";
83 if (!gGeoManager->cd(symname)) {
84 LOG(
error) <<
"Volume path " << symname <<
" not valid!";
87 m = *gGeoManager->GetCurrentMatrix();
92 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
93 const char*
path =
nullptr;
96 m = *pne->GetGlobalOrig();
99 LOG(warning) <<
"The symbolic volume name " << symname
100 <<
"does not correspond to a physical entry. Using it as a volume path!";
104 return getOriginalMatrixFromPath(
path,
m);
108Bool_t GeometryManager::getOriginalMatrixFromPath(
const char*
path, TGeoHMatrix&
m)
112 if (!gGeoManager || !gGeoManager->IsClosed()) {
113 LOG(
error) <<
"Can't get the original global matrix! gGeoManager doesn't exist or it is still opened!";
116 std::lock_guard<std::mutex> guard(sTGMutex);
117 if (!gGeoManager->CheckPath(
path)) {
122 TIter next(gGeoManager->GetListOfPhysicalNodes());
123 gGeoManager->cd(
path);
125 while (gGeoManager->GetLevel()) {
126 TGeoPhysicalNode* physNode =
nullptr;
128 TGeoNode*
node = gGeoManager->GetCurrentNode();
130 while ((physNode = (TGeoPhysicalNode*)next())) {
131 if (physNode->GetNode() ==
node) {
136 TGeoMatrix* lm =
nullptr;
138 lm = physNode->GetOriginalMatrix();
140 lm =
node->GetMatrix();
143 lm =
node->GetMatrix();
159 if (!gGeoManager || !gGeoManager->IsClosed()) {
160 LOG(
error) <<
"Can't get the global matrix! gGeoManager doesn't exist or it is still opened!";
165 TGeoPhysicalNode* pnode = pne->GetPhysicalNode();
167 return pnode->GetMatrix();
171 pne->SetPhysicalNode(
new TGeoPhysicalNode(pne->GetTitle()));
172 return pne->GetPhysicalNode()->GetMatrix();
181 if (!gGeoManager || !gGeoManager->IsClosed()) {
182 LOG(
error) <<
"No active geometry or geometry not yet closed!";
186 TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
201 TGeoPNEntry* pne = gGeoManager->GetAlignableEntryByUID(
id);
203 LOG(
error) <<
"Failed to find alignable entry with index " <<
id <<
": Det" << detid <<
" Sens.Vol:" << sensid <<
") !";
206 return pne->GetName();
215 TGeoPNEntry* pne = gGeoManager->GetAlignableEntryByUID(
id);
217 LOG(
error) <<
"The sens.vol " << sensid <<
" of det " << detid <<
" does not correspond to a physical entry!";
228 static TGeoHMatrix matTmp;
234 TGeoPhysicalNode* pnode = pne->GetPhysicalNode();
236 return pnode->GetMatrix();
239 const char*
path = pne->GetTitle();
240 gGeoManager->PushPath();
241 if (!gGeoManager->cd(
path)) {
242 gGeoManager->PopPath();
246 matTmp = *gGeoManager->GetCurrentMatrix();
247 gGeoManager->PopPath();
271 for (
auto dv : algPars) {
283 int nvols = algPars.size();
284 std::vector<int> ord(
nvols);
285 std::iota(std::begin(ord), std::end(ord), 0);
286 std::sort(std::begin(ord), std::end(ord), [&algPars](
int a,
int b) {
return algPars[
a].getLevel() < algPars[
b].getLevel(); });
289 auto pns = gGeoManager->GetListOfPhysicalNodes();
291 int npn = pns->GetEntriesFast();
294 LOG(
error) <<
"Error applying alignment object for volume" << algPars[ord[
i]].getSymName();
296 if (!keepPhysicalNodes) {
297 for (
int k = pns->GetEntriesFast() - 1; k >= npn; k--) {
298 auto pn =
static_cast<TGeoPhysicalNode*
>(pns->RemoveAt(k));
299 if (
auto pne = gGeoManager->GetAlignableEntry(algPars[ord[
i]].getSymName().c_str()); pne && pne->GetPhysicalNode() == pn) {
300 pne->SetPhysicalNode(
nullptr);
315 double nrm = 1. / step;
328 bd.
meanRho = material->GetDensity();
329 bd.
meanX2X0 = material->GetRadLen();
330 bd.
meanA = material->GetA();
331 bd.
meanZ = material->GetZ();
332 if (material->IsMixture()) {
333 TGeoMixture* mixture = (TGeoMixture*)material;
336 for (Int_t iel = 0; iel < mixture->GetNelements(); iel++) {
337 norm += mixture->GetWmixt()[iel];
338 bd.
meanZ2A += mixture->GetZmixt()[iel] * mixture->GetWmixt()[iel] / mixture->GetAmixt()[iel];
366 throw std::runtime_error(
"meanMaterialBudgetExt requires geometry loaded");
369 double dir[3] = {
x1 -
x0,
y1 -
y0, z1 - z0};
370 if ((
length = dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]) < TGeoShape::Tolerance() * TGeoShape::Tolerance()) {
374 double invlen = 1. /
length;
375 for (
int i = 3;
i--;) {
378 std::lock_guard<std::mutex> guard(sTGMutex);
380 TGeoNode* currentnode = gGeoManager->InitTrack(startD, dir);
382 LOG(
error) <<
"start point out of geometry: " <<
x0 <<
':' <<
y0 <<
':' << z0;
387 accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
392 gGeoManager->FindNextBoundaryAndStep(
length, kFALSE);
393 Double_t stepTot = 0.0;
394 Double_t step = gGeoManager->GetStep();
396 if (!gGeoManager->IsOnBoundary()) {
402 while (
length > TGeoShape::Tolerance()) {
403 if (step < 2. * TGeoShape::Tolerance()) {
411 const double* curPos = gGeoManager->GetCurrentPoint();
412 LOG(warning) <<
"Cannot cross boundary at (" << curPos[0] <<
',' << curPos[1] <<
',' << curPos[2] <<
')';
429 currentnode = gGeoManager->GetCurrentNode();
434 accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
435 gGeoManager->FindNextBoundaryAndStep(
length, kFALSE);
436 step = gGeoManager->GetStep();
464 double dir[3] = {
x1 -
x0,
y1 -
y0, z1 - z0};
465 if ((
length = dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]) < TGeoShape::Tolerance() * TGeoShape::Tolerance()) {
469 double invlen = 1. /
length;
470 for (
int i = 3;
i--;) {
477 std::unique_lock<std::mutex> guard(sTGMutex, std::defer_lock);
480 nav = gGeoManager->GetCurrentNavigator();
483 TGeoNode* currentnode = nav->InitTrack(startD, dir);
485 LOG(
error) <<
"start point out of geometry: " <<
x0 <<
':' <<
y0 <<
':' << z0;
490 accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
495 nav->FindNextBoundaryAndStep(
length, kFALSE);
496 Double_t stepTot = 0.0;
497 Double_t step = nav->GetStep();
499 if (!nav->IsOnBoundary()) {
505 while (
length > TGeoShape::Tolerance()) {
506 if (step < 2. * TGeoShape::Tolerance()) {
514 const double* curPos = nav->GetCurrentPoint();
515 LOG(warning) <<
"Cannot cross boundary at (" << curPos[0] <<
',' << curPos[1] <<
',' << curPos[2] <<
')';
517 budTotal.
length = stepTot;
528 currentnode = nav->GetCurrentNode();
533 accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
534 nav->FindNextBoundaryAndStep(
length, kFALSE);
535 step = nav->GetStep();
538 budTotal.
length = stepTot;
547 LOG(fatal) <<
"geometry is not loaded";
549 if (applyMisalignment) {
551 aligner.applyAlignment();
558 auto loadGeom = [](
const std::string_view fname) {
559 LOG(info) <<
"Loading geometry from " << fname;
560 TFile flGeom(fname.data());
561 if (flGeom.IsZombie()) {
562 LOG(fatal) <<
"Failed to open file " << fname;
571 if (preferAlignedFile) {
581#ifdef O2_WITH_VECGEOM
588bool usesBvhAcceleration(vecgeom::LogicalVolume
const* vol)
590 return vol->GetDaughtersp()->size() > 2;
596void ensureVecGeomWorldBuilt()
598 GeometryManager::buildVecGeomGeometry(
true);
602void GeometryManager::buildVecGeomGeometry(
bool flattenAssemblies,
int booleanThreshold)
604 static std::once_flag onceFlag;
605 std::call_once(onceFlag, [flattenAssemblies, booleanThreshold]() {
607 LOG(fatal) <<
"Cannot build VecGeom geometry: no TGeo geometry loaded (call GeometryManager::loadGeometry() first)";
610 tgeo2vecgeom::RootGeoManager::Instance().SetMaterialConversionHook([](TGeoMaterial
const*
m) {
return (
void*)
m; });
611 LOG(info) <<
"VecGeom conversion: flattenAssemblies=" << flattenAssemblies;
612 tgeo2vecgeom::RootGeoManager::Instance().SetFlattenAssemblies(flattenAssemblies);
613#if __has_include(<VecGeom/management/BooleanFactory.h>)
615 LOG(info) <<
"VecGeom conversion: booleanThreshold=" << booleanThreshold;
616 vecgeom::GeoManager::Instance().SetBooleanOptimizationThreshold(booleanThreshold > 0 ? booleanThreshold : 0);
618 if (booleanThreshold > 0) {
619 LOG(warning) <<
"VecGeom without BooleanFactory: Boolean solids are kept as converted";
622 tgeo2vecgeom::RootGeoManager::Instance().LoadRootGeometry();
625#if VECGEOM_VERSION < 0x020000
627 vecgeom::ABBoxManager::Instance().InitABBoxesForCompleteGeometry();
630 vecgeom::BVHManager::Init();
636 for (
auto& lvol : vecgeom::GeoManager::Instance().GetLogicalVolumesMap()) {
637 auto* vol = lvol.second;
638 if (!usesBvhAcceleration(vol)) {
639 vol->SetNavigator(vecgeom::NewSimpleNavigator<>::Instance());
640#ifdef O2_VECGEOM_HAS_BVH_VNAVIGATOR
641 vol->SetLevelLocator(vol->ContainsAssembly() ? vecgeom::SimpleAssemblyLevelLocator::GetInstance()
642 : vecgeom::SimpleLevelLocator::GetInstance());
643 vol->SetSafetyEstimator(vecgeom::SimpleSafetyEstimator::Instance());
645 vol->SetLevelLocator(vecgeom::SimpleLevelLocator::GetInstance());
648#if defined(O2_VECGEOM_HAS_BVH_VNAVIGATOR)
649 vol->SetNavigator(vecgeom::BVHNavigatorV<>::Instance());
650 vol->SetLevelLocator(vol->ContainsAssembly() ? vecgeom::BVHAssemblyAwareLevelLocator::GetInstance()
651 : vecgeom::BVHLevelLocator::GetInstance());
652 vol->SetSafetyEstimator(vecgeom::BVHSafetyEstimator::Instance());
653#elif VECGEOM_VERSION >= 0x020000
670 vol->SetLevelLocator(vecgeom::SimpleABBoxLevelLocator::GetInstance());
672 vol->SetNavigator(vecgeom::BVHNavigator<>::Instance());
673 vol->SetLevelLocator(vecgeom::BVHLevelLocator::GetInstance());
685 ensureVecGeomWorldBuilt();
687 using Vector3D = vecgeom::Vector3D<vecgeom::Precision>;
691 if ((
length = dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]) < TGeoShape::Tolerance() * TGeoShape::Tolerance()) {
695 double invlen = 1. /
length;
696 for (
int i = 3;
i--;) {
708#if VECGEOM_VERSION >= 0x020000
709 thread_local static vecgeom::NavigationState newnavstateStorage, currnavstateStorage, startCacheStorage;
710 thread_local static vecgeom::NavigationState* newnavstate = &newnavstateStorage;
711 thread_local static vecgeom::NavigationState* currnavstate = &currnavstateStorage;
712 thread_local static vecgeom::NavigationState* startCache = &startCacheStorage;
714 thread_local static vecgeom::NavigationState* newnavstate = vecgeom::NavigationState::MakeInstance(vecgeom::GeoManager::Instance().getMaxDepth());
715 thread_local static vecgeom::NavigationState* currnavstate = vecgeom::NavigationState::MakeInstance(vecgeom::GeoManager::Instance().getMaxDepth());
716 thread_local static vecgeom::NavigationState* startCache = vecgeom::NavigationState::MakeInstance(vecgeom::GeoManager::Instance().getMaxDepth());
718 thread_local static bool startCacheValid =
false;
721 Vector3D dirr(dir[0], dir[1], dir[2]);
722 constexpr double kPush = 1.E-6;
723 auto world = vecgeom::GeoManager::Instance().GetWorld();
728 if (startCacheValid && !startCache->IsOutside()) {
729 startCache->CopyTo(currnavstate);
730 vecgeom::Transformation3D
m;
731 currnavstate->TopMatrix(
m);
732 vecgeom::GlobalLocator::RelocatePointFromPath(
m.Transform(currPoint), *currnavstate);
734 currnavstate->Clear();
735 vecgeom::GlobalLocator::LocateGlobalPoint(world, currPoint, *currnavstate,
true);
737 if (currnavstate->IsOutside() || currnavstate->Top() ==
nullptr) {
738 LOG(
error) <<
"start point out of geometry: " <<
x0 <<
':' <<
y0 <<
':' << z0;
739 startCacheValid =
false;
742 currnavstate->CopyTo(startCache);
743 startCacheValid =
true;
746 double remainingDist =
length;
748 while (remainingDist > 1.E-10) {
749 auto* lvol = currnavstate->Top()->GetLogicalVolume();
753 accountMaterial(
static_cast<TGeoMaterial*
>(tgeo2vecgeom::RootGeoManager::Instance().GetMaterialPtr(lvol)), budStep);
754#if VECGEOM_VERSION >= 0x020000
756 usesBvhAcceleration(lvol)
757 ?
static_cast<double>(vecgeom::BVHNavigator::ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate))
758 : static_cast<double>(lvol->GetNavigator()->ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate));
760 const double step =
static_cast<double>(lvol->GetNavigator()->ComputeStepAndPropagatedState(currPoint, dirr, remainingDist, *currnavstate, *newnavstate));
769 LOG(warning) <<
"Cannot cross boundary at (" << currPoint[0] <<
',' << currPoint[1] <<
',' << currPoint[2] <<
')';
775 remainingDist -=
step;
779 currPoint = currPoint + (
step + kPush) * dirr;
780 std::swap(currnavstate, newnavstate);
792#ifdef O2_WITH_VECGEOM
793 ensureVecGeomWorldBuilt();
804#ifdef O2_WITH_VECGEOM
805 ensureVecGeomWorldBuilt();
808#if VECGEOM_VERSION >= 0x020000
809 thread_local vecgeom::NavigationState stateStorage;
810 thread_local vecgeom::NavigationState*
state = &stateStorage;
812 thread_local vecgeom::NavigationState*
state =
813 vecgeom::NavigationState::MakeInstance(vecgeom::GeoManager::Instance().getMaxDepth());
816 const vecgeom::Vector3D<vecgeom::Precision> point(
x,
y,
z);
817 if (vecgeom::GlobalLocator::LocateGlobalPoint(vecgeom::GeoManager::Instance().GetWorld(), point, *
state,
true) ==
821 auto const& converter = tgeo2vecgeom::RootGeoManager::Instance();
824 auto const*
node = placed !=
nullptr ? converter.tgeonode(placed) :
nullptr;
825 if (
node ==
nullptr) {
829 chain.push_back(
const_cast<TGeoNode*
>(
node));
831 return !
chain.empty();
Definition of the base alignment parameters class.
Definition of the GeometryManager class.
std::unique_ptr< expressions::Node > node
char error[CURL_ERROR_SIZE]
Definition of the Names Generator class.
static bool isGeometryLoaded()
static const char * getSymbolicName(o2::detectors::DetID detid, int sensid)
static bool ensureVecGeomWorld()
static Bool_t getOriginalMatrix(o2::detectors::DetID detid, int sensid, TGeoHMatrix &m)
static void loadGeometry(std::string_view geomFilePath="", bool applyMisalignment=false, bool preferAlignedFile=true)
static bool applyAlignment(const std::vector< o2::detectors::AlignParam > &algPars, bool keepPhysicalNodes=true)
static o2::base::MatBudget meanMaterialBudget(float x0, float y0, float z0, float x1, float y1, float z1, TGeoNavigator *nav=nullptr)
static MatBudgetExt meanMaterialBudgetExt(float x0, float y0, float z0, float x1, float y1, float z1)
static int getSensID(o2::detectors::DetID detid, int sensid)
static bool vecGeomLocate(double x, double y, double z, std::vector< TGeoNode * > &chain)
static TGeoHMatrix * getMatrix(const char *symname)
static TGeoPNEntry * getPNEntry(o2::detectors::DetID detid, Int_t sensid)
static void applyMisalignent(bool applyMisalignment=true)
static std::string getAlignedGeomFileName(const std::string_view prefix="")
static std::string getGeomFileName(const std::string_view prefix="")
static constexpr std::string_view CCDBOBJECT
static constexpr std::string_view GEOMOBJECTNAME_FAIR
static const GeometryManagerParam & Instance()
Static class with identifiers, bitmasks and names for ALICE detectors.
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLuint GLfloat GLfloat GLfloat x1
GLboolean GLboolean GLboolean b
GLuint GLsizei GLsizei * length
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLsizei const GLchar *const * path
GLboolean GLboolean GLboolean GLboolean a
GLuint GLfloat GLfloat y0
GLdouble GLdouble GLdouble z
float float float float z1
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< T >, ROOT::Math::DefaultCoordinateSystemTag > Vector3D
void normalize(double nrm)
float length
length in material
float meanRho
mean density, g/cm^3
float meanX2X0
fraction of radiaton lenght
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"