Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-20 09:27:46

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