test_random.cxx
Go to the documentation of this file.
1 /*
2  Test out the IRandom implementation Gen::Random.
3  */
4 
5 
10 #include "WireCellUtil/ExecMon.h"
11 #include "WireCellUtil/Testing.h"
12 
13 #include <iostream>
14 #include <complex>
15 #include <vector>
16 
17 using namespace std;
18 using namespace WireCell;
19 
20 double normalize(std::complex<double> val) {
21  return std::abs(val);
22 }
23 double normalize(double val) {
24  return val;
25 }
26 double normalize(int val) {
27  return (double)val;
28 }
29 
30 template<typename NumType, int nbins=10, int nstars = 100, int ntries=100000>
31 void histify(std::function<NumType()> gen) {
32  int hist[nbins+2]={};
33  for (int count=0; count<ntries; ++count) {
34  double num = normalize(gen());
35  ++num; // shift to accommodate under/overflow
36  if (num<=0) num=0;
37  if (num>nbins) num = nbins+1;
38  ++hist[(int)(0.5+num)];
39  }
40  for (int bin=0; bin<nbins+2; ++bin) {
41  int bar = (hist[bin]*nstars)/ntries;
42 
43 
44  char sbin = '0'+(bin-1);
45  if (bin == 0) {
46  sbin = '-';
47  }
48  if (bin == nbins+1) {
49  sbin = '+';
50  }
51  cout << sbin << ": " << std::string(bar,'*') << std::endl;
52  }
53 }
54 
55 void test_named(std::string generator_name)
56 {
57  const std::string gen_random_name = "Random";
58  auto rnd = Factory::lookup<IRandom>(gen_random_name);
59  auto rndcfg = Factory::lookup<IConfigurable>(gen_random_name);
60 
61  {
62  auto cfg = rndcfg->default_configuration();
63  cfg["generator"] = generator_name;
64  rndcfg->configure(cfg);
65  }
66 
67 
68  // Beware, this is evil. Busting out the shared pointer and using
69  // histify<> here is just to save some typing in this test. It's
70  // okay in this test because histify<> goes not live longer than
71  // we hold the shared pointer.
72  IRandom* ptr = &(*rnd);
73 
74  cout << "binomial(9,0.5)\n";
75  histify<int>(std::bind(&IRandom::binomial, ptr, 9, 0.5));
76 
77  cout << "normal(5.0,3.0)\n";
78  histify<double>(std::bind(&IRandom::normal, ptr, 5.0,3.0));
79 
80  cout << "uniform(0.0,10.0)\n";
81  histify<double>(std::bind(&IRandom::uniform, ptr, 0.0, 10.0));
82  cout << "uniform(2.0,5.0)\n";
83  histify<double>(std::bind(&IRandom::uniform, ptr, 2.0,5.0));
84 
85  cout << "range(0,10)\n";
86  histify<int>(std::bind(&IRandom::range, rnd, 0,10));
87  cout << "range(2,4)\n";
88  histify<int>(std::bind(&IRandom::range, rnd, 2,4));
89 }
90 
92 {
93  auto rnd = Factory::lookup<IRandom>("Random");
94 
95  const int ntries = 5;
96  std::vector<double> v1(ntries), v2(ntries);
97  for (int ind=0; ind<ntries; ++ind) {
98  v1[ind] = rnd->normal(0,1);
99  }
100  for (int ind=0; ind<ntries; ++ind) {
101  v2[ind] = rnd->normal(0,1);
102  }
103  for (size_t ind=0; ind<v1.size(); ++ind) {
104  cerr << v1[ind] << "\t" << v2[ind] << endl;
105  Assert(std::abs(v1[ind] - v2[ind]) > 1.0e-8);
106  }
107 
108 }
109 
110 int main()
111 {
112  ExecMon em("starting");
114  pm.add("WireCellGen");
115  em("plugged in");
116 
117  cout << "DEFAULT:\n";
118  test_named("default");
119  em("default generator");
120 
121  cout << "\nTWISTER:\n";
122  test_named("twister");
123  em("twister generator");
124 
125  cout << "\nBOGUS:\n";
126  test_named("bogus");
127  em("bogus generator");
128 
129  test_repeat();
130  em("test repeat");
131 
132  cout << em.summary() << endl;
133 
134  return 0;
135 }
136 
std::string string
Definition: nybbler.cc:12
void histify(std::function< NumType()> gen)
Definition: test_random.cxx:31
const std::string instance
STL namespace.
void test_named(std::string generator_name)
Definition: test_random.cxx:55
cfg
Definition: dbjson.py:29
#define Assert
Definition: Testing.h:7
T abs(T value)
int main()
const double e
Definition: Main.h:22
double normalize(std::complex< double > val)
Definition: test_random.cxx:20
const void * ptr(const T *p)
Definition: format.h:3138
QTextStream & bin(QTextStream &s)
Plugin * add(const std::string &plugin_name, const std::string &libname="")
Add a plugin. If libname is not given, try to derive it.
void function(int client, int *resource, int parblock, int *test, int p)
std::string summary() const
Return summary up to now.
Definition: ExecMon.cxx:21
QTextStream & endl(QTextStream &s)
void test_repeat()
Definition: test_random.cxx:91