-
Notifications
You must be signed in to change notification settings - Fork 3
/
timeCal.bash
executable file
·228 lines (201 loc) · 5.56 KB
/
timeCal.bash
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
#!/bin/bash
#-------------------------------------------------------------------------------
# -- \file timeCal.bash
# -- Description: This is a bash script that will extract the ToF and Tdiff
# -- spectra from an uncalibrated VANDLE histogram using readhis. It will
# -- give as final output the xml code for the pixie_scan-v3 configuration
# -- file. It expects as input the number of VANDLE bars you have in the
# -- analysis.
# --
# -- \author S. V. Paulauskas
# -- \date 05 December 2012
# --
# -- This script is distributed as part of a suite to perform
# -- calibrations for VANDLE. It is distributed under the
# -- GPL V 3.0 license.
# ------------------------------------------------------------------------------
speedOfLight=29.9792458 #cm/ns
tmpDir="./tmp"
tempHistData="$tmpDir/tcal.dat"
tempFitResults="$tmpDir/tcal.par"
errorLog="errors.log"
rm $errorLog > /dev/null 2>&1
skippedCount=0
source config.bash
if [ ! -z $1 ]
then
his="$1"
hisbase=`basename $his .his`
if [ ! -z $2 ]
then
resultDir=$2/$hisbase
fi
fi
if [ ! -d "$tmpDir" ]
then
mkdir -p $tmpDir
fi
if [ ! -d "$resultDir" ]
then
echo -e "Result directory missing, creating..."
mkdir -p $resultDir
fi
if [ ! -d "$physOffsetDir" ]
then
echo -e "Physical offsets directory missing, creating..."
mkdir -p $physOffsetDir
fi
if [ ! -f "$his" ]
then
echo -e "We could not find the requested his file this is fatal!"
exit
fi
let maxStartCount=$numStarts-1
let numSmallCount=$numSmallBars-1
let numMediumCount=$numMediumBars-1
let numBigCount=$numBigBars-1
SumSpectraCounts() {
sum=`awk '{if(NR>4){sum += $2}} END{print sum}' $tempHistData`
if [ $sum -ge $minStats ]
then
hasEnoughStats=true
else
if [[ $fitType -eq "tof" ]]
then
skipped[skippedCount]="ToF : $type $num w/ Start $startNum"
elif [[ $fitType -eq "diff" ]]
then
skipped[skippedCount]="Diff : $type $num"
else
echo "We should never have an unknown fit type."
fi
let skipCount=skipCount+1
hasEnoughStats=false
fi
}
ProjectSpectra() {
readhis $his --id $histId --gy $row,$row > $tempHistData
if [ ! -f $tempHistData ]
then
echo -e "Something went wrong with the projection for $hisId in $his"
fi
SumSpectraCounts
}
PerformFit() {
ProjectSpectra
if [ "$hasEnoughStats" = true ]
then
gnuplot timeCal.gp > /dev/null 2>&1 && fitRes=`cat $tempFitResults`
else
fitRes=0
fi
if [ -z $fitRes ]
then
echo -e "Something went wrong with the fitting, there was no result. Fatal!"
exit
fi
if [ "$fitRes" != 0 ]
then
if [ "$fitType" == "diff" ]
then
fitRes=`echo "scale=5;($histOffset-$fitRes)/$histResolution" | bc -l`
fi
if [ "$fitType" == "tof" ]
then
fitRes=`echo "scale=5;($gammaTofBins-$fitRes)/$histResolution" | bc -l`
fi
else
echo "Warning: Not enough stats for $fitType fit in $type bar $j " >> $errorLog
fi
}
CalcGammaTof() {
gammaTofNs=`echo "$z0/$speedOfLight" | bc -l`
gammaTofBins=`echo "$gammaTofNs*$histResolution+$histOffset" | bc -l`
}
SetParams(){
type=$1
if [ "$type" = small ]
then
dist=$smallDist
offset=$smallOffset
maxBarCount=$numSmallCount
numBars=$numSmallBars
physOffsets=$physOffsetDir/$smallOffsets
elif [ "$type" = medium ]
then
type=medium
dist=$mediumDist
offset=$mediumOffset
maxBarCount=$numMediumCount
numBars=$numMediumBars
physOffsets=$physOffsetDir/$mediumOffsets
elif [ "$type" = big ]
then
type=big
dist=$bigDist
offset=$bigOffset
maxBarCount=$numBigCount
numBars=$NumBigBars
physOffsets=$physOffsetDir/$bigOffsets
else
echo "ERROR: We have gotten an unknown bar type ($type)!! "\
"This should never happen! Now barfing...."
exit
fi
}
CalculateAndOutput(){
echo -e "<$type>"
for j in `seq 0 $maxBarCount`
do
num=$j; row=$j
let histId=$vandleOffset+$vandleTdiffBaseId+$offset
fitType="diff"
PerformFit
z0=`awk -v barnum=$j '{if($1==barnum && NR!=1) print $2}' $physOffsets`
CalcGammaTof
physInfo=`awk -v barnum=$j '{if($1==barnum && NR!=1) print "z0=\""$2"\" xoffset=\""$3"\" zoffset=\""$4"\""}' $physOffsets`
echo -e " <Bar number=\"$j\" lroffset=\"$fitRes\" $physInfo>"
let histId=vandleOffset+vandleTofBaseId+$offset
for i in `seq 0 $maxStartCount`
do
startNum=$i
row=`echo "$j*$numStarts+$i" | bc`
fitType="tof"
PerformFit
echo -e " <TofOffset location=\"$i\" offset=\"$fitRes\"/>"
done
echo -e " </Bar>"
done
echo -e "</$type>"
}
OutputInfo() {
echo "We are calculating the parameters for $numBars $type Bars."
}
if [ ! -d $resultDir ]
then
echo "Creating the results directory."
mkdir -p $resultDir
fi
if [[ ! -z $numSmallBars && "$numSmallBars" != 0 ]]
then
SetParams "small"
OutputInfo
CalculateAndOutput > $resultDir/smallConfig.xml
fi
if [[ ! -z $numBigBars && "$numBigBars" != 0 ]]
then
SetParams "big"
OutputInfo
CalculateAndOutput > $resultDir/bigConfig.xml
fi
if [[ ! -z $numMediumBars && "$numMediumBars" != 0 ]]
then
SetParams "medium"
OutputInfo
CalculateAndOutput > $resultDir/mediumConfig.xml
fi
if [ -f $errorLog ]
then
echo "There were errors/warnings written to the error log: $errorLog"
fi
rm -rf ./fit.log $tmpDir