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