AmpGen 2.1
Loading...
Searching...
No Matches
DiscreteDistribution.h
Go to the documentation of this file.
1#ifndef AMPGEN_DISCRETEDISTRIBUTION_H
2#define AMPGEN_DISCRETEDISTRIBUTION_H 1
3
4#include <vector>
5#include <stack>
6#include <tuple>
7#include "TRandom3.h"
8
9// Generator of discrete distributions,
10// based on https://github.com/DavidPal/discrete-distribution
11// But modified to work with TRandom
12namespace AmpGen {
13
15 public:
17 DiscreteDistribution(const std::vector<double>& weights);
18
19 unsigned operator()(TRandom3* generator) const {
20 const double number = generator->Uniform();
21 size_t index = floor(m_buckets.size() * number);
22 const auto& bucket = m_buckets[index];
23 return number < std::get<2>(bucket) ? std::get<0>(bucket) : std::get<1>(bucket);
24 }
25
26 unsigned min() const;
27 unsigned max() const;
28
29 const std::vector<double>& probabilities() const;
30 private:
31 void create_buckets();
32 std::vector<double> m_probabilities;
33 std::vector<std::tuple<unsigned, unsigned, double>> m_buckets;
34 };
35}
36
37#endif
const std::vector< double > & probabilities() const
unsigned operator()(TRandom3 *generator) const
DiscreteDistribution(const std::vector< double > &weights)