From 0078c6e020e2bf9960c73e545f5d9886b85bdec6 Mon Sep 17 00:00:00 2001
From: Norbert Herrmann <n.herrmann@physi.uni-heidelberg.de>
Date: Sat, 3 Apr 2021 16:06:28 +0200
Subject: [PATCH] fix stability issues

---
 macro/beamtime/mcbm2021/dis_digi.C            | 18 +++++++-
 macro/beamtime/mcbm2021/init_cal_all.sh       |  7 +--
 macro/mcbm/geometry/tof/create_parfiles.sh    |  8 ++--
 reco/detectors/tof/CbmTofEventClusterizer.cxx | 46 +++++++++----------
 4 files changed, 46 insertions(+), 33 deletions(-)

diff --git a/macro/beamtime/mcbm2021/dis_digi.C b/macro/beamtime/mcbm2021/dis_digi.C
index 814d1493fd..f70ffb1fc1 100644
--- a/macro/beamtime/mcbm2021/dis_digi.C
+++ b/macro/beamtime/mcbm2021/dis_digi.C
@@ -7,7 +7,8 @@ void dis_digi(Int_t nEvents = 100, Int_t calMode = 93, Int_t calSel = 1, Int_t c
   Int_t iVerbose = 1;
   if (cCalId == "") cCalId = cFileId;
   TString FId = cFileId;
-  TString cRun(FId(0, 3));
+  Int_t iNLen = FId.First(".");
+  TString cRun(FId(0, iNLen));
   Int_t iRun = cRun.Atoi();
   cout << "dis_digi for Run " << iRun << endl;
 
@@ -398,6 +399,21 @@ void dis_digi(Int_t nEvents = 100, Int_t calMode = 93, Int_t calSel = 1, Int_t c
       //tofFindTracks->SetStation(29, 6, 0, 1);
       break;
 
+    case 11:  // for calibration mode of 2-stack & test counters
+      iMinNofHits   = 4;
+      iNStations    = 9;
+      iNReqStations = 5;
+      tofFindTracks->SetStation(0, 5, 0, 0);
+      tofFindTracks->SetStation(1, 0, 4, 1);
+      tofFindTracks->SetStation(2, 0, 3, 1);
+      tofFindTracks->SetStation(3, 0, 4, 0);
+      tofFindTracks->SetStation(4, 0, 3, 2);
+      tofFindTracks->SetStation(5, 9, 0, 0);
+      tofFindTracks->SetStation(6, 9, 1, 0);
+      tofFindTracks->SetStation(7, 9, 0, 1);
+      tofFindTracks->SetStation(8, 9, 1, 1);
+      break;
+
     case 2:
       iMinNofHits   = 3;
       iNStations    = 14;
diff --git a/macro/beamtime/mcbm2021/init_cal_all.sh b/macro/beamtime/mcbm2021/init_cal_all.sh
index 50e1564805..5338124cb4 100755
--- a/macro/beamtime/mcbm2021/init_cal_all.sh
+++ b/macro/beamtime/mcbm2021/init_cal_all.sh
@@ -36,8 +36,8 @@ fi
 echo cCalSet = $cCalSet
 #iSet=0
 #lastOpt=''
-nEvi0=50000 # start value965311965311
-nEvi1=50000 # increment 
+nEvi0=100000 # start value965311965311
+nEvi1=100000 # increment 
 
 if [ -e /lustre ]; then
 source /lustre/cbm/users/nh/CBM/cbmroot/trunk/build/config.sh 
@@ -56,7 +56,8 @@ cp .rootrc ${cRun}
 cd ${cRun}
 
 # Global variables, for for-loops
-iRestart=0
+iRestart=0   
+#iRestart=1  # use copied calibration file with walk corrections 
 #iRestart=33
 iStep=0
 iStepLast=0
diff --git a/macro/mcbm/geometry/tof/create_parfiles.sh b/macro/mcbm/geometry/tof/create_parfiles.sh
index 9b0af86b47..2237645c58 100755
--- a/macro/mcbm/geometry/tof/create_parfiles.sh
+++ b/macro/mcbm/geometry/tof/create_parfiles.sh
@@ -20,12 +20,12 @@ rm -v ${Setup}*.root
 root -l -q './mcbm_transport.C(0,"'$Setup'")'
 #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
+cp -v ./data/test.geo.root     ./geometry/tof/geofile_tof_${VGEO}.root
+cp -v ./data/test.geo.root ../../geometry/tof/geofile_tof_${VGEO}.root
+cp -v ./data/test.par.root     ./geometry/tof/tof_${Setup}.par.root
 cd $CURDIR
 
-rm ../../../../parameters/tof/tof_${VGEO}.digi.par
+#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/
diff --git a/reco/detectors/tof/CbmTofEventClusterizer.cxx b/reco/detectors/tof/CbmTofEventClusterizer.cxx
index 6a15a572ae..1d7d110fe0 100644
--- a/reco/detectors/tof/CbmTofEventClusterizer.cxx
+++ b/reco/detectors/tof/CbmTofEventClusterizer.cxx
@@ -656,17 +656,6 @@ Bool_t CbmTofEventClusterizer::InitParameters()
   if (fOutHstFileName == "") { fOutHstFileName = "./tofEventClust.hst.root"; }
 
   LOG(info) << " Hst Output filename = " << fOutHstFileName;
-  /*
-	 if(fiBeamRefAddr == 0) {  // initialize defaults of sep14
-	 fiBeamRefType  = 5;
-	 fiBeamRefSm    = 1;
-	 fiBeamRefDet   = 0;
-	 fiBeamAddRefMul= 0;
-	 }
-	 if(fSelId == 0) {  // initialize defaults of sep14
-	 fSelId=4;
-	 }
-	 */
 
   LOG(info) << "<I>  BeamRefType = " << fiBeamRefType << ", Sm " << fiBeamRefSm << ", Det " << fiBeamRefDet
             << ", MulMax " << fiBeamRefMulMax;
@@ -3349,24 +3338,28 @@ Bool_t CbmTofEventClusterizer::WriteHistos()
               if (kTRUE) {  // fit gaussian around most abundant bin
                 TH1* hTy = (TH1*) htempTOff->ProjectionY(Form("%s_py%d", htempTOff->GetName(), iCh), iCh + 1, iCh + 1);
                 if (hTy->GetEntries() > WalkNHmin) {
-                  Double_t dFMean    = hTy->GetBinCenter(hTy->GetMaximumBin());
-                  Double_t dFLim     = 2.0;  // CAUTION, fixed numeric value
-                  Double_t dBinSize  = hTy->GetBinWidth(1);
-                  dFLim              = TMath::Max(dFLim, 5. * dBinSize);
-                  TFitResultPtr fRes = hTy->Fit("gaus", "SQM0", "", dFMean - dFLim, dFMean + dFLim);
-                  if (TMath::Abs(TMean - fRes->Parameter(1)) > 5.)
-                    LOG(warn) << "CalibF "
-                              << Form("TSRC %d%d%d%d gaus %8.2f %8.2f %8.2f for "
-                                      "TM %8.2f, YM %6.2f",
-                                      iSmType, iSm, iRpc, iCh, fRes->Parameter(0), fRes->Parameter(1),
-                                      fRes->Parameter(2), TMean, YMean);
-                  TMean = fRes->Parameter(1);  //overwrite mean
+                  Double_t dNPeak = hTy->GetBinContent(hTy->GetMaximumBin());
+                  if (dNPeak > WalkNHmin * 0.5) {
+                    Double_t dFMean    = hTy->GetBinCenter(hTy->GetMaximumBin());
+                    Double_t dFLim     = 2.0;  // CAUTION, fixed numeric value
+                    Double_t dBinSize  = hTy->GetBinWidth(1);
+                    dFLim              = TMath::Max(dFLim, 5. * dBinSize);
+                    TFitResultPtr fRes = hTy->Fit("gaus", "SQM", "", dFMean - dFLim, dFMean + dFLim);
+                    if (TMath::Abs(TMean - fRes->Parameter(1)) > 5.)
+                      LOG(warn) << "CalibF "
+                                << Form("TSRC %d%d%d%d gaus %8.2f %8.2f %8.2f for "
+                                        "TM %8.2f, YM %6.2f",
+                                        iSmType, iSm, iRpc, iCh, fRes->Parameter(0), fRes->Parameter(1),
+                                        fRes->Parameter(2), TMean, YMean);
+                    TMean = fRes->Parameter(1);  //overwrite mean
+                  }
                 }
               }
               Double_t dTYOff = YMean / fDigiBdfPar->GetSigVel(iSmType, iSm, iRpc);
 
               if (fiBeamRefAddr == iUniqueId) {
                 if (iFindT0 == 1) continue;  // action already done
+                Double_t dTWidth = 0;
                 // don't shift time of reference counter on average
                 if (iCh == 0) {
                   Double_t dW = 0.;
@@ -3381,14 +3374,16 @@ Bool_t CbmTofEventClusterizer::WriteHistos()
                         Double_t dFLim    = 0.5;  // CAUTION, fixed numeric value
                         Double_t dBinSize = hTy->GetBinWidth(1);
                         dFLim             = TMath::Max(dFLim, 5. * dBinSize);
-                        TFitResultPtr fRes = hTy->Fit("gaus", "SQM0", "", dFMean - dFLim, dFMean + dFLim);
+                        TFitResultPtr fRes = hTy->Fit("gaus", "SQM", "", dFMean - dFLim, dFMean + dFLim);
                         LOG(debug) << "CalibC "
                                    << Form(" TSRC %d%d%d%d gaus %8.2f %8.2f %8.2f ", iSmType, iSm, iRpc, iRefCh,
                                            fRes->Parameter(0), fRes->Parameter(1), fRes->Parameter(2));
                         TBeamRefMean += fRes->Parameter(1) * dWCh;  //overwrite mean
+                        dTWidth += fRes->Parameter(2) * dWCh;       //calculate width
                       }
                       else {
                         TBeamRefMean += ((TProfile*) htempTOff_pfx)->GetBinContent(iRefCh + 1) * dWCh;
+                        dTWidth += ((TProfile*) htempTOff_pfx)->GetBinError(iRefCh + 1) * dWCh;
                       }
                       TBeamRefMean += dWCh *  // enforce <offset>=0
                                       fvCPTOff[iSmType][iSm * iNbRpc + iRpc][iCh][0];
@@ -3396,8 +3391,9 @@ Bool_t CbmTofEventClusterizer::WriteHistos()
                   }
                   if (dW > 0.) {
                     TBeamRefMean /= dW;
+                    dTWidth /= dW;
                     // TBD apply offset all other detectors since beam counter will not be shifted
-                    LOG(info) << "<I> T0 shift all offsets by " << TBeamRefMean;
+                    LOG(info) << "<I> T0 shift all offsets by " << TBeamRefMean << ", AvWidth " << dTWidth;
                   }
                   else
                     TBeamRefMean = 0.;
-- 
GitLab