ATLAS Offline Software
AtlRunQueryCOMA.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 : AtlRunQueryAMI.py
7 # Project: AtlRunQuery
8 # Purpose: Utility to retrieve information from AMI
9 # Authors: Andreas Hoecker (CERN), Joerg Stelzer (DESY)
10 # Created: Nov 10, 2009
11 # ----------------------------------------------------------------
12 #
13 
14 from __future__ import print_function
15 import re
16 
17 class ARQ_COMA:
18 
19  __store = {}
20  all_periods = None
21 
22  __cursor = None
23  __schema = "ATLAS_TAGS_METADATA"
24 
25  __tables = {
26  'PR' : 'COMA_V_PERIOD_RUNS',
27  }
28 
29  @classmethod
30  def getUsedTables(cls, output, condition):
31  usedtables = set( [o.split('.')[0] for o in output] ) # all tables specified in output fields
32 
33  for c in condition:
34  for p in c.split():
35  if '.' in p and '\'' not in p:
36  usedtables.add(p.split('.')[0].lstrip('(')) # all tables specified in conditions
37 
38  return ["%s.%s %s" % (cls.__schema,cls.__tables[t],t) for t in usedtables] # prefix schema name to table name
39 
40 
41  @classmethod
42  def query(cls, output, condition, bindvars, verbose=False):
43  query = 'select distinct %s from %s' % (', '.join(output),
44  ', '.join(cls.getUsedTables(output, condition)))
45  if condition:
46  query += ' where ' + ' and '.join(condition)
47  if verbose:
48  print ("="*80,"\n",query)
49  c = cls.cursor()
50  c.execute(str(query),bindvars)
51  return c.fetchall()
52 
53 
54  @classmethod
55  def cursor(cls):
56  """get COMA connection
57  any connection to ATLR is fine, we use Trigger DB
58  """
59  if not cls.__cursor:
60 
61  from CoolRunQuery.utils.AtlRunQueryTriggerUtils import interpretConnection
62  connectionParameters = interpretConnection("TRIGGERDB")
63 
64  from cx_Oracle import connect
65  connection = connect ( connectionParameters["user"],
66  connectionParameters["passwd"],
67  connectionParameters["server"], threaded=True)
68  cls.__cursor = connection.cursor()
69 
70  return cls.__cursor
71 
72 
73 
74 
75 
76  @classmethod
77  def get_periods_for_run(cls, run):
78  try:
79  run = int(run)
80  except ValueError:
81  return []
82  if run not in cls.__store:
83  output = ['PR.P_LEVEL', 'PR.P_PERIOD', 'PR.P_PROJECT' ]
84  condition = [ "PR.RUN_INDEX = :run" ]
85  bindvars = { "run": run }
86 
87  cls.__store[run] = sorted( cls.query(output, condition, bindvars) )
88 
89  return [x[1] for x in cls.__store[run]]
90 
91 
92  @classmethod
93  def get_periods(cls, year=0, level=0):
94  output = ['PR.P_PERIOD', 'PR.P_PROJECT' ]
95  condition = []
96  bindvars = {}
97 
98  if level>0:
99  condition += [ "PR.P_LEVEL=:lvl" ]
100  bindvars["lvl"] = level
101 
102  if year>2000:
103  project = 'data%02i%%' % (year-2000)
104  condition += [ "PR.P_PROJECT like :proj" ]
105  bindvars["proj"] = project
106 
107  return sorted( cls.query(output, condition, bindvars) )
108 
109 
110  @classmethod
111  def get_all_periods(cls):
112  if cls.all_periods is not None:
113  return cls.all_periods
114  cls.all_periods = []
115  p = re.compile(r"(?P<periodletter>[A-Za-z]+)(?P<periodnumber>\d+)?$")
116  try:
117  result = cls.get_periods(0, 0)
118  for period, projectName in result:
119  year = int(projectName[4:6])
120  m = p.match(period)
121  if not m:
122  print ("Period '%s'does not match pattern [A-Za-z]+\\d+" % period)
123  continue
124 
125  period_letter = m.group('periodletter')
126  period_number = int(m.group('periodnumber')) if m.group('periodnumber') else 0
127  if len(period_letter)!=1:
128  pc = 0
129  else:
130  pc = 10000*year + 100*(ord(period_letter.upper())-65) + period_number
131 
132  cls.all_periods += [ ((year, period, pc, projectName), projectName+".period" + period) ]
133 
134  cls.all_periods.sort()
135  except Exception:
136  import traceback
137  traceback.print_exc()
138  pass
139  return cls.all_periods
140 
141 
142  @classmethod
143  def get_runs(cls, period, year):
144  output = ['PR.RUN_INDEX', 'PR.P_LEVEL', 'PR.P_PERIOD', 'PR.P_PROJECT' ]
145 
146  condition = [ "PR.P_PERIOD=:period" ]
147  bindvars = { "period" : period }
148  if year>2000:
149  project = 'data%02i%%' % (year-2000)
150  condition += [ "PR.P_PROJECT like :proj" ]
151  bindvars["proj"] = project
152  result = cls.query(output, condition, bindvars)
153 
154  tmpstore = {}
155 
156  for record in result:
157  run = record[0]
158  info = record[1:4]
159  if run not in tmpstore:
160  tmpstore[run] = []
161  tmpstore[run] += [info]
162 
163  cls.__store.update(tmpstore)
164 
165  return sorted( [r[0] for r in result] )
166 
167 
168 
169 # for testing purpose
170 if __name__ == "__main__":
171 
172  choice = 1
173  while choice != 0:
174  print ("\n1 - periods for run")
175  print ("2 - runs for period (and year)")
176  print ("3 - periods (by year and/or level)")
177  print ("4 - all periods (different format)")
178  print ("\n0 - exit\n")
179 
180  choice = input(" enter your choice: ")
181  choice = int(choice) if choice.isdigit() else 0
182  if choice==1:
183  run = int(input(" run number: "))
184  print (ARQ_COMA.get_periods_for_run( run ))
185  elif choice==2:
186  period = input(" period : ")
187  year = input(" year <RET> = all : ")
188  year = int(year) if year.isdigit() else 0
189  print (', '.join([str(x) for x in ARQ_COMA.get_runs(period, year)]))
190  elif choice==3:
191  year = input(" year <RET> = all : ")
192  year = int(year) if year.isdigit() else 0
193  level = input(" level [1|2|3] <RET> = all : ")
194  level = int(level) if level.isdigit() else 0
195  print (ARQ_COMA.get_periods(year, level))
196  elif choice==4:
197  print (ARQ_COMA.get_all_periods())
python.AtlRunQueryCOMA.ARQ_COMA.getUsedTables
def getUsedTables(cls, output, condition)
Definition: AtlRunQueryCOMA.py:30
python.AtlRunQueryCOMA.ARQ_COMA.__schema
string __schema
Definition: AtlRunQueryCOMA.py:23
python.AtlRunQueryCOMA.ARQ_COMA.cursor
def cursor(cls)
Definition: AtlRunQueryCOMA.py:55
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.AtlRunQueryCOMA.ARQ_COMA.__store
dictionary __store
Definition: AtlRunQueryCOMA.py:19
python.AtlRunQueryCOMA.ARQ_COMA.get_all_periods
def get_all_periods(cls)
Definition: AtlRunQueryCOMA.py:111
python.AtlRunQueryCOMA.ARQ_COMA.all_periods
all_periods
Definition: AtlRunQueryCOMA.py:20
python.AtlRunQueryCOMA.ARQ_COMA.__tables
dictionary __tables
Definition: AtlRunQueryCOMA.py:25
python.AtlRunQueryCOMA.ARQ_COMA.__cursor
__cursor
Definition: AtlRunQueryCOMA.py:22
python.utils.AtlRunQueryTriggerUtils.interpretConnection
def interpretConnection(connection, debug=False, resolveAlias=True)
Definition: AtlRunQueryTriggerUtils.py:230
python.AtlRunQueryCOMA.ARQ_COMA.get_periods
def get_periods(cls, year=0, level=0)
Definition: AtlRunQueryCOMA.py:93
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
python.AtlRunQueryCOMA.ARQ_COMA.get_periods_for_run
def get_periods_for_run(cls, run)
Definition: AtlRunQueryCOMA.py:77
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
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:224
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.AtlRunQueryCOMA.ARQ_COMA.get_runs
def get_runs(cls, period, year)
Definition: AtlRunQueryCOMA.py:143
dqt_zlumi_pandas.update
update
Definition: dqt_zlumi_pandas.py:42
python.AtlRunQueryCOMA.ARQ_COMA
Definition: AtlRunQueryCOMA.py:17
str
Definition: BTagTrackIpAccessor.cxx:11
python.AtlRunQueryCOMA.ARQ_COMA.query
def query(cls, output, condition, bindvars, verbose=False)
Definition: AtlRunQueryCOMA.py:42