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 VarHandleKeySemantics(GaudiConfig2.semantics.PropertySemantics):
9  '''
10  Semantics for all data handle keys (Read, Write, Decor, Cond).
11  '''
12  __handled_types__ = (re.compile(r"SG::.*HandleKey<.*>$"),)
13 
14  def __init__(self, cpp_type):
15  super().__init__(cpp_type)
16  # Deduce actual handle type
17  self._type = next(GaudiConfig2.semantics.extract_template_args(cpp_type))
18  self._isCond = 'CondHandle' in cpp_type
19 
20  if cpp_type.startswith("SG::Read"):
21  self._mode = "R"
22  elif cpp_type.startswith("SG::Write"):
23  self._mode = "W"
24  else:
25  raise TypeError(f"C++ type {cpp_type} not supported")
26 
27  def store(self, value):
28  if isinstance(value, DataHandle):
29  v = value.Path
30  elif isinstance(value, str):
31  v = value
32  else:
33  raise TypeError(f"cannot assign {value!r} ({type(value)}) to {self.name}"
34  ", expected string or DataHandle")
35  return DataHandle(v, self._mode, self._type, self._isCond)
36 
37 
38 class VarHandleArraySematics(GaudiConfig2.semantics.SequenceSemantics):
39  '''
40  Treat VarHandleKeyArrays like arrays of strings
41  '''
42  __handled_types__ = ("SG::VarHandleKeyArray",)
43 
44  class _ItemSemantics(GaudiConfig2.semantics.StringSemantics):
45  """Semantics for an item (DataHandle) in a VarHandleKeyArray converting to string"""
46 
47  def __init__(self):
48  super().__init__("std::string")
49 
50  def store(self, value):
51  if isinstance(value, DataHandle):
52  return value.Path
53  elif isinstance(value, str):
54  return value
55  else:
56  raise TypeError(f"cannot assign {value!r} ({type(value)}) to {self.name}"
57  ", expected string or DataHandle")
58 
59  def __init__(self, cpp_type):
60  super().__init__(cpp_type, valueSem = self._ItemSemantics())
61 
62  def merge(self,bb,aa):
63  for b in bb:
64  if b not in aa:
65  aa.append(b)
66  return aa
67 
68 
69 from AthenaServices.ItemListSemantics import OutputStreamItemListSemantics
70 
71 GaudiConfig2.semantics.SEMANTICS.append(VarHandleKeySemantics)
72 GaudiConfig2.semantics.SEMANTICS.append(VarHandleArraySematics)
73 GaudiConfig2.semantics.SEMANTICS.append(OutputStreamItemListSemantics)
python.AtlasSemantics.VarHandleKeySemantics._type
_type
Definition: AtlasSemantics.py:17
python.AtlasSemantics.VarHandleKeySemantics.__init__
def __init__(self, cpp_type)
Definition: AtlasSemantics.py:14
python.AtlasSemantics.VarHandleArraySematics._ItemSemantics.__init__
def __init__(self)
Definition: AtlasSemantics.py:47
fillPileUpNoiseLumi.next
next
Definition: fillPileUpNoiseLumi.py:52
python.AtlasSemantics.VarHandleArraySematics.merge
def merge(self, bb, aa)
Definition: AtlasSemantics.py:62
python.AtlasSemantics.VarHandleArraySematics.__init__
def __init__(self, cpp_type)
Definition: AtlasSemantics.py:59
python.AtlasSemantics.VarHandleKeySemantics._isCond
_isCond
Definition: AtlasSemantics.py:18
python.AtlasSemantics.VarHandleArraySematics._ItemSemantics
Definition: AtlasSemantics.py:44
python.AtlasSemantics.VarHandleKeySemantics
Definition: AtlasSemantics.py:8
python.AtlasSemantics.VarHandleKeySemantics._mode
_mode
Definition: AtlasSemantics.py:21
python.AtlasSemantics.VarHandleArraySematics._ItemSemantics.store
def store(self, value)
Definition: AtlasSemantics.py:50
python.AtlasSemantics.VarHandleKeySemantics.store
def store(self, value)
Definition: AtlasSemantics.py:27
python.AtlasSemantics.VarHandleArraySematics
Definition: AtlasSemantics.py:38