Garfield++ v2r0
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
8float chispre(float *x, float *p, float *f, long q) {
9 mfunnamep("float chispre(float *x, float *p, float *f, long q)");
10 check_econd11(q, <= 0, mcerr);
11 float r = 0;
12 for (long i = 0; i < q; ++i) {
13 check_econd11(p[i], < 0.0, mcerr);
14 r += p[i] * (x[i + 1] - x[i]);
15 f[i] = r;
16 }
17 check_econd11(r, <= 0, mcerr);
18 for (long i = 0; i < q; ++i) f[i] /= r;
19 return r;
20}
21
22float chisran(float flat_random_number, float *x, float *f, long q) {
24 "float chisran(float flat_random_number, float *x, float *f, long q)");
25 check_econd11(q, <= 0, mcerr);
26 check_econd21(flat_random_number, < 0.0 &&, > 1.0, mcerr);
27 if (flat_random_number == 0.0) {
28 for (long n = 0; n < q; ++n) {
29 if (f[n] > 0.0) return x[n];
30 }
31 } else {
32 if (flat_random_number == 1.0) {
33 for (long n = q - 1; n >= 0; n--) {
34 if (f[n] < 1.0) return x[n + 1];
35 }
36 } else {
37 if (flat_random_number <= f[0]) {
38 return flat_random_number / f[0];
39 } else {
40 long nl = 0;
41 long nr = q - 1;
42 long nc;
43 while (nr - nl > 1) {
44 nc = (nr + nl) / 2;
45 if (flat_random_number < f[nc]) {
46 nr = nc;
47 } else {
48 nl = nc;
49 }
50 }
51 const float xl = x[nl + 1];
52 const float xr = x[nr + 1];
53 const float yl = f[nl];
54 const float yr = f[nr];
55 const float a = (xr - xl) / (yr - yl);
56 const float b = xl;
57 return a * (flat_random_number - yl) + b;
58 }
59 }
60 }
61 funnw.ehdr(mcerr);
62 mcerr << "should never happen\n";
64 return 0.0;
65}
66
67double chispre(std::vector<double> &f, int s_allow_zero_f) {
68 mfunnamep("double chispre(vector<double>& f, int s_allow_zero_f=0)");
69 // check_econd12(p.get_qel() , != , f.get_qel() , mcerr);
70 const long q = f.size();
71 check_econd11(q, <= 0, mcerr);
72 double r = 0;
73 for (int i = 0; i < q; ++i) {
74 if (s_allow_zero_f == 0) {
75 check_econd11a(f[i], < 0.0, "i=" << i << '\n', mcerr);
76 } else {
77 if (f[i] < 0.0) {
78 mcout << "Warning: f[i] < 0.0 in double chispre(vector<double>& f, "
79 "int s_allow_zero_f)\n";
80 Iprint2n(mcout, i, f[i]);
81 f[i] = 0.0;
82 }
83 }
84 r += f[i];
85 f[i] = r;
86 }
87 check_econd11(r, <= 0, mcerr);
88 for (int i = 0; i < q; ++i) f[i] /= r;
89 return r;
90}
91
92double chisran(double flat_random_number, const std::vector<double> &f) {
93 mfunnamep("double chisran(double flat_random_number, vector<double> f)");
94 const long q = f.size();
95 check_econd11(q, <= 0, mcerr);
96 check_econd21(flat_random_number, < 0.0 &&, > 1.0, mcerr);
97 if (flat_random_number == 0.0) {
98 for (long n = 0; n < q; ++n) {
99 if (f[n] > 0.0) return double(n);
100 }
101 } else {
102 if (flat_random_number == 1.0) {
103 for (long n = q - 1; n >= 0; n--) {
104 if (f[n] < 1.0) return double(n + 1);
105 }
106 } else {
107 if (flat_random_number <= f[0]) {
108 return flat_random_number / f[0];
109 } else {
110 long nl = 0;
111 long nr = q - 1;
112 long nc;
113 while (nr - nl > 1) {
114 nc = (nr + nl) / 2;
115 if (flat_random_number < f[nc]) {
116 nr = nc;
117 } else {
118 nl = nc;
119 }
120 }
121 const double xl = double(nl + 1);
122 const double xr = double(nr + 1);
123 const double yl = f[nl];
124 const double yr = f[nr];
125 const double a = (xr - xl) / (yr - yl);
126 const double b = xl;
127 // Iprint3n(mcout, nl, nr, nc);
128 // Iprint2n(mcout, xl, xr);
129 // Iprint2n(mcout, yl, yr);
130 // Iprint2n(mcout, a, b);
131 return a * (flat_random_number - yl) + b;
132 }
133 }
134 }
135 funnw.ehdr(mcerr);
136 mcerr << "should never happen\n";
137 spexit(mcerr);
138 return 0.0;
139}
140}
#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:5
float chispre(float *x, float *p, float *f, long q)
Definition: chisran.cpp:8
float chisran(float flat_random_number, float *x, float *f, long q)
Definition: chisran.cpp:22
#define mcout
Definition: prstream.h:126
#define mcerr
Definition: prstream.h:128
#define Iprint2n(file, name1, name2)
Definition: prstream.h:220