ATLAS Offline Software
AtlRunQueryDQUtils.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 #
5 # ----------------------------------------------------------------
6 # Script : AtlRunQueryDQUtils.py
7 # Project: AtlRunQuery
8 # Purpose: Utility functions for AtlRunQuery
9 # Authors: Peter Onyisi (Chicago U)
10 # Created: May 6, 2010
11 # ----------------------------------------------------------------
12 #
13 
14 import sys, time
15 from functools import reduce
16 import xml.etree.ElementTree as et
17 import xmlrpc.client
18 from PyCool import cool
19 dbSvc = cool.DatabaseSvcFactory.databaseService()
20 
21 from CoolRunQuery.AtlRunQuerySelectorBase import DataKey
22 
23 THIRTYTWOMASK=int(2**32-1)
24 SERVER='http://atlasdqm.cern.ch:8080'
25 
26 FLAGS_WE_CARE_ABOUT = ['PIXB', 'PIX0', 'PIXEA', 'PIXEC',
27  'SCTB', 'SCTEA', 'SCTEC',
28  'TRTB', 'TRTEA', 'TRTEC','TRTTR'
29  'IDGL', 'IDAL',
30  'EMBA', 'EMBC', 'EMECA', 'EMECC', 'HECA', 'HECC',
31  'FCALA', 'FCALC',
32  'TILBA', 'TILBC', 'TIEBA', 'TIEBC',
33  'MBTSA', 'MBTSC',
34  'MDTBA', 'MDTBC', 'MDTEA', 'MDTEC',
35  'RPCBA', 'RPCBC',
36  'TGCEA', 'TGCEC',
37  'CSCEA', 'CSCEC',
38  'LCD',
39  #'L1CAL',
40  #'L1MUB', 'L1MUE',
41  #'L1CTP',
42  #'TRCAL', 'TRBJT', 'TRBPH', 'TRCOS',
43  #'TRELE', 'TRGAM', 'TRJET', 'TRMET',
44  #'TRMBI', 'TRMUO', 'TRTAU', 'TRIDT',
45 # 'EIDB', 'EIDCR', 'EIDE', 'PIDB', 'PIDCR', 'PIDE',
46 # 'JETB', 'JETEA', 'JETEC',
47 # 'MET', 'METCALO', 'METMUON',
48 # 'TAUB', 'TAUCR', 'TAUE',
49  ]
50 
51 FLAGGROUPS = {
52  'PIX': ['PIXB', 'PIX0', 'PIXEA', 'PIXEC'],
53  'SCT': ['SCTB', 'SCTEA', 'SCTEC'],
54  'TRT': ['TRTB', 'TRTEA', 'TRTEC', 'TRTTR'],
55  'LAR': ['EMBA', 'EMBC', 'EMECA', 'EMECC',
56  'HECA', 'HECC', 'FCALA', 'FCALC'],
57  'EM': ['EMBA', 'EMBC', 'EMECA', 'EMECC'],
58  'HEC': ['HECA', 'HECC'],
59  'FCAL': ['FCALA', 'FCALC'],
60  'TILE': ['TILBA', 'TILBC', 'TIEBA', 'TIEBC'],
61  'MDT': ['MDTBA', 'MDTBC', 'MDTEA', 'MDTEC'],
62  'RPC': ['RPCBA', 'RPCBC'],
63  'TGC': ['TGCEA', 'TGCEC'],
64  'CSC': ['CSCEA', 'CSCEC'],
65  'MBTS': ['MBTSA', 'MBTSC'],
66  'LUCID': ['LCD']
67  }
68 
69 def _intersect(lbr1, lbr2):
70  def normalize(lbr):
71  return (max(1, lbr[0]), lbr[1] if lbr[1] != -1 else THIRTYTWOMASK)
72  tlbr1 = normalize(lbr1)
73  tlbr2 = normalize(lbr2)
74  rv = (max(tlbr1[0],tlbr2[0]), min(tlbr1[1],tlbr2[1]))
75  if rv[1] <= rv[0]:
76  return None
77  else:
78  return rv
79 
80 lumicache = {}
81 
82 def lumi(run, lbr):
83  if tuple(lbr) in lumicache:
84  return lumicache[tuple(lbr)]
85 
86  lblbdb = dbSvc.openDatabase('COOLONL_TRIGGER/CONDBR2', True)
87  lblb = lblbdb.getFolder('/TRIGGER/LUMI/LBLB')
88  lblestonl = lblbdb.getFolder('/TRIGGER/LUMI/LBLESTONL')
89 
90  lbs = lblb.browseObjects((run<<32)+lbr[0],
91  (run<<32)+lbr[1]-1,
92  cool.ChannelSelection(0))
93  inf = {}
94  for obj in lbs:
95  lbpy = obj.payload()
96  #print ((lbpy['EndTime']-lbpy['StartTime'])/1e9)
97  inf[(run, obj.since() & 0xFFFFFFFF)] = (lbpy['EndTime']-lbpy['StartTime'])/1e9
98  if obj.since() & 0xFFFFFFFF == lbr[1]:
99  print ('Oops: this should not happen, appears to be off-by-one error')
100  lbls = lblestonl = lblestonl.browseObjects((run<<32)+lbr[0],
101  (run<<32)+lbr[1]-1,
102  cool.ChannelSelection(0))
103  infl = {}
104  for obj in lbls:
105  lblpy = obj.payload()
106  infl[(run, obj.since() & 0xFFFFFFFF)] = lblpy['LBAvInstLumi']
107 
108  #print (sorted(infl.keys()))
109  totlum = 0
110  for lb in inf:
111  if lb in infl:
112  totlum += inf[lb]*infl[lb]
113  else:
114  print ('Missing run %d, LB %d' % lb)
115  lumicache[tuple(lbr)] = totlum
116  return totlum
117 
118 def GetDQEfficiency( rundict ):
119 
120  runset = set()
121  for runnum in rundict.keys():
122  runset.add(runnum)
123 
124  s = xmlrpc.client.ServerProxy(SERVER)
125  flaginfo = s.get_dqmf_summary_flags_lb({'run_list': list(runset)}, FLAGS_WE_CARE_ABOUT, 'SHIFTOFL')
126  #print (flaginfo)
127  record = {}
128  for flag in FLAGS_WE_CARE_ABOUT:
129  record[flag] = []
130  for run in runset:
131  if str(run) not in flaginfo:
132  print ('%s not in flaginfo' % run)
133  del rundict[run]
134  continue
135  for sublb in rundict[run]:
136  for flag, periods in flaginfo[str(run)].items():
137  for period in periods:
138  ip = _intersect(period, sublb)
139  if ip is not None:
140  #print (run, flag, ip, period[2])
141  #print (lumi(run, ip))
142  record[flag].append((ip[1]-ip[0], period[2], lumi(run, ip)))
143 
144 
145  totallum = 0
146  for run in rundict:
147  for pair in rundict[run]:
148  totallum += lumi(run, pair)
149  print ('Total lumi:', totallum)
150 
151  flagsum = {}
152 
153  for flag in FLAGS_WE_CARE_ABOUT:
154  flagsum[flag] = {}
155  lr = record[flag]
156  cols = set([x[1] for x in lr])
157  accounted = 0
158  for col in cols:
159  llum = sum([x[2] for x in lr if x[1] == col])
160  accounted += llum
161  print (flag, col, llum, '%.2f%%' % (llum/totallum*100))
162  flagsum[flag][col] = (llum/totallum*100)
163  if abs(accounted-totallum) > 1e-8:
164  print (flag, 'n.a.', totallum-accounted, '%.2f%%' % ((1-accounted/totallum)*100))
165  flagsum[flag]['n.a.'] = ((1-accounted/totallum)*100)
166 
167  def _sum(d1, d2):
168  rv = {}
169  for key in set(d1.keys() + d2.keys()):
170  rv[key] = d1.get(key, 0) + d2.get(key, 0)
171  #print (rv)
172  return rv
173 
174  for flagtop, flaggroup in FLAGGROUPS.items():
175  vals = reduce(_sum, [flagsum[f] for f in flaggroup], {})
176  print (flagtop)
177  for typ in vals:
178  print (' %s: %.2f%%' % (typ, vals[typ]/len(flaggroup)))
179 
180 def MakeDQeffHtml( dic, dicsum, datapath ):
181  s = ''
182 
183 
184  # run and time ranges
185  runs = dic[DataKey('Run')]
186  times = dic[DataKey('Start and endtime')]
187 
188  starttime = float(times[-1].partition(',')[0])
189  endtime = float(times[0].partition(',')[2])
190 
191  s = '<table style="color: #777777; font-size: 85%; margin-left: 14px" cellpadding="0" cellspacing="3">\n'
192  # runs in reverse order
193  s += '<tr><td><i>Number of runs selected:</i></td><td>&nbsp;&nbsp;%g</td><td></td></tr>\n' % (len(runs))
194  s += '<tr><td><i>First run selected:</i></td><td>&nbsp;&nbsp;%s</td><td>&nbsp;&nbsp;(%s)</td></tr>\n' % (runs[-1], time.strftime("%a %b %d, %Y, %X",time.gmtime(starttime)))
195  s += '<tr><td><i>Last run selected:</i></td><td>&nbsp;&nbsp;%s</td><td>&nbsp;&nbsp;(%s)</td></tr>\n' % (runs[0], time.strftime("%a %b %d, %Y, %X",time.gmtime(endtime)))
196  s += '</table>\n'
197 
198  # --------------------------------------------------------------------------------------
199  # run summary table
200  s += '<p></p>\n'
201  s += '<table style="margin-left: 14px">\n'
202  s += '<tr><td><b>Run / Event Summary</b></td></tr>\n'
203  s += '</table>\n'
204 
205  return s
206 
207 if __name__ == '__main__':
208 
209  p = et.parse(sys.argv[1])
210 
211  rundict = {}
212  for lbc in p.getiterator('LumiBlockCollection'):
213  runnum = int(lbc.find('Run').text)
214  #print ('Run', runnum)
215  rundict[runnum] = []
216  for lbr in lbc.findall('LBRange'):
217  rundict[runnum].append([int(lbr.get('Start')),
218  int(lbr.get('End'))+1])
219  #print (' LBs %s-%s' % (lbr.get('Start'), lbr.get('End')))
220 
python.ZdcOnlineRecMonitorConfig.partition
partition
Definition: ZdcOnlineRecMonitorConfig.py:328
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
reduce
void reduce(HepMC::GenEvent *ge, std::vector< HepMC::GenParticlePtr > toremove)
Remove unwanted particles from the event, collapsing the graph structure consistently.
Definition: FixHepMC.cxx:84
normalize
Double_t normalize(TF1 *func, Double_t *rampl=NULL, Double_t from=0., Double_t to=0., Double_t step=1.)
Definition: LArPhysWaveHECTool.cxx:825
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
python.utils.AtlRunQueryDQUtils.MakeDQeffHtml
def MakeDQeffHtml(dic, dicsum, datapath)
Definition: AtlRunQueryDQUtils.py:180
python.utils.AtlRunQueryDQUtils.lumi
def lumi(run, lbr)
Definition: AtlRunQueryDQUtils.py:82
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:71
python.utils.AtlRunQueryDQUtils.GetDQEfficiency
def GetDQEfficiency(rundict)
Definition: AtlRunQueryDQUtils.py:118
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.utils.AtlRunQueryDQUtils._intersect
def _intersect(lbr1, lbr2)
Definition: AtlRunQueryDQUtils.py:69
str
Definition: BTagTrackIpAccessor.cxx:11
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65