18 import sys, json, copy
19 from CoolConvUtilities
import AtlCoolLib
20 from PyCool
import cool
23 """A class to store database folder, its specification, and its tag at the same place, so they won't be mixed up"""
24 def __init__ (self, folder,folderName,spec,tag):
31 """Turns run number and lumibock into integrer needed by the database"""
35 """A function that will ensure a folder is in the database, it creates it if necessary. Also defines content of the folders. It contains only a single entry of a long string"""
37 spec=cool.RecordSpecification()
40 spec.extend(
"data",cool.StorageType.String64k)
44 folder=AtlCoolLib.ensureFolder(db,folderName,spec,AtlCoolLib.athenaDesc(
True,
'CondAttrListCollection'),cool.FolderVersioning.MULTI_VERSION)
49 return folderBulk(folder, folderName, spec, tag)
52 """A class to create a dictionary, fill it with zeros in the constructor and to overwrite zeros later"""
54 if(folderBlk.folderName==
"/FWD/AFP/ToFParameters/Local"):
55 emptydict={
"stationID":0,
"trainID":-1,
"barID":-1,
"barWeight":1.0,
"barTimeOffset":0.0}
57 elif(folderBlk.folderName==
"/FWD/AFP/ToFParameters/Vertex"):
58 emptydict={
"stationID":0,
"timeGlobalOffset":0.0,
"timeOffset":(0.0,0.0,0.0,0.0),
"timeSlope":(0.0,0.0,0.0,0.0),
"trainEdge":(0.0,0.0,0.0,0.0,0.0)}
61 print (
"unknown folder %s, please edit \"AFPDBDict\" class constructor, exiting now" % folderBlk.folderName)
65 "version":
"AFP_DB_v2",
66 "nchannels":nchannels,
67 "data": dict.fromkeys(
range(0, nchannels))}
69 for i
in range(0, nchannels):
70 self.
mydict[
"data"][i]=copy.deepcopy(emptydict)
71 if(folderBlk.folderName==
"/FWD/AFP/ToFParameters/Local"):
72 self.
mydict[
"data"][i][
"stationID"]=(i//16)*3
73 self.
mydict[
"data"][i][
"trainID"]=(i%16)//4
74 self.
mydict[
"data"][i][
"barID"]=i%4
75 if(folderBlk.folderName==
"/FWD/AFP/ToFParameters/Vertex"):
76 self.
mydict[
"data"][i][
"stationID"]=i*3
78 def append(self, stationID, trainID=-1, barID=-1, barWeight=1.0, barTimeOffset=0.0, timeGlobalOffset=0.0, timeOffset=(0.,0.,0.,0.), timeSlope=(0.,0.,0.,0.), trainEdge=(0.,0.,0.,0.,0.)):
79 """A function that overwrites one slice of the ToF parameters in the dictionary. Local ToF parameters have to have both trainID and barID defined, Vertex ToF parameters neither."""
82 if(stationID!=0
and stationID!=3):
83 print (
"cannot save payload, got stationID=%d, unknown" % stationID)
86 trainValid=
True if 0<=trainID
and trainID<4
else False
87 barValid =
True if 0<=barID
and barID<4
else False
88 if(trainValid!=barValid):
90 print (
"cannot save payload, got trainID=%d, barID=%d" % (trainID,barID))
98 channel=(stationID//3)*16+trainID*4+barID
101 mydict_helper=self.
mydict[
"data"][channel]
102 mydict_helper[
'stationID'], mydict_helper[
'trainID'], mydict_helper[
'barID'] = stationID, trainID, barID
103 mydict_helper[
'barWeight'], mydict_helper[
'barTimeOffset'] = barWeight, barTimeOffset
110 mydict_helper=self.
mydict[
"data"][channel]
111 mydict_helper[
'stationID']=stationID
112 mydict_helper[
'timeGlobalOffset'], mydict_helper[
'timeOffset'] = timeGlobalOffset, timeOffset
113 mydict_helper[
'timeSlope'], mydict_helper[
'trainEdge'] = timeSlope, trainEdge
117 """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) is used."""
121 until=cool.ValidityKeyMax
124 payload=cool.Record(folderBlk.spec)
126 payload[
"data"]=json.dumps(self.
mydict, indent = 1)
128 folderBlk.folder.storeObject(since,until,payload,0,folderBlk.tag)
132 dbFile =
"ExampleMC.db"
143 dbSvc = cool.DatabaseSvcFactory.databaseService()
146 dbString =
"sqlite://;schema=%s;dbname=%s" % (dbFile, dbName)
148 db = dbSvc.createDatabase(dbString)
149 except Exception
as e:
150 print (
'Problem creating database', e)
152 print (
"Created database", dbString)
155 folderBlkLoc_ideal_01=
makeFolderAndSpec(db,folderName=
"/FWD/AFP/ToFParameters/Local", tag=
"AFPMCToFLoc-ideal-01")
156 folderBlkVtx_ideal_01=
makeFolderAndSpec(db,folderName=
"/FWD/AFP/ToFParameters/Vertex", tag=
"AFPMCToFVtx-ideal-01")
159 myDict =
AFPDBDict(folderBlkLoc_ideal_01)
160 myDict.append(stationID=0, trainID=0, barID=0, barWeight=1.0, barTimeOffset=1494.0)
161 myDict.append(stationID=0, trainID=0, barID=1, barWeight=1.0, barTimeOffset=1500.0)
162 myDict.append(stationID=0, trainID=0, barID=2, barWeight=1.0, barTimeOffset=1500.0)
163 myDict.append(stationID=0, trainID=0, barID=3, barWeight=1.0, barTimeOffset=1500.0)
164 myDict.append(stationID=0, trainID=1, barID=0, barWeight=1.0, barTimeOffset=1500.0)
165 myDict.append(stationID=0, trainID=1, barID=1, barWeight=1.0, barTimeOffset=1500.0)
166 myDict.append(stationID=0, trainID=1, barID=2, barWeight=1.0, barTimeOffset=1500.0)
167 myDict.append(stationID=0, trainID=1, barID=3, barWeight=1.0, barTimeOffset=1500.0)
168 myDict.append(stationID=0, trainID=2, barID=0, barWeight=1.0, barTimeOffset=1500.0)
169 myDict.append(stationID=0, trainID=2, barID=1, barWeight=1.0, barTimeOffset=1500.0)
170 myDict.append(stationID=0, trainID=2, barID=2, barWeight=1.0, barTimeOffset=1500.0)
171 myDict.append(stationID=0, trainID=2, barID=3, barWeight=1.0, barTimeOffset=1500.0)
172 myDict.append(stationID=0, trainID=3, barID=0, barWeight=1.0, barTimeOffset=1500.0)
173 myDict.append(stationID=0, trainID=3, barID=1, barWeight=1.0, barTimeOffset=1500.0)
174 myDict.append(stationID=0, trainID=3, barID=2, barWeight=1.0, barTimeOffset=1500.0)
175 myDict.append(stationID=0, trainID=3, barID=3, barWeight=1.0, barTimeOffset=1500.0)
176 myDict.append(stationID=3, trainID=0, barID=0, barWeight=1.0, barTimeOffset=1500.0)
177 myDict.append(stationID=3, trainID=0, barID=1, barWeight=1.0, barTimeOffset=1500.0)
178 myDict.append(stationID=3, trainID=0, barID=2, barWeight=1.0, barTimeOffset=1500.0)
179 myDict.append(stationID=3, trainID=0, barID=3, barWeight=1.0, barTimeOffset=1500.0)
180 myDict.append(stationID=3, trainID=1, barID=0, barWeight=1.0, barTimeOffset=1500.0)
181 myDict.append(stationID=3, trainID=1, barID=1, barWeight=1.0, barTimeOffset=1500.0)
182 myDict.append(stationID=3, trainID=1, barID=2, barWeight=1.0, barTimeOffset=1500.0)
183 myDict.append(stationID=3, trainID=1, barID=3, barWeight=1.0, barTimeOffset=1500.0)
184 myDict.append(stationID=3, trainID=2, barID=0, barWeight=1.0, barTimeOffset=1500.0)
185 myDict.append(stationID=3, trainID=2, barID=1, barWeight=1.0, barTimeOffset=1500.0)
186 myDict.append(stationID=3, trainID=2, barID=2, barWeight=1.0, barTimeOffset=1500.0)
187 myDict.append(stationID=3, trainID=2, barID=3, barWeight=1.0, barTimeOffset=1500.0)
188 myDict.append(stationID=3, trainID=3, barID=0, barWeight=1.0, barTimeOffset=1500.0)
189 myDict.append(stationID=3, trainID=3, barID=1, barWeight=1.0, barTimeOffset=1500.0)
190 myDict.append(stationID=3, trainID=3, barID=2, barWeight=1.0, barTimeOffset=1500.0)
191 myDict.append(stationID=3, trainID=3, barID=3, barWeight=1.0, barTimeOffset=1500.0)
192 myDict.savePayload(folderBlk=folderBlkLoc_ideal_01)
195 myDict =
AFPDBDict(folderBlk=folderBlkVtx_ideal_01)
196 myDict.append(stationID=0, timeGlobalOffset=0.0, timeOffset=(64.,45.,25.,11.), timeSlope=(-7.3,-5.0,-4.0,-4.0), trainEdge=(-18.7,-13.2,-8.1,-4.0,-2.5))
197 myDict.append(stationID=3, timeGlobalOffset=5.0, timeOffset=( 2., 9.,14.,12.), timeSlope=( 4.0, 5.0, 3.0, 6.0), trainEdge=(-18.7,-13.2,-8.1,-4.0,-2.5))
198 myDict.savePayload(folderBlk=folderBlkVtx_ideal_01)
201 print (
"\nClose database")
205 if __name__==
"__main__":