From d0bedc551a4e525dbeaee4ffb7f9f81db6a0d2eb Mon Sep 17 00:00:00 2001
From: Norbert Herrmann <n.herrmann@physi.uni-heidelberg.de>
Date: Sat, 10 Oct 2020 07:16:23 +0200
Subject: [PATCH] recover from rebase mess

---
 macro/beamtime/hd2020/calib_batch.sh       | 120 +++----
 macro/beamtime/hd2020/eval_tracks.sh       | 217 +++++++------
 macro/beamtime/hd2020/init_calib.sh        | 172 +++++-----
 macro/beamtime/hd2020/trk_cal_digi.sh      | 346 ++++++++++++---------
 macro/beamtime/mcbm2018/gen_digi.sh        |  46 ++-
 macro/beamtime/mcbm2018/iter_tracks.sh     |  97 ++++--
 macro/beamtime/mcbm2020/init_cal_all.sh    |  10 +-
 macro/beamtime/mcbm2020/trk_cal_digi.sh    |   2 +-
 macro/mcbm/geometry/tof/create_parfiles.sh |  13 +-
 9 files changed, 572 insertions(+), 451 deletions(-)

diff --git a/macro/beamtime/hd2020/calib_batch.sh b/macro/beamtime/hd2020/calib_batch.sh
index c00ad24147..a14e212a29 100644
--- a/macro/beamtime/hd2020/calib_batch.sh
+++ b/macro/beamtime/hd2020/calib_batch.sh
@@ -1,22 +1,27 @@
-#!/ bin / bash
-#SBATCH - J calib
-#SBATCH                                                                        \
-  - D / lustre / nyx / cbm / users / nh / CBM / cbmroot / trunk / macro        \
-      / beamtime / hd2020
-#SBATCH-- time      = 6 - 00 : 00 : 00
-#SBATCH-- mem       = 4000
-#SBATCH-- partition = long
+#!/bin/bash
+#SBATCH -J calib
+#SBATCH -D /lustre/nyx/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020
+#SBATCH --time=6-00:00:00
+#SBATCH --mem=4000
+#SBATCH --partition=long
 
-X = $(($ {SLURM_ARRAY_TASK_ID} - 0)) XXX = $(printf "%03d"
-                                                    "$X")
+X=$((${SLURM_ARRAY_TASK_ID} - 0))
+XXX=$(printf "%03d" "$X")
 
-  CalSet = "$1" if [[$ {CalSet} = ""]];
-then echo please specify CalSet !return fi
+CalSet="$1"
+if [[ ${CalSet} = "" ]]; then
+  echo please specify CalSet!
+  return
+fi
 
-  Sel2 = "$2" if [[$ {Sel2} = ""]];
-then echo please specify Sel2 !return fi
+Sel2="$2"
+if [[ ${Sel2} = "" ]]; then
+  echo please specify Sel2!
+  return
+fi
 
-  RunId = "$3" if [[$ {RunId} = ""]]; then
+RunId="$3"
+if [[ ${RunId} = "" ]]; then
 RunList1=' 
 20161121_2133_run1_gdpb
 '
@@ -24,24 +29,30 @@ RunList1='
     RunId=""
     for RunId in $RunList1
     do
-#echo check $RunId
-     if (($I == $X));
-then echo found RunId $RunId break fi((I = $I + 1)) done fi
+    #echo check $RunId
+     if (($I == $X)); then
+       echo found RunId $RunId
+       break
+     fi
+     ((I=$I+1))
+    done
+fi
 
-  CalMode = "$4" if [[$ {CalMode} = ""]]; then
-#CalMode  = "_DT200_0x00005006"
-#CalMode  = "_DT50_0x00005006"
-#CalMode  = "_DT50_0x00000000"
-#CalMode  = "_DT50_0x00019026"
-#CalMode  = "_DT50_Req5"
-#CalMode  = "_DT50_Req0"
-#CalMode  = "_DT50000_Req-3"
-#CalMode  = "_DT50000_Req-4"
-#CalMode  = "_DT26000_Req-4"
-CalMode=".100.3"
-#CalMode  = "_DT100_Req-4"
-#CalMode  = "_DT50_Req-3"
-#CalMode  = "_DT50_Req-2"
+CalMode="$4"
+if [[ ${CalMode} = "" ]]; then
+#CalMode="_DT200_0x00005006"
+#CalMode="_DT50_0x00005006"
+#CalMode="_DT50_0x00000000"
+#CalMode="_DT50_0x00019026"
+#CalMode="_DT50_Req5"
+#CalMode="_DT50_Req0"
+#CalMode="_DT50000_Req-3"
+#CalMode="_DT50000_Req-4"
+#CalMode="_DT26000_Req-4"
+CalMode=".100.3" 
+#CalMode="_DT100_Req-4"
+#CalMode="_DT50_Req-3"
+#CalMode="_DT50_Req-2"
 fi
 
 RunIdMode=`echo $RunId$CalMode`
@@ -49,33 +60,26 @@ Deadtime=50
 
 echo define root execution environment at I=$I for Run $RunId Mode $RunIdMode
 
-if [ -e /lustre/nyx ];
-then source / lustre / nyx / cbm / users / nh / CBM / cbmroot / trunk / build
-  / config.sh wdir = / lustre / nyx / cbm / users / nh / CBM / cbmroot / trunk
-                     / macro / beamtime / hd2020 outdir =
-  / lustre / nyx / cbm / users / nh / CBM / cbmroot / trunk / macro / beamtime
-  / hd2020 / $ {
-  RunId
-}
-else wdir =`pwd` outdir =
-  $ {wdir}
-  / $ {RunId} fi mkdir $ {outdir}
+if [ -e /lustre/nyx ]; then
+source /lustre/nyx/cbm/users/nh/CBM/cbmroot/trunk/build/config.sh 
+wdir=/lustre/nyx/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020
+outdir=/lustre/nyx/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020/${RunId}
+else 
+wdir=`pwd`
+outdir=${wdir}/${RunId}
+fi
+mkdir ${outdir}
 
-    cd $ {wdir} echo execute :.
-  / init_calib.sh $ {RunIdMode} $ {CalSet} $ {Deadtime} source.
-  / init_calib.sh $ {RunIdMode} $ {CalSet} $ {Deadtime}
+cd  ${wdir}
+echo execute: ./init_calib.sh ${RunIdMode} ${CalSet} ${Deadtime}
+source ./init_calib.sh ${RunIdMode} ${CalSet} ${Deadtime}
 
-    cd $ {
-  wdir
-}
-#echo execute :./ iter_calib.sh ${RunIdMode } ${CalSet } ${Sel2 } ${Deadtime }
-#source./ iter_calib.sh ${RunIdMode } ${CalSet } ${Sel2 } ${Deadtime }
+cd  ${wdir}
+#echo execute: ./iter_calib.sh ${RunIdMode} ${CalSet} ${Sel2} ${Deadtime}
+#source ./iter_calib.sh ${RunIdMode} ${CalSet} ${Sel2} ${Deadtime}
 
-cd $ {wdir} echo execute :.
-    / gen_digi.sh $ {RunIdMode} $ {CalSet} $ {Sel2} $ {Deadtime} source.
-    / gen_digi.sh $ {RunIdMode} $ {CalSet} $ {Sel2} $ {Deadtime}
+cd  ${wdir}
+echo execute: ./gen_digi.sh ${RunIdMode} ${CalSet} ${Sel2} ${Deadtime}
+source ./gen_digi.sh ${RunIdMode} ${CalSet} ${Sel2} ${Deadtime}
 
