ATLAS Offline Software
Loading...
Searching...
No Matches
runlumi.py
Go to the documentation of this file.
1# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2
3
4from logging import getLogger; log = getLogger("DQUtils.sugar")
5
6from calendar import timegm
7
8from datetime import datetime
9date_from_timestamp = datetime.utcfromtimestamp
10
11RUN_MAX = 2**31-1
12LB_MAX = 2**32-1
13IOV_MAX = 2**63-1
14
15class 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
130RunLumi = RunLumiType.make
131IOVMIN = RunLumiType(0)
132IOVMAX = RunLumiType(2**63-1)
from_string(cls, date_string)
Definition runlumi.py:115
Definition run.py:1