ATLAS Offline Software
AtlasSemantics.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
3 import GaudiConfig2.semantics
4 from GaudiKernel.DataHandle import DataHandle
5 import re
6 
7 
8 class MapMergeNoReplaceSemantics(GaudiConfig2.semantics.MappingSemantics):
9  '''
10  Extend the mapping-semantics with a merge-method that merges two mappings as long as they do not have different values for the same key
11  Use 'mapMergeNoReplace<T>' as fifth parameter of the Gaudi::Property<T> constructor
12  to invoke this merging method.
13  '''
14  __handled_types__ = (re.compile(r"^mapMergeNoReplace<.*>$"),)
15  def __init__(self, cpp_type):
16  super(MapMergeNoReplaceSemantics, self).__init__(cpp_type)
17 
18  def merge(self,a,b):
19  for k in b.keys():
20  if k in a and b[k] != a[k]:
21  raise ValueError('conflicting values in map under key %r and %r %r' % (k, b[k], a[k]))
22  a[k] = b[k]
23  return a
24 
25 
26 class VarHandleKeySemantics(GaudiConfig2.semantics.PropertySemantics):
27  '''
28  Semantics for all data handle keys (Read, Write, Decor, Cond).
29  '''
30  __handled_types__ = (re.compile(r"SG::.*HandleKey<.*>$"),)
31 
32  def __init__(self, cpp_type):
33  super().__init__(cpp_type)
34  # Deduce actual handle type
35  self._type = next(GaudiConfig2.semantics.extract_template_args(cpp_type))
36  self._isCond = 'CondHandle' in cpp_type
37 
38  if cpp_type.startswith("SG::Read"):
39  self._mode = "R"
40  elif cpp_type.startswith("SG::Write"):
41  self._mode = "W"
42  else:
43  raise TypeError(f"C++ type {cpp_type} not supported")
44 
45  def store(self, value):
46  if isinstance(value, DataHandle):
47  v = value.Path
48  elif isinstance(value, str):
49  v = value
50  else:
51  raise TypeError(f"cannot assign {value!r} ({type(value)}) to {self.name}"
52  ", expected string or DataHandle")
53  return DataHandle(v, self._mode, self._type, self._isCond)
54 
55 
56 class VarHandleArraySematics(GaudiConfig2.semantics.SequenceSemantics):
57  '''
58  Treat VarHandleKeyArrays like arrays of strings
59  '''
60  __handled_types__ = ("SG::VarHandleKeyArray",)
61 
62  class _ItemSemantics(GaudiConfig2.semantics.StringSemantics):
63  """Semantics for an item (DataHandle) in a VarHandleKeyArray converting to string"""
64 
65  def __init__(self):
66  super().__init__("std::string")
67 
68  def store(self, value):
69  if isinstance(value, DataHandle):
70  return value.Path
71  elif isinstance(value, str):
72  return value
73  else:
74  raise TypeError(f"cannot assign {value!r} ({type(value)}) to {self.name}"
75  ", expected string or DataHandle")
76 
77  def __init__(self, cpp_type):
78  super().__init__(cpp_type, valueSem = self._ItemSemantics())
79 
80  def merge(self,bb,aa):
81  for b in bb:
82  if b not in aa:
83  aa.append(b)
84  return aa
85 
86 
87 from AthenaServices.ItemListSemantics import OutputStreamItemListSemantics
88 
89 GaudiConfig2.semantics.SEMANTICS.append(VarHandleKeySemantics)
90 GaudiConfig2.semantics.SEMANTICS.append(VarHandleArraySematics)
91 GaudiConfig2.semantics.SEMANTICS.append(MapMergeNoReplaceSemantics)
92 GaudiConfig2.semantics.SEMANTICS.append(OutputStreamItemListSemantics)
python.AtlasSemantics.VarHandleKeySemantics._type
_type
Definition: AtlasSemantics.py:35
python.AtlasSemantics.VarHandleKeySemantics.__init__
def __init__(self, cpp_type)
Definition: AtlasSemantics.py:32
python.AtlasSemantics.MapMergeNoReplaceSemantics.merge
def merge(self, a, b)
Definition: AtlasSemantics.py:18
python.AtlasSemantics.VarHandleArraySematics._ItemSemantics.__init__
def __init__(self)
Definition: AtlasSemantics.py:65
fillPileUpNoiseLumi.next
next
Definition: fillPileUpNoiseLumi.py:52
python.AtlasSemantics.MapMergeNoReplaceSemantics.__init__
def __init__(self, cpp_type)
Definition: AtlasSemantics.py:15
python.AtlasSemantics.MapMergeNoReplaceSemantics
Definition: AtlasSemantics.py:8
python.AtlasSemantics.VarHandleArraySematics.merge
def merge(self, bb, aa)
Definition: AtlasSemantics.py:80
python.AtlasSemantics.VarHandleArraySematics.__init__
def __init__(self, cpp_type)
Definition: AtlasSemantics.py:77
python.AtlasSemantics.VarHandleKeySemantics._isCond
_isCond
Definition: AtlasSemantics.py:36
python.AtlasSemantics.VarHandleArraySematics._ItemSemantics
Definition: AtlasSemantics.py:62
python.AtlasSemantics.VarHandleKeySemantics
Definition: AtlasSemantics.py:26
python.AtlasSemantics.VarHandleKeySemantics._mode
_mode
Definition: AtlasSemantics.py:39
python.AtlasSemantics.VarHandleArraySematics._ItemSemantics.store
def store(self, value)
Definition: AtlasSemantics.py:68
python.AtlasSemantics.VarHandleKeySemantics.store
def store(self, value)
Definition: AtlasSemantics.py:45
python.AtlasSemantics.VarHandleArraySematics
Definition: AtlasSemantics.py:56