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 includes
0010 from impl import plot_material_scan as mat_plotter
0011 from impl import read_material_data
0012 from plotting import pyplot_factory as plt_factory
0013 from options import (
0014     common_options,
0015     detector_io_options,
0016     uniform_track_generator_options,
0017     propagation_options,
0018     plotting_options,
0019 )
0020 from options import (
0021     parse_common_options,
0022     parse_detector_io_options,
0023     parse_plotting_options,
0024 )
0025 from utils import read_detector_name
0026 from utils import add_track_generator_args, add_propagation_args, add_detector_io_args
0027 
0028 # python includes
0029 import argparse
0030 import json
0031 import os
0032 import subprocess
0033 import sys
0034 
0035 
0036 def __main__():
0037 
0038     # ---------------------------------------------------------------arg parsing
0039 
0040     descr = "Detray Material Validation"
0041 
0042     # Define options
0043     parent_parsers = [
0044         common_options(descr),
0045         detector_io_options(),
0046         uniform_track_generator_options(),
0047         propagation_options(),
0048         plotting_options(),
0049     ]
0050 
0051     parser = argparse.ArgumentParser(description=descr, parents=parent_parsers)
0052 
0053     parser.add_argument(
0054         "--bindir",
0055         "-bin",
0056         help=("Directory containing the validation executables"),
0057         default="./bin",
0058         type=str,
0059     )
0060     parser.add_argument(
0061         "--datadir",
0062         "-data",
0063         help=("Directory containing the data files"),
0064         default="./validation_data/material",
0065         type=str,
0066     )
0067     parser.add_argument(
0068         "--material_tol",
0069         "-mt",
0070         help=("Tolerance for material comparisons [%]"),
0071         default=1,
0072         type=float,
0073     )
0074     parser.add_argument(
0075         "--overlaps_tol",
0076         "-ot",
0077         help=("Tolerance for considering surfaces to be overlapping [mm]"),
0078         default=0.0001,
0079         type=float,
0080     )
0081     parser.add_argument(
0082         "--cuda",
0083         help=("Run the CUDA material validation."),
0084         action="store_true",
0085         default=False,
0086     )
0087     parser.add_argument(
0088         "--sycl",
0089         help=("Run the SYCL material validation."),
0090         action="store_true",
0091         default=False,
0092     )
0093 
0094     args = parser.parse_args()
0095 
0096     logging = parse_common_options(args, descr)
0097     parse_detector_io_options(args, logging)
0098     _, out_dir, out_format = parse_plotting_options(args, logging)
0099 
0100     # IO path for data files
0101     datadir = args.datadir.strip("/")
0102 
0103     # Check bin path
0104     bindir = args.bindir.strip("/")
0105     cpu_validation = bindir + "/detray_material_validation"
0106     cuda_validation = bindir + "/detray_material_validation_cuda"
0107 
0108     if not os.path.isdir(bindir) or not os.path.isfile(cpu_validation):
0109         logging.error(f"Material validation binaries were not found! ({args.bindir})")
0110         sys.exit(1)
0111 
0112     # -----------------------------------------------------------------------run
0113 
0114     # Pass on the options for the validation tools
0115     args_list = [
0116         "--data_dir",
0117         datadir,
0118         "--material_tol",
0119         str(args.material_tol),
0120         "--overlaps_tol",
0121         str(args.overlaps_tol),
0122     ]
0123 
0124     # Add parsed options to argument list
0125     add_detector_io_args(args_list, args)
0126     add_track_generator_args(args_list, args)
0127     add_propagation_args(args_list, args)
0128 
0129     logging.debug(args_list)
0130 
0131     if "--material_file" not in args_list:
0132         logging.error(
0133             "Detector material is required! Please add it using the '--material_file' option"
0134         )
0135         sys.exit(1)
0136 
0137     # Run the host validation and produce the truth data
0138     logging.debug("Running CPU material validation")
0139     subprocess.run([cpu_validation] + args_list)
0140 
0141     # Run the device validation (if it has been built)
0142     if args.cuda and os.path.isfile(cuda_validation):
0143         logging.debug("Running CUDA material validation")
0144         subprocess.run([cuda_validation] + args_list)
0145 
0146     elif args.cuda:
0147         logging.error("Could not find CUDA material validation executable")
0148 
0149     if args.sycl:
0150         logging.error("SYCL material validation is not implemented")
0151 
0152     # ----------------------------------------------------------------------plot
0153 
0154     logging.info("Generating data plots...\n")
0155 
0156     det_name = read_detector_name(args.geometry_file, logging)
0157     logging.debug("Detector: " + det_name)
0158 
0159     # Check the data path (should have been created when running the validation)
0160     if not os.path.isdir(datadir):
0161         logging.error(f"Data directory was not found! ({args.datadir})")
0162         sys.exit(1)
0163 
0164     df_scan, df_cpu, df_cuda = read_material_data(datadir, logging, det_name, args.cuda)
0165 
0166     plot_factory = plt_factory(out_dir, logging)
0167 
0168     # The histograms are not re-weighted (if the rays are not evenly distributed
0169     # the material in some bins might be artificially high)!
0170     mat_plotter.X0_vs_eta_phi(
0171         df_scan, "material_scan", det_name, plot_factory, out_format
0172     )
0173     mat_plotter.L0_vs_eta_phi(
0174         df_scan, "material_scan", det_name, plot_factory, out_format
0175     )
0176     mat_plotter.X0_vs_eta(df_scan, "material_scan", det_name, plot_factory, out_format)
0177     mat_plotter.L0_vs_eta(df_scan, "material_scan", det_name, plot_factory, out_format)
0178 
0179     # Navigation material Traces
0180     # CPU
0181     mat_plotter.X0_vs_eta_phi(
0182         df_cpu, "cpu_material_trace", det_name, plot_factory, out_format
0183     )
0184     mat_plotter.L0_vs_eta_phi(
0185         df_cpu, "cpu_material_trace", det_name, plot_factory, out_format
0186     )
0187     mat_plotter.X0_vs_eta(
0188         df_cpu, "cpu_material_trace", det_name, plot_factory, out_format
0189     )
0190     mat_plotter.L0_vs_eta(
0191         df_cpu, "cpu_material_trace", det_name, plot_factory, out_format
0192     )
0193 
0194     # Comparison between scan and navigator trace in sX0
0195     mat_plotter.compare_mat(
0196         df_scan, df_cpu, "cpu_material", det_name, plot_factory, out_format
0197     )
0198 
0199     # CUDA
0200     if args.cuda:
0201         mat_plotter.X0_vs_eta_phi(
0202             df_cuda, "cuda_material_trace", det_name, plot_factory, out_format
0203         )
0204         mat_plotter.L0_vs_eta_phi(
0205             df_cuda, "cuda_material_trace", det_name, plot_factory, out_format
0206         )
0207         mat_plotter.X0_vs_eta(
0208             df_cuda, "cuda_material_trace", det_name, plot_factory, out_format
0209         )
0210         mat_plotter.L0_vs_eta(
0211             df_cuda, "cuda_material_trace", det_name, plot_factory, out_format
0212         )
0213         mat_plotter.compare_mat(
0214             df_scan, df_cuda, "cuda_material", det_name, plot_factory, out_format
0215         )
0216 
0217 
0218 # ------------------------------------------------------------------------------
0219 
0220 if __name__ == "__main__":
0221     __main__()
0222 
0223 # ------------------------------------------------------------------------------