|
[yoda-svn] r605 - trunk/binblackhole at projects.hepforge.org blackhole at projects.hepforge.orgWed May 29 19:28:04 BST 2013
Author: davemallows Date: Wed May 29 19:28:03 2013 New Revision: 605 Log: Changed interface to merge-histos and factored out merging Modified: trunk/bin/merge-histos Modified: trunk/bin/merge-histos ============================================================================== --- trunk/bin/merge-histos Wed May 29 16:11:48 2013 (r604) +++ trunk/bin/merge-histos Wed May 29 19:28:03 2013 (r605) @@ -1,36 +1,30 @@ -#!/usr/bin/python +#! /usr/bin/env python -import yoda -import optparse -import cStringIO +"""\ +%prog [-o outfile] yodafile... -parser = optparse.OptionParser() -opts, filenames = parser.parse_args() - -class FileMerger(object): - def __init__(self): - self._objects = {} +Merge Analysis Objects from multiple YODA files. +""" - def merge(self, x): - existing = self._objects.get(x.path) +import yoda, optparse, operator, itertools - if existing: - x += existing +parser = optparse.OptionParser(usage=__doc__) +parser.add_option('-o', '--output', default='-', dest='OUTPUT_FILE') +opts, filenames = parser.parse_args() - self._objects[x.path] = x +# TODO: move to util module +def merge(objects, op=operator.iadd): + out_objects = {} - @property - def objects(self): - return self._objects.itervalues() + for ob in objects: + existing = out_objects.get(ob.path) + if existing: + op(ob, existing) + out_objects[ob.path] = ob -merger = FileMerger() + return out_objects -for filename in filenames: - print filename - for i in yoda.readYODA(filename): - merger.merge(i) +analysisobjects_in = (yoda.readYODA(filename) for filename in filenames) +analysisobjects_out = merge(itertools.chain(*analysisobjects_in)) -stringio = cStringIO.StringIO() -yoda.writeYODA(merger.objects, stringio) -stringio.seek(0) -print stringio.read(), +yoda.writeYODA(analysisobjects_out.values(), opts.OUTPUT_FILE)
More information about the yoda-svn mailing list |