-      mv
-  - v slurm
-  - $ {SLURM_ARRAY_JOB_ID} _$ {SLURM_ARRAY_TASK_ID}.out $ {outdir}
-      / Calib_$ {RunIdMode} _$ {CalSet} _$ {Sel2}.out
+mv -v slurm-${SLURM_ARRAY_JOB_ID}_${SLURM_ARRAY_TASK_ID}.out ${outdir}/Calib_${RunIdMode}_${CalSet}_${Sel2}.out
diff --git a/macro/beamtime/hd2020/eval_tracks.sh b/macro/beamtime/hd2020/eval_tracks.sh
index 5b4c950274..196b920faa 100644
--- a/macro/beamtime/hd2020/eval_tracks.sh
+++ b/macro/beamtime/hd2020/eval_tracks.sh
@@ -1,113 +1,108 @@
-#!/ bin / bash
-#shell script to iterate tracklet calibration histograms
-#SBATCH - J eval_tracks
-#SBATCH                                                                        \
-  - D / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro / beamtime   \
-      / hd2020
-#SBATCH-- time      = 1 - 00 : 00 : 00
-#SBATCH-- mem       = 2000
-#SBATCH-- partition = long
-
-X = $(($ {SLURM_ARRAY_TASK_ID} - 0)) XXX = $(printf "%03d"
-                                                    "$X")
-
-  cRun = $1 iDut = $2;
-iRef             = $3;
-iSel2 = $4((iSel = $iDut * 1000 + $iRef)) iTraSetup = $5
-
-  cSet    = $6 if [[$ {cSet} = ""]];
-then cSet = "900920910_911" fi
-
-  CalIdMode                                  = $7 if [[$ {CalIdMode} = ""]];
-then echo use native calibration file cCalId = $cRun;
-else cCalId                                  = $ {CalIdMode} fi
-
-  dDTres = 10000000 nEvt = 100000
-
-  cSel2 = $iSel2;
-if
-  [[$iSel2 < 100]];
-then cSel2 = "0" $iSel2 if [[$iSel2 < 10]];
-then cSel2 = "00" $iSel2 fi fi
-
-  if[-e / lustre / cbm];
-then source / lustre / cbm / users / nh / CBM / cbmroot / trunk / build
-  / config.sh wdir = / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro
-                     / beamtime / hd2020 outdir =
-  / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro / beamtime
-  / hd2020 / $ {
-  cRun
-}
-else wdir =`pwd` outdir = $ {wdir}
-                          / $ {cRun} fi
-
-#frange2 limits chi2
-                            fRange2 = 3.5
-
-#frange1 limits DT spectrum range
-  fRange1 = 1.1 dDeadtime =
-    50
-
-      cd $ {cRun} mkdir Ana_$ {cSet} _$ {iSel} _$ {cSel2} _$ {iTraSetup} cp..
-      / rootlogon.C Ana_$ {cSet} _$ {iSel} _$ {cSel2} _$ {iTraSetup} / cp..
-      /.rootrc Ana_$ {cSet} _$ {iSel} _$ {cSel2} _$ {iTraSetup}
-      / cd Ana_$ {cSet} _$ {iSel} _$ {cSel2} _$ {iTraSetup} rm
-    - v * AnaTestBeam.hst.root cp
-    - v../../ $ {cCalId} _tofFindTracks.hst.root.cSet9 =
-$ {cSet : 0 : 9} ln - s
-  - v..
-      / $ {cCalId} _set$ {
-        cSet9} _93_1tofClust.hst.root $ {cCalId} _set$ {
-        cSet9} _93_1tofClust.hst.root
-
-        while [[$dDTres > 0]];
+#!/bin/bash
+# shell script to iterate tracklet calibration histograms
+#SBATCH -J eval_tracks
+#SBATCH -D /lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020
+#SBATCH --time=1-00:00:00
+#SBATCH --mem=2000
+#SBATCH --partition=long
+
+X=$((${SLURM_ARRAY_TASK_ID} - 0))
+XXX=$(printf "%03d" "$X")
+
+cRun=$1
+iDut=$2; 
+iRef=$3; 
+iSel2=$4
+((iSel=$iDut*1000+$iRef))
+iTraSetup=$5
+
+cSet=$6
+if [[ ${cSet} = "" ]]; then
+    cSet="900920910_911"
+fi
+    
+CalIdMode=$7
+if [[ ${CalIdMode} = "" ]]; then
+    echo use native calibration file
+    cCalId=$cRun;
+else
+    cCalId=${CalIdMode}
+fi
+
+dDTres=10000000
+nEvt=100000
+
+cSel2=$iSel2;
+if [[ $iSel2 < 100 ]]; then
+    cSel2="0"$iSel2
+    if [[ $iSel2 < 10 ]]; then
+	cSel2="00"$iSel2
+    fi
+fi
+
+if [ -e /lustre/cbm ]; then
+source /lustre/cbm/users/nh/CBM/cbmroot/trunk/build/config.sh 
+wdir=/lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020
+outdir=/lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020/${cRun}
+else 
+wdir=`pwd`
+outdir=${wdir}/${cRun}
+fi
+
+# frange2 limits chi2
+fRange2=3.5
+
+#frange1 limits DT spectrum range 
+fRange1=1.1
+dDeadtime=50
+
+cd ${cRun}
+mkdir          Ana_${cSet}_${iSel}_${cSel2}_${iTraSetup}
+cp ../rootlogon.C Ana_${cSet}_${iSel}_${cSel2}_${iTraSetup}/
+cp ../.rootrc     Ana_${cSet}_${iSel}_${cSel2}_${iTraSetup}/
+cd Ana_${cSet}_${iSel}_${cSel2}_${iTraSetup}
+rm -v  *AnaTestBeam.hst.root
+cp -v ../../${cCalId}_tofFindTracks.hst.root .
+cSet9=${cSet:0:9}
+ln -s -v ../${cCalId}_set${cSet9}_93_1tofClust.hst.root  ${cCalId}_set${cSet9}_93_1tofClust.hst.root 
+
+while [[ $dDTres > 0 ]]; do
+
+for iCal in 1 2 3 5 6 7 8 1
 do
 
