From 7216de3942ca045a17196021bc03913456428357 Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Thu, 26 Nov 2026 16:30:18 +0100 Subject: [PATCH] Add the MCH gas volumes to the parallel world This adds the MCH gas volumes to the parallel world, so that a slat which the alignment moves into its support panel keeps its hits. - MCH::Detector now implements fillParallelWorld with the volumes of geo::getSensitiveVolumes(). - The detection elements are assemblies, so the gas volumes inside them are registered one by one. - A sensitive volume that is not registered is reported as an error. - Mirrored placements are left out with a warning that names the detection element, because a mirrored node in the parallel world stops tracks at its boundary. Today this concerns DE 825. https://its.cern.ch/jira/browse/O2-7358 Co-Authored-By: Claude Opus 5.5 --- .../include/MCHSimulation/Detector.h | 3 + .../MUON/MCH/Simulation/src/Detector.cxx | 84 ++++++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/Detectors/MUON/MCH/Simulation/include/MCHSimulation/Detector.h b/Detectors/MUON/MCH/Simulation/include/MCHSimulation/Detector.h index d1a04f2cf18a5..82de3c2977523 100644 --- a/Detectors/MUON/MCH/Simulation/include/MCHSimulation/Detector.h +++ b/Detectors/MUON/MCH/Simulation/include/MCHSimulation/Detector.h @@ -45,6 +45,9 @@ class Detector : public o2::base::DetImpl /// Add alignable top volumes void addAlignableVolumes() const override; + /// Add the gas volumes of every detection element to the parallel world + void fillParallelWorld() const override; + std::vector* getHits(int); void EndOfEvent() override; diff --git a/Detectors/MUON/MCH/Simulation/src/Detector.cxx b/Detectors/MUON/MCH/Simulation/src/Detector.cxx index 6e664b8f036b3..1666609893a41 100644 --- a/Detectors/MUON/MCH/Simulation/src/Detector.cxx +++ b/Detectors/MUON/MCH/Simulation/src/Detector.cxx @@ -14,10 +14,16 @@ #include "DetectorsBase/Stack.h" #include "Stepper.h" #include "TGeoManager.h" +#include "TGeoMatrix.h" +#include "TGeoParallelWorld.h" +#include "TGeoPhysicalNode.h" +#include #include #include #include -#include "TGeoManager.h" +#include +#include +#include ClassImp(o2::mch::Detector); @@ -70,6 +76,82 @@ void Detector::addAlignableVolumes() const geo::addAlignableVolumes(*gGeoManager); } +namespace +{ +/// Whether a matrix mirrors space +bool isReflection(TGeoMatrix const& matrix) +{ + const double* r = matrix.GetRotationMatrix(); + return r[0] * (r[4] * r[8] - r[5] * r[7]) - r[1] * (r[3] * r[8] - r[5] * r[6]) + r[2] * (r[3] * r[7] - r[4] * r[6]) < 0; +} + +/// Registers the gas volumes of the MCH detection elements in the parallel world +struct GasRegistrar { + TGeoParallelWorld& pw; + std::set gas; + std::set registered; + std::set mirroredDEs; + int nAdded = 0; + + /// Walks the assemblies below path (a detection element is one) and adds the gas volumes in them + void add(TGeoVolume const& volume, std::string const& path, bool reflected, std::string_view de) + { + for (int i = 0; i < volume.GetNdaughters(); ++i) { + auto node = volume.GetNode(i); + auto daughter = node->GetVolume(); + auto daughterPath = path + "/" + node->GetName(); + bool daughterReflected = reflected != isReflection(*node->GetMatrix()); + if (daughter->IsAssembly()) { + add(*daughter, daughterPath, daughterReflected, de); + } else if (gas.contains(daughter)) { + // a mirrored node in the parallel world stops tracks at its boundary with TGeo in Geant4, + // and VecGeomParallelWorld finds no VecGeom path for it (fatal) + if (daughterReflected) { + mirroredDEs.emplace(de); + continue; + } + pw.AddNode(daughterPath.c_str()); + registered.insert(daughter); + ++nAdded; + } + } + } +}; +} // namespace + +void Detector::fillParallelWorld() const +{ + TGeoParallelWorld* pw = gGeoManager->GetParallelWorld(); + if (pw == nullptr) { + LOG(error) << "Parallel world was not created"; + return; + } + auto sensitive = geo::getSensitiveVolumes(); + GasRegistrar registrar{*pw, std::set(sensitive.begin(), sensitive.end())}; + for (int i = 0; i < gGeoManager->GetNAlignable(); ++i) { + auto pne = gGeoManager->GetAlignableEntry(i); + std::string_view sname = pne->GetName(); + if (!sname.starts_with("MCH/HC") || sname.find("/DE") == std::string_view::npos) { + continue; + } + if (!gGeoManager->cd(pne->GetTitle())) { + LOG(fatal) << "Cannot reach " << sname << " at " << pne->GetTitle(); + } + registrar.add(*gGeoManager->GetCurrentVolume(), pne->GetTitle(), isReflection(*gGeoManager->GetCurrentMatrix()), + sname.substr(sname.rfind('/') + 1)); + } + gGeoManager->CdTop(); + LOG(info) << "MCH added " << registrar.nAdded << " gas volumes to the parallel world"; + for (auto const& de : registrar.mirroredDEs) { + LOG(warning) << "MCH left the gas of the mirrored placement " << de << " out of the parallel world"; + } + for (auto* vol : sensitive) { + if (!registrar.registered.contains(vol)) { + LOG(error) << "MCH sensitive volume " << vol->GetName() << " was not added to the parallel world"; + } + } +} + Bool_t Detector::ProcessHits(FairVolume* v) { mStepper->process(*fMC);