Garfield++ 4.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
chisran.cpp
Go to the documentation of this file.
3
4// I. B. Smirnov, 2003.
5
6namespace Heed {
7
8double chispre(std::vector<double> &f, int s_allow_zero_f) {
9 mfunnamep("double chispre(vector<double>& f, int s_allow_zero_f)");
10 const size_t q = f.size();
11 check_econd11(q, <= 0, mcerr);
12 double r = 0;
13 for (size_t i = 0; i < q; ++i) {
14 if (s_allow_zero_f == 0) {
15 check_econd11a(f[i], < 0.0, "i=" << i << '\n', mcerr);
16 } else {
17 if (f[i] < 0.0) {
18 mcout << "Warning: f[i] < 0.0 in chispre\n";
19 Iprint2n(mcout, i, f[i]);
20 f[i] = 0.0;
21 }
22 }
23 r += f[i];
24 f[i] = r;
25 }
26 check_econd11(r, <= 0, mcerr);
27 const double scale = 1. / r;
28 for (size_t i = 0; i < q; ++i) f[i] *= scale;
29 return r;
30}
31
32double chisran(double flat_random_number, const std::vector<double> &f) {
33 mfunnamep("double chisran(double flat_random_number, vector<double>& f)");
34 const long q = f.size();
35 check_econd11(q, <= 0, mcerr);
36 check_econd21(flat_random_number, < 0.0 ||, > 1.0, mcerr);
37 if (flat_random_number == 0.0) {
38 for (long n = 0; n < q; ++n) {
39 if (f[n] > 0.0) return double(n);
40 }
41 } else {
42 if (flat_random_number == 1.0) {
43 for (long n = q - 1; n >= 0; n--) {
44 if (f[n] < 1.0) return double(n + 1);
45 }
46 } else {
47 if (flat_random_number <= f[0]) {
48 return flat_random_number / f[0];
49 }
50 long nl = 0;
51 long nr = q - 1;
52 long nc;
53 while (nr - nl > 1) {
54 nc = (nr + nl) / 2;
55 if (flat_random_number < f[nc]) {
56 nr = nc;
57 } else {
58 nl = nc;
59 }
60 }
61 const double xl = double(nl + 1);
62 const double xr = double(nr + 1);
63 const double yl = f[nl];
64 const double yr = f[nr];
65 const double a = (xr - xl) / (yr - yl);
66 const double b = xl;
67 // Iprint3n(mcout, nl, nr, nc);
68 // Iprint2n(mcout, xl, xr);
69 // Iprint2n(mcout, yl, yr);
70 // Iprint2n(mcout, a, b);
71 return a * (flat_random_number - yl) + b;
72 }
73 }
74 funnw.ehdr(mcerr);
75 mcerr << "should never happen\n";
77 return 0.0;
78}
79}
#define check_econd21(a, sign1_b1_sign0, sign2_b2, stream)
Definition: FunNameStack.h:191
#define check_econd11(a, signb, stream)
Definition: FunNameStack.h:155
#define check_econd11a(a, signb, add, stream)
Definition: FunNameStack.h:172
#define mfunnamep(string)
Definition: FunNameStack.h:49
#define spexit(stream)
Definition: FunNameStack.h:256
Definition: BGMesh.cpp:6
double chisran(double flat_random_number, const std::vector< double > &f)
Definition: chisran.cpp:32
double chispre(std::vector< double > &f, int s_allow_zero_f)
Definition: chisran.cpp:8
#define mcout
Definition: prstream.h:126
#define mcerr
Definition: prstream.h:128
#define Iprint2n(file, name1, name2)
Definition: prstream.h:219