-for
-  iCal in 1 2 3 5 6 7 8 1 do
-
-    root
-    - b
-    - q '../../ana_trks.C(' $nEvt ',' $iSel ',-1,"' $cRun '","' $cSet '",' $iSel2 ',' $iTraSetup ',' $fRange1 ',' $fRange2 ',' $dDeadtime ',"' $cCalId '",' $iCal ')' mv
-    - v tofAnaTestBeam.hst.root $ {cRun} _TrkAnaTestBeam.hst.root rm all_*
-
-      if (!(test - f Test.res));
-then echo no resolution file available :
-  scan full statistics and exit iCal =
-    1 root - b
-    - q '../../ana_trks.C(-1,' $iSel ',-1,"' $cRun '","' $cSet '",' $iSel2 ',' $iTraSetup ',' $fRange1 ',' $fRange2 ',' $dDeadtime ',"' $cCalId '",' $iCal ')' exit 1 fi
-      done
-
-        Tres                              =`cat Test
-                .res` dTdif               =`echo "$dDTres - $Tres"
-                             | bc` dDTres =`echo "$dDTres - 0.005"
-                                           | bc` compare_result =`echo
-                                                                 "$Tres < "
-                                                                 "$dDTres"
-                                                                 | bc`
-
-                                                                 echo got Tres =
-      $Tres,
-                                compare to $dDTres, dTdif = $dTdif,
-                                compare_result = $compare_result
-
-                                if [[$compare_result > 0]];
-then dDTres = $Tres else dDTres = 0 fi
-
-  done
-
-#final action->scan full statistics
-    iCal =
-      1 root - b
-      - q '../../ana_trks.C(-1,' $iSel ',-1,"' $cRun '","' $cSet '",' $iSel2 ',' $iTraSetup ',' $fRange1 ',' $fRange2 ',' $dDeadtime ',"' $cCalId '",' $iCal ')' rm
-            all_
-          * cd..
-          /..
-
-#mv - v slurm                                                                  \
-  - ${SLURM_ARRAY_JOB_ID } _${SLURM_ARRAY_TASK_ID }.out ${                     \
-    outdir } / IterTrack_${cRun } _${cSet }.out
-           mv
-      - v slurm
-      - $ {SLURM_JOB_ID}.out $ {outdir}
-          / EvalTrack_$ {cRun} _$ {cSet} _$ {iSel} _$ {iSel2} _$ {iTraSetup}.out
+root -b -q '../../ana_trks.C('$nEvt','$iSel',-1,"'$cRun'","'$cSet'",'$iSel2','$iTraSetup','$fRange1','$fRange2','$dDeadtime',"'$cCalId'",'$iCal')'
+mv -v tofAnaTestBeam.hst.root ${cRun}_TrkAnaTestBeam.hst.root
+rm all_*
+
+if (! (test -f Test.res)); then
+echo no resolution file available: scan full statistics and exit
+iCal=1
+root -b -q '../../ana_trks.C(-1,'$iSel',-1,"'$cRun'","'$cSet'",'$iSel2','$iTraSetup','$fRange1','$fRange2','$dDeadtime',"'$cCalId'",'$iCal')'
+exit 1
+fi
+done
+
+Tres=`cat Test.res`
+dTdif=`echo "$dDTres - $Tres" | bc`
+dDTres=`echo "$dDTres - 0.005" | bc`
+compare_result=`echo "$Tres < $dDTres" | bc`
+
+echo got Tres = $Tres, compare to $dDTres, dTdif = $dTdif, compare_result = $compare_result
+
+if [[ $compare_result > 0 ]]; then
+dDTres=$Tres
+else
+dDTres=0
+fi
+
+done
+
+# final action -> scan full statistics 
+iCal=1
+root -b -q '../../ana_trks.C(-1,'$iSel',-1,"'$cRun'","'$cSet'",'$iSel2','$iTraSetup','$fRange1','$fRange2','$dDeadtime',"'$cCalId'",'$iCal')'
+rm all_*
+cd ../..
+
+#mv -v slurm-${SLURM_ARRAY_JOB_ID}_${SLURM_ARRAY_TASK_ID}.out ${outdir}/IterTrack_${cRun}_${cSet}.out
+mv -v slurm-${SLURM_JOB_ID}.out ${outdir}/EvalTrack_${cRun}_${cSet}_${iSel}_${iSel2}_${iTraSetup}.out
diff --git a/macro/beamtime/hd2020/init_calib.sh b/macro/beamtime/hd2020/init_calib.sh
index 5d7dcb90c9..3bbe437dd9 100644
--- a/macro/beamtime/hd2020/init_calib.sh
+++ b/macro/beamtime/hd2020/init_calib.sh
@@ -1,54 +1,67 @@
-#!/ bin / bash
-#shell script to initialize clusterizer calibrations
-#SBATCH - J calall
-#SBATCH                                                                        \
-  - D / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro / beamtime   \
-      / hd2020
-#SBATCH-- time      = 6 - 00 : 00 : 00
-#SBATCH-- mem       = 2000
-#SBATCH-- partition = long
-cRun                = $1
+#!/bin/bash
+# shell script to initialize clusterizer calibrations
+#SBATCH -J calall
+#SBATCH -D /lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020
+#SBATCH --time=6-00:00:00
+#SBATCH --mem=2000
+#SBATCH --partition=long
+cRun=$1
 
-  echo 'Initialize clusterizer calibration for run ' $cRun
+echo 'Initialize clusterizer calibration for run '$cRun
 
-    iCalSet = $2((iTmp = $iCalSet))((iBRef = $iTmp % 1000))((
-      iTmp = $iTmp - $iBRef))((iSet = $iTmp / 1000))((iMRef = $iTmp % 1000000))(
-      (iMRef = $iMRef / 1000))((iTmp = $iTmp - $iMRef))(
-      (iDut = $iTmp / 1000000)) echo Calib setup is $ {iCalSet},
-    iSet = $iSet, iDut = $iDut, iMRef = $iMRef,
-    iBRef = $iBRef cCalSet = $iCalSet if ((iCalSet < 100000000));
-then cCalSet               = "0" $iCalSet fi if ((iCalSet < 10000000));
-then cCalSet               = "00" $iCalSet fi if ((iCalSet < 1000000));
-then cCalSet               = "000" $iCalSet fi if ((iCalSet < 100000));
-then cCalSet = "0000" $iCalSet fi echo cCalSet = $cCalSet
-#iSet                                          = 0
-#lastOpt =''
-  nEvi0 = 100000 #start value nEvi1 = 10000 #increment
+iCalSet=$2
+((iTmp  = $iCalSet ))
+((iBRef = $iTmp % 1000))
+((iTmp  = $iTmp - $iBRef))
+((iSet  = $iTmp / 1000))
+((iMRef = $iTmp % 1000000))
+((iMRef = $iMRef / 1000))
+((iTmp  = $iTmp - $iMRef))
+((iDut  = $iTmp / 1000000))
+echo Calib setup is ${iCalSet}, iSet=$iSet, iDut=$iDut, iMRef=$iMRef, iBRef=$iBRef
+cCalSet=$iCalSet
+if (( iCalSet<100000000 )); then 
+cCalSet="0"$iCalSet
+fi
+if (( iCalSet<10000000 )); then 
+cCalSet="00"$iCalSet
+fi
+if (( iCalSet<1000000 )); then 
+cCalSet="000"$iCalSet
+fi
+if (( iCalSet<100000 )); then 
+cCalSet="0000"$iCalSet
+fi
+echo cCalSet = $cCalSet
+#iSet=0
+#lastOpt=''
+nEvi0=100000  # start value
+nEvi1=10000    # increment 
 
-  if[-e / lustre];
-then source / lustre / cbm / users / nh / CBM / cbmroot / trunk / build6
-  / config.sh wdir = / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro
-                     / beamtime / hd2020 outdir =
-  / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro / beamtime
-  / hd2020 / $ {
-  RunId
-}
-else wdir =`pwd` outdir =
-  $ {wdir}
-  / $ {RunId} fi mkdir $ {outdir}
+if [ -e /lustre ]; then
+source /lustre/cbm/users/nh/CBM/cbmroot/trunk/build6/config.sh 
+wdir=/lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020
+outdir=/lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020/${RunId}
+else 
+wdir=`pwd`
+outdir=${wdir}/${RunId}
+fi
+mkdir ${outdir}
 
-    cd $ {wdir} mkdir $ {cRun} cp rootlogon.C $ {cRun} cp.rootrc $ {cRun} cd $ {
-  cRun
-}
+cd  ${wdir}
+mkdir ${cRun}
+cp rootlogon.C ${cRun}
+cp .rootrc ${cRun}
+cd ${cRun}
 
-#Global variables, for for - loops
+# Global variables, for for-loops
 iRestart=0
-#iRestart = 12
+#iRestart=12
 iStep=0
 iStepLast=0
 iCalSel0=0
 iCalSel1=1
-#************************** Starting while Loop ***************************** #
+# ************************** Starting while Loop ***************************** #
 
 (( nEvi = nEvi0 + 10*nEvi1 ))
 optList=""
@@ -59,8 +72,8 @@ do
     (( nEvi = nEvi0 + (icallev-1)*nEvi1 ))
     optList=`echo " $nEvi,$icallev$icalmod,$iCalSel0,$iDut,0 "`$optList
     optList=`echo " $nEvi,$icallev$icalmod,$iCalSel1,$iMRef,0 "`$optList
