diff options
author | Anthony LaTorre <tlatorre9@gmail.com> | 2011-08-29 09:46:36 -0400 |
---|---|---|
committer | Anthony LaTorre <tlatorre9@gmail.com> | 2011-08-29 09:46:36 -0400 |
commit | 7e2a7e988031c22898249f3801aa0d3c690bb729 (patch) | |
tree | 2ef5d982f9dec9f90ea54e6481d3b3b88048fb3d | |
parent | 1943931e95711f9676b5585f901c5fff25b7d759 (diff) | |
download | chroma-7e2a7e988031c22898249f3801aa0d3c690bb729.tar.gz chroma-7e2a7e988031c22898249f3801aa0d3c690bb729.tar.bz2 chroma-7e2a7e988031c22898249f3801aa0d3c690bb729.zip |
add generator which yields drawn randomly from a histogram interpreted as a pdf.
-rw-r--r-- | generator/vertex.py | 9 |
1 files changed, 9 insertions, 0 deletions
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 |