ATLAS Offline Software
CaloTime_fillDB.py
Go to the documentation of this file.
1 #!/bin/env python
2 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 
4 
5 import sys
6 
7 def usage():
8  print ("Syntax for open-end IoV time constant update")
9  print (" The first parameter is the run number of IoV start, the second parameter is the lumiblock number for IoV start")
10  print (" The third and fourth parameters are the Run/lb for IoV end (if run is -1, uses open ended IoV)")
11  print (" the fifth and sixth parameters are the inputfile for allconstants and runconstants")
12 
13 if len(sys.argv)<7:
14  usage()
15  sys.exit(-1)
16 
17 runSince = sys.argv[1]
18 lbkSince = sys.argv[2]
19 runUntil = sys.argv[3]
20 lbkUntil = sys.argv[4]
21 
22 print ("runSince ", runSince, lbkSince)
23 print ("runUntil ", runUntil, lbkUntil)
24 
25 import cppyy
26 from PyCool import cool
27 from CaloCondBlobAlgs import CaloCondTools, CaloCondLogger
28 
29 #==================================================
30 #===
31 #=== Configuration section
32 #===
33 #==================================================
34 inputFile_con = sys.argv[5]
35 inputFile_run = sys.argv[6]
36 
37 #=== IOV range
38 iovSince_con = CaloCondTools.iovFromRunLumi(runSince,lbkSince)
39 iovUntil_con = CaloCondTools.iovFromRunLumi(runUntil,lbkUntil)
40 
41 iovMin = cool.ValidityKeyMin
42 iovMax = cool.ValidityKeyMax
43 
44 print (" iovSince_con ", iovSince_con)
45 print (" iovUntil_con ", iovUntil_con)
46 
47 print (" iovMin ", iovMin)
48 print (" iovMax ", iovMax)
49 
50 lbkdown = 0
51 lbkup = 4294967295
52 
53 #=== folder tag suffix
54 tagCon = "LARTimeCorrectionOflNonRunCon-00"
55 tagRun = "LARTimeCorrectionOflRunCon-00"
56 #=== values for the comment channel
57 author = "dhu"
58 comment = "Updated time constant values"
59 
60 #==================================================
61 #===
62 #=== Code starts below here
63 #===
64 #==================================================
65 #=== set shortcut
66 g = cppyy.gbl
67 cppyy.makeClass('std::vector<float>')
68 
69 #=== get a logger
70 log = CaloCondLogger.getLogger("CaloTimeConsWriter")
71 
72 #=== (re-)create the database
73 db = CaloCondTools.openDb('SQLITE', 'COMP200', 'UPDATE')
74 
75 try:
76  #=== creating folder specifications
77  spec = cool.RecordSpecification()
78  spec.extend( 'CaloCondBlob16M', cool.StorageType.Blob16M )
79 
80  #=== create the folder
81  folderPath = CaloCondTools.getCaloPrefix()+"LAR/TimeCorrectionOfl/NonRunCon"
82  folderTag = tagCon
83  print ("Filling COOL folder ", folderPath, " with tag ", folderTag )
84  desc = CaloCondTools.getAthenaFolderDescr()
85  try:
86  folder = db.getFolder(folderPath)
87  except Exception:
88  log.warning("Folder %s not found, creating it...", folderPath)
89  print ("Folder ", folderPath, " not found, creating it... " )
90  folderSpec = cool.FolderSpecification(cool.FolderVersioning.SINGLE_VERSION, spec)
91  folder = db.createFolder(folderPath, folderSpec, desc, True)
92 
93  #==================================================
94  #=== Create the CaloCondBlobFlt objects
95  #==================================================
96  #=== default a and b to be used for each gain
97  gainDefVec_1 = g.std.vector('float')()
98  gainDefVec_1.push_back(0.) # febOffset+channelOffset
99  gainDefVec_1.push_back(0.) # energy depended error parameter 1
100  gainDefVec_1.push_back(0.) # energy depended error parameter 2
101  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 1
102  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 2
103  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 3
104  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 4
105  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 5
106  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 6
107  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 7
108  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 8
109  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 9
110  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 10
111  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 11
112  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 12
113  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 13
114  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 14
115  gainDefVec_1.push_back(0.) # feb depended energy correction parameter 15
116  #=== three gains per channel for LAr
117  defVecLAr_1 = g.std.vector('std::vector<float>')()
118  defVecLAr_1.push_back(gainDefVec_1)
119  defVecLAr_1.push_back(gainDefVec_1)
120  defVecLAr_1.push_back(gainDefVec_1)
121 
122 
123  #=== system specific data: sysId -> (nChannel, hash-offset, default-vector, name)
124  systemDict = { 0 : (31872, 0, defVecLAr_1 , 'EMEC, z<0'),
125  1 : (54784, 31872, defVecLAr_1 , 'EMB , z<0'),
126  2 : (54784, 86656, defVecLAr_1 , 'EMB , z>0'),
127  3 : (31872, 141440, defVecLAr_1 , 'EMEC, z>0'),
128  }
129 
130  print ("before fill virtual database")
131  for systemId, info in systemDict.items():
132  print ("before fill virtual database for sysId=", systemId)
133  if (systemId<=3) :
134  print ("Creating BLOB for Calo sysId=", systemId)
135  nChannel = info[0]
136  defVec = info[2]
137  sysName = info[3]
138  log.info("Creating BLOB for %s", sysName)
139  data_vir = cool.Record( spec )
140  blob_vir = data_vir['CaloCondBlob16M']
141  flt_vir = g.CaloCondBlobFlt.getInstance(blob_vir)
142  flt_vir.init(defVec,nChannel,1,author,comment)
143 
144  log.info("Committing BLOB for %s", sysName)
145  channelId = cool.ChannelId(systemId)
146  log.info("Cool channel ID %s", channelId)
147  folder.storeObject(iovMin, iovMax, data_vir, channelId, folderTag)
148 
149  print ("after fill virtual database")
150 
151  fltDict_1 = {}
152  print ("Prepare BLOB for Calo")
153  for systemId, info in systemDict.items():
154  print ("before check sysId=", systemId )
155  if (systemId<=3) :
156  print ("Creating BLOB for Calo sysId=", systemId )
157  nChannel = info[0]
158  defVec = info[2]
159  sysName = info[3]
160  log.info("Creating BLOB for %s", sysName)
161  data = cool.Record( spec )
162  blob = data['CaloCondBlob16M']
163  flt = g.CaloCondBlobFlt.getInstance(blob)
164  flt.init(defVec,nChannel,1,author,comment)
165  fltDict_1[systemId] = [data,flt]
166  kbSize = float(blob.size()) / 1024
167  print ("BLOB size is ", kbSize, " KB")
168  mbSize = float(kbSize) / 1024
169  print ("BLOB size is ", mbSize, " MB")
170 
171 
172  #=== read time values from file
173  print ("before read time values from file")
174  lines = open(inputFile_con,"r").readlines()
175  print ("before enter file line loop")
176  for line in lines:
177  fields = line.split()
178 
179  systemId = int(fields[0])
180  hash_offset = int(systemDict[systemId][1])
181  hash_raw = int(fields[6])
182  hash = hash_raw - hash_offset
183  hash_check = int(fields[7])
184  gain = g.CaloCondUtils.getDbCaloGain(int(fields[8]))
185  febchanOffset = float(fields[9])
186  errPar1 = float(fields[10])
187  errPar2 = float(fields[11])
188  enePar1 = float(fields[12])
189  enePar2 = float(fields[13])
190  enePar3 = float(fields[14])
191  enePar4 = float(fields[15])
192  enePar5 = float(fields[16])
193  enePar6 = float(fields[17])
194  enePar7 = float(fields[18])
195  enePar8 = float(fields[19])
196  enePar9 = float(fields[20])
197  enePar10 = float(fields[21])
198  enePar11 = float(fields[22])
199  enePar12 = float(fields[23])
200  enePar13 = float(fields[24])
201  enePar14 = float(fields[25])
202  enePar15 = float(fields[26])
203 
204  if not hash==hash_check:
205  print ("hash!=hash_check:", hash , hash_check)
206  continue
207 
208  if systemId==0:
209  if hash>=31872:
210  print ("hash>=31872 for hash ", hash , ", sys ", systemId)
211  continue
212 
213  if systemId==1:
214  if hash>=54784:
215  print ("hash>=54784 for hash ", hash , ", sys ", systemId)
216  continue
217 
218  if systemId==2:
219  if hash>=54784:
220  print ("hash>=54784 for hash ", hash , ", sys ", systemId)
221  continue
222 
223  if systemId==3:
224  if hash>=31872:
225  print ("hash>=31872 for hash ", hash , ", sys ", systemId)
226  continue
227 
228  print (systemId, hash, hash_check, gain, febchanOffset, errPar1, errPar2, enePar1, enePar2, enePar3, enePar4, enePar5, enePar6, enePar7, enePar8, enePar9, enePar10, enePar11, enePar12, enePar13, enePar14, enePar15)
229 
230  print ("before get flt")
231  flt = fltDict_1[systemId][1]
232  flt.setData(hash,gain,0,febchanOffset)
233  flt.setData(hash,gain,1,errPar1)
234  flt.setData(hash,gain,2,errPar2)
235  flt.setData(hash,gain,3,enePar1)
236  flt.setData(hash,gain,4,enePar2)
237  flt.setData(hash,gain,5,enePar3)
238  flt.setData(hash,gain,6,enePar4)
239  flt.setData(hash,gain,7,enePar5)
240  flt.setData(hash,gain,8,enePar6)
241  flt.setData(hash,gain,9,enePar7)
242  flt.setData(hash,gain,10,enePar8)
243  flt.setData(hash,gain,11,enePar9)
244  flt.setData(hash,gain,12,enePar10)
245  flt.setData(hash,gain,13,enePar11)
246  flt.setData(hash,gain,14,enePar12)
247  flt.setData(hash,gain,15,enePar13)
248  flt.setData(hash,gain,16,enePar14)
249  flt.setData(hash,gain,17,enePar15)
250 
251  print ("outside file line loop")
252 
253  #=== write to DB
254  print ("Committing BLOB for Calo")
255  for systemId, dataList in fltDict_1.items():
256  if (systemId<=3):
257  sysName = systemDict[systemId][3]
258  log.info("Committing BLOB for %s", sysName)
259  channelId = cool.ChannelId(systemId)
260  log.info("Cool channel ID %s", channelId)
261  data = dataList[0]
262  folder.storeObject(iovSince_con, iovUntil_con, data, channelId, folderTag)
263 
264 except Exception as e:
265  log.fatal("Exception caught: fill LAR/TimeCorrectionOfl/NonRunCon")
266  print ("Exception caught: fill LAR/TimeCorrectionOfl/NonRunCon")
267  print (e)
268 
269 try:
270  #=== creating folder specifications
271  spec = cool.RecordSpecification()
272  spec.extend( 'CaloCondBlob16M', cool.StorageType.Blob16M )
273 
274  #=== create the folder
275  folderPath = CaloCondTools.getCaloPrefix()+"LAR/TimeCorrectionOfl/RunCon"
276  folderTag = tagRun
277  print ("Filling COOL folder ", folderPath, "with tag ", folderTag)
278  desc = CaloCondTools.getAthenaFolderDescr()
279  try:
280  folder = db.getFolder(folderPath)
281  except Exception:
282  log.warning("Folder %s not found, creating it...", folderPath)
283  print ("Folder ", folderPath, " not found, creating it... " )
284 
285  folder = db.createFolder(folderPath, spec, desc, cool.FolderVersioning.MULTI_VERSION, True)
286 
287  #==================================================
288  #=== Create the CaloCondBlobFlt objects
289  #==================================================
290  #=== default a and b to be used for each gain
291  gainDefVec_2 = g.std.vector('float')()
292  gainDefVec_2.push_back(0.) # runOffset
293  gainDefVec_2.push_back(0.) # runEntry
294  #=== three gains per channel for LAr
295  defVecLAr_2 = g.std.vector('std::vector<float>')()
296  defVecLAr_2.push_back(gainDefVec_2)
297  defVecLAr_2.push_back(gainDefVec_2)
298  defVecLAr_2.push_back(gainDefVec_2)
299 
300  # fill database with virtual value
301  print ("before fill virtual database")
302  nChannel = 116 # 0-31: Barrel C; 32-63: Barrel A; 64-88: EMEC; 89-113: EMEA;
303  channelId = cool.ChannelId(1)
304  data_vir = cool.Record( spec )
305  blob_vir = data_vir['CaloCondBlob16M']
306  flt_vir = g.CaloCondBlobFlt.getInstance(blob_vir)
307  flt_vir.init(defVecLAr_2,nChannel,1,author,comment)
308  folder.storeObject(iovMin, iovMax, data_vir, channelId, folderTag)
309  print ("after fill virtual database")
310 
311  run_list = []
312  lines = open(inputFile_run,"r").readlines()
313 
314  print ("before fill run_list")
315  for line in lines:
316  fields = line.split()
317  run = int(fields[0])
318  if run_list.count(run)==0:
319  run_list.append(run)
320 
321  print ("after fill run_list")
322  run_list.sort()
323  print ("number of runs: ", len(run_list))
324  print ("run_list: ", run_list)
325 
326  fltDict_2 = {}
327  for runId in run_list:
328  print ("Creating BLOB for run ", runId )
329  nChannel = 116 # 0-31: Barrel C; 32-63: Barrel A; 64-88: EMEC; 89-113: EMEA;
330  data = cool.Record( spec )
331  blob = data['CaloCondBlob16M']
332  flt = g.CaloCondBlobFlt.getInstance(blob)
333  flt.init(defVecLAr_2,nChannel,1,author,comment)
334  fltDict_2[runId] = [data,flt]
335  kbSize = float(blob.size()) / 1024.
336  print ("BLOB size is ", kbSize, " kB")
337  mbSize = float(kbSize) / 1024
338  print ("BLOB size is ", mbSize, " MB")
339 
340 
341  print ("before enter file line loop")
342  for line in lines:
343  fields = line.split()
344 
345  runId = int(fields[0])
346  ftID = int(fields[5])
347  runOffsetHigh = float(fields[6])
348  runEntryHigh = float(fields[7])
349  runOffsetMed = float(fields[8])
350  runEntryMed = float(fields[9])
351  runOffsetLow = float(fields[10])
352  runEntryLow = float(fields[11])
353  gain0 = g.CaloCondUtils.getDbCaloGain(0)
354  gain1 = g.CaloCondUtils.getDbCaloGain(1)
355  gain2 = g.CaloCondUtils.getDbCaloGain(2)
356 
357  flt = fltDict_2[runId][1]
358  flt.setData(ftID,gain0,0,runOffsetHigh)
359  flt.setData(ftID,gain0,1,runEntryHigh)
360  flt.setData(ftID,gain1,0,runOffsetMed)
361  flt.setData(ftID,gain1,1,runEntryMed)
362  flt.setData(ftID,gain2,0,runOffsetLow)
363  flt.setData(ftID,gain2,1,runEntryLow)
364 
365  print (runId, ftID, runOffsetHigh, runEntryHigh, runOffsetMed, runEntryMed, runOffsetLow, runEntryLow)
366 
367  print ("after enter file line loop")
368 
369  for runId, dataList in fltDict_2.items():
370  print ("Committing BLOB for run ", runId)
371  iovSince_run = CaloCondTools.iovFromRunLumi(runId,lbkdown)
372  iovUntil_run = CaloCondTools.iovFromRunLumi(runId,lbkup)
373  channelId = cool.ChannelId(1)
374  print ("Cool channel ID ", channelId )
375  data = dataList[0]
376  folder.storeObject(iovSince_run, iovUntil_run, data, channelId, folderTag)
377 
378 except Exception as e:
379  log.fatal("Exception caught: LAR/TimeCorrectionOfl/RunCon")
380  print ("Exception caught: LAR/TimeCorrectionOfl/RunCon")
381  print (e)
382 
383 #=== close the database
384 db.closeDatabase()
385 
CaloTime_fillDB.usage
def usage()
Definition: CaloTime_fillDB.py:7
Trk::open
@ open
Definition: BinningType.h:40
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65