Skip to content

Commit 2a9e915

Browse files
authored
o2-sim: Tell the exited volume by its path in VecGeom navigation (#16144)
This fixes tracks stopping in the mother when they leave a volume into another copy of its container. - After an exit through coinciding faces the locate must not descend back into the volume just left. - That volume was recognised by its placement alone, which also sits in every other copy of its mother. - A track going from a tile in one EMCAL tower to the same tile in the neighbouring tower was therefore left in the air around the towers, with a zero-length step. - The volume is now recognised by its whole path. - Add a unit test
1 parent 1171788 commit 2a9e915

4 files changed

Lines changed: 228 additions & 8 deletions

File tree

‎Detectors/gconfig/CMakeLists.txt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ o2_add_test(StaticTLSReserve
8080
PUBLIC_LINK_LIBRARIES O2::SimSetup
8181
LABELS simsetup)
8282

83+
if(G4SETUP_WITH_VECGEOM)
84+
o2_add_test(VecGeomG4Navigator
85+
SOURCES test/testVecGeomG4Navigator.cxx
86+
COMPONENT_NAME SimSetup
87+
TARGETVARNAME targetNavigatorTest
88+
PUBLIC_LINK_LIBRARIES O2::G4Setup TGeo2VecGeom::TGeo2VecGeom
89+
LABELS simsetup)
90+
if(targetNavigatorTest)
91+
target_compile_definitions(${targetNavigatorTest} PRIVATE O2_WITH_VECGEOM)
92+
endif()
93+
endif()
94+
8395
o2_add_test_root_macro(UserDecay.C
8496
PUBLIC_LINK_LIBRARIES O2::SimSetup
8597
LABELS simsetup)

‎Detectors/gconfig/src/VecGeomG4Navigator.cxx‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ G4double VecGeomG4Navigator::ComputeStep(const G4ThreeVector& globalPoint, const
213213
/// under a field is the momentum. Nor does it descend back into `avoid`, the volume the crossing
214214
/// exited: on a face a helix touches tangentially chord and momentum disagree about the side.
215215
void VecGeomG4Navigator::leaveFlushVolumes(const V3& point, const V3& dir, int minLevel,
216-
vecgeom::VPlacedVolume const* avoid)
216+
vecgeom::NavigationState const* avoid)
217217
{
218218
for (int round = 0; round < 4; ++round) {
219219
vecgeom::VPlacedVolume const* left = nullptr;
@@ -247,12 +247,14 @@ void VecGeomG4Navigator::leaveFlushVolumes(const V3& point, const V3& dir, int m
247247
return; // no daughter holds the point
248248
}
249249
if (avoid != nullptr && mCurState.GetCurrentLevel() > level) {
250-
// Undo a descent into the volume the crossing exited, at whatever depth the search put it.
251-
mPathScratch = mCurState;
252-
while (mPathScratch.GetCurrentLevel() > level && mPathScratch.Top() != avoid) {
253-
mPathScratch.Pop();
250+
// Undo a descent into the volume the crossing exited. It is told by its whole path: the same
251+
// placement also sits in every other copy of its mother, where the track may well go.
252+
const int exited = avoid->GetCurrentLevel();
253+
bool back = static_cast<int>(mCurState.GetCurrentLevel()) >= exited;
254+
for (int l = exited - 1; back && l > 0; --l) {
255+
back = mCurState.At(l) == avoid->At(l);
254256
}
255-
if (mPathScratch.GetCurrentLevel() > level) {
257+
if (back) {
256258
while (mCurState.GetCurrentLevel() > level) {
257259
mCurState.Pop();
258260
}
@@ -369,7 +371,7 @@ G4VPhysicalVolume* VecGeomG4Navigator::LocateGlobalPointAndSetup(const G4ThreeVe
369371
}
370372
if (crossing) {
371373
leaveFlushVolumes(q, dir, entering ? static_cast<int>(mNextState.GetCurrentLevel()) : 0,
372-
entering ? nullptr : topOf(mReloScratch));
374+
entering || topOf(mReloScratch) == nullptr ? nullptr : &mReloScratch);
373375
updateCrossingFlags(entering);
374376
}
375377
mWouldEnter = false;

‎Detectors/gconfig/src/VecGeomG4Navigator.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class VecGeomG4Navigator : public VecGeomG4NavigatorBase
6565
G4VPhysicalVolume* updateG4History();
6666
void locateFromWorld(const V3& point);
6767
/// Go up past every volume the point is on the surface of and heading out of, then down again.
68-
void leaveFlushVolumes(const V3& point, const V3& dir, int minLevel, vecgeom::VPlacedVolume const* avoid);
68+
void leaveFlushVolumes(const V3& point, const V3& dir, int minLevel, vecgeom::NavigationState const* avoid);
6969
/// Sets fEnteredDaughter and fExitedMother from the paths before (mReloScratch) and after a crossing.
7070
void updateCrossingFlags(bool entering);
7171
void clearLastExited()
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
/// The relocating VecGeom navigator against TGeo on a geometry of coinciding faces: two copies of a tower
13+
/// side by side that fill their mother, each a stack of tiles that reach the tower's faces, as in a sampling
14+
/// calorimeter. A ray has to visit the same volumes in the same order, and never stop for a zero-length step
15+
/// in a mother it only touches.
16+
17+
#define BOOST_TEST_MODULE Test SimSetup VecGeomG4Navigator
18+
#define BOOST_TEST_MAIN
19+
#define BOOST_TEST_DYN_LINK
20+
#include <boost/test/unit_test.hpp>
21+
22+
// before the VecGeom headers, which define a VECGEOM macro
23+
#include "DetectorsBase/GeometryManager.h"
24+
25+
#include "../src/VecGeomG4Map.h"
26+
#include "../src/VecGeomG4Navigator.h"
27+
28+
#include "TG4RootDetectorConstruction.h"
29+
30+
#include "G4SystemOfUnits.hh"
31+
#include "G4TouchableHistory.hh"
32+
#include "G4VPhysicalVolume.hh"
33+
34+
#include <TGeoManager.h>
35+
#include <TGeoMaterial.h>
36+
#include <TGeoMatrix.h>
37+
#include <TGeoMedium.h>
38+
#include <TGeoNode.h>
39+
#include <TGeoVolume.h>
40+
#include <TString.h>
41+
42+
#include <cmath>
43+
#include <memory>
44+
#include <random>
45+
#include <string>
46+
#include <vector>
47+
48+
using o2::simsetup::VecGeomG4Map;
49+
using o2::simsetup::VecGeomG4Navigator;
50+
51+
namespace
52+
{
53+
struct Fixture {
54+
std::unique_ptr<TG4RootDetectorConstruction> construction;
55+
VecGeomG4Map map;
56+
std::unique_ptr<VecGeomG4Navigator> navigator;
57+
58+
Fixture()
59+
{
60+
auto* geom = new TGeoManager("navtest", "coinciding faces");
61+
auto* medium = new TGeoMedium("alu", 1, new TGeoMaterial("alu", 26.98, 13., 2.7));
62+
auto* top = geom->MakeBox("TOP", medium, 100., 100., 100.);
63+
geom->SetTopVolume(top);
64+
// module > pair > two towers > paper tiles > lead tile; every box reaches the faces of the one around it
65+
// in x and y, and the towers fill the pair, the pair the module
66+
auto* module = geom->MakeBox("MODU", medium, 10., 5., 10.);
67+
auto* pair = geom->MakeBox("PAIR", medium, 10., 5., 10.);
68+
auto* tower = geom->MakeBox("TOWR", medium, 5., 5., 10.);
69+
auto* paper = geom->MakeBox("PAPR", medium, 5., 5., 0.5);
70+
auto* lead = geom->MakeBox("LEAD", medium, 5., 5., 0.3);
71+
paper->AddNode(lead, 1, new TGeoTranslation(0., 0., 0.));
72+
for (int i = 0; i < 8; ++i) {
73+
tower->AddNode(paper, i + 1, new TGeoTranslation(0., 0., -8.75 + 2.5 * i));
74+
}
75+
pair->AddNode(tower, 1, new TGeoTranslation(-5., 0., 0.));
76+
pair->AddNode(tower, 2, new TGeoTranslation(5., 0., 0.));
77+
module->AddNode(pair, 1, new TGeoTranslation(0., 0., 0.));
78+
// two modules that touch in y, and one apart
79+
top->AddNode(module, 1, new TGeoTranslation(0., -5., 0.));
80+
top->AddNode(module, 2, new TGeoTranslation(0., 5., 0.));
81+
top->AddNode(module, 3, new TGeoTranslation(40., 0., 0.));
82+
geom->CloseGeometry();
83+
84+
construction = std::make_unique<TG4RootDetectorConstruction>(geom);
85+
auto* world = construction->Construct();
86+
BOOST_REQUIRE(world != nullptr);
87+
o2::base::GeometryManager::buildVecGeomGeometry(true);
88+
map.build(*construction, true);
89+
navigator = std::make_unique<VecGeomG4Navigator>(map, 1.e-9, false);
90+
navigator->SetWorldVolume(world);
91+
}
92+
};
93+
94+
Fixture& fixture()
95+
{
96+
static Fixture f;
97+
return f;
98+
}
99+
100+
/// The path of the navigator's touchable, in TGeo's notation.
101+
std::string touchablePath(G4Navigator& nav)
102+
{
103+
std::unique_ptr<G4TouchableHistory> touchable(nav.CreateTouchableHistory());
104+
std::string path;
105+
for (int depth = touchable->GetHistoryDepth(); depth >= 0; --depth) {
106+
auto const* pv = touchable->GetVolume(depth);
107+
if (pv == nullptr) {
108+
return "";
109+
}
110+
// TGeo names a node by volume and copy number, and its top node is copy 1
111+
path += "/" + std::string(pv->GetName()) + "_" + std::to_string(depth == touchable->GetHistoryDepth() ? 1 : pv->GetCopyNo());
112+
}
113+
return path;
114+
}
115+
116+
struct Visit {
117+
std::string path;
118+
double length; // cm
119+
};
120+
121+
/// The volumes a ray crosses with a non-zero length, and how many zero-length steps came between.
122+
std::vector<Visit> withNavigator(G4Navigator& nav, const double* p, const double* d, int& zeroSteps)
123+
{
124+
std::vector<Visit> visits;
125+
G4ThreeVector point(p[0] * cm, p[1] * cm, p[2] * cm);
126+
const G4ThreeVector dir(d[0], d[1], d[2]);
127+
auto* pv = nav.LocateGlobalPointAndSetup(point, &dir, false, false);
128+
for (int k = 0; k < 10000 && pv != nullptr; ++k) {
129+
G4double safety = 0.;
130+
const double step = nav.ComputeStep(point, dir, 1.e6 * cm, safety);
131+
if (!(step < 1.e6 * cm)) {
132+
break;
133+
}
134+
if (step > 1.e-7 * cm) {
135+
visits.push_back({touchablePath(nav), step / cm});
136+
} else {
137+
++zeroSteps;
138+
}
139+
point += step * dir;
140+
nav.SetGeometricallyLimitedStep();
141+
pv = nav.LocateGlobalPointAndSetup(point, &dir, true, false);
142+
}
143+
return visits;
144+
}
145+
146+
std::vector<Visit> withTGeo(const double* p, const double* d)
147+
{
148+
std::vector<Visit> visits;
149+
gGeoManager->InitTrack(p[0], p[1], p[2], d[0], d[1], d[2]);
150+
for (int k = 0; k < 10000 && !gGeoManager->IsOutside(); ++k) {
151+
const std::string path = gGeoManager->GetPath();
152+
gGeoManager->FindNextBoundaryAndStep();
153+
if (gGeoManager->GetStep() > 1.e-7) {
154+
visits.push_back({path, gGeoManager->GetStep()});
155+
}
156+
}
157+
return visits;
158+
}
159+
} // namespace
160+
161+
BOOST_AUTO_TEST_CASE(rays_visit_the_volumes_tgeo_visits)
162+
{
163+
auto& f = fixture();
164+
std::mt19937_64 gen(1);
165+
std::uniform_real_distribution<double> u(-1., 1.);
166+
std::normal_distribution<double> g;
167+
int rays = 0, differentCount = 0, differentPath = 0, differentLength = 0, zeroSteps = 0, crossings = 0;
168+
std::string firstDifference;
169+
for (int r = 0; r < 20000; ++r) {
170+
// start inside the two touching modules, off every face
171+
const double p[3] = {9.9 * u(gen), 9.9 * u(gen), 9.9 * u(gen)};
172+
double d[3] = {g(gen), g(gen), 0.3 * g(gen)};
173+
const double norm = std::sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
174+
for (auto& x : d) {
175+
x /= norm;
176+
}
177+
const auto expected = withTGeo(p, d);
178+
const auto found = withNavigator(*f.navigator, p, d, zeroSteps);
179+
++rays;
180+
crossings += expected.size();
181+
if (found.size() != expected.size()) {
182+
++differentCount;
183+
if (firstDifference.empty()) {
184+
firstDifference = Form("ray %d: %zu visits, TGeo %zu", r, found.size(), expected.size());
185+
}
186+
continue;
187+
}
188+
for (std::size_t i = 0; i < found.size(); ++i) {
189+
if (found[i].path != expected[i].path) {
190+
++differentPath;
191+
if (firstDifference.empty()) {
192+
firstDifference = Form("ray %d visit %zu: %s, TGeo %s", r, i, found[i].path.c_str(), expected[i].path.c_str());
193+
}
194+
break;
195+
}
196+
differentLength += std::abs(found[i].length - expected[i].length) > 1.e-6;
197+
}
198+
}
199+
BOOST_TEST_MESSAGE("rays " << rays << ", volumes crossed " << crossings << ", zero-length steps " << zeroSteps << " " << firstDifference);
200+
BOOST_CHECK_GT(crossings, 100000);
201+
BOOST_CHECK_MESSAGE(differentCount == 0, differentCount << " rays with a different number of volumes; " << firstDifference);
202+
BOOST_CHECK_MESSAGE(differentPath == 0, differentPath << " rays with a different volume; " << firstDifference);
203+
BOOST_CHECK_EQUAL(differentLength, 0);
204+
// every face here coincides with others, and none of them is a place to stop
205+
BOOST_CHECK_EQUAL(zeroSteps, 0);
206+
}

0 commit comments

Comments
 (0)