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"
21#include "TGeoManager.h"
22#include "TGeoMaterial.h"
23#include "TGeoMatrix.h"
26#include "TGeoVolume.h"
32namespace u = Acts::UnitConstants;
36constexpr int kGasCopyOffset = 1000;
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) {
56std::shared_ptr<const Acts::HomogeneousSurfaceMaterial> strawMaterial(
57 const TGeoVolume& volume) {
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") {
66 throw std::runtime_error(std::string(
"ActsStrawGeometry: no straw ") +
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");
76 diameter = 2. * tube->GetRmax();
78 const TGeoMaterial*
material = node->GetVolume()->GetMaterial();
79 const double area = M_PI * (tube->GetRmax() * tube->GetRmax() -
80 tube->GetRmin() * tube->GetRmin());
82 Acts::Material::fromMassDensity(
88 return std::make_shared<Acts::HomogeneousSurfaceMaterial>(
89 Acts::MaterialSlab::combineLayers(slabs));
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]);
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()
111 .withLayer(8 * station + 2 * view + layer)
112 .withSensitive(straw);
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;
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) {
129 material = strawMaterial(volume);
131 addStraw(*node, global);
133 visit(*daughter, global);
138 void addStraw(
const TGeoNode& node,
const TGeoHMatrix& global) {
140 dynamic_cast<const TGeoTube*
>(node.GetVolume()->GetShape());
141 if (tube ==
nullptr) {
142 throw std::runtime_error(
"ActsStrawGeometry: straw is not a TGeoTube");
144 const int detectorId = node.GetNumber() + kGasCopyOffset;
145 auto bounds = std::make_shared<const Acts::LineBounds>(
148 auto surface = Acts::Surface::makeShared<Acts::StrawSurface>(
150 surface->assignSurfaceMaterial(material);
151 surface->assignGeometryId(geometryId(detectorId));
152 surfaces.emplace(detectorId, std::move(surface));
154 const Double_t*
t = global.GetTranslation();
155 const Double_t* r = global.GetRotationMatrix();
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()});
168 const TGeoVolume* top = geometry.GetTopVolume();
169 if (top ==
nullptr) {
170 throw std::invalid_argument(
"ActsStrawGeometry: no geometry loaded");
173 builder.visit(*top, TGeoHMatrix());
175 throw std::runtime_error(
"ActsStrawGeometry: no straws in the geometry");
181 return it ==
m_surfaces.end() ? nullptr : it->second.get();
185 const auto it =
m_wires.find(detectorId);
186 return it ==
m_wires.end() ? nullptr : &it->second;
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.
constexpr double kShipLength
Length unit of the SHiP frame, in ACTS units.
A straw wire in the SHiP frame (cm), for pattern recognition.