diff --git a/Detectors/gconfig/CMakeLists.txt b/Detectors/gconfig/CMakeLists.txt index a1f61a9961760..c5ba4922f3ee5 100644 --- a/Detectors/gconfig/CMakeLists.txt +++ b/Detectors/gconfig/CMakeLists.txt @@ -80,6 +80,18 @@ o2_add_test(StaticTLSReserve PUBLIC_LINK_LIBRARIES O2::SimSetup LABELS simsetup) +if(G4SETUP_WITH_VECGEOM) + o2_add_test(VecGeomG4Navigator + SOURCES test/testVecGeomG4Navigator.cxx + COMPONENT_NAME SimSetup + TARGETVARNAME targetNavigatorTest + PUBLIC_LINK_LIBRARIES O2::G4Setup TGeo2VecGeom::TGeo2VecGeom + LABELS simsetup) + if(targetNavigatorTest) + target_compile_definitions(${targetNavigatorTest} PRIVATE O2_WITH_VECGEOM) + endif() +endif() + o2_add_test_root_macro(UserDecay.C PUBLIC_LINK_LIBRARIES O2::SimSetup LABELS simsetup) diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.cxx b/Detectors/gconfig/src/VecGeomG4Navigator.cxx index 00302f00b367c..7fdb2c8b1bff6 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.cxx +++ b/Detectors/gconfig/src/VecGeomG4Navigator.cxx @@ -213,7 +213,7 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const /// under a field is the momentum. Nor does it descend back into `avoid`, the volume the crossing /// exited: on a face a helix touches tangentially chord and momentum disagree about the side. void VecGeomG4Navigator::leaveFlushVolumes(const V3& point, const V3& dir, int minLevel, - vecgeom::VPlacedVolume const* avoid) + vecgeom::NavigationState const* avoid) { for (int round = 0; round < 4; ++round) { vecgeom::VPlacedVolume const* left = nullptr; @@ -247,12 +247,14 @@ void VecGeomG4Navigator::leaveFlushVolumes(const V3& point, const V3& dir, int m return; // no daughter holds the point } if (avoid != nullptr && mCurState.GetCurrentLevel() > level) { - // Undo a descent into the volume the crossing exited, at whatever depth the search put it. - mPathScratch = mCurState; - while (mPathScratch.GetCurrentLevel() > level && mPathScratch.Top() != avoid) { - mPathScratch.Pop(); + // Undo a descent into the volume the crossing exited. It is told by its whole path: the same + // placement also sits in every other copy of its mother, where the track may well go. + const int exited = avoid->GetCurrentLevel(); + bool back = static_cast(mCurState.GetCurrentLevel()) >= exited; + for (int l = exited - 1; back && l > 0; --l) { + back = mCurState.At(l) == avoid->At(l); } - if (mPathScratch.GetCurrentLevel() > level) { + if (back) { while (mCurState.GetCurrentLevel() > level) { mCurState.Pop(); } @@ -369,7 +371,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe } if (crossing) { leaveFlushVolumes(q, dir, entering ? static_cast(mNextState.GetCurrentLevel()) : 0, - entering ? nullptr : topOf(mReloScratch)); + entering || topOf(mReloScratch) == nullptr ? nullptr : &mReloScratch); updateCrossingFlags(entering); } mWouldEnter = false; diff --git a/Detectors/gconfig/src/VecGeomG4Navigator.h b/Detectors/gconfig/src/VecGeomG4Navigator.h index 300c318090f10..937f909aeca0c 100644 --- a/Detectors/gconfig/src/VecGeomG4Navigator.h +++ b/Detectors/gconfig/src/VecGeomG4Navigator.h @@ -65,7 +65,7 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase G4VPhysicalVolume* updateG4History(); void locateFromWorld(const V3& point); /// Go up past every volume the point is on the surface of and heading out of, then down again. - void leaveFlushVolumes(const V3& point, const V3& dir, int minLevel, vecgeom::VPlacedVolume const* avoid); + void leaveFlushVolumes(const V3& point, const V3& dir, int minLevel, vecgeom::NavigationState const* avoid); /// Sets fEnteredDaughter and fExitedMother from the paths before (mReloScratch) and after a crossing. void updateCrossingFlags(bool entering); void clearLastExited() diff --git a/Detectors/gconfig/test/testVecGeomG4Navigator.cxx b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx new file mode 100644 index 0000000000000..85fcd47436dac --- /dev/null +++ b/Detectors/gconfig/test/testVecGeomG4Navigator.cxx @@ -0,0 +1,206 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +/// The relocating VecGeom navigator against TGeo on a geometry of coinciding faces: two copies of a tower +/// side by side that fill their mother, each a stack of tiles that reach the tower's faces, as in a sampling +/// calorimeter. A ray has to visit the same volumes in the same order, and never stop for a zero-length step +/// in a mother it only touches. + +#define BOOST_TEST_MODULE Test SimSetup VecGeomG4Navigator +#define BOOST_TEST_MAIN +#define BOOST_TEST_DYN_LINK +#include + +// before the VecGeom headers, which define a VECGEOM macro +#include "DetectorsBase/GeometryManager.h" + +#include "../src/VecGeomG4Map.h" +#include "../src/VecGeomG4Navigator.h" + +#include "TG4RootDetectorConstruction.h" + +#include "G4SystemOfUnits.hh" +#include "G4TouchableHistory.hh" +#include "G4VPhysicalVolume.hh" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +using o2::simsetup::VecGeomG4Map; +using o2::simsetup::VecGeomG4Navigator; + +namespace +{ +struct Fixture { + std::unique_ptr construction; + VecGeomG4Map map; + std::unique_ptr navigator; + + Fixture() + { + auto* geom = new TGeoManager("navtest", "coinciding faces"); + auto* medium = new TGeoMedium("alu", 1, new TGeoMaterial("alu", 26.98, 13., 2.7)); + auto* top = geom->MakeBox("TOP", medium, 100., 100., 100.); + geom->SetTopVolume(top); + // module > pair > two towers > paper tiles > lead tile; every box reaches the faces of the one around it + // in x and y, and the towers fill the pair, the pair the module + auto* module = geom->MakeBox("MODU", medium, 10., 5., 10.); + auto* pair = geom->MakeBox("PAIR", medium, 10., 5., 10.); + auto* tower = geom->MakeBox("TOWR", medium, 5., 5., 10.); + auto* paper = geom->MakeBox("PAPR", medium, 5., 5., 0.5); + auto* lead = geom->MakeBox("LEAD", medium, 5., 5., 0.3); + paper->AddNode(lead, 1, new TGeoTranslation(0., 0., 0.)); + for (int i = 0; i < 8; ++i) { + tower->AddNode(paper, i + 1, new TGeoTranslation(0., 0., -8.75 + 2.5 * i)); + } + pair->AddNode(tower, 1, new TGeoTranslation(-5., 0., 0.)); + pair->AddNode(tower, 2, new TGeoTranslation(5., 0., 0.)); + module->AddNode(pair, 1, new TGeoTranslation(0., 0., 0.)); + // two modules that touch in y, and one apart + top->AddNode(module, 1, new TGeoTranslation(0., -5., 0.)); + top->AddNode(module, 2, new TGeoTranslation(0., 5., 0.)); + top->AddNode(module, 3, new TGeoTranslation(40., 0., 0.)); + geom->CloseGeometry(); + + construction = std::make_unique(geom); + auto* world = construction->Construct(); + BOOST_REQUIRE(world != nullptr); + o2::base::GeometryManager::buildVecGeomGeometry(true); + map.build(*construction, true); + navigator = std::make_unique(map, 1.e-9, false); + navigator->SetWorldVolume(world); + } +}; + +Fixture& fixture() +{ + static Fixture f; + return f; +} + +/// The path of the navigator's touchable, in TGeo's notation. +std::string touchablePath(G4Navigator& nav) +{ + std::unique_ptr touchable(nav.CreateTouchableHistory()); + std::string path; + for (int depth = touchable->GetHistoryDepth(); depth >= 0; --depth) { + auto const* pv = touchable->GetVolume(depth); + if (pv == nullptr) { + return ""; + } + // TGeo names a node by volume and copy number, and its top node is copy 1 + path += "/" + std::string(pv->GetName()) + "_" + std::to_string(depth == touchable->GetHistoryDepth() ? 1 : pv->GetCopyNo()); + } + return path; +} + +struct Visit { + std::string path; + double length; // cm +}; + +/// The volumes a ray crosses with a non-zero length, and how many zero-length steps came between. +std::vector withNavigator(G4Navigator& nav, const double* p, const double* d, int& zeroSteps) +{ + std::vector visits; + G4ThreeVector point(p[0] * cm, p[1] * cm, p[2] * cm); + const G4ThreeVector dir(d[0], d[1], d[2]); + auto* pv = nav.LocateGlobalPointAndSetup(point, &dir, false, false); + for (int k = 0; k < 10000 && pv != nullptr; ++k) { + G4double safety = 0.; + const double step = nav.ComputeStep(point, dir, 1.e6 * cm, safety); + if (!(step < 1.e6 * cm)) { + break; + } + if (step > 1.e-7 * cm) { + visits.push_back({touchablePath(nav), step / cm}); + } else { + ++zeroSteps; + } + point += step * dir; + nav.SetGeometricallyLimitedStep(); + pv = nav.LocateGlobalPointAndSetup(point, &dir, true, false); + } + return visits; +} + +std::vector withTGeo(const double* p, const double* d) +{ + std::vector visits; + gGeoManager->InitTrack(p[0], p[1], p[2], d[0], d[1], d[2]); + for (int k = 0; k < 10000 && !gGeoManager->IsOutside(); ++k) { + const std::string path = gGeoManager->GetPath(); + gGeoManager->FindNextBoundaryAndStep(); + if (gGeoManager->GetStep() > 1.e-7) { + visits.push_back({path, gGeoManager->GetStep()}); + } + } + return visits; +} +} // namespace + +BOOST_AUTO_TEST_CASE(rays_visit_the_volumes_tgeo_visits) +{ + auto& f = fixture(); + std::mt19937_64 gen(1); + std::uniform_real_distribution u(-1., 1.); + std::normal_distribution g; + int rays = 0, differentCount = 0, differentPath = 0, differentLength = 0, zeroSteps = 0, crossings = 0; + std::string firstDifference; + for (int r = 0; r < 20000; ++r) { + // start inside the two touching modules, off every face + const double p[3] = {9.9 * u(gen), 9.9 * u(gen), 9.9 * u(gen)}; + double d[3] = {g(gen), g(gen), 0.3 * g(gen)}; + const double norm = std::sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); + for (auto& x : d) { + x /= norm; + } + const auto expected = withTGeo(p, d); + const auto found = withNavigator(*f.navigator, p, d, zeroSteps); + ++rays; + crossings += expected.size(); + if (found.size() != expected.size()) { + ++differentCount; + if (firstDifference.empty()) { + firstDifference = Form("ray %d: %zu visits, TGeo %zu", r, found.size(), expected.size()); + } + continue; + } + for (std::size_t i = 0; i < found.size(); ++i) { + if (found[i].path != expected[i].path) { + ++differentPath; + if (firstDifference.empty()) { + firstDifference = Form("ray %d visit %zu: %s, TGeo %s", r, i, found[i].path.c_str(), expected[i].path.c_str()); + } + break; + } + differentLength += std::abs(found[i].length - expected[i].length) > 1.e-6; + } + } + BOOST_TEST_MESSAGE("rays " << rays << ", volumes crossed " << crossings << ", zero-length steps " << zeroSteps << " " << firstDifference); + BOOST_CHECK_GT(crossings, 100000); + BOOST_CHECK_MESSAGE(differentCount == 0, differentCount << " rays with a different number of volumes; " << firstDifference); + BOOST_CHECK_MESSAGE(differentPath == 0, differentPath << " rays with a different volume; " << firstDifference); + BOOST_CHECK_EQUAL(differentLength, 0); + // every face here coincides with others, and none of them is a place to stop + BOOST_CHECK_EQUAL(zeroSteps, 0); +}