示例#1
0
def test_pickler_proxy():
    h = Hist(5, 0, 1, name='hist')
    f = tempfile.NamedTemporaryFile(suffix='.root')

    with root_open(f.name, 'recreate') as outfile:
        dump([h], outfile)

    class IsCalled(object):
        def __init__(self, func):
            self.func = func
            self.called = False

        def __call__(self, path):
            if path != '_pickle;1':
                self.called = True
            return self.func(path)

    with root_open(f.name) as infile:
        infile.Get = IsCalled(infile.Get)
        hlist = load(infile, use_proxy=False)
        assert_true(infile.Get.called)

    with root_open(f.name) as infile:
        infile.Get = IsCalled(infile.Get)
        hlist = load(infile, use_proxy=True)
        assert_false(infile.Get.called)
        assert_equal(hlist[0].name, 'hist')
        assert_true(infile.Get.called)

    f.close()
示例#2
0
def test_pickler():
    hlist = list()
    for i in range(10):
        hlist.append(Hist(10, 0, 10))

    with TemporaryFile() as tmpfile:
        dump(hlist, tmpfile)
        hlist_out = load(tmpfile)
        assert_equal([h.name for h in hlist_out], [h.name for h in hlist])

    hdict = dict()
    for i in range(100):
        hist = Hist(10, 0, 1, type=random.choice('CSIFD'))
        hdict[hist.name] = hist

    with TemporaryFile() as tmpfile:
        rdir = tmpfile.mkdir('pickle')
        dump(hdict, rdir)
        hdict_out = load(rdir)
        assert_equal(len(hdict_out), 100)
        for name, hist in hdict_out.items():
            assert_equal(name, hist.name)
            assert_equal(hist.TYPE, hdict[hist.name].TYPE)
示例#3
0
def from_root(input_file):
    '''
        Loads the obj from a ROOT file
    '''
    reloaded = load(input_file, use_proxy=False)

    # Histogram objects are "owned" by the current TDirectory.  Need to unhook
    # them, so that they're not free-ed when we close that the directory
    for obj in reloaded.__dict__.values():
        if isinstance(obj, (Hist, _HistBase)):
            obj.SetDirectory(None)
        elif isinstance(obj, HistogramCollection):
            for bins, hist in obj.flat_items_all():
                if isinstance(hist, (Hist, _HistBase)):
                    hist.SetDirectory(None)

    return reloaded
示例#4
0
 def from_root(input_file):
     from rootpy.io.pickler import load
     instance, pileupHist = load(input_file)
     instance._pileupHist = pileupHist
     return instance
示例#5
0
 def from_root(input_file):
     from rootpy.io.pickler import load
     return load(input_file)