ATLAS Offline Software
RunLumiTime.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # RunLumiTime.py
4 #
5 # Simple utility script to take a range of run numbers and output a text list of times associated
6 # with the luminosity blocks in those runs.
7 #
8 # This is also a useful example of how to use the CoolLumiUtilities tools
9 #
10 import sys
11 from argparse import ArgumentParser
12 
13 # Get our global DB handler object
14 from CoolLumiUtilities.CoolDataReader import CoolDataReader
15 from CoolLumiUtilities.LumiDBHandler import LumiDBHandler
16 
18 
19  def __init__(self):
20 
21  # online luminosity database
22  self.onlLumiDB = 'COOLONL_TRIGGER/CONDBR2'
23 
24  # folder with LB -> time conversion
25  self.onlLBLBFolder = '/TRIGGER/LUMI/LBLB'
26 
27  # the LumiDBHandler
28  self.dbHandler = None
29 
30  # parse command-line input
31  args = self.parseArgs()
32 
33  # output level
34  self.verbose = args.verbose
35 
36  # output file (default stdout)
37  self.outFile = args.outfile
38 
39  # List of (integer) run numbers specified on the command line
40  self.runList = args.runlist
41 
42  print(f"Finished parsing run list: {', '.join([str(run) for run in self.runList])}")
43 
44  def __enter__(self):
45  # Instantiate the LumiDBHandler, so we can cleanup all COOL connections at exit
46  self.dbHandler = LumiDBHandler()
47  return self
48 
49  def __exit__(self, exc_type, exc_value, exc_traceback):
50  self.closeDb()
51 
52  def __del__(self):
53  self.closeDb()
54 
55  def closeDb(self):
56  if self.dbHandler is not None:
57  self.dbHandler.closeAllDB()
58 
59  def parseArgs(self):
60  parser = ArgumentParser()
61 
62  parser.add_argument("-v", "--verbose",
63  action="store_true", dest="verbose",
64  help="turn on verbose output")
65 
66  parser.add_argument("-r", "--run", nargs='*', required=True, type=int,
67  dest="runlist", metavar="RUN",
68  help="show specific run(s)")
69 
70  parser.add_argument('-o', '--output',
71  dest='outfile', metavar = "FILE", default=None, type=str,
72  help="write results to output file (default stdout). If filename ends in csv or json those formats are used.")
73 
74  return parser.parse_args()
75 
76  def execute(self):
77  # Instantiate the LumiDBHandler, so we can cleanup all COOL connections in the destructor
78  if self.dbHandler is None:
79  self.dbHandler = LumiDBHandler()
80 
81  # Open outfile if desired
82  fp = None
83  format = "stdout"
84  if self.outFile is not None:
85  fp = open(self.outFile, 'w')
86  format = "txt"
87  if self.outFile.endswith(".json"):
88  format = "json"
89  output = {}
90  if self.outFile.endswith(".csv"):
91  format = "csv"
92 
93 
94  # Get our COOL folder
95  lblb = CoolDataReader(self.onlLumiDB, self.onlLBLBFolder)
96 
97  # Load data for each run specified
98  for run in self.runList:
99 
100  lblb.setIOVRangeFromRun(run)
101  if not lblb.readData():
102  print(f'RunLumiTime - No LBLB data found for run {run}!')
103  continue
104 
105  for obj in lblb.data:
106  # IOV is equal to (Run << 32) + LB number.
107  run = obj.since() >> 32
108  lb = obj.since() & 0xFFFFFFFF
109  # Time is UTC nanoseconds
110  startTime = obj.payload()['StartTime']
111  endTime = obj.payload()['EndTime']
112  if format == "json":
113  if not run in output:
114  output[run] = []
115  output[run] += [{
116  "lb":lb,
117  "begin": startTime/1.e9,
118  "end": endTime/1.e9
119  }]
120  else:
121  entry = (run, lb, startTime/1.e9, endTime/1.e9)
122  separator = ',' if format=='csv' else ' '
123  print(separator.join([str(v) for v in entry]), file=fp)
124 
125  if format=="json":
126  import json
127  json.dump(output, fp, indent=4)
128 
129  # close the file
130  if fp is not None:
131  fp.close()
132  print(f"Wrote file {self.outFile}")
133 
134 # Executed from the command line
135 if __name__ == '__main__':
136  # rlt = RunLumiTime()
137  # sys.exit(rlt.execute())
138 
139  with RunLumiTime() as rlt:
140  rlt.execute()
141 
RunLumiTime.RunLumiTime.__del__
def __del__(self)
Definition: RunLumiTime.py:52
RunLumiTime.RunLumiTime.onlLBLBFolder
onlLBLBFolder
Definition: RunLumiTime.py:25
RunLumiTime.RunLumiTime
Definition: RunLumiTime.py:17
RunLumiTime.RunLumiTime.runList
runList
Definition: RunLumiTime.py:40
RunLumiTime.RunLumiTime.closeDb
def closeDb(self)
Definition: RunLumiTime.py:55
RunLumiTime.RunLumiTime.parseArgs
def parseArgs(self)
Definition: RunLumiTime.py:59
RunLumiTime.RunLumiTime.__enter__
def __enter__(self)
Definition: RunLumiTime.py:44
Trk::open
@ open
Definition: BinningType.h:40
RunLumiTime.RunLumiTime.onlLumiDB
onlLumiDB
Definition: RunLumiTime.py:22
RunLumiTime.RunLumiTime.__init__
def __init__(self)
Definition: RunLumiTime.py:19
RunLumiTime.RunLumiTime.execute
def execute(self)
Definition: RunLumiTime.py:76
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
RunLumiTime.RunLumiTime.outFile
outFile
Definition: RunLumiTime.py:37
str
Definition: BTagTrackIpAccessor.cxx:11
RunLumiTime.RunLumiTime.verbose
verbose
Definition: RunLumiTime.py:34
RunLumiTime.RunLumiTime.dbHandler
dbHandler
Definition: RunLumiTime.py:28
RunLumiTime.RunLumiTime.__exit__
def __exit__(self, exc_type, exc_value, exc_traceback)
Definition: RunLumiTime.py:49