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.upper()
142if folderTAG.startswith("TILE") or folderTAG.startswith("CALO") or folderTAG.startswith("LAR") :
143 folderPath=""
144if not os.path.isfile(schema):
145 log.info("Initializing folder %s with tag %s", folderPath, tag)
146blobReader = TileCalibCrest.TileBlobReaderCrest(schema, folderPath, tag, run, lumi)
147log.info("Comment: %s", blobReader.getComment())
148
149#=== create CaloCondBlobFlt
150blobFlt = blobReader.getDrawer(-1,chan,None,False,False)
151if blobFlt is None:
152 log.critical("Could not locate a data blob in CREST payload for COOL channel %s", chan)
153 sys.exit(1)
154
155#=== retrieve data from the blob
156#cell = 0 # 0..5183 - Tile hash
157#gain = 0 # 0..3 - four Tile cell gains: -11, -12, -15, -16
158#index = 0 # 0..4 - electronic or pile-up noise or 2-G noise parameters
159
160ncell=blobFlt.getNChans()
161ngain=blobFlt.getNGains()
162nval=blobFlt.getObjSizeUint32()
163
164if ncell>hashMgrA.getHashMax():
165 hashMgr=hashMgrABC
166elif ncell>hashMgrBC.getHashMax():
167 hashMgr=hashMgrA
168elif ncell>hashMgrDef.getHashMax():
169 hashMgr=hashMgrBC
170else:
171 hashMgr=hashMgrDef
172log.info("Using %s CellMgr with hashMax %d", hashMgr.getGeometry(),hashMgr.getHashMax())
173
174#=== Filling the iovList
175iovList = []
176if iov:
177 if begin<0:
178 if iovonly or IOVONLY:
179 begin = end
180 else:
181 begin=0
182 iovList = blobReader.getIovs((begin,0),(end,0))
183
184 be=iovList[0][0]
185 en=iovList[-1][0]
186
187 if begin != be or end != en:
188 log.info( "" )
189 if be != begin:
190 log.info( "Changing begin run from %d to %d (start of IOV)", begin,be)
191 begin=be
192 if en != end:
193 if en>end:
194 log.info( "Changing end run from %d to %d (start of next IOV)", end,en)
195 else:
196 log.info( "Changing end run from %d to %d (start of last IOV)", end,en)
197 end=en
198 log.info( "%d IOVs in total", len(iovList))
199
200 #=== IOV only option
201 if iovonly or IOVONLY:
202 option = 1 if iovonly else 0
203 option += (2 if IOVONLY else 0)
204 blobReader.dumpIovs(iovList,-1,0,chan,chan+1,option,tile,False)
205 sys.exit(0)
206else:
207 iovList.append((run,lumi))
208
209log.info( "\n" )
210
211
212if cell<0 or cell>=ncell:
213 cellmin=0
214 cellmax=ncell
215else:
216 cellmin=cell
217 cellmax=cell+1
218
219if gain<0 or gain>=ngain:
220 gainmin=0
221 gainmax=ngain
222else:
223 gainmin=gain
224 gainmax=gain+1
225
226if index<0 or index>=nval:
227 indexmin=0
228 indexmax=nval
229else:
230 indexmin=index
231 indexmax=index+1
232
233if brief or doubl:
234 name1 = ["","","0.0 "]
235 names = []
236 dm=" "
237 for i in range(indexmax):
238 names += [""]
239else:
240 name1 = ["Noise cell ", "gain ","0.00 "]
241 names = ["RMS ", "pileup ", "RMS1 ", "RMS2 ", "Ratio "]
242 for i in range(len(names),indexmax):
243 names += ["c"+str(i)+" "]
244 dm="\t"
245
246oldBlob = None
247pref = ""
248for iovs in iovList:
249 if iov:
250 pref = "(%i,%i) " % (iovs[0],iovs[1])
251 newBlob = blobReader.getBlob(-1, chan, iovs, False)
252 if oldBlob == newBlob:
253 log.info( f'Cell noise in IOV {iovs} is identical to previous IOV')
254 if comment:
255 log.info( pref + str(blobReader.getComment(iovs)) )
256 continue
257 else:
258 if oldBlob is None:
259 log.info( f'Reading IOV {iovs}')
260 else:
261 log.info( f'Cell noise was updated in IOV {iovs}')
262 oldBlob = newBlob
263 blobFlt = blobReader.getDrawer(-1,chan,iovs,False,False)
264 if comment:
265 log.info( pref + str(blobReader.getComment(iovs)) )
266 for cell in range(cellmin,cellmax):
267 if tile and len(name1[0]):
268 name1[0] = "%s %6s hash " % hashMgr.getNames(cell)
269 for gain in range(gainmin,gainmax):
270 msg="%s%4d %s%d\t" % ( name1[0], cell, name1[1], gain)
271 for index in range(indexmin,indexmax):
272 v=blobFlt.getData(cell, gain, index)
273 if doubl:
274 msg += "%s%s%s" % (names[index],"{0:<15.10g}".format(v).ljust(15),dm)
275 elif v<5.e-7:
276 msg += "%s%s%s" % (names[index],name1[2],dm)
277 elif v<1:
278 msg += "%s%8.6f%s" % (names[index],v,dm)
279 else:
280 msg += "%s%s%s" % (names[index],"{0:<8.7g}".format(v).ljust(8),dm)
281 print (pref+msg)
282