/
analysisFreq.py
91 lines (82 loc) · 2.61 KB
/
analysisFreq.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
import root_utils
import calc_utils
import os
import sys
import ROOT
import matplotlib.pyplot as plt
import numpy as np
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
freqValues = []
peakValues = []
peakErrors = []
FWHMValues = []
FWHMErrors = []
areaValues = []
areaErrors = []
topFolder = sys.argv[1]
os.chdir(topFolder)
items = os.listdir(".")
folders = []
for freqfolder in items:
if os.path.isdir(freqfolder) and is_number(freqfolder[:-3]):
print freqfolder[:-3]
folders.append(freqfolder)
folders = sorted(folders,key=lambda x:int(x[:-2]))
print folders
for freqFolder in folders:
print "THIS IS FREQ FOLDER: "+str(freqFolder)
x,y = calc_utils.readTRCFiles(freqFolder,False)
output = ROOT.TFile(freqFolder+".root","recreate")
areaHisto, area, areaErr = root_utils.plot_area(x,y,"area")
widthHisto, width, widthErr = root_utils.plot_width(x,y,"FWHM")
peakHisto, meanPeak, peakErr = root_utils.plot_peak(x,y,"peak")
numSqrt = np.sqrt(len(os.listdir(freqFolder)))
areaHisto.Write()
widthHisto.Write()
peakHisto.Write()
output.Close()
peakValues.append(meanPeak)
peakErrors.append(peakErr/numSqrt)
FWHMValues.append(width)
FWHMErrors.append(widthErr/numSqrt)
areaValues.append(area)
areaErrors.append(areaErr/numSqrt)
freqValues.append(float(freqFolder[:-3]))
areaFile = open("areas.txt","w")
peakFile = open("peaks.txt","w")
widthFile = open("FWHM.txt","w")
for i in range(len(areaValues)):
areaLine = str(freqValues[i])+" "+str(areaValues[i])+" "+str(areaErrors[i])+"\n"
peakLine = str(freqValues[i])+" "+str(peakValues[i])+" "+str(peakErrors[i])+"\n"
FWHMLine = str(freqValues[i])+" "+str(FWHMValues[i])+" "+str(FWHMErrors[i])+"\n"
areaFile.write(areaLine)
peakFile.write(peakLine)
widthFile.write(FWHMLine)
areaFile.close()
peakFile.close()
widthFile.close()
plt.figure(0)
plt.errorbar(freqValues,peakValues,yerr=peakErrors)
plt.title("freqValues vs peakValues")
plt.savefig("peaks.png")
plt.figure(1)
plt.errorbar(freqValues,FWHMValues,yerr=FWHMErrors)
plt.title("freqValues vs FWHMValues")
plt.savefig("FWHM.png")
plt.figure(2)
plt.errorbar(freqValues,areaValues,yerr=areaErrors)
fitWeights = []
#for iError in range(len(areaErrors)):
# fitWeights.append(1.0/np.sqrt(areaErrors[iError]**2))
#fitValues = np.polyfit(freqValues,areaValues,1,w=fitWeights)
#poly = np.poly1d(fitValues)
#plt.plot(freqValues,poly(freqValues))
plt.title("freqValues vs areaValues")
plt.xscale("log")
plt.savefig("area.png")
#print "Number of Photons: "+str(calc_utils.get_photons(area,0.5))