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