Garfield++ 4.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
GeometryRoot.cc
Go to the documentation of this file.
1#include <cmath>
2#include <iostream>
3
4#include <TGeoBBox.h>
5#include <TGeoNode.h>
6#include <TList.h>
7
9
10namespace Garfield {
11
13
14void GeometryRoot::SetGeometry(TGeoManager* geoman) {
15 if (!geoman) {
16 std::cerr << m_className << "::SetGeometry: Null pointer.\n";
17 return;
18 }
19 m_geoManager = geoman;
20 m_materials.clear();
21}
22
23Medium* GeometryRoot::GetMedium(const double x, const double y, const double z,
24 const bool /*tesselated*/) const {
25 if (!m_geoManager) return nullptr;
26 m_geoManager->SetCurrentPoint(x, y, z);
27 if (m_geoManager->IsOutside()) return nullptr;
28 TGeoNode* cnode = m_geoManager->GetCurrentNode();
29 std::string name(cnode->GetMedium()->GetMaterial()->GetName());
30
31 const auto it = m_materials.find(name);
32 if (it == m_materials.end()) return nullptr;
33 return it->second;
34}
35
37 if (!m_geoManager) {
38 PrintGeoNotDefined("GetNumberOfMaterials");
39 return 0;
40 }
41
42 return m_geoManager->GetListOfMaterials()->GetEntries();
43}
44
45TGeoMaterial* GeometryRoot::GetMaterial(const unsigned int i) {
46 if (!m_geoManager) {
47 PrintGeoNotDefined("GetMaterial");
48 return nullptr;
49 }
50
51 return m_geoManager->GetMaterial(i);
52}
53
54TGeoMaterial* GeometryRoot::GetMaterial(const char* name) {
55 if (!m_geoManager) {
56 PrintGeoNotDefined("GetMaterial");
57 return nullptr;
58 }
59
60 return m_geoManager->GetMaterial(name);
61}
62
63void GeometryRoot::SetMedium(const unsigned int imat, Medium* med) {
64 if (!m_geoManager) {
65 PrintGeoNotDefined("SetMedium");
66 return;
67 }
68
69 if (!med) {
70 std::cerr << m_className << "::SetMedium: Null pointer.\n";
71 return;
72 }
73
74 TGeoMaterial* mat = m_geoManager->GetMaterial(imat);
75 if (!mat) {
76 std::cerr << m_className << "::SetMedium:\n"
77 << " ROOT material " << imat << " does not exist.\n";
78 return;
79 }
80
81 std::string name(mat->GetName());
82
83 // Check if this material has already been associated with a medium.
84 if (m_materials.count(name) > 0) {
85 std::cout << m_className << "::SetMedium:\n"
86 << " Replacing existing association of material " << name
87 << " with medium " << med->GetName() << ".\n";
88 }
89 m_materials[name] = med;
90
91 // Check if material properties match
92 const double rho1 = mat->GetDensity();
93 const double rho2 = med->GetMassDensity();
94 std::cout << m_className << "::SetMedium:\n"
95 << " ROOT material: " << name << "\n"
96 << " Density: " << rho1 << " g / cm3\n"
97 << " Medium: " << med->GetName() << "\n"
98 << " Density: " << rho2 << " g / cm3\n";
99 if (rho1 > 0 && fabs(rho1 - rho2) / rho1 > 0.01) {
100 std::cout << " WARNING: Densities differ by > 1%.\n";
101 }
102}
103
104void GeometryRoot::SetMedium(const char* name, Medium* med) {
105 if (!m_geoManager) {
106 PrintGeoNotDefined("SetMedium");
107 return;
108 }
109
110 if (!med) {
111 std::cerr << m_className << "::SetMedium: Null pointer.\n";
112 return;
113 }
114
115 const int imat = m_geoManager->GetMaterialIndex(name);
116 if (imat < 0) {
117 std::cerr << m_className << "::SetMedium:\n"
118 << " ROOT material " << name << " does not exist.\n";
119 return;
120 }
121
122 SetMedium(imat, med);
123}
124
125bool GeometryRoot::IsInside(const double x, const double y, const double z,
126 const bool /*tesselated*/) const {
127 if (m_geoManager) {
128 m_geoManager->SetCurrentPoint(x, y, z);
129 return !m_geoManager->IsOutside();
130 }
131 return false;
132}
133
134bool GeometryRoot::GetBoundingBox(double& xmin, double& ymin, double& zmin,
135 double& xmax, double& ymax, double& zmax) {
136 if (!m_geoManager) return false;
137 auto top = m_geoManager->GetTopVolume();
138 if (!top) return false;
139 if (!top->GetShape()) return false;
140 TGeoBBox* box = (TGeoBBox*)m_geoManager->GetTopVolume()->GetShape();
141 if (!box) return false;
142 const double dx = box->GetDX();
143 const double dy = box->GetDY();
144 const double dz = box->GetDZ();
145 const double ox = box->GetOrigin()[0];
146 const double oy = box->GetOrigin()[1];
147 const double oz = box->GetOrigin()[2];
148 xmin = ox - dx;
149 xmax = ox + dx;
150 ymin = oy - dy;
151 ymax = oy + dy;
152 zmin = oz - dz;
153 zmax = oz + dz;
154 return true;
155}
156
157void GeometryRoot::PrintGeoNotDefined(const std::string& fcn) const {
158
159 std::cerr << m_className + "::" + fcn << ":\n"
160 << " ROOT geometry is not defined. Call SetGeometry first.\n";
161}
162
163}
TGeoManager * m_geoManager
Definition: GeometryRoot.hh:49
unsigned int GetNumberOfMaterials()
Get the number of materials defined in the ROOT geometry.
Definition: GeometryRoot.cc:36
GeometryRoot()
Constructor.
Definition: GeometryRoot.cc:12
bool IsInside(const double x, const double y, const double z, const bool tesselated=false) const override
Check if a point is inside the geometry.
void SetMedium(const unsigned int imat, Medium *med)
Associate a ROOT material with a Garfield medium.
Definition: GeometryRoot.cc:63
void PrintGeoNotDefined(const std::string &fcn) const
std::map< std::string, Medium * > m_materials
Definition: GeometryRoot.hh:52
void SetGeometry(TGeoManager *geoman)
Set the geometry (pointer to ROOT TGeoManager).
Definition: GeometryRoot.cc:14
TGeoMaterial * GetMaterial(const unsigned int i)
Get a pointer to the ROOT material with a given index.
Definition: GeometryRoot.cc:45
bool GetBoundingBox(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax) override
Get the bounding box (envelope of the geometry).
Medium * GetMedium(const double x, const double y, const double z, const bool tesselated=false) const override
Retrieve the medium at a given point.
Definition: GeometryRoot.cc:23
Abstract base class for geometry classes.
Definition: Geometry.hh:13
std::string m_className
Definition: Geometry.hh:44
Abstract base class for media.
Definition: Medium.hh:13
virtual double GetMassDensity() const
Get the mass density [g/cm3].
Definition: Medium.cc:101
const std::string & GetName() const
Get the medium name/identifier.
Definition: Medium.hh:23