Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-24 08:06:21

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