-    optList=`echo " $nEvi,$icallev$icalmod,$iCalSel0,99,50 "`$optList
-#optList =`echo " $nEvi,$icallev$icalmod,$iCalSel1,99,50 "`$optList
+    optList=`echo " $nEvi,$icallev$icalmod,$iCalSel0,99,50 "`$optList 
+    #optList=`echo " $nEvi,$icallev$icalmod,$iCalSel1,99,50 "`$optList
 done
  optList=`echo " $nEvi,0,0,99,50 "`$optList      # start Init1
  echo optList:  $optList
@@ -74,53 +87,44 @@ do
     mkdir Init${iStep}
     cp rootlogon.C Init${iStep}
     cp .rootrc Init${iStep}
-    cd Init${
-  iStep
-}
+    cd Init${iStep}
 
-if
-  [[$ {lastOpt : +1}]];
-then
-#echo last round was done with $lastOpt, extract 2. and 3. word
+    if [[ ${lastOpt:+1} ]] ; then
+	# echo last round was done with $lastOpt, extract 2. and 3. word
 	i1=`expr index $inOpt , `
 	i2=($i1+3)
-#echo `expr index $inOpt, ` = $i1
+	#echo `expr index $inOpt , ` = $i1
 	cMode=${inOpt:$i1:2}
 	cSel=${inOpt:$i2:1}
 	echo next iteration: cMode=$cMode, cSel=$cSel 
-	if [[ ${cSel} = "-" ]];
-then cSel = $ {inOpt : $i2 : 2} echo cSel = $cSel cSel = "0" fi
-#copy calibration file
-  if (($iStep > $iRestart));
-then cp
-  - v../ Init$ {iStepLast}
-      / tofClust_$ {cRun} _set$ {cCalSet}
-          .hst.root $ {cRun} _set$ {cCalSet} _$ {cMode} _$ {
-            cSel} tofClust.hst.root fi fi
+	if [[ ${cSel} = "-" ]];then 
+	    cSel=${inOpt:$i2:2}
+	    echo cSel=$cSel 
+	    cSel="0"
+	fi
+	#copy calibration file 
+	if (($iStep > $iRestart)) ; then
+	    cp -v ../Init${iStepLast}/tofClust_${cRun}_set${cCalSet}.hst.root ${cRun}_set${cCalSet}_${cMode}_${cSel}tofClust.hst.root
+	fi
+    fi 
 
-            lastOpt = $inOpt
-#generate new calibration file
-  if (($iStep > $iRestart));
-then root - b - q '../../ana_digi_cal.C(' $inOpt ',"' $ { cRun }
-'",' $ { iCalSet }
-',0,' $ { iBRef }
-') '
+    lastOpt=$inOpt
+    # generate new calibration file
+    if (($iStep > $iRestart)) ; then 
+	root -b -q '../../ana_digi_cal.C('$inOpt',"'${cRun}'",'${iCalSet}',0,'${iBRef}') '
 
-  cp
-  - v tofClust_$ {cRun} _set$ {cCalSet}.hst.root..
-      / $ {cRun} _set$ {cCalSet} _$ {cMode} _$ {
-        cSel} tofClust.hst.root cp *pdf..
-      /
-#./ screenshot.sh
-      cd..rm..
-      / $ {cRun} _set$ {cCalSet} _$ {cMode} _$ {cSel} tofClust.hst.root ln
-  - s./ $ {cRun}
-      / $ {cRun} _set$ {cCalSet} _$ {cMode} _$ {cSel} tofClust.hst.root..
-      / $ {cRun} _set$ {cCalSet} _$ {cMode} _$ {
-        cSel} tofClust.hst.root echo Init step $iStep with mode $ {cMode},
-  option $inOpt finished else cd..echo Init step $iStep with mode $ {cMode},
-  option $inOpt skipped fi done
+	cp -v tofClust_${cRun}_set${cCalSet}.hst.root ../${cRun}_set${cCalSet}_${cMode}_${cSel}tofClust.hst.root
+	cp *pdf ../
+	#./screenshot.sh
+	cd .. 
+	rm ../${cRun}_set${cCalSet}_${cMode}_${cSel}tofClust.hst.root
+	ln -s ./${cRun}/${cRun}_set${cCalSet}_${cMode}_${cSel}tofClust.hst.root ../${cRun}_set${cCalSet}_${cMode}_${cSel}tofClust.hst.root
+	echo Init step $iStep with mode ${cMode}, option $inOpt  finished
+    else 
+	cd ..
+	echo Init step $iStep with mode ${cMode}, option $inOpt  skipped
+    fi   
+done
 
