From 7e2a7e988031c22898249f3801aa0d3c690bb729 Mon Sep 17 00:00:00 2001 From: Anthony LaTorre Date: Mon, 29 Aug 2011 09:46:36 -0400 Subject: add generator which yields drawn randomly from a histogram interpreted as a pdf. --- generator/vertex.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'generator') diff --git a/generator/vertex.py b/generator/vertex.py index 5626541..450c0e4 100644 --- a/generator/vertex.py +++ b/generator/vertex.py @@ -4,9 +4,18 @@ from itertools import izip, count from chroma.pi0 import pi0_decay from chroma.event import Event, Subtrack from chroma.sample import uniform_sphere +from chroma.itertoolset import repeat_func # generator parts for use with gun() +def from_histogram(h): + "Yield values drawn randomly from the histogram `h` interpreted as a pdf." + pdf = h.hist/h.hist.sum() + cdf = np.cumsum(pdf) + + for x in repeat_func(np.random.random_sample): + yield h.bincenters[np.searchsorted(cdf, x)] + def constant(obj): while True: yield obj -- cgit