|
| 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