]> Creatis software - clitk.git/blob - cluster_tools/mergeDoseByRegions.sh
Debug RTStruct conversion with empty struc
[clitk.git] / cluster_tools / mergeDoseByRegions.sh
1 #!/bin/bash
2 set -u
3
4 function usage {
5   echo "$0 -i <result> -j <file2> -o <result>"
6   exit 1
7 }
8
9 function addToPartialResult {
10   IN1=$2 #merged file for previous jobs
11   IN2=$4 # current job
12   RESULT=$6 #output merged file
13
14   test -f ${IN1} && test -f ${IN2} || usage
15
16   TMP="$(mktemp)"
17
18   # check if all 3 text files have the same number of lines
19   nblines=`cat ${IN1} | wc -l`
20   nb2=`cat ${IN2} | wc -l`
21   nb3=`cat ${RESULT} | wc -l`
22
23   if [ $nblines -ne $nb2 ] || [ $nblines -ne $nb3 ]; then
24     echo "Files does not have the same size"
25     exit 1
26   fi
27
28   # Find the number of primaries for this job (the same for all line and different of 0)
29   # Get the number of primaries from edep and square_edep to determine the std.
30   nbPrimary=0
31   for i in $(seq 2 $nblines); do
32     #Get the line
33     file2=`sed -n "${i}p" < ${IN2}`
34
35     edep2=$(echo ${file2} | cut -f3 -d' ')
36     stdEdep2=$(echo ${file2} | cut -f4 -d' ')
37     sqEdep2=$(echo ${file2} | cut -f5 -d' ')
38     nbPrimary=$(python <<EOP
39 if ($edep2 == 0 or $sqEdep2 == 0):
40   print(0)
41 else:
42   temp=pow($edep2,2)*(pow($stdEdep2,2)-1)/(pow($stdEdep2*$edep2,2)-$sqEdep2)
43   print(int(round(temp)))
44 EOP
45     )
46     if [ $nbPrimary -ne 0 ]; then
47       break
48     fi
49
50     dose2=$(echo ${file2} | cut -f6 -d' ')
51     stdDose2=$(echo ${file2} | cut -f7 -d' ')
52     sqDose2=$(echo ${file2} | cut -f8 -d' ')
53     nbPrimary=$(python <<EOP
54 if ($dose2 == 0 or $sqDose2 == 0):
55   print(0)
56 else:
57   temp=pow($dose2,2)*(pow($stdDose2,2)-1)/(pow($stdDose2*$dose2,2)-$sqDose2)
58   print(int(round(temp)))
59 EOP
60     )
61     if [ $nbPrimary -ne 0 ]; then
62       break
63     fi
64   done
65
66   #Copy the 1st line in output
67   line1=`sed -n "1p" < ${RESULT}`
68   echo "${line1}" >> ${TMP}
69   # for all lines (except the 1st), split according tab
70   # sum the some elements (dose, edep) ...
71   for i in $(seq 2 $nblines); do
72     #Get the 3 lines
73     file1=`sed -n "${i}p" < ${IN1}`
74     file2=`sed -n "${i}p" < ${IN2}`
75     file3=`sed -n "${i}p" < ${RESULT}`
76
77     # sum edep: get the 2 values, sum them and replace it in the file3
78     # The /#./0. is important to add 0 for decimal value between 0 and 1
79     edep1=$(echo ${file1} | cut -f3 -d' ')
80     edep2=$(echo ${file2} | cut -f3 -d' ')
81     edep3=$(python -c "print($edep1+$edep2)")
82     edep3=${edep3/#./0.}
83     file3=$(echo $file3 |awk -v r=${edep3} '{$3=r}1')
84
85     # sum square_edep: get the 2 values, sum them and replace it in the file3
86     sqEdep1=$(echo ${file1} | cut -f5 -d' ')
87     sqEdep2=$(echo ${file2} | cut -f5 -d' ')
88     sqEdep3=$(python -c "print($sqEdep1+$sqEdep2)")
89     sqEdep3=${sqEdep3/#./0.}
90     file3=$(echo $file3 |awk -v r=${sqEdep3} '{$5=r}1')
91
92     # Get the number of primaries from edep and square_edep to determine the std.
93     # Sum the number of primaries with the latter ones
94     nbPrimaryEdep1=$(echo ${file1} | cut -f4 -d' ')
95     nbPrimaryEdep3=$(python <<EOP
96 print($nbPrimary+$nbPrimaryEdep1)
97 EOP
98     )
99     nbPrimaryEdep3=${nbPrimaryEdep3/#./0.}
100     file3=$(echo $file3 |awk -v r=${nbPrimaryEdep3} '{$4=r}1')
101
102     # sum dose: get the 2 values, sum them and replace it in the file3
103     dose1=$(echo ${file1} | cut -f6 -d' ')
104     dose2=$(echo ${file2} | cut -f6 -d' ')
105     dose3=$(python -c "print($dose1+$dose2)")
106     dose3=${dose3/#./0.}
107     file3=$(echo $file3 |awk -v r=${dose3} '{$6=r}1')
108
109     # sum square_dose: get the 2 values, sum them and replace it in the file3
110     sqDose1=$(echo ${file1} | cut -f8 -d' ')
111     sqDose2=$(echo ${file2} | cut -f8 -d' ')
112     sqDose3=$(python -c "print($sqDose1+$sqDose2)")
113     sqDose3=${sqDose3/#./0.}
114     file3=$(echo $file3 |awk -v r=${sqDose3} '{$8=r}1')
115
116     # Get the number of primaries from dose and square_dose to determine the std.
117     # Sum the number of primaries with the latter ones
118     nbPrimaryDose1=$(echo ${file1} | cut -f7 -d' ')
119     stdDose2=$(echo ${file2} | cut -f7 -d' ')
120     nbPrimaryDose3=$(python <<EOP
121 print($nbPrimary+$nbPrimaryDose1)
122 EOP
123     )
124     nbPrimaryDose3=${nbPrimaryDose3/#./0.}
125     file3=$(echo $file3 |awk -v r=${nbPrimaryDose3} '{$7=r}1')
126
127     # sum n_hits: get the 2 values, sum them and replace it in the file3
128     hit1=$(echo ${file1} | cut -f9 -d' ')
129     hit2=$(echo ${file2} | cut -f9 -d' ')
130     hit3=$(python -c "print($hit1+$hit2)")
131     file3=$(echo $file3 |awk -v r=${hit3} '{$9=r}1')
132
133     # sum n_event_hits: get the 2 values, sum them and replace it in the file3
134     event1=$(echo ${file1} | cut -f10 -d' ')
135     event2=$(echo ${file2} | cut -f10 -d' ')
136     event3=$(python -c "print($event1+$event2)")
137     file3=$(echo $file3 |awk -v r=${event3} '{$10=r}1')
138
139     #Write the output
140     echo "${file3}" >> ${TMP}
141   done
142   mv -f ${TMP} ${RESULT}
143 }
144
145 function copyFirstPartialResult {
146   IN1=$2
147   RESULT=$4
148
149   TMP="$(mktemp)"
150
151   # check if all 2 text files have the same number of lines
152   nblines=`cat ${IN1} | wc -l`
153   nb3=`cat ${RESULT} | wc -l`
154
155   if [ $nblines -ne $nb3 ]; then
156     echo "Files does not have the same size"
157     exit 1
158   fi
159   # Find the number of primaries for this job (the same for all line and different of 0)
160   # Get the number of primaries from edep and square_edep to determine the std.
161   nbPrimary=0
162   for i in $(seq 2 $nblines); do
163     #Get the line
164     file2=`sed -n "${i}p" < ${IN1}`
165
166     edep2=$(echo ${file2} | cut -f3 -d' ')
167     stdEdep2=$(echo ${file2} | cut -f4 -d' ')
168     sqEdep2=$(echo ${file2} | cut -f5 -d' ')
169     nbPrimary=$(python <<EOP
170 if ($edep2 == 0 or $sqEdep2 == 0):
171   print(0)
172 else:
173   temp=pow($edep2,2)*(pow($stdEdep2,2)-1)/(pow($stdEdep2*$edep2,2)-$sqEdep2)
174   print(int(round(temp)))
175 EOP
176     )
177     if [ $nbPrimary -ne 0 ]; then
178       break
179     fi
180
181     dose2=$(echo ${file2} | cut -f6 -d' ')
182     stdDose2=$(echo ${file2} | cut -f7 -d' ')
183     sqDose2=$(echo ${file2} | cut -f8 -d' ')
184     nbPrimary=$(python <<EOP
185 if ($dose2 == 0 or $sqDose2 == 0):
186   print(0)
187 else:
188   temp=pow($dose2,2)*(pow($stdDose2,2)-1)/(pow($stdDose2*$dose2,2)-$sqDose2)
189   print(int(round(temp)))
190 EOP
191     )
192     if [ $nbPrimary -ne 0 ]; then
193       break
194     fi
195   done
196
197   #Copy the 1st line in output
198   line1=`sed -n "1p" < ${RESULT}`
199   echo "${line1}" >> ${TMP}
200   # for all lines (except the 1st), split according tab
201   # write the number of primaries
202   for i in $(seq 2 $nblines); do
203     #Get the lines
204     file3=`sed -n "${i}p" < ${RESULT}`
205
206     file3=$(echo $file3 |awk -v r=${nbPrimary} '{$4=r}1')
207     file3=$(echo $file3 |awk -v r=${nbPrimary} '{$7=r}1')
208
209     #Write the output
210     echo "${file3}" >> ${TMP}
211   done
212   mv -f ${TMP} ${RESULT}
213 }
214
215 function divideUncertaintyResult {
216   IN1=$2
217   RESULT=$4
218
219   TMP="$(mktemp)"
220
221   # check if all 2 text files have the same number of lines
222   nblines=`cat ${IN1} | wc -l`
223   nb3=`cat ${RESULT} | wc -l`
224
225   if [ $nblines -ne $nb3 ]; then
226     echo "Files does not have the same size"
227     exit 1
228   fi
229
230   #Copy the 1st line in output
231   line1=`sed -n "1p" < ${RESULT}`
232   echo "${line1}" >> ${TMP}
233   # for all lines (except the 1st), split according tab
234   # sum the some elements (dose, edep) ...
235   for i in $(seq 2 $nblines); do
236     #Get the lines
237     file1=`sed -n "${i}p" < ${IN1}`
238     file3=`sed -n "${i}p" < ${RESULT}`
239
240     # Divide uncertainty and replace it in the file3
241     edep1=$(echo ${file1} | cut -f3 -d' ')
242     nbPrimaryEdep1=$(echo ${file1} | cut -f4 -d' ')
243     sqEdep1=$(echo ${file1} | cut -f5 -d' ')
244     stdEdep3=$(python <<EOP
245 import math
246 if ($edep1 == 0 or $sqEdep1 == 0):
247   print(1.0)
248 else:
249   temp=math.sqrt(($sqEdep1/$nbPrimaryEdep1-pow($edep1/$nbPrimaryEdep1, 2))/($nbPrimaryEdep1-1))/($edep1/$nbPrimaryEdep1)
250   print(temp)
251 EOP
252     )
253     stdEdep3=${stdEdep3/#./0.}
254     file3=$(echo $file3 |awk -v r=${stdEdep3} '{$4=r}1')
255
256     # Divide uncertainty and replace it in the file3
257     dose1=$(echo ${file1} | cut -f6 -d' ')
258     nbPrimaryDose1=$(echo ${file1} | cut -f7 -d' ')
259     sqDose1=$(echo ${file1} | cut -f8 -d' ')
260     stdDose3=$(python <<EOP
261 import math
262 if ($edep1 == 0 or $sqEdep1 == 0):
263   print(1.0)
264 else:
265   temp=math.sqrt(($sqDose1/$nbPrimaryDose1-pow($dose1/$nbPrimaryDose1, 2))/($nbPrimaryDose1-1))/($dose1/$nbPrimaryDose1)
266   print(temp)
267 EOP
268     )
269     stdDose3=${stdDose3/#./0.}
270     file3=$(echo $file3 |awk -v r=${stdDose3} '{$7=r}1')
271
272     #Write the output
273     echo "${file3}" >> ${TMP}
274   done
275   mv -f ${TMP} ${RESULT}
276 }
277