18 from __future__
import print_function
20 import sys, json, copy
21 from CoolConvUtilities
import AtlCoolLib
22 from PyCool
import cool
28 """A class to store database folder, its specification, and its tag at the same place, so they won't be mixed up"""
29 def __init__ (self, folder,folderName,spec,tag):
36 """Turns run number and lumibock into integrer needed by the database"""
40 """A function that will ensure a folder is in the database, it creates it if necessary. Also defines content of the folders. There are two folders: one for local constants and one for global constants. They both contain single entry of a long string"""
42 spec=cool.RecordSpecification()
45 spec.extend(
"data",cool.StorageType.String64k)
51 folder=AtlCoolLib.ensureFolder(db,folderName,spec,AtlCoolLib.athenaDesc(runLumi,
'CondAttrListCollection'),cool.FolderVersioning.MULTI_VERSION)
56 return folderBulk(folder, folderName, spec, tag)
59 """A class to create a dictionary, fill it with zeros in the constructor and to overwrite zeros later"""
61 if(folderBlk.folderName==
"/FWD/Onl/AFP/Align/Local"):
62 emptydict={
"stationID":0,
"layerID":-1,
"shiftX":0.0,
"shiftY":0.0,
"shiftZ":0.0,
"alpha":0.0,
"beta":0.0,
"gamma":0.0}
63 elif(folderBlk.folderName==
"/FWD/Onl/AFP/Align/Global"):
64 emptydict={
"stationID":0,
"alignType":
"None",
"shiftX":0.0,
"shiftY":0.0,
"shiftZ":0.0,
"alpha":0.0,
"beta":0.0,
"gamma":0.0}
66 print (
"unknown folder %s, please edit \"AFPDBDict\" class constructor, exiting now" % folderBlk.folderName)
70 "version":
"AFP_DB_v2",
72 "data": dict.fromkeys(
range(0, 16))}
74 for i
in range(0, 16):
75 self.
mydict[
"data"][i]=copy.deepcopy(emptydict)
76 self.
mydict[
"data"][i][
"stationID"]=i//4
77 if(folderBlk.folderName==
"/FWD/Onl/AFP/Align/Local"):
78 self.
mydict[
"data"][i][
"layerID"]=i%4
79 if(folderBlk.folderName==
"/FWD/Onl/AFP/Align/Global"):
80 if i%4==0: self.
mydict[
"data"][i][
"alignType"]=
"tracker"
81 if i%4==1: self.
mydict[
"data"][i][
"alignType"]=
"beam"
82 if i%4==2: self.
mydict[
"data"][i][
"alignType"]=
"RP"
83 if i%4==3: self.
mydict[
"data"][i][
"alignType"]=
"correction"
85 def append(self, stationID, layerID=-1, alignType="None", shiftX=0.0, shiftY=0.0, shiftZ=0.0, alpha=0.0, beta=0.0, gamma=0.0):
86 """A function that overwrites one slice of the alignment constants in the dictionary. Local constants have to have layerID defined, while alignType is undefined. Global constants have to have alignType defined (tracker/RP/beam/correction) and layerID undefined."""
89 if(layerID==-1
and alignType==
"None"):
90 print (
"cannot save payload, got layerID=%d and alignType=%s; one of them has to be specified" % layerID, alignType)
92 elif(layerID!=-1
and alignType!=
"None"):
93 print (
"cannot save payload, got layerID=%d and alignType=%s; one of them should not be specified" % layerID, alignType)
96 if(stationID<0
or stationID>=4):
97 print (
"cannot save payload, got stationID=%d, unknown" % stationID)
103 if(alignType!=
"None"):
105 if(alignType==
"tracker"):
107 elif(alignType==
"beam"):
109 elif(alignType==
"RP"):
111 elif(alignType==
"correction"):
114 print (
"cannot save payload, got alignType=%s, unknown" % alignType)
118 self.
mydict[
"data"][channel][
'alignType']=alignType
123 if(0<=layerID
and layerID<4):
126 print (
"cannot save payload, got layerID=%d, unknown" % layerID)
130 self.
mydict[
"data"][channel][
'layerID']=layerID
133 mydict_helper=self.
mydict[
"data"][channel]
134 mydict_helper[
'shiftX'], mydict_helper[
'shiftY'], mydict_helper[
'shiftZ'] = shiftX, shiftY, shiftZ
135 mydict_helper[
'alpha'], mydict_helper[
'beta'], mydict_helper[
'gamma'] = alpha, beta, gamma
136 mydict_helper[
'stationID']=stationID
140 """A function to transform the dictionary to JSON and save it in IOV from a given run and LB; upper limits are undefined and the maximum value (until beginning of a next entry) is used."""
144 print (
"cannot save payload, got fromRun=%d, it has to be positive" % fromRun)
149 until=cool.ValidityKeyMax
152 payload=cool.Record(folderBlk.spec)
154 payload[
"data"]=json.dumps(self.
mydict, indent = 1)
156 folderBlk.folder.storeObject(since,until,payload,0,folderBlk.tag)
160 dbFile =
"Example.db"
171 dbSvc = cool.DatabaseSvcFactory.databaseService()
174 dbString =
"sqlite://;schema=%s;dbname=%s" % (dbFile, dbName)
176 db = dbSvc.createDatabase(dbString)
177 except Exception
as e:
178 print (
'Problem creating database', e)
180 print (
"Created database", dbString)
183 folderBlkLoc_03=
makeFolderAndSpec(db,folderName=
"/FWD/Onl/AFP/Align/Local", tag=
"AFPAlignLoc-03")
184 folderBlkGlob_03=
makeFolderAndSpec(db,folderName=
"/FWD/Onl/AFP/Align/Global", tag=
"AFPAlignGlob-03")
194 myDict =
AFPDBDict(folderBlk=folderBlkLoc_03)
195 myDict.savePayload(folderBlk=folderBlkLoc_03, fromRun=fromRun)
199 myDict.append(stationID=0, layerID=1, shiftX=17.1313e-3, shiftY=-46.7438e-3, shiftZ=0.0000, alpha=2.9785e-3, beta=0.0000, gamma=0.0000)
200 myDict.append(stationID=0, layerID=2, shiftX=15.7960e-3, shiftY=-53.7707e-3, shiftZ=0.0000, alpha=3.3048e-3, beta=0.0000, gamma=0.0000)
201 myDict.append(stationID=0, layerID=3, shiftX=0.0000, shiftY=0.0000, shiftZ=0.0000, alpha=0.0000, beta=0.0000, gamma=0.0000)
202 myDict.append(stationID=1, layerID=1, shiftX=138.0070e-3, shiftY=6.0528e-3, shiftZ=0.0000, alpha=-1.4420e-3, beta=0.0000, gamma=0.0000)
203 myDict.append(stationID=1, layerID=2, shiftX=-80.6720e-3, shiftY=24.1310e-3, shiftZ=0.0000, alpha=-3.6605e-3, beta=0.0000, gamma=0.0000)
204 myDict.append(stationID=1, layerID=3, shiftX=-51.5111e-3, shiftY=-32.9151e-3, shiftZ=0.0000, alpha=1.0762e-3, beta=0.0000, gamma=0.0000)
205 myDict.append(stationID=2, layerID=1, shiftX=149.6927e-3, shiftY=103.5674e-3, shiftZ=0.0000, alpha=-3.9565e-3, beta=0.0000, gamma=0.0000)
206 myDict.append(stationID=2, layerID=2, shiftX=144.1316e-3, shiftY=88.0891e-3, shiftZ=0.0000, alpha=3.3219e-3, beta=0.0000, gamma=0.0000)
207 myDict.append(stationID=2, layerID=3, shiftX=47.8090e-3, shiftY=153.5737e-3, shiftZ=0.0000, alpha=5.1961e-3, beta=0.0000, gamma=0.0000)
208 myDict.append(stationID=3, layerID=1, shiftX=0.0000, shiftY=0.0000, shiftZ=0.0000, alpha=0.0000, beta=0.0000, gamma=0.0000)
209 myDict.append(stationID=3, layerID=2, shiftX=-153.0397e-3,shiftY=132.8483e-3, shiftZ=0.0000, alpha=-3.9787e-3, beta=0.0000, gamma=0.0000)
210 myDict.append(stationID=3, layerID=3, shiftX=13.9500e-3, shiftY=136.9500e-3, shiftZ=0.0000, alpha=0.3829e-3, beta=0.0000, gamma=0.0000)
211 myDict.savePayload(folderBlk=folderBlkLoc_03, fromRun=fromRun)
214 myDict =
AFPDBDict(folderBlk=folderBlkLoc_03)
215 myDict.savePayload(folderBlk=folderBlkLoc_03, fromRun=fromRun)
220 myDict =
AFPDBDict(folderBlk=folderBlkGlob_03)
221 myDict.append(stationID=0, alignType=
"RP", shiftX=-2.34, shiftY=-13.22)
222 myDict.append(stationID=1, alignType=
"RP", shiftX=-3.68, shiftY=-13.28)
223 myDict.append(stationID=2, alignType=
"RP", shiftX=-3.61, shiftY=-10.09)
224 myDict.append(stationID=3, alignType=
"RP", shiftX=-2.39, shiftY=-10.53)
225 myDict.savePayload(folderBlk=folderBlkGlob_03, fromRun=fromRun)
229 myDict =
AFPDBDict(folderBlk=folderBlkGlob_03)
230 myDict.append(stationID=0, alignType=
"tracker", shiftX=-0.5)
231 myDict.append(stationID=1, alignType=
"tracker", shiftX=-0.5)
232 myDict.append(stationID=2, alignType=
"tracker", shiftX=-0.5)
233 myDict.append(stationID=3, alignType=
"tracker", shiftX=-0.5)
234 myDict.append(stationID=0, alignType=
"beam", shiftX=-1.045)
235 myDict.append(stationID=1, alignType=
"beam", shiftX=-0.864)
236 myDict.append(stationID=2, alignType=
"beam", shiftX=-1.155)
237 myDict.append(stationID=3, alignType=
"beam", shiftX=-0.891)
238 myDict.append(stationID=0, alignType=
"RP", shiftX=-3.16)
239 myDict.append(stationID=1, alignType=
"RP", shiftX=-4.07)
240 myDict.append(stationID=2, alignType=
"RP", shiftX=-4.26)
241 myDict.append(stationID=3, alignType=
"RP", shiftX=-2.93)
242 myDict.append(stationID=0, alignType=
"correction", shiftX=-0.420)
243 myDict.append(stationID=1, alignType=
"correction", shiftX=-0.320)
244 myDict.append(stationID=2, alignType=
"correction", shiftX=-0.220)
245 myDict.append(stationID=3, alignType=
"correction", shiftX=-0.320)
246 myDict.savePayload(folderBlk=folderBlkGlob_03, fromRun=fromRun)
250 myDict =
AFPDBDict(folderBlk=folderBlkGlob_03)
251 myDict.append(stationID=0, alignType=
"tracker", shiftX=-0.5)
252 myDict.append(stationID=1, alignType=
"tracker", shiftX=-0.5)
253 myDict.append(stationID=2, alignType=
"tracker", shiftX=-0.5)
254 myDict.append(stationID=3, alignType=
"tracker", shiftX=-0.5)
255 myDict.append(stationID=0, alignType=
"beam", shiftX=-1.045)
256 myDict.append(stationID=1, alignType=
"beam", shiftX=-0.864)
257 myDict.append(stationID=2, alignType=
"beam", shiftX=-1.155)
258 myDict.append(stationID=3, alignType=
"beam", shiftX=-0.891)
259 myDict.append(stationID=0, alignType=
"RP", shiftX=-2.65)
260 myDict.append(stationID=1, alignType=
"RP", shiftX=-3.57)
261 myDict.append(stationID=2, alignType=
"RP", shiftX=-3.76)
262 myDict.append(stationID=3, alignType=
"RP", shiftX=-2.43)
263 myDict.append(stationID=0, alignType=
"correction", shiftX=-0.420)
264 myDict.append(stationID=1, alignType=
"correction", shiftX=-0.320)
265 myDict.append(stationID=2, alignType=
"correction", shiftX=-0.220)
266 myDict.append(stationID=3, alignType=
"correction", shiftX=-0.320)
267 myDict.savePayload(folderBlk=folderBlkGlob_03, fromRun=fromRun)
271 myDict =
AFPDBDict(folderBlk=folderBlkGlob_03)
272 myDict.append(stationID=0, alignType=
"tracker", shiftX=-0.5)
273 myDict.append(stationID=1, alignType=
"tracker", shiftX=-0.5)
274 myDict.append(stationID=2, alignType=
"tracker", shiftX=-0.5)
275 myDict.append(stationID=3, alignType=
"tracker", shiftX=-0.5)
276 myDict.append(stationID=0, alignType=
"beam", shiftX=-1.045)
277 myDict.append(stationID=1, alignType=
"beam", shiftX=-0.864)
278 myDict.append(stationID=2, alignType=
"beam", shiftX=-1.155)
279 myDict.append(stationID=3, alignType=
"beam", shiftX=-0.891)
280 myDict.append(stationID=0, alignType=
"RP", shiftX=-2.38)
281 myDict.append(stationID=1, alignType=
"RP", shiftX=-3.60)
282 myDict.append(stationID=2, alignType=
"RP", shiftX=-3.87)
283 myDict.append(stationID=3, alignType=
"RP", shiftX=-2.23)
284 myDict.append(stationID=0, alignType=
"correction", shiftX=-0.420)
285 myDict.append(stationID=1, alignType=
"correction", shiftX=-0.320)
286 myDict.append(stationID=2, alignType=
"correction", shiftX=-0.220)
287 myDict.append(stationID=3, alignType=
"correction", shiftX=-0.320)
288 myDict.savePayload(folderBlk=folderBlkGlob_03, fromRun=fromRun)
292 myDict =
AFPDBDict(folderBlk=folderBlkGlob_03)
293 myDict.append(stationID=0, alignType=
"RP", shiftX=-2.34, shiftY=-13.22)
294 myDict.append(stationID=1, alignType=
"RP", shiftX=-3.68, shiftY=-13.28)
295 myDict.append(stationID=2, alignType=
"RP", shiftX=-6.46, shiftY=-5.47)
296 myDict.append(stationID=3, alignType=
"RP", shiftX=-6.79, shiftY=-5.03)
297 myDict.savePayload(folderBlk=folderBlkGlob_03, fromRun=fromRun)
300 myDict =
AFPDBDict(folderBlk=folderBlkGlob_03)
301 myDict.append(stationID=0, alignType=
"tracker", shiftX=-0.5)
302 myDict.append(stationID=1, alignType=
"tracker", shiftX=-0.5)
303 myDict.append(stationID=2, alignType=
"tracker", shiftX=-0.5)
304 myDict.append(stationID=3, alignType=
"tracker", shiftX=-0.5)
305 myDict.append(stationID=0, alignType=
"RP", shiftX=-1.509)
306 myDict.append(stationID=1, alignType=
"RP", shiftX=-2.315)
307 myDict.append(stationID=2, alignType=
"RP", shiftX=-2.325)
308 myDict.append(stationID=3, alignType=
"RP", shiftX=-1.5)
309 myDict.savePayload(folderBlk=folderBlkGlob_03, fromRun=fromRun)
311 print (
"\nClose database")
315 if __name__==
"__main__":