|
43 | 43 | xSpec,ySpec = np.loadtxt(userInput.__filePathSpec, delimiter=userInput.__specDataDelimiter, unpack=True, dtype='float'); |
44 | 44 | xIRF,yIRF = np.loadtxt(userInput.__filePathIRF, delimiter=userInput.__irfDataDelimiter, unpack=True, dtype='float'); |
45 | 45 |
|
| 46 | +yIRFOrigin = yIRF; |
| 47 | + |
46 | 48 | print("shifting x to x = 0...") |
47 | 49 |
|
48 | 50 | #shift that: [0] = 0: |
|
78 | 80 |
|
79 | 81 | stddevIRF = 1.0 |
80 | 82 |
|
81 | | -for i in range(0, len(xVal)-1): |
| 83 | +for i in range(0, len(xVal)): |
82 | 84 | if yIRF[i] > yMaxIRF*0.5: |
83 | 85 | stddevIRF = np.abs((xWhereYMaxIRF-xIRF[i]))/(2*np.sqrt(2*np.log(2))); |
84 | 86 | break; |
85 | 87 |
|
86 | | -estimatedBkgrd = 0; |
| 88 | +estimatedBkgrd = 0; |
| 89 | +estimatedBkgrdIRF = 0; |
87 | 90 | for i in range(userInput.__bkgrd_startIndex, userInput.__bkgrd_startIndex + userInput.__bkgrd_count): |
88 | | - estimatedBkgrd += ySpec[i]; |
| 91 | + estimatedBkgrd += ySpec[i]; |
| 92 | + estimatedBkgrdIRF += yIRF[i]; |
89 | 93 |
|
90 | | -estimatedBkgrd /= userInput.__bkgrd_count; |
| 94 | +estimatedBkgrd /= userInput.__bkgrd_count; |
| 95 | +estimatedBkgrdIRF /= userInput.__bkgrd_count; |
91 | 96 |
|
92 | 97 | #fit the IRF model function on data (xIRF, yIRF): |
93 | 98 | if userInput.__bUsingModel: |
|
99 | 104 | fitModelIRF.set_param_hint('ampl', min=0.0); |
100 | 105 | fitModelIRF.set_param_hint('y0', min=0.0); |
101 | 106 |
|
102 | | - parameterListIRFFit = fitModelIRF.make_params(x=xVal, ampl=yMaxIRF, sigma=stddevIRF, y0=0, x0=xWhereYMaxIRF, args=yIRF); |
| 107 | + parameterListIRFFit = fitModelIRF.make_params(x=xVal, ampl=yMaxIRF, sigma=stddevIRF, y0=estimatedBkgrdIRF, x0=xWhereYMaxIRF, args=yIRF); |
103 | 108 | #change here if you want to fix x0 and/or y0: |
104 | 109 | parameterListIRFFit['x0'].vary = True; |
105 | 110 | parameterListIRFFit['y0'].vary = True; |
|
157 | 162 | fitModelIRF.set_param_hint('ampl', min=0.0); |
158 | 163 | fitModelIRF.set_param_hint('y0', min=0.0); |
159 | 164 |
|
160 | | - parameterListIRFFit = fitModelIRF.make_params(x=xVal, ampl=yMaxIRF, s=stddevIRF, y0=0, x0=xWhereYMaxIRF, args=yIRF); |
| 165 | + parameterListIRFFit = fitModelIRF.make_params(x=xVal, ampl=yMaxIRF, s=stddevIRF, y0=estimatedBkgrdIRF, x0=xWhereYMaxIRF, args=yIRF); |
161 | 166 | #change here if you want to fix x0 and/or y0: |
162 | 167 | parameterListIRFFit['x0'].vary = True; |
163 | 168 | parameterListIRFFit['y0'].vary = True; |
|
216 | 221 | fitModelIRF.set_param_hint('ampl', min=0.0); |
217 | 222 | fitModelIRF.set_param_hint('y0', min=0.0); |
218 | 223 |
|
219 | | - parameterListIRFFit = fitModelIRF.make_params(x=xVal, ampl=yMaxIRF, a=1, sigma=stddevIRF, s=stddevIRF, y0=0, x0=xWhereYMaxIRF, args=yIRF); |
| 224 | + parameterListIRFFit = fitModelIRF.make_params(x=xVal, ampl=yMaxIRF, a=0.8, sigma=stddevIRF, s=stddevIRF*0.5, y0=estimatedBkgrdIRF, x0=xWhereYMaxIRF, args=yIRF); |
220 | 225 | #change here if you want to fix x0 and/or y0: |
221 | 226 | parameterListIRFFit['x0'].vary = True; |
222 | 227 | parameterListIRFFit['y0'].vary = True; |
|
227 | 232 | chiSquare = resultsOfModelIRF.chisqr; |
228 | 233 | redChiSquare = resultsOfModelIRF.redchi; |
229 | 234 |
|
230 | | - fract = (float)(resultsOfModelIRF.params['a'].value*userInput); |
231 | | - fract_err = (float)(resultsOfModelIRF.params['a'].stderr*userInput); |
| 235 | + fract = (float)(resultsOfModelIRF.params['a'].value); |
| 236 | + fract_err = (float)(resultsOfModelIRF.params['a'].stderr); |
232 | 237 |
|
233 | 238 | sigma = (float)(resultsOfModelIRF.params['sigma'].value*userInput.__channelResolutionInPs); |
234 | 239 | sigma_err = (float)(resultsOfModelIRF.params['sigma'].stderr*userInput.__channelResolutionInPs); |
|
251 | 256 | print("background = {0} ({1})".format(yRes, yRes_err)); |
252 | 257 | print(""); |
253 | 258 | print("---------------------------------------------------------------"); |
254 | | - print("G - Gaussian: a = {0} ({})".format(fract, fract_err)); |
| 259 | + print("G - Gaussian: a = {0} ({1})".format(fract, fract_err)); |
255 | 260 | print("---------------------------------------------------------------"); |
256 | 261 | print("stddev [ps] = {0} ({1})".format(sigma, sigma_err)); |
257 | 262 | print("FWHM [ps] = {0} ({1})".format(sigma*(2*np.sqrt(2*np.log(2))), sigma_err*(2*np.sqrt(2*np.log(2))))); |
258 | 263 | print(""); |
259 | 264 | print("---------------------------------------------------------------"); |
260 | | - print("L - Lorentzian: (1 - a) = {0} ({})".format(1-fract, fract_err)); |
| 265 | + print("L - Lorentzian: (1 - a) = {0} ({1})".format(1-fract, fract_err)); |
261 | 266 | print("---------------------------------------------------------------"); |
262 | 267 | print("s [ps] = {0} ({1})".format(s, s_err)); |
263 | 268 | print("---------------------------------------------------------------"); |
|
288 | 293 | fitModelIRF.set_param_hint('ampl', min=0.0); |
289 | 294 | fitModelIRF.set_param_hint('y0', min=0.0); |
290 | 295 |
|
291 | | - parameterListIRFFit = fitModelIRF.make_params(x=xVal, ampl=yMaxIRF, alpha=stddevIRF, m=2, y0=0, x0=xWhereYMaxIRF, args=yIRF); |
| 296 | + parameterListIRFFit = fitModelIRF.make_params(x=xVal, ampl=yMaxIRF, alpha=stddevIRF, m=2, y0=estimatedBkgrdIRF, x0=xWhereYMaxIRF, args=yIRF); |
292 | 297 | #change here if you want to fix x0 and/or y0: |
293 | 298 | parameterListIRFFit['x0'].vary = True; |
294 | 299 | parameterListIRFFit['y0'].vary = True; |
|
302 | 307 | alpha = (float)(resultsOfModelIRF.params['alpha'].value*userInput.__channelResolutionInPs); |
303 | 308 | alpha_err = (float)(resultsOfModelIRF.params['alpha'].stderr*userInput.__channelResolutionInPs); |
304 | 309 |
|
305 | | - m = (float)(resultsOfModelIRF.params['m'].value*userInput); |
306 | | - m_err = (float)(resultsOfModelIRF.params['m'].stderr*userInput); |
| 310 | + m = (float)(resultsOfModelIRF.params['m'].value); |
| 311 | + m_err = (float)(resultsOfModelIRF.params['m'].stderr); |
307 | 312 |
|
308 | 313 | amplitude = (float)(resultsOfModelIRF.params['ampl'].value); |
309 | 314 | amplitude_err = (float)(resultsOfModelIRF.params['ampl'].stderr); |
@@ -483,7 +488,6 @@ def ExpDecay_4(x, ampl1, tau1, ampl2, tau2, ampl3, tau3, ampl4, tau4, y0, x0, ar |
483 | 488 | ax2 = plt.subplot(2,1,2); |
484 | 489 | ax2.set_title("Best fit: Residuals"); |
485 | 490 | plt.plot(xVal, resultsOfModelDecay.residual); |
486 | | - plt.show(); |
487 | 491 |
|
488 | 492 | if userInput.__numberOfExpDec == 2: |
489 | 493 | print("\nrunning reconvolution with 2 component...\n") |
@@ -568,7 +572,6 @@ def ExpDecay_4(x, ampl1, tau1, ampl2, tau2, ampl3, tau3, ampl4, tau4, y0, x0, ar |
568 | 572 | ax2 = plt.subplot(2,1,2); |
569 | 573 | ax2.set_title("Best fit: Residuals"); |
570 | 574 | plt.plot(xVal, resultsOfModelDecay.residual); |
571 | | - plt.show(); |
572 | 575 |
|
573 | 576 | if userInput.__numberOfExpDec == 3: |
574 | 577 | print("\nrunning reconvolution with 3 component...\n") |
@@ -667,7 +670,6 @@ def ExpDecay_4(x, ampl1, tau1, ampl2, tau2, ampl3, tau3, ampl4, tau4, y0, x0, ar |
667 | 670 | ax2 = plt.subplot(2,1,2); |
668 | 671 | ax2.set_title("Best fit: Residuals"); |
669 | 672 | plt.plot(xVal, resultsOfModelDecay.residual); |
670 | | - plt.show(); |
671 | 673 |
|
672 | 674 | if userInput.__numberOfExpDec == 4: |
673 | 675 | print("\nrunning reconvolution with 4 component...\n") |
@@ -787,5 +789,31 @@ def ExpDecay_4(x, ampl1, tau1, ampl2, tau2, ampl3, tau3, ampl4, tau4, y0, x0, ar |
787 | 789 | ax2 = plt.subplot(2,1,2); |
788 | 790 | ax2.set_title("Best fit: Residuals"); |
789 | 791 | plt.plot(xVal, resultsOfModelDecay.residual); |
790 | | - plt.show(); |
| 792 | + |
| 793 | +#save data if required: |
| 794 | +if userInput.__saveReconvolutionSpectrum: |
| 795 | + ab = np.zeros(len(xVal), dtype=[('time [ps]', float), ('raw counts [#]', float), ('best fit [#]', float)]); |
| 796 | + ab['time [ps]'] = xVal*userInput.__channelResolutionInPs; |
| 797 | + ab['raw counts [#]'] = ySpec; |
| 798 | + ab['best fit [#]'] = resultsOfModelDecay.best_fit; |
| 799 | + np.savetxt(userInput.__saveReconvolutionSpectrumPath, ab, fmt='%10.3f\t%10.3f\t%10.3f', delimiter='\t', newline='\n', header='time [ps]\traw counts [#]\tbest fit [#]\n'); |
| 800 | + |
| 801 | + abRes = np.zeros(len(xVal), dtype=[('time [ps]', float), ('residuals [conv. level]', float)]); |
| 802 | + abRes['time [ps]'] = xVal*userInput.__channelResolutionInPs; |
| 803 | + abRes['residuals [conv. level]'] = resultsOfModelDecay.residual; |
| 804 | + np.savetxt(userInput.__saveReconvolutionSpectrumResidualPath, abRes, fmt='%10.3f\t%10.3f', delimiter='\t', newline='\n', header='time [ps]\traw counts [#]\tresiduals [conv. level]\n'); |
| 805 | + |
| 806 | +if userInput.__saveReconvolutionIRF and userInput.__bUsingModel: |
| 807 | + ab = np.zeros(len(xVal), dtype=[('time [ps]', float), ('raw counts [#]', float), ('best fit [#]', float)]); |
| 808 | + ab['time [ps]'] = xVal*userInput.__channelResolutionInPs; |
| 809 | + ab['raw counts [#]'] = yIRFOrigin; |
| 810 | + ab['best fit [#]'] = yIRF; |
| 811 | + np.savetxt(userInput.__saveReconvolutionIRFPath, ab, fmt='%10.3f\t%10.3f\t%10.3f', delimiter='\t', newline='\n', header='time [ps]\traw counts [#]\tbest fit [#]\n'); |
| 812 | + |
| 813 | + abRes = np.zeros(len(xVal), dtype=[('time [ps]', float), ('residuals [conv. level]', float)]); |
| 814 | + abRes['time [ps]'] = xVal*userInput.__channelResolutionInPs; |
| 815 | + abRes['residuals [conv. level]'] = resultsOfModelIRF.residual; |
| 816 | + np.savetxt(userInput.__saveReconvolutionIRFResidualPath, abRes, fmt='%10.3f\t%10.3f', delimiter='\t', newline='\n', header='time [ps]\traw counts [#]\tresiduals [conv. level]\n'); |
| 817 | + |
| 818 | +plt.show(); |
791 | 819 |
|
0 commit comments