Garfield++ v2r0
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 <iostream>
2#include <cmath>
3
4#include <TGeoNode.h>
5#include <TGeoBBox.h>
6#include <TList.h>
7
8#include "GeometryRoot.hh"
9
10namespace Garfield {
11
13 : m_geoManager(NULL), m_debug(false) {
14
15 m_className = "GeometryRoot";
16}
17
18void GeometryRoot::SetGeometry(TGeoManager* geoman) {
19
20 if (!geoman) {
21 std::cerr << m_className << "::SetGeometry:\n Null pointer.";
22 return;
23 }
24
25 m_geoManager = geoman;
26 m_materials.clear();
27}
28
29Medium* GeometryRoot::GetMedium(const double x, const double y,
30 const double z) const {
31
32 if (!m_geoManager) return NULL;
33 m_geoManager->SetCurrentPoint(x, y, z);
34 if (m_geoManager->IsOutside()) return NULL;
35 TGeoNode* cnode = m_geoManager->GetCurrentNode();
36 std::string name(cnode->GetMedium()->GetMaterial()->GetName());
37
38 const unsigned int nMaterials = m_materials.size();
39 for (unsigned int i = 0; i < nMaterials; ++i) {
40 if (m_materials[i].name == name) {
41 return m_materials[i].medium;
42 }
43 }
44 return NULL;
45}
46
48
49 if (!m_geoManager) {
50 std::cerr << "GeometryRoot::GetNumberOfMaterials:\n";
51 std::cerr << " ROOT geometry is not defined.\n";
52 std::cerr << " Call SetGeometry first.\n";
53 return 0;
54 }
55
56 return m_geoManager->GetListOfMaterials()->GetEntries();
57}
58
59TGeoMaterial* GeometryRoot::GetMaterial(const unsigned int i) {
60
61 if (!m_geoManager) {
62 std::cerr << "GeometryRoot::GetMaterial:\n";
63 std::cerr << " ROOT geometry is not defined.\n";
64 std::cerr << " Call SetGeometry first.\n";
65 return NULL;
66 }
67
68 return m_geoManager->GetMaterial(i);
69}
70
71TGeoMaterial* GeometryRoot::GetMaterial(const char* name) {
72
73 if (!m_geoManager) {
74 std::cerr << "GeometryRoot::GetMaterial:\n";
75 std::cerr << " ROOT geometry is not defined.\n";
76 std::cerr << " Call SetGeometry first.\n";
77 return NULL;
78 }
79
80 return m_geoManager->GetMaterial(name);
81}
82
83void GeometryRoot::SetMedium(const unsigned int imat, Medium* med) {
84
85 if (!m_geoManager) {
86 std::cerr << "GeometryRoot::SetMedium:\n";
87 std::cerr << " ROOT geometry is not defined.\n";
88 std::cerr << " Call SetGeometry first.\n";
89 return;
90 }
91
92 if (!med) {
93 std::cerr << "GeometryRoot::SetMedium:\n";
94 std::cerr << " Medium pointer is null.\n";
95 return;
96 }
97
98 TGeoMaterial* mat = m_geoManager->GetMaterial(imat);
99 if (!mat) {
100 std::cerr << "GeometryRoot::SetMedium:\n";
101 std::cerr << " ROOT material with index " << imat
102 << " does not exist.\n";
103 return;
104 }
105
106 std::string name(mat->GetName());
107 bool isNew = true;
108 // Check if this material has already been associated with a medium
109 const unsigned int nMaterials = m_materials.size();
110 for (unsigned int i = 0; i < nMaterials; ++i) {
111 if (name != m_materials[i].name) continue;
112 std::cout << "GeometryRoot::SetMedium:\n";
113 std::cout << " Current association of material " << name
114 << " with medium " << med->GetName() << " is overwritten.\n";
115 m_materials[i].medium = med;
116 isNew = false;
117 break;
118 }
119
120 if (isNew) {
121 material newMaterial;
122 newMaterial.name = name;
123 newMaterial.medium = med;
124 m_materials.push_back(newMaterial);
125 }
126
127 // Check if material properties match
128 const double rho1 = mat->GetDensity();
129 const double rho2 = med->GetMassDensity();
130 std::cout << "GeometryROOT::SetMedium:\n";
131 std::cout << " ROOT material: " << name << "\n";
132 std::cout << " Density: " << rho1 << " g / cm3\n";
133 std::cout << " Medium: " << med->GetName() << "\n";
134 std::cout << " Density: " << rho2 << " g / cm3\n";
135 if (rho1 > 0 && fabs(rho1 - rho2) / rho1 > 0.01) {
136 std::cout << " WARNING: Densities differ by > 1%.\n";
137 }
138}
139
140void GeometryRoot::SetMedium(const char* name, Medium* med) {
141
142 if (!m_geoManager) {
143 std::cerr << "GeometryRoot::SetMedium:\n";
144 std::cerr << " ROOT geometry is not defined.\n";
145 std::cerr << " Call SetGeometry first.\n";
146 return;
147 }
148
149 if (!med) {
150 std::cerr << "GeometryRoot::SetMedium:\n";
151 std::cerr << " Medium pointer is null.\n";
152 return;
153 }
154
155 const int imat = m_geoManager->GetMaterialIndex(name);
156 if (imat < 0) {
157 std::cerr << "GeometryRoot::SetMedium:" << std::endl;
158 std::cerr << " ROOT material " << name << " does not exist."
159 << std::endl;
160 return;
161 }
162
163 SetMedium(imat, med);
164}
165
166bool GeometryRoot::GetBoundingBox(double& xmin, double& ymin, double& zmin,
167 double& xmax, double& ymax, double& zmax) {
168
169 if (!m_geoManager) return false;
170 TGeoBBox* box = (TGeoBBox*)m_geoManager->GetTopVolume()->GetShape();
171 const double dx = box->GetDX();
172 const double dy = box->GetDY();
173 const double dz = box->GetDZ();
174 const double ox = box->GetOrigin()[0];
175 const double oy = box->GetOrigin()[1];
176 const double oz = box->GetOrigin()[2];
177 xmin = ox - dx;
178 xmax = ox + dx;
179 ymin = oy - dy;
180 ymax = oy + dy;
181 zmin = oz - dz;
182 zmax = oz + dz;
183 return true;
184}
185}
TGeoManager * m_geoManager
Definition: GeometryRoot.hh:59
unsigned int GetNumberOfMaterials()
Get the number of materials defined in the ROOT geometry.
Definition: GeometryRoot.cc:47
std::vector< material > m_materials
Definition: GeometryRoot.hh:66
GeometryRoot()
Constructor.
Definition: GeometryRoot.cc:12
void SetMedium(const unsigned int imat, Medium *med)
Definition: GeometryRoot.cc:83
bool GetBoundingBox(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax)
void SetGeometry(TGeoManager *geoman)
Set the geometry (pointer to ROOT TGeoManager).
Definition: GeometryRoot.cc:18
TGeoMaterial * GetMaterial(const unsigned int i)
Get pointer to ROOT material with given index.
Definition: GeometryRoot.cc:59
Medium * GetMedium(const double x, const double y, const double z) const
Definition: GeometryRoot.cc:29
Abstract base class for media.
Definition: Medium.hh:11
virtual double GetMassDensity() const
Definition: Medium.cc:137
const std::string & GetName() const
Definition: Medium.hh:22