CMS 3D CMS Logo

condhdf5tohdf5.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 import argparse
3 import sys
4 import logging
5 import copy
6 import h5py
7 import numpy as np
8 from collections import OrderedDict
9 import zlib
10 import lzma
11 from CondCore.CondHDF5ESSource.hdf5Writer import writeH5File
12 
13 #Global tags hold a list of Tags
14 # Tags give the
15 # record name,
16 # list of data products
17 # list of IOVs
18 # list of payloads per IOV
19 # Payloads give
20 # a payload name and
21 # the serialized data for a data product
22 # the type of data for the data product
23 #
24 
25 
27  def __init__(self, high, low):
28  self.high = high
29  self.low = low
30 
32  def __init__(self,dataset,name, compressor):
33  self._dataset = dataset
34  self._hash = name
35  self._type = dataset.attrs['type']
36  self._memsize = dataset.attrs['memsize']
37  self._compressor = compressor
38  def name(self):
39  return self._hash
40  def actualType(self):
41  return self._type
42  def memsize(self):
43  return self._memsize
44  def data(self):
45  ds = self._dataset[()]
46  if len(ds) == self.memsize():
47  return ds
48  #was compressed
49  return self._compressor.decompress(ds)
50 
52  def __init__(self, group, name, compressor):
53  self._type = group.attrs['type']
54  self._name = name
55  self._payloadGroup = group['Payloads']
56  self._compressor = compressor
57  def name(self):
58  return self._name
59  def objtype(self):
60  return self._type
61  def payloads(self):
62  return [H5Payload(self._payloadGroup[p],p.split('/')[-1], self._compressor) for p in self._payloadGroup]
63  def idToPayloadNames(self):
64  return { self._payloadGroup[p].id:p.split('/')[-1] for p in self._payloadGroup }
65 
66 class H5Tag(object):
67  def __init__(self, file, group, name):
68  self._file = file
69 
70  compressor = None
71  compressorName = self._file.attrs['default_payload_compressor']
72  if compressorName == 'lzma':
73  compressor = lzma
74  if compressorName == 'zlib':
75  compressor = zlib
76  self._group = group
77  self._record = self._group.attrs['record']
78  self._name = name
79 
80  recordGroup = file['Records'][self._record]
81  dataProductsGroup = recordGroup['DataProducts']
82 
83  self._dataProducts = [H5DataProduct(dataProductsGroup[g],g.split('/')[-1], compressor) for g in dataProductsGroup]
84  self._dbtags = self._group.attrs['db_tags']
85  self._time_type = self._group.attrs['time_type']
86  def record(self):
87  return self._record
88  def name(self):
89  return self._name
90  def time_type(self):
91  return self._time_type
92  def originalTagNames(self):
93  return self._dbtags
94  def iovsNPayloadNames(self):
95 
96  #asking an h5 object for its name is a slow operation
97  idToName = {self._file['null_payload'].id: None}
98  for d in self._dataProducts:
99  idToName.update(d.idToPayloadNames())
100 
101  first = self._group['first'][()]
102  last = self._group['last'][()]
103  payloadRefs = self._group['payload']
104  return list(zip( (IOVSyncValue(x['high'],x['low']) for x in first),
105  (IOVSyncValue(x['high'],x['low']) for x in last),
106  ([idToName[self._file[r].id] for r in refs] for refs in payloadRefs)) )
107 
108  def dataProducts(self):
109  return self._dataProducts
110 
112  def __init__(self, filename, name):
113  self._file = h5py.File(filename,'r')
114  self._name = name
115 
116  def tags(self):
117  #looking up names is slow so better to make cache
118  tagID2Name = {}
119  recordsGroup = self._file['Records']
120  for recordName in recordsGroup:
121  r = recordsGroup[recordName]
122  tagsGroup = r['Tags']
123  for tagName in tagsGroup:
124  tagID2Name[tagsGroup[tagName].id] = tagName
125  globalTagGroup = self._file['GlobalTags'][self._name]
126  return (H5Tag(self._file, self._file[t], tagID2Name[self._file[t].id]) for t in globalTagGroup['Tags'])
127 
128 def main():
129  parser = argparse.ArgumentParser(description='Read from HDF5 file and write to HDF5 file')
130  parser.add_argument('input', help="Name of file to read")
131  parser.add_argument('name', nargs='+', help="Name of the global tag.")
132 
133  parser.add_argument('--exclude', '-e', nargs='*', help = 'list of records to exclude from the file (can not be used with --include)')
134  parser.add_argument('--include', '-i', nargs='*', help = 'lost of the only records that should be included in the file (can not be used with --exclude')
135  parser.add_argument('--output', '-o', default='test.h5cond', help='name of hdf5 output file to write')
136  parser.add_argument('--compressor', '-c', default='zlib', choices=['zlib', 'lzma', 'none'], help="compress data using 'zlib', 'lzma' or 'none'")
137  args = parser.parse_args()
138 
139  if args.exclude and args.include:
140  print("Can not use --exclude and --include at the same time")
141  exit(-1)
142 
143  excludeRecords = set()
144  if args.exclude:
145  excludeRecords = set(args.exclude)
146  includeRecords = set()
147  if args.include:
148  includeRecords = set(args.include)
149 
150  writeH5File(args.output, args.name, excludeRecords, includeRecords, lambda x: H5GlobalTag(args.input, x), args.compressor)
151 if __name__ == '__main__':
152  main()
def __init__(self, group, name, compressor)
def iovsNPayloadNames(self)
def writeH5File(fileName, globalTags, excludeRecords, includeRecords, tagReader, compressorName)
Definition: hdf5Writer.py:70
def __init__(self, dataset, name, compressor)
def originalTagNames(self)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def __init__(self, high, low)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def __init__(self, file, group, name)
def __init__(self, filename, name)
Definition: main.py:1
def exit(msg="")