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