ATLAS Offline Software
iovtype.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 from __future__ import division
4 
5 from collections import namedtuple
6 
7 from ..sugar import IOVMAX, RunLumi
8 
9 def restore_iov_type(name, fields, bases, content, empty, _memoized={}):
10  """
11  This would be better as a class method, but it's not possible to pickle
12  those, hence why this exists.
13  _memoized is used to store the type to avoid a function call to rebuild the
14  type un-necessarily
15  """
16  args = name, fields, bases, empty
17  if args not in _memoized:
18  # Don't take the last argument
19  iov_type = make_iov_type(*args[:-1])
20  _memoized[args] = iov_type._emptycls if empty else iov_type
21  return _memoized[args]._make(content)
22 
23 class IOVType(object):
24  """
25  A type which encapsulates an IoV
26  """
27  __slots__ = ()
28  _is_empty = False
29 
30 
31  def __reduce__(self):
32  """
33  Make IOVType pickleable
34  """
35  # Take all but the first base class, which is unpicklable
36  bases = type(self).__bases__[1:]
37  name = type(self).__name__
38  args = name, self._fields[2:], bases, tuple(self), self._is_empty
39  return restore_iov_type, args
40 
41  @classmethod
42  def empty(cls, since=None, until=None):
43  empty_fields = [since, until] + [None] * (len(cls._fields)-2)
44  return cls._emptycls(*empty_fields)
45 
46  @property
47  def runs(self):
48  """
49  Return a generator of runs
50  """
51  if self.until == IOVMAX:
52  return [self.since.run]
53  return range(self.since.run, self.until.run+1)
54 
55  @property
56  def run(self):
57  """
58  Returns the run the IoV started in
59  """
60  return self.since.run
61 
62  def contains_point(self, iovkey):
63  return self.since <= iovkey < self.until
64 
65  def intersect(self, iov):
66  if not self.intersects(iov):
67  return None
68  since, until = max(self.since, iov.since), min(self.until, iov.until)
69  if since == until:
70  return None
71  return self._replace(since=since, until=until)
72 
73  def intersects(self, iov):
74  return self.since < iov.until and self.until >= iov.since
75 
76  @property
77  def length(self):
78  return self.until - self.since if self.until != IOVMAX else 0
79 
80  def connected_to(self, rhs):
81  return self.until == rhs.since and self._contents == rhs._contents
82 
83  @property
84  def _contents(self):
85  return self[2+self._has_insertion_time:]
86 
87  def merge(self, rhs):
88  """
89  Assuming this and `rhs` are `connected_to` each other, then extend.
90  """
91  return self._replace(until=rhs.until)
92 
93  @property
94  def trimmed(self):
95  """
96  If this IoV starts on lumiblock 0, instead make it start from 1.
97  """
98  if self.since.lumi == 0:
99  return self._replace(since=self.since+1)
100  return self
101 
102  @property
103  def is_time_based(self):
104  return self.since.is_time
105 
106  def intersect_run(self, run_number):
107  runpart = run_number << 32
108  sincelo, sincehi = RunLumi(runpart | 1), RunLumi(runpart | 0xFFFFFFFF)
109  since, until = max(self.since, sincelo), min(self.until, sincehi)
110  if since >= until:
111  return None
112  return self._replace(since=since, until=until)
113 
114 def make_iov_type(name, variables, bases=(IOVType,), _memoized={}):
115  """
116  Create an IOV type and its empty type
117  """
118  args = name, tuple(variables), bases
119  if args in _memoized:
120  return _memoized[args]
121 
122  has_insertion_time = "insertion_time" in variables
123  has_channel = "channel" in variables
124  all_fields = " ".join(["since", "until"] + list(variables))
125 
126  class cls(namedtuple(name, all_fields)):
127  "Named tuple to store IoV types"
128 
129  cls.__name__ = name
130  cls.__bases__ += bases
131  cls._has_channel = has_channel
132  cls._has_insertion_time = has_insertion_time
133 
134  cls._emptycls = type(name + "_EMPTY", (cls,), dict(
135  _is_empty=True,
136  __bool__ = lambda self: False,
137  ))
138 
139  _memoized[args] = cls
140  return cls
141 
142 def define_iov_type(func):
143  """
144  A decorator which creates a lightweight iov type.
145  """
146  co_code = func.__code__ if hasattr(func, "__code__") else func.func_code
147  name = func.__name__
148  variables = co_code.co_varnames
149 
150  return make_iov_type(name, variables)
151 
152 @define_iov_type
154  "Just store a since, until"
python.sugar.iovtype.IOVType.trimmed
def trimmed(self)
Definition: iovtype.py:94
python.sugar.iovtype.IOVType.is_time_based
def is_time_based(self)
Definition: iovtype.py:103
max
#define max(a, b)
Definition: cfImp.cxx:41
python.sugar.iovtype.IOVType.merge
def merge(self, rhs)
Definition: iovtype.py:87
python.sugar.iovtype.RANGEIOV_VAL
def RANGEIOV_VAL()
Definition: iovtype.py:153
CaloClusterListBadChannel.cls
cls
Definition: CaloClusterListBadChannel.py:8
python.sugar.runlumi.RunLumi
RunLumi
Definition: runlumi.py:131
python.sugar.iovtype.IOVType.intersect_run
def intersect_run(self, run_number)
Definition: iovtype.py:106
python.sugar.iovtype.IOVType._is_empty
bool _is_empty
Definition: iovtype.py:28
python.sugar.iovtype.IOVType.intersect
def intersect(self, iov)
Definition: iovtype.py:65
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
min
#define min(a, b)
Definition: cfImp.cxx:40
python.sugar.iovtype.define_iov_type
def define_iov_type(func)
Definition: iovtype.py:142
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.sugar.iovtype.IOVType.contains_point
def contains_point(self, iovkey)
Definition: iovtype.py:62
python.sugar.iovtype.IOVType.runs
def runs(self)
Definition: iovtype.py:47
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
python.sugar.iovtype.IOVType.__reduce__
def __reduce__(self)
Definition: iovtype.py:31
python.sugar.iovtype.restore_iov_type
def restore_iov_type(name, fields, bases, content, empty, _memoized={})
Definition: iovtype.py:9
python.sugar.iovtype.IOVType.connected_to
def connected_to(self, rhs)
Definition: iovtype.py:80
python.sugar.iovtype.IOVType.length
def length(self)
Definition: iovtype.py:77
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
python.sugar.iovtype.make_iov_type
def make_iov_type(name, variables, bases=(IOVType,), _memoized={})
Definition: iovtype.py:114
python.sugar.iovtype.IOVType._contents
_contents
Definition: iovtype.py:81
pickleTool.object
object
Definition: pickleTool.py:30
python.sugar.iovtype.IOVType.empty
def empty(cls, since=None, until=None)
Definition: iovtype.py:42
python.sugar.iovtype.IOVType.until
until
Definition: iovtype.py:51
python.sugar.iovtype.IOVType
Definition: iovtype.py:23
python.sugar.iovtype.IOVType.run
def run(self)
Definition: iovtype.py:56
python.sugar.iovtype.IOVType.intersects
def intersects(self, iov)
Definition: iovtype.py:73