Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:14:12

0001 #!/bin/bash
0002 #
0003 #  file:        MakeSherpaLibs.sh
0004 #  description: BASH script handling the SHERPA data cards for the
0005 #               library production and cross section calculation
0006 #  uses:        the required SHERPA data cards (+ libraries) [see below]
0007 #
0008 #  author:      Markus Merschmeyer, Philipp Millet, Sebastian Thueer, RWTH Aachen
0009 #  date:        5th Nov 2015
0010 #  version:     4.4
0011 #
0012 
0013 set +o posix
0014 
0015 
0016 # +-----------------------------------------------------------------------------------------------+
0017 # function definitions
0018 # +-----------------------------------------------------------------------------------------------+
0019 
0020 print_help() {
0021     echo "" && \
0022     echo "MakeSherpaLibs version 4.4" && echo && \
0023     echo "options: -d  path       (optional) path to your SHERPA installation (otherwise the SHERPA" && \
0024     echo "                         package belonging to the release under '\$CMSSW_BASE' is used)" && \
0025     echo "                         -> ( "${shr}" )" && \
0026     echo "         -i  path       path to SHERPA datacard (and library, see -o) files" && \
0027     echo "                         -> ( "${inc}" )" && \
0028     echo "         -p  process    SHERPA process/dataset name ( "${prc}" )" && \
0029     echo "         -o  option     library/cross section options ( "${lbo}" )" && \
0030     echo "                         [ 'LBCR' : generate libraries and cross sections     ]" && \
0031     echo "                         [ 'LIBS' : generate libraries only                   ]" && \
0032     echo "                         [ 'CRSS' : generate cross sections, needs libraries! ]" && \
0033     echo "                         [ 'EVTS' : generate events, needs libs + crss. sec.! ]" && \
0034     echo "         -f  path       output path for SHERPA library & cross section files" && \
0035     echo "                         -> ( "${fin}" )" && \
0036     echo "         -D  filename   (optional) name of data card file ( "${cfdc}" )" && \
0037     echo "         -L  filename   (optional) name of library file ( "${cflb}" )" && \
0038     echo "         -C  filename   (optional) name of cross section file ( "${cfcr}" )" && \
0039     echo "         -A             switch on multiple interactions in Run.dat card ( "${FLGAMISIC}" )" && \
0040     echo "         -v             verbose mode ( "${verbose}" )" && \
0041     echo "         -T             disable library compilation in multithreading mode ( "${nomultithread}" )"  && \
0042     echo "         -m  command    enable running in MPI mode with command ( "${ML_MPICMD}" )" && \
0043     echo "         -M  option     additional options for MPI command: ( "${ML_MPIOPT}" )" && \
0044     echo "         -e  # evts.    number of events to be produced ( "${NEVTS}" )" && \
0045     echo "         -h             display this help and exit" && \
0046     echo ""
0047 }
0048 
0049 check_occurence() {
0050 # $1: name of a text file
0051 # $2: string to search in file named $1
0052 # returns: number of occurences of string $2 in file $1
0053   if [ -e $1 ]; then
0054     cnt=0
0055     if [ $# -eq 3 ]; then
0056       cnt=`cat $1 | grep -i $2 | grep -i -c $3`
0057     else
0058       cnt=`cat $1 | grep -i $2 | grep -i -c $2`
0059     fi
0060     if [ $cnt -gt 0 ]; then
0061       echo 1
0062     else
0063       echo 0
0064     fi
0065   else
0066     echo " <E> file "$1" not found!"
0067   fi
0068 }
0069 
0070 clean_libs() {
0071   DIRS=`find Process -name P?_?`" "`find Process -name P?_??`
0072   BASEDIR=`pwd`
0073   for J in $DIRS ; do
0074     echo "."
0075     echo "======================"
0076     echo "$J";
0077     echo "======================"
0078     cd $J
0079 #   make clean
0080     rm config* Makefile*
0081 #    rm *.tex
0082 #   rm aclocal.m4 ChangeLog depcomp install-sh libtool ltmain.sh missing
0083     rm aclocal.m4 ChangeLog depcomp install-sh ltmain.sh missing
0084     rm AUTHORS COPYING INSTALL NEWS README 
0085     rm -rf autom4te.cache
0086     find ./ -type f -name 'Makefile*' -exec rm -rf {} \;
0087     find ./ -type d -name '.deps'     -exec rm -rf {} \;
0088     find ./ -type f -name '*.C'       -exec rm -rf {} \;
0089     find ./ -type f -name '*.H'       -exec rm -rf {} \;
0090     cd $BASEDIR
0091   done
0092 }
0093 
0094 
0095 
0096 
0097 # +-----------------------------------------------------------------------------------------------+
0098 # start of the script
0099 # +-----------------------------------------------------------------------------------------------+
0100 
0101 # save current path
0102 HDIR=`pwd`
0103 
0104 # dummy setup (if all options are missing)
0105 shr=${HDIR}/SHERPA_1.4.2        # path to SHERPA installation
0106 scrloc=`which scramv1 &> tmp.tmp; cat tmp.tmp | cut -f1 -d"/"; rm tmp.tmp`
0107 if [ "${scrloc}" = "" ]; then
0108   shr=`scramv1 tool info sherpa | grep "SHERPA_BASE" | cut -f2 -d"="`
0109   shrinit=$shr
0110 fi
0111 pth="TMP"                          # name of SHERPA data card directory
0112 prc="XXX"                          # SHERPA process name
0113 lbo="LIBS"                         # library/cross section option
0114 inc=${HDIR}                        # path to SHERPA datacards (libraries)
0115 cfdc=""                            # custom data card file name
0116 cflb=""                            # custom library file name
0117 cfcr=""                            # custom cross section file name
0118 fin=${HDIR}                        # output path for SHERPA libraries & cross sections
0119 FLGAMISIC="FALSE"                  # switch on multiple interactions for production
0120 FLGAMEGIC="FALSE"                  # flag to indicate the usage of AMEGIC -> library compilation required
0121 verbose="FALSE"                    # controls verbose mode
0122 nomultithread="FALSE"              # disables multithread mode of Sherpa library compilation
0123 ML_MPICMD=""                       # standard MPI running command
0124 ML_MPIOPT=""                       # additional MPI options
0125 NEVTS=0                            # number of events to be produced
0126 
0127 
0128 # get & evaluate options
0129 while getopts :d:i:p:o:f:m:e:D:L:C:M:AhvT OPT
0130 do
0131   case $OPT in
0132   d) shr=$OPTARG ;;
0133   i) inc=$OPTARG ;;
0134   p) prc=$OPTARG ;;
0135   o) lbo=$OPTARG ;;
0136   f) fin=$OPTARG ;;
0137   D) cfdc=$OPTARG ;;
0138   L) cflb=$OPTARG ;;
0139   C) cfcr=$OPTARG ;;
0140   A) FLGAMISIC="TRUE" ;;
0141   v) verbose="TRUE" ;;
0142   T) nomultithread="TRUE" ;;
0143 #  m) ML_MPICMD="$OPTARG" ;;
0144   m) echo "XXX: "$OPTARG && ML_MPICMD=`echo $OPTARG` ;;
0145   M) ML_MPIOPT="$OPTARG "${ML_MPIOPT} ;;
0146   e) NEVTS=$OPTARG ;;
0147   h) print_help && exit 0 ;;
0148   \?)
0149     shift `expr $OPTIND - 1`
0150     if [ "$1" = "--help" ]; then print_help && exit 0;
0151     else 
0152       echo -n "MakeSherpaLibs: error: unrecognized option "
0153       if [ $OPTARG != "-" ]; then echo "'-$OPTARG'. try '-h'"
0154       else echo "'$1'. try '-h'"
0155       fi
0156       print_help && exit 1
0157     fi
0158     shift 1
0159     OPTIND=1
0160   esac
0161 done
0162 
0163 
0164 # make sure to use absolute path names...
0165 cd ${shr} && shr=`pwd`; cd ${HDIR}
0166 cd ${inc} && inc=`pwd`; cd ${HDIR}
0167 cd ${fin} && fin=`pwd`; cd ${HDIR}
0168 
0169 # test whether to take $shr from CMSSW installation
0170 if [ ! "$CMSSW_BASE" = "" ] && [ "$shr" = "$shrinit" ]; then
0171   newshr=""
0172   cd $CMSSW_BASE &&
0173   newshr=`scramv1 tool info sherpa | grep BASE | cut -f 2 -d "="`
0174   if [ "${newshr}" = "" ]; then
0175     echo " <E> no 'sherpa' tool defined in CMSSW, are you sure that"
0176     echo " <E>  1. the command 'scramv1' is available ?"
0177     echo " <E>  2. the path to your CMSSW is correct ?"
0178     echo " <E>  3. there exists a SHERPA package in your CMSSW ?"
0179     exit 0
0180   fi
0181   shr=${newshr}
0182   cd ${HDIR}
0183 fi
0184 if [ "${SHERPA_SHARE_PATH}"   = "" ]; then export SHERPA_SHARE_PATH=${shr}/share/SHERPA-MC;     fi
0185 if [ "${SHERPA_INCLUDE_PATH}" = "" ]; then export SHERPA_INCLUDE_PATH=${shr}/include/SHERPA-MC; fi
0186 if [ "${SHERPA_LIBRARY_PATH}" = "" ]; then export SHERPA_LIBRARY_PATH=${shr}/lib/SHERPA-MC;     fi
0187 
0188 
0189 # find 'Run' directory
0190 shrun=${HDIR}/SHERPATMP_${prc}
0191 mkdir -p ${shrun}
0192 pth=${shrun}/${pth} #SHERPA 1.3.0 needs full path, MN 070611
0193 
0194 echo "  -> SHERPA path: '"${shr}"'"
0195 echo "  -> SHERPA run path: '"${shrun}"'"
0196 echo "  -> PROCESS name: '"${prc}"'"
0197 echo "  -> Library & cross section otions: '"${lbo}"'"
0198 echo "  -> include path: '"${inc}"'"
0199 echo "  -> custom data card file name: '"${cfdc}"'"
0200 echo "  -> custom library file name: '"${cflb}"'"
0201 echo "  -> custom cross section file name: '"${cfcr}"'"
0202 echo "  -> output path: '"${fin}"'"
0203 echo "  -> MPI running command: '"${ML_MPICMD}"'"
0204 echo "  -> additional MPI options: '"${ML_MPIOPT}"'"
0205 echo "  -> No. of events: '"${NEVTS}"'"
0206 
0207 
0208 # get the number of CPU cores
0209 FLGMCORE="TRUE"
0210 POPTS=""
0211 nprc=1
0212 if [ "$FLGMCORE" == "TRUE" ]; then
0213     nprc=`cat /proc/cpuinfo | grep  -c processor`
0214     let nprc=$nprc+1
0215     if [ $nprc -gt 2 ]; then
0216       echo " <I> multiple CPU cores detected: "$nprc"-1"
0217       POPTS=" -j"$nprc" "
0218     fi
0219 fi
0220 
0221 # enable multithreading for library compilation
0222 multithread_opt="-j $((nprc-1))"
0223 if [ "$nomultithread" == "TRUE" ]; then
0224   multithread_opt=""
0225   POPTS=""
0226 fi
0227 
0228 
0229 ### go to 'Run' subdirectory of SHERPA
0230 cd ${shrun}
0231 
0232 
0233 ### set base name for SHERPA output file(s) and directories
0234 runfile="Run.dat"
0235 runcardfile=Run.dat_${prc}
0236 outflbs=sherpa_${prc}
0237 cardfile=${outflbs}_cards.tgz             # input card file (master -> libraries)
0238 if [ "${lbo}" = "CRSS" ]; then
0239   cardfile=${outflbs}_crdC.tgz
0240 elif [ "${lbo}" = "EVTS" ]; then
0241   cardfile=${outflbs}_crdE.tgz
0242 fi
0243 libsfile=${outflbs}_libs.tgz              # output libs
0244 crssfile=${outflbs}_crss.tgz              # output cross sections
0245 evtsfile=${outflbs}_evts.tgz              # output events
0246 if [ ! "${cfdc}" = "" ]; then
0247   cardfile=${cfdc}                        # custom input data card file
0248   echo " <I> using custom data card file: "${cardfile}
0249 fi
0250 if [ ! "${cflb}" = "" ]; then
0251   libsfile=${cflb}                        # custom input library file
0252   echo " <I> using custom library file: "${libsfile}
0253 fi
0254 if [ ! "${cfcr}" = "" ]; then
0255   crssfile=${cfcr}                        # custom input cross section file
0256   echo " <I> using custom cross section file: "${crssfile}
0257 fi
0258 crdlfile=${outflbs}_crdL.tgz              # output cardfile (-> from library production)
0259 crdcfile=${outflbs}_crdC.tgz              # output cardfile (-> from cross section calculation)
0260 crdefile=${outflbs}_crdE.tgz              # output cardfile (-> from event generation)
0261 loglfile=${outflbs}_logL.tgz              # output messages (-> from library production)
0262 logcfile=${outflbs}_logC.tgz              # output messages (-> from cross section calculation)
0263 logefile=${outflbs}_logE.tgz              # output messages (-> from event generation)
0264 #
0265 gridfile=${outflbs}_migr.tgz              # multiple interactions phase-space grid
0266 #
0267 dir1="Process"                            # SHERPA process directory name
0268 dir2="Result"                             # SHERPA results directory name
0269 dir3="Analysis"                           # SHERPA analysis directory name
0270 
0271 ### clean up existing xsection files
0272 for FILE in `ls xsections_*.dat 2> /dev/null`; do
0273   echo " <W> deleting existing cross section file: "${FILE}
0274   rm ${FILE}
0275 done
0276 
0277 
0278 ### test whether PATH exists
0279 if [ -e ${pth} ]; then
0280   echo " path '"${pth}"' exists,"
0281   echo "  -> cleaning path '"${pth}"'"
0282   rm -rf ${pth}/*
0283 else
0284   echo " path '"${pth}"' does not exist,"
0285   echo "  -> creating path '"${pth}"'"
0286   mkdir ${pth}
0287 fi
0288 #MM() pth="./"$pth
0289 cd ${pth}
0290 #MM() pth=`pwd`
0291 
0292 
0293 ### get data card (+ library) tarball(s) from include path
0294 if [ -e ${inc}/${cardfile} ]; then
0295   cp ${inc}/${cardfile} ./
0296 else
0297   if [ -e ${inc}/${runcardfile} ]; then
0298     cp ${inc}/${runcardfile} ./
0299   fi
0300 fi
0301 if [ "${lbo}" == "CRSS" ]; then
0302   cp ${inc}/${libsfile} ./
0303 fi
0304 if [ "${lbo}" == "EVTS" ]; then
0305   cp ${inc}/${libsfile} ./
0306   cp ${inc}/${crssfile} ./
0307   cp ${inc}/${gridfile} ./
0308 fi
0309 
0310 
0311 ### check existence of data card file
0312 if [ -e ${cardfile} ]; then
0313   echo " data card file '"${cardfile}"' exists,"
0314   echo "  -> unpacking data card file"
0315   tar -xzf ${cardfile}
0316 else
0317   if [ -e ${runcardfile} ]; then
0318     mv ${runcardfile} ${runfile}
0319   else
0320     echo " <E> no data card file found"
0321     echo "  -> stopping..."
0322     exit
0323   fi
0324 fi
0325 
0326 
0327 ### find out whether COMIX or AMEGIC is being used
0328 if [ -e ${runfile} ]; then
0329   iamegic=`check_occurence ${runfile} "ME_SIGNAL_GENERATOR" "AMEGIC"`
0330   if [ ${iamegic} -gt 0 ]; then
0331     FLGAMEGIC="TRUE"                   # using AMEGIC
0332     echo " <I> using AMEGIC ME generator"
0333   else
0334 #    FLGAMEGIC="FALSE"                  # not using AMEGIC
0335     echo " <I> using COMIX/internal ME generator"
0336 ###    lbo="LBCR"
0337   fi
0338 fi
0339 ###exit
0340 
0341 ### find out if openloops is used
0342 if [ -e ${runfile} ]; then
0343   iopenloops=`check_occurence ${runfile} "OpenLoops"`
0344   if [ ${iopenloops} -gt 0 ]; then
0345     echo " <I> using OpenLoops as loop generator"
0346     iopenloopsprefix=`check_occurence ${runfile} "OL_PREFIX"`
0347     if [ ${iopenloopsprefix} -gt 0 ]; then
0348       echo " <I> OL_PREFIX prefix specified in RunCard."
0349     else
0350       echo " <I> NO OL_PREFIX specified in RunCard."
0351       ol_prefix=$(scram tool info openloops | grep OPENLOOPS_BASE)
0352       ol_prefix=$(echo $ol_prefix | sed -e "s/OPENLOOPS_BASE/OL_PREFIX/g")
0353       echo " <I> Will use ${ol_prefix}"
0354       sed -i -e"/ME_SIGNAL_GENERATOR/a \ \ $ol_prefix" ${runfile}
0355     fi
0356   fi
0357 fi
0358 ###exit
0359 
0360 
0361 
0362 ### reject mixed occurences of Sherpa's "Enhance" options
0363 #runfile="Run.dat"
0364 if [ -e ${runfile} ]; then
0365   nenhfac=0; nenhfac=`check_occurence ${runfile} "enhance_factor"`
0366   nenhfnc=0; nenhfnc=`check_occurence ${runfile} "enhance_function"`
0367   nenhobs=0; nenhobs=`check_occurence ${runfile} "enhance_observable"`
0368   sumenh=0; let sumenh=$nenhfac+$nenhfnc+$nenhobs
0369   if [ ${sumenh} -gt 1 ]; then
0370     echo " <E> mixed occurence of enhance options in "${runfile}
0371     echo "  -> stopping..."
0372     exit
0373   fi
0374   flgwgt=0; flgwgt=`check_occurence ${runfile} "EVENT_GENERATION_MODE" "Weighted"`
0375   if [ ${flgwgt} -eq 0 ] && [ ${nenhfnc} -eq 1 ]; then
0376     echo " <E> unweighted production and enhance_function not supported by Sherpa!"
0377     echo "  -> stopping..."
0378     exit
0379   fi
0380   if [ ${flgwgt} -eq 0 ] && [ ${nenhobs} -eq 1 ]; then
0381     echo " <E> unweighted production and enhance_observable currently not supported!"
0382     echo "  -> stopping..."
0383     exit
0384   fi
0385 fi
0386 ###exit
0387 
0388 
0389 
0390 
0391 
0392 ### check required subdirectories
0393 ## generate/clean 'Process' subdirectory
0394 if [ ! -e ${dir1} ]; then
0395   echo " '"${dir1}"' subdirectory does not exist and will be created"
0396   mkdir ${dir1}
0397 else
0398   echo " cleaning '"${dir1}"' subdirectory"
0399   rm -rf ${dir1}/*
0400 fi
0401 ## generate/clean 'Results' subdirectory
0402 if [ ! -e ${dir2} ]; then
0403   echo " '"${dir2}"' subdirectory does not exist and will be created"
0404   mkdir ${dir2}
0405 else
0406   echo " cleaning '"${dir2}"' subdirectory"
0407   rm -rf ${dir2}/*
0408 fi
0409 
0410 ### check, whether only cross sections have to be calculated
0411 if [ "${lbo}" = "CRSS" ] || [ "${lbo}" = "EVTS" ]; then
0412   if [ -e ${libsfile} ]; then
0413     echo " <I> library file '"${libsfile}"' exists,"
0414     echo "  -> unpacking library file"
0415     tar -xzf ${libsfile}
0416   else
0417     echo " <E> no library file found"
0418     echo "  -> stopping..."
0419     exit
0420   fi
0421 fi
0422 
0423 ### check, whether only events have to be generated
0424 if [ "${lbo}" = "EVTS" ]; then
0425   if [ -e ${crssfile} ]; then
0426     echo " <I> cross section file '"${crssfile}"' exists,"
0427     echo "  -> unpacking cross section file"
0428     tar -xzf ${crssfile}
0429   else
0430     echo " <E> no cross section file found"
0431     echo "  -> stopping..."
0432     exit
0433   fi
0434   if [ -e ${gridfile} ]; then
0435     echo " <I> MI grid file '"${gridfile}"' exists,"
0436     echo "  -> unpacking phase-space grid"
0437     tar -xzf ${gridfile}
0438   else
0439     echo " <W> no MI grid file found"
0440     echo "  -> will be recreated..."
0441   fi
0442 fi
0443 
0444 
0445 
0446 ### generate process-specific libraries -> redirect output (stdout, stderr) to files
0447 sherpaexe=`find ${shr} -type f -name Sherpa`
0448 echo " <I> Sherpa executable is "${sherpaexe}
0449 cd ${pth}
0450 
0451 # create logfiles
0452 touch ${shrun}/${outflbs}_pass${lbo}.out
0453 touch ${shrun}/${outflbs}_pass${lbo}.err
0454 if [ "${FLGAMEGIC}" == "TRUE" ]; then
0455   touch ${shrun}/${outflbs}_mklib.out
0456   touch ${shrun}/${outflbs}_mklib.err
0457   touch ${shrun}/${outflbs}_cllib.out
0458   touch ${shrun}/${outflbs}_cllib.err
0459 fi
0460 
0461 #Executes a command and logs its stdout and stderr outputs to files.
0462 #If global variable verbose is TRUE, then output is also display on the terminal
0463 #Usage exec_log2 [-a] stdout_file stderr_file cmd args ....
0464 # -a: append outputs to the files.
0465 exec_log2(){
0466     local append_opt=""
0467     if [ $1 = -a ]; then
0468         append_opt=-a
0469         shift
0470     else
0471         unset append_opt
0472     fi
0473     local fout=$1
0474     local ferr=$2
0475     shift 2;
0476 
0477     if [ "$verbose" = "TRUE" ]; then
0478          $@ 1> >(tee $append_opt $fout) 2> >(tee $append_opt $ferr >&2)
0479     elif [ -n "$append_opt" ]; then
0480         "$@" >> "$fout" 2>> "$ferr"
0481     else
0482         "$@" > "$fout" 2> "$ferr"
0483     fi
0484 }
0485 
0486 ## first pass (loop if AMEGIC + NLO loop generators are used)
0487 if [ "${lbo}" == "LIBS" ] || [ "${lbo}" == "LBCR" ]; then
0488 
0489 # force Sherpa to only determine the processes & compile the libraries
0490   SHERPAOPTS="-MNone -FOff INIT_ONLY=1"
0491 
0492   echo " <I> creating library code..."
0493   echo "     ...Logs stored in ${shrun}/${outflbs}_pass${lbo}.out and ${shrun}/${outflbs}_pass${lbo}.err."
0494   exec_log2 -a ${shrun}/${outflbs}_pass${lbo}.out ${shrun}/${outflbs}_pass${lbo}.err ${sherpaexe} -p ${pth} -r ${dir2} ${SHERPAOPTS}
0495 
0496   if [ "${FLGAMEGIC}" == "TRUE" ]; then
0497 
0498     FLGNEWCODE="TRUE"
0499     FLGWRITLIB="TRUE"
0500 
0501     while [ "${FLGNEWCODE}" == "TRUE" ] || [ "${FLGWRITLIB}" == "TRUE" ]; do
0502 
0503 # compile created library code
0504       echo " <I> compiling libraries..."
0505       echo "     ...Logs stored in ${shrun}/${outflbs}_mklib.out and ${shrun}/${outflbs}_mklib.err."
0506       exec_log2 -a ${shrun}/${outflbs}_mklib.out ${shrun}/${outflbs}_mklib.err ./makelibs ${POPTS} -i $SHERPA_INCLUDE_PATH
0507 # get gross size of created libraries
0508       nf=`du -sh | grep -o "\." | grep -c "\."`
0509       lsize=`du -sh  | cut -f 1-${nf} -d "."`
0510       echo " <I>  -> raw size: "${lsize}
0511       echo " <I> cleaning libraries..."
0512       echo "     ...Logs stored in ${shrun}/${outflbs}_cllib.out and ${shrun}/${outflbs}_cllib.err."
0513       exec_log2 -a ${shrun}/${outflbs}_cllib.out ${shrun}/${outflbs}_cllib.err clean_libs
0514 # get net size of created libraries
0515       nf=`du -sh | grep -o "\." | grep -c "\."`
0516       lsize=`du -sh  | cut -f 1-${nf} -d "."`
0517       echo " <I>  -> clean size: "${lsize}
0518 
0519 # reinvoke Sherpa
0520       echo " <I> re-invoking Sherpa for futher library/cross section calculation..."
0521       echo "     ...Logs stored in ${shrun}/${outflbs}_pass${lbo}.out and ${shrun}/${outflbs}_pass${lbo}.err."
0522       exec_log2 -a ${shrun}/${outflbs}_pass${lbo}.out ${shrun}/${outflbs}_pass${lbo}.err ${sherpaexe} -p ${pth} -r ${dir2} ${SHERPAOPTS}
0523 
0524 # newly created process code by AMEGIC?
0525       cd ${dir1}
0526       lastdir=`ls -C1 -drt * | tail -1`
0527       npdir=`echo ${lastdir} | grep -c "P2"`
0528       if [ ${npdir} -gt 0 ]; then
0529         echo " <I> (AMEGIC) library code was created in (at least) "${lastdir}
0530         FLGNEWCODE="TRUE"
0531       else
0532         FLGNEWCODE="FALSE"
0533       fi
0534       cd ${pth}
0535 
0536 # mentioning of "" in last 100 lines output file?
0537       nlines=200
0538 #      nphbw=`tail -${nlines} ${shrun}/${outflbs}_pass${lbo}.out | grep -c "has been written"`
0539 #      npasw=`tail -${nlines} ${shrun}/${outflbs}_pass${lbo}.out | grep -c "AMEGIC::Single_Process::WriteLibrary"`
0540       npnlc=`tail -${nlines} ${shrun}/${outflbs}_pass${lbo}.out | grep -c "New libraries created. Please compile."` 
0541 #      if [ ${nphbw} -gt 0 ] || [ ${npasw} -gt 0 ] || [ ${npnlc} -gt 0 ] ; then
0542       if [ ${npnlc} -gt 0 ] ; then
0543         echo " <I> (AMEGIC) detected library writing: "${nphbw}" (HBW), "${npasw}" (ASW), "${npnlc}" (NLC)"
0544         FLGWRITLIB="TRUE"
0545       else
0546         FLGWRITLIB="FALSE"
0547       fi
0548 
0549     done
0550 
0551   fi
0552 
0553 ###  cd ${shrun}
0554 fi
0555 
0556 if [ "${lbo}" == "LBCR" ] || [ "${lbo}" == "CRSS" ]; then
0557 # ...only calculate a few events for a sanity check of the libraries + cross sections
0558   SHERPAOPTS="-e 101"
0559   echo " <I> calculating cross sections... Logs stored in ${shrun}/${outflbs}_pass${lbo}.out and ${shrun}/${outflbs}_pass${lbo}.err."
0560   if [ "$ML_MPICMD" == "" ]; then
0561     exec_log2 ${shrun}/${outflbs}_pass${lbo}.out ${shrun}/${outflbs}_pass${lbo}.err ${sherpaexe} -p ${pth} -r ${dir2} ${SHERPAOPTS}
0562   else
0563     echo " <I> ...using MPI"
0564     exec_log2 ${shrun}/${outflbs}_pass${lbo}.out ${shrun}/${outflbs}_pass${lbo}.err ${ML_MPICMD} ${ML_MPIOPT} ${sherpaexe} -p ${pth} -r ${dir2} "-e 0"
0565     echo "<I> ...generating events without MPI"
0566     exec_log2 ${shrun}/${outflbs}_pass${lbo}.out ${shrun}/${outflbs}_pass${lbo}.err ${sherpaexe} -p ${pth} -r ${dir2} ${SHERPAOPTS}
0567   fi
0568 fi
0569 
0570 
0571 ## last pass (event generation)
0572 if [ "${lbo}" == "EVTS" ]; then
0573   SHEVTOPT=""
0574   if [ ${NEVTS} -gt 0 ]; then
0575     SHEVTOPT="-e "${NEVTS}
0576   else
0577     NEVTS="default -> run card"
0578   fi
0579   echo " <I> generating events (${NEVTS})... Logs stored in ${shrun}/${outflbs}_pass${lbo}.out and ${shrun}/${outflbs}_pass${lbo}.err."
0580   if [ "$ML_MPICMD" == "" ]; then
0581     exec_log2 ${shrun}/${outflbs}_pass${lbo}.out ${shrun}/${outflbs}_pass${lbo}.err ${sherpaexe} -p ${pth} -r ${dir2} ${SHEVTOPT}
0582   else
0583     echo " <I> ...using MPI"
0584     exec_log2 ${shrun}/${outflbs}_pass${lbo}.out ${shrun}/${outflbs}_pass${lbo}.err ${ML_MPICMD} ${ML_MPIOPT} ${sherpaexe} -p ${pth} -r ${dir2} ${SHEVTOPT}
0585   fi
0586 fi
0587 
0588 
0589 ## generate tar balls with data cards, libraries, cross sections, events
0590 cd ${pth}
0591 
0592 ## libraries & cross sections
0593 if [ "${lbo}" == "LIBS" ] || [ "${lbo}" = "LBCR" ]; then
0594   touch ${libsfile}.tmp
0595   find ./${dir1}/ -name '*'     > tmp.lst && tar --no-recursion -rf ${libsfile}.tmp -T tmp.lst; rm tmp.lst
0596   gzip -9 ${libsfile}.tmp && mv ${libsfile}.tmp.gz ${libsfile}
0597   mv ${libsfile} ${shrun}/
0598 fi
0599 
0600 if [ "${lbo}" == "LBCR" ] || [ "${lbo}" = "CRSS" ]; then
0601   touch ${crssfile}.tmp
0602   find ./${dir2}/ -name '*'     > tmp.lst 
0603   if [ -e Result.db ]; then
0604     echo Result.db >> tmp.lst
0605   fi
0606   tar --no-recursion -rf ${crssfile}.tmp -T tmp.lst; rm tmp.lst
0607   if [ -e ${dir3} ]; then
0608   find ./${dir3}/ -name '*'     > tmp.lst && tar --no-recursion -rf ${crssfile}.tmp -T tmp.lst; rm tmp.lst
0609   fi
0610   gzip -9 ${crssfile}.tmp && mv ${crssfile}.tmp.gz ${crssfile}
0611   mv ${crssfile} ${shrun}/
0612 fi
0613 
0614 #### create tarball with multiple interactions grid files
0615 if [ ! "${lbo}" == "EVTS" ]; then
0616   migdir=`find ./ -name MIG\*`
0617   echo " <I> MPI (mult. part. int.) grid located in "${migdir}
0618   migfil=`find ./ -type f -name MPI\*.dat`
0619   echo " <I> MPI (mult. part. int.) file found: "${migfil}
0620   if [ -d "${migdir}" ] || [ -e "${migdir}" ]; then
0621     if [ -e "${migfil}" ]; then
0622       tar -czf ${gridfile} ${migdir} ${migfil}
0623     else
0624       tar -czf ${gridfile} ${migdir}
0625     fi
0626     mv ${gridfile} ${shrun}/
0627   fi
0628 fi
0629 ####
0630 
0631 if [ "${lbo}" == "EVTS" ]; then
0632   rm ${libsfile}
0633   rm ${crssfile}
0634   rm ${gridfile}
0635   tar -czf ${evtsfile} *.*
0636   mv ${evtsfile} ${shrun}/
0637 fi
0638 #rm -rf ${dir1}/*
0639 #rm -rf ${dir2}/*
0640 #rm -rf ${dir3}/*
0641 #rm *.md5
0642 
0643 ## data cards
0644 FILES=`ls *.md5 *.dat *slha.out 2> /dev/null`
0645 if [ "${lbo}" == "LIBS" ]; then
0646   tar -czf ${crdefile} ${FILES}
0647   mv ${crdefile} ${shrun}/
0648 elif [ "${lbo}" == "LBCR" ]; then
0649   if [ "${FLGAMISIC}" == "TRUE" ]; then
0650     sed -e 's:MI_HANDLER.*:MI_HANDLER   = Amisic:' < Run.dat > Run.dat.tmp
0651     mv Run.dat.tmp Run.dat
0652   fi
0653   tar -czf ${crdefile} ${FILES}
0654   mv ${crdefile} ${shrun}/
0655 elif [ "${lbo}" == "CRSS" ]; then
0656   if [ "${FLGAMISIC}" == "TRUE" ]; then
0657     sed -e 's:MI_HANDLER.*:MI_HANDLER   = Amisic:' < Run.dat > Run.dat.tmp
0658     mv Run.dat.tmp Run.dat
0659   fi
0660   tar -czf ${crdefile} ${FILES}
0661   mv ${crdefile} ${shrun}/
0662 elif [ "${lbo}" == "EVTS" ]; then
0663   echo ""
0664 fi
0665 
0666 ## log files
0667 cd ${shrun}
0668 FILES=`ls *.err *.out 2> /dev/null`
0669 if [ "${lbo}" == "LIBS" ]; then
0670   tar -czf ${loglfile} ${FILES}
0671 elif [ "${lbo}" == "LBCR" ]; then
0672   tar -czf ${loglfile} ${FILES}
0673 elif [ "${lbo}" == "CRSS" ]; then
0674   tar -czf ${logcfile} ${FILES}
0675 elif [ "${lbo}" == "EVTS" ]; then
0676   tar -czf ${logefile} ${FILES}
0677 fi
0678 #rm *.err *.out
0679 mv *.tgz ${fin}/
0680 
0681 
0682 # go back to original directory
0683 cd ${HDIR}
0684 rm -rf ${shrun}