-      cd $ {wdir} mv
-    - v slurm
-    - $ {SLURM_JOB_ID}.out $ {outdir} / InitCalib_$ {cRun} _$ {cCalSet}.out
+cd  ${wdir}
+mv -v slurm-${SLURM_JOB_ID}.out ${outdir}/InitCalib_${cRun}_${cCalSet}.out
diff --git a/macro/beamtime/hd2020/trk_cal_digi.sh b/macro/beamtime/hd2020/trk_cal_digi.sh
index 4d7e76248c..4136796a5a 100644
--- a/macro/beamtime/hd2020/trk_cal_digi.sh
+++ b/macro/beamtime/hd2020/trk_cal_digi.sh
@@ -1,47 +1,78 @@
-#!/ bin / bash
-#shell script to apply clusterizer calibrations
-#SBATCH - J trk_cal_digi
-#SBATCH                                                                        \
-  - D / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro / beamtime   \
-      / hd2020
-#SBATCH-- time      = 5 - 24 : 00 : 00
-#SBATCH-- mem       = 4000
-#SBATCH-- partition = long
-cRun                = $1
-
-  iCalSet = $2((iTmp = $iCalSet))((iBRef = $iTmp % 1000))(
-    (iTmp = $iTmp - $iBRef))((iSet = $iTmp / 1000))((iRef = $iTmp % 1000000))(
-    (iRef = $iRef / 1000))((iTmp = $iTmp - $iRef))((iDut = $iTmp / 1000000))
-
-    iSel2 = $3 cSel2 = $iSel2 if ((iSel2 < 100));
-then cSel2           = "0" $iSel2 fi if ((iSel2 < 10));
-then cSel2           = "00" $iSel2 fi
-
-  cCalSet    = $iCalSet if ((iCalSet < 100000000));
-then cCalSet = "0" $iCalSet fi if ((iCalSet < 10000000));
-then cCalSet = "00" $iCalSet fi if ((iCalSet < 1000000));
-then cCalSet = "000" $iCalSet fi if ((iCalSet < 100000));
-then cCalSet = "0000" $iCalSet fi echo cCalSet = $cCalSet
-
-  Deadtime    = $4 if [[$ {Deadtime} = ""]];
-then Deadtime = 50. fi
-
-  CalIdMode                                     = $5 if [[$ {CalIdMode} = ""]];
-then echo use native calibration file CalIdMode = $ {cRun} CalFile =
-  $ {cRun} _set$ {cCalSet} _93_1tofClust.hst.root else CalFile =
-    $ {CalIdMode} _set$ {cCalSet} _93_1tofClust.hst.root RunFile =
-      $ {cRun} _set$ {cCalSet} _93_1tofClust.hst.root
-#rm ${RunFile }
-#ln - s ${CalFile } ${RunFile }
-        echo use calibrations from $ {CalFile} fi
-
-          iCalOpt = $6 if [[$ {iCalOpt} = ""]];
-then iCalOpt      = 1 fi
-
-  iTraSetup    = $7 if [[$iTraSetup = ""]];
-then iTraSetup = 11 fi
-
-  CalIdSet = $8 if [[$ {CalIdSet} = ""]]; then
+#!/bin/bash
+# shell script to apply clusterizer calibrations
+#SBATCH -J trk_cal_digi
+#SBATCH -D /lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020
+#SBATCH --time=5-24:00:00
+#SBATCH --mem=4000
+#SBATCH --partition=long
+
+trk_cal_digi() {
+cRun=$1
+
+iCalSet=$2
+((iTmp  = $iCalSet ))
+((iBRef = $iTmp % 1000))
+((iTmp  = $iTmp - $iBRef))
+((iSet  = $iTmp / 1000))
+((iRef  = $iTmp % 1000000))
+((iRef  = $iRef / 1000))
+((iTmp  = $iTmp - $iRef))
+((iDut  = $iTmp / 1000000))
+
+iSel2=$3
+cSel2=$iSel2
+if (( iSel2<100 )); then 
+cSel2="0"$iSel2
+fi
+if (( iSel2<10 )); then 
+cSel2="00"$iSel2
+fi
+
+cCalSet=$iCalSet
+if (( iCalSet<100000000 )); then 
+cCalSet="0"$iCalSet
+fi
+if (( iCalSet<10000000 )); then 
+cCalSet="00"$iCalSet
+fi
+if (( iCalSet<1000000 )); then 
+cCalSet="000"$iCalSet
+fi
+if (( iCalSet<100000 )); then 
+cCalSet="0000"$iCalSet
+fi
+echo cCalSet = $cCalSet
+
+Deadtime=$4
+if [[ ${Deadtime} = "" ]]; then
+Deadtime=50.
+fi
+
+CalIdMode=$5
+if [[ ${CalIdMode} = "" ]]; then
+ echo use native calibration file 
+ CalIdMode=${cRun}
+ CalFile=${cRun}_set${cCalSet}_93_1tofClust.hst.root
+else 
+ CalFile=${CalIdMode}_set${cCalSet}_93_1tofClust.hst.root
+ RunFile=${cRun}_set${cCalSet}_93_1tofClust.hst.root
+# rm ${RunFile}
+# ln -s ${CalFile} ${RunFile} 
+ echo use calibrations from  ${CalFile}
+fi
+
+iCalOpt=$6
+if [[ ${iCalOpt} = "" ]]; then
+  iCalOpt=1	
+fi
+
+iTraSetup=$7
+if [[ $iTraSetup = "" ]]; then 
+  iTraSetup=1
+fi
+
+CalIdSet=$8
+if [[ ${CalIdSet} = "" ]]; then
     echo use native calibration file
     CalIdSet=$cCalSet
 else
@@ -51,19 +82,25 @@ fi
 
 echo trk_cal_digi for $cRun with iDut=$iDut, iRef=$iRef, iSet=$iCalSet, iSel2=$iSel2, iBRef=$iBRef, Deadtime=$Deadtime, CalFile=$CalFile
 
-if [ -e /lustre/cbm ];
-then source / lustre / cbm / users / nh / CBM / cbmroot / trunk / build
-  / config.sh wdir = / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro
-                     / beamtime / hd2020 outdir =
-  / lustre / cbm / users / nh / CBM / cbmroot / trunk / macro / beamtime
-  / hd2020 / $ {
-  cRun
-}
-else wdir =`pwd` outdir = $ {wdir}
-                          / $ {cRun} fi
+if [[ $iShLev = "" ]]; then 
+  iShLev=0
+  nEvt=200000
+  dDTres=100000
+  dDTRMSres=100000
+fi 
+
+echo execute trk_cal_digi at shell level $iShLev
+
+if [ -e /lustre/cbm ]; then
+source /lustre/cbm/users/nh/CBM/cbmroot/trunk/build/config.sh 
+wdir=/lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020
+outdir=/lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/hd2020/${cRun}
+else 
+wdir=`pwd`
+outdir=${wdir}/${cRun}
+fi
 
-                            nEvt = 200000 dDTres = 100000 dDTRMSres =
-  100000 iter                                    = 0;
+iter=0;
 
 cd $wdir
 mkdir $cRun
@@ -71,22 +108,22 @@ cd    $cRun
 cp    ../.rootrc .
 cp    ../rootlogon.C .
 
-echo Execute in `pwd`: ./trk_cal_digi.sh $1 $2 $3 $4 $5 $6 $7 $8
-
-#get initial digi calibration
-#cp - v./ I* / ${CalFile }.
+echo Execute in `pwd` at $iShLev: ./trk_cal_digi.sh $1 $2 $3 $4 $5 $6 $7 $8
 
-#get latest tracker offsets
-#cp - v../ ${cRun } _tofFindTracks.hst.root.
+# get initial digi calibration 
+#cp -v  ./I*/${CalFile}  .
+ 
+# get latest tracker offsets
+# cp -v ../${cRun}_tofFindTracks.hst.root .
 
 rm -v TCalib.res
 nEvtMax=0
 (( nEvtMax = nEvt*10 ))
 
 #frange1 limits DT spectrum range 
-fRange1=2.
-#frange2 limits chi2
-fRange2=5.0
+fRange1=1.2
+# frange2 limits chi2
+fRange2=4.0
 TRange2Limit=3. 
 
 iSel=911921
@@ -95,98 +132,97 @@ cCalSet2=${cCalSet}_$cSel2
 
 case $iCalOpt in
   1)   # TOff
-  ;
-;
+  ;;
   2)   # Walk
   (( nEvt *= 10 ))
   (( nEvtMax *= 10 ))
-  ;
-  ;
-  esac
-
-    while [[$dDTres > 0]];
-  do
-    nEvt =`echo "scale=0;$nEvt * 1./1."
-          | bc`
-#nEvt =`echo "scale=0;$nEvt * 1.1/1." | bc`
-          if[$nEvt - gt $nEvtMax];
-  then nEvt = $nEvtMax fi
-
-    fRange2                        =`echo "$fRange2 * 0.9"
-             | bc` compare_TRange2 =`echo "$fRange2 < $TRange2Limit"
-                                    | bc` if [[$compare_TRange2 > 0]];
-  then fRange2 = $TRange2Limit fi
-
-                   cd $wdir
-                 / $cRun iCalAct =
-    $iCalOpt echo Enter while loop with $iCalAct in dir `pwd` while
-    [[$iCalAct > 0]];
-  do
-    if
-      [[$iCalOpt = 1]] || [[$iCalAct > 1]];
-  then root - b
-    - q '../ana_digi_cal.C(' $nEvt ',93,1,' $iRef ',1,"' $cRun '",' $iCalSet ',1,' $iSel2 ',' $Deadtime ',"' $CalIdMode '") '
-#update calibration parameter file, will only be active in next iteration
-#cp                                                                            \
-  - v tofClust_${cRun } _set${                                                 \
-    cCalSet }.hst.root../ ${cRun } _set${cCalSet } _93_1tofClust.hst.root
-
-    root
-    - b
-    - q '../ana_trks.C(' $nEvt ',' $iSel ',' $iGenCor ',"' $cRun '","' $cCalSet2 '",' $iSel2 ',' $iTraSetup ',' $fRange1 ',' $fRange2 ',' $Deadtime ',"' $CalIdMode '",1,1,' $iCalSet ',' $iCalAct ')'
-#root                                                                          \
-  - l 'ana_trksi.C(-1,10,1,"385.50.5.0","000014500_020",20,1,1.90,7.60,50,"385.50.5.0",1,1)'
-
-    cp
-    - v New_$ {CalFile} $ {
-    CalFile
-  }
-
-  else cd $wdir
-#store current status
-    dLDTres = $dDTres dLDTRMSres = $dDTRMSres echo Store limits $dLDTres,
-    $dLDTRMSres echo exec in `pwd`: trk_cal_digi.sh $1 $2 $3 $4 $5 1 $7.
-      / trk_cal_digi.sh $1 $2 $3 $4 $5 1 $7
-#restore old status
-          dDTres = $dLDTres dDTRMSres = $dLDTRMSres echo exec1done,
-    resume with old status $dDTres,
-    $dDTRMSres fi((iCalAct -= 1)) echo Continue while loop with $iCalAct done
+  ;;
+esac
+
+while [[ $dDTres > 0 ]]; do
+  nEvt=`echo "scale=0;$nEvt * 1./1." | bc`
+  #nEvt=`echo "scale=0;$nEvt * 1.1/1." | bc`
+  if [ $nEvt -gt $nEvtMax ]; then
+    nEvt=$nEvtMax
+  fi
 
