-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis.py
1192 lines (978 loc) · 46.4 KB
/
Analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import Chn
#import pylab as plb
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['axes.linewidth'] = 1.5 #set the value globally
mpl.rcParams.update({'font.size': 15})
mpl.rcParams['axes.labelsize'] = 18
from scipy.optimize import curve_fit
from scipy.integrate import quad
import scipy as sp
import math
from mpl_toolkits.axes_grid1 import make_axes_locatable
import os
from chnsum import chnsum
#import uncertainties as unc
#import uncertainties.unumpy as unp
global E0 # americium energy needed for calibration
global E0Err
global calibIntercept #intercept on channel number versus voltage. beta in our notes.
global calibInterceptErr
global N0
global N0Err
global slope #slope on energy versus channel number
global slopeErr
global intercept #intercept on energy versus channel number
global interceptErr
global TOTAL
global TOTALErr
global M_air
global M_air_e
global R
global R_e
global T
global T_e
slope = 0.00457880799792
slopeErr = 3.35945387177e-06
intercept = 0.0661289090633
interceptErr = 0.00397196078318
#The value and error of M_air are referenced in the .bib file
M_air = 28.964 #in [g] [mol-1]
M_air_e = 0.002 #in [g] [mol-1]
#The value and error of R are referenced in the .bib file
R = 83.14449*1000.0 #in [cm3] [mbar] [K−1] [mol−1]
R_e = 0.00056*1000.0 #in [cm3] [mbar] [K−1] [mol−1]
#T was measured as multiple values between 21.0 and 22.6, so for now take 21.8
T = 21.8+273.16 #in [K]
T_e = 0.1 #in [K]
slope = 0.00457880799792
slopeErr = 3.35945387177e-06
intercept = 0.0661289090633
interceptErr = 0.00397196078318
#E0 = 5.48556
#E0Err = 0.00012
####################################################################################################################
################################################### Fitters ########################################################
####################################################################################################################
def plotStudRes(ax, d, xx , yerr, res_tick, x0=0, left=0):
stu_d = d/yerr
stu_d_err = np.ones(len(d))
divider = make_axes_locatable(ax)
ax2 = divider.append_axes("top", size="20%", pad=0.1)
ax.figure.add_axes(ax2)
ax2.set_xlim(ax.get_xlim())
ax2.set_yticks(res_tick)
ax.tick_params(width=1.3, axis='both', direction='in', bottom=True, top=True, left=True, right=True)
ax2.tick_params(width=1.3, axis='both', direction='in', bottom=True, top=True, left=True, right=True, labelbottom=False)
ax2.set_ylabel('Studentized\nResiduals', color='k', fontsize=12)
from matplotlib.ticker import AutoLocator, AutoMinorLocator
ax2.get_yaxis().set_major_locator(AutoLocator())
# ax2.get_yaxis().set_minor_locator(AutoMinorLocator())
ax2.axhline(y=0, color='r', linestyle='-', linewidth=2)
ax.tick_params(axis='both', direction='in')
ax2.tick_params(axis='both', direction='in')
ax2.errorbar(xx+x0-left, stu_d, yerr=stu_d_err, fmt='o', elinewidth=1.5 ,capsize=2, ecolor='b', \
label='Data', linestyle='None', markersize=3 ,color='k')
def reducedChiSquare(y,fx,yerr, npara):
"""
:param y: y vector
:param fx: vector of f(x), where f is the fitting function
:param m: the number of fitted parameters
:return: Reduced chi^2 of the fit. Ideally should be 1, dof the degree of freedom.
"""
for i in range(len(yerr)):
if yerr[i]==0:
yerr[i] = 1
toReturn, count = 0.0, 0
for i in range(len(y)):
if yerr[i]==0:
continue
else:
toReturn += (y[i]-fx[i])**2/yerr[i]**2
count += 1
dof = count-npara
return toReturn/dof, dof
def linFitXIntercept(x, m, h):
return m*x-m*h
def LinearFit_xIntercept(x, y, yerr):
yerr = np.asarray(yerr)
x = np.asarray(x)
y = np.asarray(y)
popt, pcov = curve_fit(linFitXIntercept, x, y, p0=[0.01, -20], maxfev=50000)
perr = np.sqrt(np.diag(pcov))
return popt, perr
def linearFit(x, y, yerr):
"""
To perform linear fit; x: x data; y: y data; yerr: error on y data;
Output: popt: list of parameter value; perr: list of parameter err
"""
yerr = np.asarray(yerr)
x = np.asarray(x)
y = np.asarray(y)
fit = np.polyfit(x, y, 1, w=1/yerr, cov=True)
popt = fit[0]
pcov = fit[1]
perr = np.sqrt(np.diag(pcov))
return popt, perr
def gauss(x, a, mean, sigma):
return a*np.exp(-(x-mean)**2/(2*sigma**2))
def gaussMul(x, *params, sigmaFixed=True):
if not sigmaFixed:
y = np.zeros_like(x)
for i in range(0, len(params), 3):
a = params[i]
mean = params[i+1]
sigma = params[i+2]
y = y + a*np.exp(-(x-mean)**2/(2*sigma**2))
return y
else:
y = np.zeros_like(x)
for i in range(0, len(params)-1, 2):
a = params[i]
mean = params[i+1]
sigma = params[-1]
y = y + a*np.exp(-(x-mean)**2/(2*sigma**2))
return y
def expGauss(x, A, l, s, m):
return A*l/2*np.exp(l/2*(2*x-2*m+l*s*s))*(1-sp.special.erf((x+l*s*s-m)/(math.sqrt(2)*s)))
def expGaussMul(x, *params):
y = np.zeros_like(x)
for i in range(0, len(params), 4):
A = params[i]
l = params[i+1]
s = params[i+2]
m = params[i+3]
y = y + A*l/2*np.exp(l/2*(2*x-2*m+l*s*s))*(1-sp.special.erf((x+l*s*s-m)/(math.sqrt(2)*s)))
return y
def expGaussFitMul(filePathtobeSaved, x, y, yerr, p0, x0, left, res_tick=[-3,0,3]):
fig = plt.figure(figsize=(8, 6))
popt, pcov = curve_fit(expGaussMul, x, y, p0=p0, maxfev=5000000)
npara = len(p0)
rchi, dof = reducedChiSquare(y, expGaussMul(x, *popt), yerr, npara)
xx, xx_e = convertChannelToEnergy(x)
x0, x0_e = convertChannelToEnergy(x0)
left, l_e = convertChannelToEnergy(left)
# plt.errorbar(xx+x0-left, y, yerr=yerr, xerr=xx_e+x0_e-l_e, fmt='o', elinewidth=1.5 ,capsize=3, ecolor='b', \
# label='Data', linestyle='None', markersize=4 ,color='k')
# plt.errorbar(xx+x0-left, y, fmt='o', elinewidth=1.5 ,capsize=3, ecolor='b', \
# label='Data', linestyle='None', markersize=3 ,color='k')
xxx = np.linspace(min(x),max(x),1000)
xxxx = np.linspace(min(xx+x0-left), max(xx+x0-left), 1000)
# plt.plot(xxxx, expGaussMul(xxx, *popt), '-r', label='Fit', linewidth=2)
plt.legend(loc=2)
plt.xlabel('Energy (MeV)')
plt.ylabel(r'$^{212}$Po Decay to $^{208}$Pb'+'\nChannel Counts')
# plt.ylim((-100,3400))
perr = np.sqrt(np.diag(pcov))
for i in range(0,len(popt),4):
print('A %d: %f $\pm$ %f'%(i/4+1, popt[i], perr[i]))
print('Lambda %d: %f $\pm$ %f'%(i/4+1, popt[i+1], perr[i+1]))
print('Sigma %d: %f $\pm$ %f'%(i/4+1, popt[i+2], perr[i+2]))
print('Mean %d (Scaled): %f $\pm$ %f\n'%(i/4+1, popt[i+3], perr[i+3]))
print('RChi: %f'%(rchi))
print('DOF: %d'%(dof))
for i in range(int(len(popt)/4)):
temp = popt[i*4:(i+1)*4]
temp_x = np.linspace(min(xx+x0-left),max(xx+x0-left),2000)
temp_fit = np.linspace(min(x),max(x),2000)
plt.plot(temp_x, expGauss(temp_fit, *temp), label='#%d'%(i+1), linewidth=3)
plt.legend(loc=2)
# Plot residuals
# d = y-expGaussMul(x,*popt)
# axes = plt.gca()
# plotStudRes(axes, d, xx, yerr, res_tick=res_tick, x0=x0, left=left)
# ax = fig.add_subplot(111)
# ax.annotate('#1', xy=(convertChannelToEnergy(popt[3]-2, noErr=True), 200), xytext=(convertChannelToEnergy(popt[3]-2, noErr=True), 200+400),\
# arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),horizontalalignment='center')
# ax.annotate('#2', xy=(convertChannelToEnergy(popt[7]-2, noErr=True), 200), xytext=(convertChannelToEnergy(popt[7]-2, noErr=True), 200+400),\
# arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),horizontalalignment='center')
# ax.annotate('#3', xy=(convertChannelToEnergy(popt[11]-2, noErr=True), 2700), xytext=(convertChannelToEnergy(popt[11]-2, noErr=True), 2700+400),\
# arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),horizontalalignment='center')
# ax.annotate('#4', xy=(convertChannelToEnergy(popt[15]-1, noErr=True), 1200), xytext=(convertChannelToEnergy(popt[15]-1, noErr=True), 1200+400),\
# arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),horizontalalignment='center')
plt.show()
fig.savefig(filePathtobeSaved+'.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
return popt, perr, rchi, dof # return the mean channel values
def expGaussFit(filePathtobeSaved, x, y, yerr, p0, x0, left, res_tick=[-3,0,3], xerr=0):
fig = plt.figure(figsize=(8, 6))
popt, pcov = curve_fit(expGauss, x, y, p0=p0, maxfev=50000)
npara = len(p0)
rchi, dof = reducedChiSquare(y, expGauss(x, *popt), yerr, npara)
plt.errorbar(x+x0-left, y, yerr=yerr, xerr=xerr,fmt='o', elinewidth=1.5 ,capsize=3, ecolor='b', \
label='Data', linestyle='None', markersize=3 ,color='k')
plt.plot(x+x0-left, expGauss(x, *popt), '-r', label='Fit')
plt.xlabel('Energy (MeV)')
plt.ylabel('Counts')
perr = np.sqrt(np.diag(pcov))
print('\nMean 1 (Scaled): %f $\pm$ %f'%(popt[3], perr[3]))
print('Sigma 1: %f $\pm$ %f'%(popt[2], perr[2]))
print('Lambda 1: %f $\pm$ %f'%(popt[1], perr[1]))
print('A 1: %f $\pm$ %f'%(popt[0], perr[0]))
print('RChi: %f'%(rchi))
print('DOF: %d'%(dof))
# Plot residuals
d = y-expGauss(x,*popt)
axes = plt.gca()
plotStudRes(axes, d, x, yerr, res_tick=res_tick, x0=x0, left=left)
plt.show()
fig.savefig(filePathtobeSaved+'.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
return popt, perr, rchi, dof # return the mean channel values
def gaussianFit(filePathtobeSaved, x, y, yerr, p0=[300, 20, 2.5], left=15, right=15, res_tick=[-3,0,3]):
fig = plt.figure(figsize=(8,6))
ind = np.argmax(y) #to get the peak value x-coord
x0 = x[ind] #x0 is the peak value x-coord (channel number)
yy = y[x0-left:x0+right]
xx = np.arange(len(yy))
yerr = yerr[x0-left:x0+right]
popt, pcov = curve_fit(gauss, xx, yy, p0=p0, maxfev=50000) #initial guess of the amplitude is 100, mean is x0 and variance (sigma) 5
npara = len(p0)
rchi, dof = reducedChiSquare(yy, gauss(xx, *popt), yerr, npara)
perr = np.sqrt(np.diag(pcov))
plt.errorbar(xx+x0-left, yy, yerr=yerr, fmt='o', elinewidth=1.5 ,capsize=3, ecolor='b', \
label='Data', linestyle='None', markersize=3 ,color='k')
# fmt='+', elinewidth=1 ,capsize=2, ecolor='b', \
# label='Data', linestyle='None', markersize=4,color='b'
xxx = np.linspace(min(xx),max(xx),1000)
plt.plot(xxx+x0-left, gauss(xxx, *popt), 'r-', label='Fit', linewidth=2)
plt.xlabel('MCA Channel Number')
plt.ylabel('Pulse Channel Counts')
# Plot residuals
d = yy-gauss(xx,*popt)
axes = plt.gca()
plotStudRes(axes, d, xx, yerr, res_tick=res_tick, x0=x0, left=left)
popt[1] = popt[1]+x0-left
print('A: %f $\pm$ %f'%(popt[0], perr[0]))
print('Mean: %f\pm%f'%(popt[1], perr[1]))
print('Sigma: %f $\pm$ %f'%(popt[2], perr[2]))
print('RChi: %f'%(rchi))
print('DOF: %d'%(dof))
plt.show()
fig.savefig(filePathtobeSaved+'.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
return popt, perr, rchi
def gaussianFitMul(filePathtobeSaved, x, y, yerr, p0, left=15, right=15, res_tick=[-3,0,3], sigmaFixed=True):
fig = plt.figure(figsize=(8, 6))
ind = np.argmax(y) #to get the peak value x-coord
x0 = x[ind] #x0 is the peak value x-coord (channel number)
yy = y[x0-left:x0+right]
xx = np.arange(len(yy))
yerr = yerr[x0-left:x0+right]
for i in range(len(yerr)):
if yerr[i]==0:
yerr[i] = 1
popt, pcov = curve_fit(gaussMul, xx, yy, p0=p0, maxfev=500000) #initial guess of the amplitude is 100, mean is x0 and variance (sigma) 5
npara = len(p0)
rchi, dof = reducedChiSquare(yy, gaussMul(xx, *popt), yerr, npara)
perr = np.sqrt(np.diag(pcov))
plt.errorbar(xx+x0-left, yy, yerr=yerr,fmt='o', elinewidth=1.5 ,capsize=3, ecolor='b', \
label='Data', linestyle='None', markersize=3 ,color='k')
xxx = np.linspace(min(xx),max(xx),1000)
plt.plot(xxx+x0-left, gaussMul(xxx, *popt), 'r-', label='Fit', linewidth=2)
#plt.legend()
plt.xlabel('MCA Channel Number')
plt.ylabel('Alpha Decay Channel Counts')
# Plot residuals
d = yy-gaussMul(xx,*popt)
axes = plt.gca()
plotStudRes(axes, d, xx, yerr, res_tick=res_tick, x0=x0, left=left)
if not sigmaFixed:
a1, a1_e = popt[0], perr[0]
m1, m1_e = popt[1], perr[1]
s1, s1_e = popt[2], perr[2]
a2, a2_e = popt[3], perr[3]
m2, m2_e = popt[4], perr[4]
s2, s2_e = popt[5], perr[5]
a3, a3_e = popt[6], perr[6]
m3, m3_e = popt[7], perr[7]
s3, s3_e = popt[8], perr[8]
else:
a1, a1_e = popt[0], perr[0]
m1, m1_e = popt[1], perr[1]
s1, s1_e = popt[-1], perr[-1]
a2, a2_e = popt[2], perr[2]
m2, m2_e = popt[3], perr[3]
s2, s2_e = popt[-1], perr[-1]
a3, a3_e = popt[4], perr[4]
m3, m3_e = popt[5], perr[5]
s3, s3_e = popt[-1], perr[-1]
print('A 1: %f $\pm$ %f'%(a1, a1_e))
print('Mean 1: %f\pm%f'%(m1, m1_e))
print('Sigma 1: %f $\pm$ %f'%(s1, s1_e))
print('A 2: %f $\pm$ %f'%(a2, a2_e))
print('Mean 2: %f\pm%f'%(m2, m2_e))
print('Sigma 2: %f $\pm$ %f'%(s2, s2_e))
print('A 3: %f $\pm$ %f'%(a3, a3_e))
print('Mean 3: %f\pm%f'%(m3, m3_e))
print('Sigma 3: %f $\pm$ %f'%(s3, s3_e))
print('RChi: %f'%(rchi))
print('DOF: %d'%(dof))
ax = fig.add_subplot(111)
ax.annotate('Peak 1', xy=(m1+x0-left, a1+20), xytext=(m1+x0-left, a1+100),\
arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),horizontalalignment='center')
ax.annotate('Peak 2', xy=(m2+x0-left, a2+20), xytext=(m2+x0-left, a2+100),\
arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),horizontalalignment='center')
ax.annotate('Peak 3', xy=(m3+x0-left-3, a3), xytext=(m3+x0-left-17, a3),\
arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),horizontalalignment='center')
plt.show()
fig.savefig(filePathtobeSaved+'.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
return popt, perr, rchi, dof
def fitAlphaPeaks(filePathtobeSaved, filePath, p0, left=100, right=100, res_tick=[-3,0,3]):
"""
This is the function to fit Alpha Peak
filepath: full path to the file; p0: list of initial guess; left: how much away
to the left from peak channel; right: how much away to the right from peak channel
res_tick: the residual plot y axis ticks
"""
ch = Chn.Chn(filePath)
y = ch.spectrum
#print('Real time: %d'%ch.real_time)
x = np.arange(len(y))
ind = np.argmax(y)
x0 = x[ind]
yy = y[x0-left:x0+right]
xx = np.arange(len(yy))
yerr = np.sqrt(yy)
popt, perr, rchi, dof = expGaussFitMul(filePathtobeSaved, xx, yy, yerr, p0, x0, left, res_tick)
popt[3] += x0-left
popt[7] += x0-left
print('Mean 1 (Not Scaled): %f \pm %f'%(popt[3], perr[3]))
print('Mean 2 (Not Scaled): %f \pm %f'%(popt[7], perr[7])+'\n')
return popt, perr, rchi, dof
def fitAlphaPeak(filePathtobeSaved, filePath, p0, left=100, right=100, res_tick=[-3,0,3], filenm=None):
"""
This is the function to fit Alpha Peak
filepath: full path to the file; p0: list of initial guess; left: how much away
to the left from peak channel; right: how much away to the right from peak channel
res_tick: the residual plot y axis ticks
"""
ch = Chn.Chn(filePath)
y = ch.spectrum
#print('Real time: %d'%ch.real_time)
x = np.arange(len(y))
ind = np.argmax(y)
x0 = x[ind]
yy = y[x0-left:x0+right]
xx = np.arange(len(yy))
print(xx)
print(x0-left)
xx, xxErr = convertChannelToEnergy(xx)
x0, x0Err = convertChannelToEnergy(x0)
left, leftErr = convertChannelToEnergy(left)
print(xx)
print(x0-left)
# p0[3] = convertChannelToEnergy(p0[3])
yerr = np.sqrt(yy)
popt, perr, rchi, dof = expGaussFit(filePathtobeSaved, xx, yy, yerr, p0, x0, left, res_tick, xerr=xxErr)
popt[3] += x0-left
print('File Name: %s'%filenm)
print('Mean 1 (Not Scaled): %f \pm %f'%(popt[3], perr[3]))
return popt, perr, rchi, dof
def fitAlphaPeaksGaussMul(filePathtobeSaved, filePath, p0, left=100, right=100, res_tick=[-3,0,3], sigmaFixed=True):
"""
This is the function to fit Alpha Peak
filepath: full path to the file; p0: list of initial guess; left: how much away
to the left from peak channel; right: how much away to the right from peak channel
res_tick: the residual plot y axis ticks
"""
ch = Chn.Chn(filePath)
y = ch.spectrum
x = np.arange(len(y))
ind = np.argmax(y)
x0 = x[ind]
yerr = np.sqrt(y)
popt, perr, rchi, dof = gaussianFitMul(filePathtobeSaved, x, y, yerr, p0, left, right, res_tick, sigmaFixed=sigmaFixed)
if not sigmaFixed:
#a1, a1_e = popt[0], perr[0]
m1, m1_e = popt[1], perr[1]
#s1, s1_e = popt[2], perr[2]
#a2, a2_e = popt[3], perr[3]
m2, m2_e = popt[4], perr[4]
#s2, s2_e = popt[5], perr[5]
#a3, a3_e = popt[6], perr[6]
m3, m3_e = popt[7], perr[7]
#s3, s3_e = popt[8], perr[8]
popt[1] += x0-left
popt[4] += x0-left
popt[7] += x0-left
else:
#a1, a1_e = popt[0], perr[0]
m1, m1_e = popt[1], perr[1]
#s1, s1_e = popt[-1], perr[-1]
#a2, a2_e = popt[2], perr[2]
m2, m2_e = popt[3], perr[3]
#s2, s2_e = popt[-1], perr[-1]
#a3, a3_e = popt[4], perr[4]
m3, m3_e = popt[5], perr[5]
#s3, s3_e = popt[-1], perr[-1]
popt[1] += x0-left
popt[3] += x0-left
popt[5] += x0-left
print('Mean 1 (Not Scaled): %f \pm %f'%(m1+x0-left, m1_e))
print('Mean 2 (Not Scaled): %f \pm %f'%(m2+x0-left, m2_e))
print('Mean 3 (Not Scaled): %f \pm %f'%(m3+x0-left, m3_e)+'\n')
return popt, perr, rchi, dof
####################################################################################################################
################################# Transform from channel number data to energy #####################################
####################################################################################################################
def convertChannelToEnergy(channelData, err=0, noErr=False):
m = slope
b = intercept
m_e = slopeErr
b_e = interceptErr
energyData = m*channelData + b
errProp = np.sqrt((m*err)**2+(m_e*channelData)**2+b_e**2)
if not noErr:
return energyData, errProp
else:
return energyData
def calibratePulses(folderName):
mean, sigma, mean_e, vol, vol_e, rchi = [], [], [], [], [], []
data = os.listdir(folderName)
for d in data:
filePathtobeSaved = 'Figures/Calibration/'+d.split('.')[0]
ch = Chn.Chn(folderName+'/'+d)
y = ch.spectrum
x = np.arange(len(y))
yerr = np.sqrt(y)
popt, perr, rchi_temp = gaussianFit(filePathtobeSaved, x, y, yerr, p0=[300, 20, 5], res_tick=[-3,0,3])
mean.append(popt[1])
print(type(popt[1]))
sigma.append(popt[2])
mean_e.append(perr[1])
vol.append(int(d.split('_')[0]))
vol_e.append(int(d.split('_')[2]))
rchi.append(rchi_temp)
y = vol
y = np.asarray(y)/1000 # convert to volts
x = mean
xerr = mean_e
yerr = np.asarray(vol_e)/1000 # convert to volts
print(yerr)
###############For latex#################
#for i in range(len(vol)):
# print('%d & $%.2f\pm%.2f$ & $%.2f\pm0.04$ & $%.2f$ \\\\'%(i+1, y[i], yerr[i], x[i], rchi[i]))
#########################################
filePathtobeSaved = 'Figures/Calibration/pulseLinear'
fig = plt.figure(figsize=(8,6))
plt.errorbar(x, y, yerr=yerr, xerr=xerr, fmt='o', elinewidth=1.5 ,capsize=4.5, ecolor='b', \
label='Data', linestyle='None', markersize=3 ,color='k')
popt, perr = LinearFit_xIntercept(x, y, yerr)
m = popt[0]
h = popt[1]
m_e = perr[0]
h_e = perr[1] # y-intercept
npara = 2
x = np.asarray(x) # this line of code saved my life, and it may save your life with this error
# 'numpy.float64' object cannot be interpreted as an integer - Alvin
y = np.asarray(y)
yerr = np.asarray(yerr)
rchi, dof = reducedChiSquare(y, m*x-m*h*np.ones(len(x)), yerr, npara)
xx = np.linspace(0, max(x))
plt.plot(xx, m*xx-m*h*np.ones(len(xx)), color='r', label='Fit', linewidth=2)
plt.xlabel('Mean MCA Channel Number')
plt.ylabel('Pulser Voltage (V)')
print('x-intercept: %f $\pm$ %f'%(h,h_e))
print('Slope: %f $\pm$ %f'%(m,m_e))
print('RChi: %f'%(rchi))
print('DOF: %d'%(dof))
plt.legend()
d = y-(x*m-m*h*np.ones(len(x)))
axes = plt.gca()
plotStudRes(axes, d, x, yerr, res_tick=[-1,0,1])
func = 'm=slope x-intercept=b'
func = func.replace('b','('+str(int(round(popt[1],0)))+'$\pm$'+str(int(round(perr[1],0)))+')')
func = func.replace('slope','('+str(round(popt[0],5))+'$\pm$'+str(round(perr[0],5))+')')
# textstr = '$\chi^2$=%.2f\tDOF=%d\t%s'%(rchi, dof, func)
# plt.text(0.1, 0.9, textstr, fontsize=10, transform=plt.gcf().transFigure)
plt.show()
fig.savefig(filePathtobeSaved+'.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
global E0 # americium energy needed for calibration
global E0Err
global calibIntercept #intercept on channel number versus voltage. beta in our notes.
global calibInterceptErr
global N0
global N0Err
global slope #slope on energy versus channel number
global slopeErr
global intercept #intercept on energy versus channel number
global interceptErr
E0 = 5.48556
E0Err = 0.00012
calibInterceptErr = h_e
calibIntercept = h
popt_am, perr_am, rchi_am, dof_am = fitAlphaPeaksGaussMul("Figures/Calibration/Americium_300_sec.Chn", "Americium/Americium_300_sec.Chn", \
[8, 20, 60, 30, 310, 40, 3], left=50, right=20, res_tick=[-2,0,2], sigmaFixed=True)
N0, N0Err = popt_am[-2], perr_am[-2]
print(N0)
slope = E0/(N0-calibIntercept)
slopeErr = slope*np.sqrt((E0Err/E0)**2+(1/(N0-calibIntercept))**2*(calibInterceptErr**2+N0Err**2))
intercept = E0*calibIntercept/(calibIntercept-N0)
interceptErr = intercept*np.sqrt((E0Err/E0)**2+(N0*calibInterceptErr/(calibIntercept*(calibIntercept-N0)))**2+(N0Err/(calibIntercept-N0))**2)
print('\nAmericium Energy: '+str(E0)+' \pm '+str(E0Err)+' MeV')
print('Beta intercept: ' + str(calibIntercept) + ' \pm ' + str(calibInterceptErr))
print('Calibration Slope, m: ' + str(slope) + ' \pm ' + str(slopeErr))
print('Calibration Intercept, b: ' + str(intercept) + ' \pm ' + str(interceptErr))
return m, h, m_e, h_e
####################################################################################################################
############################################# Stopping power calculation ###########################################
####################################################################################################################
def pressureData(folderName):
"""
This function reads all pressure varied data from the directory and fit linearly;
folderName: string of the folder name under root directory
"""
outPath = 'Figures/Pressure'
peak_means, peak_means_e = [], []
data = os.listdir(folderName)
pressure = []
for d in data:
p = d.split('_')[0]
pressure.append(int(p))
pressure = np.asarray(pressure)
for i in range(len(pressure)):
if pressure[i]==50:
pressure[i] = pressure[i]*0.0013332237
print(pressure)
for file in data:
popt, perr, rchi, dof = fitAlphaPeak(outPath+'/'+file, folderName+'/'+file, p0=[60, 1, 0.7, 10],\
right=30, filenm=file)
peak_means.append(popt[3])
peak_means_e.append(perr[3])
fig = plt.figure(figsize=(8,6))
x = pressure
y = peak_means
yerr = peak_means_e
popt, perr = linearFit(x, y, yerr)
m = popt[0]
b = popt[1]
m_e = perr[0]
b_e = perr[1]
xx = np.linspace(min(x), max(x))
plt.plot(xx, m*xx+b*np.ones(len(xx)),label='Fit', color='r')
plt.errorbar(x, y, yerr=yerr, fmt='o', elinewidth=2 ,capsize=3, ecolor='b', \
label='Data', linestyle='None', markersize=4,color='b')
npara = 2
rchi, dof = reducedChiSquare(y, m*x+b*np.ones(len(x)), yerr, npara)
plt.xlabel('Pressure (mBar)')
plt.ylabel('Energy (MeV)')
d = y-(m*x+b*np.ones(len(x)))
axes = plt.gca()
plotStudRes(axes, d, x, yerr, res_tick=[-5,0,5])
print('\nIntercept: %f $\pm$ %f'%(b,b_e))
print('Slope: %f $\pm$ %f'%(m,m_e))
print('r-chi-square: %.2f'%rchi)
print('DOF: %d'%dof)
plt.show()
fig.savefig(outPath+'/StoppingPower.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
#Target thickness is (air density) * (distance b/w source and detector)
#Using the names given in the notebook, the distance between the source and the
#detector is Distance = L - length of detector - (length of source - bottom half of source)
#I multiply by 100 to get the distance in centimeters
Distance = (89.530 - 15.810 - (13.890-9.380))/10.0
#The error, since we simply add 4 values with 0.005 mm error, is given by the following
#(once again, in cm)
Distance_e = ((4.0*(0.005**2.0))**0.5)/10.0
#Air density rho_air is (M_air*pressure)/(R*T)
#The values of M_air, T, R, are defined at the beginning of this file
pressure_e = 10.0 #in [mbar]
#Thickness is defined as M_air*pressure*Distance/(R*T)
Thickness = []
Thickness_e = []
for i in range(len(pressure)):
Thickness.append( M_air*pressure[i]*Distance/(R*T) ) #in [g] [cm-2]
Thickness_e.append( ( (M_air_e * pressure[i]*Distance/(R*T))**2 + (pressure_e * M_air*Distance/(R*T))**2 + (Distance_e * M_air*pressure[i]/(R*T))**2 + (R_e * M_air*pressure[i]*Distance/(R*R*T))**2 + (T_e * M_air*pressure[i]*Distance/(R*T*T))**2 )**0.5 ) #in [g] [cm-2]
fig = plt.figure(figsize=(8,6))
# x = pressure
x = Thickness
y = peak_means
yerr = peak_means_e
popt, perr = linearFit(x, y, yerr)
m = popt[0]
b = popt[1]
m_e = perr[0]
b_e = perr[1]
# xx = np.linspace(min(x), max(x))
# plt.plot(xx, m*xx+b*np.ones(len(xx)),label='Fit', color='r')
plt.errorbar(x, y, yerr=yerr, fmt='o', elinewidth=1.5 ,capsize=3, ecolor='b', \
label='Data', linestyle='None', markersize=3,color='k')
# npara = 2
# rchi, dof = reducedChiSquare(y, m*x+b*np.ones(len(x)), yerr, npara)
# plt.xlabel('Pressure (mBar)')
plt.xlabel(r'Thickness (g$\cdot$cm$^{-2}$)')
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0), useMathText=True)
plt.ylabel('Energy (MeV)')
# d = y-(m*x+b*np.ones(len(x)))
# axes = plt.gca()
# plotStudRes(axes, d, x, yerr, res_tick=[-5,0,5])
# print('\nIntercept: %f $\pm$ %f'%(b,b_e))
# print('Slope: %f $\pm$ %f'%(m,m_e))
# print('r-chi-square: %.2f'%rchi)
# print('DOF: %d'%dof)
plt.show()
# fig.savefig(outPath+'/EnergyVsPressure.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
fig.savefig(outPath+'/EnergyVsThickness.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
## Reshuffle data to be in order of increasing thickness
Energy = peak_means
Energy_e = peak_means_e
data = [(Thickness[n], Energy[n], Thickness_e[n], Energy_e[n]) for n in range(len(Thickness))]
data = sorted(data, key=lambda Thickness: Thickness[2])
print(data)
Thickness = [data[n][0] for n in range(len(data))]
Energy = [data[n][1] for n in range(len(data))]
Thickness_e = [data[n][2] for n in range(len(data))]
Energy_e = [data[n][3] for n in range(len(data))]
return m, m_e, b, b_e, Energy, Energy_e, Thickness, Thickness_e
def locallyDifferentiate(x,y,xerr,yerr):
"""
:param x: x data vector
:param y: y data vector
:param xerr: vector of error on x data
:param yerr: vector of error on y data
:return: (X,Y) are the local derivative of the (x,y) data set. Ie for each adjacent data points in the input data set, a straight line is drawn between them
and the slope of the line is added to the vector Y. The vector X is compromised of the midpoints between the adjacent x's. The errors on X,Y are computed in terms
of xerr and yerr
"""
X=[]
Xerr=[]
for n in range(1,len(x)):
X.append(np.sqrt(x[n]**2+x[n-1]**2)/2)
Xerr.append((xerr[n]+xerr[n-1])/2)
print(len(X))
Y=[]
Yerr=[]
for n in range(1,len(x)):
Y.append((y[n]-y[n-1])/(x[n]-x[n-1]))
Yerr.append(Y[n-1]*np.sqrt(((xerr[n]**2+xerr[n-1]**2)/(x[n]-x[n-1])**2)+((yerr[n]**2+yerr[n-1]**2)/(y[n]-y[n-1])**2)))
print(len(Y)==len(y)-1)
return X,Y,Xerr,Yerr
def calculateStoppingPower(folderName):
# constants
Distance = (89.530 - 15.810 - (13.890-9.380))/10.0
Distance_e = ((4.0 * (0.005 ** 2.0)) ** 0.5) / 10.0
pressure_e = 10.0 # in [mbar]
prefactor = 1/(Distance) # such that the spotting power is just - prefactor * thickness * energy/thickness local derivative :)
prefactor_e = Distance_e/(Distance)**2
#summon the thickness versus energy data (properly ordered)
m_press, m_press_e, b_press, b_press_e, Energy, Energy_e, Thickness, Thickness_e = pressureData('PressureWBias_1')
# take the slopes of adjacent points to create a "local derivative" dataset
x, y, xerr, yerr = locallyDifferentiate(Thickness, Energy, Thickness_e, Energy_e)
#suggestively relabel
t = np.asarray(x)
dEdt = np.asarray(y)
t_err = np.asarray(xerr)
dEdt_err = np.asarray(yerr)
#calculate the stopping power :DDD
S = [-prefactor*t[n]*dEdt[n] for n in range(len(t))]
#propagate the error
Serr = [S[n]*np.sqrt((t_err[n]/t[n])**2+(prefactor_e/prefactor)**2+(dEdt_err[n]/dEdt[n])**2) for n in range(len(t))]
# plot away
fig = plt.figure(figsize=(8, 6))
plt.errorbar(t, S, yerr=Serr, xerr=t_err, fmt='o', elinewidth=1.5 ,capsize=3, ecolor='b', \
label='Data', linestyle='None', markersize=3,color='k')
plt.xlabel(r'Thickness (g$\cdot$cm$^{-2}$)')
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0), useMathText=True)
plt.ylabel('Stopping Power (MeV/cm)')
outPath = 'Figures/Pressure'
fig.savefig(outPath+'/StoppingPower_thickeness.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
####################################################################################################################
################## Fit bismuth activity data in order to extract lead and bismuth half-lives #######################
####################################################################################################################
def activityFitFunc(x, lambda1, lambda2, N0, N1):
return N0*lambda1*lambda2*(np.exp(-lambda1*x)-np.exp(-lambda2*x))/(lambda2-lambda1)+N1*np.exp(-lambda2*x)
def activityFit(filePathtobeSaved, x, y, yerr, guess):
fig = plt.figure(figsize=[8,6])
popt, pcov = curve_fit(activityFitFunc, x, y, p0=guess, maxfev=50000)
perr = np.sqrt(np.diag(pcov))
npara = 4
rchi, dof = reducedChiSquare(y, activityFitFunc(x, *popt), yerr, npara)
plt.errorbar(x, y, yerr=yerr, fmt='o', elinewidth=1.5 ,capsize=3, ecolor='b', \
label='Data', linestyle='None', markersize=3,color='k')
xx = np.linspace(min(x), max(x))
plt.plot(xx, activityFitFunc(xx, *popt), '-r', label='Fit')
plt.legend()
plt.ylabel('Activity ($s^{-1}$)')
plt.xlabel('Time (s)')
print('lambda1: %f \pm %f'%(popt[0], perr[0]))
print('lambda2: %f \pm %f'%(popt[1], perr[1]))
print('N0: %f \pm %f'%(popt[2], perr[2]))
print('N1: %f \pm %f'%(popt[3], perr[3]))
print('RChi: %f'%(rchi))
print('DOF: %d'%(dof))
# Plot residuals
d = y-activityFitFunc(x,*popt)
axes = plt.gca()
plotStudRes(axes, d, x, yerr, res_tick=[-2,0,2])
plt.show()
fig.savefig(filePathtobeSaved+'.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0.0)
return popt, perr, rchi, dof
def halflifeMeasurement(outFileName, folderName):
filePathtobeSaved = 'Figures/HalfLifeMeasurement/'+outFileName
files = os.listdir(folderName)
files.sort()
activity, elapse = [], []
for f in files:
ch = Chn.Chn(folderName+'/'+f)
spectrum = ch.spectrum
activity.append(np.float64(sum(spectrum)))
fileInd = float(f.split('_')[0])
elapse.append(fileInd*10*60) #10 minutes, starts at 0
x = np.asarray(elapse)
# have not converted to energy
y = np.asarray(activity)
yerr = np.sqrt(activity)
popt, perr, rchi, dof = activityFit(filePathtobeSaved, x, y, yerr, [1.81e-5,1.9e-4,1.0e5,1.0e2])
l1, l2 = popt[0], popt[1]
l1_e, l2_e = perr[0], perr[1]
T1 = np.log(2)/l1/60 # in hour
T2 = np.log(2)/l2/3600 # in hour
T1_e = np.log(2)/60/l1**2*l1_e
T2_e = np.log(2)/3600/l2**2*l2_e
print('Tau1: %.1f \pm %.1f'%(T1, T1_e))
print('Tau1: %.1f \pm %.1f'%(T2, T2_e))
return T1, T1_e, T2, T2_e
def branchingRatio_FourPeaks(InFileName):
outFileName = 'Figures/BranchingRatio/'+InFileName+'_FourPeaks'
spectrum = chnsum(InFileName)
left = 1200
# left = 1280
right = 1330
# right = 1280
leftSpec = spectrum[left:right]
y = leftSpec
yerr = np.sqrt(y)
x = np.arange(left,right)
plt.plot(x,leftSpec)
# Guess of fitting 5 peaks
p0 = [600, 0.1, 2.5, left+20, 600, 0.1, 1.5, left+50, 15000, 0.3, 1.5, left+115, 6000, 0.1, 1.5, left+125, 5000, 0.3, 1.3, left+105] # A, l, s, m
# Guess of fitting 4 peaks
# p0 = [700, 0.1, 2.5, left+20, 7000, 0.1, 1.5, left+50, 15000, 0.3, 1.5, left+115, 6000, 0.3, 1.5, left+125]
# p0 = [10000, 0.3, 1.3, left+35, 4000, 0.3, 1.3, left+45, 3000, 1, 1.3, left+15, 2000, 1, 1.3, left+25] # A, l, s, m
# p0 = [500, 0.1, 2, left+20, 600, 0.1, 2, left+55] # A, l, s, m
popt, perr, rchi, dof = expGaussFitMul(outFileName, x, y, yerr, p0=p0, x0=0, left=0, res_tick=[-2,0,2])
# Plot each component convolution
for i in range(int(len(popt)/4)):
temp = popt[i*4:(i+1)*4]
plt.plot(x, expGauss(x, *temp))
#convert mean channels to energies
for i in range(0, len(popt), 4):
popt[i+3], perr[i+3] = convertChannelToEnergy(popt[i+3], err=perr[i+3])
# make fitted parameter into a matrix, as well as the fitting error in another matrix
l = int(len(popt)/4)
valueToReturn, errToReturn = np.zeros((l,4)), np.zeros((l,4))
for i in range(l):
valueToReturn[i] = popt[4*i:4*i+4]
errToReturn[i] = perr[4*i:4*i+4]
print(valueToReturn)
print(errToReturn)
return valueToReturn, errToReturn
def branchingRatio_Largest(InFileName):
outFileName = 'Figures/BranchingRatio/'+InFileName+'_Largest'
spectrum = chnsum(InFileName)
left = 1820
right = 1839
leftSpec = spectrum[left:right]
y = leftSpec
yerr = np.sqrt(y)
x = np.arange(left,right)
plt.plot(x,leftSpec)
p0 = [35000, 1, 1, left+15, 12000, 1, 1, left+10]
popt, perr, rchi, dof = expGaussFitMul(outFileName, x, y, yerr, p0=p0, x0=0, left=0, res_tick=[-2,0,2])
# Plot each component convolution
for i in range(int(len(popt)/4)):
temp = popt[i*4:(i+1)*4]
plt.plot(x, expGauss(x, *temp))
# convert mean channels to energies
#for i in range(0, len(popt), 4):
# popt[i+3], perr[i+3] = convertChannelToEnergy(popt[i+3], err=perr[i+3])
<<<<<<< HEAD
=======
print(popt)
print(perr)
>>>>>>> 1a8d4df9c1a28eec7bb7b9431626565807aa8371
# make fitted parameter into a matrix, as well as the fitting error in another matrix
l = int(len(popt)/4)
valueToReturn, errToReturn = np.zeros((l,4)), np.zeros((l,4))
for i in range(l):
valueToReturn[i] = popt[4*i:4*i+4]
errToReturn[i] = perr[4*i:4*i+4]
<<<<<<< HEAD
# print(popt[4*i:4*i+4])
print(valueToReturn)
print(errToReturn)
=======
# print(popt[4*i:4*i+4])
# print(valueToReturn)
# print(errToReturn)
>>>>>>> 1a8d4df9c1a28eec7bb7b9431626565807aa8371
# valueToReturn[0], valueToReturn[1] = valueToReturn[1], valueToReturn[0]
# errToReturn[0], errToReturn[1] = errToReturn[1], errToReturn[0]
# print(valueToReturn)
# print(errToReturn)
return valueToReturn, errToReturn
####################################################################################################################
#################################### Branching ratios calculation ##################################################
####################################################################################################################
def integrateExpGauss(params):
# params is the vector (A,lambda, sigma, mu) describing the expgaussian integrand
return quad(expGauss, 1200, 1900, args=(params[0],params[1],params[2],params[3]))[0]
def diffExpGaussSigma(x,params):
#returns the derivative of ExpGaussFunction with respect to sigma as function of x
A = params[0]
l = params[1]
s = params[2]
m = params[3]
return A*l/(2*s**2)*np.exp(-(x-m)**2/(2*s**2))*(np.sqrt(2/np.pi)*(x-m-l*(s**2))+(l**2)*(s**3)*np.exp(l/2*(2*x-2*m+l*s*s))*(1-sp.special.erf((x+l*s*s-m)/(math.sqrt(2)*s))))
def integrateDiffExpGaussSigma(params):
return quad(diffExpGaussSigma, 1200, 1900, args=(params))[0]
def diffExpGaussLambda(x, params):
# returns the derivative of ExpGaussFunction with respect to lambda as function of x
A = params[0]
l = params[1]
s = params[2]
m = params[3]
return A/2*np.exp(l/2*(2*x-2*m+l*s*s))*((1+l*(x-m+l*s**2))*(1-sp.special.erf((x+l*s*s-m)/(math.sqrt(2)*s)))-np.sqrt(2/np.pi)*l*s*(np.exp(-(x-m+l*s**2)**2/(2*s**2))))
def integrateDiffExpGaussLambda(params):
return quad(diffExpGaussLambda, 1200, 1900, args=(params))[0]
def diffExpGaussA(x,params):
# returns the derivative of ExpGaussFunction with respect to lambda as function of x
l = params[1]
s = params[2]
m = params[3]
return l/2*np.exp(l/2*(2*x-2*m+l*s*s))*(1-sp.special.erf((x+l*s*s-m)/(math.sqrt(2)*s)))