ATLAS Offline Software
Loading...
Searching...
No Matches
ReadCellNoiseFromCrestCompare.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
3#
4# ReadCellNoiseFromCrestCompare.py
5# (Based on ReadCellNoiseFromCoolCompare.py, adapted for CREST)
6# Ekaterina Ramakoti 2026-05-21
7#
8
9import sys,os
10os.environ['TERM'] = 'linux'
11
12#------------------------------- parse arguments and change defaults
13import argparse
14
15parser = argparse.ArgumentParser(
16 prog='ReadCellNoiseFromCrestCompare.py',
17 description='Dumps noise constants from online or offline folders/tags in CREST and compares two sources',
18 formatter_class=argparse.RawDescriptionHelpFormatter,
19 epilog="""
20Notes:
21- Conditions are compared using logical AND: both --maxdiff AND --maxdiffpercent
22 thresholds must be exceeded (both have to be TRUE) to print a difference
23- If *2 parameters (run2, lumi2, folder2, tag2, schema2) are not given,
24 values from primary parameters are used automatically
25- Use --cell, --gain, --index with -1 to process all values
26
27Usage:
28 # Compare two tags from same folder
29 python %(prog)s -f /TILE/OFL02/NOISE/CELL -t UPD4 -t2 UPD1 -r 497970
30
31 # Compare specific cell/gain with thresholds
32 python %(prog)s -f /TILE/OFL02/NOISE/CELL -t UPD4 -c 123 -g 0 --maxdiff 0.001 --maxdiffpercent 5.0
33
34 # Wide format, brief output, all cells
35 python %(prog)s -f /TILE/OFL02/NOISE/CELL -t UPD4 -w -b -r 497970
36 """
37)
38
39# primary source parameters
40parser.add_argument('-s', '--schema', default='CREST',
41 help='CREST server path or schema (default: CREST from env)')
42parser.add_argument('-f', '--folder', default='/TILE/OFL02/NOISE/CELL',
43 help='Status folder to use, e.g., /TILE/OFL02/NOISE/CELL')
44parser.add_argument('-t', '--tag', default='UPD4',
45 help='Tag to use, e.g., UPD4 or tag suffix like 14TeV-N200_dT50-01')
46parser.add_argument('-r', '--run', type=int, default=2147483647,
47 help='Run number (default: 2147483647, uses latest IOV)')
48parser.add_argument('-l', '--lumi', type=int, default=0,
49 help='Luminosity block number (default: 0)')
50
51# secondary parameters (*2) - default to primary values if not specified
52parser.add_argument('-s2', '--schema2', default='none',
53 help='Second schema for comparison (default: same as --schema)')
54parser.add_argument('-f2', '--folder2', default='none',
55 help='Second folder for comparison (default: same as --folder)')
56parser.add_argument('-t2', '--tag2', default='none',
57 help='Second tag for comparison (default: same as --tag)')
58parser.add_argument('-r2', '--run2', type=int, default=0,
59 help='Second run number (default: same as --run)')
60parser.add_argument('-l2', '--lumi2', type=int, default=-1,
61 help='Second lumi block (default: same as --lumi)')
62
63# threshold parameters
64parser.add_argument('-m', '--maxdiff', type=float, default=-1.0,
65 help='Absolute maximal difference to compare constants (default: -1.0 = dump all)')
66parser.add_argument('-m2', '--maxdiffpercent', type=float, default=-1.0,
67 help='Maximal difference in percent to compare constants (default: -1.0 = dump all)')
68
69# others parameters
70parser.add_argument('-n', '--channel', type=int, default=48,
71 help='COOL channel to read (default: 48 for Tile)')
72parser.add_argument('-c', '--cell', type=int, default=-1,
73 help='Cell hash 0-5183 (default: -1 = all cells)')
74parser.add_argument('-g', '--gain', type=int, default=-1,
75 help='Gain 0-3 (default: -1 = all gains)')
76parser.add_argument('-i', '--index', type=int, default=-1,
77 help='Parameter index 0-4 (default: -1 = all parameters)')
78parser.add_argument('-z', '--zero', type=float, default=5e-7,
79 help='Zero threshold: treat DB values below this as zeros (default: 5e-7)')
80parser.add_argument('-w', '--wide', action='store_true', default=False,
81 help='Wide format: print all values per cell in one line')
82parser.add_argument('-b', '--brief', action='store_true', default=False,
83 help='Brief output: print only numbers without character names')
84parser.add_argument('-d', '--double', action='store_true', default=False,
85 help='Print values with double precision')
86parser.add_argument('--stdout', '-o', action='store_true',
87 help='Print differences to standard output (in addition to file)')
88
89args = parser.parse_args()
90
91# apply default logic for *2 parameters (can be modified from command line)
92run = args.run
93run2 = args.run2 if args.run2 != 0 else run
94lumi = args.lumi
95lumi2 = args.lumi2 if args.lumi2 >= 0 else lumi
96schema = args.schema
97schema2 = args.schema2 if args.schema2 != "none" else schema
98folderPath = args.folder
99folderPath2 = args.folder2 if args.folder2 != "none" else folderPath
100tag = args.tag
101tag2 = args.tag2 if args.tag2 != "none" else tag
102maxdiff = args.maxdiff
103maxdiffpercent = args.maxdiffpercent
104chan = args.channel
105cell = args.cell
106gain = args.gain
107index = args.index
108zthr = args.zero
109multi = not args.wide # --wide sets multi=False
110brief = args.brief
111doubl = args.double
112print_to_stdout = args.stdout
113
114tile=(chan==48)
115
116print("\n" + "="*65)
117print(" ReadCellNoiseFromCrestCompare - Configuration")
118print("="*65)
119print(f" {'Run/Lumi:':<14} 1st: run={run:<10} lumi={lumi:<6}")
120print(f" {' ':<14} 2nd: run={run2:<10} lumi={lumi2:<6}")
121print(f" {'Folder:':<14} {folderPath}")
122print(f" {'Folder2:':<14} {folderPath2}")
123print(f" {'Tag:':<14} {tag}")
124print(f" {'Tag2:':<14} {tag2}")
125print(f" {'Schema:':<14} {schema}")
126print(f" {'Schema2:':<14} {schema2}")
127print(f" {'Channel:':<14} {chan}")
128print(f" {'Cell/Gain/Idx:':<14} {cell}/{gain}/{index}")
129print(f" {'Thresholds:':<14} maxdiff={maxdiff:<8} maxdiffpercent={maxdiffpercent}")
130print(f" {'Zero thr:':<14} {zthr:.2e}")
131print(f" {'Format:':<14} wide={args.wide}, brief={brief}, double={doubl}")
132print("="*65 + "\n")
133
134from CaloCondBlobAlgs import CaloCondLogger
135from TileCalibBlobPython import TileCalibCrest
136from TileCalibBlobPython import TileCellTools
137
138#=== get a logger
139log = CaloCondLogger.getLogger("ReadCellNoise")
140import logging
141log.setLevel(logging.DEBUG)
142
143if run>=400000:
144 cabling = 'RUN3'
145elif run>=342550:
146 cabling = 'RUN2a'
147elif run>=222222:
148 cabling = 'RUN2'
149else:
150 cabling = 'RUN1'
151# hashMgr=TileCellTools.TileCellHashMgr(cabling=cabling)
152hashMgrDef=TileCellTools.TileCellHashMgr(cabling=cabling)
153hashMgrA=TileCellTools.TileCellHashMgr("UpgradeA")
154hashMgrBC=TileCellTools.TileCellHashMgr("UpgradeBC")
155hashMgrABC=TileCellTools.TileCellHashMgr("UpgradeABC")
156
157#=== Initialize blob reader for sources
158folderTag = tag
159tag_upper = tag.upper()
160tag1 = tag_upper.split('_')[1][:4] if '_' in tag_upper else tag_upper[:4]
161if tag1 == "TILE" or tag1 == "CALO" or tag1[:3] == "LAR" or tag.startswith("TILE") or tag.startswith("CALO") or tag.startswith("LAR"):
162 folderPath1=""
163else:
164 folderPath1=folderPath
165if not os.path.isfile(schema):
166 log.info("Initializing folder %s with tag %s (%s)", folderPath1, folderTag, tag1)
167
168folderTag2 = tag2
169tag2_upper = tag2.upper()
170tag2_1 = tag2_upper.split('_')[1][:4] if '_' in tag2_upper else tag2_upper[:4]
171if tag2_1 == "TILE" or tag2_1 == "CALO" or tag2_1[:3] == "LAR" or tag2.startswith("TILE") or tag2.startswith("CALO") or tag2.startswith("LAR"):
172 folderPath2=""
173else:
174 folderPath2=folderPath2
175if not os.path.isfile(schema2):
176 log.info("Initializing folder2 %s with tag2 %s (%s)", folderPath2, folderTag2, tag2_1)
177
178try:
179 blobReader = TileCalibCrest.TileBlobReaderCrest(schema, folderPath1, folderTag, run, lumi)
180 log.debug("Connecting to DB: schema=%s, folder=%s, tag=%s, run=%s, lumi=%s",
181 schema, folderPath1, folderTag, run, lumi)
182 log.info("Comment1: %s", blobReader.getComment((run,lumi)))
183 blobReader2 = TileCalibCrest.TileBlobReaderCrest(schema2, folderPath2, folderTag2, run2, lumi2)
184 log.debug("Connecting to DB2: schema=%s, folder=%s, tag=%s, run=%s, lumi=%s",
185 schema2, folderPath2, folderTag2, run2, lumi2)
186 log.info("Comment1: %s", blobReader2.getComment((run2,lumi2)))
187except Exception as e:
188 log.error("Initialization failed: %s", e)
189 sys.exit(1)
190
191#=== create CaloCondBlobFlt
192blobFlt = blobReader.getDrawer(-1,chan,None,False,False)
193if blobFlt is None:
194 log.critical("Could not locate a data blob in CREST payload for COOL channel %s", chan)
195 sys.exit(1)
196
197blobFlt2 = blobReader2.getDrawer(-1,chan,None,False,False)
198if blobFlt2 is None:
199 log.critical("Could not locate a data blob in CREST payload for COOL channel %s", chan)
200 sys.exit(1)
201
202#=== create the output file
203outputfile = 'output_crest.ascii'
204f = open(outputfile, 'w')
205
206f.write("Command line parameters used: \n %s \n\n" % sys.argv[1:])
207f.write("=== Source 1 ===\n")
208f.write("schema : %s\n" % schema)
209f.write("folder : %s\n" % folderPath1)
210f.write("tag : %s\n" % folderTag)
211f.write("run : %d\n" % run)
212f.write("lumi : %d\n" % lumi)
213f.write("channel : %d\n" % chan)
214f.write("comment : %s\n\n" % blobReader.getComment((run,lumi)))
215
216f.write("=== Source 2 ===\n")
217f.write("schema : %s\n" % schema2)
218f.write("folder : %s\n" % folderPath2)
219f.write("tag : %s\n" % folderTag2)
220f.write("run : %d\n" % run2)
221f.write("lumi : %d\n" % lumi2)
222f.write("channel : %d\n" % chan)
223f.write("comment : %s\n\n" % blobReader2.getComment((run2,lumi2)))
224
225f.write("=== Comparison parameters ===\n")
226f.write("maxdiff : %f\n" % maxdiff)
227f.write("maxdiffpercent : %f\n\n" % maxdiffpercent)
228f.write("zero threshold : %e\n\n" % zthr)
229
230
231#=== retrieve data from the blob
232#cell = 0 # 0..5183 - Tile hash
233#gain = 0 # 0..3 - four Tile cell gains: -11, -12, -15, -16
234#index = 0 # 0..4 - electronic or pile-up noise or 2-G noise parameters
235ncell=blobFlt.getNChans()
236ngain=blobFlt.getNGains()
237nval=blobFlt.getObjSizeUint32()
238
239if cell<0 or cell>=ncell:
240 cellmin=0
241 cellmax=ncell
242else:
243 cellmin=cell
244 cellmax=cell+1
245
246if gain<0 or gain>=ngain:
247 gainmin=0
248 gainmax=ngain
249else:
250 gainmin=gain
251 gainmax=gain+1
252
253if index<0 or index>=nval:
254 indexmin=0
255 indexmax=nval
256else:
257 indexmin=index
258 indexmax=index+1
259
260log.info("From DB: ncell: %d ngain %d index nval %d", ncell, ngain, nval)
261
262if brief or doubl:
263 name1 = ["","","0.0 "]
264 names = ["S0 ", "Pl ", "S1 ", "S2 ", "Ra "]
265 dm=" "
266else:
267 name1 = ["Noise cell ", "gain ","0.00 "]
268 names = [" RMS ", "pileup ", " RMS1 ", " RMS2 ", " Ratio "]
269 for i in range(len(names),indexmax):
270 names += ["c"+str(i)+" "]
271 dm="\t"
272
273# Use appropriate hashMgr based on cabling
274if ncell>hashMgrA.getHashMax():
275 hashMgr=hashMgrABC
276elif ncell>hashMgrBC.getHashMax():
277 hashMgr=hashMgrA
278elif ncell>hashMgrDef.getHashMax():
279 hashMgr=hashMgrBC
280else:
281 hashMgr=hashMgrDef
282log.info("Using %s CellMgr with hashMax %d", hashMgr.getGeometry(),hashMgr.getHashMax())
283
284for cell in range(cellmin,cellmax):
285 if tile and len(name1[0]):
286 name1[0] = "%s %6s hash " % hashMgr.getNames(cell)
287 for gain in range(gainmin,gainmax):
288 msg="%s%4d %s%d\t" % ( name1[0], cell, name1[1], gain)
289 l0=len(msg)
290 if multi:
291 dm="\n"+msg
292 for index in range(indexmin,indexmax):
293 v=blobFlt.getData(cell, gain, index)
294 v2=blobFlt2.getData(cell, gain, index)
295 dv12 = v - v2
296 if abs(dv12)<zthr:
297 dv12 = 0
298 if v2 == 0:
299 if v==0:
300 dp12=0
301 else:
302 dp12=100
303 else:
304 dp12=dv12*100./v2
305
306 if abs(dv12) > maxdiff and abs(dp12) > maxdiffpercent:
307 if doubl:
308 s1 = "{0:<14.9g}".format(v) if v<0 else "{0:<15.10g}".format(v)
309 s2 = "{0:<14.9g}".format(v2) if v2<0 else "{0:<15.10g}".format(v2)
310 s3 = "{0:<14.9g}".format(dv12) if dv12<0 else "{0:<15.10g}".format(dv12)
311 s4 = "{0:<14.9g}".format(dp12) if dp12<0 else "{0:<15.10g}".format(dp12)
312 msg += "%s v1 %s v2 %s diff %s diffpercent %s%s" % (names[index],s1.ljust(15),s2.ljust(15),s3.ljust(15),s4.ljust(15),dm)
313 else:
314 s1 = name1[2] if abs(v)<zthr else "%8.6f" % v if abs(v)<1 else "{0:<8.7g}".format(v).ljust(8)
315 s2 = name1[2] if abs(v2)<zthr else "%8.6f" % v2 if abs(v2)<1 else "{0:<8.7g}".format(v2).ljust(8)
316 s3 = name1[2] if abs(dv12)<zthr else "%8.6f" % dv12 if abs(dv12)<1 else "{0:<8.7g}".format(dv12).ljust(8)
317 s4 = name1[2] if abs(dp12)<zthr else "%8.6f" % dp12 if abs(dp12)<1 else "{0:<8.7g}".format(dp12).ljust(8)
318 msg += "%s v1 %s v2 %s diff %s diffpercent %s%s" % (names[index],s1[:8],s2[:8],s3[:8],s4[:8],dm)
319
320 if len(msg)>l0:
321 output_line = msg[:len(msg)-len(dm)]
322 f.write(output_line + "\n")
323 if print_to_stdout:
324 print(output_line)
325
326f.close()
327log.info("Output written to %s", outputfile)
void print(char *figname, TCanvas *c1)