Garfield++ v1r0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
ViewGeometry.cc
Go to the documentation of this file.
1#include <iostream>
2#include <cmath>
3
4#include "GeometrySimple.hh"
5#include "Solid.hh"
6#include "Plotting.hh"
9#include "ViewGeometry.hh"
10
11namespace Garfield {
12
14 : m_className("ViewGeometry"),
15 m_debug(false),
16 m_canvas(NULL),
17 m_hasExternalCanvas(false),
18 m_geometry(NULL),
19 m_geoManager(NULL) {
20
22}
23
25
26 if (!m_hasExternalCanvas && m_canvas != NULL) delete m_canvas;
27 Reset();
28}
29
31
32 if (geo == NULL) {
33 std::cerr << m_className << "::SetGeometry:\n";
34 std::cerr << " Geometry pointer is null.\n";
35 return;
36 }
37
38 m_geometry = geo;
39}
40
41void ViewGeometry::SetCanvas(TCanvas* c) {
42
43 if (c == NULL) return;
44 if (!m_hasExternalCanvas && m_canvas != NULL) {
45 delete m_canvas;
46 m_canvas = NULL;
47 }
48 m_canvas = c;
49 m_hasExternalCanvas = true;
50}
51
53
54 if (m_geometry == NULL) {
55 std::cerr << m_className << "::Plot:\n";
56 std::cerr << " Geometry is not defined.\n";
57 return;
58 }
59
60 if (m_canvas == NULL) {
61 m_canvas = new TCanvas();
62 m_canvas->SetTitle(m_label.c_str());
63 if (m_hasExternalCanvas) m_hasExternalCanvas = false;
64 }
65 m_canvas->cd();
66
67 const unsigned int nSolids = m_geometry->GetNumberOfSolids();
68 if (nSolids == 0) {
69 std::cerr << m_className << "::Plot:\n";
70 std::cerr << " Geometry is empty.\n";
71 return;
72 }
73
74 // Get the bounding box.
75 double xMin = 0., yMin = 0., zMin = 0.;
76 double xMax = 0., yMax = 0., zMax = 0.;
77 if (!m_geometry->GetBoundingBox(xMin, yMin, zMin, xMax, yMax, zMax)) {
78 std::cerr << m_className << "::Plot:\n";
79 std::cerr << " Cannot retrieve bounding box.\n";
80 return;
81 }
82 m_geoManager = new TGeoManager("ViewGeometryGeoManager", m_label.c_str());
83 TGeoMaterial* matVacuum = new TGeoMaterial("Vacuum", 0., 0., 0.);
84 TGeoMedium* medVacuum = new TGeoMedium("Vacuum", 1, matVacuum);
85 m_media.push_back(medVacuum);
86 // Use silicon as "default" material.
87 TGeoMaterial* matDefault = new TGeoMaterial("Default", 28.085, 14., 2.329);
88 TGeoMedium* medDefault = new TGeoMedium("Default", 1, matDefault);
89 TGeoVolume* world = m_geoManager->MakeBox("World", medVacuum,
90 std::max(fabs(xMin), fabs(xMax)),
91 std::max(fabs(yMin), fabs(yMax)),
92 std::max(fabs(zMin), fabs(zMax)));
93 m_geoManager->SetTopVolume(world);
94 m_volumes.push_back(world);
95
96 for (unsigned int i = 0; i < nSolids; ++i) {
97 Solid* solid = m_geometry->GetSolid(i);
98 if (solid == NULL) {
99 std::cerr << m_className << "::Plot:\n";
100 std::cerr << " Could not get solid " << i << " from geometry.\n";
101 continue;
102 }
103 // Get the center coordinates.
104 double x0 = 0., y0 = 0., z0 = 0.;
105 if (!solid->GetCenter(x0, y0, z0)) {
106 std::cerr << m_className << "::Plot:\n";
107 std::cerr << " Could not determine solid center.\n";
108 continue;
109 }
110 // Get the rotation.
111 double ctheta = 1., stheta = 0.;
112 double cphi = 1., sphi = 0.;
113 if (!solid->GetOrientation(ctheta, stheta, cphi, sphi)) {
114 std::cerr << m_className << "::Plot:\n";
115 std::cerr << " Could not determine solid orientation.\n";
116 continue;
117 }
118 double matrix[9] = {cphi * ctheta, -sphi, cphi * stheta,
119 sphi * ctheta, cphi, sphi * stheta,
120 -stheta, 0, ctheta};
121 TGeoVolume* volume = NULL;
122 if (solid->IsTube()) {
123 double rmin = 0., rmax = 0., lz = 0.;
124 if (!solid->GetDimensions(rmin, rmax, lz)) {
125 std::cerr << m_className << "::Plot:\n";
126 std::cerr << " Could not determine tube dimensions.\n";
127 continue;
128 }
129 volume = m_geoManager->MakeTube("Tube", medDefault, rmin, rmax, lz);
130 } else if (solid->IsBox()) {
131 double dx = 0., dy = 0., dz = 0.;
132 if (!solid->GetDimensions(dx, dy, dz)) {
133 std::cerr << m_className << "::Plot:\n";
134 std::cerr << " Could not determine box dimensions.\n";
135 continue;
136 }
137 volume = m_geoManager->MakeBox("Box", medDefault, dx, dy, dz);
138 } else if (solid->IsSphere()) {
139 double rmin = 0., rmax = 0., dummy = 0.;
140 if (!solid->GetDimensions(rmin, rmax, dummy)) {
141 std::cerr << m_className << "::Plot:\n";
142 std::cerr << " Could not determine sphere dimensions.\n";
143 continue;
144 }
145 volume = m_geoManager->MakeSphere("Sphere", medDefault, rmin, rmax);
146 } else {
147 std::cerr << m_className << "::Plot:\n";
148 std::cerr << " Unknown solid type.\n";
149 continue;
150 }
151 Medium* medium = m_geometry->GetMedium(x0, y0, z0);
152 if (medium == NULL) {
153 volume->SetLineColor(kGreen + 2);
154 volume->SetTransparency(50);
155 } else if (medium->IsGas()) {
156 volume->SetLineColor(kBlue + medium->GetId());
157 volume->SetTransparency(50);
158 } else if (medium->IsSemiconductor()) {
159 volume->SetLineColor(kRed + medium->GetId());
160 volume->SetTransparency(50);
161 } else {
162 volume->SetLineColor(kViolet + medium->GetId());
163 volume->SetTransparency(0);
164 }
165 TGeoRotation r;
166 r.SetMatrix(matrix);
167 TGeoTranslation t(x0, y0, z0);
168 TGeoCombiTrans* transform = new TGeoCombiTrans(t, r);
169 m_volumes.push_back(volume);
170 m_geoManager->GetTopVolume()->AddNode(volume, 1, transform);
171 }
172 m_geoManager->CloseGeometry();
173 m_geoManager->GetTopNode()->Draw("ogl");
174
175}
176
177void ViewGeometry::Reset() {
178
179 for (std::vector<TGeoVolume*>::iterator it = m_volumes.begin();
180 it != m_volumes.end(); ++it) {
181 if (*it) {
182 TGeoShape* shape = (*it)->GetShape();
183 if (shape) delete shape;
184 delete *it;
185 }
186 }
187 m_volumes.clear();
188 for (std::vector<TGeoMedium*>::iterator it = m_media.begin();
189 it != m_media.end(); ++it) {
190 if (*it) {
191 TGeoMaterial* material = (*it)->GetMaterial();
192 if (material) delete material;
193 delete *it;
194 }
195 }
196 m_media.clear();
197
198 if (m_geoManager) {
199 delete m_geoManager;
200 m_geoManager = NULL;
201 }
202
203}
204
205}
DoubleAc fabs(const DoubleAc &f)
Definition: DoubleAc.h:616
unsigned int GetNumberOfSolids() const
Medium * GetMedium(const double x, const double y, const double z) const
Solid * GetSolid(const double x, const double y, const double z) const
bool GetBoundingBox(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax)
int GetId() const
Definition: Medium.hh:20
virtual bool IsSemiconductor() const
Definition: Medium.hh:24
virtual bool IsGas() const
Definition: Medium.hh:23
virtual bool IsTube() const
Definition: Solid.hh:22
virtual bool IsBox() const
Definition: Solid.hh:21
virtual bool GetOrientation(double &ctheta, double &stheta, double &cphi, double &shpi) const =0
virtual bool GetCenter(double &x, double &y, double &z) const =0
virtual bool GetDimensions(double &l1, double &l2, double &l3) const =0
virtual bool IsSphere() const
Definition: Solid.hh:23
void SetCanvas(TCanvas *c)
Definition: ViewGeometry.cc:41
void SetGeometry(GeometrySimple *geo)
Definition: ViewGeometry.cc:30
PlottingEngineRoot plottingEngine