1 | /** |
---|
2 | This software was developed by the University of Tennessee as part of the |
---|
3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | project funded by the US National Science Foundation. |
---|
5 | |
---|
6 | If you use DANSE applications to do scientific research that leads to |
---|
7 | publication, we ask that you acknowledge the use of the software with the |
---|
8 | following sentence: |
---|
9 | |
---|
10 | "This work benefited from DANSE software developed under NSF award DMR-0520547." |
---|
11 | |
---|
12 | copyright 2009, University of Tennessee |
---|
13 | */ |
---|
14 | #include "smearer.hh" |
---|
15 | #include <stdio.h> |
---|
16 | #include <math.h> |
---|
17 | using namespace std; |
---|
18 | |
---|
19 | |
---|
20 | /** |
---|
21 | * Constructor for BaseSmearer |
---|
22 | * |
---|
23 | * @param qmin: minimum Q value |
---|
24 | * @param qmax: maximum Q value |
---|
25 | * @param nbins: number of Q bins |
---|
26 | */ |
---|
27 | BaseSmearer :: BaseSmearer(double qmin, double qmax, int nbins) { |
---|
28 | // Number of bins |
---|
29 | this->nbins = nbins; |
---|
30 | this->qmin = qmin; |
---|
31 | this->qmax = qmax; |
---|
32 | // Flag to keep track of whether we have a smearing matrix or |
---|
33 | // whether we need to compute one |
---|
34 | has_matrix = false; |
---|
35 | even_binning = true; |
---|
36 | }; |
---|
37 | |
---|
38 | /** |
---|
39 | * Constructor for BaseSmearer |
---|
40 | * |
---|
41 | * Used for uneven binning |
---|
42 | * @param q: array of Q values |
---|
43 | * @param nbins: number of Q bins |
---|
44 | */ |
---|
45 | BaseSmearer :: BaseSmearer(double* q, int nbins) { |
---|
46 | // Number of bins |
---|
47 | this->nbins = nbins; |
---|
48 | this->q_values = q; |
---|
49 | // Flag to keep track of whether we have a smearing matrix or |
---|
50 | // whether we need to compute one |
---|
51 | has_matrix = false; |
---|
52 | even_binning = false; |
---|
53 | }; |
---|
54 | |
---|
55 | /** |
---|
56 | * Constructor for SlitSmearer |
---|
57 | * |
---|
58 | * @param width: slit width in Q units |
---|
59 | * @param height: slit height in Q units |
---|
60 | * @param qmin: minimum Q value |
---|
61 | * @param qmax: maximum Q value |
---|
62 | * @param nbins: number of Q bins |
---|
63 | */ |
---|
64 | SlitSmearer :: SlitSmearer(double width, double height, double qmin, double qmax, int nbins) : |
---|
65 | BaseSmearer(qmin, qmax, nbins){ |
---|
66 | this->height = height; |
---|
67 | this->width = width; |
---|
68 | }; |
---|
69 | |
---|
70 | /** |
---|
71 | * Constructor for SlitSmearer |
---|
72 | * |
---|
73 | * @param width: slit width in Q units |
---|
74 | * @param height: slit height in Q units |
---|
75 | * @param q: array of Q values |
---|
76 | * @param nbins: number of Q bins |
---|
77 | */ |
---|
78 | SlitSmearer :: SlitSmearer(double width, double height, double* q, int nbins) : |
---|
79 | BaseSmearer(q, nbins){ |
---|
80 | this->height = height; |
---|
81 | this->width = width; |
---|
82 | }; |
---|
83 | |
---|
84 | /** |
---|
85 | * Constructor for QSmearer |
---|
86 | * |
---|
87 | * @param width: array slit widths for each Q point, in Q units |
---|
88 | * @param qmin: minimum Q value |
---|
89 | * @param qmax: maximum Q value |
---|
90 | * @param nbins: number of Q bins |
---|
91 | */ |
---|
92 | QSmearer :: QSmearer(double* width, double qmin, double qmax, int nbins) : |
---|
93 | BaseSmearer(qmin, qmax, nbins){ |
---|
94 | this->width = width; |
---|
95 | }; |
---|
96 | |
---|
97 | /** |
---|
98 | * Constructor for QSmearer |
---|
99 | * |
---|
100 | * @param width: array slit widths for each Q point, in Q units |
---|
101 | * @param q: array of Q values |
---|
102 | * @param nbins: number of Q bins |
---|
103 | */ |
---|
104 | QSmearer :: QSmearer(double* width, double* q, int nbins) : |
---|
105 | BaseSmearer(q, nbins){ |
---|
106 | this->width = width; |
---|
107 | }; |
---|
108 | |
---|
109 | /** |
---|
110 | * Compute the slit smearing matrix |
---|
111 | * |
---|
112 | * For even binning (q_min to q_max with nbins): |
---|
113 | * |
---|
114 | * step = (q_max-q_min)/(nbins-1) |
---|
115 | * first bin goes from q_min to q_min+step |
---|
116 | * last bin goes from q_max to q_max+step |
---|
117 | * |
---|
118 | * For binning according to q array: |
---|
119 | * |
---|
120 | * Each q point represents a bin going from half the distance between it |
---|
121 | * and the previous point to half the distance between it and the next point. |
---|
122 | * |
---|
123 | * Example: bin i goes from (q_values[i-1]+q_values[i])/2 to (q_values[i]+q_values[i+1])/2 |
---|
124 | * |
---|
125 | * The exceptions are the first and last bins, which are centered at the first and |
---|
126 | * last q-values, respectively. The width of the first and last bins is the distance between |
---|
127 | * their respective neighboring q-value. |
---|
128 | */ |
---|
129 | void SlitSmearer :: compute_matrix(){ |
---|
130 | |
---|
131 | weights = new vector<double>(nbins*nbins,0); |
---|
132 | |
---|
133 | // Check the length of the data |
---|
134 | if (nbins<2) return; |
---|
135 | int npts_h = height>0.0 ? npts : 1; |
---|
136 | int npts_w = width>0.0 ? npts : 1; |
---|
137 | |
---|
138 | // If both height and width are great than zero, |
---|
139 | // modify the number of points in each direction so |
---|
140 | // that the total number of points is still what |
---|
141 | // the user would expect (downgrade resolution) |
---|
142 | //if(npts_h>1 && npts_w>1){ |
---|
143 | // npts_h = (int)ceil(sqrt((double)npts)); |
---|
144 | // npts_w = npts_h; |
---|
145 | //} |
---|
146 | double shift_h, shift_w, hbin_size, wbin_size; |
---|
147 | // Make sure height and width are all positive (FWMH/2) |
---|
148 | // Assumption; height and width are all same for all q points |
---|
149 | if(npts_h == 1){ |
---|
150 | shift_h = 0.0; |
---|
151 | } else { |
---|
152 | shift_h = fabs(height); |
---|
153 | } |
---|
154 | if(npts_w == 1){ |
---|
155 | shift_w = 0.0; |
---|
156 | } else { |
---|
157 | shift_w = fabs(width); |
---|
158 | } |
---|
159 | // size of the h bin and w bin |
---|
160 | hbin_size = shift_h / nbins; |
---|
161 | wbin_size = shift_w / nbins; |
---|
162 | |
---|
163 | // Loop over all q-values |
---|
164 | for(int i=0; i<nbins; i++) { |
---|
165 | // Find Weights |
---|
166 | // Find q where the resolution smearing calculation of I(q) occurs |
---|
167 | double q, q_min, q_max, q_0; |
---|
168 | get_bin_range(i, &q, &q_min, &q_max); |
---|
169 | // Block q becomes <=0 |
---|
170 | if (q <= 0){ |
---|
171 | continue; |
---|
172 | } |
---|
173 | bool last_qpoint = true; |
---|
174 | // Find q[0] value to normalize the weight later, |
---|
175 | // otherwise, we will have a precision problem. |
---|
176 | if (i == 0){ |
---|
177 | q_0 = q; |
---|
178 | } |
---|
179 | // Loop over all qj-values |
---|
180 | bool first_w = true; |
---|
181 | for(int j=0; j<nbins; j++) { |
---|
182 | double q_j, q_high, q_low; |
---|
183 | // Calculate bin size of q_j |
---|
184 | get_bin_range(j, &q_j, &q_low, &q_high); |
---|
185 | // Block q_j becomes <=0 |
---|
186 | if (q_j <= 0){ |
---|
187 | continue; |
---|
188 | } |
---|
189 | // Check q_low that can not be negative. |
---|
190 | if (q_low < 0.0){ |
---|
191 | q_low = 0.0; |
---|
192 | } |
---|
193 | // default parameter values |
---|
194 | (*weights)[i*nbins+j] = 0.0; |
---|
195 | // protect for negative q |
---|
196 | if (q <= 0.0 || q_j <= 0.0){ |
---|
197 | continue; |
---|
198 | } |
---|
199 | double shift_w = 0.0; |
---|
200 | // Condition: zero slit smear. |
---|
201 | if (npts_w == 1 && npts_h == 1){ |
---|
202 | if(q_j == q) { |
---|
203 | (*weights)[i*nbins+j] = 1.0; |
---|
204 | } |
---|
205 | } |
---|
206 | //Condition:Smear weight integration for width >0 when the height (=0) does not present. |
---|
207 | //Or height << width. |
---|
208 | else if((npts_w!=1 && npts_h == 1)|| (npts_w!=1 && npts_h != 1 && width/height > 100.0)){ |
---|
209 | shift_w = width; |
---|
210 | //del_w = width/((double)npts_w-1.0); |
---|
211 | double q_shifted_low = q - shift_w; |
---|
212 | // High limit of the resolution range |
---|
213 | double q_shifted_high = q + shift_w; |
---|
214 | // Go through all the q_js for weighting those points |
---|
215 | if(q_j >= q_shifted_low && q_j <= q_shifted_high) { |
---|
216 | // The weighting factor comes, |
---|
217 | // Give some weight (delq_bin) for the q_j within the resolution range |
---|
218 | // Weight should be same for all qs except |
---|
219 | // for the q bin size at j. |
---|
220 | // Note that the division by q_0 is only due to the precision problem |
---|
221 | // where q_high - q_low gets to very small. |
---|
222 | // Later, it will be normalized again. |
---|
223 | (*weights)[i*nbins+j] += (q_high - q_low)/q_0 ; |
---|
224 | } |
---|
225 | } |
---|
226 | else{ |
---|
227 | // Loop for width (;Height is analytical.) |
---|
228 | // Condition: height >>> width, otherwise, below is not accurate enough. |
---|
229 | // Smear weight numerical iteration for width >0 when the height (>0) presents. |
---|
230 | // When width = 0, the numerical iteration will be skipped. |
---|
231 | // The resolution calculation for the height is done by direct integration, |
---|
232 | // assuming the I(q'=sqrt(q_j^2-(q+shift_w)^2)) is constant within a q' bin, [q_high, q_low]. |
---|
233 | // In general, this weight numerical iteration for width >0 might be a rough approximation, |
---|
234 | // but it must be good enough when height >>> width. |
---|
235 | for(int k=(-npts_w + 1); k<npts_w; k++){ |
---|
236 | if(npts_w!=1){ |
---|
237 | shift_w = width/((double)npts_w-1.0)*(double)k; |
---|
238 | } |
---|
239 | // For each q-value, compute the weight of each other q-bin |
---|
240 | // in the I(q) array |
---|
241 | // Low limit of the resolution range |
---|
242 | double q_shift = q + shift_w; |
---|
243 | if (q_shift < 0.0){ |
---|
244 | q_shift = 0.0; |
---|
245 | } |
---|
246 | double q_shifted_low = q_shift; |
---|
247 | // High limit of the resolution range |
---|
248 | double q_shifted_high = sqrt(q_shift * q_shift + shift_h * shift_h); |
---|
249 | |
---|
250 | |
---|
251 | // Go through all the q_js for weighting those points |
---|
252 | if(q_j >= q_shifted_low && q_j <= q_shifted_high) { |
---|
253 | // The weighting factor comes, |
---|
254 | // Give some weight (delq_bin) for the q_j within the resolution range |
---|
255 | // Weight should be same for all qs except |
---|
256 | // for the q bin size at j. |
---|
257 | // Note that the division by q_0 is only due to the precision problem |
---|
258 | // where q_high - q_low gets to very small. |
---|
259 | // Later, it will be normalized again. |
---|
260 | |
---|
261 | double q_shift_min = q - width; |
---|
262 | |
---|
263 | double u = (q_j * q_j - (q_shift) * (q_shift)); |
---|
264 | // The fabs below are not necessary but in case: the weight should never be imaginary. |
---|
265 | // At the edge of each sub_width. weight += u(at q_high bin) - u(0), where u(0) = 0, |
---|
266 | // and weighted by (2.0* npts_w -1.0)once for each q. |
---|
267 | if (q == q_j) { |
---|
268 | if (k==0) |
---|
269 | (*weights)[i*nbins+j] += (sqrt(fabs((q_high)*(q_high)-q_shift * q_shift)))/q_0 * (2.0*double(npts_w)-1.0); |
---|
270 | } |
---|
271 | // For the rest of sub_width. weight += u(at q_high bin) - u(at q_low bin) |
---|
272 | else if (u > 0.0){ |
---|
273 | (*weights)[i*nbins+j] += (sqrt(fabs((q_high)*(q_high)- q_shift * q_shift))-sqrt(fabs((q_low)*(q_low)- q_shift * q_shift)))/q_0 ; |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | }; |
---|
281 | |
---|
282 | /** |
---|
283 | * Compute the point smearing matrix |
---|
284 | */ |
---|
285 | void QSmearer :: compute_matrix(){ |
---|
286 | weights = new vector<double>(nbins*nbins,0); |
---|
287 | |
---|
288 | // Loop over all q-values |
---|
289 | double step = (qmax-qmin)/((double)nbins-1.0); |
---|
290 | double q, q_min, q_max; |
---|
291 | double q_j, q_jmax, q_jmin; |
---|
292 | for(int i=0; i<nbins; i++) { |
---|
293 | get_bin_range(i, &q, &q_min, &q_max); |
---|
294 | |
---|
295 | for(int j=0; j<nbins; j++) { |
---|
296 | get_bin_range(j, &q_j, &q_jmin, &q_jmax); |
---|
297 | |
---|
298 | // Compute the fraction of the Gaussian contributing |
---|
299 | // to the q_j bin between q_jmin and q_jmax |
---|
300 | double value = erf( (q_jmax-q)/(sqrt(2.0)*width[i]) ); |
---|
301 | value -= erf( (q_jmin-q)/(sqrt(2.0)*width[i]) ); |
---|
302 | (*weights)[i*nbins+j] += value; |
---|
303 | } |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | * Computes the Q range of a given bin of the Q distribution. |
---|
309 | * The range is computed according the the data distribution that |
---|
310 | * was given to the object at initialization. |
---|
311 | * |
---|
312 | * @param i: number of the bin in the distribution |
---|
313 | * @param q: q-value of bin i |
---|
314 | * @param q_min: lower bound of the bin |
---|
315 | * @param q_max: higher bound of the bin |
---|
316 | * |
---|
317 | */ |
---|
318 | int BaseSmearer :: get_bin_range(int i, double* q, double* q_min, double* q_max) { |
---|
319 | if (even_binning) { |
---|
320 | double step = (qmax-qmin)/((double)nbins-1.0); |
---|
321 | *q = qmin + (double)i*step; |
---|
322 | *q_min = *q - 0.5*step; |
---|
323 | *q_max = *q + 0.5*step; |
---|
324 | return 1; |
---|
325 | } else if (i>=0 && i<nbins) { |
---|
326 | *q = q_values[i]; |
---|
327 | if (i==0) { |
---|
328 | double step = (q_values[1]-q_values[0])/2.0; |
---|
329 | *q_min = *q - step; |
---|
330 | *q_max = *q + step; |
---|
331 | } else if (i==nbins-1) { |
---|
332 | double step = (q_values[i]-q_values[i-1])/2.0; |
---|
333 | *q_min = *q - step; |
---|
334 | *q_max = *q + step; |
---|
335 | } else { |
---|
336 | *q_min = *q - (q_values[i]-q_values[i-1])/2.0; |
---|
337 | *q_max = *q + (q_values[i+1]-q_values[i])/2.0; |
---|
338 | } |
---|
339 | return 1; |
---|
340 | } |
---|
341 | return -1; |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * Perform smearing by applying the smearing matrix to the input Q array |
---|
346 | */ |
---|
347 | void BaseSmearer :: smear(double *iq_in, double *iq_out, int first_bin, int last_bin){ |
---|
348 | |
---|
349 | // If we haven't computed the smearing matrix, do it now |
---|
350 | if(!has_matrix) { |
---|
351 | compute_matrix(); |
---|
352 | has_matrix = true; |
---|
353 | } |
---|
354 | |
---|
355 | // Loop over q-values and multiply apply matrix |
---|
356 | for(int q_i=first_bin; q_i<=last_bin; q_i++){ |
---|
357 | double sum = 0.0; |
---|
358 | double counts = 0.0; |
---|
359 | |
---|
360 | for(int i=first_bin; i<=last_bin; i++){ |
---|
361 | // Skip if weight is less than 1e-03(this value is much smaller than |
---|
362 | // the weight at the 3*sigma distance |
---|
363 | // Will speed up a little bit... |
---|
364 | if ((*weights)[q_i*nbins+i] < 1.0e-003){ |
---|
365 | continue; |
---|
366 | } |
---|
367 | sum += iq_in[i] * (*weights)[q_i*nbins+i]; |
---|
368 | counts += (*weights)[q_i*nbins+i]; |
---|
369 | } |
---|
370 | |
---|
371 | // Normalize counts |
---|
372 | iq_out[q_i] = (counts>0.0) ? sum/counts : 0.0; |
---|
373 | } |
---|
374 | } |
---|