Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-05 08:18:37

0001 # This file is part of the ACTS project.
0002 #
0003 # Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 #
0005 # This Source Code Form is subject to the terms of the Mozilla Public
0006 # License, v. 2.0. If a copy of the MPL was not distributed with this
0007 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 import gc
0010 import os
0011 import weakref
0012 from xml.etree import ElementTree
0013 
0014 import pytest
0015 
0016 import detray.core
0017 import detray.io
0018 import detray.svgtools
0019 
0020 # Every draw method is bound for every view, so covering all of the views in a
0021 # single draw method is enough.
0022 VIEWS = [
0023     (detray.svgtools.ViewXY, "xy"),
0024     (detray.svgtools.ViewZR, "zr"),
0025     (detray.svgtools.ViewZPhi, "zphi"),
0026     (detray.svgtools.ViewZRPhi, "zrphi"),
0027 ]
0028 
0029 
0030 @pytest.fixture
0031 def illustrator(detector):
0032     det, names = detector
0033     return detray.svgtools.Illustrator(
0034         det, names, detray.svgtools.tableauColorblindStyle()
0035     )
0036 
0037 
0038 def test_write_svg(tmp_path):
0039     axes = detray.svgtools.drawAxes("axes", [-1.0, 1.0], [-1.0, 1.0], "x", "y", 12)
0040     assert axes.id == "axes"
0041 
0042     path = os.path.join(str(tmp_path), axes.id)
0043     detray.svgtools.writeSvg(path, [axes])
0044 
0045     ElementTree.parse(path + ".svg")
0046 
0047 
0048 def test_style_properties():
0049     style = detray.svgtools.tableauColorblindStyle()
0050     style.fontSize = 20
0051     style.materialGradientPos = [800.0, -200.0]
0052 
0053     assert style.fontSize == 20
0054     assert style.materialGradientPos == pytest.approx([800.0, -200.0], rel=1e-6)
0055 
0056 
0057 @pytest.mark.parametrize("view, suffix", VIEWS)
0058 def test_draw_detector(illustrator, view, suffix):
0059     svg = illustrator.drawDetector(view(), detray.core.GeometryContext())
0060 
0061     assert svg.id == f"toy_detector_{suffix}"
0062 
0063 
0064 def test_draw_volumes_by_index(illustrator):
0065     svg, grids = illustrator.drawVolumes(
0066         [0, 1], detray.svgtools.ViewXY(), detray.core.GeometryContext()
0067     )
0068 
0069     assert svg.id == "toy_detector_volumes_xy"
0070     # One entry per volume, empty for a volume that has no grid in this view.
0071     assert len(grids) == 2
0072 
0073 
0074 def test_draw_volumes_by_name(illustrator, detector):
0075     _, names = detector
0076     svg, _ = illustrator.drawVolumes(
0077         [names[0], names[1]], detray.svgtools.ViewXY(), detray.core.GeometryContext()
0078     )
0079 
0080     assert svg.id == "toy_detector_volumes_xy"
0081 
0082 
0083 def test_draw_surfaces(illustrator):
0084     svg, material = illustrator.drawSurfaces(
0085         [0, 1], detray.svgtools.ViewXY(), detray.core.GeometryContext()
0086     )
0087 
0088     assert svg.id == "toy_detector_surfaces_xy"
0089     assert material.id == "toy_detector_material_xy"
0090 
0091 
0092 def test_illustrator_keeps_detector_and_names_alive(toy_detector_files):
0093     reader_config = detray.io.DetectorReaderConfig().addFile(
0094         toy_detector_files["geometry"]
0095     )
0096     memory_resource = detray.core.HostMemoryResource()
0097     det, names = detray.io.readDetector(memory_resource, reader_config)
0098 
0099     mr_ref = weakref.ref(memory_resource)
0100     det_ref = weakref.ref(det)
0101     names_ref = weakref.ref(names)
0102 
0103     il = detray.svgtools.Illustrator(
0104         det, names, detray.svgtools.tableauColorblindStyle()
0105     )
0106 
0107     del memory_resource, det, names
0108     gc.collect()
0109 
0110     assert mr_ref() is not None
0111     assert det_ref() is not None
0112     assert names_ref() is not None
0113     assert il.detName == "toy_detector"
0114 
0115     del il
0116     gc.collect()
0117 
0118     assert mr_ref() is None
0119     assert det_ref() is None
0120     assert names_ref() is None