ATLAS Offline Software
Loading...
Searching...
No Matches
TimeStampToRunLumi.py
Go to the documentation of this file.
1# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2
3
4def _ds2ts(datestamp, format):
5 from time import strptime
6 from calendar import timegm
7 try:
8 ts = strptime(datestamp, format)
9 return int(timegm(ts)) * 10**9
10 except ValueError:
11 raise ValueError(f"ERROR in time specification for TimeStampToRunLumi,"
12 f"it should be in format {format}")
13
14
15def TimeStampToRunLumi(tmstmp, guard=1, dbInstance="CONDBR2", format="%Y-%m-%d:%H:%M:%S/%Z", gettime=False):
16 """returns a (run, lumiblock) pair corresponding to the indicated timestamp;
17 it consists in the run started at most two days before the timestamp,
18 and still ongoing at that time.
19
20 Arguments:
21 tmstmp -- timestamp (numerical or text)
22 if numerical, interpreted as a unix timestamp (in nanoseconds)
23 if text, interpreted either as a date/time in the 'format' format
24 guard -- scales the default two-days window to search for runs in the DB
25 dbInstance -- DB instance used for the translation
26 format -- anything understood by time.strptime()
27 gettime -- if True, returns a triplet (run, LB, inferred timestamp in ns) instead of the default pair
28 """
29 from PyCool import cool
30 from time import asctime,localtime
31 if isinstance(tmstmp, str): tmstmp = _ds2ts(tmstmp, format)
32 dbSvc = cool.DatabaseSvcFactory.databaseService()
33 db=dbSvc.openDatabase("COOLONL_TRIGGER/"+dbInstance)
34 folder=db.getFolder("/TRIGGER/LUMI/LBTIME")
35 range=guard*24*60*60*1e9 # 2 days in ns
36 t1=int(tmstmp-range)
37 t2=int(tmstmp+range)
38 itr=folder.browseObjects(t1,t2,cool.ChannelSelection.all())
39 while itr.goToNext():
40 obj=itr.currentRef()
41 if obj.until()>tmstmp:
42 pl=obj.payload()
43 run=pl["Run"]
44 lb=pl["LumiBlock"]
45 print ("Found Run/Lumi [%i/%i] lasting from %s to %s" %\
46 (run,lb,asctime(localtime(obj.since()/1e9)),asctime(localtime(obj.until()/1e9))))
47 itr.close()
48 db.closeDatabase()
49 return ((run,lb), tmstmp) if gettime else (run,lb)
50 print ("WARNING: No run/lumi block found for time",asctime(localtime(tmstmp/1e9)),"in folder /TRIGGER/LUMI/LBTIME of DB COOLONL_TRIGGER/CONDBR2")
51 itr.close()
52 db.closeDatabase()
53 return (None, tmstmp) if gettime else None
54
55
56def fillInputFlags(flags, datestamp: str, infiniteRun=999999):
57 rlb, t = TimeStampToRunLumi(datestamp, gettime=True)
58 if rlb is None:
59 rlb = (infiniteRun, 0)
60 print(f"WARNING: failed to convert specified date/time into a run/lumiblock number. "
61 f"Using 'infinite' run-number {rlb[0]}")
62 flags.Input.RunNumbers = [rlb[0]]
63 flags.Input.LumiBlockNumbers = [rlb[1]]
64 flags.Input.TimeStamps = [int(t / 10**9)]
void print(char *figname, TCanvas *c1)
fillInputFlags(flags, str datestamp, infiniteRun=999999)