+  fRange2=`echo "$fRange2 * 0.9" | bc`
+  compare_TRange2=`echo "$fRange2 < $TRange2Limit" | bc`
+  if  [[ $compare_TRange2 > 0 ]]; then
+   fRange2=$TRange2Limit
+  fi
+  
+  iCalAct=$iCalOpt
+  iIter=0
+  echo Enter while loop with $iCalAct in dir `pwd`
+  while [[ $iCalAct -gt 0 ]]; do  
+    cd $wdir/$cRun
+    echo start next loop $iIter with opt $iCalOpt and Act $iCalAct 
+    if [[ $iCalOpt -eq 1 ]] || [[ $iCalAct -gt 1 ]]; then 
+      root -b -q '../ana_digi_cal.C('$nEvt',93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
+      # update calibration parameter file, will only be active in next iteration 
+      if [[ $iIter = -10 ]] && [[ $iCalOpt = 1 ]]; then  # exploratory option when iIter set to 0 
+        echo Update Calibration file from ana_digi_cal
+        cp -v tofClust_${cRun}_set${cCalSet}.hst.root ../${cRun}_set${cCalSet}_93_1tofClust.hst.root
+        echo 20000 > TOffAvOff.res
+        echo 20000 > TOffAvRMS.res
+      else
+        root -b -q '../ana_trks.C('$nEvt','$iSel','$iGenCor',"'$cRun'","'$cCalSet2'",'$iSel2','$iTraSetup','$fRange1','$fRange2','$Deadtime',"'$CalIdMode'",1,1,'$iCalSet','$iCalAct')'
+      #root -l 'ana_trksi.C(-1,10,1,"385.50.5.0","000014500_020",20,1,1.90,7.60,50,"385.50.5.0",1,1)'
+  
+        cp -v New_${CalFile} ${CalFile}  
+      fi
+    else 
       cd $wdir
-      / $cRun Tres =`cat TOffAvOff.res` TRMSres =`cat TOffAvRMS.res`
-
-                                                 if [[$Tres = 0]];
-  then Tres =
-    1 fi dTdif                       =`echo "$dDTres - $Tres"
-                | bc` compare_result =`echo "$Tres < $dDTres"
-                                      | bc`
-
-                                      dTRMSdif =`echo "$dDTRMSres - $TRMSres"
-                                                | bc` compare_RMS =`echo
-                                                                   "$TRMSres < "
-                                                                   "$dDTRMSres"
-                                                                   | bc`
-
-                                                                   echo at
-                                                                     iter =
-      $iter got TOff = $Tres,
-       compare to $dDTres, dTdif = $dTdif, result = $compare_result,
-       TRMS = $TRMSres, old $dDTRMSres, dif = $dTRMSdif,
-       result = $compare_RMS
-
-       ((compare_result += $compare_RMS)) echo CMPR result_summary :
-    $compare_result
-
-#if[$iter = 1]; then
-#exit 0 #for debugging
-#fi
-
-    if [[$compare_result > 0]];
-  then if [[$Tres = 0]]; then
+      # store current status 
+      dLDTres=$dDTres
+      dLDTRMSres=$dDTRMSres
+      iLCalOpt=$iCalOpt
+      echo Store limits $dLDTres, $dLDTRMSres
+      (( iShLev += 1 ))
+      echo exec in `pwd` at level $iShLev: trk_cal_digi $1 $2 $3 $4 $5 1 $7
+      trk_cal_digi $1 $2 $3 $4 $5 1 $7
+      (( iShLev -= 1 ))
+      # restore old status
+      dDTres=$dLDTres
+      dDTRMSres=$dLDTRMSres
+      iCalOpt=$iLCalOpt
+      echo exec1done, resume old CalOpt $iCalOpt with status $dDTres, $dDTRMSres
+    fi
+    (( iCalAct -= 1 ))
+    (( iIter   += 1 ))
+    echo Continue while loop with $iCalAct
+  done
+  
+  cd $wdir/$cRun
+  Tres=`cat TOffAvOff.res`
+  TRMSres=`cat TOffAvRMS.res`
+
+  if [[ $Tres = 0 ]]; then
+    Tres=1
+  fi
+  dTdif=`echo "$dDTres - $Tres" | bc`
+  compare_result=`echo "$Tres < $dDTres" | bc`
+
+  dTRMSdif=`echo "$dDTRMSres - $TRMSres" | bc`
+  compare_RMS=`echo "$TRMSres < $dDTRMSres" | bc`
+
+  echo at iter=$iter got TOff = $Tres, compare to $dDTres, dTdif = $dTdif, result = $compare_result, TRMS = $TRMSres, old $dDTRMSres, dif = $dTRMSdif, result = $compare_RMS 
+
+  ((compare_result += $compare_RMS))
+  echo CMPR result_summary: $compare_result 
+
+#  if [ $iter = 1 ]; then 
+#    exit 0  # for debugging 
+#  fi
+
+  if [[ $compare_result > 0 ]]; then
+    if [[ $Tres = 0 ]]; then
       Tres=1
     fi
     dDTres=$Tres
     dDTRMSres=$TRMSres
+    echo Store new res values $dDTres, $dDTRMSres
     (( dDTRMSres -= 1 ))  # next attempt should be at least 1ps better for continuation
     cp -v New_${CalFile} ${CalFile}  
     cp -v New_${CalFile} ${CalFile}_$iter  
@@ -197,9 +233,17 @@ case $iCalOpt in
 done
 
 cd $wdir/$cRun
-#generate full statistics digi file 
-root -b -q '../ana_digi_cal.C(-1,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
+# generate full statistics digi file 
+if [[ $iShLev = 0 ]]; then 
+  root -b -q '../ana_digi_cal.C(-1,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
+fi
 
 cd $wdir
 
-mv -v slurm-${SLURM_JOB_ID}.out ${outdir}/TrkCalDigi_${cRun}_${iCalSet}_${iSel2}_${iCalIdMode}.out
+if [[ $iShLev = 0 ]]; then 
+  mv -v slurm-${SLURM_JOB_ID}.out ${outdir}/TrkCalDigi_${cRun}_${iCalSet}_${iSel2}_${CalIdMode}.out
+fi
+
+} #end of function body
+
+trk_cal_digi $1 $2 $3 $4 $5 $6 $7 $8
diff --git a/macro/beamtime/mcbm2018/gen_digi.sh b/macro/beamtime/mcbm2018/gen_digi.sh
index bd80b8e928..1ea3879fcd 100755
--- a/macro/beamtime/mcbm2018/gen_digi.sh
+++ b/macro/beamtime/mcbm2018/gen_digi.sh
@@ -1,4 +1,10 @@
-# shell script to iterate clusterizer calibrations 
+#!/bin/bash
+# shell script to apply clusterizer calibrations
+#SBATCH -J gen_digi
+#SBATCH -D /lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/mcbm2018
+#SBATCH --time=8:00:00
+#SBATCH --mem=2000
+##SBATCH --partition=long
 cRun=$1
 
 iCalSet=$2
@@ -13,10 +19,6 @@ iCalSet=$2
 
 iSel2=$3
 
-if((${iSel2}<0));then
- ((iBRef=-$iSel2))
-fi
-
 cCalSet=$iCalSet
 if (( iCalSet<100000000 )); then 
 cCalSet="0"$iCalSet
