ATLAS Offline Software
CaloNoise_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 math
8 import six
9 
10 def usage():
11  print ("Syntax for UPD4 open-end IoV noise update")
12  print (" The first parameter is the run number of IoV start, the second parameter is the lumiblock number for IoV start")
13  print (" The third and fourth parameter are the Run/lb for IoV end (if run is -1, uses open ended IoV)")
14  print (" The fifth parameter is the upd4 tag name")
15  print (" The sixth parameter is input text file name (default calonoise.txt)")
16  print (" The seventh parameter is output sqlite file name (default caloSqlite.db)")
17  print (" The eigth parameter is output DB name (default CONDBR2)")
18  print (" The nineth parameter is output folder name (default /LAR/NoiseOfl/CellNoise) ")
19  print (" The tenth parameter is mu (default 30)")
20  print (" The eleventh parameter is dt (default 25)")
21 
22 
23 if len(sys.argv)<6:
24  usage()
25  sys.exit(-1)
26 
27 runSince = sys.argv[1]
28 lbkSince = sys.argv[2]
29 runUntil = sys.argv[3]
30 lbkUntil = sys.argv[4]
31 tag = sys.argv[5]
32 if len(sys.argv)>6:
33  inputFile=sys.argv[6]
34 else:
35  inputFile="calonoise.txt"
36 
37 if len(sys.argv)>7:
38  filename=sys.argv[7]
39 else:
40  filename="larnoisesqlite.db"
41 
42 if len(sys.argv)>8:
43  dbname=sys.argv[8]
44 else:
45  dbname="CONDBR2"
46 
47 if len(sys.argv)>9:
48  folderPath = sys.argv[9]
49 else:
50  folderPath = "/LAR/NoiseOfl/CellNoise"
51 
52 if len(sys.argv)>10:
53  mu = int(sys.argv[10])
54 else:
55  mu = -1
56 
57 if len(sys.argv)>11:
58  dt = int(sys.argv[11])
59 else:
60  dt = -1
61 
62 if len(sys.argv)>11:
63  dt = int(sys.argv[11])
64 else:
65  dt = -1
66 
67 
68 print ("input: ",inputFile)
69 print ("runUntil ", runUntil, lbkUntil)
70 print ("output:",filename)
71 print ("input: ",inputFile)
72 
73 import ROOT
74 import cppyy
75 from PyCool import cool
76 from CaloCondBlobAlgs import CaloCondTools, CaloCondLogger
77 
78 #==================================================
79 #===
80 #=== Configuration section
81 #===
82 #==================================================
83 #=== IOV range
84 iovSince = CaloCondTools.iovFromRunLumi(runSince,lbkSince)
85 iovUntil = cool.ValidityKeyMax
86 print (" iovUntil max ",iovUntil)
87 if int(runUntil) > 0:
88  print (" use run number to define iobUntil ", runUntil)
89  iovUntil = CaloCondTools.iovFromRunLumi(runUntil,lbkUntil)
90 
91 print (" iovSince ", iovSince)
92 print (" iovUntil ", iovUntil)
93 
94 #=== values for the comment channel
95 author = "gunal"
96 comment = "Updated noise values"
97 
98 #==================================================
99 #===
100 #=== Code starts below here
101 #===
102 #==================================================
103 
104 #=== get a logger
105 log = CaloCondLogger.getLogger("CaloNoiseWriter")
106 
107 #=== (re-)create the database
108 dbSvc = cool.DatabaseSvcFactory.databaseService()
109 try:
110  db=dbSvc.openDatabase("sqlite://;schema="+filename+";dbname="+dbname, False)
111 except Exception:
112  db=dbSvc.createDatabase("sqlite://;schema="+filename+";dbname="+dbname)
113 
114 try:
115  #=== creating folder specifications
116  spec = cool.RecordSpecification()
117  spec.extend( 'CaloCondBlob16M', cool.StorageType.Blob16M )
118  fspec = cool.FolderSpecification(cool.FolderVersioning.MULTI_VERSION, spec)
119 
120  #=== create the folder
121  folderTag = tag
122  log.info( "Filling COOL folder %s with tag %s", folderPath, folderTag )
123  desc = CaloCondTools.getAthenaFolderDescr()
124  try:
125  folder = db.getFolder(folderPath)
126  except Exception:
127  log.warning("Folder %s not found, creating it...", folderPath)
128  #folder = db.createFolder(folderPath, spec, desc, cool.FolderVersioning.MULTI_VERSION, True)
129  folder = db.createFolder(folderPath, fspec, desc, True)
130 
131  #==================================================
132  #=== Create the CaloCondBlobFlt objects
133  #==================================================
134  #=== default a and b to be used for each gain
135  gainDefVec = ROOT.std.vector('float')()
136  gainDefVec.push_back(0.) # a
137  gainDefVec.push_back(0.) # b
138  #=== three gains per channel for LAr
139  #defVecLAr = g.std.vector('std::vector<float>')()
140  defVecLAr = ROOT.std.vector('std::vector<float>')()
141  defVecLAr.push_back(gainDefVec)
142  defVecLAr.push_back(gainDefVec)
143  defVecLAr.push_back(gainDefVec)
144  #=== four "gains" per channel for Tile
145  #defVecTile = g.std.vector('std::vector<float>')()
146  defVecTile = ROOT.std.vector('std::vector<float>')()
147  defVecTile.push_back(gainDefVec)
148  defVecTile.push_back(gainDefVec)
149  defVecTile.push_back(gainDefVec)
150  defVecTile.push_back(gainDefVec)
151 
152  #=== system specific data: sysId -> (nChannel, hash-offset, default-vector, name)
153  systemDict = { 0 : (31872, 0, defVecLAr , 'EMEC, z<0'),
154  1 : (54784, 31872, defVecLAr , 'EMB , z<0'),
155  2 : (54784, 86656, defVecLAr , 'EMB , z>0'),
156  3 : (31872, 141440, defVecLAr , 'EMEC, z>0'),
157  16 : ( 5632, 0, defVecLAr , 'HEC' ),
158  32 : ( 3524, 0, defVecLAr , 'FCAL' ),
159  48 : ( 5184, 0, defVecTile, 'TILE' )
160  }
161  fltDict = {}
162  for systemId, info in six.iteritems (systemDict):
163  if (systemId<48) :
164  nChannel = info[0]
165  defVec = info[2]
166  sysName = info[3]
167  log.info("Creating BLOB for %s", sysName)
168  data = cool.Record( spec )
169  blob = data['CaloCondBlob16M']
170  flt = ROOT.CaloCondBlobFlt.getInstance(blob)
171  flt.init(defVec,nChannel,1,author,comment)
172  fltDict[systemId] = [data,flt]
173  mbSize = float(blob.size()) / 1024.
174  log.info("---> BLOB size is %4.1f kB", mbSize)
175 
176  #=== read noise values from file
177  lines = open(inputFile,"r").readlines()
178  for line in lines:
179  fields = line.split()
180  if len(fields) < 5:
181  log.info("---> wrong line length %d entries ", len(fields))
182  continue
183  pass
184  systemId = int(fields[0])
185  hash = int(fields[1]) - systemDict[systemId][1]
186  gain = ROOT.CaloCondUtils.getDbCaloGain(int(fields[2]))
187  noiseA = float(fields[3])
188  noiseB = float(fields[4])
189  flt = fltDict[systemId][1]
190  if mu > 0 and dt > 0:
191  # new normalization
192  if dt > 25:
193  noiseB /= math.sqrt(mu/53.*10.)
194  else:
195  noiseB /= math.sqrt(mu/29.*10.)
196  pass
197  flt.setData(hash,gain,0,noiseA)
198  flt.setData(hash,gain,1,noiseB)
199 
200  #=== write to DB
201  for systemId, dataList in six.iteritems (fltDict):
202  if (systemId<48):
203  sysName = systemDict[systemId][3]
204  log.info("Committing BLOB for %s", sysName)
205  channelId = cool.ChannelId(systemId)
206  log.info("Cool channel ID %s", channelId)
207  data = dataList[0]
208  folder.storeObject(iovSince, iovUntil, data, channelId, folderTag)
209 
210 except Exception as e:
211  log.fatal("Exception caught:")
212  print (e)
213 
214 #=== close the database
215 db.closeDatabase()
216 
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
CaloNoise_fillDB.usage
def usage()
Definition: CaloNoise_fillDB.py:10
Trk::open
@ open
Definition: BinningType.h:40
readCCLHist.float
float
Definition: readCCLHist.py:83