Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:08

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