FairShip
Loading...
Searching...
No Matches
ActsStrawGeometry.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
6
7#include <cmath>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11#include <vector>
12
13#include "Acts/Definitions/Units.hpp"
14#include "Acts/Geometry/GeometryIdentifier.hpp"
15#include "Acts/Material/HomogeneousSurfaceMaterial.hpp"
16#include "Acts/Material/Material.hpp"
17#include "Acts/Material/MaterialSlab.hpp"
18#include "Acts/Surfaces/LineBounds.hpp"
19#include "Acts/Surfaces/StrawSurface.hpp"
20#include "ActsFrame.hpp"
21#include "TGeoManager.h"
22#include "TGeoMaterial.h"
23#include "TGeoMatrix.h"
24#include "TGeoNode.h"
25#include "TGeoTube.h"
26#include "TGeoVolume.h"
27
28namespace SHiP {
29
30namespace {
31
32namespace u = Acts::UnitConstants;
33
34// The gas volume, which defines the detector ID, is placed with the straw
35// copy number plus 1000 (see strawtubes::ConstructGeometry).
36constexpr int kGasCopyOffset = 1000;
37
40const TGeoNode* strawComponent(const TGeoVolume& volume,
41 std::string_view name) {
42 for (int i = 0; i < volume.GetNdaughters(); ++i) {
43 const TGeoNode* node = volume.GetNode(i);
44 if (std::string_view(node->GetVolume()->GetName()) == name) {
45 return node;
46 }
47 }
48 return nullptr;
49}
50
56std::shared_ptr<const Acts::HomogeneousSurfaceMaterial> strawMaterial(
57 const TGeoVolume& volume) {
58 double diameter = 0.;
59 std::vector<Acts::MaterialSlab> slabs;
60 for (const char* name : {"straw", "gas", "wire"}) {
61 const TGeoNode* node = strawComponent(volume, name);
62 if (node == nullptr) {
63 if (std::string_view(name) == "wire") {
64 continue;
65 }
66 throw std::runtime_error(std::string("ActsStrawGeometry: no straw ") +
67 name + " volume");
68 }
69 const auto* tube =
70 dynamic_cast<const TGeoTube*>(node->GetVolume()->GetShape());
71 if (tube == nullptr) {
72 throw std::runtime_error(std::string("ActsStrawGeometry: straw ") + name +
73 " is not a TGeoTube");
74 }
75 if (diameter == 0.) {
76 diameter = 2. * tube->GetRmax();
77 }
78 const TGeoMaterial* material = node->GetVolume()->GetMaterial();
79 const double area = M_PI * (tube->GetRmax() * tube->GetRmax() -
80 tube->GetRmin() * tube->GetRmin());
81 slabs.emplace_back(
82 Acts::Material::fromMassDensity(
83 material->GetRadLen() * ActsFrame::kShipLength,
84 material->GetIntLen() * ActsFrame::kShipLength, material->GetA(),
85 material->GetZ(), material->GetDensity() * u::g / u::cm3),
86 area / diameter * ActsFrame::kShipLength);
87 }
88 return std::make_shared<Acts::HomogeneousSurfaceMaterial>(
89 Acts::MaterialSlab::combineLayers(slabs));
90}
91
92Acts::Transform3 toTransform(const TGeoHMatrix& matrix) {
93 const Double_t* r = matrix.GetRotationMatrix();
94 const Double_t* t = matrix.GetTranslation();
95 Acts::RotationMatrix3 rotation;
96 rotation << r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8];
97 Acts::Transform3 transform = Acts::Transform3::Identity();
98 transform.linear() = rotation;
99 transform.translation() = Acts::Vector3(t[0], t[1], t[2]);
100 return transform;
101}
102
104Acts::GeometryIdentifier geometryId(int detectorId) {
105 const int station = detectorId / 1000000;
106 const int view = detectorId / 100000 % 10;
107 const int layer = detectorId / 10000 % 10;
108 const int straw = detectorId % 1000;
109 return Acts::GeometryIdentifier()
110 .withVolume(1)
111 .withLayer(8 * station + 2 * view + layer)
112 .withSensitive(straw);
113}
114
115struct Builder {
116 std::unordered_map<int, std::shared_ptr<Acts::Surface>>& surfaces;
117 std::unordered_map<int, StrawWire>& wires;
118 std::shared_ptr<const Acts::HomogeneousSurfaceMaterial> material;
119
120 void visit(const TGeoVolume& volume, const TGeoHMatrix& parent) {
121 for (int i = 0; i < volume.GetNdaughters(); ++i) {
122 const TGeoNode* node = volume.GetNode(i);
123 TGeoHMatrix global = parent;
124 global.Multiply(node->GetMatrix());
125 const TGeoVolume* daughter = node->GetVolume();
126 if (std::string_view(daughter->GetName()) == "straw") {
127 if (material == nullptr) {
128 // All straws are alike
129 material = strawMaterial(volume);
130 }
131 addStraw(*node, global);
132 } else {
133 visit(*daughter, global);
134 }
135 }
136 }
137
138 void addStraw(const TGeoNode& node, const TGeoHMatrix& global) {
139 const auto* tube =
140 dynamic_cast<const TGeoTube*>(node.GetVolume()->GetShape());
141 if (tube == nullptr) {
142 throw std::runtime_error("ActsStrawGeometry: straw is not a TGeoTube");
143 }
144 const int detectorId = node.GetNumber() + kGasCopyOffset;
145 auto bounds = std::make_shared<const Acts::LineBounds>(
146 tube->GetRmax() * ActsFrame::kShipLength,
147 tube->GetDZ() * ActsFrame::kShipLength);
148 auto surface = Acts::Surface::makeShared<Acts::StrawSurface>(
149 ActsFrame::transformToActs(toTransform(global)), bounds);
150 surface->assignSurfaceMaterial(material);
151 surface->assignGeometryId(geometryId(detectorId));
152 surfaces.emplace(detectorId, std::move(surface));
153
154 const Double_t* t = global.GetTranslation();
155 const Double_t* r = global.GetRotationMatrix();
156 // The tube axis is the local z axis, i.e. the third column
157 wires.emplace(
158 detectorId,
159 StrawWire{detectorId, detectorId / 1000000, detectorId / 100000 % 10,
160 detectorId / 10000 % 10, t[0], t[1], t[2], r[2], r[5], r[8],
161 tube->GetRmin(), tube->GetDZ()});
162 }
163};
164
165} // namespace
166
167ActsStrawGeometry::ActsStrawGeometry(const TGeoManager& geometry) {
168 const TGeoVolume* top = geometry.GetTopVolume();
169 if (top == nullptr) {
170 throw std::invalid_argument("ActsStrawGeometry: no geometry loaded");
171 }
172 Builder builder{m_surfaces, m_wires, nullptr};
173 builder.visit(*top, TGeoHMatrix());
174 if (m_surfaces.empty()) {
175 throw std::runtime_error("ActsStrawGeometry: no straws in the geometry");
176 }
177}
178
179const Acts::Surface* ActsStrawGeometry::surface(int detectorId) const {
180 const auto it = m_surfaces.find(detectorId);
181 return it == m_surfaces.end() ? nullptr : it->second.get();
182}
183
184const StrawWire* ActsStrawGeometry::wire(int detectorId) const {
185 const auto it = m_wires.find(detectorId);
186 return it == m_wires.end() ? nullptr : &it->second;
187}
188
189} // namespace SHiP
ActsStrawGeometry(const TGeoManager &geometry)
const StrawWire * wire(int detectorId) const
The wire for a strawtubes detector ID, or nullptr if unknown.
std::unordered_map< int, std::shared_ptr< Acts::Surface > > m_surfaces
std::unordered_map< int, StrawWire > m_wires
const Acts::Surface * surface(int detectorId) const
The straw surface for a strawtubes detector ID, or nullptr if unknown.
Acts::Transform3 transformToActs(const Acts::Transform3 &shipTransform)
Transform of a TGeo placement (SHiP frame, cm) in the ACTS frame.
Definition ActsFrame.hpp:56
constexpr double kShipLength
Length unit of the SHiP frame, in ACTS units.
Definition ActsFrame.hpp:36
int i
Definition ShipAna.py:108
A straw wire in the SHiP frame (cm), for pattern recognition.