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