ATLAS Offline Software
runlumi.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 
4 from logging import getLogger; log = getLogger("DQUtils.sugar")
5 
6 from calendar import timegm
7 
8 from datetime import datetime
9 date_from_timestamp = datetime.utcfromtimestamp
10 
11 RUN_MAX = 2**31-1
12 LB_MAX = 2**32-1
13 IOV_MAX = 2**63-1
14 
15 class IovKey(int):
16  __slots__ = ()
17 
19  """
20  Syntactic sugar for an IoV key.
21 
22  This class intentionally has no constructor so that we can benefit from the
23  speed of the builtin long constructor. (Since we use it a lot!)
24 
25  On the python side though, it is useful to have a constructor which takes
26  (Run, Lumiblock). This is provided by the make() class method, which is
27  aliased as RunLumi.
28  """
29  __slots__ = ()
30 
31  is_time = False
32 
33  @classmethod
34  def make(cls, *args):
35  """
36  Create a RunLumiType in a smarter (but slower) way, depending on whether
37  one or two args are passed.
38  """
39  if len(args) == 1:
40  if isinstance(args[0], tuple):
41  run, lumi = args[0]
42  return cls(run << 32 | lumi)
43  return cls(args[0])
44  elif len(args) == 2:
45  run, lumi = args
46  return cls(run << 32 | lumi)
47  raise RuntimeError("Invalid argument to RunLumiType.make")
48 
49  def __repr__(self):
50  """
51  Pretty representation for an IoV key
52  """
53  run, lumi = self.run, self.lumi
54  if run == RUN_MAX: run = "[MAX]"
55  if lumi == LB_MAX: lumi = "[MAX]"
56  return "%5s:%5s" % (run, lumi)
57 
58  def __str__(self):
59  return self.__repr__()
60 
61  def __add__(self, what):
62  """
63  How to add something to an LB: in case of a numeric type, do the normal
64  thing. In case of a tuple, use the first element as a run number count
65  and the second element as a luminosity block count.
66  """
67  if isinstance(what, tuple):
68  nrun, nlumi = what
69  return RunLumiType((self.run + nrun << 32) + (self.lumi + nlumi))
70 
71  return RunLumiType(int.__add__(self, what))
72 
73  def __sub__(self, what):
74  return RunLumiType(int.__sub__(self, what))
75 
76  def start_nextrun(self):
77  """
78  Return a new RunLumiType with run number += 1 and lumi = 1
79  """
80  return self + (1, -self.lumi) + (0, 1)
81 
82  @property
83  def run(self):
84  return self >> 32
85 
86  @property
87  def lumi(self):
88  return self & 0xFFFFFFFF
89 
91  __slots__ = ()
92  is_time = True
93 
94  def __repr__(self):
95  return str(self.date) + " UTC"
96 
97  def __str__(self):
98  return self.__repr__()
99 
100  @property
101  def date(self):
102  if self == IOV_MAX:
103  return "([MAX]:[MAX])"
104  try:
105  return date_from_timestamp(self/1e9)
106  except ValueError:
107  return "[InvalidDate]"
108 
109  @classmethod
110  def from_date(cls, date):
111  return cls((timegm(date.utctimetuple()) * 1000000000)
112  + 1000*date.microsecond)
113 
114  @classmethod
115  def from_string(cls, date_string):
116  """
117  Create a TimestampType IOVKey from a %d/%m/%Y string or %H:%M:%S
118  """
119  try:
120  d = datetime.strptime(date_string, "%d/%m/%Y")
121  except ValueError:
122  try:
123  d = datetime.strptime(date_string, "%d/%m/%Y %H:%M:%S")
124  except ValueError:
125  log.exception("Invalid")
126  raise
127 
128  return cls.from_date(d)
129 
130 RunLumi = RunLumiType.make
131 IOVMIN = RunLumiType(0)
132 IOVMAX = RunLumiType(2**63-1)
python.sugar.runlumi.TimestampType.date
def date(self)
Definition: runlumi.py:101
python.sugar.runlumi.RunLumiType.run
def run(self)
Definition: runlumi.py:83
python.sugar.runlumi.RunLumiType.make
def make(cls, *args)
Definition: runlumi.py:34
python.sugar.runlumi.TimestampType.__str__
def __str__(self)
Definition: runlumi.py:97
CaloClusterListBadChannel.cls
cls
Definition: CaloClusterListBadChannel.py:8
python.sugar.runlumi.TimestampType.__repr__
def __repr__(self)
Definition: runlumi.py:94
python.sugar.runlumi.RunLumiType.__add__
def __add__(self, what)
Definition: runlumi.py:61
python.sugar.runlumi.TimestampType
Definition: runlumi.py:90
python.sugar.runlumi.date_from_timestamp
date_from_timestamp
Definition: runlumi.py:9
python.sugar.runlumi.RunLumiType.start_nextrun
def start_nextrun(self)
Definition: runlumi.py:76
python.sugar.runlumi.RunLumiType.__repr__
def __repr__(self)
Definition: runlumi.py:49
python.sugar.runlumi.TimestampType.from_date
def from_date(cls, date)
Definition: runlumi.py:110
python.sugar.runlumi.RunLumiType
Definition: runlumi.py:18
python.sugar.runlumi.IovKey
Definition: runlumi.py:15
python.sugar.runlumi.RunLumiType.__str__
def __str__(self)
Definition: runlumi.py:58
python.sugar.runlumi.RunLumiType.__sub__
def __sub__(self, what)
Definition: runlumi.py:73
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.sugar.runlumi.TimestampType.from_string
def from_string(cls, date_string)
Definition: runlumi.py:115
str
Definition: BTagTrackIpAccessor.cxx:11
python.sugar.runlumi.RunLumiType.lumi
def lumi(self)
Definition: runlumi.py:87
python.CaloCondLogger.getLogger
def getLogger(name="CaloCond")
Definition: CaloCondLogger.py:16