594d338f |
1 | #!/bin/bash |
2 | |
3 | |
4 | function usage { |
5 | echo "Usage:" |
6 | echo "makeCalibTree [options]" |
7 | echo |
8 | echo "options:" |
9 | echo " -r, --range : create tree for the given range of runs" |
10 | echo " e.g. 70000-80000" |
11 | echo " -a, --auto : create trees in chunks of \$NFILES for all the files" |
12 | echo " found in the OCDB entries of \$AUTOFILES" |
13 | echo " -l, --list : text file with a list of runs to process" |
14 | echo " -d, --outdir : output file directory" |
15 | echo " -f, --outfile : output file name" |
16 | echo " -b, --batch : run as batch job(s)" |
17 | echo " -o, --overwrite : overwrite old files" |
18 | echo " -h, --help : display this help" |
19 | echo "" |
20 | exit 0 |
21 | |
22 | } |
23 | |
24 | # |
25 | # create a list of files in $RANGE which are also available in $AUTOFILES |
26 | # |
27 | function createRangeList { |
28 | #get first and last run from range |
29 | local first=$(echo $RANGE | sed 's|\(.*\)-.*|\1|') |
30 | local last=$(echo $RANGE | sed 's|.*-\(.*\)|\1|') |
31 | #create list with all TPC runs |
32 | local command=ls |
33 | if [ $WITHALIEN -eq 1 ]; then command=alien_ls; fi |
34 | local dir=$AUTOFILES |
909c8c99 |
35 | RUNS=($($command $dir | sed 's|Run\([0-9]\{1,6\}\)_.*|\1|' | sort -n)) |
594d338f |
36 | #define filename |
37 | local filename=calibTreeTime_${first}_${last} |
38 | #remove old files |
39 | #test -d $TMPLISTDIR && rm -rf $TMPLISTDIR |
40 | test -d $TMPLISTDIR |
41 | mkdir -p $TMPLISTDIR |
42 | for (( i=0; i<${#RUNS[*]}; i=i+1 )); do |
43 | if [ ${RUNS[$i]} -lt $first ]||[ ${RUNS[$i]} -gt $last ]; then continue; fi |
44 | echo ${RUNS[$i]} >> $TMPLISTDIR/$filename |
45 | done |
46 | } |
47 | |
48 | # |
49 | # automatic list creation for runs in files in OCDB entry pointed to by $AUTOFILES |
50 | # crete as many lists as needed in chunks of $NFILES files |
51 | # respect the run range, if given |
52 | # |
53 | function createGuiLists { |
54 | local command=ls |
55 | if [ $WITHALIEN -eq 1 ]; then command=alien_ls; fi |
56 | local dir=$AUTOFILES |
909c8c99 |
57 | RUNS=($($command $dir | sed 's|Run\([0-9]\{1,6\}\)_.*|\1|' | sort -n)) |
594d338f |
58 | local count=0 |
59 | local iter=0 |
60 | local skip=0 |
61 | local filename= |
62 | local nfiles=$NFILES |
63 | #find first and last iterator |
64 | #get first and last run from range |
65 | local first=0 |
66 | local last=999999999 |
67 | local ifirst=0 |
68 | local ilast=${#RUNS[*]} |
69 | if [ "x$RANGE" != "x" ]; then |
70 | first=$(echo $RANGE | sed 's|\(.*\)-.*|\1|') |
71 | last=$(echo $RANGE | sed 's|.*-\(.*\)|\1|') |
909c8c99 |
72 | echo $first-$last |
594d338f |
73 | for (( i=0; i<${#RUNS[*]}; i=i+1 )); do |
74 | if [ ${RUNS[i]} -ge $first ]&&[ $ifirst -eq 0 ]; then ifirst=$i; fi |
75 | if [ ${RUNS[i]} -gt $last ]&&[ $ilast -eq ${#RUNS[*]} ]; then ilast=$i; fi |
76 | done |
909c8c99 |
77 | echo $ifirst-$ilast |
78 | echo ${RUNS[$ifirst]}-${RUNS[$ilast-1]} |
594d338f |
79 | fi |
80 | #remove old files |
81 | #test -d $TMPLISTDIR && rm -rf $TMPLISTDIR |
82 | test -d $TMPLISTDIR |
83 | mkdir -p $TMPLISTDIR |
84 | for (( i=$ifirst; i<$ilast; i=i+1 )); do |
85 | if [ $count -eq $nfiles ]; then |
86 | count=0 |
87 | let iter=$iter+1 |
88 | fi |
89 | if [ $count -eq 0 ]; then |
90 | local firstLocal=$(echo "$iter*$nfiles+$ifirst" | bc) |
909c8c99 |
91 | # local lastLocal=$(echo "if ($firstLocal+$nfiles-1>=$ilast-$ifirst) $ilast-1 else $firstLocal+$nfiles-1" | bc) |
92 | local lastLocal=$(echo "if (($iter+1)*$nfiles>=$ilast-$ifirst) $ilast-1 else $firstLocal+$nfiles-1" | bc) |
93 | filename=calibTreeTime_${RUNS[$firstLocal]}_${RUNS[$lastLocal]} |
594d338f |
94 | fi |
95 | echo ${RUNS[$i]} >> $TMPLISTDIR/$filename |
96 | let count=$count+1 |
97 | done |
98 | } |
99 | |
100 | function runJobs { |
101 | local count=0; |
102 | local filename= |
103 | for file in $TMPLISTDIR/*; do |
104 | if [ "x$OUTFILE" == "x" ]; then |
105 | filename=$(basename $file).root |
106 | else |
107 | filename=${OUTFILE}_${count} |
108 | fi |
109 | if [ $OVERWRITE -eq 0 ];then |
110 | test -f $OUTDIR/$filename && continue |
111 | fi |
112 | if [ $BATCH -eq 0 ]; then |
113 | $SCRIPTDIR/makeCalibTree -l $file -f $filename -d $OUTDIR |
114 | else |
115 | $SCRIPTDIR/makeCalibTree -l $file -f $filename -d $OUTDIR -b |
116 | fi |
117 | let count=$count+1 |
118 | done |
119 | } |
120 | |
121 | function runJobOnList { |
122 | if [ $OVERWRITE -eq 0 ];then |
123 | if [ -f $OUTDIR/$OUTFILE ]; then |
124 | echo "Not overwriting file '$OUTDIR/$OUTFILE' use -o to enforce it" |
125 | exit 0; |
126 | fi |
127 | fi |
128 | if [ $BATCH -eq 1 ]; then |
129 | $BATCHCOMMAND -oo $GUI_OUTDIR/logs/$OUTFILE.out.log -eo $GUI_OUTDIR/logs/$OUTFILE.err.log /$SCRIPTDIR/makeCalibTreeList $LISTFILE $OUTDIR/$OUTFILE $SCRIPTDIR |
130 | else |
131 | $SCRIPTDIR/makeCalibTreeList $LISTFILE $OUTDIR/$OUTFILE $SCRIPTDIR |
132 | fi |
133 | } |
134 | |
135 | if [ "x$SCRIPTDIR" == "x" ]; then |
136 | echo please run: |
137 | echo export SCRIPTDIR=whatever_is_you_path |
138 | echo |
139 | exit 1 |
140 | fi |
141 | source $SCRIPTDIR/guiEnv.sh |
142 | |
143 | |
144 | #parse options |
145 | TEMP=`getopt -o r:l:f:d:aboh --long range:,list:,calibDir:,outfile:,outdir:,auto,batch,overwrite,help \ |
146 | -n 'run' -- "$@"` |
147 | |
148 | if [ $? != 0 ] ; then usage ; fi |
149 | |
150 | eval set -- "$TEMP" |
151 | |
152 | RANGE= |
153 | OUTFILE= |
154 | OVERWRITE=0 |
155 | LISTFILE= |
156 | BATCH=0 |
157 | COMMAND= |
158 | AUTO=0 |
159 | OUTDIR=$GUI_OUTDIR_TIME |
160 | |
161 | while true ; do |
162 | case "$1" in |
163 | -r|--range) RANGE=$2; shift 2;; |
164 | -l|--list) LISTFILE=$2; shift 2;; |
165 | -f|--outfile) OUTFILE=$2; shift 2;; |
166 | -d|--outdir) OUTDIR=$2; shift 2;; |
167 | -a|--auto) AUTO=1; shift;; |
168 | -b|--batch) BATCH=1; shift;; |
169 | -o|--overwrite) OVERWRITE=1; shift;; |
170 | -h|--help) usage;; |
171 | --) shift ; break ;; |
172 | *) echo "Internal error!" ; exit 1 ;; |
173 | esac |
174 | done |
175 | |
176 | |
177 | |
178 | if [ $AUTO -eq 1 ]; then |
179 | test -d $OUTDIR || mkdir -p $OUTDIR |
180 | #remove last file if it does not contain $NFILES files |
181 | lastfile=$(ls $OUTDIR/* | tail -1) |
182 | if [ "x$lastfile" != "x" ]; then |
183 | nfiles=$(wc -l $lastfile | awk '{print $1}') |
184 | if [ $nfiles -ne $NFILES ]; then |
185 | rm $lastfile |
186 | fi |
187 | fi |
188 | createGuiLists |
189 | runJobs |
190 | echo "files written to '$OUTDIR'" |
191 | exit 0 |
192 | fi |
193 | |
194 | if [ "x$RANGE" != "x" ]&&[ "x$LISTFILE" == "x" ]; then |
195 | test -d $OUTDIR || mkdir -p $OUTDIR |
196 | createRangeList |
197 | runJobs |
198 | exit 0 |
199 | fi |
200 | |
201 | if [ "x$RANGE" == "x" ]&&[ "x$LISTFILE" != "x" ]; then |
202 | test -d $OUTDIR || mkdir -p $OUTDIR |
203 | runJobOnList |
204 | exit 0 |
205 | fi |
206 | |
207 | #we should have left already, so show usage |
208 | usage |