Skip to content

Commit 0c794ca

Browse files
committed
Query the run timerange from GRPECS
It seems to be more reliable than taking this from RCT/Information. Some accompanying features: * ability to get CCDB object as JSON for direct treatment in python
1 parent 1634f69 commit 0c794ca

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

MC/bin/o2dpg_sim_workflow_anchored.py

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from copy import deepcopy
1010
import array as arr
1111
import os
12+
import requests
13+
import re
14+
import json
1215

1316
# Creates a time anchored MC workflow; positioned within a given run-number (as function of production size etc)
1417

@@ -23,7 +26,7 @@
2326

2427

2528
# this is PyROOT; enables reading ROOT C++ objects
26-
from ROOT import o2, TFile, TString, std
29+
from ROOT import o2, TFile, TString, TBufferJSON, TClass, std
2730

2831

2932
# these need to go into a module / support layer
@@ -52,14 +55,14 @@ def fetch(self, path, obj_type, timestamp=None, meta_info=None):
5255
"""
5356

5457
if not timestamp:
55-
timestamp = self.mgr.getTimestamp()
58+
timestamp = o2.ccdb.BasicCCDBManager.instance().getTimestamp()
5659
else:
57-
self.mgr.setTimestamp(timestamp)
60+
o2.ccdb.BasicCCDBManager.instance().setTimestamp(timestamp)
5861

5962
if not meta_info:
60-
obj = self.mgr.get[obj_type](path)
63+
obj = o2.ccdb.BasicCCDBManager.instance().get[obj_type](path)
6164
else:
62-
obj = self.mgr.getSpecific[obj_type](path, meta_info)
65+
obj = o2.ccdb.BasicCCDBManager.instance().get[obj_type](path, meta_info)
6366

6467
return timestamp, obj
6568

@@ -75,6 +78,7 @@ def fetch_header(self, path, timestamp=None):
7578
def retrieve_sor_eor(ccdbreader, run_number):
7679
"""
7780
retrieves start of run (sor) and end of run (eor) given a run number
81+
from the RCT/RunInformation table
7882
"""
7983

8084
path_run_info = "RCT/RunInformation"
@@ -86,6 +90,67 @@ def retrieve_sor_eor(ccdbreader, run_number):
8690
return {"SOR": int(header["SOR"]), "EOR": int(header["EOR"])}
8791

8892

93+
def retrieve_CCDBObject_asJSON(ccdbreader, path, timestamp):
94+
"""
95+
Retrieves a CCDB object as a JSON/dictionary.
96+
No need to know the type of the object a-priori.
97+
"""
98+
header = ccdbreader.fetch_header(path, timestamp)
99+
if not header:
100+
print(f"WARNING: Could not get header for path ${path} and timestamp {timestamp}")
101+
return None
102+
objtype=header["ObjectType"]
103+
print (objtype)
104+
ts, obj = ccdbreader.fetch(path, objtype, timestamp)
105+
print (obj)
106+
# convert object to json
107+
jsonTString = TBufferJSON.ConvertToJSON(obj, TClass.GetClass(objtype))
108+
return json.loads(jsonTString.Data())
109+
110+
def retrieve_sor_eor_fromGRPECS(ccdbreader, run_number):
111+
"""
112+
Retrieves start of run (sor) and end of run (eor) given a run number
113+
from via the GRPECS object. We first need to find the right object
114+
... but this is possible with a browsing request and meta_data filtering.
115+
"""
116+
117+
# make a simple HTTP request on the "browsing" endpoint
118+
url="http://alice-ccdb.cern.ch/browse/GLO/Config/GRPECS/run_number="+str(run_number)
119+
ansobject=requests.get(url)
120+
tokens=ansobject.text.split("\n")
121+
# look for the validity token
122+
123+
# look for the ID token
124+
ID=None
125+
VALIDITY=None
126+
for t in tokens:
127+
if t.count("ID:") > 0:
128+
ID=t.split(":")[1]
129+
if t.count("Validity:") > 0:
130+
VALIDITY=t.split(":")[1]
131+
if ID!=None and VALIDITY!=None:
132+
break
133+
134+
assert(ID != None)
135+
assert(VALIDITY != None)
136+
137+
match_object=re.match("\s*([0-9]*)\s*-\s*([0-9]*)\s*.*", VALIDITY)
138+
SOV = -1 # start of object validity (not necessarily the same as actual run-start)
139+
EOV = -1 # end of object validity (not the same as actual run-end)
140+
if match_object != None:
141+
SOV=match_object[1]
142+
EOV=match_object[2]
143+
144+
# we make a suitable request (at the start time) --> this gives the actual
145+
# object, with which we can query the end time as well
146+
grp=retrieve_CCDBObject_asJSON(ccdbreader, "/GLO/Config/GRPECS", int(SOV))
147+
148+
# check that this object is really the one we wanted based on run-number
149+
assert(int(grp["mRun"]) == int(run_number))
150+
151+
print ("SOR GRP (internal) is ", grp["mTimeStart"], " EOR is ", grp["mTimeEnd"])
152+
return {"SOR": int(grp["mTimeStart"]), "EOR": int(grp["mTimeEnd"])}
153+
89154
def retrieve_GRP(ccdbreader, timestamp):
90155
"""
91156
retrieves the GRP for a given time stamp
@@ -151,7 +216,8 @@ def main():
151216
# make a CCDB accessor object
152217
ccdbreader = CCDBAccessor(args.ccdb_url)
153218
# fetch the EOR/SOR
154-
sor_eor = retrieve_sor_eor(ccdbreader, args.run_number)
219+
# retrieve_sor_eor(ccdbreader, args.run_number) # <-- from RCT/Info
220+
sor_eor = retrieve_sor_eor_fromGRPECS(ccdbreader, args.run_number)
155221
if not sor_eor:
156222
print ("No time info found")
157223
sys.exit(1)

0 commit comments

Comments
 (0)