@@ -41,25 +43,45 @@ CalIdMode=$5
 if [[ ${CalIdMode} = "" ]]; then
  echo use native calibration file 
  CalIdMode=${cRun}
- CalFile=${cRun}_set${cCalSet}_93_1tofCosmicClust.hst.root
+ CalFile=${cRun}_set${cCalSet}_93_1tofClust.hst.root
 else 
- CalFile=${CalIdMode}_set${cCalSet}_93_1tofCosmicClust.hst.root
- RunFile=${cRun}_set${cCalSet}_93_1tofCosmicClust.hst.root
+ CalFile=${CalIdMode}_set${cCalSet}_93_1tofClust.hst.root
+ RunFile=${cRun}_set${cCalSet}_93_1tofClust.hst.root
 # rm ${RunFile}
 # ln -s ${CalFile} ${RunFile} 
  echo use calibrations from  ${CalFile}
 fi
 
+CalIdSet=$6
+if [[ ${CalIdSet} = "" ]]; then
+    echo use native calibration file
+    CalIdSet=$cCalSet
+else
+    CalFile=${CalIdMode}_set${CalIdSet}_93_1tofClust.hst.root    
+fi
+
 echo gen_digi for $cRun with iDut=$iDut, iRef=$iRef, iSet=$iCalSet, iSel2=$iSel2, iBRef=$iBRef, Deadtime=$Deadtime, CalFile=$CalFile
 
+if [ -e /lustre/cbm ]; then
+source /lustre/cbm/users/nh/CBM/cbmroot/trunk/build/config.sh 
+wdir=/lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/mcbm2018
+outdir=/lustre/cbm/users/nh/CBM/cbmroot/trunk/macro/beamtime/mcbm2018/${cRun}
+else 
+wdir=`pwd`
+outdir=${wdir}/${cRun}
+fi
+
+cd $wdir
 mkdir $cRun
 cd    $cRun 
 cp    ../.rootrc .
 cp    ../rootlogon.C .
-cp -v ../${CalFile} ./${RunFile}
-#root -b -q './ana_digi.C(-1,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime') '
-#root -b -q '../ana_digi_dev.C(-1,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
-root -b -q '../ana_digi_cal.C(-1,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
+cp -v ../${CalFile}  .
+#root -b -q '../ana_digi_cal.C(100000,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
+root -b -q '../ana_digi_cal_all.C(-1,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
 #root -b -q '../ana_digi_cos.C(-1,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
 #root -b -q '../ana_digi_star.C(-1,93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
 
+cd ..
+
+mv -v slurm-${SLURM_JOB_ID}.out ${outdir}/GenDigi_${cRun}_${iCalSet}_${iSel2}_${CalIdMode}.out
diff --git a/macro/beamtime/mcbm2018/iter_tracks.sh b/macro/beamtime/mcbm2018/iter_tracks.sh
index a40fe970be..3bcc581171 100755
--- a/macro/beamtime/mcbm2018/iter_tracks.sh
+++ b/macro/beamtime/mcbm2018/iter_tracks.sh
@@ -10,21 +10,47 @@ X=$((${SLURM_ARRAY_TASK_ID} - 0))
 XXX=$(printf "%03d" "$X")
 
 cRun=$1
