Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:23:12

0001 # This file is part of the ACTS project.
0002 #
0003 # Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 #
0005 # This Source Code Form is subject to the terms of the Mozilla Public
0006 # License, v. 2.0. If a copy of the MPL was not distributed with this
0007 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 """Generate a detector metadata header from a packaged or external configuration.
0010 
0011 A configuration is a module named ``<name>_detector`` that provides a
0012 ``configure(md)`` function. It produces ``<name>_metadata.hpp`` declaring
0013 ``detray::<name>_metadata``.
0014 """
0015 
0016 import argparse
0017 from importlib import import_module
0018 from pathlib import Path
0019 import runpy
0020 
0021 from detray_metadata import (
0022     add_io_options,
0023     add_logging_options,
0024     metadata,
0025     metadata_generator,
0026     parse_io_options,
0027     parse_logging_options,
0028 )
0029 
0030 
0031 def detector_name(stem: str) -> str:
0032     """Map a configuration module name to the detector name."""
0033     name = stem.removesuffix("_detector")
0034     if not name or not name.isidentifier():
0035         raise ValueError(f"cannot derive a detector name from {stem!r}")
0036     return name
0037 
0038 
0039 def load_configuration(selector: str):
0040     """Resolve NAME or FILE.py:FUNCTION to (detector name, callable)."""
0041     if ":" in selector:
0042         filename, function_name = selector.rsplit(":", 1)
0043         name = detector_name(Path(filename).stem)
0044         configure = runpy.run_path(filename)[function_name]
0045     else:
0046         name = selector
0047         module = import_module(f"detray_metadata.configurations.{name}_detector")
0048         configure = module.configure
0049     if not callable(configure):
0050         raise ValueError(f"{selector} is not callable")
0051     return name, configure
0052 
0053 
0054 def main():
0055     parser = argparse.ArgumentParser(description=__doc__)
0056     parser.add_argument(
0057         "--detector",
0058         required=True,
0059         metavar="NAME or FILE:FUNCTION",
0060         help="Shipped detector name (e.g. 'odd') or an external Python file and "
0061         "callable; <name>_detector.py names the detector <name>",
0062     )
0063     add_logging_options(parser)
0064     add_io_options(parser)
0065     args = parser.parse_args()
0066     parse_logging_options(args)
0067     parse_io_options(args)
0068 
0069     try:
0070         name, configure = load_configuration(args.detector)
0071     except (ValueError, ImportError, AttributeError, KeyError, OSError) as error:
0072         parser.error(f"Invalid detector configuration {args.detector!r}: {error}")
0073 
0074     md = metadata(name)
0075     configure(md)
0076     metadata_generator(
0077         md, output=args.output, format_header=args.format, output_file=args.output_file
0078     )
0079 
0080 
0081 if __name__ == "__main__":
0082     main()