EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TGeoTubeConversionTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TGeoTubeConversionTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
11 
22 
23 #include "TGeoManager.h"
24 #include "TGeoMaterial.h"
25 #include "TGeoMatrix.h"
26 #include "TGeoMedium.h"
27 #include "TGeoTube.h"
28 #include "TGeoVolume.h"
29 #include "TView.h"
30 
31 namespace Acts {
32 
33 namespace Test {
34 
36 
37 ViewConfig red({200, 0, 0});
38 ViewConfig green({0, 200, 0});
39 ViewConfig blue({0, 0, 200});
40 
41 std::vector<std::string> allowedAxes = {"XY*", "Xy*", "xy*", "xY*",
42  "YX*", "yx*", "yX*", "Yx*"};
43 
44 std::vector<std::string> notAllowedAxes = {"YZ*", "ZX*", "ZY*"};
45 
49 BOOST_AUTO_TEST_CASE(TGeoTube_to_CylinderSurface) {
50  ObjVisualization3D objVis;
51 
52  double rmin = 10.;
53  double rmax = 11;
54  double hz = 40.;
55  double phimin = -45.;
56  double phimax = 45.;
57 
58  new TGeoManager("trd1", "poza9");
59  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
60  TGeoMedium *med = new TGeoMedium("MED", 1, mat);
61  TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 100, 100, 100);
62  gGeoManager->SetTopVolume(top);
63  TGeoVolume *vol = gGeoManager->MakeTube("Tube", med, rmin, rmax, hz);
64  TGeoVolume *vols =
65  gGeoManager->MakeTubs("Tube", med, rmin, rmax, hz, phimin, phimax);
66  gGeoManager->CloseGeometry();
67 
68  size_t icyl = 0;
69  for (const auto &axes : allowedAxes) {
70  auto cylinder = TGeoSurfaceConverter::toSurface(*vol->GetShape(),
71  *gGeoIdentity, axes, 1);
72  BOOST_CHECK_NE(cylinder, nullptr);
73  BOOST_CHECK_EQUAL(cylinder->type(), Surface::Cylinder);
74 
75  auto bounds = dynamic_cast<const CylinderBounds *>(&(cylinder->bounds()));
76  BOOST_CHECK_NE(bounds, nullptr);
77  double bR = bounds->get(CylinderBounds::eR);
78  double bhZ = bounds->get(CylinderBounds::eHalfLengthZ);
79 
80  CHECK_CLOSE_ABS(bR, 10.5, s_epsilon);
81  CHECK_CLOSE_ABS(bhZ, hz, s_epsilon);
82 
83  auto transform = cylinder->transform(tgContext);
84  auto rotation = transform.rotation();
85 
86  // Check if the surface is the (negative) identity
87  GeometryView3D::drawSurface(objVis, *cylinder, tgContext);
88  const Vector3D center = cylinder->center(tgContext);
90  objVis, center, center + 1.2 * bR * rotation.col(0), 4., 2.5, red);
92  objVis, center, center + 1.2 * bR * rotation.col(1), 4., 2.5, green);
94  objVis, center, center + 1.2 * bhZ * rotation.col(2), 4., 2.5, blue);
95 
96  objVis.write("TGeoConversion_TGeoTube_CylinderSurface_" +
97  std::to_string(icyl));
98  objVis.clear();
99 
100  if (icyl < 2) {
101  auto cylinderSegment = TGeoSurfaceConverter::toSurface(
102  *vols->GetShape(), *gGeoIdentity, axes, 1);
103  BOOST_CHECK_NE(cylinderSegment, nullptr);
104  BOOST_CHECK_EQUAL(cylinderSegment->type(), Surface::Cylinder);
105 
106  auto boundsSegment =
107  dynamic_cast<const CylinderBounds *>(&(cylinderSegment->bounds()));
108  BOOST_CHECK_NE(boundsSegment, nullptr);
109  bR = boundsSegment->get(CylinderBounds::eR);
110  bhZ = boundsSegment->get(CylinderBounds::eHalfLengthZ);
111  double hphi = boundsSegment->get(CylinderBounds::eHalfPhiSector);
112  double mphi = boundsSegment->get(CylinderBounds::eAveragePhi);
113  CHECK_CLOSE_ABS(bR, 10.5, s_epsilon);
114  CHECK_CLOSE_ABS(bhZ, hz, s_epsilon);
115  CHECK_CLOSE_ABS(hphi, 0.25 * M_PI, s_epsilon);
116  CHECK_CLOSE_ABS(mphi, 0., s_epsilon);
117  GeometryView3D::drawSurface(objVis, *cylinderSegment, tgContext);
119  objVis, center, center + 1.2 * bR * rotation.col(0), 4., 2.5, red);
121  objVis, center, center + 1.2 * bR * rotation.col(1), 4., 2.5, green);
123  objVis, center, center + 1.2 * bhZ * rotation.col(2), 4., 2.5, blue);
124  objVis.write("TGeoConversion_TGeoTube_CylinderSegmentSurface_" +
125  std::to_string(icyl));
126  objVis.clear();
127  } else {
128  BOOST_CHECK_THROW(TGeoSurfaceConverter::toSurface(*vols->GetShape(),
129  *gGeoIdentity, axes, 1),
130  std::invalid_argument);
131  }
132  ++icyl;
133  }
134 
135  // Check exceptions for not allowed axis definition
136  for (const auto &naxes : notAllowedAxes) {
137  BOOST_CHECK_THROW(TGeoSurfaceConverter::toSurface(*vol->GetShape(),
138  *gGeoIdentity, naxes, 1),
139  std::invalid_argument);
140  }
141 }
142 
146 BOOST_AUTO_TEST_CASE(TGeoTube_to_DiscSurface) {
147  ObjVisualization3D objVis;
148 
149  double rmin = 5.;
150  double rmax = 25;
151  double hz = 2.;
152  double phimin = -45.;
153  double phimax = 45.;
154 
155  new TGeoManager("trd1", "poza9");
156  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
157  TGeoMedium *med = new TGeoMedium("MED", 1, mat);
158  TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 100, 100, 100);
159  gGeoManager->SetTopVolume(top);
160  TGeoVolume *vol = gGeoManager->MakeTube("Tube", med, rmin, rmax, hz);
161  vol->SetLineWidth(2);
162  TGeoVolume *vols =
163  gGeoManager->MakeTubs("Tube", med, rmin, rmax, hz, phimin, phimax);
164  gGeoManager->CloseGeometry();
165 
166  size_t idisc = 0;
167  for (const auto &axes : allowedAxes) {
168  auto disc = TGeoSurfaceConverter::toSurface(*vol->GetShape(), *gGeoIdentity,
169  axes, 1);
170  BOOST_CHECK_NE(disc, nullptr);
171  BOOST_CHECK_EQUAL(disc->type(), Surface::Disc);
172 
173  auto bounds = dynamic_cast<const RadialBounds *>(&(disc->bounds()));
174  BOOST_CHECK_NE(bounds, nullptr);
175  double bminr = bounds->get(RadialBounds::eMinR);
176  double bmaxr = bounds->get(RadialBounds::eMaxR);
177 
178  CHECK_CLOSE_ABS(bminr, rmin, s_epsilon);
179  CHECK_CLOSE_ABS(bmaxr, rmax, s_epsilon);
180 
181  // Check if the surface is the (negative) identity
182  GeometryView3D::drawSurface(objVis, *disc, tgContext);
183  const Vector3D center = disc->center(tgContext);
185  objVis, center, center + 1.2 * rmax * Vector3D::UnitX(), 4., 2.5, red);
186  GeometryView3D::drawArrowForward(objVis, center,
187  center + 1.2 * rmax * Vector3D::UnitY(),
188  4., 2.5, green);
190  objVis, center, center + 1.2 * hz * Vector3D::UnitZ(), 4., 2.5, blue);
191  objVis.write("TGeoConversion_TGeoTube_DiscSurface_" +
192  std::to_string(idisc));
193  objVis.clear();
194 
195  if (idisc < 2) {
196  auto discSegment = TGeoSurfaceConverter::toSurface(
197  *vols->GetShape(), *gGeoIdentity, axes, 1);
198  BOOST_CHECK_NE(discSegment, nullptr);
199  BOOST_CHECK_EQUAL(discSegment->type(), Surface::Disc);
200 
201  auto boundsSegment =
202  dynamic_cast<const RadialBounds *>(&(discSegment->bounds()));
203  BOOST_CHECK_NE(boundsSegment, nullptr);
204  bminr = boundsSegment->get(RadialBounds::eMinR);
205  bmaxr = boundsSegment->get(RadialBounds::eMaxR);
206  double hphi = boundsSegment->get(RadialBounds::eHalfPhiSector);
207  double mphi = boundsSegment->get(RadialBounds::eAveragePhi);
208  CHECK_CLOSE_ABS(bminr, rmin, s_epsilon);
209  CHECK_CLOSE_ABS(bmaxr, rmax, s_epsilon);
210  CHECK_CLOSE_ABS(hphi, 0.25 * M_PI, s_epsilon);
211  CHECK_CLOSE_ABS(mphi, 0., s_epsilon);
212  GeometryView3D::drawSurface(objVis, *discSegment, tgContext);
213  GeometryView3D::drawArrowForward(objVis, center,
214  center + 1.2 * bmaxr * Vector3D::UnitX(),
215  4., 2.5, red);
216  GeometryView3D::drawArrowForward(objVis, center,
217  center + 1.2 * bmaxr * Vector3D::UnitY(),
218  4., 2.5, green);
220  objVis, center, center + 1.2 * hz * Vector3D::UnitZ(), 4., 2.5, blue);
221  objVis.write("TGeoConversion_TGeoTube_DiscSegmentSurface_" +
222  std::to_string(idisc));
223  objVis.clear();
224 
225  } else {
226  BOOST_CHECK_THROW(TGeoSurfaceConverter::toSurface(*vols->GetShape(),
227  *gGeoIdentity, axes, 1),
228  std::invalid_argument);
229  }
230  ++idisc;
231  }
232 
233  // Check exceptions for not allowed axis definition
234  for (const auto &naxes : notAllowedAxes) {
235  BOOST_CHECK_THROW(TGeoSurfaceConverter::toSurface(*vol->GetShape(),
236  *gGeoIdentity, naxes, 1),
237  std::invalid_argument);
238  }
239 }
240 
241 } // namespace Test
242 
243 } // namespace Acts