ATLAS Offline Software
Loading...
Searching...
No Matches
ReadCellNoiseFromCrest.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4#
5# File: ReadCellNoiseCrest.py
6# Sanya Solodkov <Sanya.Solodkov@cern.ch>, 2025-11-16
7#
8# Purpose: Read cell noise parameters from CREST DB or from JSON file
9#
10# Each Tile cell has 5 values stored in DB.
11# The first two values are the RMS of a sigle gaussian model of the electronic noise
12# and the pile-up noise normalized at 10^33cm-2s-1, (backwards compatibility)
13# The next three values are used for a two gaussian model.
14# These are: ratio between first and second gaussian, RMS of the first gaussian, and RMS of the second gaussian
15#
16
17import getopt,sys,os
18os.environ['TERM'] = 'linux'
19
20def usage():
21 print ("Usage: ",sys.argv[0]," [OPTION] ... ")
22 print ("Dumps noise constants from online or offline folders / tags")
23 print ("")
24 print ("-h, --help shows this help")
25 print ("-s, --schema= specify name of input JSON file or CREST_SERVER_PATH")
26 print ("-f, --folder= specify status folder to use f.i. /TILE/OFL02/NOISE/CELL or /CALO/Noise/CellNoise")
27 print ("-t, --tag= specify tag to use, f.i. UPD1 or UPD4 or tag suffix like 14TeV-N200_dT50-01")
28 print ("-r, --run= specify run number, by default uses latest iov")
29 print ("-l, --lumi= specify lumi block number, default is 0")
30 print ("-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
31 print ("-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
32 print ("-C, --comment print comment for every IOV")
33 print ("-i, --iov print IOVs only for every COOL channel")
34 print ("-I, --IOV print IOVs only")
35 print ("-n, --channel= specify COOL channel to read (48 by defalt)")
36 print ("-c, --cell= specify cell hash (0-5183), default is -1, means all cells")
37 print ("-g, --gain= specify gain to print (0-3), default is -1, means all gains")
38 print ("-x, --index= specify parameter index (0-4), default is -1, means all parameters")
39 print ("-B, --brief print only numbers without character names")
40 print ("-D, --double print values with double precision")
41
42letters = "hs:t:f:r:l:b:e:n:c:g:x:BDCiI"
43keywords = ["help","schema=","tag=","folder=","run=","lumi=","begin=","end=","channel=","cell=","gain=","index=","brief","double","comment","iov","IOV"]
44
45try:
46 opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
47except getopt.GetoptError as err:
48 print (str(err))
49 usage()
50 sys.exit(2)
51
52# defaults
53run = 2147483647
54lumi = 0
55schema = 'CREST'
56folderPath = '/TILE/OFL02/NOISE/CELL'
57tag = 'UPD4'
58chan = 48 # represents Tile
59cell = -1
60gain = -1
61index = -1
62brief = False
63doubl = False
64begin = -1
65end = 2147483647
66iov = False
67iovonly = False
68IOVONLY = False
69comment = False
70
71for o, a in opts:
72 a = a.strip()
73 if o in ("-s","--schema"):
74 schema = a
75 elif o in ("-f","--folder"):
76 folderPath = a
77 elif o in ("-t","--tag"):
78 tag = a
79 elif o in ("-n","--channel"):
80 chan = int(a)
81 elif o in ("-c","--cell"):
82 cell = int(a)
83 elif o in ("-g","--gain"):
84 gain = int(a)
85 elif o in ("-x","--index"):
86 index = int(a)
87 elif o in ("-r","--run"):
88 run = int(a)
89 elif o in ("-l","--lumi"):
90 lumi = int(a)
91 elif o in ("-b","--begin"):
92 begin = int(a)
93 iov = True
94 elif o in ("-e","--end"):
95 end = int(a)
96 iov = True
97 elif o in ("-i","--iov"):
98 iov = True
99 iovonly = True
100 elif o in ("-I","--IOV"):
101 iov = True
102 IOVONLY = True
103 elif o in ("-C","--comment"):
104 comment = True
105 elif o in ("-B","--brief"):
106 brief = True
107 elif o in ("-D","--double"):
108 doubl = True
109 elif o in ("-h","--help"):
110 usage()
111 sys.exit(2)
112 else:
113 usage()
114 sys.exit(2)
115
116tile=(chan==48)
117
118from TileCalibBlobPython import TileCalibLogger
119from TileCalibBlobPython import TileCalibCrest
120from TileCalibBlobPython import TileCellTools
121
122#=== get a logger
123log = TileCalibLogger.getLogger("ReadCellNoise")
124
125if run>=400000:
126 cabling = 'RUN3'
127elif run>=342550:
128 cabling = 'RUN2a'
129elif run>=222222:
130 cabling = 'RUN2'
131else:
132 cabling = 'RUN1'
133
134hashMgr=None
135hashMgrDef=TileCellTools.TileCellHashMgr(cabling=cabling)
136hashMgrA=TileCellTools.TileCellHashMgr("UpgradeA")
137hashMgrBC=TileCellTools.TileCellHashMgr("UpgradeBC")
138hashMgrABC=TileCellTools.TileCellHashMgr("UpgradeABC")
139
140#=== Initialize blob reader
141folderTag = tag
142tag = tag.upper()
143tag1 = tag.split('_')[1][:4] if '_' in tag else tag[:4]
144if tag1 == "TILE" or tag1 == "CALO" or tag1[:3] == "LAR" or tag.startswith("TILE") or tag.startswith("CALO") or tag.startswith("LAR"):
145 folderPath=""
146if not os.path.isfile(schema):
147 log.info("Initializing folder %s with tag %s", folderPath, folderTag)
148blobReader = TileCalibCrest.TileBlobReaderCrest(schema, folderPath, folderTag, run, lumi)
149log.info("Comment: %s", blobReader.getComment())
150
151#=== create CaloCondBlobFlt
152blobFlt = blobReader.getDrawer(-1,chan,None,False,False)
153if blobFlt is None:
154 log.critical("Could not locate a data blob in CREST payload for COOL channel %s", chan)
155 sys.exit(1)
156
157#=== retrieve data from the blob
158#cell = 0 # 0..5183 - Tile hash
159#gain = 0 # 0..3 - four Tile cell gains: -11, -12, -15, -16
160#index = 0 # 0..4 - electronic or pile-up noise or 2-G noise parameters
161
162ncell=blobFlt.getNChans()
163ngain=blobFlt.getNGains()
164nval=blobFlt.getObjSizeUint32()
165
166if ncell>hashMgrA.getHashMax():
167 hashMgr=hashMgrABC
168elif ncell>hashMgrBC.getHashMax():
169 hashMgr=hashMgrA
170elif ncell>hashMgrDef.getHashMax():
171 hashMgr=hashMgrBC
172else:
173 hashMgr=hashMgrDef
174log.info("Using %s CellMgr with hashMax %d", hashMgr.getGeometry(),hashMgr.getHashMax())
175
176#=== Filling the iovList
177iovList = []
178if iov:
179 if begin<0:
180 if iovonly or IOVONLY:
181 begin = end
182 else:
183 begin=0
184 iovList = blobReader.getIovs((begin,0),(end,0))
185
186 be=iovList[0][0]
187 en=iovList[-1][0]
188
189 if begin != be or end != en:
190 log.info( "" )
191 if be != begin:
192 log.info( "Changing begin run from %d to %d (start of IOV)", begin,be)
193 begin=be
194 if en != end:
195 if en>end:
196 log.info( "Changing end run from %d to %d (start of next IOV)", end,en)
197 else:
198 log.info( "Changing end run from %d to %d (start of last IOV)", end,en)
199 end=en
200 log.info( "%d IOVs in total", len(iovList))
201
202 #=== IOV only option
203 if iovonly or IOVONLY:
204 option = 1 if iovonly else 0
205 option += (2 if IOVONLY else 0)
206 blobReader.dumpIovs(iovList,-1,0,chan,chan+1,option,tile,False)
207 sys.exit(0)
208else:
209 iovList.append((run,lumi))
210
211log.info( "\n" )
212
213
214if cell<0 or cell>=ncell:
215 cellmin=0
216 cellmax=ncell
217else:
218 cellmin=cell
219 cellmax=cell+1
220
221if gain<0 or gain>=ngain:
222 gainmin=0
223 gainmax=ngain
224else:
225 gainmin=gain
226 gainmax=gain+1
227
228if index<0 or index>=nval:
229 indexmin=0
230 indexmax=nval
231else:
232 indexmin=index
233 indexmax=index+1
234
235if brief or doubl:
236 name1 = ["","","0.0 "]
237 names = []
238 dm=" "
239 for i in range(indexmax):
240 names += [""]
241else:
242 name1 = ["Noise cell ", "gain ","0.00 "]
243 names = ["RMS ", "pileup ", "RMS1 ", "RMS2 ", "Ratio "]
244 for i in range(len(names),indexmax):
245 names += ["c"+str(i)+" "]
246 dm="\t"
247
248oldBlob = None
249pref = ""
250for iovs in iovList:
251 if iov:
252 pref = "(%i,%i) " % (iovs[0],iovs[1])
253 newBlob = blobReader.getBlob(-1, chan, iovs, False)
254 if oldBlob == newBlob:
255 log.info( f'Cell noise in IOV {iovs} is identical to previous IOV')
256 if comment:
257 log.info( pref + str(blobReader.getComment(iovs)) )
258 continue
259 else:
260 if oldBlob is None:
261 log.info( f'Reading IOV {iovs}')
262 else:
263 log.info( f'Cell noise was updated in IOV {iovs}')
264 oldBlob = newBlob
265 blobFlt = blobReader.getDrawer(-1,chan,iovs,False,False)
266 if comment:
267 log.info( pref + str(blobReader.getComment(iovs)) )
268 for cell in range(cellmin,cellmax):
269 if tile and len(name1[0]):
270 name1[0] = "%s %6s hash " % hashMgr.getNames(cell)
271 for gain in range(gainmin,gainmax):
272 msg="%s%4d %s%d\t" % ( name1[0], cell, name1[1], gain)
273 for index in range(indexmin,indexmax):
274 v=blobFlt.getData(cell, gain, index)
275 if doubl:
276 msg += "%s%s%s" % (names[index],"{0:<15.10g}".format(v).ljust(15),dm)
277 elif v<5.e-7:
278 msg += "%s%s%s" % (names[index],name1[2],dm)
279 elif v<1:
280 msg += "%s%8.6f%s" % (names[index],v,dm)
281 else:
282 msg += "%s%s%s" % (names[index],"{0:<8.7g}".format(v).ljust(8),dm)
283 print (pref+msg)
284