From 320ef1bebdb5e4fc49c849d7181975f769c6a0a5 Mon Sep 17 00:00:00 2001 From: Benjamin Huth <37871400+benjaminhuth@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:10:11 +0200 Subject: [PATCH] refactor: Conditional import & refactor in python scripts related to material mapping (#3518) This mainly allows using these scripts when not all plugins are built. Some additional refactoring and improvement of error messages. --- ...eometryVisualisationAndMaterialHandling.py | 18 ++++++++------- .../MaterialMapping/material_comparison.py | 4 +++- .../Scripts/Python/material_mapping_core.py | 15 +++++++------ .../Python/material_validation_core.py | 22 +++++++++++-------- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/Examples/Scripts/MaterialMapping/GeometryVisualisationAndMaterialHandling.py b/Examples/Scripts/MaterialMapping/GeometryVisualisationAndMaterialHandling.py index c6271054d44..ebd90169878 100644 --- a/Examples/Scripts/MaterialMapping/GeometryVisualisationAndMaterialHandling.py +++ b/Examples/Scripts/MaterialMapping/GeometryVisualisationAndMaterialHandling.py @@ -141,7 +141,7 @@ def dump_geo(filename, plot, output_folder, dump_steering, steering_file): index_to_extends_layers_discs[vol - 1].append(extends) else: print( - "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered." + f"WARNING: Processing surface with unknown type '{entry["value"]["type"]}'. Only CylinderSurface and DiscSurface are considered." ) if "boundary" in entry: @@ -176,7 +176,7 @@ def dump_geo(filename, plot, output_folder, dump_steering, steering_file): index_to_extends_layers_bounds_discs[vol - 1].append(extends) else: print( - "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered." + f"WARNING: Processing surface with unknown type '{entry["value"]["type"]}'. Only CylinderSurface and DiscSurface are considered." ) # Steering the information and collect it into an output file if needed @@ -436,10 +436,12 @@ def dump_geo(filename, plot, output_folder, dump_steering, steering_file): def read_and_modify(filename, plot, output_folder, steering_file, output_file): - f = open(filename) - layers = open(steering_file) - data = json.load(f) - full_data = json.load(layers) + with open(filename) as f: + data = json.load(f) + + with open(steering_file) as f: + full_data = json.load(f) + layer_data = full_data["SteeringField"] index_to_names = [] @@ -548,7 +550,7 @@ def read_and_modify(filename, plot, output_folder, steering_file, output_file): ] else: print( - "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered." + f"WARNING: Processing surface with unknown type '{entry["value"]["type"]}. Only CylinderSurface and DiscSurface are considered." ) if val["bins"] == 0: print( @@ -624,7 +626,7 @@ def read_and_modify(filename, plot, output_folder, steering_file, output_file): material_layer_discs[vol - 1].append(extends) else: print( - "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered." + f"WARNING: Processing surface with unknown type '{entry["value"]["type"]}'. Only CylinderSurface and DiscSurface are considered." ) if "boundary" in entry: diff --git a/Examples/Scripts/MaterialMapping/material_comparison.py b/Examples/Scripts/MaterialMapping/material_comparison.py index 0c980af3287..b4555988b27 100644 --- a/Examples/Scripts/MaterialMapping/material_comparison.py +++ b/Examples/Scripts/MaterialMapping/material_comparison.py @@ -121,5 +121,7 @@ # Set the range of x-axis plt.xlabel(pr.x_label) - plt.show() fig.savefig(pr.saveAs) + + +plt.show() diff --git a/Examples/Scripts/Python/material_mapping_core.py b/Examples/Scripts/Python/material_mapping_core.py index 837d42c6047..9a9801b50af 100644 --- a/Examples/Scripts/Python/material_mapping_core.py +++ b/Examples/Scripts/Python/material_mapping_core.py @@ -26,13 +26,6 @@ JsonFormat, ) -from acts.examples.dd4hep import ( - DD4hepDetector, - DD4hepDetectorOptions, - DD4hepGeometryService, -) - -from acts import geomodel as gm from acts.examples.odd import getOpenDataDetector, getOpenDataDetectorDirectory @@ -191,6 +184,8 @@ def runMaterialMapping(surfaces, inputFile, outputFile, outputMap, loglevel): if args.experimental: if len(args.geomodel_input) > 0: + from acts import geomodel as gm + # Read the geometry model from the database gmTree = acts.geomodel.readFromDb(args.geomodel_input) @@ -251,6 +246,12 @@ def runMaterialMapping(surfaces, inputFile, outputFile, outputMap, loglevel): materialSurfaces = detector.extractMaterialSurfaces() else: + from acts.examples.dd4hep import ( + DD4hepDetector, + DD4hepDetectorOptions, + DD4hepGeometryService, + ) + odd_xml = getOpenDataDetectorDirectory() / "xml" / "OpenDataDetector.xml" # Create the dd4hep geometry service and detector diff --git a/Examples/Scripts/Python/material_validation_core.py b/Examples/Scripts/Python/material_validation_core.py index a0f3a19d9e5..e696f704a5f 100644 --- a/Examples/Scripts/Python/material_validation_core.py +++ b/Examples/Scripts/Python/material_validation_core.py @@ -22,15 +22,6 @@ MaterialValidation, ) -from acts.examples.dd4hep import ( - DD4hepDetector, - DD4hepDetectorOptions, - DD4hepGeometryService, -) - -from acts import geomodel as gm -from acts.examples.odd import getOpenDataDetector, getOpenDataDetectorDirectory - def runMaterialValidation(s, ntracks, surfaces, outputFile, seed, loglevel): # IO for material tracks reading @@ -133,6 +124,8 @@ def runMaterialValidation(s, ntracks, surfaces, outputFile, seed, loglevel): if args.experimental: if len(args.geomodel_input) > 0: + from acts import geomodel as gm + # Read the geometry model from the database gmTree = acts.geomodel.readFromDb(args.geomodel_input) @@ -194,6 +187,17 @@ def runMaterialValidation(s, ntracks, surfaces, outputFile, seed, loglevel): materialSurfaces = detector.extractMaterialSurfaces() else: + from acts.examples.dd4hep import ( + DD4hepDetector, + DD4hepDetectorOptions, + DD4hepGeometryService, + ) + + from acts.examples.odd import ( + getOpenDataDetector, + getOpenDataDetectorDirectory, + ) + odd_xml = getOpenDataDetectorDirectory() / "xml" / "OpenDataDetector.xml" # Create the dd4hep geometry service and detector