16 from PyCool
import cool
17 from optparse
import OptionParser
19 import PlotCalibrationGains
20 import PlotCalibrationHV
31 dbSvc = cool.DatabaseSvcFactory.databaseService()
33 dbString =
'oracle://ATLAS_COOLPROD;schema=ATLAS_COOLONL_TRIGGER;dbname=CONDBR2'
35 db = dbSvc.openDatabase(dbString,
False)
36 except Exception
as e:
37 print (
'Error: Problem opening database', e)
40 folder_name =
"/TRIGGER/Receivers/Factors/HVCorrections"
41 folder=db.getFolder(folder_name)
43 startUtime =
int(time.time())
44 endUtime =
int(time.time())
47 chsel = cool.ChannelSelection(0,sys.maxsize)
50 itr=folder.browseObjects(startValKey, endValKey, chsel)
51 except Exception
as e:
56 ReceiverId = hex(
int(row.channelId()))
57 payload = row.payload()
58 HVCorrection = payload[
'factor']
78 file_name = ROOT.PathResolver.find_calib_file(
"TrigT1Calo/HVcorrPhysicsWeights_v1.txt")
84 file =
open(file_name)
88 print (
"\ncould not find file: %s ....exiting\n" % file_name)
92 for line
in file.readlines():
119 for n
in range(len(layer_names)):
121 layer = layer_names[n]
152 regions =
"EmbFcalHighEta"
156 for n
in range(len(layer_names)):
158 layer = layer_names[n]
180 totalCorrection = hv_coef / normalisation
182 return totalCorrection
186 UNIX2COOL = 1000000000
188 dbSvc = cool.DatabaseSvcFactory.databaseService()
189 connectString =
'sqlite://;schema='+name+
';dbname=L1CALO'
191 print (
'\nrecreating database file:',name)
192 dbSvc.dropDatabase( connectString )
193 db = dbSvc.createDatabase( connectString )
195 spec = cool.RecordSpecification()
196 spec.extend(
"factor", cool.StorageType.Float)
197 spec.extend(
"status", cool.StorageType.UInt32 )
198 folderSpec = cool.FolderSpecification(cool.FolderVersioning.SINGLE_VERSION, spec)
200 now =
int(time.time())
202 since = now*UNIX2COOL
205 until = cool.ValidityKeyMax
206 db.createFolderSet(
'/TRIGGER')
207 db.createFolderSet(
'/TRIGGER/Receivers')
208 db.createFolderSet(
'/TRIGGER/Receivers/Factors')
210 folder_description =
'<timeStamp>time</timeStamp><addrHeader><address_header service_type="71" clid="1238547719"/></addrHeader><typeName>CondAttrListCollection</typeName>'
211 f = db.createFolder(
"/TRIGGER/Receivers/Factors/HVCorrections", folderSpec, folder_description)
213 print (
" Now creating sqlite file for ", len(
list(input_dict.keys())),
" channels")
214 for i
in list(input_dict.keys()):
215 data = cool.Record( spec )
216 data[
'factor'] = input_dict[i][0]
217 data[
'status'] = input_dict[i][1]
218 f.storeObject(since,until, data,
int(i,16) )
223 if __name__ ==
"__main__":
225 print (
" Starting script for calculating L1Calo HV corrections")
229 parser = OptionParser(add_help_option=
False)
231 parser.add_option(
"-i",
"--hv_input", action =
"store", type =
"string", dest =
"hv_input", default =
"")
232 parser.add_option(
"-t",
"--hv_corr_diff", action =
"store", type =
"float", dest =
"hv_corr_diff", default = 0.01)
233 parser.add_option(
"-c",
"--channel_list", action =
"store", type =
"string", dest =
"channel_list", default =
"")
234 parser.add_option(
"-o",
"--output_files", action =
"store", type =
"string", dest =
"output_files", default =
"doL1CaloHVCorrections")
236 parser.add_option(
"--noFCAL", action =
"store_true", dest =
"noFCAL", default =
False)
237 parser.add_option(
"--noFCAL23",action =
"store_true", dest =
"noFCAL23", default =
False)
239 parser.add_option(
"-h",
"--help", action =
"store_true")
241 (options, args) = parser.parse_args()
245 print (
"\nusage: python doL1CaloHVCorrections.py [options]")
247 print (
"\noptions:\n" )
249 print (
"-i, --hv_input hv input .sqlite file (default: '') *")
250 print (
"-t, --hv_corr_diff minimum abs. change in hv corrections (default: 0.01)")
251 print (
"-c, --channel_list file containing an input channel list (default: '')")
252 print (
"-o, --output_files name assigned to all the output files (default: doL1CaloHVCorrections)")
253 print (
"--noFCAL receiver channels in FCAL not considered")
254 print (
"--noFCAL23 receiver channels in hadronic FCAL (FCAL23) not considered")
257 print (
"\n[*] minimal requirements")
259 print (
"\nexample: python doL1CaloHVCorrections.py -i file_1 ")
261 print (
"\nnote: if no input channel list is given all the channels will be considered by default")
263 print (
"\n(see https://twiki.cern.ch/twiki/bin/save/Atlas/LevelOneCaloGainPredictor for more info)\n")
267 if options.hv_input ==
"" :
268 print (
"\ntoo few input arguments given ....exiting , need at least sqlite file with input HV corrections\n")
274 ROOT.gStyle.SetPalette(1)
275 ROOT.gStyle.SetOptStat(111111)
276 ROOT.gStyle.SetCanvasColor(10)
278 c1 = ROOT.TCanvas(
'c1',
'Example',200,10,700,500)
304 geometry_convertor.LoadReceiverPPMMap()
310 print (
"Creating output file", options.output_files+
".txt")
311 output_text =
open(options.output_files+
".txt",
"w")
316 if options.channel_list !=
"":
320 channel_list =
open(options.channel_list)
324 print (
"\ncould not find file: %s ....exiting\n" % options.channel_list)
328 print (
"\nreading input channel list from:", options.channel_list)
332 for channel
in channel_list.readlines():
334 parts = channel.split()
335 receiver_list.append(parts[0])
341 print (
"\nreading input channel list from oracle database")
343 receiver_list =
list(geometry_convertor.receiver_to_ppm_map.keys())
347 if not receiver_list:
349 print (
"\ninput channel list is empty ....exiting\n")
353 print (
"\nsearching %s channels for hv correction changes, at least one layer > %s" % (len(receiver_list), options.hv_corr_diff))
357 calculatedCorrections = {}
359 for receiver
in receiver_list:
361 if receiver
not in list(hv_input.GetNLayers().
keys()):
366 coolid = geometry_convertor.getPPMfromReceiver(receiver)
368 eta_bin = geometry_convertor.getEtaBin(coolid)
369 phi_bin = geometry_convertor.getPhiBin(coolid)
371 if options.noFCAL
and geometry_convertor.isPPMFCAL(coolid):
374 if options.noFCAL23
and geometry_convertor.isPPMFCAL(coolid)
and geometry_convertor.isCoolHad(coolid):
379 num_layers = (hv_input.GetNLayers())[receiver]
381 layer_names = [-1,-1,-1,-1]
384 layer_names[0] = (hv_input.GetName1()[receiver])
386 layer_names[1] = (hv_input.GetName2()[receiver])
388 layer_names[2] = (hv_input.GetName3()[receiver])
390 layer_names[3] = (hv_input.GetName4()[receiver])
394 layer_corr = [1.,1.,1.,1.]
396 if receiver
in list(hv_input.GetMeanCorections().
keys()):
399 layer_corr[0] = (hv_input.GetCorLayer1()[receiver])
401 layer_corr[1] = (hv_input.GetCorLayer2()[receiver])
403 layer_corr[2] = (hv_input.GetCorLayer3()[receiver])
405 layer_corr[3] = (hv_input.GetCorLayer4()[receiver])
409 predictedCorrection = correctionCalculator.GetCorrection(receiver, layer_corr, layer_names)
410 referenceCorrection = referenceCorrectionReader.getCorrection(receiver)
412 correctionDifference = (predictedCorrection-referenceCorrection)
416 if abs(correctionDifference) <= options.hv_corr_diff:
419 calculatedCorrections[receiver] = [predictedCorrection,0]
420 print ((
"%5s %9s %3i %2i %.3f (%.3f)") % (receiver, coolid, eta_bin, phi_bin, predictedCorrection,referenceCorrection), file=output_text)
423 if geometry_convertor.isCoolEm(coolid):
424 if not geometry_convertor.isPPMOverlap(coolid):
425 h_corrEmb_em.Fill(eta_bin,phi_bin,predictedCorrection)
426 h_RefcorrEmb_em.Fill(eta_bin,phi_bin,referenceCorrection)
427 h_DiffcorrEmb_em.Fill(eta_bin,phi_bin,correctionDifference)
429 if geometry_convertor.getOverlapLayer(receiver)==
'EMB':
430 h_corrEmb_em.Fill(eta_bin,phi_bin,predictedCorrection)
431 h_RefcorrEmb_em.Fill(eta_bin,phi_bin,referenceCorrection)
432 h_DiffcorrEmb_em.Fill(eta_bin,phi_bin,correctionDifference)
434 h_corrEmec_em.Fill(eta_bin,phi_bin,predictedCorrection)
435 h_RefcorrEmec_em.Fill(eta_bin,phi_bin,referenceCorrection)
436 h_DiffcorrEmec_em.Fill(eta_bin,phi_bin,correctionDifference)
439 if geometry_convertor.isCoolHad(coolid):
440 if not geometry_convertor.isPPMFCAL(coolid):
441 h_corrFcalLowEta_had.Fill(eta_bin,phi_bin,predictedCorrection)
442 h_RefcorrFcalLowEta_had.Fill(eta_bin,phi_bin,referenceCorrection)
443 h_DiffcorrFcalLowEta_had.Fill(eta_bin,phi_bin,correctionDifference)
445 if geometry_convertor.getFCAL23RecEta(receiver)==
'HighEta':
446 h_corrFcalHighEta_had.Fill(eta_bin,phi_bin,predictedCorrection)
447 h_RefcorrFcalHighEta_had.Fill(eta_bin,phi_bin,referenceCorrection)
448 h_DiffcorrFcalHighEta_had.Fill(eta_bin,phi_bin,correctionDifference)
450 h_corrFcalLowEta_had.Fill(eta_bin,phi_bin,predictedCorrection)
451 h_RefcorrFcalLowEta_had.Fill(eta_bin,phi_bin,referenceCorrection)
452 h_DiffcorrFcalLowEta_had.Fill(eta_bin,phi_bin,correctionDifference)
459 h_corrEmb_em.SetMinimum(1.)
460 h_corrEmb_em.SetMaximum(2.1)
462 c1.Print(options.output_files+
".ps(")
464 h_corrFcalLowEta_had.SetMinimum(1.)
465 h_corrFcalLowEta_had.SetMaximum(2.1)
466 h_corrFcalLowEta_had.Draw()
467 c1.Print(options.output_files+
".ps")
470 h_corrEmec_em.SetMinimum(1.)
471 h_corrEmec_em.SetMaximum(2.1)
473 c1.Print(options.output_files+
".ps")
475 h_corrFcalHighEta_had.SetMinimum(1.)
476 h_corrFcalHighEta_had.SetMaximum(2.1)
477 h_corrFcalHighEta_had.Draw()
478 c1.Print(options.output_files+
".ps")
482 h_RefcorrEmb_em.SetMinimum(1.)
483 h_RefcorrEmb_em.SetMaximum(2.1)
484 h_RefcorrEmb_em.Draw()
485 c1.Print(options.output_files+
".ps")
487 h_RefcorrFcalLowEta_had.SetMinimum(1.)
488 h_RefcorrFcalLowEta_had.SetMaximum(2.1)
489 h_RefcorrFcalLowEta_had.Draw()
490 c1.Print(options.output_files+
".ps")
493 h_RefcorrEmec_em.SetMinimum(1.)
494 h_RefcorrEmec_em.SetMaximum(2.1)
495 h_RefcorrEmec_em.Draw()
496 c1.Print(options.output_files+
".ps")
498 h_RefcorrFcalHighEta_had.SetMinimum(1.)
499 h_RefcorrFcalHighEta_had.SetMaximum(2.1)
500 h_RefcorrFcalHighEta_had.Draw()
501 c1.Print(options.output_files+
".ps")
504 h_DiffcorrEmb_em.SetMinimum(-0.1)
505 h_DiffcorrEmb_em.SetMaximum(0.1)
506 h_DiffcorrEmb_em.Draw()
507 c1.Print(options.output_files+
".ps")
509 h_DiffcorrFcalLowEta_had.SetMinimum(-0.1)
510 h_DiffcorrFcalLowEta_had.SetMaximum(0.1)
511 h_DiffcorrFcalLowEta_had.Draw()
512 c1.Print(options.output_files+
".ps")
515 h_DiffcorrEmec_em.SetMinimum(-0.1)
516 h_DiffcorrEmec_em.SetMaximum(0.1)
517 h_DiffcorrEmec_em.Draw()
518 c1.Print(options.output_files+
".ps")
520 h_DiffcorrFcalHighEta_had.SetMinimum(-0.1)
521 h_DiffcorrFcalHighEta_had.SetMaximum(0.1)
522 h_DiffcorrFcalHighEta_had.Draw()
523 c1.Print(options.output_files+
".ps)")
525 os.system(
"ps2pdf " + options.output_files+
".ps")