Skip to content
Snippets Groups Projects
Commit 9c6040cf authored by Andre Sailer's avatar Andre Sailer
Browse files

RegexSD: change ddsim interface to use a dictionary instead of a list

Too much hassle to ensure that the list was not duplicating the entries in the list

The original implementation also caused infinite loop by appending to the list that was iterated over causing ddsim to use up all the memory and not do anything

Added a test with an example ddsim steering file
parent e1017e36
No related branches found
No related tags found
No related merge requests found
......@@ -36,45 +36,29 @@ class Geometry(ConfigHelper):
self._dumpDGDML_EXTRA = {"help": "If not empty, filename to dump the Geometry as GDML"}
self.dumpGDML = ""
self._regexSDDetectorList = []
self._regexSDDict = {}
self._closeProperties()
@property
def regexSensitiveDetector(self):
"""Configure a sensitive detector for a given detector matched by regular expression (regex).
'Detector' and 'Match' are mandatory elements of the dictionary, other Keys are assigned as property to the object.
>>> SIM.geometry.regexSensitiveDetector = {'Detector': 'DRcalo',
'Match': ['(core|clad)'],
'OutputLevel': 3,
}
This can be assigned repeatedly to add multiple RegexSDs
""" The map key is the name of the Detector, and 'Match' is a mandatory elements of the dictionary, other Keys are
assigned as property to the object. OutputLevel _sets_ the outputlevel of the plugin, so lower numbers mean more
output from the plugin.
>>> SIM.geometry.regexSensitiveDetector['DRcalo'] = {
'Match': ['(core|clad)'],
'OutputLevel': 3,
}
"""
return self._regexSDDetectorList
return self._regexSDDict
@regexSensitiveDetector.setter
def regexSensitiveDetector(self, val):
if not val:
return
if isinstance(val, dict):
self.__checkRegexKeys(val)
self._regexSDDetectorList.append(val)
elif isinstance(val, list):
for value in val:
self.__checkRegexKeys(value)
self._regexSDDetectorList.append(value)
else:
raise RuntimeError(f"Unsupported type for regexSensitiveDetector: {val!r}")
@staticmethod
def __checkRegexKeys(val):
"""Check the regex SD arguments for required keys."""
requiredKeys = ('Detector', 'Match')
if not all(key in val for key in requiredKeys):
raise RuntimeError(f"RegexSD configuration {val} is missing mandatory key(s): {', '.join(requiredKeys)}")
self._regexSDDict = val
return
raise RuntimeError(f"Unsupported type for regexSensitiveDetector: {val!r}")
def constructGeometry(self, kernel, geant4, geoPrintLevel=2, numberOfThreads=1):
"""Construct Geant4 geometry."""
......@@ -100,8 +84,9 @@ class Geometry(ConfigHelper):
sensitives.enableUI()
seq.adopt(sensitives)
for index, regexDetectors in enumerate(self._regexSDDetectorList):
seq, act = geant4.addDetectorConstruction(f'Geant4RegexSensitivesConstruction/ConstrSDRegEx_{index}')
# this will set Match and Detector, and other properties if possible
for index, (detName, regexDetectors) in enumerate(sorted(self._regexSDDict.items())):
seq, act = geant4.addDetectorConstruction(f'Geant4RegexSensitivesConstruction/ConstrSDRegEx_{index}_{detName}')
act.Detector = detName
# this will set Match, and other properties if possible
for key, value in regexDetectors.items():
setattr(act, key, value)
......@@ -632,4 +632,18 @@ if (DD4HEP_USE_GEANT4)
REGEX_PASS "ResourcesAfterConstruction ConstructSD: VmRSS"
REGEX_FAIL "Error;ERROR; Exception"
)
dd4hep_add_test_reg(ClientTests_ddsim_setup_BoxOfStraws
COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
EXEC_ARGS ddsim
--steeringFile ${ClientTestsEx_INSTALL}/scripts/BoxOfStraws_DDSim.py
--compactFile ${ClientTestsEx_INSTALL}/compact/BoxOfStraws.xml
--enableGun
--numberOfEvents 1
--outputFile regex.slcio
REGEX_PASS "BoxOfStrawsDet Handled 1 nodes with 1 sensitive volume type"
REGEX_FAIL "Error;ERROR; Exception"
)
endif(DD4HEP_USE_GEANT4)
from DDSim.DD4hepSimulation import DD4hepSimulation
SIM = DD4hepSimulation()
# make ddsim find the sensitive detector for box of straws
SIM.action.calorimeterSDTypes = ['sensitive']
SIM.filter.calo = ""
# Configure the regexSD for the BoxOfStraws gas_
SIM.geometry.regexSensitiveDetector['BoxOfStrawsDet'] = {'Match': ['gas_'],
'OutputLevel': 4,
}
# Disable userParticleHander for box of straws
SIM.part.userParticleHandler = ""
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment