3 from __future__
import print_function
5 from optparse
import OptionParser
7 import PlotCalibrationHV
9 import PlotCalibrationGains
11 from PyCool
import cool
17 import mergeEnergyRamps
33 dbSvc = cool.DatabaseSvcFactory.databaseService()
35 dbString =
'oracle://ATLAS_COOLPROD;schema=ATLAS_COOLONL_TRIGGER;dbname=CONDBR2'
37 db = dbSvc.openDatabase(dbString,
False)
38 except Exception
as e:
39 print (
'Error: Problem opening database', e)
42 folder_name =
"/TRIGGER/Receivers/Factors/CalibGains"
43 folder=db.getFolder(folder_name)
45 startUtime =
int(time.time())
46 endUtime =
int(time.time())
49 chsel = cool.ChannelSelection(0,sys.maxint)
52 itr=folder.browseObjects(startValKey, endValKey, chsel)
53 except Exception
as e:
58 ReceiverId = hex(
int(row.channelId()))
59 payload = row.payload()
60 gain = payload[
'factor']
84 file_name= ROOT.PathResolver.find_calib_file(
"TrigT1Calo/HVcorrPhysicsWeights_v1.txt")
90 file =
open(file_name)
94 print (
"\ncould not find file: %s ....exiting\n" % file_name)
98 for line
in file.readlines():
108 def GetGain(self, receiver, orig_gain, layer_corr_ref, layer_corr_new, layer_names):
125 for n
in range(len(layer_names)):
127 layer = layer_names[n]
158 regions =
"EmbFcalHighEta"
162 for n
in range(len(layer_names)):
164 layer = layer_names[n]
186 pred_gain = orig_gain * (hv_coef_new/hv_coef_ref)
191 if __name__ ==
"__main__":
195 parser = OptionParser(add_help_option=
False)
197 parser.add_option(
"-n",
"--hv_input_new", action =
"store", type =
"string", dest =
"hv_input_new", default =
"")
198 parser.add_option(
"-r",
"--hv_input_ref", action =
"store", type =
"string", dest =
"hv_input_ref", default =
"")
199 parser.add_option(
"-t",
"--hv_corr_diff", action =
"store", type =
"float", dest =
"hv_corr_diff", default = 0.01)
200 parser.add_option(
"-c",
"--channel_list", action =
"store", type =
"string", dest =
"channel_list", default =
"")
201 parser.add_option(
"-o",
"--output_files", action =
"store", type =
"string", dest =
"output_files", default =
"gain_updates")
203 parser.add_option(
"-h",
"--help", action =
"store_true")
205 (options, args) = parser.parse_args()
209 print (
"\nusage: python LArHVGainsPredictor.py [options]")
211 print (
"\noptions:\n" )
213 print (
"-n, --hv_input_new new hv input .sqlite file (see below) (default: '') *")
214 print (
"-r, --hv_input_ref ref hv input .sqlite file (see below) (default: '') *")
215 print (
"-t, --hv_corr_diff minimum abs. change in hv corrections (default: 0.01)")
216 print (
"-c, --channel_list file containing an input channel list (default: '')")
217 print (
"-o, --output_files name assigned to all the output files (default: gains_update)")
219 print (
"\n[*] minimal requirements")
221 print (
"\nexample: python LArHVGainsPredictor.py -n file_1 -r file_2")
223 print (
"\nnote: if no input channel list is given all the channels will be considered by default")
225 print (
"\n(see https://twiki.cern.ch/twiki/bin/save/Atlas/LevelOneCaloGainPredictor for more info)\n")
233 if options.hv_input_new ==
"" \
234 or options.hv_input_ref ==
"":
236 print (
"\ntoo few input arguments given ....exiting (see python LArHVGainsPredictor.py -h for more info)\n")
244 print (
"\nreading hv input: %s and hv reference: %s" % (options.hv_input_new, options.hv_input_ref))
250 geometry_convertor.LoadReceiverPPMMap()
269 output_text =
open(options.output_files+
".txt",
"w")
277 if options.channel_list !=
"":
281 channel_list =
open(options.channel_list)
285 print (
"\ncould not find file: %s ....exiting\n" % options.channel_list)
289 print (
"\nreading input channel list from:", options.channel_list)
293 for channel
in channel_list.readlines():
295 parts = channel.split()
297 orig_gains[parts[0]] =
float(parts[1])
301 receiver_list = orig_gains.keys()
305 print (
"\nreading input channel list from oracle database")
309 receiver_list = geometry_convertor.receiver_to_ppm_map.keys()
313 if not receiver_list:
315 print (
"\ninput channel list is empty ....exiting\n")
319 print (
"\nsearching %s channels for hv correction changes > %s" % (len(receiver_list), options.hv_corr_diff))
323 print (
"\npreparing gain updates for the following list of channels:")
325 for receiver
in receiver_list:
327 if receiver
not in hv_input_ref.GetNLayers().
keys():
331 coolid = geometry_convertor.getPPMfromReceiver(receiver)
333 eta_bin = geometry_convertor.getEtaBin(coolid)
334 phi_bin = geometry_convertor.getPhiBin(coolid)
338 num_layers = (hv_input_ref.GetNLayers())[receiver]
340 layer_names = [-1,-1,-1,-1]
343 layer_names[0] = (hv_input_ref.GetName1()[receiver])
345 layer_names[1] = (hv_input_ref.GetName2()[receiver])
347 layer_names[2] = (hv_input_ref.GetName3()[receiver])
349 layer_names[3] = (hv_input_ref.GetName4()[receiver])
353 layer_corr_new = [1.,1.,1.,1.]
355 if receiver
in hv_input_new.GetMeanCorections().
keys():
358 layer_corr_new[0] = (hv_input_new.GetCorLayer1()[receiver])
360 layer_corr_new[1] = (hv_input_new.GetCorLayer2()[receiver])
362 layer_corr_new[2] = (hv_input_new.GetCorLayer3()[receiver])
364 layer_corr_new[3] = (hv_input_new.GetCorLayer4()[receiver])
368 layer_corr_ref = [1.,1.,1.,1.]
370 if receiver
in hv_input_ref.GetMeanCorections().
keys():
373 layer_corr_ref[0] = (hv_input_ref.GetCorLayer1()[receiver])
375 layer_corr_ref[1] = (hv_input_ref.GetCorLayer2()[receiver])
377 layer_corr_ref[2] = (hv_input_ref.GetCorLayer3()[receiver])
379 layer_corr_ref[3] = (hv_input_ref.GetCorLayer4()[receiver])
383 if abs(layer_corr_new[0] - layer_corr_ref[0]) <= options.hv_corr_diff \
384 and abs(layer_corr_new[1] - layer_corr_ref[1]) <= options.hv_corr_diff \
385 and abs(layer_corr_new[2] - layer_corr_ref[2]) <= options.hv_corr_diff \
386 and abs(layer_corr_new[3] - layer_corr_ref[3]) <= options.hv_corr_diff:
392 if options.channel_list !=
"":
394 orig_gain = orig_gains[receiver]
398 orig_gain = orig_gains.getGain(receiver)
402 pred_gain = gain_predictor.GetGain(receiver,orig_gain,layer_corr_ref,layer_corr_new,layer_names)
408 pred_gains[receiver] = [pred_gain, error_code]
410 diff_gain = ((pred_gain - orig_gain) / orig_gain) * 100.0
414 if geometry_convertor.isCoolEm (coolid):
418 h_orig_gains_em. Fill(eta_bin,phi_bin,orig_gain)
419 h_pred_gains_em. Fill(eta_bin,phi_bin,pred_gain)
420 h_diff_gains_em. Fill(eta_bin,phi_bin,diff_gain)
422 if geometry_convertor.isCoolHad(coolid):
426 h_orig_gains_had.Fill(eta_bin,phi_bin,orig_gain)
427 h_pred_gains_had.Fill(eta_bin,phi_bin,pred_gain)
428 h_diff_gains_had.Fill(eta_bin,phi_bin,diff_gain)
432 print ((
"%9s %3s %3i %2i %6s %.3f %.3f %6.1f %% %1i") % (coolid, layer, eta_bin, phi_bin, receiver, orig_gain, pred_gain, diff_gain, num_layers), end=
'')
434 print ((
" [%s]") %
", ".
join(
"%.3f" % x
for x
in layer_corr_ref), end=
'')
436 print ((
" [%s]") %
", ".
join(
"%.3f" % x
for x
in layer_corr_new), end=
'')
438 print ((
" [%s]") %
", ".
join(
"%2i" % x
for x
in layer_names))
442 print ((
"%6s %.3f %s") % (receiver, pred_gain,
" ".
join(
"%.3f" % x
for x
in layer_corr_new)), file=output_text, end=
'')
444 print ((
" # "), file=output_text, end=
'')
446 print ((
"%9s %3i %2i %3s %.3f") % (coolid, eta_bin, phi_bin, layer, orig_gain), file=output_text, end=
'')
448 print ((
" %s") %
" ".
join(
"%6.3f" % (layer_corr_new[x] - layer_corr_ref[x])
for x
in range (4)), file=output_text)
458 print (
"\nrecreating text and ps/pdf files: %s and %s" % (options.output_files+
".txt", options.output_files+
".ps/pdf"))
460 canvas = ROOT.TCanvas(
"canvas",
"",200,10,700,500)
462 ROOT.gStyle.SetPalette(1)
463 ROOT.gStyle.SetOptStat(111111)
464 ROOT.gStyle.SetCanvasColor(10)
466 canvas.Print(options.output_files+
".ps[")
468 h_orig_gains_em.Draw()
469 canvas.Print(options.output_files+
".ps")
471 h_orig_gains_had.Draw()
472 canvas.Print(options.output_files+
".ps")
474 h_pred_gains_em.Draw()
475 canvas.Print(options.output_files+
".ps")
477 h_pred_gains_had.Draw()
478 canvas.Print(options.output_files+
".ps")
480 h_diff_gains_em.Draw()
481 canvas.Print(options.output_files+
".ps")
483 h_diff_gains_had.Draw()
484 canvas.Print(options.output_files+
".ps")
486 canvas.Print(options.output_files+
".ps]")
488 os.system(
"ps2pdf "+options.output_files+
".ps")
490 print (
"\nfinished.... need a drink now!\n")