Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:18

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 # detray json schema definitions
0010 from impl import merge_surfaces, update_grids, update_material
0011 
0012 from options import (
0013     common_options,
0014     parse_common_options,
0015     detector_io_options,
0016     parse_detector_io_options,
0017 )
0018 
0019 # python includes
0020 import argparse
0021 import json
0022 import os
0023 
0024 """ Update the file name for the output json files"""
0025 
0026 
0027 def __update_filename(input_path):
0028     _, filename = os.path.split(input_path)
0029     stem, extension = os.path.splitext(filename)
0030 
0031     return f"{stem}_merged{extension}"
0032 
0033 
0034 def __main__():
0035     # ---------------------------------------------------------------arg parsing
0036 
0037     descr = "Detray Surface Merging Service"
0038 
0039     # Define options
0040     parent_parsers = [
0041         common_options(descr),
0042         detector_io_options(),
0043     ]
0044 
0045     parser = argparse.ArgumentParser(description=descr, parents=parent_parsers)
0046     args = parser.parse_args()
0047 
0048     logging = parse_common_options(args, descr)
0049     parse_detector_io_options(args, logging)
0050 
0051     if args.grid_file == "" or args.material_file == "":
0052         logging.warning(
0053             "If there are grid or material files, these need to be updated as well!!!\n"
0054         )
0055 
0056     # -----------------------------------------------------------------------run
0057 
0058     with open(args.geometry_file) as geo_file:
0059         try:
0060             geo_json = json.load(geo_file)
0061         except json.decoder.JSONDecodeError:
0062             logging.error(f"Invalid json file: {geo_file}")
0063         else:
0064             # Merge the portal surfaces
0065             logging.info(f"Read geometry file {args.geometry_file}")
0066             out_geo_json, sf_index_dict = merge_surfaces(logging, geo_json)
0067 
0068             out_file_name = __update_filename(args.geometry_file)
0069             with open(out_file_name, "w") as out_file:
0070                 json.dump(out_geo_json, out_file, indent=4)
0071 
0072             logging.info(f"Geometry: Finished, written to: {out_file_name}\n")
0073 
0074             # Update the grids
0075             if args.grid_file != "":
0076                 with open(args.grid_file) as grid_file:
0077                     try:
0078                         grid_json = json.load(grid_file)
0079                     except json.decoder.JSONDecodeError:
0080                         logging.error(f"Invalid json file: {grid_file}")
0081                     else:
0082                         logging.info(f"Read grid file {args.grid_file}")
0083                         out_grid_json = update_grids(logging, grid_json, sf_index_dict)
0084 
0085                         out_file_name = __update_filename(args.grid_file)
0086                         with open(out_file_name, "w") as out_file:
0087                             json.dump(out_grid_json, out_file, indent=4)
0088 
0089                         logging.info(f"Grids: Finished, written to: {out_file_name}\n")
0090 
0091             # Update the material
0092             if args.material_file != "":
0093                 with open(args.material_file) as mat_file:
0094                     try:
0095                         mat_json = json.load(mat_file)
0096                     except json.decoder.JSONDecodeError:
0097                         logging.error(f"Invalid json file: {mat_file}")
0098                     else:
0099                         logging.info(f"Read material file {args.material_file}")
0100                         out_mat_json = update_material(logging, mat_json, sf_index_dict)
0101 
0102                         out_file_name = __update_filename(args.material_file)
0103                         with open(out_file_name, "w") as out_file:
0104                             json.dump(out_mat_json, out_file, indent=4)
0105 
0106                         logging.info(
0107                             f"Material: Finished, written to: {out_file_name}\n"
0108                         )
0109 
0110 
0111 # ------------------------------------------------------------------------------
0112 
0113 if __name__ == "__main__":
0114     __main__()
0115 
0116 # ------------------------------------------------------------------------------