-#cRun='r0047_20170915_1636_DT50_Req3'
-#cRun='r0002_20171215_1810_DT50_Req-2'
-iDut=0; iRef=10; iSel2=20
-#iDut=911; iRef=900; iSel2=-910
+
+iTraSetup=$2
+
+#which file should be analyzed ?  
+cSet=$3
+if [[ $cSet = "" ]]; then 
+    cSet="010020500_500"
+    #cSet="900041500_901"
+    #cSet="900041500_500"
+fi
+
+# extract iCalSet from cSet
+i1=0
+while [ "${cSet:$i1:1}" = "0" ]; do
+(( i1 += 1 ))
+done
+i2=0
+while [ "${cSet:$i2:1}" != "_" ] && [ $i2 -lt  ${#cSet} ]; do
+(( i2 += 1 ))
+done
+(( i2 -= i1 ))
+iCalSet=${cSet:$i1:$i2}
+echo got i1=$i1, i2=$i2, iCalSet=$iCalSet from $cSet
+
+cCalId=$4;
+if [[ $cCalId = "" ]]; then 
+    cCalId=$cRun;
+fi
+
+iMc=0
+McId=${cRun:0:4}
+if [ "$McId" = "mcbm" ]; then 
+  echo processing MC simulation
+  iMc=1
+fi
 
 # what should be done ?
+iDut=12; iRef=22; iSel2=2
 ((iSel=$iDut*1000+$iRef))
-#which file should be analyzed ?  
-#cSet="010020000_020"
-cSet="000010500_020"
-#cSet="020040030_030"
 
-cCalId=$cRun;
-iTraSetup=3
-nEvt=1000000
+nEvt=100000
 dDTres=2000
 dDTRMSres=2000
 iter=0;
@@ -39,15 +65,17 @@ outdir=${wdir}/${cRun}
 fi
 
 # frange2 limits chi2
-fRange2=5.5
-TRange2Limit=3.5
+fRange2=7.
+TRange2Limit=2.5 
 
 #frange1 limits DT spectrum range 
-fRange1=1.5
-TRange1Limit=0.8
+fRange1=3.
+TRange1Limit=1.5
 dDeadtime=50
 
-mkdir ${cRun}
+if [ ! -e ${cRun} ]; then 
+  mkdir $cRun
+fi
 cd ${cRun}
 cp ../.rootrc .
 cp ../rootlogon.C .
@@ -56,19 +84,27 @@ cp ../rootlogon.C .
 rm -v ${cRun}_tofFindTracks.hst.root
 rm -v TCalib.res
 
-if [[ $iter > 0 ]]; then
+if [[ $iter -gt 0 ]]; then
  cp -v  ${cRun}_tofFindTracks.hst${iter}.root  ${cRun}_tofFindTracks.hst.root
 fi
- 
+
+nEvtMax=0
+(( nEvtMax = nEvt*10 ))
+
 while [[ $dDTres > 0 ]]; do
 
-nEvt=`echo "$nEvt * 1.3" | bc`
+nEvt=`echo "scale=0;$nEvt * 1./1." | bc`
+#nEvt=`echo "scale=0;$nEvt * 1.1/1." | bc`
+
+if [ $nEvt -gt $nEvtMax ]; then
+    nEvt=$nEvtMax
+fi
 
 #((fRange2 /= 2))
 #if((${fRange2}<$Range2Limit));then
 # ((fRange2=$Range2Limit))
 #fi
-fRange2=`echo "$fRange2 * 0.95" | bc`
+fRange2=`echo "$fRange2 * 0.8" | bc`
 compare_TRange2=`echo "$fRange2 < $TRange2Limit" | bc`
 if  [[ $compare_TRange2 > 0 ]]; then
 fRange2=$TRange2Limit
@@ -79,7 +115,7 @@ fi
 #if((${fRange1}<1));then
 # ((fRange1=1))
 #fi
-fRange1=`echo "$fRange1 * 0.95" | bc`
+fRange1=`echo "$fRange1 * 0.8" | bc`
 compare_TRange=`echo "$fRange1 < $TRange1Limit" | bc`
 if  [[ $compare_TRange > 0 ]]; then
 fRange1=$TRange1Limit
@@ -87,14 +123,19 @@ fi
 
 # correction modes: 2 - TOff from Tt, 3 - Pull t, 4 - x, 5 - y, 6 - z, >10 - Pull t of individual stations 
 #for iCal in 3 2 10 11 12 13 14 15 4 5; do
+#for iCal in 3 ; do
 for iCal in 3 4 5; do
 #for iCal in 3 2 4; do
 #for iCal in 3 2 ; do
 #for iCal in 2 ; do
     nIt=1
+    if [ $iter -eq 0 ] && [ $iMc -eq 1 ]; then
+      echo skip iCal $iCal for MC calibration
+      iCal=5
+    fi
     while [[ $nIt > 0 ]]; do
 	((iter += 1))
-	root -b -q '../ana_trks.C('$nEvt','$iSel','$iCal',"'$cRun'","'$cSet'",'$iSel2','$iTraSetup','$fRange1','$fRange2','$dDeadtime',"'$cCalId'")'
+	root -b -q '../ana_trks.C('$nEvt','$iSel','$iCal',"'$cRun'","'$cSet'",'$iSel2','$iTraSetup','$fRange1','$fRange2','$dDeadtime',"'$cCalId'",1,1,'$iCalSet',1,'$iMc')'
 	cp -v tofFindTracks.hst.root ${cRun}_tofFindTracks.hst.root
 	cp -v tofFindTracks.hst.root ${cRun}_tofFindTracks.hst${iter}.root
 	cp -v tofAnaTestBeam.hst.root ${cRun}_TrkAnaTestBeam.hst.root
@@ -111,6 +152,10 @@ fi
 ((TRMSres=$iTres%1000))
 ((iTres -= TRMSres ))
 ((Tres   = iTres / 1000)) 
+
+if [[ $Tres = 0 ]]; then
+    Tres=1
+fi
 dTdif=`echo "$dDTres - $Tres" | bc`
 compare_result=`echo "$Tres < $dDTres" | bc`
 
@@ -130,12 +175,16 @@ if [[ $compare_result > 0 ]]; then
   dDTRMSres=$TRMSres
 else
   dDTres=0
+  rm ../${cRun}_tofFindTracks.hst.root
   cp -v  tofFindTracks.hst.root  ../${cRun}_tofFindTracks.hst.root
+  cp -v  tofFindTracks.hst.root  ./${cRun}_${cSet}._${iTraSetup}_tofFindTracks.hst.root  # keep a copy 
+  rm ../${cRun}_TrkAnaTestBeam.hst.root
   cp -v  tofAnaTestBeam.hst.root ../${cRun}_TrkAnaTestBeam.hst.root
 fi
 
 done
 
-
-mv -v slurm-${SLURM_ARRAY_JOB_ID}_${SLURM_ARRAY_TASK_ID}.out ${outdir}/Calib_${cRun}_${cSet}.out
+cd ..
+#mv -v slurm-${SLURM_ARRAY_JOB_ID}_${SLURM_ARRAY_TASK_ID}.out ${outdir}/IterTrack_${cRun}_${cSet}.out
+mv -v slurm-${SLURM_JOB_ID}.out ${outdir}/IterTrack_${cRun}_${cSet}.out
 
diff --git a/macro/beamtime/mcbm2020/init_cal_all.sh b/macro/beamtime/mcbm2020/init_cal_all.sh
index ed1dce89e3..a7bebd05e5 100755
--- a/macro/beamtime/mcbm2020/init_cal_all.sh
+++ b/macro/beamtime/mcbm2020/init_cal_all.sh
@@ -74,14 +74,14 @@ do
     (( nEvi = nEvi0 + (icallev-1)*nEvi1 ))
     optList=`echo " $nEvi,$icallev$icalmod,$iCalSel0,$iDut,0 "`$optList
     if [ $iMRef -ne 14 ]; then 
-	optList=`echo " $nEvi,$icallev$icalmod,$iCalSel1,$iMRef,0 "`$optList
+	  optList=`echo " $nEvi,$icallev$icalmod,$iCalSel1,$iMRef,0 "`$optList
     else 
-	for iMod in 40  10 
-	do
+	  for iMod in 40  10 
+	  do
 	    if [ $iMod -ne $iDut ]; then
-		optList=`echo " $nEvi,$icallev$icalmod,$iCalSel1,$iMod,0 "`$optList
+		  optList=`echo " $nEvi,$icallev$icalmod,$iCalSel1,$iMod,0 "`$optList
 	    fi
-	done
+	  done
     fi
     if [ $icallev -lt 7 ]; then
       optList=`echo " $nEvi,$icallev$icalmod,$iCalSel0,$iBRef,50 "`$optList 
diff --git a/macro/beamtime/mcbm2020/trk_cal_digi.sh b/macro/beamtime/mcbm2020/trk_cal_digi.sh
index 764e7e7b45..ffe8009884 100755
--- a/macro/beamtime/mcbm2020/trk_cal_digi.sh
+++ b/macro/beamtime/mcbm2020/trk_cal_digi.sh
@@ -159,7 +159,7 @@ while [[ $dDTres > 0 ]]; do
   while [[ $iCalAct -gt 0 ]]; do  
     cd $wdir/$cRun
     echo Current loop with CalAct $iCalAct and CalOpt $iCalOpt
-    if [[ $iCalOpt = 1 ]] || [[ $iCalAct > 1 ]]; then 
+    if [[ $iCalOpt = 1 ]] || [[ $iCalAct -gt 1 ]]; then 
       root -b -q '../ana_digi_cal.C('$nEvt',93,1,'$iRef',1,"'$cRun'",'$iCalSet',1,'$iSel2','$Deadtime',"'$CalIdMode'") '
       # update calibration parameter file, will only be active in next iteration 
       if [[ $iIter = -10 ]] && [[ $iCalOpt = 1 ]]; then  # exploratory option when iIter set to 0 
diff --git a/macro/mcbm/geometry/tof/create_parfiles.sh b/macro/mcbm/geometry/tof/create_parfiles.sh
index 7d384b89d8..9b0af86b47 100755
--- a/macro/mcbm/geometry/tof/create_parfiles.sh
+++ b/macro/mcbm/geometry/tof/create_parfiles.sh
@@ -10,19 +10,22 @@ root -l -q Create_TOF_Geometry_${VGEO}.C
 #cp tof_v14a.root tof_${VGEO}.root
 cp -v tof_${VGEO}.geo.root ../../../../geometry/tof/
 
-cp -v tof_${VGEO}_geo.root ../../../../geometry/tof/geofile_tof_${VGEO}.root
-cp -v tof_${VGEO}_geo.root  ./geofile_tof_${VGEO}.root
+#cp -v tof_${VGEO}_geo.root ../../../../geometry/tof/geofile_tof_${VGEO}.root
+#cp -v tof_${VGEO}_geo.root  ./geofile_tof_${VGEO}.root
 
 CURDIR=`pwd`
 cd ../..
 rm -v test*.root
 rm -v ${Setup}*.root
 root -l -q './mcbm_transport.C(0,"'$Setup'")'
-#cp -v ./test.geo.root     ./geometry/tof/geofile_tof_${VGEO}.root
+#cp -v ./${Setup}.geo.root     ./geometry/tof/geofile_tof_${VGEO}.root
+#cp -v ./${Setup}.geo.root ../../geometry/tof/geofile_tof_${VGEO}.root
+cp -v ./test.geo.root     ./geometry/tof/geofile_tof_${VGEO}.root
 cp -v ./test.geo.root ../../geometry/tof/geofile_tof_${VGEO}.root
 cp -v ./test.par.root     ./geometry/tof/tof_${Setup}.par.root
 cd $CURDIR
 
-root -l 'create_digipar.C("tof_'${VGEO}'")'
-cp tof_${VGEO}.digi.par      ../../../../parameters/tof/
+rm ../../../../parameters/tof/tof_${VGEO}.digi.par
+#root -l 'create_digipar.C("tof_'${VGEO}'")'
+#cp tof_${VGEO}.digi.par      ../../../../parameters/tof/
 #cp geofile_tof_${VGEO}.root  ../../../../geometry/tof/
-- 
GitLab