3 from __future__
import print_function
9 from PyCool
import cool
10 from optparse
import OptionParser
25 print (
" Loading forced file", forced_file_name)
28 myfile =
open(forced_file_name,
'r')
29 for line
in myfile.readlines():
32 line_cont = line.split(
' ')
33 line_cont = [iii
for iii
in line_cont
if not iii ==
'']
34 rec_chan = line_cont[0]
36 new_hv = [
float(line_cont[iii])
for iii
in range(2,6)]
39 print (
"updating channel ", rec_chan,
" to HV ", new_hv )
44 print (
" Loading reference HV file", ref_file_name)
46 dbSvc = cool.DatabaseSvcFactory.databaseService()
48 dbString=
'sqlite://;schema='+ref_file_name+
';dbname=L1CALO'
50 db = dbSvc.openDatabase(dbString,
False)
51 except Exception
as e:
52 print (
'Error: Problem opening database', e)
55 folder_name =
'/TRIGGER/L1Calo/V1/Results/HVCorrections'
56 folder=db.getFolder(folder_name)
58 startUtime =
int(time.time())
59 endUtime =
int(time.time())
62 chsel = cool.ChannelSelection(0,sys.maxint)
65 itr=folder.browseObjects(startValKey, endValKey, chsel)
66 except Exception
as e:
72 Affected_cells = 4*[0]
74 CoolId = hex(
int(row.channelId()))
75 payload = row.payload()
77 HV_corrections[0] = payload[
'LayerMean1']
78 HV_corrections[1] = payload[
'LayerMean2']
79 HV_corrections[2] = payload[
'LayerMean3']
80 HV_corrections[3] = payload[
'LayerMean4']
82 Affected_cells[0] = struct.unpack(
'B',payload[
'AffectedCells1'])[0]
83 Affected_cells[1] = struct.unpack(
'B',payload[
'AffectedCells2'])[0]
84 Affected_cells[2] = struct.unpack(
'B',payload[
'AffectedCells3'])[0]
85 Affected_cells[3] = struct.unpack(
'B',payload[
'AffectedCells4'])[0]
103 print (
" Reading HV geometry from file", ref_file_name)
105 dbSvc = cool.DatabaseSvcFactory.databaseService()
107 dbString=
'sqlite://;schema='+ref_file_name+
';dbname=L1CALO'
109 db = dbSvc.openDatabase(dbString,
False)
110 except Exception
as e:
111 print (
'Error: Problem opening database', e)
114 folder_name =
'/TRIGGER/L1Calo/V1/Results/RxLayers'
115 folder=db.getFolder(folder_name)
117 startUtime =
int(time.time())
118 endUtime =
int(time.time())
119 startValKey = startUtime * self.
UNIX2COOL
121 chsel = cool.ChannelSelection(0,sys.maxint)
124 itr=folder.browseObjects(startValKey, endValKey, chsel)
125 except Exception
as e:
133 CoolId = hex(
int(row.channelId()))
134 payload = row.payload()
136 self.
NLayers[CoolId] = struct.unpack(
'B',payload[
'NLayers'])[0]
138 HV_layer_names[0] = struct.unpack(
'B',payload[
'Name1'])[0]
139 HV_layer_names[1] = struct.unpack(
'B',payload[
'Name2'])[0]
140 HV_layer_names[2] = struct.unpack(
'B',payload[
'Name3'])[0]
141 HV_layer_names[3] = struct.unpack(
'B',payload[
'Name4'])[0]
143 HV_Ncells[0] = struct.unpack(
'B',payload[
'NCells1'])[0]
144 HV_Ncells[1] = struct.unpack(
'B',payload[
'NCells2'])[0]
145 HV_Ncells[2] = struct.unpack(
'B',payload[
'NCells3'])[0]
146 HV_Ncells[3] = struct.unpack(
'B',payload[
'NCells4'])[0]
149 self.
NCells[CoolId] = HV_Ncells
165 print (
" Updating internal HV list")
194 print (
" Writing geometry to file " , new_file_name)
196 dbSvc = cool.DatabaseSvcFactory.databaseService()
197 connectString =
'sqlite://;schema='+new_file_name+
';dbname=L1CALO'
200 print (
'Writing into database file',new_file_name)
201 db = dbSvc.openDatabase(connectString,
False)
203 spec = cool.RecordSpecification()
204 spec.extend(
'NLayers',cool.StorageType.UChar)
206 spec.extend(
'Name1', cool.StorageType.UChar)
207 spec.extend(
'Name2', cool.StorageType.UChar)
208 spec.extend(
'Name3', cool.StorageType.UChar)
209 spec.extend(
'Name4', cool.StorageType.UChar)
211 spec.extend(
'NCells1',cool.StorageType.UChar)
212 spec.extend(
'NCells2',cool.StorageType.UChar)
213 spec.extend(
'NCells3',cool.StorageType.UChar)
214 spec.extend(
'NCells4',cool.StorageType.UChar)
216 now =
int(time.time())
219 until = cool.ValidityKeyMax
221 folder_description =
"<timeStamp>time</timeStamp><addrHeader><address_header service_type=\"71\" clid=\"1238547719\"/></addrHeader><typeName>CondAttrListCollection</typeName>"
222 f = db.createFolder(
"/TRIGGER/L1Calo/V1/Results/RxLayers" , spec,folder_description)
226 data = cool.Record( spec )
228 data[
'NLayers'] = struct.pack(
'B',self.
NLayers[i])
229 data[
'Name1'] = struct.pack(
'B',self.
LayerName[i][0])
230 data[
'Name2'] = struct.pack(
'B',self.
LayerName[i][1])
231 data[
'Name3'] = struct.pack(
'B',self.
LayerName[i][2])
232 data[
'Name4'] = struct.pack(
'B',self.
LayerName[i][3])
234 data[
'NCells1'] = struct.pack(
'B',self.
NCells[i][0])
235 data[
'NCells2'] = struct.pack(
'B',self.
NCells[i][1])
236 data[
'NCells3'] = struct.pack(
'B',self.
NCells[i][2])
237 data[
'NCells4'] = struct.pack(
'B',self.
NCells[i][3])
239 f.storeObject(since,until, data,
int(i,16) )
248 print (
" Writing output to file " , new_file_name)
250 dbSvc = cool.DatabaseSvcFactory.databaseService()
251 connectString =
'sqlite://;schema='+new_file_name+
';dbname=L1CALO'
253 print (
'recreating database file',new_file_name)
254 dbSvc.dropDatabase( connectString )
255 db = dbSvc.createDatabase( connectString )
257 spec = cool.RecordSpecification()
258 spec.extend(
'RxMean',cool.StorageType.Float)
260 spec.extend(
'AffectedCells1', cool.StorageType.UChar)
261 spec.extend(
'AffectedCells2', cool.StorageType.UChar)
262 spec.extend(
'AffectedCells3', cool.StorageType.UChar)
263 spec.extend(
'AffectedCells4', cool.StorageType.UChar)
265 spec.extend(
'LayerMean1',cool.StorageType.Float)
266 spec.extend(
'LayerMean2',cool.StorageType.Float)
267 spec.extend(
'LayerMean3',cool.StorageType.Float)
268 spec.extend(
'LayerMean4',cool.StorageType.Float)
270 now =
int(time.time())
273 until = cool.ValidityKeyMax
274 db.createFolderSet(
'/TRIGGER')
275 db.createFolderSet(
'/TRIGGER/L1Calo')
276 db.createFolderSet(
'/TRIGGER/L1Calo/V1')
277 db.createFolderSet(
'/TRIGGER/L1Calo/V1/Results')
280 folder_description =
"<timeStamp>time</timeStamp><addrHeader><address_header service_type=\"71\" clid=\"1238547719\"/></addrHeader><typeName>CondAttrListCollection</typeName>"
281 f = db.createFolder(
"/TRIGGER/L1Calo/V1/Results/HVCorrections" , spec,folder_description)
286 data = cool.Record( spec )
291 for ilayer
in range(4):
292 if self.
NCells[i][ilayer]>0:
294 TotalNrCells += self.
NCells[i][ilayer]
296 MeanHV /= TotalNrCells
299 data[
'RxMean'] = MeanHV
311 f.storeObject(since,until, data,
int(i,16) )
317 if __name__ ==
"__main__":
319 print (
"Starting UpdateHVReference")
321 parser = OptionParser()
323 parser.add_option(
"-r",
"--ReferenceFile",action=
"store",type=
"string",dest=
"reference_file",help=
"Name of input file")
324 parser.add_option(
"-f",
"--ForcedChannels",action=
"store",type=
"string",dest=
"forced_file",help=
"Name of input file")
325 parser.add_option(
"-o",
"--OutputFile" ,action=
"store",type=
"string",dest=
"output_file",help=
"Name of input file")
327 (options, args) = parser.parse_args()
332 if options.forced_file:
333 HVUpdatingTool.read_forced_list(options.forced_file)
335 print (
"No forced channel list given, run python UpdateHVReference.py -h for more information")
339 if options.reference_file:
340 HVUpdatingTool.read_HV_reference(options.reference_file)
341 HVUpdatingTool.read_HV_geometry(options.reference_file)
343 print (
"No HV reference file given, run python UpdateHVReference.py -h for more information")
346 HVUpdatingTool.update_HV()
348 if options.output_file:
349 print (
"Writing output into file ", options.output_file)
350 HVUpdatingTool.write_new_file(options.output_file)
351 HVUpdatingTool.write_geometry(options.output_file)
353 print (
"Writing output into file new_hv_ref.sqlite" )
354 HVUpdatingTool.write_new_file(
"new_hv_ref.sqlite")
355 HVUpdatingTool.write_geometry(
"new_hv_ref.sqlite")