From 17e5db711018d83a4b9309b0c1037f0eeb3d79c5 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Thu, 11 Jul 2024 13:47:32 +0200 Subject: [PATCH 1/2] Fix a crash when an action has dictionaries and is not hashable Refactor the functions to setup a calorimeter and a tracker detector into a single one since the logic is the same. In this new function, use a try-except and if the type is not hashable used the default value. --- DDG4/python/DDG4.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/DDG4/python/DDG4.py b/DDG4/python/DDG4.py index eb0822034..0d4937624 100644 --- a/DDG4/python/DDG4.py +++ b/DDG4/python/DDG4.py @@ -670,35 +670,40 @@ def setupDetector(self, name, action, collections=None): return (seq, acts) return (seq, acts[0]) - def setupCalorimeter(self, name, type=None, collections=None): # noqa: A002 + def setupCaloOrTracker(self, name, detType=None, collections=None, isCalo=True): # noqa: A002 + """ + Setup subdetector of type 'calorimeter' and assign the proper sensitive action + """ + self.description.sensitiveDetector(str(name)) + retType = detType + if detType is None: + retType = self.sensitive_types['calorimeter' if isCalo else 'tracker'] + elif detType is not None: + try: + retType = self.sensitive_types[detType] + # KeyError = not found in the dictionary + # TypeError = detType is not a hashable type + except (KeyError, TypeError): + pass + except Exception as X: + raise X + return self.setupDetector(name, retType, collections) + + def setupCalorimeter(self, name, caloType=None, collections=None): # noqa: A002 """ Setup subdetector of type 'calorimeter' and assign the proper sensitive action \author M.Frank """ - typ = type # noqa: A002 - self.description.sensitiveDetector(str(name)) - # sd.setType('calorimeter') - if typ is None: - typ = self.sensitive_types['calorimeter'] - elif typ is not None and self.sensitive_types.get(typ): - typ = self.sensitive_types[typ] - return self.setupDetector(name, typ, collections) + return self.setupCaloOrTracker(name, caloType, collections, True) - def setupTracker(self, name, type=None, collections=None): # noqa: A002 + def setupTracker(self, name, trackerType=None, collections=None): # noqa: A002 """ Setup subdetector of type 'tracker' and assign the proper sensitive action \author M.Frank """ - typ = type - self.description.sensitiveDetector(str(name)) - # sd.setType('tracker') - if typ is None: - typ = self.sensitive_types['tracker'] - elif typ is not None and self.sensitive_types.get(typ): - typ = self.sensitive_types[typ] - return self.setupDetector(name, typ, collections) + return self.setupCaloOrTracker(name, trackerType, collections, False) def _private_setupField(self, field, stepper, equation, prt): import g4units From 86e370fae1c6df629c88aa247d4131339505780e Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Fri, 19 Jul 2024 10:58:00 +0200 Subject: [PATCH 2/2] Use different logic to choose when to use the dictionary of sensitive types --- DDG4/python/DDG4.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/DDG4/python/DDG4.py b/DDG4/python/DDG4.py index 0d4937624..71cfc80c8 100644 --- a/DDG4/python/DDG4.py +++ b/DDG4/python/DDG4.py @@ -678,15 +678,9 @@ def setupCaloOrTracker(self, name, detType=None, collections=None, isCalo=True): retType = detType if detType is None: retType = self.sensitive_types['calorimeter' if isCalo else 'tracker'] - elif detType is not None: - try: - retType = self.sensitive_types[detType] - # KeyError = not found in the dictionary - # TypeError = detType is not a hashable type - except (KeyError, TypeError): - pass - except Exception as X: - raise X + # detType is a tuple when an action with parameters in a dictionary is passed + elif not isinstance(detType, tuple) and detType in self.sensitive_types: + retType = self.sensitive_types[detType] return self.setupDetector(name, retType, collections) def setupCalorimeter(self, name, caloType=None, collections=None): # noqa: A002