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