ATLAS Offline Software
Loading...
Searching...
No Matches
AFPAlignMCDBCreate.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
4
5# file AFPAlignMCDBCreate.py
6# author Petr Balek <petr.balek@cern.ch>
7# date 2021-11-10
8#
9# brief A python script that generates COOL database with alignment constants for AFP. A lot of inspiration obtained in InnerDetector/InDetConditions/InDetBeamSpotService/utils/beamSpot_set.py.
10# This version of DB is intended to be used in MC simulations. Each tag has one entry with IOV from 0 to infinite. The tags reflect various alignments through data-taking
11# usage 0. setup Athena environment (setupATLAS, asetup ...)
12# 1. run this script:
13# python AFPAlignMCDBCreate.py
14# 2. check the output:
15# AtlCoolConsole.py "sqlite://;schema=ExampleMC.db;dbname=OFLP200"
16# 3. check "testAFPDB.py" for more testing
17
18
19
20import sys, json, copy
21from CoolConvUtilities import AtlCoolLib
22from PyCool import cool
23
24# AFPDBBase is no longer needed, partially replaced by AtlCoolLib
25#from AFP_DBTools.AFPDBBase import AFPDBRecordBase, AFPDBTableBase
26
27class folderBulk():
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):
30 self.folder=folder
31 self.folderName=folderName
32 self.spec=spec
33 self.tag=tag
34
35def runLBtoDB(run,lb):
36 """Turns run number and lumibock into integrer needed by the database"""
37 return (run<<32) + lb
38
39def makeFolderAndSpec(db,folderName,tag):
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"""
41
42 spec=cool.RecordSpecification()
43
44 # each folder contain just a single entry, this string will be JSON entry
45 spec.extend("data",cool.StorageType.String64k)
46
47 runLumi=True
48
49 # if there are more channels, use "CondAttrListCollection"; if there is only one channel, use "AthenaAttributeList"
50 # it's also possible to use "CondAttrListCollection" if there's only one channel
51 folder=AtlCoolLib.ensureFolder(db,folderName,spec,AtlCoolLib.athenaDesc(runLumi,'CondAttrListCollection'),cool.FolderVersioning.MULTI_VERSION)
52
53 if(folder is None):
54 sys.exit(1)
55
56 return folderBulk(folder, folderName, spec, tag)
57
58class AFPDBDict():
59 """A class to create a dictionary, fill it with zeros in the constructor and to overwrite zeros later"""
60 def __init__ (self, folderBlk):
61 if(folderBlk.folderName=="/FWD/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/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}
65 else:
66 print ("unknown folder %s, please edit \"AFPDBDict\" class constructor, exiting now" % folderBlk.folderName)
67 sys.exit(1)
68
69 self.mydict={"author":"Petr Balek",
70 "version":"AFP_DB_v2",
71 "nchannels":16,
72 "data": dict.fromkeys(range(0, 16))}
73
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/AFP/Align/Local"):
78 self.mydict["data"][i]["layerID"]=i%4
79 if(folderBlk.folderName=="/FWD/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"
84
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."""
87
88 # perform some simple check so basic mistakes are avoided
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)
91 sys.exit(1)
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)
94 sys.exit(1)
95
96 if(stationID<0 or stationID>=4):
97 print ("cannot save payload, got stationID=%d, unknown" % stationID)
98 sys.exit(1)
99
100 channel=0
101 # station 0 occupies channels 0-3, station 1 occupies channels 4-7, ..., and station 3 occupies channels 12-15
102 # if you are insterested in e.g. "beam" constants, look at channels 1, 5, 9, and 13
103 if(alignType!="None"):
104 channel=stationID*4
105 if(alignType=="tracker"):
106 channel+=0
107 elif(alignType=="beam"):
108 channel+=1
109 elif(alignType=="RP"):
110 channel+=2
111 elif(alignType=="correction"):
112 channel+=3
113 else:
114 print ("cannot save payload, got alignType=%s, unknown" % alignType)
115 sys.exit(1)
116
117 # overwrite it
118 self.mydict["data"][channel]['alignType']=alignType
119
120 # station 0 again occupies channels 0-3 etc.
121 if(layerID!=-1):
122 channel=stationID*4
123 if(0<=layerID and layerID<4):
124 channel+=layerID
125 else:
126 print ("cannot save payload, got layerID=%d, unknown" % layerID)
127 sys.exit(1)
128
129 # overwrite it
130 self.mydict["data"][channel]['layerID']=layerID
131
132 # overwrite it
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
137
138
139 def savePayload(self, folderBlk):
140 """A function to transform the dictionary to JSON and save it in IOV from 0 to infinity; technically, upper limits are undefined and the maximum value (until beginning of a next entry) would be used."""
141
142 # transform run nr. and LB to the right integers
143 since=runLBtoDB(0,0)
144 until=cool.ValidityKeyMax
145
146 # provided set of constants have to be the right one for the provided specification
147 payload=cool.Record(folderBlk.spec)
148 # transform dictionary to JSON
149 payload["data"]=json.dumps(self.mydict, indent = 1)
150 # save everything (that "0" stands for channel nr. 0, there is only 1 real channel in the DB)
151 folderBlk.folder.storeObject(since,until,payload,0,folderBlk.tag)
152
153
154def main():
155 dbFile = "ExampleMC.db"
156 dbName = "OFLP200"
157
158 # remove the old db file so that we can write the new one
159 try:
160 import os
161 os.remove(dbFile)
162 except IOError:
163 pass
164
165 # get database service and open database
166 dbSvc = cool.DatabaseSvcFactory.databaseService()
167
168 # database accessed via physical name
169 dbString = "sqlite://;schema=%s;dbname=%s" % (dbFile, dbName)
170 try:
171 db = dbSvc.createDatabase(dbString)
172 except Exception as e:
173 print ('Problem creating database', e)
174 sys.exit(-1)
175 print ("Created database", dbString)
176
177 # define the folder in the database, its specification and its tag
178 folderBlkLoc_ideal_01 =makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Local", tag="AFPMCAlignLoc-ideal-01")
179 folderBlkLoc_329484_01=makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Local", tag="AFPMCAlignLoc-329484-01")
180 folderBlkLoc_329484_02=makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Local", tag="AFPMCAlignLoc-329484-02")
181
182 folderBlkGlob_ideal_01 =makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Global", tag="AFPMCAlignGlob-ideal-01")
183 folderBlkGlob_203302_01=makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Global", tag="AFPMCAlignGlob-203302-01")
184 folderBlkGlob_329484_01=makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Global", tag="AFPMCAlignGlob-329484-01")
185 folderBlkGlob_331020_01=makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Global", tag="AFPMCAlignGlob-331020-01")
186 folderBlkGlob_336288_01=makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Global", tag="AFPMCAlignGlob-336288-01")
187 folderBlkGlob_347955_01=makeFolderAndSpec(db,folderName="/FWD/AFP/Align/Global", tag="AFPMCAlignGlob-347955-01")
188
189
190 # local
191 myDict = AFPDBDict(folderBlkLoc_ideal_01)
192 myDict.savePayload(folderBlk=folderBlkLoc_ideal_01)
193
194
195 myDict = AFPDBDict(folderBlkLoc_329484_01)
196 myDict.append(stationID=0, layerID=1, shiftX=17.1871e-3, shiftY=-31.4828e-3, shiftZ=0.0000, alpha=2.8832e-3,beta=0.0000, gamma=0.0000)
197 myDict.append(stationID=0, layerID=2, shiftX=15.2353e-3, shiftY=-51.4641e-3, shiftZ=0.0000, alpha=3.1571e-3,beta=0.0000, gamma=0.0000)
198 myDict.append(stationID=0, layerID=3, shiftX=0.0000, shiftY=0.0000, shiftZ=0.0000, alpha=0.0000, beta=0.0000, gamma=0.0000)
199 myDict.append(stationID=1, layerID=1, shiftX=137.0504e-3, shiftY=5.5895e-3, shiftZ=0.0000, alpha=-1.5424e-3,beta=0.0000, gamma=0.0000)
200 myDict.append(stationID=1, layerID=2, shiftX=-81.5657e-3, shiftY=21.9504e-3, shiftZ=0.0000, alpha=-3.4834e-3,beta=0.0000, gamma=0.0000)
201 myDict.append(stationID=1, layerID=3, shiftX=-52.5330e-3, shiftY=-31.8738e-3, shiftZ=0.0000, alpha=0.9359e-3, beta=0.0000, gamma=0.0000)
202 myDict.append(stationID=2, layerID=1, shiftX=148.6858e-3, shiftY=104.3183e-3, shiftZ=0.0000, alpha=-3.3322e-3,beta=0.0000, gamma=0.0000)
203 myDict.append(stationID=2, layerID=2, shiftX=150.8107e-3, shiftY=82.0611e-3, shiftZ=0.0000, alpha=4.2123e-3, beta=0.0000, gamma=0.0000)
204 myDict.append(stationID=2, layerID=3, shiftX=116.9848e-3, shiftY=113.5771e-3, shiftZ=0.0000, alpha=29.2103e-3,beta=0.0000, gamma=0.0000)
205 myDict.append(stationID=3, layerID=1, shiftX=-184.2146e-3,shiftY=55.3273e-3, shiftZ=0.0000, alpha=-0.7655e-3,beta=0.0000, gamma=0.0000)
206 myDict.append(stationID=3, layerID=2, shiftX=-154.2151e-3,shiftY=133.8304e-3, shiftZ=0.0000, alpha=-3.2126e-3,beta=0.0000, gamma=0.0000)
207 myDict.append(stationID=3, layerID=3, shiftX=13.2748e-3, shiftY=138.4570e-3, shiftZ=0.0000, alpha=0.5984e-3, beta=0.0000, gamma=0.0000)
208 myDict.savePayload(folderBlk=folderBlkLoc_329484_01)
209
210
211 myDict = AFPDBDict(folderBlkLoc_329484_02)
212 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)
213 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)
214 myDict.append(stationID=0, layerID=3, shiftX=0.0000, shiftY=0.0000, shiftZ=0.0000, alpha=0.0000, beta=0.0000, gamma=0.0000)
215 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)
216 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)
217 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)
218 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)
219 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)
220 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)
221 myDict.append(stationID=3, layerID=1, shiftX=0.0000, shiftY=0.0000, shiftZ=0.0000, alpha=0.0000, beta=0.0000, gamma=0.0000)
222 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)
223 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)
224 myDict.savePayload(folderBlk=folderBlkLoc_329484_02)
225
226
227 # global
228 myDict = AFPDBDict(folderBlkGlob_ideal_01)
229 myDict.savePayload(folderBlk=folderBlkGlob_ideal_01)
230
231
232 myDict = AFPDBDict(folderBlk=folderBlkGlob_203302_01)
233 myDict.append(stationID=0, alignType="RP", shiftX=-2.34, shiftY=-13.22)
234 myDict.append(stationID=1, alignType="RP", shiftX=-3.68, shiftY=-13.28)
235 myDict.append(stationID=2, alignType="RP", shiftX=-3.61, shiftY=-10.09)
236 myDict.append(stationID=3, alignType="RP", shiftX=-2.39, shiftY=-10.53)
237 myDict.savePayload(folderBlk=folderBlkGlob_203302_01)
238
239
240 myDict = AFPDBDict(folderBlk=folderBlkGlob_329484_01)
241 myDict.append(stationID=0, alignType="tracker", shiftX=-0.5)
242 myDict.append(stationID=1, alignType="tracker", shiftX=-0.5)
243 myDict.append(stationID=2, alignType="tracker", shiftX=-0.5)
244 myDict.append(stationID=3, alignType="tracker", shiftX=-0.5)
245 myDict.append(stationID=0, alignType="beam", shiftX=-1.045)
246 myDict.append(stationID=1, alignType="beam", shiftX=-0.864)
247 myDict.append(stationID=2, alignType="beam", shiftX=-1.155)
248 myDict.append(stationID=3, alignType="beam", shiftX=-0.891)
249 myDict.append(stationID=0, alignType="RP", shiftX=-3.16)
250 myDict.append(stationID=1, alignType="RP", shiftX=-4.07)
251 myDict.append(stationID=2, alignType="RP", shiftX=-4.26)
252 myDict.append(stationID=3, alignType="RP", shiftX=-2.93)
253 myDict.append(stationID=0, alignType="correction", shiftX=-0.420)
254 myDict.append(stationID=1, alignType="correction", shiftX=-0.320)
255 myDict.append(stationID=2, alignType="correction", shiftX=-0.220)
256 myDict.append(stationID=3, alignType="correction", shiftX=-0.320)
257 myDict.savePayload(folderBlk=folderBlkGlob_329484_01)
258
259
260 myDict = AFPDBDict(folderBlk=folderBlkGlob_331020_01)
261 myDict.append(stationID=0, alignType="tracker", shiftX=-0.5)
262 myDict.append(stationID=1, alignType="tracker", shiftX=-0.5)
263 myDict.append(stationID=2, alignType="tracker", shiftX=-0.5)
264 myDict.append(stationID=3, alignType="tracker", shiftX=-0.5)
265 myDict.append(stationID=0, alignType="beam", shiftX=-1.045)
266 myDict.append(stationID=1, alignType="beam", shiftX=-0.864)
267 myDict.append(stationID=2, alignType="beam", shiftX=-1.155)
268 myDict.append(stationID=3, alignType="beam", shiftX=-0.891)
269 myDict.append(stationID=0, alignType="RP", shiftX=-2.65)
270 myDict.append(stationID=1, alignType="RP", shiftX=-3.57)
271 myDict.append(stationID=2, alignType="RP", shiftX=-3.76)
272 myDict.append(stationID=3, alignType="RP", shiftX=-2.43)
273 myDict.append(stationID=0, alignType="correction", shiftX=-0.420)
274 myDict.append(stationID=1, alignType="correction", shiftX=-0.320)
275 myDict.append(stationID=2, alignType="correction", shiftX=-0.220)
276 myDict.append(stationID=3, alignType="correction", shiftX=-0.320)
277 myDict.savePayload(folderBlk=folderBlkGlob_331020_01)
278
279
280 myDict = AFPDBDict(folderBlk=folderBlkGlob_336288_01)
281 myDict.append(stationID=0, alignType="tracker", shiftX=-0.5)
282 myDict.append(stationID=1, alignType="tracker", shiftX=-0.5)
283 myDict.append(stationID=2, alignType="tracker", shiftX=-0.5)
284 myDict.append(stationID=3, alignType="tracker", shiftX=-0.5)
285 myDict.append(stationID=0, alignType="beam", shiftX=-1.045)
286 myDict.append(stationID=1, alignType="beam", shiftX=-0.864)
287 myDict.append(stationID=2, alignType="beam", shiftX=-1.155)
288 myDict.append(stationID=3, alignType="beam", shiftX=-0.891)
289 myDict.append(stationID=0, alignType="RP", shiftX=-2.38)
290 myDict.append(stationID=1, alignType="RP", shiftX=-3.60)
291 myDict.append(stationID=2, alignType="RP", shiftX=-3.87)
292 myDict.append(stationID=3, alignType="RP", shiftX=-2.23)
293 myDict.append(stationID=0, alignType="correction", shiftX=-0.420)
294 myDict.append(stationID=1, alignType="correction", shiftX=-0.320)
295 myDict.append(stationID=2, alignType="correction", shiftX=-0.220)
296 myDict.append(stationID=3, alignType="correction", shiftX=-0.320)
297 myDict.savePayload(folderBlk=folderBlkGlob_336288_01)
298
299
300 myDict = AFPDBDict(folderBlk=folderBlkGlob_347955_01)
301 myDict.append(stationID=0, alignType="RP", shiftX=-2.34, shiftY=-13.22)
302 myDict.append(stationID=1, alignType="RP", shiftX=-3.68, shiftY=-13.28)
303 myDict.append(stationID=2, alignType="RP", shiftX=-6.46, shiftY=-5.47)
304 myDict.append(stationID=3, alignType="RP", shiftX=-6.79, shiftY=-5.03)
305 myDict.savePayload(folderBlk=folderBlkGlob_347955_01)
306
307 print ("\nClose database")
308 db.closeDatabase()
309
310
311if __name__=="__main__":
312 main()
if(febId1==febId2)
__init__(self, folder, folderName, spec, tag)
makeFolderAndSpec(db, folderName, tag)