8 from __future__
import print_function
10 __author__ =
"Alexandre Vaniachine <vaniachine@anl.gov>"
11 __doc__ =
"peek into APR files to read in-file metadata"
15 from CLIDComps.clidGenerator
import clidGenerator
16 cgen = clidGenerator(
"")
19 """utility algorithm to inspect a file's content
26 def run(self,writelog=False):
37 """ the real function doing all the work of peeking at the APR file
38 @return a dict of peeked-at data
44 'peeker-tool-%i-%s.log.txt' %
45 (os.getpid(), uuid.uuid4())
47 stdout =
open(stdout_fname,
"w")
48 print (
"Extracting in-file metadata without athena sub-process from file", self.
f.GetName(), file=stdout)
49 print (
"="*80, file=stdout)
52 pool = self.
f.
Get(
"##Params")
54 pool_token = re.compile(
r'[\[]NAME=(?P<name>.*?)[]]' r'[\[]VALUE=(?P<value>.*?)[]]').match
56 for i
in range(pool.GetEntries()):
57 if pool.GetEntry(i)>0:
58 match = pool_token(pool.FindLeaf(
"db_string").GetValueString())
62 params.append((d[
'name'], d[
'value']))
63 if d[
'name'].lower() ==
'fid':
66 meta = self.
f.
Get(
'MetaData' )
69 print (
'No metadata', file=stdout)
72 from AthenaPython.FilePeekerLib
import toiter
77 esiTypeName =
'EventStreamInfo'
78 for l
in meta.GetListOfLeaves():
79 if l.GetTypeName().startswith(esiTypeName):
80 esiTypeName = l.GetTypeName()
84 if esiTypeName !=
'EventStreamInfo_p3':
86 print (
"old schema is not supported:", esiTypeName, file=stdout)
91 esic = cppyy.gbl.EventStreamInfoPTCnv_p3()
92 esi = getattr (meta, esiName)
93 if esiName.startswith(esiTypeName):
94 esiName = esiName[len(esiTypeName)+1:]
99 peeked_data[
'file_guid'] = guid
101 peeked_data[
'run_type'] = [
'N/A']
104 print (peeked_data, file=stdout)
106 peeked_data[
'nentries'] = esic.getNumberOfEvents(esi)
107 peeked_data[
'lumi_block'] =
list(esic.lumiBlockNumbers(esi))
108 peeked_data[
'run_number'] =
list(esic.runNumbers(esi))
109 peeked_data[
'stream_names'] = [s
for s
in esic.processingTags(esi)]
111 item_list =
list( (cgen.getNameFromClid(i.first), i.second)
for i
in esic.itemList(esi))
113 if (
'DataHeader', esiName)
in item_list:
114 item_list.remove((
'DataHeader', esiName))
115 item_list.append((
'DataHeader', esiName))
117 peeked_data[
'eventdata_items'] = item_list
119 etc = cppyy.gbl.EventTypePTCnv_p3()
121 raw_bit_mask=etc.bit_mask(self)
122 bit_mask_typecodes = [ (
'IS_DATA',
'IS_SIMULATION'), (
'IS_ATLAS',
'IS_TESTBEAM'), (
'IS_PHYSICS',
'IS_CALIBRATION'),]
123 def decode_bitmask(idx):
124 if len(raw_bit_mask) <= idx:
125 return bit_mask_typecodes[idx][0]
126 isa_idx = raw_bit_mask[idx]
127 return bit_mask_typecodes[idx][isa_idx]
128 bm = map(decode_bitmask,
129 range(len(bit_mask_typecodes)))
132 def _get_detdescr_tags(evt_type):
133 ddt = evt_type.get_detdescr_tags().
split()
136 ddt = dict(zip(ddt[0::2],
140 etp=esic.eventTypes(esi)
144 peeked_data[
'evt_type'] =
bit_mask(et)
146 peeked_data[
'mc_channel_number'] = [et.m_mc_channel_number]
147 peeked_data[
'evt_number'] = [et.m_mc_event_number]
149 print (
'mc_event_weights.size:', et.m_mc_event_weights.size(), file=stdout)
150 print (
'mc_event_weights value', et.m_mc_event_weights[0], file=stdout)
151 print (
'user_type', et.m_user_type, file=stdout)
154 if peeked_data[
'nentries'] == 0:
155 peeked_data[
'stream_names'] = [esiName]
159 if len (peeked_data[
'run_number']) == 0
and meta.FindBranch(
'ByteStreamMetadata'):
160 bsmd = cppyy.gbl.ByteStreamMetadataPTCnv_p1()
161 peeked_data[
'lumi_block'] = [bsmd.getLumiBlock(meta.ByteStreamMetadata[0])]
162 peeked_data[
'run_number'] = [bsmd.getRunNumber(meta.ByteStreamMetadata[0])]
163 peeked_data[
'beam_type'] = [bsmd.getBeamType(meta.ByteStreamMetadata[0])]
164 peeked_data[
'beam_energy'] = [bsmd.getBeamEnergy(meta.ByteStreamMetadata[0])]
166 for md
in bsmd.getFreeMetaDataStrings(meta.ByteStreamMetadata[0]):
167 if md.startswith(
'Event type:'):
170 if 'is sim' in md: v.append(
'IS_SIMULATION')
171 else: v.append(
'IS_DATA')
172 if 'is atlas' in md: v.append(
'IS_ATLAS')
173 else: v.append(
'IS_TESTBEAM')
174 if 'is physics' in md: v.append(
'IS_PHYSICS')
175 else: v.append(
'IS_CALIBRATION')
176 bs_metadata[k] = tuple(v)
177 elif md.startswith(
'GeoAtlas:'):
179 v = md.split(
'GeoAtlas:')[1].strip()
181 elif md.startswith(
'IOVDbGlobalTag:'):
183 v = md.split(
'IOVDbGlobalTag:')[1].strip()
191 bs_metadata[
'Stream'] = bsmd.getStream(meta.ByteStreamMetadata[0])
192 bs_metadata[
'Project'] = bsmd.getProject(meta.ByteStreamMetadata[0])
193 if len(bs_metadata.get(
'evt_type',
'')) == 0 :
194 evt_type = [
'IS_DATA',
'IS_ATLAS']
195 if bs_metadata.get(
'Stream',
'').startswith(
'physics_'):
196 evt_type.append(
'IS_PHYSICS')
197 elif bs_metadata.get(
'Stream',
'').startswith(
'calibration_'):
198 evt_type.append(
'IS_CALIBRATION')
199 elif bs_metadata.get(
'Project',
'').endswith(
'_calib'):
200 evt_type.append(
'IS_CALIBRATION')
202 evt_type.append(
'Unknown')
203 bs_metadata[
'evt_type'] = evt_type
205 peeked_data[
'evt_type'] = bs_metadata.get(
'evt_type', [])
206 peeked_data[
'geometry'] = bs_metadata.get(
'geometry',
None)
207 peeked_data[
'conditions_tag'] = bs_metadata.get(
'conditions_tag',
None)
208 peeked_data[
'bs_metadata'] = bs_metadata
211 cnv = cppyy.gbl.IOVMetaDataContainerPTCnv_p1()
213 obj = cppyy.gbl.IOVMetaDataContainer()
215 def process_metadata(obj, metadata_name):
217 print (
'processing container [%s]' % obj.folderName(), file=stdout)
219 payloads = obj.payloadContainer()
220 payloads_sz = payloads.size()
221 if hasattr(payloads,
'at'):
225 for ii
in range(payloads_sz):
226 payloads.append(_tmp.at(ii))
228 for ii,payload
in zip(
range(payloads_sz), payloads):
231 print (
"**error** null-pointer ?", file=stdout)
235 sz = payload.name_size()
237 print (
'==names== (sz: %s)' % sz, file=stdout)
238 for idx
in range(sz):
239 chan = payload.chanNum(idx)
240 chan_name = payload.chanName(chan)
241 chan_names.append(chan_name)
245 sz = payload.iov_size()
247 print (
'==iovs== (sz: %s)' % sz, file=stdout)
248 for idx
in range(sz):
249 chan = payload.chanNum(idx)
250 iov_range = payload.iovRange(chan)
251 iov_start = iov_range.start()
252 iov_stop = iov_range.stop()
254 print (
'(%s, %s) => (%s, %s) valid=%s runEvt=%s' % (
260 iov_start.isRunEvent()),
267 print (
'==attrs== (sz: %s)' % sz, file=stdout)
268 for idx
in range(sz):
269 chan = payload.chanNum(idx)
270 attr_list = payload.attributeList(chan)
272 for a
in list(
toiter(attr_list.begin(), attr_list.end())):
273 spec = a.specification()
274 a_type = spec.typeName()
275 if a_type.find(
'string') >= 0:
276 a_data = a.data[
'string']()
278 a_data = eval(a_data,{},{})
283 a_data = a.data[a_type]()
284 attr_data.append( (spec.name(), a_data) )
285 attrs.append(dict(attr_data))
286 if len(attrs) == len(chan_names):
287 data.append(dict(zip(chan_names,attrs)))
291 data.append(attrs[0])
295 data.append(chan_names)
300 def maybe_get(o, idx, default=None):
314 for l
in meta.GetListOfLeaves():
315 clName = l.GetTypeName().
split(
'_p')[0]
317 if l.GetTypeName() ==
'IOVMetaDataContainer_p1':
319 pers = getattr (meta, k)
320 cnv.persToTrans(pers,obj)
323 obj.payloadContainer().at(0).
dump()
326 print (l.GetName(), file=stdout)
328 v = process_metadata(obj, k)
329 flName = obj.folderName()
330 metadata[obj.folderName()] = maybe_get(v, -1)
331 if flName[:11]
in [
'TriggerMenu',
'CutBookkeep',
'IncompleteC']
and clName[:6] !=
'xAOD::':
continue
332 metadata_items.append((clName,flName))
333 if clName ==
'EventStreamInfo':
334 metadata_items.append((
'DataHeader',
';00;MetaDataSvc'))
336 peeked_data[
'metadata'] = metadata
337 peeked_data[
'metadata_items'] = metadata_items
340 if '/TRIGGER/HLT/Menu' in metadata
and peeked_data[
'nentries'] > 0:
342 for d
in metadata.get(
'/TRIGGER/HLT/Menu'):
343 ti.add(d[
'StreamInfo'])
351 print(
'**error** Invalid StreamInfo entry:',item, file=stdout)
353 stream_tags.append( {
'obeys_lbk':
bool(i[2]),
'stream_name':i[0],
'stream_type':i[1] } )
355 peeked_data[
'stream_tags'] = stream_tags
357 from PyUtils.Helpers
import release_metadata
359 project = rel_metadata[
'project name']
360 version = rel_metadata[
'release']
361 release = project +
'-' + version
365 if '/TagInfo' in metadata:
366 taginfo = metadata[
'/TagInfo'].
copy()
369 if peeked_data[
'nentries'] > 0:
370 taginfo[
'AtlasRelease'] = release
372 peeked_data[
'tag_info'] = taginfo
374 peeked_data[
'det_descr_tags'] = taginfo
375 peeked_data[
'geometry'] = taginfo.get(
'GeoAtlas',
None)
376 peeked_data[
'conditions_tag'] = taginfo.get(
'IOVDbGlobalTag',
None)
377 peeked_data[
'beam_type'] = [taginfo.get(
'beam_type',
'N/A')]
378 peeked_data[
'beam_energy']= [maybe_float(taginfo.get(
'beam_energy',
381 if 'evt_type' not in peeked_data:
382 if '/Simulation/Parameters' in metadata:
383 peeked_data[
'evt_type'] = [
'IS_SIMULATION',
'IS_ATLAS',
'IS_PHYSICS']
384 peeked_data[
'run_number'] = [metadata[
'/Simulation/Parameters'].
get(
'RunNumber',
'')]
386 peeked_data[
'evt_type'] = []
388 if 'geometry' not in peeked_data:
389 peeked_data[
'geometry'] =
None
390 if 'conditions_tag' not in peeked_data:
391 peeked_data[
'conditions_tag'] =
None
392 if 'det_descr_tags' not in peeked_data:
393 peeked_data[
'det_descr_tags'] = {}
397 print (
':::::: summary ::::::', file=stdout)
398 print (
' - nbr events: %s' % peeked_data[
'nentries'], file=stdout)
399 print (
' - run numbers: %s' % peeked_data[
'run_number'], file=stdout)
400 print (
' - lumiblocks: %s' % peeked_data[
'lumi_block'], file=stdout)
401 print (
' - evt types: ', peeked_data[
'evt_type'], file=stdout)
402 print (
' - item list: %s' % len(peeked_data[
'eventdata_items']), file=stdout)
403 print (
' - processing tags: %s' % peeked_data[
'stream_names'], file=stdout)
404 print (
' - geometry: %s' % peeked_data[
'geometry'], file=stdout)
405 print (
' - conditions tag: %s' % peeked_data[
'conditions_tag'], file=stdout)
406 print (
' - tag-info: %s' % peeked_data[
'tag_info'].
keys(), file=stdout)
413 """save file informations using sqlite"""
415 fd_pkl,out_pkl_fname = tempfile.mkstemp(suffix=
'.pkl')
417 if os.path.exists(out_pkl_fname):
418 os.remove(out_pkl_fname)
419 oname = os.path.expanduser(os.path.expandvars(out_pkl_fname))
420 if os.path.exists(oname):
423 import PyUtils.dbsqlite
as dbsqlite
424 db = dbsqlite.open(oname,flags=
'w')
428 if os.path.exists(oname)
and len(self.
peeked_data) > 0:
429 return 0, out_pkl_fname
432 return 1, out_pkl_fname