Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-07-16 08:08:19

0001 #!/usr/bin/env python3
0002 
0003 import pytest
0004 import tempfile
0005 import pathlib
0006 import os
0007 
0008 # Test imports to verify availability - make optional dependencies optional
0009 try:
0010     pytest.importorskip("acts.ActsPythonBindingsDD4hep")
0011     HAS_DD4HEP = True
0012 except pytest.skip.Exception:
0013     HAS_DD4HEP = False
0014 
0015 try:
0016     pytest.importorskip("acts.ActsPythonBindingsGeoModel")
0017     HAS_GEOMODEL = True
0018 except pytest.skip.Exception:
0019     HAS_GEOMODEL = False
0020 
0021 import acts
0022 import acts.examples
0023 
0024 # Optional imports
0025 if HAS_DD4HEP:
0026     try:
0027         import acts.examples.dd4hep
0028         import acts.examples.geant4
0029         import acts.examples.geant4.dd4hep
0030     except ImportError:
0031         pass
0032 
0033 if HAS_GEOMODEL:
0034     try:
0035         import acts.examples.geomodel
0036     except ImportError:
0037         pass
0038 
0039 try:
0040     import acts.acts_toroidal_field
0041 
0042     TOROIDAL_FIELD_AVAILABLE = True
0043 except ImportError:
0044     TOROIDAL_FIELD_AVAILABLE = False
0045 
0046 u = acts.UnitConstants
0047 
0048 
0049 @pytest.mark.skipif(not TOROIDAL_FIELD_AVAILABLE, reason="ToroidalField not available")
0050 def test_toroidal_field_basic():
0051     """Test basic ToroidalField functionality."""
0052 
0053     # Test default configuration
0054     config = acts.acts_toroidal_field.Config()
0055     field = acts.acts_toroidal_field.ToroidalField(config)
0056     assert field is not None
0057 
0058     # Test field at origin
0059     ctx = acts.MagneticFieldContext()
0060     cache = field.makeCache(ctx)
0061 
0062     # Test field calculation at a point in barrel region
0063     position = acts.Vector3(6000.0, 0.0, 0.0)  # 6m radius
0064     field_value = field.getField(position, cache)
0065 
0066     # Should have non-zero field components
0067     assert (
0068         abs(field_value[0]) > 0.0
0069         or abs(field_value[1]) > 0.0
0070         or abs(field_value[2]) > 0.0
0071     )
0072 
0073 
0074 @pytest.mark.skipif(not TOROIDAL_FIELD_AVAILABLE, reason="ToroidalField not available")
0075 def test_toroidal_field_custom():
0076     """Test ToroidalField with custom parameters."""
0077 
0078     config = acts.acts_toroidal_field.Config()
0079 
0080     # Customize barrel configuration
0081     config.barrel.R_in = 5.0
0082     config.barrel.R_out = 9.0
0083     config.barrel.I = 15000.0
0084 
0085     field = acts.acts_toroidal_field.ToroidalField(config)
0086     assert field is not None
0087 
0088     # Test field calculation
0089     ctx = acts.MagneticFieldContext()
0090     cache = field.makeCache(ctx)
0091 
0092     position = acts.Vector3(7000.0, 0.0, 0.0)
0093     field_value = field.getField(position, cache)
0094 
0095     # Verify field is calculated (should be a Vector3 with components)
0096     assert hasattr(field_value, "__getitem__")  # Can access components
0097     assert field_value[0] is not None
0098     assert field_value[1] is not None
0099     assert field_value[2] is not None
0100 
0101 
0102 @pytest.mark.skipif(not TOROIDAL_FIELD_AVAILABLE, reason="ToroidalField not available")
0103 def test_toroidal_field_symmetry():
0104     """Test that the field has expected symmetries."""
0105 
0106     config = acts.acts_toroidal_field.Config()
0107     field = acts.acts_toroidal_field.ToroidalField(config)
0108     ctx = acts.MagneticFieldContext()
0109     cache = field.makeCache(ctx)
0110 
0111     # Test azimuthal symmetry - field should be similar at same radius
0112     radius = 7000.0
0113     z = 1000.0
0114 
0115     pos1 = acts.Vector3(radius, 0.0, z)
0116     pos2 = acts.Vector3(0.0, radius, z)
0117 
0118     field1 = field.getField(pos1, cache)
0119     field2 = field.getField(pos2, cache)
0120 
0121     # Due to toroidal symmetry, field magnitudes should be similar
0122     mag1 = (field1[0] ** 2 + field1[1] ** 2 + field1[2] ** 2) ** 0.5
0123     mag2 = (field2[0] ** 2 + field2[1] ** 2 + field2[2] ** 2) ** 0.5
0124 
0125     assert abs(mag1 - mag2) / max(mag1, mag2, 1e-10) < 0.1  # Within 10%
0126 
0127 
0128 @pytest.mark.skipif(not TOROIDAL_FIELD_AVAILABLE, reason="ToroidalField not available")
0129 def test_toroidal_field_regions():
0130     """Test field behavior in different regions (barrel vs endcap)."""
0131 
0132     config = acts.acts_toroidal_field.Config()
0133     field = acts.acts_toroidal_field.ToroidalField(config)
0134     ctx = acts.MagneticFieldContext()
0135     cache = field.makeCache(ctx)
0136 
0137     # Test in barrel region
0138     pos_barrel = acts.Vector3(7000.0, 0.0, 1000.0)
0139     field_barrel = field.getField(pos_barrel, cache)
0140     mag_barrel = (
0141         field_barrel[0] ** 2 + field_barrel[1] ** 2 + field_barrel[2] ** 2
0142     ) ** 0.5
0143 
0144     # Test in endcap region
0145     pos_endcap = acts.Vector3(2000.0, 0.0, 15000.0)
0146     field_endcap = field.getField(pos_endcap, cache)
0147     mag_endcap = (
0148         field_endcap[0] ** 2 + field_endcap[1] ** 2 + field_endcap[2] ** 2
0149     ) ** 0.5
0150 
0151     # Both should have some field
0152     assert mag_barrel > 0.0
0153     assert mag_endcap > 0.0
0154 
0155 
0156 @pytest.mark.skipif(not TOROIDAL_FIELD_AVAILABLE, reason="ToroidalField not available")
0157 def test_toroidal_field_configuration():
0158     """Test configuration classes."""
0159 
0160     # Test BarrelConfig
0161     barrel_config = acts.acts_toroidal_field.BarrelConfig()
0162     assert barrel_config.R_in > 0
0163     assert barrel_config.R_out > barrel_config.R_in
0164     assert barrel_config.I > 0
0165 
0166     # Test ECTConfig
0167     ect_config = acts.acts_toroidal_field.ECTConfig()
0168     assert ect_config.R_in > 0
0169     assert ect_config.R_out > ect_config.R_in
0170     assert ect_config.I > 0
0171 
0172     # Test LayoutConfig
0173     layout_config = acts.acts_toroidal_field.LayoutConfig()
0174     assert layout_config.nCoils > 0
0175     assert layout_config.nArc > 0
0176     assert layout_config.nStraight > 0
0177 
0178 
0179 if __name__ == "__main__":
0180     # Run basic tests if called directly
0181     if TOROIDAL_FIELD_AVAILABLE:
0182         test_toroidal_field_basic()
0183         test_toroidal_field_custom()
0184         test_toroidal_field_symmetry()
0185         test_toroidal_field_regions()
0186         test_toroidal_field_configuration()
0187         print("All ToroidalField tests passed!")
0188     else:
0189         print("ToroidalField not available - skipping tests")