ATLAS Offline Software
Loading...
Searching...
No Matches
CaloNoise_fillDB.py
Go to the documentation of this file.
1#!/bin/env python
2# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3import getpass
4import math
5import sys
6_default_inputFile = "calonoise.txt"
7_default_filename = "larnoisesqlite.db"
8
9
10def 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
23def fillLArNoiseDB(iovSince, iovUntil, tag, inputFile=_default_inputFile, filename=_default_filename,
24 dbname="CONDBR2", folderPath="/LAR/NoiseOfl/CellNoise", mu=-1, dt=-1):
25 import ROOT
26 from PyCool import cool
27 from AthenaPython.PyAthena import StatusCode
28 from CaloCondBlobAlgs import CaloCondTools, CaloCondLogger
29 #=== values for the comment channel
30 author = getpass.getuser()
31 comment = "Updated noise values"
32 #=== get a logger
33 log = CaloCondLogger.getLogger("CaloNoiseWriter")
34
35 #=== (re-)create the database
36 dbSvc = cool.DatabaseSvcFactory.databaseService()
37 try:
38 db=dbSvc.openDatabase("sqlite://;schema="+filename+";dbname="+dbname, False)
39 except Exception:
40 db=dbSvc.createDatabase("sqlite://;schema="+filename+";dbname="+dbname)
41
42 try:
43 #=== creating folder specifications
44 spec = cool.RecordSpecification()
45 spec.extend( 'CaloCondBlob16M', cool.StorageType.Blob16M )
46 fspec = cool.FolderSpecification(cool.FolderVersioning.MULTI_VERSION, spec)
47
48 #=== create the folder
49 folderTag = tag
50 log.info( "Filling COOL folder %s with tag %s", folderPath, folderTag )
51 desc = CaloCondTools.getAthenaFolderDescr()
52 try:
53 folder = db.getFolder(folderPath)
54 except Exception:
55 log.warning("Folder %s not found, creating it...", folderPath)
56 #folder = db.createFolder(folderPath, spec, desc, cool.FolderVersioning.MULTI_VERSION, True)
57 folder = db.createFolder(folderPath, fspec, desc, True)
58
59 #==================================================
60 #=== Create the CaloCondBlobFlt objects
61 #==================================================
62 #=== default a and b to be used for each gain
63 gainDefVec = ROOT.std.vector('float')()
64 gainDefVec.push_back(0.) # a
65 gainDefVec.push_back(0.) # b
66 #=== three gains per channel for LAr
67 #defVecLAr = g.std.vector('std::vector<float>')()
68 defVecLAr = ROOT.std.vector('std::vector<float>')()
69 defVecLAr.push_back(gainDefVec)
70 defVecLAr.push_back(gainDefVec)
71 defVecLAr.push_back(gainDefVec)
72 #=== four "gains" per channel for Tile
73 #defVecTile = g.std.vector('std::vector<float>')()
74 defVecTile = ROOT.std.vector('std::vector<float>')()
75 defVecTile.push_back(gainDefVec)
76 defVecTile.push_back(gainDefVec)
77 defVecTile.push_back(gainDefVec)
78 defVecTile.push_back(gainDefVec)
79
80 #=== system specific data: sysId -> (nChannel, hash-offset, default-vector, name)
81 systemDict = { 0 : (31872, 0, defVecLAr , 'EMEC, z<0'),
82 1 : (54784, 31872, defVecLAr , 'EMB , z<0'),
83 2 : (54784, 86656, defVecLAr , 'EMB , z>0'),
84 3 : (31872, 141440, defVecLAr , 'EMEC, z>0'),
85 16 : ( 5632, 0, defVecLAr , 'HEC' ),
86 32 : ( 3524, 0, defVecLAr , 'FCAL' ),
87 48 : ( 5184, 0, defVecTile, 'TILE' )
88 }
89 fltDict = {}
90 for systemId, info in systemDict.items():
91 if (systemId<48) :
92 nChannel = info[0]
93 defVec = info[2]
94 sysName = info[3]
95 log.info("Creating BLOB for %s", sysName)
96 data = cool.Record( spec )
97 blob = data['CaloCondBlob16M']
98 flt = ROOT.CaloCondBlobFlt.getInstance(blob)
99 flt.init(defVec,nChannel,1,author,comment)
100 fltDict[systemId] = [data,flt]
101 mbSize = float(blob.size()) / 1024.
102 log.info("---> BLOB size is %4.1f kB", mbSize)
103
104 #=== read noise values from file
105 lines = open(inputFile,"r").readlines()
106 for line in lines:
107 fields = line.split()
108 if len(fields) < 5:
109 log.info("---> wrong line length %d entries ", len(fields))
110 continue
111 pass
112 systemId = int(fields[1])
113 hash = int(fields[2]) - systemDict[systemId][1]
114 gain = ROOT.CaloCondUtils.getDbCaloGain(int(fields[3]))
115 noiseA = float(fields[4])
116 noiseB = float(fields[5])
117 flt = fltDict[systemId][1]
118 if mu > 0 and dt > 0:
119 # new normalization
120 if dt > 25:
121 noiseB /= math.sqrt(mu/53.*10.)
122 else:
123 noiseB /= math.sqrt(mu/29.*10.)
124 pass
125 if mu == 0:
126 noiseB = 0
127 flt.setData(hash,gain,0,noiseA)
128 flt.setData(hash,gain,1,noiseB)
129
130 #=== write to DB
131 for systemId, dataList in fltDict.items():
132 if (systemId<48):
133 sysName = systemDict[systemId][3]
134 log.info("Committing BLOB for %s", sysName)
135 channelId = cool.ChannelId(systemId)
136 log.info("Cool channel ID %s", channelId)
137 data = dataList[0]
138 folder.storeObject(iovSince, iovUntil, data, channelId, folderTag)
139 sc = StatusCode.Success
140 except Exception as e:
141 log.fatal("Exception caught:")
142 print (e)
143 sc = StatusCode.Failure
144 #=== close the database
145 db.closeDatabase()
146 return sc
147
148
149if __name__ == "__main__":
150 if len(sys.argv)<6:
151 usage()
152 sys.exit(-1)
153 runSince = sys.argv[1]
154 lbkSince = sys.argv[2]
155 runUntil = sys.argv[3]
156 lbkUntil = sys.argv[4]
157 tag = sys.argv[5]
158 kwargs = {}
159 if len(sys.argv)>6:
160 kwargs["inputFile"]=sys.argv[6]
161
162 if len(sys.argv)>7:
163 kwargs["filename"]=sys.argv[7]
164
165 if len(sys.argv)>8:
166 kwargs["dbname"]=sys.argv[8]
167
168 if len(sys.argv)>9:
169 kwargs["folderPath"] = sys.argv[9]
170
171 if len(sys.argv)>10:
172 kwargs["mu"] = int(sys.argv[10])
173 mu = -1
174
175 if len(sys.argv)>11:
176 kwargs["dt"] = int(sys.argv[11])
177
178 print ("input: ", kwargs.get("inputFile", _default_inputFile))
179 print ("runUntil ", runUntil, lbkUntil)
180 print ("output:", kwargs.get("filename", _default_filename))
181 #=== IOV range
182 from CaloCondBlobAlgs import CaloCondTools
183 from PyCool import cool
184 iovSince = CaloCondTools.iovFromRunLumi(runSince,lbkSince)
185 iovUntil = cool.ValidityKeyMax
186 print (" iovUntil max ",iovUntil)
187 if int(runUntil) > 0:
188 print (" use run number to define iobUntil ", runUntil)
189 iovUntil = CaloCondTools.iovFromRunLumi(runUntil,lbkUntil)
190 print (" iovSince ", iovSince)
191 print (" iovUntil ", iovUntil)
192 sc = fillLArNoiseDB(iovSince, iovUntil, tag, **kwargs)
193 sys.exit(0 if sc.isSuccess() else -1)
fillLArNoiseDB(iovSince, iovUntil, tag, inputFile=_default_inputFile, filename=_default_filename, dbname="CONDBR2", folderPath="/LAR/NoiseOfl/CellNoise", mu=-1, dt=-1)