ATLAS Offline Software
Loading...
Searching...
No Matches
WriteLumiToCool.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
4#
5# WriteLumiToCool.py
6# Sanya Solodkov <Sanya.Solodkov@cern.ch>
7#
8# Purpose: Manual update of lumi values in CALO COOL DB
9#
10
11import getopt,sys,os
12os.environ['TERM'] = 'linux'
13
14def usage():
15 print ("Usage: ",sys.argv[0]," [OPTION] ... ")
16 print ("Prepare sqlite file with Lumi values for CALO database")
17 print ("")
18 print ("-h, --help shows this help")
19 print ("-i, --infile= specify the input sqlite file for comparison or full schema string")
20 print ("-o, --outfile= specify the output sqlite file")
21 print ("-t, --tag= specify the tag")
22 print ("-f, --folder= specify folder to use e.g. /CALO/Ofl/Noise/PileUpNoiseLumi ")
23 print ("-d, --dbname= specify the database name e.g. OFLP200")
24 print ("-S, --server= specify server - ORACLE or FRONTIER, default is FRONTIER")
25 print ("-x, --txtfile= specify the text file with the new lumi constants")
26 print ("-v, --value= specify new lumi value")
27 print ("-V, --value2= specify new valid flag")
28 print ("-c, --channel= specify COOL channel, by default COOL channels 0 and 1 are used")
29 print ("-r, --run= specify run number for start of IOV")
30 print ("-l, --lumi= specify lumiblock number for start of IOV, default is 0")
31 print ("-u --update set this flag if output sqlite file should be updated, otherwise it'll be recreated")
32
33letters = "hi:o:t:f:d:S:x:v:V:c:r:l:u"
34keywords = ["help","infile=","outfile=","tag=","folder=","dbname=","server=","txtfile=","value=","value2=","channel=","run=","lumi=","update"]
35
36try:
37 opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
38except getopt.GetoptError as err:
39 print (str(err))
40 usage()
41 sys.exit(2)
42
43# defaults
44inFile = 'COOLOFL_CALO/CONDBR2'
45outFile = 'caloSqlite.db'
46tag = ''
47folderPath = ''
48dbName = 'CONDBR2'
49server = ''
50txtFile = ''
51value = None
52value2 = None
53run = -1
54lumi = 0
55update = False
56channels = [0,1]
57
58for o, a in opts:
59 a = a.strip()
60 if o in ("-i","--infile"):
61 inFile = a
62 elif o in ("-o","--outfile"):
63 outFile = a
64 elif o in ("-t","--tag"):
65 tag = a
66 elif o in ("-f","--folder"):
67 folderPath = a
68 elif o in ("-d","--dbname"):
69 dbName = a
70 elif o in ("-S","--server"):
71 server = a
72 elif o in ("-c","--channel"):
73 channels = [int(a)]
74 elif o in ("-r","--run"):
75 run = int(a)
76 elif o in ("-l","--lumi"):
77 lumi = int(a)
78 elif o in ("-u","--update"):
79 update = True
80 elif o in ("-x","--txtfile"):
81 txtFile = a
82 elif o in ("-v","--value"):
83 value = a
84 elif o in ("-V","--value2"):
85 value2 = a
86 elif o in ("-h","--help"):
87 usage()
88 sys.exit(2)
89 else:
90 print (o, a)
91 usage()
92 sys.exit(2)
93
94#=== check presence of all parameters
95print ("")
96if len(inFile)<1:
97 raise Exception("Please, provide infile (e.g. --infile=COOLOFL_TILE/OFLP200)")
98if len(outFile)<1:
99 raise Exception("Please, provide outfile (e.g. --outfile=caloSqlite.db)")
100if len(tag)<1:
101 raise Exception("Please, provide tag (e.g. --tag=RUN2-UPD4-04)")
102if len(dbName)<1:
103 raise Exception("Please, provide dbname (e.g. --dbname=OFLP200 or --dbname=CONDBR2)")
104if not txtFile and value is None:
105 raise Exception("Please, provide input file (e.g. --txtfile=lumi.txt) or value (e.g. --value=12345)")
106if run<0:
107 raise Exception("Please, provide run number (e.g. --run=123456)")
108
109from PyCool import cool
110from CaloCondBlobAlgs import CaloCondTools, CaloCondLogger
111
112#=== get a logger
113log = CaloCondLogger.getLogger("WriteLumi")
114import logging
115log.setLevel(logging.DEBUG)
116
117#=== Set tag and schema name:
118
119if tag=="HEAD":
120 tag=""
121
122if os.path.isfile(inFile):
123 ischema = 'sqlite://;schema='+inFile+';dbname='+dbName
124else:
125 log.info("File %s was not found, assuming it's full schema string" , inFile)
126 ischema = inFile
127 # possible strings for inFile - full schema connection string or short names like
128 # COOLONL_CALO/CONDBR2 COOLOFL_CALO/CONDBR2 COOLOFL_CALO/OFLP200
129
130#=== Open DB connections
131oschema = 'sqlite://;schema='+outFile+';dbname='+dbName
132dbr = CaloCondTools.openDbConn(ischema,server)
133update = update or (inFile==outFile)
134dbw = CaloCondTools.openDbConn(oschema,('UPDATE' if update else 'RECREATE'))
135
136if len(folderPath)==0:
137 if 'ONL' in ischema:
138 folderPath = '/CALO/Noise/PileUpNoiseLumi'
139 else:
140 folderPath = '/CALO/Ofl/Noise/PileUpNoiseLumi'
141
142from TileCalibBlobPython import TileCalibTools
143folderTag = TileCalibTools.getFolderTag(dbr, folderPath, tag )
144
145#=== creating folder specifications
146spec = cool.RecordSpecification()
147spec.extend( 'LBAvInstLumi', cool.StorageType.Float )
148spec.extend( 'Valid', cool.StorageType.UInt32 )
149
150multiVersion=(len(folderTag)>0)
151
152if multiVersion:
153 folderMode = cool.FolderVersioning.MULTI_VERSION
154 folderSpec = cool.FolderSpecification(folderMode, spec)
155else:
156 folderMode = cool.FolderVersioning.SINGLE_VERSION
157 folderSpec = cool.FolderSpecification(folderMode, spec)
158
159log.info( "Using folder %s with tag %s", folderPath,folderTag )
160folderDescr = CaloCondTools.getAthenaFolderDescr()
161folderR = dbr.getFolder(folderPath)
162if update:
163 try:
164 folderW = dbw.getFolder(folderPath)
165 except Exception:
166 folderW = dbw.createFolder(folderPath, folderSpec, folderDescr, True)
167else:
168 folderW = dbw.createFolder(folderPath, folderSpec, folderDescr, True)
169
170newiov = "[%d,%d] - infinity" % (run, lumi)
171since = CaloCondTools.iovFromRunLumi( run, lumi )
172until = CaloCondTools.iovFromRunLumi( CaloCondTools.MAXRUN, CaloCondTools.MAXLBK )
173
174input = {}
175for chan in channels:
176 input[chan] = ['keep','keep']
177 if value is not None:
178 input[chan][0] = value
179 if value2 is not None:
180 input[chan][1] = value2
181
182if len(txtFile):
183 try:
184 with open(txtFile,"r") as f:
185 allData = f.readlines()
186 except Exception:
187 print("\nCan not read input file %s" % (txtFile))
188 sys.exit(2)
189
190 for line in allData:
191 fields = line.strip().split()
192 #=== ignore empty and comment lines
193 if not len(fields):
194 continue
195 if fields[0].startswith("#"):
196 continue
197 try:
198 ind = 0
199 ch = int(fields[0])
200 for fld in fields[1:3]:
201 for chan in (channels if ch<0 else [ch]):
202 input[chan][ind] = fld
203 ind += 1
204 except (TypeError,ValueError):
205 log.error(f"Can not process line '{line.strip()}' - skipping")
206
207for chan in channels:
208 try:
209 obj = folderR.findObject( since, chan, folderTag )
210 (sinceRun,sinceLum) = CaloCondTools.runLumiFromIov(obj.since())
211 (untilRun,untilLum) = CaloCondTools.runLumiFromIov(obj.until())
212 val1 = obj.payload()[0]
213 val2 = obj.payload()[1]
214 oldiov = "[%d,%d] - (%d,%d)" % (sinceRun,sinceLum,untilRun,untilLum)
215 except Exception:
216 val1 = None
217 val2 = None
218 oldiov = "[NaN,NaN] - (NaN,NaN)"
219 #log.warning("IOV [%d,%d] was not found in input DB", run, lumi )
220
221 try:
222 newval1 = abs(float(input[chan][0]))
223 except (TypeError,ValueError):
224 newval1 = val1 if val1 is not None else 0.0
225 try:
226 newval2 = abs(int(float(input[chan][1])))
227 except (TypeError,ValueError):
228 newval2 = val2 if val2 is not None else 0
229
230 print("COOL channel",chan,"old iov",oldiov," old values [",val1,",",val2,
231 "] new values [",newval1,",",newval2,"] new iov",newiov)
232
233 spec = folderW.payloadSpecification()
234 data = cool.Record( spec )
235 data['LBAvInstLumi'] = newval1
236 data['Valid'] = newval2
237 folderW.storeObject(since, until, data, chan, folderTag, multiVersion)
238
239#=== Cleanup
240dbw.closeDatabase()
241dbr.closeDatabase()
void print(char *figname, TCanvas *c1)
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177