Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegexSD: change ddsim interface to use a dictionary instead of a list, fix infinite loop #1317

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 17 additions & 32 deletions DDG4/python/DDSim/Helper/Geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,29 @@ def __init__(self):
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):
andresailer marked this conversation as resolved.
Show resolved Hide resolved
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."""
Expand All @@ -100,8 +84,9 @@ def constructGeometry(self, kernel, geant4, geoPrintLevel=2, numberOfThreads=1):
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)
14 changes: 14 additions & 0 deletions examples/ClientTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should only run when DDG4 and LCIO are used for building.
Also currently gets no hits. Not sure if shooting in a bad direction, or SD not working.

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)
16 changes: 16 additions & 0 deletions examples/ClientTests/scripts/BoxOfStraws_DDSim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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 = ""
Loading