File indexing completed on 2026-06-25 07:48:53
0001 import acts
0002 import argparse
0003
0004 from acts.examples.odd import getOpenDataDetector
0005
0006 u = acts.UnitConstants
0007
0008
0009 def main():
0010
0011 import acts
0012
0013 global __geant4Handle
0014 global __pmr
0015
0016 p = argparse.ArgumentParser()
0017
0018 p.add_argument(
0019 "-i", "--input", type=str, default="", help="Input file(s) for the geometry"
0020 )
0021
0022 p.add_argument("-o", "--output", type=str, default="", help="Output prefix")
0023
0024 p.add_argument(
0025 "-m", "--map", type=str, default="", help="Input file for the material map"
0026 )
0027
0028
0029
0030
0031
0032
0033 p.add_argument(
0034 "--geo-mode",
0035 type=str,
0036 default="gen3",
0037 choices=["gen1", "gen3", "detray", "geant4"],
0038 help="Convert to detray detector and run detray navigation and propagation",
0039 )
0040
0041
0042 p.add_argument(
0043 "--log-level",
0044 type=lambda x: acts.logging.Level.__members__[x.upper()],
0045 default=acts.logging.INFO,
0046 metavar="|".join(acts.logging.Level.__members__.keys()),
0047 help="Log level for the geometry construction and conversion",
0048 )
0049
0050
0051 p.add_argument(
0052 "--output-json",
0053 action="store_true",
0054 help="Output the detector to a json file for visualization",
0055 )
0056
0057
0058 p.add_argument(
0059 "--detray-consistency-check",
0060 action="store_true",
0061 help="Run the consistency check for the detray geometry",
0062 )
0063
0064 args = p.parse_args()
0065
0066 prfx = args.output + "_" if args.output != "" else ""
0067
0068 gContext = acts.GeometryContext.dangerouslyDefaultConstruct()
0069
0070 logLevel = args.log_level
0071
0072
0073 materialDecorator = None
0074 if args.map != "":
0075 print(">>> Loading a material decorator from file:", args.map)
0076 materialDecorator = acts.IMaterialDecorator.fromFile(args.map)
0077
0078 trackingGeometry = None
0079 detectorStore = {}
0080
0081
0082 build_from_odd = args.input == "" or args.geo_mode in ("gen1", "geant4")
0083
0084 if build_from_odd:
0085 buildGen3 = args.geo_mode == "gen3" or args.geo_mode == "detray"
0086 with getOpenDataDetector(gen3=buildGen3, logLevel=logLevel) as detector:
0087 trackingGeometry = detector.trackingGeometry()
0088 detectorStore["Detector"] = detector
0089 detectorStore["Volume"] = trackingGeometry.highestTrackingVolume
0090 detectorStore["SurfaceByIdentifier"] = trackingGeometry.geoIdSurfaceMap()
0091
0092 print(">>> Test mode is :", args.geo_mode)
0093
0094 if args.geo_mode == "gen3":
0095 from pathlib import Path
0096 from acts.json import TrackingGeometryJsonConverter
0097
0098 converter = TrackingGeometryJsonConverter(level=logLevel)
0099
0100 if args.input != "":
0101 print(">>> Reading tracking geometry from", args.input)
0102 trackingGeometry = converter.fromJson(
0103 gContext, Path(args.input).read_text()
0104 )
0105 print(">>> Read tracking geometry from", args.input)
0106
0107 if args.output_json:
0108 print(">>> Outputting the tracking geometry to json file ...")
0109 json_str = converter.toJson(gContext, trackingGeometry)
0110 out_path = prfx + "tracking-geometry.json"
0111 Path(out_path).write_text(json_str)
0112 print(">>> Written to", out_path)
0113
0114
0115 elif args.geo_mode == "detray":
0116 import glob
0117 import acts.vecmem, acts.detray
0118 import acts.examples.detray
0119
0120 __pmr = acts.vecmem.HostMemoryResource()
0121
0122 if args.input != "":
0123 files = glob.glob(args.input.rstrip("/") + "/*.json")
0124 print(">>> Reading detray geometry from", args.input, "->", files)
0125 detrayGeometry, detrayNames = acts.detray.readODD(__pmr, files)
0126 else:
0127 detrayGeometry, detrayNames = acts.detray.convertODD(
0128 __pmr,
0129 gContext,
0130 trackingGeometry,
0131 beampipeVolumeName="BeamPipe",
0132 detectorName="odd",
0133 logLevel=logLevel,
0134 )
0135
0136 if args.detray_consistency_check:
0137 detrayGeometry.checkConsistency()
0138
0139 if args.output_json:
0140 print(">>> Outputting the detray geometry to json file ...")
0141 detray_out = prfx + "detray/"
0142 detrayGeometry.writeToJson(detrayNames, detray_out)
0143 print(">>> Written to", detray_out)
0144
0145 elif args.geo_mode == "geant4":
0146
0147 print(">>> Building Geant4 detector ...")
0148 detector = detectorStore["Detector"]
0149
0150 import acts.examples.geant4
0151
0152 from acts.examples.geant4 import (
0153 Geant4Simulation,
0154 Geant4ConstructionOptions,
0155 SensitiveSurfaceMapper,
0156 )
0157
0158 smmConfig = SensitiveSurfaceMapper.Config()
0159 smmConfig.volumeMappings = []
0160 smmConfig.materialMappings = ["Silicon"]
0161 sensitiveMapper = SensitiveSurfaceMapper.create(
0162 smmConfig, acts.logging.INFO, trackingGeometry
0163 )
0164
0165 detectorConstructionOptions = acts.examples.geant4.Geant4ConstructionOptions()
0166
0167
0168 physicsList = "FTFP_BERT"
0169 killVolume = detectorStore["Volume"]
0170 killAfterTime = float("inf")
0171 bfield = None
0172
0173 inputParticles = "particles_generated"
0174 outputParticles = "particles_final"
0175
0176
0177 if "__main__" == __name__:
0178 main()