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
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