File indexing completed on 2025-08-05 08:10:09
0001
0002 from pathlib import Path
0003 import argparse
0004
0005 from acts.examples import (
0006 WhiteBoard,
0007 AlgorithmContext,
0008 ProcessCode,
0009 CsvTrackingGeometryWriter,
0010 ObjTrackingGeometryWriter,
0011 JsonSurfacesWriter,
0012 JsonMaterialWriter,
0013 JsonFormat,
0014 )
0015
0016 import acts
0017
0018 from acts import MaterialMapJsonConverter
0019
0020
0021 def runITk(
0022 trackingGeometry,
0023 decorators,
0024 outputDir: Path,
0025 events=1,
0026 outputObj=True,
0027 outputCsv=False,
0028 outputJson=False,
0029 ):
0030 for ievt in range(events):
0031 eventStore = WhiteBoard(name=f"EventStore#{ievt}", level=acts.logging.INFO)
0032 ialg = 0
0033
0034 context = AlgorithmContext(ialg, ievt, eventStore)
0035
0036 for cdr in decorators:
0037 r = cdr.decorate(context)
0038 if r != ProcessCode.SUCCESS:
0039 raise RuntimeError("Failed to decorate event context")
0040
0041 if outputCsv:
0042 csv_dir = outputDir / "csv"
0043 csv_dir.mkdir(exist_ok=True)
0044 writer = CsvTrackingGeometryWriter(
0045 level=acts.logging.INFO,
0046 trackingGeometry=trackingGeometry,
0047 outputDir=str(csv_dir),
0048 writePerEvent=True,
0049 )
0050 writer.write(context)
0051
0052 if outputObj:
0053 obj_dir = outputDir / "obj"
0054 obj_dir.mkdir(exist_ok=True)
0055 writer = ObjTrackingGeometryWriter(
0056 level=acts.logging.INFO,
0057 outputDir=str(obj_dir),
0058 )
0059 writer.write(context, trackingGeometry)
0060
0061 if outputJson:
0062 json_dir = outputDir / "json"
0063 json_dir.mkdir(exist_ok=True)
0064 writer = JsonSurfacesWriter(
0065 level=acts.logging.INFO,
0066 trackingGeometry=trackingGeometry,
0067 outputDir=str(json_dir),
0068 writePerEvent=True,
0069 writeSensitive=True,
0070 )
0071 writer.write(context)
0072
0073 jmConverterCfg = MaterialMapJsonConverter.Config(
0074 processSensitives=True,
0075 processApproaches=True,
0076 processRepresenting=True,
0077 processBoundaries=True,
0078 processVolumes=True,
0079 processNonMaterial=True,
0080 context=context.geoContext,
0081 )
0082
0083 jmw = JsonMaterialWriter(
0084 level=acts.logging.VERBOSE,
0085 converterCfg=jmConverterCfg,
0086 fileName=str(json_dir / "material-map"),
0087 writeFormat=JsonFormat.Json,
0088 )
0089
0090 jmw.write(trackingGeometry)
0091
0092
0093 if "__main__" == __name__:
0094 p = argparse.ArgumentParser(
0095 description="Example script to construct the ITk geometry and write it out to CSV and OBJ formats"
0096 )
0097 p.add_argument(
0098 "geo_dir",
0099 help="Input directory containing the ITk standalone geometry. Get in touch if you don't have this.",
0100 )
0101 p.add_argument(
0102 "--output-dir",
0103 default=Path.cwd(),
0104 type=Path,
0105 help="Directory to write outputs to",
0106 )
0107 p.add_argument(
0108 "--output-csv", action="store_true", help="Write geometry in CSV format."
0109 )
0110 p.add_argument(
0111 "--output-obj", action="store_true", help="Write geometry in OBJ format."
0112 )
0113 p.add_argument(
0114 "--output-json",
0115 action="store_true",
0116 help="Write geometry and material in JSON format.",
0117 )
0118 p.add_argument(
0119 "--no-material", action="store_true", help="Decorate material to the geometry"
0120 )
0121
0122 args = p.parse_args()
0123 args.output_dir.mkdir(exist_ok=True, parents=True)
0124
0125 geo_example_dir = Path(args.geo_dir)
0126 assert geo_example_dir.exists(), "Detector example input directory missing"
0127 from acts.examples.itk import buildITkGeometry
0128
0129 detector, trackingGeometry, decorators = buildITkGeometry(
0130 geo_example_dir,
0131 material=not args.no_material,
0132 )
0133
0134 runITk(
0135 trackingGeometry=trackingGeometry,
0136 decorators=decorators,
0137 outputDir=args.output_dir,
0138 outputCsv=args.output_csv,
0139 outputObj=args.output_obj,
0140 outputJson=args.output_json,
0141 )