4source $JPP_DIR/setenv.sh $JPP_DIR >& /dev/null
 
    6set_variable:  DEBUG       RECONSTRUCTION_DEBUG          2
 
    7set_variable:  WORKDIR     RECONSTRUCTION_WORKDIR        ${TMPDIR:-/tmp}/
 
    8set_variable:  FORMAT      GRAPHICS_FORMAT               gif
 
    9set_variable+  BATCH       GRAPHICS_BATCH                -B
 
   12    usage "$script <option> [<detector> (input file)+]"\
 
   13          "\nPossible options: run, plot, update and clean."\
 
   14              "\nThe option run should be followed by a detector file and one or more input files (output of JTriggerEfficiency)."\
 
   15              "\nThe option plot could be followed by the name of the fit (prefit, simplex or gandalf) and the quantity (50%, 90%, 99% or efficiency)."
 
   19    set_variable  OPTION          ${argv[1]}
 
   20elif (( $# >= 3 )) && [[ $1 == "run" ]]; then
 
   21    set_variable  OPTION          ${argv[1]}
 
   22    set_variable  DETECTOR        ${argv[2]:a}
 
   23    set_array     INPUT_FILES     ${argv[3,-1]:a}
 
   24elif (( $# >= 1 )) && [[ $1 == "plot" ]]; then
 
   25    set_variable  OPTION          ${argv[1]}
 
   26    set_array     APPS            ${argv[2]}
 
   27    set_variable  QUANTITY        ${argv[3]}
 
   29    fatal "Wrong number of arguments."
 
   32if [[ $OPTION != "run" && $OPTION != "plot" && $OPTION != "update" && $OPTION != "clean" ]]; then
 
   33    fatal "Invalid option $OPTION."
 
   36if (( ${#APPS} == 0 )); then
 
   37    APPS=(prefit simplex gandalf)
 
   40typeset -A NUMBER_OF_FIT
 
   42NUMBER_OF_FIT=(prefit     0
 
   58PARAMETERS[prefit.gridAngle_deg]="0.7 1.0 1.5"
 
   59PARAMETERS[prefit.roadWidth_m]="150.0 175.0 200.0 225.0 250.0 275.0 300.0"
 
   60PARAMETERS[prefit.TMaxLocal_ns]="15.0 17.5 20.0 22.5 25.0 27.5 30.0"
 
   61PARAMETERS[prefit.sigma_ns]="4.0 5.0 6.0 7.0"
 
   62PARAMETERS[prefit.ctMin]="-0.1 0.0 0.1 0.2 0.3"
 
   63PARAMETERS[prefit.numberOfPrefits]="10 25 50 75 100"
 
   64PARAMETERS[prefit.numberOfPostfits]="0 10 20 50"
 
   65PARAMETERS[prefit.Qwatershed]="0 0.5 1.0 1.5 2.0 2.5"
 
   66#PARAMETERS[prefit.numberOfOutliers]="0 1 2 3 4 5"
 
   67#PARAMETERS[prefit.factoryLimit]="7 8 9 10"
 
   68PARAMETERS[prefit.useL0]="0 1"
 
   70PARAMETERS[simplex.sigma_ns]="2.0 2.5 3.0 3.5 4.0"
 
   71PARAMETERS[simplex.TMaxLocal_ns]="12.5 15.0 17.5 20.0 22.5 25.0"
 
   72PARAMETERS[simplex.ctMin]="-0.1 0.0 0.1 0.2 0.3"
 
   73PARAMETERS[simplex.roadWidth_m]="175.0 200.0 225.0 250.0"
 
   75PARAMETERS[gandalf.TTS_ns]="0.0 0.5 1.0 1.5 2.0 2.5"
 
   76PARAMETERS[gandalf.E_GeV]="0.5e3 1.0e3 1.5e3 2.0e3"
 
   77PARAMETERS[gandalf.TMin_ns]="-60.0 -50.0 -40.0 -30.0 -20.0"
 
   78PARAMETERS[gandalf.TMax_ns]="+350.0 +400.0 +450.0 +500.0 +550.0"
 
   79PARAMETERS[gandalf.roadWidth_m]="150.0 175.0 200.0 225.0 250.0"
 
   81if [[ $OPTION == "run" ]]; then
 
   83    set_variable  DIR      `pwd`
 
   84    set_variable  WORKDIR  `mktemp -d $WORKDIR/XXXXXX`
 
   88    set_variable RECONSTRUCTION_NUMBER_OF_THREADS  10
 
   90    for PARAMETER VALUES in ${(kv)PARAMETERS}; do
 
   92            VALUES=($(echo $VALUES))
 
   94            if [[ ! -f $DIR/$PARAMETER.$APPS[1].txt ]]; then
 
   96                for VALUE in $VALUES[*]; do
 
   98                set_variable RECONSTRUCTION_OPTION -@ $PARAMETER=$VALUE
 
  100                        { rm -f jmuon.*.root } >& /dev/null
 
  102                        for (( i = 1; $i <= ${#INPUT_FILES}; i += 1 )); do
 
  103                            JARCAMuonReconstruction.sh  $DETECTOR  $INPUT_FILES[$i]  jmuon.$i.root  $JPP_DATA/PMT_parameters.txt  >&  /dev/null
 
  106                        for APP in $APPS[*]; do
 
  108                            JMuonPostfit -fjmuon.{1..${#INPUT_FILES}}.root -A $ENUMERATION[$APP] -N $NUMBER_OF_FIT[$APP] -o /dev/null  >&  $PARAMETER.$APP.log
 
  110                    set_variable  N0  $(JPrintChain -fjmuon.{1..${#INPUT_FILES}}.root | awk '/^EVT/ {print $2}')
 
  111                    set_variable  N1  $(awk '/Number of events with fit/ {print $NF}' $PARAMETER.$APP.log)
 
  112                    set_array     N2  $(awk '/Space angle/               {print $NF}' $PARAMETER.$APP.log)
 
  114                    printf "%-1s %7.3f %7.3f %7.3f %6.4f\n"  ${VALUE}  ${N2[1]:-0}  ${N2[2]:-0}  ${N2[3]:-0}  $(((1.0 * ${N1:-0}) / (1.0 * ${N0:-1})))  >>  $DIR/$PARAMETER.$APP.txt
 
  122    rm -rf $WORKDIR >& /dev/null
 
  124elif [[ $OPTION == "plot" ]]; then
 
  126    if [[ "$QUANTITY" == "" ]]; then
 
  127        set_variable  QUANTITY  50%
 
  131        50%)        let "COLUMN = 2";  set_variable Y_LABEL "$QUANTITY angle [deg]";;
 
  132        90%)        let "COLUMN = 3";  set_variable Y_LABEL "$QUANTITY angle [deg]";;
 
  133        99%)        let "COLUMN = 4";  set_variable Y_LABEL "$QUANTITY angle [deg]";;
 
  134        efficiency) let "COLUMN = 5";  set_variable Y_LABEL "$QUANTITY";;
 
  135        *) fatal "Invalid quantity $QUANTITY."
 
  141    for PARAMETER in ${(k)PARAMETERS}; do
 
  143            Y=$(sort -gr -k 2 $PARAMETER.${^APPS}.txt | awk -v COLUMN=$COLUMN '{if (NR == 1) {print $COLUMN}}')
 
  145            if (( $Y > $YMAX )); then
 
  150    YMAX=$(printf "%1.1f" $(($YMAX * 1.1)))
 
  152    for PARAMETER VALUES in ${(kv)PARAMETERS}; do
 
  154            echo "Generating graphics for $PARAMETER."
 
  156            VALUES=($(echo $VALUES | sed 's/[[:space:]][[:space:]]*/\n/g' | sort -g))
 
  158        set_variable RECONSTRUCTION_OPTION -h!
 
  160        typeset -T PARAMETER ARRAY .
 
  162        set_variable X  $(eval JARCAMuonReconstruction.sh - - - -  \|  sed -n \'/\^$ARRAY[1]\\\./,/\^\$/s/\.\*$ARRAY[2] \*= \*//p\')
 
  165            -p "$X $YMIN $X $YMAX"          \
 
  168            for APP in $APPS[*]; do
 
  170                        -f $PARAMETER.$APP.txt      \
 
  171                        -o $PARAMETER\[$APP\].root  \
 
  176            let "NX    =  ${#VALUES}"
 
  177            let "XMIN  =  $VALUES[+1]"
 
  178            let "XMAX  =  $VALUES[-1]"
 
  179            let "DX    =  ($XMAX - $XMIN) / ($NX - 1)"
 
  182                -f$PARAMETER\[${^APPS}\].root:"${PARAMETER}\[$(($COLUMN - 2))\]"  \
 
  184                -x "$(($XMIN - 0.5*$DX)) $(($XMAX + 0.5*$DX))"                    \
 
  191                -o $PARAMETER.$FORMAT                                             $BATCH
 
  193            { rm -f $PARAMETER\[${^APPS}\].root line.root } >& /dev/null
 
  196elif [[ $OPTION == "update" ]]; then
 
  202    for PARAMETER in ${(k)PARAMETERS}; do
 
  204            if [[ -f $PARAMETER.$APP.txt ]]; then
 
  206                VALUES=($(sort -g -k 2 $PARAMETER.$APP.txt | awk '{print $2}'))
 
  208                if (( $VALUES[+1] < $VALUES[-1] - $EPS )); then
 
  210                        VALUE=$(sort -g -k 2 $PARAMETER.$APP.txt | awk '{if (NR == 1) {print $1}}')
 
  212                        eval sed -i \'s/\\\(${PARAMETER} \*\\\)\[\^\)\]\*/\\\1${VALUE}/\' $JPP_DIR/software/JReconstruction/JARCAMuonReconstruction.sh
 
  217elif [[ $OPTION == "clean" ]]; then
 
  219    for PARAMETER in ${(k)PARAMETERS}; do
 
  220            for APP in $APPS[*]; do
 
  221                rm -f $PARAMETER.$APP.txt   >&  /dev/null