ATLAS Offline Software
AtlRunQueryUtils.py
Go to the documentation of this file.
1 #!/bin/env
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 #
5 # ----------------------------------------------------------------
6 # Script : AtlRunQueryUtils.py
7 # Project: AtlRunQuery
8 # Purpose: Utility functions for AtlRunQuery
9 # Authors: Andreas Hoecker (CERN), Joerg Stelzer (DESY)
10 # Created: Jan 20, 2009
11 # ----------------------------------------------------------------
12 #
13 
14 from __future__ import with_statement, print_function
15 from CoolRunQuery.utils.AtlRunQueryTimer import timer
16 
17 import sys, os, time, re, calendar
18 from math import exp,sqrt,pi
19 from copy import copy
20 import cx_Oracle
21 import struct
22 
23 import http.client
24 import urllib.parse
25 
26 import DQDefects
27 
28 def checkURL(url):
29  p = urllib.parse(url)
30  h = http.client.HTTP(p[1])
31  h.putrequest('HEAD', p[2])
32  h.endheaders()
33  if h.getreply()[0] == 200:
34  return 1
35  return 0
36 
37 class RunPeriods():
38  def findPeriod( self, runno ):
39  from CoolRunQuery.AtlRunQueryCOMA import ARQ_COMA
40  s = ",".join( ARQ_COMA.get_periods_for_run(runno) )
41  return s
42 
44  def __init__(self, names):
45  for number, name in enumerate(names.split()):
46  setattr(self, name, number)
47 
49  hostname = os.getenv('HOSTNAME')
50  print ("Execution on host: %r" % hostname )
51  if not hostname:
52  onserver = False
53  else:
54  onserver = ( re.match(r'aiatlas.*\.cern\.ch',hostname) is not None )
55  print ("Execution on server: %r" % onserver)
56  return onserver
57 
58 def importroot(batch=True):
59  import sys
60  cmdline_args = sys.argv
61  sys.argv[1:]=[]
62  if batch:
63  sys.argv += ['-b']
64  import ROOT
65  ROOT.gROOT.SetBatch(batch)
66  if batch:
67  del sys.argv[1]
68  ROOT.gErrorIgnoreLevel = 2000 # to avoid printout when creating images
69  sys.argv=cmdline_args
70  return ROOT
71 
72 
73 def prettyNumber( n, width=-1, delim=',',decimal='.' ):
74  """Converts a float to a string with appropriately placed commas"""
75  if width >= 0:
76  s = "%.*f" % (width, n)
77  else:
78  s = str(n)
79  dec = s.find(decimal)
80  if dec == -1:
81  dec = len(s)
82  threes = int((dec-1)/3)
83  for i in range(threes):
84  loc = dec-3*(i+1)
85  s = s[:loc] + delim + s[loc:]
86  return s
87 
88 def durationInSeconds( duration_string ):
89  lst = duration_string.split()
90  sec = 0
91  for entry in lst:
92  if 's' in entry:
93  sec += int(entry.replace('s',''))
94  elif 'm' in entry:
95  sec += int(entry.replace('m',''))*60
96  elif 'h' in entry:
97  sec += int(entry.replace('h',''))*3600
98  elif 'd' in entry:
99  sec += int(entry.replace('d',''))*3600*24
100  elif 'w' in entry:
101  sec += int(entry.replace('w',''))*3600*24*7
102  elif 'y' in entry:
103  sec += int(entry.replace('y',''))*3600*24*365
104  else:
105  print ('Big troubles... in function "AtlRunQueryUtils::durationInSeconds": cannot decode string "%s"' % entry)
106  sys.exit(1)
107  return sec
108 
110  def __init__(self):
111  self.openConn = {}
112  self.pw = {}
113 
114  def GetDBConn(self, schema, db):
115  """for example schema=COOLONL_TRIGGER', db='CONDBR2"""
116  if (schema,db) in self.openConn:
117  return self.openConn[(schema,db)]
118  try:
119  if schema=="DEFECTS":
120  #from AthenaCommon.Logging import logging # this is needed because of some problem in DQUtils logger
121  # the logger there, if called first makes the athena logger crash
122  defdb = DQDefects.DefectsDB("COOLOFL_GLOBAL/CONDBR2",tag=db)
123  defdb.closeDatabase = lambda: None
124  self.openConn[(schema,db)] = defdb
125  else:
126  logging=False
127  from CoolConvUtilities.AtlCoolLib import indirectOpen
128  self.openConn[(schema,db)] = indirectOpen("%s/%s"%(schema,db),True, logging)
129  except Exception:
130  import traceback
131  traceback.print_exc()
132  sys.exit(-1)
133  return self.openConn[(schema,db)]
134 
135  def get_auth(self,key):
136  if key not in self.pw:
137  from os import environ as env
138  #lookup = XMLReader(env['CORAL_DBLOOKUP_PATH']+"/dblookup.xml")
139  #for s in lookup.servicelist.logicalservices:
140  # print ("Service",s['name'])
141  # for p in s.services:
142  # print (" name",p['name'])
143  auth = XMLReader(env['CORAL_AUTH_PATH']+"/authentication.xml")
144  for c in auth.connectionlist.connections:
145  if key!=c['name']:
146  continue
147  self.pw[key] = dict([(p['name'],p['value']) for p in c.parameters])
148  break
149  if key not in self.pw:
150  print ("Can not authenticate DB",key)
151  sys.exit(0)
152  return self.pw[key]
153 
154 
156  if 'run' not in self.openConn:
157  auth = self.get_auth('oracle://ATLAS_COOLPROD/ATLAS_COOLOFL_TRIGGER')
158  self.openConn['run'] = cx_Oracle.connect("%s/%s@ATLAS_COOLPROD" % (auth['user'],auth['password']))
159  return self.openConn['run']
160 
162  if 'sfo' not in self.openConn:
163  auth = self.get_auth('oracle://ATLAS_CONFIG/ATLAS_SFO_T0_R')
164  with timer("Opening Connection to ATLAS_SFO_T0_R @ ATLAS_CONFIG"):
165  self.openConn['sfo'] = cx_Oracle.connect("%s/%s@ATLAS_CONFIG" % (auth['user'],auth['password']))
166  return self.openConn['sfo']
167 
169  if 'tier0' not in self.openConn:
170  auth = self.get_auth('oracle://ATLAS_T0/ATLAS_T0')
171  self.openConn['tier0'] = cx_Oracle.connect("%s/%s@ATLAS_T0" % (auth['user'],auth['password']))
172  return self.openConn['tier0']
173 
175  if 'pvss' not in self.openConn:
176  auth = self.get_auth('oracle://ATLAS_PVSSPROD/ATLAS_PVSS_READER')
177  self.openConn['pvss'] = cx_Oracle.connect("%s/%s@ATLAS_PVSSPROD" % (auth['user'],auth['password']))
178  return self.openConn['pvss']
179 
180  def CloseAll(self):
181  for (dbname,dbconn) in self.openConn.items():
182  if isinstance(dbconn,cx_Oracle.Connection):
183  dbconn.close()
184  else:
185  dbconn.closeDatabase()
186 
187 
189 
191  gr = [str(no)]
192  if not gr[0].isdigit():
193  return gr[0]
194  while gr[0]:
195  gr[0:1]=[gr[0][:-3],gr[0][-3:]]
196  return ','.join(gr[1:])
197 
198 def filesize(no):
199  if no is None:
200  return "n.a."
201  if no > 0x4000000000000:
202  return "%1.1f PB" % (no*1.0/0x4000000000000)
203  if no > 0x10000000000:
204  return "%1.1f TB" % (no*1.0/0x10000000000)
205  if no > 0x40000000:
206  return "%1.1f GB" % (no*1.0/0x40000000)
207  if no > 0x100000:
208  return "%1.1f MB" % (no*1.0/0x100000)
209  if no > 0x400:
210  return "%1.1f kB" % (no*1.0/0x400)
211 
212  return "%i B" % no
213 
214 
215 # a helper function to decode range lists and merge the intervals
216 # par: listOfRanges: [(b1,e1),(b2,e2),....,(bn,en)] -> [(b1,e2),....,(bn,en)] if b2-1<=e1
217 def MergeRanges(listOfRanges):
218  listOfRanges.sort()
219  newRR=[]
220  for rr in listOfRanges:
221  if len(newRR)==0 or rr[0]-1>newRR[-1][1]:
222  newRR.append(copy(rr))
223  else:
224  newRR[-1] = [newRR[-1][0], max(rr[1],newRR[-1][1]) ]
225  return newRR
226 
227 # a helper function to decode range lists and merge the intervals (like above)
228 # par: listOfRanges: [(b1,e1],(b2,e2],....,(bn,en]] -> [(b1,e2],....,(bn,en]] if b2<=e1
229 def MergeLBRanges(listOfRanges):
230  listOfRanges.sort()
231  newRR=[]
232  for rr in listOfRanges:
233  if len(newRR)==0 or rr[0]>newRR[-1][1]:
234  newRR.append(copy(rr))
235  else:
236  newRR[-1] = (newRR[-1][0], max(rr[1],newRR[-1][1]) )
237  return newRR
238 
239 class Matrix(object):
240 
241  def __init__(self, cols, rows):
242  self.cols = cols
243  self.rows = rows
244  # initialize matrix and fill with zeroes
245  self.matrix = []
246  for i in range(rows):
247  ea_row = []
248  for j in range(cols):
249  ea_row.append(0)
250  self.matrix.append(ea_row)
251 
252  def setitem(self, col, row, v):
253  self.matrix[col-1][row-1] = v
254 
255  def getitem(self, col, row):
256  return self.matrix[col-1][row-1]
257 
258  def __repr__(self):
259  outStr = ""
260  for i in range(self.rows):
261  outStr += 'Row %s = %s\n' % (i+1, self.matrix[i])
262  return outStr
263 
264  def __iter__(self):
265  for row in range(self.rows):
266  for col in range(self.cols):
267  yield (self.matrix, row, col)
268 
270  if re.match(r"^\d+$",s):
271  return int(s)
272  # convert the string into seconds since epoch
273 
274  # string is meant to be UTC time (hence we need calendar.timegm
275  # and not time.mktime to convert the tuple into seconds)
276  t = int(1E9*calendar.timegm(time.strptime(s,"%d.%m.%Y")))
277  #print ("turning",s,"into time",t)
278  #print ("cross-check:",time.strftime("%d.%m.%Y %H:%M:%S",time.gmtime(t/1E9)))
279  return t
280 
282  """ convert string into seconds since epoch
283 
284  string is meant to be UTC time (hence we need calendar.timegm
285  and not time.mktime to convert the tuple into seconds)
286 
287  format can be '1.5.2010_14:23:10', '1.5.2010 14:23:10', or '1.5.2010'
288  """
289  try:
290  t = time.strptime(t,"%d.%m.%Y")
291  except ValueError:
292  try:
293  t = time.strptime(t,"%d.%m.%Y %H:%M:%S")
294  except ValueError:
295  t = time.strptime(t,"%d.%m.%Y_%H:%M:%S")
296 
297  return int(calendar.timegm(t))
298 
300  """ convert string into seconds since epoch
301 
302  string is meant to be in local time (hence we use time.mktime
303  and not calender.timegm to convert the tuple into seconds)
304 
305  format can be '1.5.2010_14:23:10', '1.5.2010 14:23:10', or '1.5.2010'
306  """
307  try:
308  t = time.strptime(t,"%d.%m.%Y")
309  except ValueError:
310  try:
311  t = time.strptime(t,"%d.%m.%Y %H:%M:%S")
312  except ValueError:
313  t = time.strptime(t,"%d.%m.%Y_%H:%M:%S")
314 
315  return int(time.mktime(t))
316 
318  # convert seconds since epoch into utc
319  return time.strftime("%d.%m.%Y %H:%M:%S",time.gmtime(s))
320 
321 
323  # convert seconds since epoch into localtime
324  return time.strftime("%d.%m.%Y %H:%M:%S",time.localtime(s))
325 
326 
327 
328 def GetRanges(rangestr, intRepFnc=stringToIntOrTime, maxval=1<<30):
329  if type(rangestr)==list:
330  ranges = rangestr
331  else:
332  if rangestr[:4]=='STR:':
333  return [ [rangestr[4:], rangestr[4:]] ]
334  ranges = rangestr.split(',')
335  listOfRanges = []
336  for r in ranges:
337  if r == '-':
338  listOfRanges += [[0,maxval]]
339  elif not ('-' in r or '+' in r):
340  # single value
341  x = intRepFnc(r)
342  listOfRanges += [[x,x]]
343  elif r[-1] == '+':
344  listOfRanges += [[intRepFnc(r[:-1]),maxval]]
345  elif r[-1] == '-':
346  listOfRanges += [[0,intRepFnc(r[:-1])]]
347  else:
348  startend = r.split('-')
349  if len(startend)!=2:
350  raise RuntimeError ("Range format error '%s'" % r)
351  listOfRanges += [[intRepFnc(x) for x in startend]]
352  return MergeRanges(listOfRanges)
353 
354 
355 
356 
357 def full_time_string(s,startofday):
358  try:
359  time.strptime(s,"%d.%m.")
360  year=str(time.gmtime().tm_year)
361  return s+year+"_00:00:00" if startofday else s+year+"_23:59:59"
362  except ValueError:
363  try:
364  time.strptime(s,"%d.%m.%Y")
365  return s+"_00:00:00" if startofday else s+"_23:59:59"
366  except ValueError:
367  return s
368 
369 def GetTimeRanges(timeranges, intRepFnc=timeStringToSecondsUTC, maxval=1<<30):
370  if type(timeranges)==list:
371  timeranges = ','.join(timeranges)
372 
373  timeranges = timeranges.split(',')
374  listOfRanges = []
375  listOfRangesHR = []
376 
377  for r in timeranges:
378  start = 0
379  end = 'inf'
380  if r == '-':
381  listOfRangesHR += [['0','inf']]
382  elif not ('-' in r or '+' in r):
383  # single value
384  start = full_time_string( r, startofday=True )
385  end = full_time_string( r, startofday=False )
386  elif r[-1] == '+':
387  start = full_time_string( r[:-1], startofday=True )
388  elif r[-1] == '-':
389  end = full_time_string( r[:-1], startofday=False )
390  else:
391  try:
392  start,end = r.split('-')
393  except ValueError:
394  raise RuntimeError ("Time format '%s' wrong, should be 'from-until'" % r)
395  start = full_time_string( start, startofday=True )
396  end = full_time_string( end, startofday=False )
397 
398  listOfRangesHR += [[start,end]]
399  start = 0 if start==0 else intRepFnc(start)
400  end = maxval if end=='inf' else intRepFnc(end)
401  listOfRanges += [[start,end]]
402 
403  return MergeRanges(listOfRanges),listOfRangesHR
404 
405 
406 def SmartRangeCalulator(runlist,singleRuns=True):
407  if len(runlist) == 0:
408  return []
409  if isinstance(runlist[0], list):
410  return runlist
411  if singleRuns:
412  rr = [[r.runNr,r.runNr] for r in runlist]
413  else:
414  rr = [[runlist[0].runNr,runlist[-1].runNr]]
415  return rr
416 
417 # code from Eric Torrence
418 # Optional argument to nval to specify number of values to read
419 def bConvertList(b, nbyte=1, nval=1):
420  # routine to store an unsigned int (1, 2, 4 or 8 byte) in a blob
421  packopt=dict([[1,'B'],[2,'H'],[4,'f'],[8,'d']])
422  if nbyte in packopt:
423  # print ('bConvert - b:[', b[0:nbyte], '] nbyte:', nbyte, ' fmt:', packopt[nbyte], type(b))
424  fmt = '%d%s' % (nval, packopt[nbyte])
425  ival=struct.unpack(fmt, b[0:nval*nbyte])
426  else:
427  print ('bConvertList: Unrecognized pack option')
428  sys.exit()
429  return list(ival)
430 
431 
432 def unpackRun1BCIDMask(blob, nb1, nb2, nbc):
433  size = nb1+nb2+nbc
434  a = bConvertList(blob, 2, size)
435  beam1 = a[0:nb1]
436  beam2 = a[nb1:nb1+nb2]
437  coll = a[nb1+nb2:]
438  #print ('unpackRun1BCIDMask found:')
439  #print (' Beam1:', beam1)
440  #print (' Beam2:', beam2)
441  #print (' Coll: ', coll)
442  return beam1, beam2, coll
443 
444 
446  beam1=[]
447  beam2=[]
448  coll=[]
449  rawData = bConvertList(blob, 1, 3564)
450  for i in range(3564):
451  val = rawData[i]
452  if val & 0x01:
453  beam1.append(i)
454  if val & 0x02:
455  beam2.append(i)
456  if (val & 0x03) == 0x03:
457  coll.append(i)
458  #print ('unpackRun2BCIDMask found:')
459  #print (' Beam1:', beam1)
460  #print (' Beam2:', beam2)
461  #print (' Coll: ', coll)
462  return beam1, beam2, coll
463 
464 
465 
466 def Poisson( n, mu ):
467  # only valid for small mu and integer n
468  if n < 0:
469  return 0
470  else:
471  p = exp(-mu)
472  for i in range(n):
473  p *= mu
474  p /= float(i+1)
475  return p
476 
477 def ComputePileup( lumi_mb, sigma, nbunch, fOrbit ):
478  print("Calling compute pileup with")
479  print(" lumi_mb :", lumi_mb)
480  print(" sigma :", sigma)
481  print(" # bunches:", nbunch)
482  print(" # fOrbit :", fOrbit)
483  p = []
484  # sanity
485  if nbunch <= 0:
486  return 0, None
487 
488  # compute first probability of interaction
489  nint = lumi_mb * sigma / fOrbit / nbunch
490 
491  # large
492  if nint > 100:
493  return nint, None
494 
495  # small 'nint', compute poisson probabilities
496  for n in range(40):
497  p.append(Poisson(n,nint))
498  if n > 20 and p[-1] < 1e40:
499  break
500  return nint, p
501 
502 def Pileup( args ):
503  # orbit revolution frequency
504  fOrbit = 11245.511 # [Hz] given by: 40.0790e6 Hz (bunch frequency) / 3564 (nbunches)
505 
506  if len(args) < 3:
507  return '<font color="red">ERROR: need at least 3 arguments for pileup calculation: inst.-lumi cross-section-in-mb nbunch [nevents]</font>'
508  try:
509  fstarg = float(args[0])
510  if fstarg < 1e20:
511  # input expects first two arguments to be number of protons and transverse beam size
512  nprotons = fstarg
513  sigtrans = float(args[1]) # given in microns
514  sigtrans *= 1e-4 # transform to cm
515 
516  sigma = float(args[2])
517  nbunch = float(args[3])
518  lumi = nprotons**2 * fOrbit * nbunch / (4.0 * pi * sigtrans**2)
519  nevents = 1
520  if len(args) == 5:
521  nevents = float(args[4])
522  else:
523  # input expects first argument to be luminosity in cm-2s-1
524  lumi = float(args[0])
525  sigma = float(args[1])
526  nbunch = int(args[2])
527  nevents = 1
528  if len(args) == 4:
529  nevents = int(args[3])
530 
531  # compute pileup
532  lumi_mb = lumi/1.e27 # luminosity in mb-1s-1
533  nint, plist = ComputePileup( lumi_mb, sigma, nbunch, fOrbit )
534  print("Resultat:")
535  print(" nint ",nint)
536  print(" plist ",plist)
537  referenceInfo = '7TeV: 60.3&pm;2.1 mb'
538  referenceInfo += ' [<a href="https://arxiv.org/abs/1104.0326" target="_blank" title="Measurement of the Inelastic Proton-Proton Cross Section at &sqrt;s=7 TeV with the ATLAS Detector at the LHC">arXiv:1104.0326</a>], '
539  referenceInfo += '13TeV: 78.1&pm;2.9mb'
540  referenceInfo += ' [<a href="https://arxiv.org/abs/1606.02625" target="_blank" title="Measurement of the Inelastic Proton-Proton Cross Section at &sqrt;s=13 TeV with the ATLAS Detector at the LHC">arXiv:1606.02625</a>]'
541  # create output string
542  s = ' '
543  s += '<table class="datasettable" style="font-size: 140%">'
544  s += '<tr><td>Instantaneous luminosity :</td><td> %g cm&minus;2s&minus;1 (= %g mb&minus;1s&minus;1)</td></tr>' % (lumi, lumi_mb)
545  s += '<tr><td>Inelastic cross section :</td><td> %g mb &nbsp;(%s)</td></tr>' % (sigma, referenceInfo)
546  s += '<tr><td>Number of colliding bunches :</td><td> %g</td></tr>' % nbunch
547  s += '<tr><td colspan="2"><hr style="width:100%; #999999; background-color: #999999; height:1px; margin-left:0px; border:0"></td></tr>'
548  s += '<tr><td>Inelastic interaction rate:</td><td>%g Hz</td></tr>' % (lumi_mb*sigma)
549  s += '<tr><td>Average number of interactions per crossing:&nbsp;&nbsp;</td><td> %g</td></tr>' % nint
550  s += '</table>'
551  s += '<hr style="width:100%; #999999; background-color: #999999; height:0px; border:0">\n<p>\n'
552  if nint > 100:
553  s += 'Very large pileup probability (assume Gaussian distribution): %g +- %g' % (nint, sqrt(nint))
554  else:
555  s += '<table class="pileuptable">'
556  s += '<tr><th>Num. of interactions per filled bunch crossing</th><th>Probability per filled bunch crossing</th><th>Probability per triggered minimum bias event*</th>'
557  if nevents>1:
558  s += '<th>Expected number of events in sample*</th>'
559  s += '</tr>'
560 
561  pref = 1.0-plist[0] # probability for a zero bias trigger to have triggered an event
562  psum = 0
563  for i,p in enumerate(plist):
564  if i>=1:
565  s += '<tr><td>&gt;= %i</td><td>%g</td><td>%g</td>' % (i,1.0-psum,(1.0-psum)/pref)
566  if nevents > 1:
567  nevexp = (1.0-psum)/pref*nevents
568  s += '<td> %g</td>' % (nevexp)
569  s += '</tr>'
570  if p < 1e-15:
571  break
572  psum += p
573  s += '</table><p></p>'
574  s += '<font size=-2>*assuming 100% trigger efficiency for inelastic events</font><br>'
575 
576  return s
577 
578  except ValueError:
579  return '<font color="red">ERROR: only numerical arguments allowed</font>'
580 
581 
582 def get_run_range(start,end=None):
583  runs = get_runs(start,end)
584  if len(runs) == 0:
585  return None
586  if len(runs) == 1:
587  return (runs[0],runs[0])
588  return (runs[0],runs[-1])
589 
591  lt = last.replace('last','').strip()
592  nsec = durationInSeconds( lt )
593  start = time.gmtime( time.mktime(time.gmtime()) - nsec )
594  return get_runs(time.strftime("%d.%m.%Y_%H:%M:%S",start))
595 
596 def get_runs(start,end=None):
597  """start and end are given in the format '1.5.2010_14:23:10', '1.5.2010 14:23:10', or '1.5.2010'"""
598 
599  co = coolDbConn.GetAtlasRunDBConnection()
600  cu = co.cursor()
601 
602  #available:
603  # NAME,RUNNUMBER,STARTAT,DURATION,CREATEDBY,HOST,PARTITIONNAME,CONFIGSCHEMA,CONFIGDATA,COMMENTS
604  records = 'RUNNUMBER'
605 
606  t = time.gmtime( timeStringToSecondsUTC(start) )
607  starttime = time.strftime("%Y%m%dT%H%M%S",t)
608 
609  if not end:
610  q = "SELECT %s FROM ATLAS_RUN_NUMBER.RUNNUMBER WHERE STARTAT>'%s' ORDER BY RUNNUMBER desc" % (records,starttime)
611  else:
612  t = time.gmtime( timeStringToSecondsUTC(end) )
613  endtime = time.strftime("%Y%m%dT%H%M%S",t)
614  q = "SELECT %s FROM ATLAS_RUN_NUMBER.RUNNUMBER WHERE STARTAT>'%s' and STARTAT<'%s' ORDER BY RUNNUMBER" % (records,starttime,endtime)
615 
616  cu.arraysize=100
617  cu.execute(q)
618 
619  res = cu.fetchall()
620  return [r[0] for r in res]
621 
622 
623 def get_run_range2(start,end=None):
624  """
625  find all runs between two timestamps
626  start and end are given in the format '1.5.2010_14:23:10', '1.5.2010 14:23:10', or '1.5.2010'
627  """
628 
629  # first we find the last run before the specified start time
630  # second we check if it spans over the specified start time
631  # - if it does then we use this run number
632  # - if it does not we find the first run after the specified start time and use this run number
633  # if an end is given we search for the last run that starts before the specified end time
634 
635  co = coolDbConn.GetAtlasRunDBConnection()
636  cu = co.cursor()
637 
638  #available:
639  # NAME,RUNNUMBER,STARTAT,DURATION,CREATEDBY,HOST,PARTITIONNAME,CONFIGSCHEMA,CONFIGDATA,COMMENTS
640 
641  start_seconds_utc = timeStringToSecondsUTC(start)
642  start_gmtime = time.gmtime( start_seconds_utc )
643  start_fstring = time.strftime("%Y%m%dT%H%M%S", start_gmtime)
644 
645  # last run that started before the begin of the range
646  subq = "SELECT MAX(RUNNUMBER) FROM ATLAS_RUN_NUMBER.RUNNUMBER WHERE STARTAT<'%s' AND PARTITIONNAME='ATLAS'" % start_fstring
647  q = "select RUNNUMBER,STARTAT,DURATION from ATLAS_RUN_NUMBER.RUNNUMBER where RUNNUMBER=(%s)" % subq
648  cu.execute(q)
649  try:
650  run1,startat,duration = cu.fetchone()
651 
652  startAtGmtime = time.strptime( startat, "%Y%m%dT%H%M%S" ) # parse the format from the RunNumber DB
653  startAtUtcSecond = calendar.timegm( startAtGmtime )
654  # startAtHuman = time.strftime("%Y%m%dT%H%M%S", startAtGmtime)
655 
656  endAtUtcSecond = startAtUtcSecond + duration
657  # endAtGmtime = time.gmtime( endAtUtcSecond )
658  # endAtHuman = time.strftime("%Y%m%dT%H%M%S", endAtGmtime)
659 
660  # print("start: ", start)
661  # print("Last run before start: ",run1)
662  # print(" starts at: ",startat)
663  # print(" lasts for: ", duration)
664  # print("start sec", startAtUtcSecond)
665  # print("start gmt", startAtGmtime)
666  # print("start hr ", startAtHuman)
667  # print("end sec", endAtUtcSecond)
668  # print("end gmt", endAtGmtime)
669  # print("end hr ", endAtHuman)
670 
671  # note that duration is not exact, but always a bit larger than the length of the run
672 
673  if endAtUtcSecond < start_seconds_utc:
674  print("Run started and ended before the specified start time, so need to take the first one that started of the specified start time")
675  q = "SELECT MIN(RUNNUMBER) FROM ATLAS_RUN_NUMBER.RUNNUMBER WHERE STARTAT>'%s' AND PARTITIONNAME='ATLAS'" % start_fstring
676  cu.execute(q)
677  try:
678  run1 = cu.fetchone()[0]
679  except cx_Oracle.Error:
680  run1 = None
681  except cx_Oracle.Error:
682  run1 = None
683 
684  run2 = None
685  if end is not None:
686  # last run that started before the end of the range
687  start_gmtime = time.gmtime( timeStringToSecondsUTC(end) )
688  endtime = time.strftime("%Y%m%dT%H%M%S",start_gmtime)
689  q = "SELECT MAX(RUNNUMBER) FROM ATLAS_RUN_NUMBER.RUNNUMBER WHERE STARTAT<'%s'" % endtime
690  cu.execute(q)
691  run2 = cu.fetchone()[0]
692 
693  return (run1,run2)
694 
695 
696 
698  class XMLElement:
699  def __init__(self,element):
700  self.element = element
701  self.tag = element.tag
702  self.attributes = dict(element.items())
703  self.children = list(element)
704  self.readchildren()
705 
706  def items(self):
707  return self.attributes.items()
708  def __str__(self):
709  return "<%s %s>" % (self.tag, " ".join(['%s="%s"' % x for x in self.items()]))
710  def __repr__(self):
711  return self.tag
712  def __getitem__(self,k):
713  if k not in self.attributes:
714  raise KeyError ("'%s'. XML element '%s' has attributes %s" % (k,self.tag, self.attributes.keys()))
715  return self.attributes[k]
716 
717  def readchildren(self):
718  self.childtags = []
719  self._childtagdict = {}
720  for c in self.children:
721  self._childtagdict.setdefault(c.tag,[]).append(XMLReader.XMLElement(c))
722  if c.tag not in self.childtags:
723  self.childtags += [c.tag]
724  for t in self.childtags:
725  self.__dict__['%ss'%t] = self._childtagdict[t]
726  if len(self._childtagdict[t])==1:
727  self.__dict__['%s'%t] = self._childtagdict[t][0]
728 
729  def __getattr__(self,name):
730  raise AttributeError ("'%s'. XML element '%s' has tags %s" % (name,self.tag, ["%ss" % t for t in self.childtags]))
731 
732 
733  def __init__(self,filename):
734  import xml.etree.cElementTree as ET
735  self.doc = ET.parse(filename)
736  root = XMLReader.XMLElement(self.doc.getroot())
737  self.__filename = filename
738  self.__root = root
739  self.__dict__[root.tag] = root
740 
741  def __getattr__(self,name):
742  raise AttributeError ("'%s'. XML document '%s' has root tag '%s'" % (name,self.__filename, self.__root.tag))
python.utils.AtlRunQueryUtils.XMLReader.__init__
def __init__(self, filename)
Definition: AtlRunQueryUtils.py:733
python.utils.AtlRunQueryUtils.DBConnectionController.GetSFODBConnection
def GetSFODBConnection(self)
Definition: AtlRunQueryUtils.py:161
python.utils.AtlRunQueryUtils.RunPeriods.findPeriod
def findPeriod(self, runno)
Definition: AtlRunQueryUtils.py:38
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.childtags
childtags
Definition: AtlRunQueryUtils.py:718
python.utils.AtlRunQueryUtils.unpackRun2BCIDMask
def unpackRun2BCIDMask(blob)
Definition: AtlRunQueryUtils.py:445
python.utils.AtlRunQueryUtils.unpackRun1BCIDMask
def unpackRun1BCIDMask(blob, nb1, nb2, nbc)
Definition: AtlRunQueryUtils.py:432
python.utils.AtlRunQueryUtils.DBConnectionController.GetDBConn
def GetDBConn(self, schema, db)
Definition: AtlRunQueryUtils.py:114
python.utils.AtlRunQueryUtils.get_runs_last_dt
def get_runs_last_dt(last)
Definition: AtlRunQueryUtils.py:590
python.utils.AtlRunQueryUtils.XMLReader.doc
doc
Definition: AtlRunQueryUtils.py:735
python.utils.AtlRunQueryUtils.prettyNumber
def prettyNumber(n, width=-1, delim=',', decimal='.')
Definition: AtlRunQueryUtils.py:73
python.utils.AtlRunQueryUtils.Matrix.__iter__
def __iter__(self)
Definition: AtlRunQueryUtils.py:264
python.utils.AtlRunQueryUtils.filesize
def filesize(no)
Definition: AtlRunQueryUtils.py:198
python.utils.AtlRunQueryUtils.DBConnectionController.pw
pw
Definition: AtlRunQueryUtils.py:112
max
#define max(a, b)
Definition: cfImp.cxx:41
python.utils.AtlRunQueryUtils.get_run_range
def get_run_range(start, end=None)
Definition: AtlRunQueryUtils.py:582
python.utils.AtlRunQueryUtils.DBConnectionController.GetPVSSDBConnection
def GetPVSSDBConnection(self)
Definition: AtlRunQueryUtils.py:174
python.utils.AtlRunQueryUtils.importroot
def importroot(batch=True)
Definition: AtlRunQueryUtils.py:58
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.children
children
Definition: AtlRunQueryUtils.py:703
python.utils.AtlRunQueryUtils.addKommaToNumber
def addKommaToNumber(no)
Definition: AtlRunQueryUtils.py:190
python.utils.AtlRunQueryUtils.stringToIntOrTime
def stringToIntOrTime(s)
Definition: AtlRunQueryUtils.py:269
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.__str__
def __str__(self)
Definition: AtlRunQueryUtils.py:708
python.utils.AtlRunQueryUtils.DBConnectionController.__init__
def __init__(self)
Definition: AtlRunQueryUtils.py:110
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.__getitem__
def __getitem__(self, k)
Definition: AtlRunQueryUtils.py:712
python.utils.AtlRunQueryUtils.RunPeriods
Definition: AtlRunQueryUtils.py:37
python.utils.AtlRunQueryUtils.GetTimeRanges
def GetTimeRanges(timeranges, intRepFnc=timeStringToSecondsUTC, maxval=1<< 30)
Definition: AtlRunQueryUtils.py:369
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
python.utils.AtlRunQueryUtils.DBConnectionController.GetTier0DBConnection
def GetTier0DBConnection(self)
Definition: AtlRunQueryUtils.py:168
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.readchildren
def readchildren(self)
Definition: AtlRunQueryUtils.py:717
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.__getattr__
def __getattr__(self, name)
Definition: AtlRunQueryUtils.py:729
python.utils.AtlRunQueryUtils.runsOnServer
def runsOnServer()
Definition: AtlRunQueryUtils.py:48
python.utils.AtlRunQueryTimer.timer
def timer(name, disabled=False)
Definition: AtlRunQueryTimer.py:86
python.utils.AtlRunQueryUtils.get_run_range2
def get_run_range2(start, end=None)
Definition: AtlRunQueryUtils.py:623
python.utils.AtlRunQueryUtils.durationInSeconds
def durationInSeconds(duration_string)
Definition: AtlRunQueryUtils.py:88
python.utils.AtlRunQueryUtils.Matrix
Definition: AtlRunQueryUtils.py:239
python.utils.AtlRunQueryUtils.XMLReader.__filename
__filename
Definition: AtlRunQueryUtils.py:737
python.utils.AtlRunQueryUtils.Matrix.rows
rows
Definition: AtlRunQueryUtils.py:243
python.utils.AtlRunQueryUtils.Matrix.setitem
def setitem(self, col, row, v)
Definition: AtlRunQueryUtils.py:252
python.utils.AtlRunQueryUtils.bConvertList
def bConvertList(b, nbyte=1, nval=1)
Definition: AtlRunQueryUtils.py:419
python.utils.AtlRunQueryUtils.MergeRanges
def MergeRanges(listOfRanges)
Definition: AtlRunQueryUtils.py:217
python.utils.AtlRunQueryUtils.MergeLBRanges
def MergeLBRanges(listOfRanges)
Definition: AtlRunQueryUtils.py:229
python.utils.AtlRunQueryUtils.XMLReader.XMLElement
Definition: AtlRunQueryUtils.py:698
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.element
element
Definition: AtlRunQueryUtils.py:700
python.utils.AtlRunQueryUtils.secondsToTimeStringLocaltime
def secondsToTimeStringLocaltime(s)
Definition: AtlRunQueryUtils.py:322
python.utils.AtlRunQueryUtils.checkURL
def checkURL(url)
Definition: AtlRunQueryUtils.py:28
python.utils.AtlRunQueryUtils.Matrix.__init__
def __init__(self, cols, rows)
Definition: AtlRunQueryUtils.py:241
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.utils.AtlRunQueryUtils.DBConnectionController.CloseAll
def CloseAll(self)
Definition: AtlRunQueryUtils.py:180
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
python.utils.AtlRunQueryUtils.get_runs
def get_runs(start, end=None)
Definition: AtlRunQueryUtils.py:596
python.utils.AtlRunQueryUtils.secondsToTimeStringUTC
def secondsToTimeStringUTC(s)
Definition: AtlRunQueryUtils.py:317
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.__repr__
def __repr__(self)
Definition: AtlRunQueryUtils.py:710
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.attributes
attributes
Definition: AtlRunQueryUtils.py:702
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.utils.AtlRunQueryUtils.DBConnectionController.get_auth
def get_auth(self, key)
Definition: AtlRunQueryUtils.py:135
python.utils.AtlRunQueryUtils.Matrix.__repr__
def __repr__(self)
Definition: AtlRunQueryUtils.py:258
python.utils.AtlRunQueryUtils.GetRanges
def GetRanges(rangestr, intRepFnc=stringToIntOrTime, maxval=1<< 30)
Definition: AtlRunQueryUtils.py:328
python.utils.AtlRunQueryUtils.Matrix.matrix
matrix
Definition: AtlRunQueryUtils.py:245
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
python.utils.AtlRunQueryUtils.XMLReader.XMLElement._childtagdict
_childtagdict
Definition: AtlRunQueryUtils.py:719
python.utils.AtlRunQueryUtils.DBConnectionController.GetAtlasRunDBConnection
def GetAtlasRunDBConnection(self)
Definition: AtlRunQueryUtils.py:155
python.utils.AtlRunQueryUtils.ComputePileup
def ComputePileup(lumi_mb, sigma, nbunch, fOrbit)
Definition: AtlRunQueryUtils.py:477
python.utils.AtlRunQueryUtils.Enumerate
Definition: AtlRunQueryUtils.py:43
python.utils.AtlRunQueryUtils.Pileup
def Pileup(args)
Definition: AtlRunQueryUtils.py:502
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.tag
tag
Definition: AtlRunQueryUtils.py:701
python.utils.AtlRunQueryUtils.XMLReader.__root
__root
Definition: AtlRunQueryUtils.py:738
python.utils.AtlRunQueryUtils.timeStringToSecondsLocalTime
def timeStringToSecondsLocalTime(t)
Definition: AtlRunQueryUtils.py:299
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
python.utils.AtlRunQueryUtils.Matrix.getitem
def getitem(self, col, row)
Definition: AtlRunQueryUtils.py:255
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
python.utils.AtlRunQueryUtils.timeStringToSecondsUTC
def timeStringToSecondsUTC(t)
Definition: AtlRunQueryUtils.py:281
python.utils.AtlRunQueryUtils.DBConnectionController
Definition: AtlRunQueryUtils.py:109
pickleTool.object
object
Definition: pickleTool.py:30
str
Definition: BTagTrackIpAccessor.cxx:11
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
calibdata.copy
bool copy
Definition: calibdata.py:27
python.utils.AtlRunQueryUtils.XMLReader.__getattr__
def __getattr__(self, name)
Definition: AtlRunQueryUtils.py:741
python.utils.AtlRunQueryUtils.XMLReader
Definition: AtlRunQueryUtils.py:697
python.utils.AtlRunQueryUtils.full_time_string
def full_time_string(s, startofday)
Definition: AtlRunQueryUtils.py:357
python.utils.AtlRunQueryUtils.SmartRangeCalulator
def SmartRangeCalulator(runlist, singleRuns=True)
Definition: AtlRunQueryUtils.py:406
python.AtlCoolLib.indirectOpen
def indirectOpen(coolstr, readOnly=True, debug=False)
Definition: AtlCoolLib.py:130
readCCLHist.float
float
Definition: readCCLHist.py:83
python.utils.AtlRunQueryUtils.Enumerate.__init__
def __init__(self, names)
Definition: AtlRunQueryUtils.py:44
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.items
def items(self)
Definition: AtlRunQueryUtils.py:706
python.utils.AtlRunQueryUtils.Poisson
def Poisson(n, mu)
Definition: AtlRunQueryUtils.py:466
python.utils.AtlRunQueryUtils.DBConnectionController.openConn
openConn
Definition: AtlRunQueryUtils.py:111
python.utils.AtlRunQueryUtils.XMLReader.XMLElement.__init__
def __init__(self, element)
Definition: AtlRunQueryUtils.py:699
python.utils.AtlRunQueryUtils.Matrix.cols
cols
Definition: AtlRunQueryUtils.py:242