Garfield++ v1r0
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.
4
5// I. B. Smirnov, 2003.
6
7float chispre(float *x, float *p, float *f, long q) {
8 mfunnamep("float chispre(float *x, float *p, float *f, long q)");
9 check_econd11(q, <= 0, mcerr);
10 float r = 0;
11 for (long i = 0; i < q; ++i) {
12 check_econd11(p[i], < 0.0, mcerr);
13 r += p[i] * (x[i + 1] - x[i]);
14 f[i] = r;
15 }
16 check_econd11(r, <= 0, mcerr);
17 for (long i = 0; i < q; ++i)
18 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(DynLinArr<double> &f, int s_allow_zero_f) {
68 mfunnamep("double chispre(DynLinArr< double >& f, int s_allow_zero_f=0)");
69 //check_econd12(p.get_qel() , != , f.get_qel() , mcerr);
70 const long q = f.get_qel();
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(DynLinArr< double >& "
79 "f, 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)
89 f[i] /= r;
90 return r;
91}
92
93double chisran(double flat_random_number, DynLinArr<double> f) {
95 "double chisran(double flat_random_number, DynLinArr < double > f)");
96 //mcout<<"chisran is started\n";
97 //Iprintn(mcout, flat_random_number);
98 const long q = f.get_qel();
99 check_econd11(q, <= 0, mcerr);
100 check_econd21(flat_random_number, < 0.0 &&, > 1.0, mcerr);
101 if (flat_random_number == 0.0) {
102 for (long n = 0; n < q; ++n) {
103 if (f[n] > 0.0) return double(n);
104 }
105 } else {
106 if (flat_random_number == 1.0) {
107 for (long n = q - 1; n >= 0; n--) {
108 if (f[n] < 1.0) return double(n + 1);
109 }
110 } else {
111 if (flat_random_number <= f[0]) {
112 return flat_random_number / f[0];
113 } else {
114 long nl = 0;
115 long nr = q - 1;
116 long nc;
117 while (nr - nl > 1) {
118 nc = (nr + nl) / 2;
119 if (flat_random_number < f[nc]) {
120 nr = nc;
121 } else {
122 nl = nc;
123 }
124 }
125 const double xl = double(nl + 1);
126 const double xr = double(nr + 1);
127 const double yl = f[nl];
128 const double yr = f[nr];
129 const double a = (xr - xl) / (yr - yl);
130 const double b = xl;
131 //Iprint3n(mcout, nl, nr, nc);
132 //Iprint2n(mcout, xl, xr);
133 //Iprint2n(mcout, yl, yr);
134 //Iprint2n(mcout, a, b);
135 return a * (flat_random_number - yl) + b;
136 }
137 }
138 }
139 funnw.ehdr(mcerr);
140 mcerr << "should never happen\n";
141 spexit(mcerr);
142 return 0.0;
143}
#define check_econd21(a, sign1_b1_sign0, sign2_b2, stream)
Definition: FunNameStack.h:428
#define check_econd11(a, signb, stream)
Definition: FunNameStack.h:366
#define check_econd11a(a, signb, add, stream)
Definition: FunNameStack.h:395
#define mfunnamep(string)
Definition: FunNameStack.h:77
#define spexit(stream)
Definition: FunNameStack.h:536
float chispre(float *x, float *p, float *f, long q)
Definition: chisran.cpp:7
float chisran(float flat_random_number, float *x, float *f, long q)
Definition: chisran.cpp:22
long get_qel(void) const
Definition: AbsArr.h:420
#define mcout
Definition: prstream.h:133
#define mcerr
Definition: prstream.h:135
#define Iprint2n(file, name1, name2)
Definition: prstream.h:236