# Create a row-wise ntuple in an HBOOK file. schema = hep.table.Schema() schema.addColumn("index", "int32") schema.addColumn("value32", "float32") schema.addColumn("value64", "float64") hbook_file = create("cwn1.hbook") table = createTable("cwn", hbook_file, schema, column_wise=1) # Fill it with random values, saving these in an array. values = [] for i in xrange(0, 100): value = random() values.append(value) table.append(index=i, value32=value, value64=value) del table, hbook_file # Open the ntuple. table = open("cwn1.hbook")["cwn"] assert_(table.column_wise) compare(len(table), 100) # Compare the values in it to the array. i = 0 for row in table: compare(row["index"], i) compare(row["value32"], values[i], precision=1e-6) compare(row["value64"], values[i], precision=1e-9) i += 1 del row, table
from hep.cernlib import hbook import sys def ls(hbook_dir, indent=0): # Loop over directory elements in 'path'. for name in hbook_dir.keys(): info = hbook_dir.getinfo(name) print "%s%s%s%5d %s" % (" " * indent, name, " " * (40 - indent - len(name)), info.rz_id, info.type) # If it's a directory, list its contents recursively. if info.type == "directory": ls(hbook_dir[name], indent + 1) ls(hbook.open(sys.argv[1]))
from hep.cernlib import hbook def copy(src_dir, dest_dir): # Loop over directory elements in 'path'. for name in src_dir.keys(): info = src_dir.getInfo(name) if info.is_directory: # It's a directory. Make the destination directory, and # call ourselves recursively to copy its contents. copy(src_dir[name], dest_dir.mkdir(name)) elif info.type in ("1D histogram", "2D histogram"): # It's a histogram. Load it, and save it to the destination. dest_dir[name] = src_dir[name] # Ignore other types of entries. if __name__ == "__main__": import sys copy(hbook.open(sys.argv[1]), hbook.create(sys.argv[2]))
def ls(hbook_path, path): keys = hbook.open(hbook_path)[path].keys() keys.sort() return keys
from hep.bool import * from hep.cernlib import hbook from hep.test import compare #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- hbook_file = hbook.create("remove2.hbook") hbook_file.mkdir("dir1") hbook_file.mkdir("dir2") hbook_file.mkdir("dir3") hbook_file.mkdir("dir3/dir4") del hbook_file def ls(hbook_path, path): keys = hbook.open(hbook_path)[path].keys() keys.sort() return keys compare(ls("remove2.hbook", ""), ["dir1", "dir2", "dir3"]) compare(ls("remove2.hbook", "dir3"), ["dir4"]) del hbook.open("remove2.hbook", writable=True)["dir2"] compare(ls("remove2.hbook", ""), ["dir1", "dir3"]) compare(ls("remove2.hbook", "dir3"), ["dir4"]) del hbook.open("remove2.hbook", writable=True)["dir3/dir4"] compare(ls("remove2.hbook", ""), ["dir1", "dir3"]) compare(ls("remove2.hbook", "dir3"), [])
#----------------------------------------------------------------------- # test #----------------------------------------------------------------------- hbook_file = hbook.create("remove1.hbook") hbook_file.mkdir("dir1") hbook_file.mkdir("dir2") histograms = [ Histogram1D(10, (0., 1.)) for i in range(0, 4) ] hbook_file.set("hist0", histograms[0], rz_id=100) hbook_file.set("hist1", histograms[1], rz_id=101) hbook_file.set("dir1/hist2", histograms[2], rz_id=100) hbook_file.set("dir2/hist3", histograms[3], rz_id=100) del hbook_file, histograms del hbook.open("remove1.hbook", writable=True)["hist1"] hbook_file = hbook.open("remove1.hbook") compare(len(hbook_file.keys()), 3) compare(hbook_file["dir1"].keys(), ["hist2"]) compare(hbook_file["dir2"].keys(), ["hist3"]) del hbook_file del hbook.open("remove1.hbook", writable=True)["dir2/hist3"] hbook_file = hbook.open("remove1.hbook") compare(len(hbook_file.keys()), 3) compare(hbook_file["dir1"].keys(), ["hist2"]) compare(hbook_file["dir2"].keys(), []) del hbook_file