Garfield++ v2r0
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) delete m_canvas;
27 Reset();
28}
29
31
32 if (!geo) {
33 std::cerr << m_className << "::SetGeometry: Null pointer.\n";
34 return;
35 }
36
37 m_geometry = geo;
38}
39
40void ViewGeometry::SetCanvas(TCanvas* c) {
41
42 if (!c) return;
43 if (!m_hasExternalCanvas && m_canvas) {
44 delete m_canvas;
45 m_canvas = NULL;
46 }
47 m_canvas = c;
48 m_hasExternalCanvas = true;
49}
50
52
53 if (!m_geometry) {
54 std::cerr << m_className << "::Plot: Geometry is not defined.\n";
55 return;
56 }
57
58 if (!m_canvas) {
59 m_canvas = new TCanvas();
60 if (m_hasExternalCanvas) m_hasExternalCanvas = false;
61 }
62 m_canvas->cd();
63
64 const unsigned int nSolids = m_geometry->GetNumberOfSolids();
65 if (nSolids == 0) {
66 std::cerr << m_className << "::Plot: Geometry is empty.\n";
67 return;
68 }
69
70 // Get the bounding box.
71 double xMin = 0., yMin = 0., zMin = 0.;
72 double xMax = 0., yMax = 0., zMax = 0.;
73 if (!m_geometry->GetBoundingBox(xMin, yMin, zMin, xMax, yMax, zMax)) {
74 std::cerr << m_className << "::Plot: Cannot retrieve bounding box.\n";
75 return;
76 }
77 m_geoManager = new TGeoManager("ViewGeometryGeoManager", "");
78 TGeoMaterial* matVacuum = new TGeoMaterial("Vacuum", 0., 0., 0.);
79 TGeoMedium* medVacuum = new TGeoMedium("Vacuum", 1, matVacuum);
80 m_media.push_back(medVacuum);
81 // Use silicon as "default" material.
82 TGeoMaterial* matDefault = new TGeoMaterial("Default", 28.085, 14., 2.329);
83 TGeoMedium* medDefault = new TGeoMedium("Default", 1, matDefault);
84 TGeoVolume* world = m_geoManager->MakeBox("World", medVacuum,
85 std::max(fabs(xMin), fabs(xMax)),
86 std::max(fabs(yMin), fabs(yMax)),
87 std::max(fabs(zMin), fabs(zMax)));
88 m_geoManager->SetTopVolume(world);
89 m_volumes.push_back(world);
90
91 for (unsigned int i = 0; i < nSolids; ++i) {
92 Solid* solid = m_geometry->GetSolid(i);
93 if (!solid) {
94 std::cerr << m_className << "::Plot:\n"
95 << " Could not get solid " << i << " from geometry.\n";
96 continue;
97 }
98 // Get the center coordinates.
99 double x0 = 0., y0 = 0., z0 = 0.;
100 if (!solid->GetCenter(x0, y0, z0)) {
101 std::cerr << m_className << "::Plot:\n"
102 << " Could not determine solid center.\n";
103 continue;
104 }
105 // Get the rotation.
106 double ctheta = 1., stheta = 0.;
107 double cphi = 1., sphi = 0.;
108 if (!solid->GetOrientation(ctheta, stheta, cphi, sphi)) {
109 std::cerr << m_className << "::Plot:\n"
110 << " Could not determine solid orientation.\n";
111 continue;
112 }
113 double matrix[9] = {cphi * ctheta, -sphi, cphi * stheta,
114 sphi * ctheta, cphi, sphi * stheta,
115 -stheta, 0, ctheta};
116 TGeoVolume* volume = NULL;
117 if (solid->IsTube()) {
118 double rmin = 0., rmax = 0., lz = 0.;
119 if (!solid->GetDimensions(rmin, rmax, lz)) {
120 std::cerr << m_className << "::Plot:\n"
121 << " Could not determine tube dimensions.\n";
122 continue;
123 }
124 volume = m_geoManager->MakeTube("Tube", medDefault, rmin, rmax, lz);
125 } else if (solid->IsBox()) {
126 double dx = 0., dy = 0., dz = 0.;
127 if (!solid->GetDimensions(dx, dy, dz)) {
128 std::cerr << m_className << "::Plot:\n"
129 << " Could not determine box dimensions.\n";
130 continue;
131 }
132 volume = m_geoManager->MakeBox("Box", medDefault, dx, dy, dz);
133 } else if (solid->IsSphere()) {
134 double rmin = 0., rmax = 0., dummy = 0.;
135 if (!solid->GetDimensions(rmin, rmax, dummy)) {
136 std::cerr << m_className << "::Plot:\n"
137 << " Could not determine sphere dimensions.\n";
138 continue;
139 }
140 volume = m_geoManager->MakeSphere("Sphere", medDefault, rmin, rmax);
141 } else {
142 std::cerr << m_className << "::Plot: Unknown type of solid.\n";
143 continue;
144 }
145 Medium* medium = m_geometry->GetMedium(x0, y0, z0);
146 if (!medium) {
147 volume->SetLineColor(kGreen + 2);
148 volume->SetTransparency(50);
149 } else if (medium->IsGas()) {
150 volume->SetLineColor(kBlue + medium->GetId());
151 volume->SetTransparency(50);
152 } else if (medium->IsSemiconductor()) {
153 volume->SetLineColor(kRed + medium->GetId());
154 volume->SetTransparency(50);
155 } else {
156 volume->SetLineColor(kViolet + medium->GetId());
157 volume->SetTransparency(0);
158 }
159 TGeoRotation r;
160 r.SetMatrix(matrix);
161 TGeoTranslation t(x0, y0, z0);
162 TGeoCombiTrans* transform = new TGeoCombiTrans(t, r);
163 m_volumes.push_back(volume);
164 m_geoManager->GetTopVolume()->AddNode(volume, 1, transform);
165 }
166 m_geoManager->CloseGeometry();
167 m_geoManager->GetTopNode()->Draw("ogl");
168
169}
170
171void ViewGeometry::Reset() {
172
173 for (std::vector<TGeoVolume*>::iterator it = m_volumes.begin();
174 it != m_volumes.end(); ++it) {
175 if (*it) {
176 TGeoShape* shape = (*it)->GetShape();
177 if (shape) delete shape;
178 delete *it;
179 }
180 }
181 m_volumes.clear();
182 for (std::vector<TGeoMedium*>::iterator it = m_media.begin();
183 it != m_media.end(); ++it) {
184 if (*it) {
185 TGeoMaterial* material = (*it)->GetMaterial();
186 if (material) delete material;
187 delete *it;
188 }
189 }
190 m_media.clear();
191
192 if (m_geoManager) {
193 delete m_geoManager;
194 m_geoManager = NULL;
195 }
196
197}
198
199}
"Native" geometry, using simple shapes.
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)
Abstract base class for media.
Definition: Medium.hh:11
int GetId() const
Definition: Medium.hh:20
virtual bool IsSemiconductor() const
Definition: Medium.hh:24
virtual bool IsGas() const
Definition: Medium.hh:23
Abstract base class for solids.
Definition: Solid.hh:8
virtual bool IsTube() const
Return true if the solid is a tube.
Definition: Solid.hh:25
virtual bool IsBox() const
Return true if the solid is a box.
Definition: Solid.hh:23
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
Return true if the solid is a sphere.
Definition: Solid.hh:27
void Plot()
Draw the geometry.
Definition: ViewGeometry.cc:51
void SetCanvas(TCanvas *c)
Set the canvas to be painted on.
Definition: ViewGeometry.cc:40
void SetGeometry(GeometrySimple *geo)
Set the geometry to be drawn.
Definition: ViewGeometry.cc:30
~ViewGeometry()
Destructor.
Definition: ViewGeometry.cc:24
ViewGeometry()
Constructor.
Definition: ViewGeometry.cc:13
PlottingEngineRoot plottingEngine