[a3f8d58] | 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; |
---|
[5859862] | 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; |
---|
[a3f8d58] | 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 |
---|
[5859862] | 62 | * @param nbins: number of Q bins |
---|
[a3f8d58] | 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 | |
---|
[5859862] | 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 | */ |
---|
[a3f8d58] | 92 | QSmearer :: QSmearer(double* width, double qmin, double qmax, int nbins) : |
---|
| 93 | BaseSmearer(qmin, qmax, nbins){ |
---|
| 94 | this->width = width; |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | /** |
---|
[5859862] | 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. |
---|
[a3f8d58] | 128 | */ |
---|
| 129 | void SlitSmearer :: compute_matrix(){ |
---|
| 130 | |
---|
| 131 | weights = new vector<double>(nbins*nbins,0); |
---|
| 132 | |
---|
[5859862] | 133 | // Check the length of the data |
---|
| 134 | if (nbins<2) return; |
---|
| 135 | |
---|
[a3f8d58] | 136 | // Loop over all q-values |
---|
| 137 | for(int i=0; i<nbins; i++) { |
---|
[5859862] | 138 | double q, q_min, q_max; |
---|
| 139 | get_bin_range(i, &q, &q_min, &q_max); |
---|
[a3f8d58] | 140 | |
---|
| 141 | // For each q-value, compute the weight of each other q-bin |
---|
| 142 | // in the I(q) array |
---|
| 143 | int npts_h = height>0 ? npts : 1; |
---|
| 144 | int npts_w = width>0 ? npts : 1; |
---|
| 145 | |
---|
| 146 | // If both height and width are great than zero, |
---|
| 147 | // modify the number of points in each direction so |
---|
| 148 | // that the total number of points is still what |
---|
| 149 | // the user would expect (downgrade resolution) |
---|
| 150 | if(npts_h>1 && npts_w>1){ |
---|
| 151 | npts_h = (int)ceil(sqrt((double)npts)); |
---|
| 152 | npts_w = npts_h; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | double shift_h, shift_w; |
---|
| 156 | for(int k=0; k<npts_h; k++){ |
---|
| 157 | if(npts_h==1){ |
---|
| 158 | shift_h = 0; |
---|
| 159 | } else { |
---|
| 160 | shift_h = height/((double)npts_h-1.0) * (double)k; |
---|
| 161 | } |
---|
| 162 | for(int j=0; j<npts_w; j++){ |
---|
| 163 | if(npts_w==1){ |
---|
| 164 | shift_w = 0; |
---|
| 165 | } else { |
---|
| 166 | shift_w = width/((double)npts_w-1.0) * (double)j; |
---|
| 167 | } |
---|
| 168 | double q_shifted = sqrt( ((q-shift_w)*(q-shift_w) + shift_h*shift_h) ); |
---|
[5859862] | 169 | |
---|
| 170 | // Find in which bin this shifted value belongs |
---|
| 171 | int q_i=nbins; |
---|
| 172 | if (even_binning) { |
---|
| 173 | // This is kept for backward compatibility since the binning |
---|
| 174 | // was originally defined differently for even bins. |
---|
| 175 | q_i = (int)(floor( (q_shifted-qmin) /((qmax-qmin)/((double)nbins -1.0)) )); |
---|
| 176 | } else { |
---|
| 177 | for(int t=0; t<nbins; t++) { |
---|
| 178 | double q_t, q_high, q_low; |
---|
| 179 | get_bin_range(t, &q_t, &q_low, &q_high); |
---|
| 180 | if(q_shifted>=q_low && q_shifted<q_high) { |
---|
| 181 | q_i = t; |
---|
| 182 | break; |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | } |
---|
[a3f8d58] | 186 | |
---|
| 187 | // Skip the entries outside our I(q) range |
---|
| 188 | //TODO: be careful with edge effect |
---|
| 189 | if(q_i<nbins) |
---|
| 190 | (*weights)[i*nbins+q_i]++; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | }; |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * Compute the point smearing matrix |
---|
| 198 | */ |
---|
| 199 | void QSmearer :: compute_matrix(){ |
---|
| 200 | weights = new vector<double>(nbins*nbins,0); |
---|
| 201 | |
---|
| 202 | // Loop over all q-values |
---|
| 203 | double step = (qmax-qmin)/((double)nbins-1.0); |
---|
[5859862] | 204 | double q, q_min, q_max; |
---|
| 205 | double q_j, q_jmax, q_jmin; |
---|
[a3f8d58] | 206 | for(int i=0; i<nbins; i++) { |
---|
[5859862] | 207 | get_bin_range(i, &q, &q_min, &q_max); |
---|
[a3f8d58] | 208 | |
---|
| 209 | for(int j=0; j<nbins; j++) { |
---|
[5859862] | 210 | get_bin_range(j, &q_j, &q_jmin, &q_jmax); |
---|
[a3f8d58] | 211 | |
---|
| 212 | // Compute the fraction of the Gaussian contributing |
---|
| 213 | // to the q bin between q_min and q_max |
---|
| 214 | double value = erf( (q_max-q_j)/(sqrt(2.0)*width[j]) ); |
---|
| 215 | value -= erf( (q_min-q_j)/(sqrt(2.0)*width[j]) ); |
---|
| 216 | (*weights)[i*nbins+j] += value; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | /** |
---|
[5859862] | 222 | * Computes the Q range of a given bin of the Q distribution. |
---|
| 223 | * The range is computed according the the data distribution that |
---|
| 224 | * was given to the object at initialization. |
---|
| 225 | * |
---|
| 226 | * @param i: number of the bin in the distribution |
---|
| 227 | * @param q: q-value of bin i |
---|
| 228 | * @param q_min: lower bound of the bin |
---|
| 229 | * @param q_max: higher bound of the bin |
---|
| 230 | * |
---|
| 231 | */ |
---|
[65883cf] | 232 | int BaseSmearer :: get_bin_range(int i, double* q, double* q_min, double* q_max) { |
---|
[5859862] | 233 | if (even_binning) { |
---|
| 234 | double step = (qmax-qmin)/((double)nbins-1.0); |
---|
| 235 | *q = qmin + (double)i*step; |
---|
| 236 | *q_min = *q - 0.5*step; |
---|
| 237 | *q_max = *q + 0.5*step; |
---|
[65883cf] | 238 | return 1; |
---|
| 239 | } else if (i>=0 && i<nbins) { |
---|
[5859862] | 240 | *q = q_values[i]; |
---|
| 241 | if (i==0) { |
---|
| 242 | double step = (q_values[1]-q_values[0])/2.0; |
---|
| 243 | *q_min = *q - step; |
---|
| 244 | *q_max = *q + step; |
---|
| 245 | } else if (i==nbins-1) { |
---|
| 246 | double step = (q_values[i]-q_values[i-1])/2.0; |
---|
| 247 | *q_min = *q - step; |
---|
| 248 | *q_max = *q + step; |
---|
| 249 | } else { |
---|
| 250 | *q_min = *q - (q_values[i]-q_values[i-1])/2.0; |
---|
| 251 | *q_max = *q + (q_values[i+1]-q_values[i])/2.0; |
---|
| 252 | } |
---|
[65883cf] | 253 | return 1; |
---|
[5859862] | 254 | } |
---|
[65883cf] | 255 | return -1; |
---|
[5859862] | 256 | } |
---|
| 257 | |
---|
| 258 | /** |
---|
[a3f8d58] | 259 | * Perform smearing by applying the smearing matrix to the input Q array |
---|
| 260 | */ |
---|
| 261 | void BaseSmearer :: smear(double *iq_in, double *iq_out, int first_bin, int last_bin){ |
---|
| 262 | |
---|
| 263 | // If we haven't computed the smearing matrix, do it now |
---|
| 264 | if(!has_matrix) { |
---|
| 265 | compute_matrix(); |
---|
| 266 | has_matrix = true; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | // Loop over q-values and multiply apply matrix |
---|
| 270 | for(int q_i=first_bin; q_i<=last_bin; q_i++){ |
---|
| 271 | double sum = 0.0; |
---|
| 272 | double counts = 0.0; |
---|
| 273 | |
---|
| 274 | for(int i=first_bin; i<=last_bin; i++){ |
---|
| 275 | sum += iq_in[i] * (*weights)[q_i*nbins+i]; |
---|
| 276 | counts += (*weights)[q_i*nbins+i]; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | // Normalize counts |
---|
| 280 | iq_out[q_i] = (counts>0.0) ? sum/counts : 0; |
---|
| 281 | } |
---|
| 282 | } |
---|