From 88759fd450cc61a1b6818f80548c8632b2a3ffea Mon Sep 17 00:00:00 2001 From: tbaudier Date: Fri, 9 Nov 2018 09:49:28 +0100 Subject: [PATCH] Modify mergeDoseByRegion Before, it summed 2 files to write the result. But I had to modify it to be able to sum partial std. So I created 3 functions: -addToPartialResult with the previous code - addWithoutPartialResult for the first file - divideUncertaintyResult for the last step --- cluster_tools/gate_power_merge.sh | 6 +- cluster_tools/mergeDoseByRegions.sh | 338 ++++++++++++++++++++-------- 2 files changed, 251 insertions(+), 93 deletions(-) diff --git a/cluster_tools/gate_power_merge.sh b/cluster_tools/gate_power_merge.sh index 8fa4cfc..bc3b3e0 100755 --- a/cluster_tools/gate_power_merge.sh +++ b/cluster_tools/gate_power_merge.sh @@ -207,6 +207,7 @@ function merge_doseByRegions { echo " ${indent}creating ${merged}" local count=0 start_bar $# + source ${doseMerger} while test $# -gt 0 do local partial="$1" @@ -216,13 +217,14 @@ function merge_doseByRegions { if test ! -f "${merged}" then update_bar ${count} "copying first partial result ${partial}" - cp "${partial}" "${merged}" + addWithoutPartialResult -i "${partial}" -o "${merged}" continue fi update_bar ${count} "adding ${partial}" - ${doseMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${doseMerger}" + addToPartialResult -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${doseMerger}" done + divideUncertaintyResult -i "${merged}" -j "${count}" -o "${merged}" end_bar echo " ${indent}merged ${count} files" } diff --git a/cluster_tools/mergeDoseByRegions.sh b/cluster_tools/mergeDoseByRegions.sh index f6a438c..0548db6 100755 --- a/cluster_tools/mergeDoseByRegions.sh +++ b/cluster_tools/mergeDoseByRegions.sh @@ -2,102 +2,258 @@ set -u function usage { - echo "$0 -i -j -o " + echo "$0 -i -j -o " exit 1 } -if [ $# != 6 ] -then - usage -fi +function addToPartialResult { + IN1=$2 + IN2=$4 + RESULT=$6 -IN1=$2 -IN2=$4 -RESULT=$6 + test -f ${IN1} && test -f ${IN2} || usage -test -f ${IN1} && test -f ${IN2} || usage + TMP="$(mktemp)" -TMP="$(mktemp)" -echo "merging dose by regions file" -# check if all 3 text files have the same number of lines -nblines=`cat ${IN1} | wc -l` -nb2=`cat ${IN2} | wc -l` -nb3=`cat ${RESULT} | wc -l` + # check if all 3 text files have the same number of lines + nblines=`cat ${IN1} | wc -l` + nb2=`cat ${IN2} | wc -l` + nb3=`cat ${RESULT} | wc -l` -if [ $nblines -ne $nb2 ] || [ $nblines -ne $nb3 ]; then - echo "Files does not have the same size" - exit 1 -fi - -#Copy the 1st line in output -line1=`sed -n "1p" < ${RESULT}` -echo "${line1}" >> ${TMP} - -# for all lines (except the 1st), split according tab -# sum the some elements (dose, edep) ... -for i in $(seq 2 $nblines); do - #Get the 3 lines - line1=`sed -n "${i}p" < ${IN1}` - line2=`sed -n "${i}p" < ${IN2}` - line3=`sed -n "${i}p" < ${RESULT}` - - # sum edep: get the 2 values, sum them and replace it in the line3 - # The /#./0. is important to add 0 for decimal value between 0 and 1 - edep1=$(echo ${line1} | cut -f3 -d' ') - edep2=$(echo ${line2} | cut -f3 -d' ') - edep3=$(python -c "print($edep1+$edep2)") - edep3=${edep3/#./0.} - line3=$(echo $line3 |awk -v r=${edep3} '{$3=r}1') - - # sqrt sum square std_edep: get the 2 values, sum the square, take the sqrt and replace it in the line3 - edep1=$(echo ${line1} | cut -f4 -d' ') - edep2=$(echo ${line2} | cut -f4 -d' ') - edep3=$(python -c "import math; print(math.sqrt($edep1*$edep1+$edep2*$edep2))") - edep3=${edep3/#./0.} - line3=$(echo $line3 |awk -v r=${edep3} '{$4=r}1') - - # sum square_edep: get the 2 values, sum them and replace it in the line3 - edep1=$(echo ${line1} | cut -f5 -d' ') - edep2=$(echo ${line2} | cut -f5 -d' ') - edep3=$(python -c "print($edep1+$edep2)") - edep3=${edep3/#./0.} - line3=$(echo $line3 |awk -v r=${edep3} '{$5=r}1') - - # sum dose: get the 2 values, sum them and replace it in the line3 - edep1=$(echo ${line1} | cut -f6 -d' ') - edep2=$(echo ${line2} | cut -f6 -d' ') - edep3=$(python -c "print($edep1+$edep2)") - edep3=${edep3/#./0.} - line3=$(echo $line3 |awk -v r=${edep3} '{$6=r}1') - - # sqrt sum square std_dose: get the 2 values, sum the square, take the sqrt and replace it in the line3 - edep1=$(echo ${line1} | cut -f7 -d' ') - edep2=$(echo ${line2} | cut -f7 -d' ') - edep3=$(python -c "import math; print(math.sqrt($edep1*$edep1+$edep2*$edep2))") - edep3=${edep3/#./0.} - line3=$(echo $line3 |awk -v r=${edep3} '{$7=r}1') - - # sum square_dose: get the 2 values, sum them and replace it in the line3 - edep1=$(echo ${line1} | cut -f8 -d' ') - edep2=$(echo ${line2} | cut -f8 -d' ') - edep3=$(python -c "print($edep1+$edep2)") - edep3=${edep3/#./0.} - line3=$(echo $line3 |awk -v r=${edep3} '{$8=r}1') - - # sum n_hits: get the 2 values, sum them and replace it in the line3 - edep1=$(echo ${line1} | cut -f9 -d' ') - edep2=$(echo ${line2} | cut -f9 -d' ') - edep3=$(python -c "print($edep1+$edep2)") - line3=$(echo $line3 |awk -v r=${edep3} '{$9=r}1') - - # sum n_event_hits: get the 2 values, sum them and replace it in the line3 - edep1=$(echo ${line1} | cut -f10 -d' ') - edep2=$(echo ${line2} | cut -f10 -d' ') - edep3=$(python -c "print($edep1+$edep2)") - line3=$(echo $line3 |awk -v r=${edep3} '{$10=r}1') - - #Write the output - echo "${line3}" >> ${TMP} -done -mv -f ${TMP} ${RESULT} + if [ $nblines -ne $nb2 ] || [ $nblines -ne $nb3 ]; then + echo "Files does not have the same size" + exit 1 + fi + + #Copy the 1st line in output + line1=`sed -n "1p" < ${RESULT}` + echo "${line1}" >> ${TMP} + # for all lines (except the 1st), split according tab + # sum the some elements (dose, edep) ... + for i in $(seq 2 $nblines); do + #Get the 3 lines + file1=`sed -n "${i}p" < ${IN1}` + file2=`sed -n "${i}p" < ${IN2}` + file3=`sed -n "${i}p" < ${RESULT}` + + # sum edep: get the 2 values, sum them and replace it in the file3 + # The /#./0. is important to add 0 for decimal value between 0 and 1 + edep1=$(echo ${file1} | cut -f3 -d' ') + edep2=$(echo ${file2} | cut -f3 -d' ') + edep3=$(python -c "print($edep1+$edep2)") + edep3=${edep3/#./0.} + file3=$(echo $file3 |awk -v r=${edep3} '{$3=r}1') + + # sqrt sum square std_edep*edep: get the 2 values, sum the square, take the sqrt and replace it in the file3 + stdEdep1=$(echo ${file1} | cut -f4 -d' ') + stdEdep2=$(echo ${file2} | cut -f4 -d' ') + stdEdep3=$(python <> ${TMP} + done + mv -f ${TMP} ${RESULT} +} + +function addWithoutPartialResult { + IN1=$2 + RESULT=$4 + + test -f ${IN1} || usage + + TMP="$(mktemp)" + + nblines=`cat ${IN1} | wc -l` + + #Copy the 1st line in output + line1=`sed -n "1p" < ${IN1}` + echo "${line1}" >> ${TMP} + # for all lines (except the 1st), split according tab + # sum the some elements (dose, edep) ... + for i in $(seq 2 $nblines); do + #Get the lines + file1=`sed -n "${i}p" < ${IN1}` + + # copy id + id1=$(echo ${file1} | cut -f1 -d' ') + id1=${id1/#./0.} + file3=$(echo $id1) + + # copy volume + vol1=$(echo ${file1} | cut -f2 -d' ') + vol1=${vol1/#./0.} + file3=$(echo "$file3 $vol1") + + # copy edep + edep1=$(echo ${file1} | cut -f3 -d' ') + edep1=${edep1/#./0.} + file3=$(echo "$file3 $edep1") + + # sqrt sum square std_edep*edep + stdEdep1=$(echo ${file1} | cut -f4 -d' ') + stdEdep3=$(python <> ${TMP} + done + mv -f ${TMP} ${RESULT} +} + + +function divideUncertaintyResult { + IN1=$2 + value=$4 + RESULT=$6 + + TMP="$(mktemp)" + + # check if all 3 text files have the same number of lines + nblines=`cat ${IN1} | wc -l` + nb3=`cat ${RESULT} | wc -l` + + if [ $nblines -ne $nb3 ]; then + echo "Files does not have the same size" + exit 1 + fi + + #Copy the 1st line in output + line1=`sed -n "1p" < ${RESULT}` + echo "${line1}" >> ${TMP} + # for all lines (except the 1st), split according tab + # sum the some elements (dose, edep) ... + for i in $(seq 2 $nblines); do + #Get the lines + file1=`sed -n "${i}p" < ${IN1}` + file3=`sed -n "${i}p" < ${RESULT}` + + # Divide uncertainty and replace it in the file3 + edep1=$(echo ${file1} | cut -f3 -d' ') + stdEdep1=$(echo ${file1} | cut -f4 -d' ') + stdEdep3=$(python <> ${TMP} + done + mv -f ${TMP} ${RESULT} +} -- 2.45.0