Crombie Tools
dojob.sh
Go to the documentation of this file.
1 #!/bin/bash
2 
3 ##
4 # @file: dojob.sh
5 #
6 # This is the script that is run for each LXBATCH or Condor job run by CrombieTools.
7 # The only argument is the filename that will be created.
8 # Any other configuration is done in [CrombieSlimmingConfig.sh](@ref slimming).
9 #
10 # @author: Daniel Abercrombie <dabercro@mit.edu>
11 #
12 
13 outFile=$1 # The only argument is the output file (no extension)
14 
15 env
16 
17 if [ "$LSB_JOBID" != "" ] # Do some automatic setup if on LSB
18 then
19 
20  macroDir=$LS_SUBCWD # Get the configuration from the submission directory
21 
22  source $macroDir/CrombieSlimmingConfig.sh
23 
24  ERRORLOG=$macroDir/LxbatchFileChecks.log
25 
26 
27  if [ "$CMSSW_BASE" != "" ] # If CMSSW_BASE filled, scram it
28  then
29 
30  cd $CMSSW_BASE/src
31  eval `scram runtime -sh`
32  cd -
33 
34  fi
35 
36  # Copy all files from $CrombieJobScriptList to local directories
37 
38  for file in `cat $macroDir/$CrombieJobScriptList`
39  do
40 
41  if [ "$file" != "${file/\//}" ] # If the file has directories
42  then # make the directory for it to live in
43 
44  making=${file%%/${file##*/}}
45 
46  if [ ! -d $making ]
47  then
48 
49  mkdir -p $making
50 
51  fi
52  fi
53 
54  cp $macroDir/$file $file # Copy file to local location
55 
56  done
57 
58  cp $outFile.txt . # Copy the list of files for this file to local spot
59 
60 else # Condor has alternative setup
61 
62  ERRORLOG=condor.err
63 
64  tar -xzvf condor_package.tar.gz # Open the package
65 
66  export X509_USER_PROXY=`pwd`/voms_proxy.cert # Export location of my proxy
67  source CrombieSubmitConfig.sh # Get the configuration
68 
69  export SCRAM_ARCH=$CrombieScram # Get CMSSW setup from CVMFS
70  export VO_CMS_SW_DIR=/cvmfs/cms.cern.ch
71  source $VO_CMS_SW_DIR/cmsset_default.sh
72 
73  scramv1 project CMSSW $CrombieRelease
74  cd $CrombieRelease
75 
76  if [ -f ../cmssw_patch.tgz ] # If we have a personal patch to CMSSW, apply it
77  then
78 
79  mv ../cmssw_patch.tgz .
80  tar -xzvf cmssw_patch.tgz
81 
82  fi
83 
84  eval `scramv1 runtime -sh`
85  cd ..
86 
87  if [ -f crombie.tgz ] # Install CrombieTools if present
88  then
89 
90  mkdir CrombieTools
91  mv crombie.tgz CrombieTools/.
92  cd CrombieTools
93  tar -xzvf crombie.tgz
94  export CROMBIEPATH=`pwd`
95  export PATH=`pwd`/bin:$PATH
96  export PYTHONPATH=`pwd`/python:$PYTHONPATH
97  cd ..
98 
99  fi
100 
101  # Get the input file out
102 
103  set -- `tar -ztf input_files.tar.gz | sort`
104  tar -xzvf input_files.tar.gz ${!outFile}
105  outFile=${!outFile}
106  outFile=${outFile%%.txt}
107 
108  for file in `ls *.tgz` # Finally extract all the other files
109  do
110 
111  tar -xzvf $file
112 
113  done
114 
115  export PATH=`pwd`:$PATH # Make sure executables here are in the path
116 
117 fi
118 
119 echo "Trying to make $outFile.root"
120 echo ""
121 
122 NUM=0 # Count the subfiles made
123 
124 $CrombieSlimmerScript # Compile the slimmer script
125 
126 if [ $? -ne 0 ] # Check that compilation succeeded
127 then
128  echo "Compilation failed. Submit again..."
129  exit 0
130 fi
131 
132 OutputBase="lxbatchTmpOutput" # Some default names
133 CommandList="ArgsForThisJob.txt" # These variables aren't too important
134 > $CommandList # Initialize command list
135 
136 for file in `cat $outFile.txt` # Dump name of each input file into argument list
137 do
138 
139  echo "Getting $file"
140  xrdcp $file .
141  echoCommand="echo ${file##*/} $OutputBase\_$NUM.root"
142 
143  $echoCommand # Debugging
144  $echoCommand >> $CommandList # Put into text file
145  NUM=$((NUM + 1))
146 
147 done
148 
149 # Run the slimmer in parallel using xargs
150 
151 cat $CommandList | xargs -n2 -P$LSB_MAX_NUM_PROCESSORS $CrombieSlimmerScript
152 
153 # Hadd the results together
154 
155 hadd $OutputBase.root $OutputBase\_*.root
156 
157 FATALERROR=0
158 ALLEMPTY=1
159 FINALEMPTY=0
160 
161 for file in `ls $OutputBase*.root` # Now check for errors
162 do
163 
164  "$CrombieCheckerScript" $file # Check, with exit code 5 for fatal
165 
166  if [ $? -ne 0 ] # If found error
167  then
168 
169  if [ $? -eq 5 ] # Check for fatal error
170  then
171 
172  echo "Could not find acceptable output in $file" >> $ERRORLOG
173  echo "Check output in job bout/out.$LSB_JOBID" >> $ERRORLOG
174  echo "" >> $ERRORLOG
175  FATALERROR=1
176 
177  elif [ "$file" = "$OutputBase.root" ] # If the final output is not fatal, make a note
178  then
179 
180  FINALEMPTY=1
181 
182  fi
183 
184  else
185 
186  ALLEMPTY=0 # Note whether or not everything is empty
187 
188  fi
189 
190 done
191 
192 if [ $FINALEMPTY -eq 1 ] # If the final is empty and not everything else is
193 then # that's bad and should be a fatal error.
194  # If everything is empty, that's okay.
195  if [ $ALLEMPTY -eq 0 ] # Copy a placeholder over anyway.
196  then
197 
198  echo "Final output is empty, but not all are." >> $ERRORLOG
199  echo "Check output in job bout/out.$LSB_JOBID" >> $ERRORLOG
200  echo "" >> $ERRORLOG
201  FATALERROR=1
202 
203  else
204 
205  echo "Everything is empty in this job, copying anyway." >> $ERRORLOG
206  echo "Check output in job bout/out.$LSB_JOBID" >> $ERRORLOG
207  echo "" >> $ERRORLOG
208 
209  fi
210 fi
211 
212 if [ $FATALERROR -eq 1 ] # For fatal error, report to user and quit quietly
213 then
214 
215  echo "FATAL ERROR in bout/out.$LSB_JOBID" >> $ERRORLOG
216  echo "" >> $ERRORLOG
217 
218  if [ -f condor.err ] # Let condor handle the errors for us
219  then
220 
221  cat condor.err >&2
222 
223  fi
224 
225  exit 0
226 
227 fi
228 
229 echo ""
230 
231 if [ "$LSB_JOBID" = "" ]
232 then
233 
234  destination=$CrombieTempDir/${outFile%%_[0-9]*}/$outFile.root
235  echo "Copying to $destination!"
236  lcg-cp -v -D srmv2 -b file://$PWD/$OutputBase.root \
237  srm://t3serv006.mit.edu:8443/srm/v2/server\?SFN=$destination
238 
239 else
240 
241  echo "Copying to $outFile.root"
242  cp $OutputBase.root $outFile.root # Try to copy
243 
244 fi
245 
246 if [ $? -ne 0 ] # Check for success
247 then
248 
249  echo "Copying failed! Check your quota." >> $ERRORLOG
250  echo "Check output in job bout/out*$LSB_JOBID.log" >> $ERRORLOG
251  echo "" >> $ERRORLOG
252 
253 fi
254 
255 if [ -f condor.err ] # Let condor handle the errors for us
256 then
257 
258  cat condor.err >&2
259 
260 fi
261 
262 exit 0