forked from CMSROMA/Utilities
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fastHadd.sh
executable file
·74 lines (54 loc) · 1.65 KB
/
fastHadd.sh
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
#!/bin/bash
# this is to kill with control+C also the subshells
#trap '[[ -z "$(jobs -p)" ]] || kill $(jobs -p)' EXIT
ID=`uuidgen -r | cut -d '-' -f 2`
outFile=$1
mkdir -p `dirname $outFile`
tmpDir=/tmp/$USER/tmpHadd/`dirname $1`/`basename $2 .list`-$ID # this is the base path
tmpList=$tmpDir/filelist.list
echo "tmpDir=$tmpDir"
echo "tmpList=$tmpList"
mkdir -p $tmpDir
cp $2 $tmpList
cd $tmpDir
nFiles=5
iterNumber=0
while [ "`wc -l $tmpList |cut -d ' ' -f 1`" != "1" ];
do
let iterNumber=$iterNumber+1
let nFiles=$nFiles*$iterNumber # merge a larger number of files at each iteration
echo "############################## ITER: $iterNumber : $nFiles : `wc -l $tmpList`"
iterDir=${tmpDir}/$iterNumber
mkdir -p ${iterDir}/tmp
cd ${iterDir}
# make sub-lists with the files, splitting the original list by $nFiles
prefix=split_${iterNumber}_
cd tmp/
split -a 3 -l $nFiles -d $tmpList ${prefix}
cd ../
# make the hadd for each sub-list in parallel
for splitList in tmp/${prefix}[0-9][0-9][0-9]
do
# echo "------------------------------ $splitList"
iterFile=${iterDir}/`basename $splitList`.root
(nice -18 hadd $iterFile `cat $splitList` &> /dev/null || touch failed) &
joblist=($(jobs -p))
while (( ${#joblist[*]} >= 20 )); do sleep 1; joblist=($(jobs -p)); done
# echo $iterFile
# cat $splitList
# echo $!
# pids="$pids $!"
done
wait # wait that all the hadds are finished
if [ -e "failed" ];then
rm ${tmpDir}/* -Rf
exit 1
fi
cd $tmpDir
# echo $pids
# update the list of files to be merged with the temporary sub-merged files
ls ${iterDir}/*.root > $tmpList
# cat $tmpList
done
cp `cat $tmpList` $outFile
rm ${tmpDir}/* -Rf