4 from optparse
import OptionParser
6 import PlotCalibrationHV
8 import PlotCalibrationGains
10 from PyCool
import cool
16 import mergeEnergyRamps
32 dbSvc = cool.DatabaseSvcFactory.databaseService()
34 dbString =
'oracle://ATLAS_COOLPROD;schema=ATLAS_COOLONL_TRIGGER;dbname=CONDBR2'
36 db = dbSvc.openDatabase(dbString,
False)
37 except Exception
as e:
38 print (
'Error: Problem opening database', e)
41 folder_name =
"/TRIGGER/Receivers/Factors/CalibGains"
42 folder=db.getFolder(folder_name)
44 startUtime =
int(time.time())
45 endUtime =
int(time.time())
48 chsel = cool.ChannelSelection(0,sys.maxint)
51 itr=folder.browseObjects(startValKey, endValKey, chsel)
52 except Exception
as e:
57 ReceiverId = hex(
int(row.channelId()))
58 payload = row.payload()
59 gain = payload[
'factor']
83 file_name= ROOT.PathResolver.find_calib_file(
"TrigT1Calo/HVcorrPhysicsWeights_v1.txt")
89 file =
open(file_name)
93 print (
"\ncould not find file: %s ....exiting\n" % file_name)
97 for line
in file.readlines():
107 def GetGain(self, receiver, orig_gain, layer_corr_ref, layer_corr_new, layer_names):
124 for n
in range(len(layer_names)):
126 layer = layer_names[n]
157 regions =
"EmbFcalHighEta"
161 for n
in range(len(layer_names)):
163 layer = layer_names[n]
185 pred_gain = orig_gain * (hv_coef_new/hv_coef_ref)
190 if __name__ ==
"__main__":
194 parser = OptionParser(add_help_option=
False)
196 parser.add_option(
"-n",
"--hv_input_new", action =
"store", type =
"string", dest =
"hv_input_new", default =
"")
197 parser.add_option(
"-r",
"--hv_input_ref", action =
"store", type =
"string", dest =
"hv_input_ref", default =
"")
198 parser.add_option(
"-t",
"--hv_corr_diff", action =
"store", type =
"float", dest =
"hv_corr_diff", default = 0.01)
199 parser.add_option(
"-c",
"--channel_list", action =
"store", type =
"string", dest =
"channel_list", default =
"")
200 parser.add_option(
"-o",
"--output_files", action =
"store", type =
"string", dest =
"output_files", default =
"gain_updates")
202 parser.add_option(
"-h",
"--help", action =
"store_true")
204 (options, args) = parser.parse_args()
208 print (
"\nusage: python LArHVGainsPredictor.py [options]")
210 print (
"\noptions:\n" )
212 print (
"-n, --hv_input_new new hv input .sqlite file (see below) (default: '') *")
213 print (
"-r, --hv_input_ref ref hv input .sqlite file (see below) (default: '') *")
214 print (
"-t, --hv_corr_diff minimum abs. change in hv corrections (default: 0.01)")
215 print (
"-c, --channel_list file containing an input channel list (default: '')")
216 print (
"-o, --output_files name assigned to all the output files (default: gains_update)")
218 print (
"\n[*] minimal requirements")
220 print (
"\nexample: python LArHVGainsPredictor.py -n file_1 -r file_2")
222 print (
"\nnote: if no input channel list is given all the channels will be considered by default")
224 print (
"\n(see https://twiki.cern.ch/twiki/bin/save/Atlas/LevelOneCaloGainPredictor for more info)\n")
232 if options.hv_input_new ==
"" \
233 or options.hv_input_ref ==
"":
235 print (
"\ntoo few input arguments given ....exiting (see python LArHVGainsPredictor.py -h for more info)\n")
243 print (
"\nreading hv input: %s and hv reference: %s" % (options.hv_input_new, options.hv_input_ref))
249 geometry_convertor.LoadReceiverPPMMap()
268 output_text =
open(options.output_files+
".txt",
"w")
276 if options.channel_list !=
"":
280 channel_list =
open(options.channel_list)
284 print (
"\ncould not find file: %s ....exiting\n" % options.channel_list)
288 print (
"\nreading input channel list from:", options.channel_list)
292 for channel
in channel_list.readlines():
294 parts = channel.split()
296 orig_gains[parts[0]] =
float(parts[1])
300 receiver_list = orig_gains.keys()
304 print (
"\nreading input channel list from oracle database")
308 receiver_list = geometry_convertor.receiver_to_ppm_map.keys()
312 if not receiver_list:
314 print (
"\ninput channel list is empty ....exiting\n")
318 print (
"\nsearching %s channels for hv correction changes > %s" % (len(receiver_list), options.hv_corr_diff))
322 print (
"\npreparing gain updates for the following list of channels:")
324 for receiver
in receiver_list:
326 if receiver
not in hv_input_ref.GetNLayers().
keys():
330 coolid = geometry_convertor.getPPMfromReceiver(receiver)
332 eta_bin = geometry_convertor.getEtaBin(coolid)
333 phi_bin = geometry_convertor.getPhiBin(coolid)
337 num_layers = (hv_input_ref.GetNLayers())[receiver]
339 layer_names = [-1,-1,-1,-1]
342 layer_names[0] = (hv_input_ref.GetName1()[receiver])
344 layer_names[1] = (hv_input_ref.GetName2()[receiver])
346 layer_names[2] = (hv_input_ref.GetName3()[receiver])
348 layer_names[3] = (hv_input_ref.GetName4()[receiver])
352 layer_corr_new = [1.,1.,1.,1.]
354 if receiver
in hv_input_new.GetMeanCorections().
keys():
357 layer_corr_new[0] = (hv_input_new.GetCorLayer1()[receiver])
359 layer_corr_new[1] = (hv_input_new.GetCorLayer2()[receiver])
361 layer_corr_new[2] = (hv_input_new.GetCorLayer3()[receiver])
363 layer_corr_new[3] = (hv_input_new.GetCorLayer4()[receiver])
367 layer_corr_ref = [1.,1.,1.,1.]
369 if receiver
in hv_input_ref.GetMeanCorections().
keys():
372 layer_corr_ref[0] = (hv_input_ref.GetCorLayer1()[receiver])
374 layer_corr_ref[1] = (hv_input_ref.GetCorLayer2()[receiver])
376 layer_corr_ref[2] = (hv_input_ref.GetCorLayer3()[receiver])
378 layer_corr_ref[3] = (hv_input_ref.GetCorLayer4()[receiver])
382 if abs(layer_corr_new[0] - layer_corr_ref[0]) <= options.hv_corr_diff \
383 and abs(layer_corr_new[1] - layer_corr_ref[1]) <= options.hv_corr_diff \
384 and abs(layer_corr_new[2] - layer_corr_ref[2]) <= options.hv_corr_diff \
385 and abs(layer_corr_new[3] - layer_corr_ref[3]) <= options.hv_corr_diff:
391 if options.channel_list !=
"":
393 orig_gain = orig_gains[receiver]
397 orig_gain = orig_gains.getGain(receiver)
401 pred_gain = gain_predictor.GetGain(receiver,orig_gain,layer_corr_ref,layer_corr_new,layer_names)
407 pred_gains[receiver] = [pred_gain, error_code]
409 diff_gain = ((pred_gain - orig_gain) / orig_gain) * 100.0
413 if geometry_convertor.isCoolEm (coolid):
417 h_orig_gains_em. Fill(eta_bin,phi_bin,orig_gain)
418 h_pred_gains_em. Fill(eta_bin,phi_bin,pred_gain)
419 h_diff_gains_em. Fill(eta_bin,phi_bin,diff_gain)
421 if geometry_convertor.isCoolHad(coolid):
425 h_orig_gains_had.Fill(eta_bin,phi_bin,orig_gain)
426 h_pred_gains_had.Fill(eta_bin,phi_bin,pred_gain)
427 h_diff_gains_had.Fill(eta_bin,phi_bin,diff_gain)
431 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=
'')
433 print ((
" [%s]") %
", ".
join(
"%.3f" % x
for x
in layer_corr_ref), end=
'')
435 print ((
" [%s]") %
", ".
join(
"%.3f" % x
for x
in layer_corr_new), end=
'')
437 print ((
" [%s]") %
", ".
join(
"%2i" % x
for x
in layer_names))
441 print ((
"%6s %.3f %s") % (receiver, pred_gain,
" ".
join(
"%.3f" % x
for x
in layer_corr_new)), file=output_text, end=
'')
443 print ((
" # "), file=output_text, end=
'')
445 print ((
"%9s %3i %2i %3s %.3f") % (coolid, eta_bin, phi_bin, layer, orig_gain), file=output_text, end=
'')
447 print ((
" %s") %
" ".
join(
"%6.3f" % (layer_corr_new[x] - layer_corr_ref[x])
for x
in range (4)), file=output_text)
457 print (
"\nrecreating text and ps/pdf files: %s and %s" % (options.output_files+
".txt", options.output_files+
".ps/pdf"))
459 canvas = ROOT.TCanvas(
"canvas",
"",200,10,700,500)
461 ROOT.gStyle.SetPalette(1)
462 ROOT.gStyle.SetOptStat(111111)
463 ROOT.gStyle.SetCanvasColor(10)
465 canvas.Print(options.output_files+
".ps[")
467 h_orig_gains_em.Draw()
468 canvas.Print(options.output_files+
".ps")
470 h_orig_gains_had.Draw()
471 canvas.Print(options.output_files+
".ps")
473 h_pred_gains_em.Draw()
474 canvas.Print(options.output_files+
".ps")
476 h_pred_gains_had.Draw()
477 canvas.Print(options.output_files+
".ps")
479 h_diff_gains_em.Draw()
480 canvas.Print(options.output_files+
".ps")
482 h_diff_gains_had.Draw()
483 canvas.Print(options.output_files+
".ps")
485 canvas.Print(options.output_files+
".ps]")
487 os.system(
"ps2pdf "+options.output_files+
".ps")
489 print (
"\nfinished.... need a drink now!\n")