ATLAS Offline Software
Loading...
Searching...
No Matches
AtlRunQuerySelectorMisc.py
Go to the documentation of this file.
1# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2
3
4import re,sys
5from time import gmtime
6
7from CoolRunQuery.AtlRunQueryRun import Run
8from CoolRunQuery.utils.AtlRunQueryUtils import coolDbConn, GetRanges
9from CoolRunQuery.selector.AtlRunQuerySelectorBase import Selector, RunLBBasedCondition, TimeBasedCondition, DataKey
10
11
12class FilenameSelector(RunLBBasedCondition):
13 def __init__(self, name, projecttag=None):
14 self.filenametag = projecttag
15 self.fntpatterns = None
16 if projecttag:
17 self.fntpatterns = [re.compile(pt.strip().replace('*','.*').replace('?','.'),re.I) for pt in projecttag.split(',')]
18 super(FilenameSelector,self).__init__(name=name,
19 dbfolderkey='COOLONL_TDAQ::/TDAQ/RunCtrl/SOR%s' % ("_Params" if Selector.condDB() == "COMP200" else ""),
20 channelKeys = [(0, 'Project tag','FilenameTag' if Selector.condDB() == "COMP200" else 'T0ProjectTag')])
21
22 def __str__(self):
24 return 'SELOUT Checking if the filename tag matches "%s"' % self.filenametag
25 else:
26 return "Retrieving filenametag"
27
28 def passes(self,value,key):
29 for p in self.fntpatterns:
30 if p.match(value):
31 return True
32 return False
33
34class PartitionSelector(RunLBBasedCondition):
35 def __init__(self, name, partition=None):
36 self.partition = partition
37 super(PartitionSelector,self).__init__(name=name,
38 dbfolderkey='COOLONL_TDAQ::/TDAQ/RunCtrl/EventCounters',
39 channelKeys = [(0,'Partition','PartitionName')])
40
41 def __str__(self):
43 return 'SELOUT Checking if partition name matches "%s"' % self.partition
44 else:
45 return "Retrieving partition name"
46
47 def passes(self,value,key):
48 if value=='n.a.':
49 self.selDataMissing = True
50 return True
51 return value==self.partition
52
53class ReadyForPhysicsSelector(RunLBBasedCondition):
54 def __init__(self, name, readyforphysics=None):
55 self.readyforphysics = readyforphysics
56 super(ReadyForPhysicsSelector,self).__init__(name=name,
57 dbfolderkey='COOLONL_TDAQ::/TDAQ/RunCtrl/DataTakingMode',
58 channelKeys = [(0,'Ready for physics','ReadyForPhysics')])
59
60 def __str__(self):
62 return 'SELOUT Checking if ReadyForPhysics flag matches "%s"' % self.readyforphysics
63 else:
64 return "Retrieving ReadyForPhysics flag"
65
66 def passes(self,value,key):
67 if value=='n.a.':
68 self.selDataMissing = True
69 return True
70 return value==self.readyforphysics
71
72
73class DurationSelector(Selector):
74 def __init__(self,name,duration):
75 self.geReq = (duration[-1]!='-')
76 self.duration = int(duration.rstrip('+-'))
77 super(DurationSelector,self).__init__(name)
78 def __str__(self):
79 return "SELOUT Checking if duration of run is %s than %i seconds" % ("more" if self.geReq else "less",self.duration)
80 def select(self, runlist):
81 print (self, end='')
82 sys.stdout.flush()
83 if self.geReq:
84 rmlist = [r for r in runlist if (r.eor-r.sor)/1E9<self.duration]
85 else:
86 rmlist = [r for r in runlist if (r.eor-r.sor)/1E9>self.duration]
87 for r in rmlist:
88 runlist.remove(r)
90 print (" ==> %i runs selected" % len(runlist))
91 return runlist
92
93
94class DetectorSelector(RunLBBasedCondition):
95 def __init__(self, name, dmin=None, dmout=None):
96 self.bm = self.bp = self.bmany = 0
97 if not dmin:
98 dmin = []
99 if not dmout:
100 dmout = []
101 for m in dmin+dmout:
102 if 'A' in m:
103 continue
104 self.bm |= int(m)
105 for m in dmin:
106 if 'A' in m:
107 continue
108 self.bp |= int(m)
109 for m in dmin:
110 if 'A' not in m:
111 continue
112 self.bmany |= int(m.rstrip('A'))
113
114 dbfolder = ""
115 if Selector.condDB() == "COMP200":
116 dbfolder = "/TDAQ/RunCtrl/SOR_Params"
117 if Selector.condDB() == "CONDBR2":
118 dbfolder = "/TDAQ/RunCtrl/SOR"
119
120 super(DetectorSelector,self).__init__(name=name,
121 dbfolderkey='COOLONL_TDAQ::%s' % dbfolder,
122 channelKeys = [(0, 'Detector systems', 'DetectorMask')])
123
124 self.data_keys[0]._type = DataKey.DETECTOR
125
126
127
128 def __str__(self):
130 if self.bm!=0 and self.bmany!=0:
131 return "SELOUT Checking if [detector mask & %i] matches %i and [detector mask & %i] is greater 0" % (self.bm,self.bp,self.bmany)
132 elif self.bm==0:
133 return "SELOUT Checking if [detector mask & %i] is greater 0" % self.bmany
134 else:
135 return "SELOUT Checking if [detector mask & %i] matches %i" % (self.bm,self.bp)
136 else:
137 return "Retrieving detector mask"
138
139 def passes(self,values,key):
140 try:
141 val = int(values,16)
142 except ValueError:
143 self.selDataMissing = True
144 return True
145 retval = True
146 if self.bm!=0:
147 retval &= ((val & self.bm) == self.bp)
148 if self.bmany!=0:
149 retval &= ((val & self.bmany) != 0)
150 return retval
151
152 def prettyValue(self, value, key):
153 """
154 run 1: run 211541 has DetectorMask 281474976710647
155 run 2: run 252233 has DetectorMask 0d00069fffffffff0
156 """
157 return value
158
159
160
161
162class BFieldCondition(TimeBasedCondition):
163 def __init__(self, name, condition=None, channel=0, resDictKey=''):
164 super(BFieldCondition,self).__init__(name,
165 dbfolderkey='COOLOFL_DCS::/EXT/DCS/MAGNETS/SENSORDATA',
166 channelKeys = [(channel,resDictKey,'value')])
167 if condition:
168 self.cutRange = GetRanges(condition)
169 def __str__(self):
171 if self.cutRange[0][1] > 1000000:
172 txt = '[%.0f,+inf]' % (self.cutRange[0][0])
173 else:
174 txt = '[%.0f,%0.f]' % (self.cutRange[0][0],self.cutRange[0][1])
175 return "SELOUT Checking if the %s is within %s" % (', '.join(self.ResultKey()), txt)
176 else:
177 return "Retrieving magnet currents %s" % self.ResultKey()
178
179 def passes(self,values,key):
180 try:
181 val = abs(float(values))
182 except ValueError:
183 self.selDataMissing = True
184 return True
185 for cr in self.cutRange:
186 if val>=cr[0] and val<=cr[1]:
187 return True
188 return False
189
190 def prettyValue(self, value, key):
191 if value=='n.a.':
192 return value
193 return round(float(value),2)
194
195
196class BFieldSelector(Selector):
197 def __init__(self, name, bf=None):
198 super(BFieldSelector,self).__init__(name)
199 self.conditions = []
200 if bf:
201 if 'solenoidon' in bf or 'solenoidoff' in bf:
202 tmp = []
203 if 'solenoidon' in bf:
204 tmp += ['7700+']
205 elif 'solenoidoff' in bf:
206 tmp += ['10-']
207 self.conditions += [ BFieldCondition('csolcur', ','.join(tmp), channel = 1, resDictKey = 'SolCurrent') ]
208 if 'toroidon' in bf or 'toroidoff' in bf:
209 tmp = []
210 if 'toroidon' in bf:
211 tmp += ['20000+']
212 elif 'toroidoff' in bf:
213 tmp += ['10-']
214 self.conditions += [ BFieldCondition('ctorcur', ','.join(tmp), channel = 3, resDictKey = 'TorCurrent') ]
215 else:
216 self.conditions += [ BFieldCondition('csolcur', channel = 1, resDictKey = 'SolCurrent') ]
217 self.conditions += [ BFieldCondition('ctorcur', channel = 3, resDictKey = 'TorCurrent') ]
218 for sel in self.conditions:
219 sel.setShowOutput()
220 sel.applySelection = False
221
222 def __str__(self):
223 s = ""
224 for sel in self.conditions:
225 s += "\n%s" % sel
226 return s
227
228 def setShowOutput(self):
229 for cond in self.conditions:
230 cond.setShowOutput()
231
233 sol = tor = None
234 for s in self.conditions:
235 if s.name == 'csolcur':
236 sol = s
237 if s.name == 'ctorcur':
238 tor = s
239 if not sol:
240 sol = BFieldCondition('csolcur', channel = 1, resDictKey = 'SolCurrent')
241 sol.applySelection = False
242 self.conditions += [ sol ]
243 if not tor:
244 tor = BFieldCondition('ctorcur', channel = 3, resDictKey = 'TorCurrent')
245 tor.applySelection = False
246 self.conditions += [ tor ]
247 sol.setShowOutput()
248 tor.setShowOutput()
249
250 def select(self, runlist):
251 for sel in self.conditions:
252 runlist = sel.select(runlist)
253 return runlist
254
255
256class BPMSelector(Selector):
257 def __init__(self, name, events=None):
258 self.name = name
259 self.selpattern = []
260 super(BPMSelector,self).__init__(name)
261
262 def __str__(self):
263 return "Retrieving Beam Position Monitor values from PVSS archive"
264 def passes(self,values,key):
265 return True
266 def setShowOutput(self):
267 pass
268 def select(self, runlist):
269 return runlist
270
271 def runAfterQuery(self,runlist):
272 # do show selection here
273 from CoolRunQuery.AtlRunQueryPVSS import GetPVSS_BPMs
274 pvssdb = coolDbConn.GetPVSSDBConnection()
275 cursor = pvssdb.cursor()
276 cursor.arraysize = 1000
277 for r in runlist:
278 t = gmtime(r.sor/1.E9)
279 sor = "%02i-%02i-%4i %02i:%02i:%02i" % (t[2], t[1], t[0], t[3], t[4], t[5])
280 t = gmtime(r.eor/1.E9)
281 eor = "%02i-%02i-%4i %02i:%02i:%02i" % (t[2], t[1], t[0], t[3], t[4], t[5])
282
283 # retrieve values
284 res = GetPVSS_BPMs( cursor, sor, eor )
285 r.addResult('BPM', res )
286
287 Run.AddToShowOrder(DataKey('BPM'))
288
289
290
291class DatasetsSelector(Selector):
292 def __init__(self, name, events=None):
293 self.name = name
294 self.selpattern = []
295 self.showCAFLinks = False
296 super(DatasetsSelector,self).__init__(name)
297
298 def __str__(self):
299 return "Retrieving datasets from Tier-0 DB"
300 def passes(self,values,key):
301 return True
302 def setShowOutput(self):
303 pass
304 def select(self, runlist):
305 return runlist
306 def addShowDatasetPattern(self, pattern = ''):
307 # format: '*NTUP*,*dESD*,... [caf]'
308 if pattern.lower() == 'caf':
309 self.showCAFLinks = True
310 elif pattern:
311 # first check if 'caf'
312 sp = pattern.split()
313 if len(sp)>1:
314 if 'caf' == sp[0].lower():
315 self.showCAFLinks = True
316 pattern = sp[1]
317 elif 'caf' == sp[1].lower():
318 self.showCAFLinks = True
319 pattern = sp[0]
320 # sanity check
321 if len(sp) != 2 or not self.showCAFLinks:
322 print ('ERROR: wrong format in "show dataset". Usage: "show dataset [pattern] [caf]"')
323 sys.exit(1)
324
325 self.selpattern = pattern.split(',')
326
327 def runAfterQuery(self,runlist):
328 useTier0DB = False # ATLAS_T0 responsible refusing to allow access to their DB
329 if useTier0DB:
330 # do show selection here
331 from CoolRunQuery.AtlRunQueryTier0 import GetTier0_allDatasets
332 runnrlist = [r.runNr for r in runlist]
333 tier0connection = coolDbConn.GetTier0DBConnection()
334 tier0retdico = GetTier0_allDatasets( tier0connection.cursor(), runnrlist, self.selpattern )
335
336 for run in runlist: # go through old runlist and see
337 if run.runNr in tier0retdico:
338 run.addResult('Datasets', tier0retdico[run.runNr])
339 else:
340 run.addResult('Datasets', {})
341
342 Run.AddToShowOrder(DataKey('Datasets'))
343 Run.showCAFLinks = self.showCAFLinks
344
345
346
347class LArcondSelector(RunLBBasedCondition):
348 def __init__(self, name, larcond=[]):
349 super(LArcondSelector,self).__init__(name=name,
350 dbfolderkey='COOLONL_LAR::/LAR/Configuration/RunLog',
351 channelKeys = [(0,'lar:runtype', 'runType'),
352 (0,'lar:nsamples', 'nbOfSamples'),
353 (0,'lar:format', 'format')])
354 # define arguments (after initialising bas class!)
355 self.larcond = {}
356 if larcond:
357 for c in larcond:
358 # format: 'nsamples 7' or 'runtype 1', etc
359 larcondargs = c.split()
360 if len(larcondargs) == 2:
361 key = 'lar:' + larcondargs[0].strip().lower()
362 # does it exist?
363 if key not in self.ResultKey():
364 print ('ERROR: unknown larcond variable "%s"' % key)
365 sys.exit(1)
366 self.larcond[key] = larcondargs[1].strip()
367 else:
368 print ('ERROR: unknown condition format for larcond: "%s" -> need two arguments separated by blank' % larcondargs)
369 sys.exit(1)
370
371
372 def __str__(self):
374 return "SELOUT Checking if LAr condition matches %s" % self.larcond
375 else:
376 return "Retrieving LAr run conditions"
377
378 def passes(self,values,key):
379 if key.lower().strip() in self.larcond:
380 if values.strip() == self.larcond[key.lower().strip()]:
381 return True
382 else:
383 return False
384 return True
385
__init__(self, name, condition=None, channel=0, resDictKey='')
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:310