10 from array
import array
11 from PyCool
import cool
12 from optparse
import OptionParser
17 def __init__(self,title,XaxisTitle="",YaxisTitle=""):
20 self.
xbins =
array(
'd',[-49.5,-44.5,-40.50,-36.5,-32.5,-31.5,-29.5,
21 -27.5,-25.5,-24.5,-23.5,-22.5,-21.5,-20.5,-19.5,
22 -18.5,-17.5,-16.5,-15.5,-14.5,-13.5,-12.5,-11.5,
23 -10.5,-9.5,-8.5,-7.5,-6.5,-5.5,-4.5,-3.5,
24 -2.5,-1.5,-0.5,0.5,1.5,2.5,3.5,4.5,5.5,6.5,
25 7.5,8.5,9.5,10.5,11.5,12.5,13.5,14.5,15.5,16.5,
26 17.5,18.5,19.5,20.5,21.5,22.5,23.5,24.5,26.5,28.5,
27 30.5,31.5,35.5,39.5,43.5,47.5])
31 self.
h_1.GetXaxis().SetTitle(XaxisTitle)
32 self.
h_1.GetYaxis().SetTitle(YaxisTitle)
37 ROOT.gPad.RedrawAxis()
39 def Fill(self,eta,phi,gain=1):
41 if eta >= 32
or eta < -32:
46 elif eta >= 25
or eta < -25:
62 self.
coolIdPath=ROOT.PathResolver.find_calib_file(
"TrigT1Calo/COOLIdDump_v1.txt")
67 for line
in input.readlines():
68 parts = line.split(
' ')
69 emCool = parts[4].rstrip()
70 hadCool = parts[5].rstrip()
82 dbSvc = cool.DatabaseSvcFactory.databaseService()
83 dbString =
'oracle://ATLAS_COOLPROD;schema=ATLAS_COOLONL_TRIGGER;dbname=CONDBR2'
85 db = dbSvc.openDatabase(dbString,
False)
86 except Exception
as e:
87 print (
'Error: Problem opening database', e)
90 folder_name =
"/TRIGGER/Receivers/RxPpmIdMap"
91 folder=db.getFolder(folder_name)
93 startUtime =
int(time.time())
94 endUtime =
int(time.time())
97 chsel = cool.ChannelSelection(0,sys.maxsize)
100 itr=folder.browseObjects(startValKey, endValKey, chsel)
101 except Exception
as e:
106 ReceiverId = hex(
int(row.channelId()))
107 payload = row.payload()
108 PPMId = hex(
int(payload[
'ppmid']))
127 if strategy_string
is None:
128 print (
" Warning! in getReceiverfromPPM no runtype given, using default!")
129 return ReceiverChannels[0]
133 if strategy_string ==
"GainOneOvEmbFcalHighEta":
134 for channel
in ReceiverChannels:
137 if strategy_string ==
"GainOneOvEmecFcalLowEta":
138 for channel
in ReceiverChannels:
144 if strategy_string ==
"GainOneOvEmbFcalHighEta":
145 for channel
in ReceiverChannels:
148 if strategy_string ==
"GainOneOvEmecFcalLowEta":
149 for channel
in ReceiverChannels:
154 return ReceiverChannels[0]
185 return int(channel[0][0])
188 return int(channel[0][0])
195 return int(channel[0][1])
198 return int(channel[0][1])
205 return missing_channels
210 recI=
int(ReceiverId,16)
213 recI = recI - crate*1024
216 recI = recI - module*64
219 recI = recI - conn*16
223 return [crate,module,conn,pair]
229 if eta_bin >= 32
or eta_bin <= -36:
238 if self.
isCoolEm(CoolId)
is True and (eta_bin == 14
or eta_bin == -15):
253 elif cabling[2] == 0:
255 elif cabling[2] == 2:
258 print (
"Error in GetOverlapLayer, can't determine layer!")
269 RecCoolInt =
int(RecCoolId,16)
270 if RecCoolInt%2 == 1:
302 input_file =
open(name)
304 for line
in input_file.readlines():
305 parts = line.split(
' ')
306 if parts[0] ==
'<Channel':
307 list_cool=parts[1].
split(
'\'')
310 list_gain=parts[2].
split(
'\'')
314 list_offset=parts[3].
split(
'\'')
315 offset=list_offset[1]
318 list_chi2=parts[4].
split(
'\'')
327 input_gains_reference =
open(name)
329 for line
in input_gains_reference.readlines():
330 parts = line.split(
' ')
331 if parts[0] ==
'<Channel':
332 list_cool=parts[1].
split(
'\'')
335 list_gain=parts[2].
split(
'\'')
343 dbSvc = cool.DatabaseSvcFactory.databaseService()
345 dbString=
'sqlite://;schema='+name+
';dbname=L1CALO'
347 db = dbSvc.openDatabase(dbString,
False)
348 except Exception
as e:
349 print (
'Error: Problem opening database', e)
352 folder_name =
'/TRIGGER/L1Calo/V1/Results/EnergyScanResults'
353 folder=db.getFolder(folder_name)
355 startUtime =
int(time.time())
356 endUtime =
int(time.time())
357 startValKey = startUtime * self.
UNIX2COOL
359 chsel = cool.ChannelSelection(0,sys.maxsize)
362 itr=folder.browseObjects(startValKey, endValKey, chsel)
363 except Exception
as e:
368 CoolId = hex(
int(row.channelId()))
369 payload = row.payload()
376 folder_gen_name =
'/TRIGGER/L1Calo/V1/Results/EnergyScanRunInfo'
377 folder_gen=db.getFolder(folder_gen_name)
380 itr=folder_gen.browseObjects(startValKey, endValKey, chsel)
382 payload = row.payload()
383 self.
run_nr = payload[
'RunNumber']
384 self.
strategy = payload[
'GainStrategy']
388 print (
"Warning, in LoadGainsSqlite can't get runtype info! Hope this is not serious!")
397 dbSvc = cool.DatabaseSvcFactory.databaseService()
399 dbString=
'sqlite://;schema='+name+
';dbname=L1CALO'
401 db = dbSvc.openDatabase(dbString,
False)
402 except Exception
as e:
403 print (
'Error: Problem opening database', e)
406 folder_name =
'/TRIGGER/L1Calo/V1/Results/EnergyScanResults'
407 folder=db.getFolder(folder_name)
409 startUtime =
int(time.time())
410 endUtime =
int(time.time())
411 startValKey = startUtime * self.
UNIX2COOL
413 chsel = cool.ChannelSelection(0,sys.maxsize)
416 itr=folder.browseObjects(startValKey, endValKey, chsel)
417 except Exception
as e:
422 CoolId = hex(
int(row.channelId()))
423 payload = row.payload()
434 dbSvc = cool.DatabaseSvcFactory.databaseService()
436 dbString =
'oracle://ATLAS_COOLPROD;schema=ATLAS_COOLONL_TRIGGER;dbname=CONDBR2'
438 db = dbSvc.openDatabase(dbString,
False)
439 except Exception
as e:
440 print (
'Error: Problem opening database', e)
443 folder_name =
"/TRIGGER/Receivers/Factors/CalibGains"
444 folder=db.getFolder(folder_name)
446 startUtime =
int(time.time())
447 endUtime =
int(time.time())
448 startValKey = startUtime * self.
UNIX2COOL
450 chsel = cool.ChannelSelection(0,sys.maxsize)
453 itr=folder.browseObjects(startValKey, endValKey, chsel)
454 except Exception
as e:
459 ReceiverId = hex(
int(row.channelId()))
460 PPMId = mapping_tool.getPPMfromReceiver(ReceiverId)
461 payload = row.payload()
462 gain = payload[
'factor']
464 if PPMId
is not None:
468 if mapping_tool.getReceiverfromPPM(PPMId,self.
strategy) == ReceiverId:
518 def __init__(self,name,nbins=40,minimum=0.,maximum=2.,XaxisTitle="",YaxisTitle=""):
521 self.
ext = [
"all",
"00_15",
"15_25",
"25_32",
"32_50"]
522 self.
name = [
"all",
"EMB",
"EMEC outer",
"EMEC Inner",
"FCAL 1"]
528 self.
his_partitions.
append(ROOT.TH1F(
"GainTTEm"+self.
ext[i_em_partition],name+
" for "+self.
name[i_em_partition],nbins,minimum,maximum))
531 self.
his_partitions[i_em_partition].GetXaxis().SetTitle(XaxisTitle)
532 self.
his_partitions[i_em_partition].GetYaxis().SetTitle(YaxisTitle)
539 if ( -9 <= eta_bin
and eta_bin <= 8):
541 elif ((eta_bin>8
and eta_bin<=14)
or (eta_bin>=-15
and eta_bin<-9)):
544 elif ((eta_bin>14
and eta_bin<=24)
or (eta_bin>=-25
and eta_bin<-15)):
546 elif ((eta_bin>24
and eta_bin<=31)
or (eta_bin>=-32
and eta_bin<-25)):
548 elif ((eta_bin>31)
or (eta_bin<-32)):
562 print (
"Warning in EmPartitionPlots, nonexisting partition!" )
567 def __init__(self,name,nbins=40,minimum=0.,maximum=2.,XaxisTitle="",YaxisTitle=""):
570 self.
ext = [
"all",
"00_09",
"09_15",
"15_25",
"25_32",
"32_50"]
571 self.
name = [
"all",
"Tile LB",
"Tile EB",
"HEC outer",
"HEC inner",
"FCAL 2/3"]
576 self.
his_partitions.
append(ROOT.TH1F(
"GainTTHad"+self.
ext[i_had_partition],name+
" for "+self.
name[i_had_partition],nbins,minimum,maximum))
579 self.
his_partitions[i_had_partition].GetXaxis().SetTitle(XaxisTitle)
580 self.
his_partitions[i_had_partition].GetYaxis().SetTitle(YaxisTitle)
585 if ( -9 <= eta_bin
and eta_bin <= 8):
587 elif ((eta_bin>8
and eta_bin<=14)
or (eta_bin>=-15
and eta_bin<-9)):
590 elif ((eta_bin>14
and eta_bin<=24)
or (eta_bin>=-25
and eta_bin<-15)):
592 elif ((eta_bin>24
and eta_bin<=31)
or (eta_bin>=-32
and eta_bin<-25)):
594 elif ((eta_bin>31)
or (eta_bin<-32)):
607 print (
"Warning in HadPartitionPlots, nonexisting partition!" )
610 def PlotCalibrationGains(input_file_name="",reference_file_name="",isInputXml=False,isInputSqlite=False,isRefXml=False,isRefSqlite=False,isRefOracle=False):
612 ROOT.gStyle.SetPalette(1)
613 ROOT.gStyle.SetOptStat(111111)
614 ROOT.gStyle.SetCanvasColor(10)
616 c1 = ROOT.TCanvas(
'c1',
'Example',200,10,700,500)
617 c2 = ROOT.TCanvas(
'c2',
'Example Partitions',200,10,700,500)
620 h_gains_em =
L1CaloMap(
"Eta-phi map of EM gains",
"#eta bin",
"#phi bin")
621 h_gains_had =
L1CaloMap(
"Eta-phi map of HAD gains",
"#eta bin",
"#phi bin")
623 h_gains_em_fselect =
L1CaloMap(
"Eta-phi map of EM gains that failed selection",
"#eta bin",
"#phi bin")
624 h_gains_had_fselect =
L1CaloMap(
"Eta-phi map of HAD gains that failed selection",
"#eta bin",
"#phi bin")
626 h_chi2_em =
L1CaloMap(
"Eta-phi map of EM Chi2",
"#eta bin",
"#phi bin")
627 h_chi2_had =
L1CaloMap(
"Eta-phi map of HAD Chi2",
"#eta bin",
"#phi bin")
629 h_offset_em =
L1CaloMap(
"Eta-phi map of EM offsets",
"#eta bin",
"#phi bin")
630 h_offset_had =
L1CaloMap(
"Eta-phi map of HAD offsets",
"#eta bin",
"#phi bin")
633 h_unfitted_em =
L1CaloMap(
"Eta-phi map of EM failed fits",
"#eta bin",
"#phi bin")
634 h_unfitted_had =
L1CaloMap(
"Eta-phi map of HAD failed fits",
"#eta bin",
"#phi bin")
636 h_drifted_em =
L1CaloMap(
"EM TTs that drifted more then 10 %",
"#eta bin",
"#phi bin")
637 h_drifted_had =
L1CaloMap(
"HAD TTs that drifted more then 10 %",
"#eta bin",
"#phi bin")
640 h_gains_em_reference =
L1CaloMap(
"Eta-phi map of EM gains (gain-reference)",
"#eta bin",
"#phi bin")
641 h_gains_had_reference =
L1CaloMap(
"Eta-phi map of HAD gains (gain-reference)",
"#eta bin",
"#phi bin")
643 h_gains_em_reference_rel =
L1CaloMap(
"Eta-phi map of EM gains: (gain-reference)/reference",
"#eta bin",
"#phi bin")
644 h_gains_had_reference_rel =
L1CaloMap(
"Eta-phi map of HAD gains: (gain-reference)/reference",
"#eta bin",
"#phi bin")
650 em_partition_gains_ref =
EmPartitionPlots(
" EM gains - reference",40,-0.2,0.2,
"gain-reference",
"N")
651 had_partition_gains_ref =
HadPartitionPlots(
" HAD gains - reference",40,-1.,1.,
"gain-reference",
"N")
653 em_partition_gains_ref_rel =
EmPartitionPlots(
" EM gains-reference / reference",40,-0.2,0.2,
"(gain-reference)/reference",
"N")
654 had_partition_gains_ref_rel =
HadPartitionPlots(
" HAD gains-reference / reference",40,-1.,1.,
"(gain-reference)/reference",
"N")
656 threshold_change = 0.1
661 bad_gain_file =
open(
'bad_gains.txt',
'w')
662 drifted_towers_file =
open(
'drifted_towers.txt',
'w')
664 if isInputXml
is True:
665 print (
"Taking input from xml file: ", input_file_name)
666 receiver_gains.LoadGainsXml(input_file_name)
667 elif isInputSqlite
is True:
668 print (
"Taking input from Sqlite file: ", input_file_name)
669 receiver_gains.LoadGainsSqlite(input_file_name)
671 print (
"No option for input file selected, assuming sqlite file energyscanresults.sqlite")
672 receiver_gains.LoadGainsSqlite(
"energyscanresults.sqlite")
676 print (
"Taking reference from Xml file: ",reference_file_name)
677 receiver_gains.LoadReferenceXml(reference_file_name)
678 elif isRefSqlite
is True:
679 print (
"Taking reference from Sqlite file: ",reference_file_name)
680 receiver_gains.LoadReferenceSqlite(reference_file_name)
681 elif isRefOracle
is True:
682 print (
"Taking reference from Oracle")
683 geometry_convertor.LoadReceiverPPMMap()
684 receiver_gains.LoadReferenceOracle(geometry_convertor)
686 print (
" No option for reference file, assuming Oracle")
687 geometry_convertor.LoadReceiverPPMMap()
688 receiver_gains.LoadReferenceOracle(geometry_convertor)
691 for i_eta
in range(-49,45):
692 for i_phi
in range(0,64):
694 coolEm = geometry_convertor.getCoolEm(i_eta,i_phi)
695 coolHad = geometry_convertor.getCoolHad(i_eta,i_phi)
699 gain = receiver_gains.getGain(coolEm)
700 chi2 = receiver_gains.getChi2(coolEm)
701 offset = receiver_gains.getOffset(coolEm)
702 reference_gain = receiver_gains.getReferenceGain(coolEm)
703 passes_selection = receiver_gains.passesSelection(coolEm)
705 if (
not gain ==
'')
and (
not reference_gain ==
''):
708 h_unfitted_em.Fill(i_eta,i_phi)
709 bad_gain_file.write(
'%i %i %s EM gain= %.3f \n' % (i_eta,i_phi,coolEm,
float(gain)))
710 h_gains_em_reference.Fill(i_eta,i_phi,-100.)
711 h_gains_em_reference_rel.Fill(i_eta,i_phi,-100.)
713 h_gains_em.Fill(i_eta,i_phi,gain)
714 h_chi2_em.Fill(i_eta,i_phi,chi2)
715 h_offset_em.Fill(i_eta,i_phi,offset)
716 em_partition_gains.Fill(i_eta,gain)
717 em_partition_gains_ref.Fill(i_eta,gain-reference_gain)
718 h_gains_em_reference.Fill(i_eta,i_phi,gain-reference_gain)
720 if passes_selection
is False:
721 h_gains_em_fselect.Fill(i_eta,i_phi)
722 bad_gain_file.write(
'%i %i %s EM gain= %.3f chi2= %.3f offset= %.3f \n' % (i_eta,i_phi,coolEm,
float(gain),
float(chi2),
float(offset)))
726 if reference_gain > 0:
727 em_partition_gains_ref_rel.Fill(i_eta,(gain-reference_gain)/reference_gain)
728 h_gains_em_reference_rel.Fill(i_eta,i_phi,(gain-reference_gain)/reference_gain)
729 if fabs((gain-reference_gain)/reference_gain) > threshold_change:
730 h_drifted_em.Fill(i_eta,i_phi)
731 drifted_towers_file.write(
'%i %i %s EM gain= %.3f refGain= %.3f (%.3f %%) \n' % (i_eta,i_phi,coolEm,
float(gain),
float(reference_gain),(gain-reference_gain)*100/reference_gain))
736 if not coolHad ==
'':
738 gain = receiver_gains.getGain(coolHad)
739 chi2 = receiver_gains.getChi2(coolHad)
740 offset = receiver_gains.getOffset(coolHad)
741 reference_gain = receiver_gains.getReferenceGain(coolHad)
742 passes_selection = receiver_gains.passesSelection(coolHad)
744 if (
not gain ==
'')
and (
not reference_gain ==
''):
747 h_unfitted_had.Fill(i_eta,i_phi)
748 bad_gain_file.write(
'%i %i %s HAD gain= %.3f \n' % (i_eta,i_phi,coolHad,
float(gain)))
749 h_gains_had_reference.Fill(i_eta,i_phi,-100.)
750 h_gains_had_reference_rel.Fill(i_eta,i_phi,-100.)
752 h_gains_had.Fill(i_eta,i_phi,gain)
753 h_chi2_had.Fill(i_eta,i_phi,chi2)
754 h_offset_had.Fill(i_eta,i_phi,offset)
755 had_partition_gains.Fill(i_eta,gain)
756 had_partition_gains_ref.Fill(i_eta,gain-reference_gain)
757 h_gains_had_reference.Fill(i_eta,i_phi,gain-reference_gain)
759 if passes_selection
is False:
760 h_gains_had_fselect.Fill(i_eta,i_phi)
761 bad_gain_file.write(
'%i %i %s HAD gain= %.3f chi2= %.3f offset= %.3f \n' % (i_eta,i_phi,coolHad,
float(gain),
float(chi2),
float(offset)))
765 if reference_gain > 0:
766 had_partition_gains_ref_rel.Fill(i_eta,(gain-reference_gain)/reference_gain)
767 h_gains_had_reference_rel.Fill(i_eta,i_phi,(gain-reference_gain)/reference_gain)
768 if fabs((gain-reference_gain)/reference_gain) > threshold_change:
769 h_drifted_had.Fill(i_eta,i_phi)
770 drifted_towers_file.write(
'%i %i %s HAD gain= %.3f refGain= %.3f (%.3f %%) \n' % (i_eta,i_phi,coolHad,
float(gain),
float(reference_gain),(gain-reference_gain)*100/reference_gain))
778 h_gains_em.SetMinimum(0.6)
779 h_gains_em.SetMaximum(1.4)
782 c1.Print(
"Gains.ps(")
784 h_gains_had.SetMinimum(0.6)
785 h_gains_had.SetMaximum(1.4)
799 h_gains_em_reference_rel.SetMinimum(-0.5)
800 h_gains_em_reference_rel.SetMaximum(0.5)
801 h_gains_em_reference_rel.Draw()
804 h_gains_had_reference_rel.SetMinimum(-0.5)
805 h_gains_had_reference_rel.SetMaximum(0.5)
806 h_gains_had_reference_rel.Draw()
810 h_gains_em_reference_rel.SetMinimum(-0.1)
811 h_gains_em_reference_rel.SetMaximum(0.1)
812 h_gains_em_reference_rel.Draw()
814 palette = h_gains_em_reference_rel.h_1.GetListOfFunctions().FindObject(
"palette")
816 palette.SetLabelSize(0.025)
818 h_gains_em_reference_rel.Draw()
821 h_gains_had_reference_rel.SetMinimum(-0.1)
822 h_gains_had_reference_rel.SetMaximum(0.1)
823 h_gains_had_reference_rel.Draw()
826 palette = h_gains_had_reference_rel.h_1.GetListOfFunctions().FindObject(
"palette")
828 palette.SetLabelSize(0.025)
830 h_gains_had_reference_rel.Draw()
837 h_gains_em_reference.SetMinimum(-0.5)
838 h_gains_em_reference.SetMaximum(0.5)
840 h_gains_em_reference.Draw()
843 h_gains_had_reference.SetMinimum(-0.5)
844 h_gains_had_reference.SetMaximum(0.5)
849 h_gains_had_reference.Draw()
854 h_chi2_em.SetMinimum(0.1)
855 h_chi2_em.SetMaximum(100)
861 h_chi2_had.SetMinimum(0.1)
862 h_chi2_had.SetMaximum(100)
868 h_offset_em.SetMinimum(-1.)
869 h_offset_em.SetMaximum(1.)
875 h_offset_had.SetMinimum(-1.)
876 h_offset_had.SetMaximum(1.)
884 for i_p
in range(0,em_partition_gains.nPartitions):
886 if em_partition_gains.his_partitions[i_p].
GetEntries() > 0:
890 em_partition_gains.his_partitions[i_p].Draw()
897 for i_p
in range(0,had_partition_gains.nPartitions):
899 if had_partition_gains.his_partitions[i_p].
GetEntries() > 0:
903 had_partition_gains.his_partitions[i_p].Draw()
911 for i_p
in range(0,em_partition_gains_ref.nPartitions):
913 if em_partition_gains_ref.his_partitions[i_p].
GetEntries() > 0:
917 em_partition_gains_ref.his_partitions[i_p].Draw()
924 for i_p
in range(0,had_partition_gains_ref.nPartitions):
926 if had_partition_gains_ref.his_partitions[i_p].
GetEntries() > 0:
930 had_partition_gains_ref.his_partitions[i_p].Draw()
937 for i_p
in range(0,em_partition_gains_ref_rel.nPartitions):
939 if em_partition_gains_ref_rel.his_partitions[i_p].
GetEntries() > 0:
943 em_partition_gains_ref_rel.his_partitions[i_p].Draw()
950 for i_p
in range(0,had_partition_gains_ref_rel.nPartitions):
952 if had_partition_gains_ref_rel.his_partitions[i_p].
GetEntries() > 0:
956 had_partition_gains_ref_rel.his_partitions[i_p].Draw()
964 h_gains_em_fselect.Draw()
967 h_gains_had_fselect.Draw()
974 h_unfitted_had.Draw()
975 c1.Print(
"Gains.ps)")
977 os.system(
"ps2pdf Gains.ps")
979 bad_gain_file.close()
980 drifted_towers_file.close()
984 if __name__ ==
"__main__":
986 print (
"Starting plot_gains_xml")
988 parser = OptionParser()
990 parser.add_option(
"-f",
"--InputFile",action=
"store",type=
"string",dest=
"input_file_name",help=
"Name of input file")
992 parser.add_option(
"-r",
"--ReferenceFile",action=
"store",type=
"string",dest=
"reference_file_name",help=
"Name of reference file")
994 parser.add_option(
"--InputXml",action=
"store_true",dest=
"isInputXml",help=
"Input is .xml file")
995 parser.add_option(
"--InputSqlite",action=
"store_true",dest=
"isInputSqlite",help=
"Input is .sqlite file")
996 parser.add_option(
"--RefXml",action=
"store_true",dest=
"isRefXml",help=
"Reference is .xml file")
997 parser.add_option(
"--RefSqlite",action=
"store_true",dest=
"isRefSqlite",help=
"Reference is .sqlite file")
998 parser.add_option(
"--RefOracle",action=
"store_true",dest=
"isRefOracle",help=
"Reference is from Oracle")
1000 (options, args) = parser.parse_args()
1003 options.isInputSqlite, options.isRefXml, options.isRefSqlite, options.isRefOracle)