Skip to content

Commit

Permalink
refactor: Conditional import & refactor in python scripts related to …
Browse files Browse the repository at this point in the history
…material mapping (#3518)

This mainly allows using these scripts when not all plugins are built. Some additional refactoring and improvement of error messages.
  • Loading branch information
benjaminhuth authored Aug 20, 2024
1 parent 337343f commit 320ef1b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion Examples/Scripts/MaterialMapping/material_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,7 @@

# Set the range of x-axis
plt.xlabel(pr.x_label)
plt.show()
fig.savefig(pr.saveAs)


plt.show()
15 changes: 8 additions & 7 deletions Examples/Scripts/Python/material_mapping_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions Examples/Scripts/Python/material_validation_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 320ef1b

Please sign in to comment.