From 9cdc46643b3716e3479f1df990b4c9729e59668b Mon Sep 17 00:00:00 2001
From: "s.zharko@gsi.de" <s.zharko@gsi.de>
Date: Wed, 19 Jun 2024 13:04:22 +0200
Subject: [PATCH] mcbm2024 data recostruction chain:

- adding TsEventHeader and TimeSlice. branch to the run_inspect_digi_timeslice.C macro
---
 macro/beamtime/mcbm2024/mcbm_event_reco_L1.C | 20 ++------------
 macro/qa/configs/qa_tasks_config_mcbm.yaml   | 14 ++++++----
 macro/qa/run_recoQa.C                        |  1 +
 macro/run/CMakeLists.txt                     |  2 +-
 macro/run/run_inspect_digi_timeslice.C       |  2 ++
 reco/L1/CbmCaMCModule.h                      |  1 +
 reco/steer/CbmSourceDigiTimeslice.cxx        | 29 ++++++++++++++++++++
 reco/steer/CbmSourceDigiTimeslice.h          | 11 ++++++++
 8 files changed, 57 insertions(+), 23 deletions(-)

diff --git a/macro/beamtime/mcbm2024/mcbm_event_reco_L1.C b/macro/beamtime/mcbm2024/mcbm_event_reco_L1.C
index ff008712b5..f8a71483f7 100644
--- a/macro/beamtime/mcbm2024/mcbm_event_reco_L1.C
+++ b/macro/beamtime/mcbm2024/mcbm_event_reco_L1.C
@@ -9,9 +9,9 @@
 //
 // --------------------------------------------------------------------------
 
-#include <math.h>
-#include <stdio.h>
-#include <string.h>
+#include <cmath>
+#include <cstdio>
+#include <string>
 
 /// FIXME: Disable clang formatting to keep easy parameters overview
 /* clang-format off */
@@ -427,20 +427,6 @@ Bool_t mcbm_event_reco_L1(UInt_t uRunId                   = 3105,
     Int_t stsAddress10 = CbmStsAddress::GetAddress(2, 2, 1, 1, 0, 0);  // U2 L2 M1  6 cm
     Int_t stsAddress11 = CbmStsAddress::GetAddress(2, 2, 1, 2, 0, 0);  // U2 L2 M2  6 cm
 
-    // // setup available up to sts_v22f. Available if reconstructing from tsa files (AB 14.05.2024)
-    // Int_t stsAddress00 = 0;
-    // Int_t stsAddress01 = CbmStsAddress::GetAddress(0, 0, 1, 0, 0, 0);  // U0 L0 M0  6 cm
-    // Int_t stsAddress02 = CbmStsAddress::GetAddress(0, 0, 1, 1, 0, 0);  // U0 L0 M1  6 cm
-    // Int_t stsAddress03 = CbmStsAddress::GetAddress(0, 1, 1, 0, 0, 0);  // U0 L1 M0  6 cm
-    // Int_t stsAddress04 = CbmStsAddress::GetAddress(0, 1, 1, 1, 0, 0);  // U0 L1 M1  6 cm
-    // Int_t stsAddress05 = CbmStsAddress::GetAddress(1, 0, 1, 0, 0, 0);  // U1 L0 M0  6 cm
-    // Int_t stsAddress06 = CbmStsAddress::GetAddress(1, 0, 1, 1, 0, 0);  // U1 L0 M1 12 cm
-    // Int_t stsAddress07 = CbmStsAddress::GetAddress(1, 1, 1, 0, 0, 0);  // U1 L1 M0  6 cm
-    // Int_t stsAddress08 = CbmStsAddress::GetAddress(1, 1, 1, 1, 0, 0);  // U1 L1 M1 12 cm
-    // Int_t stsAddress09 = CbmStsAddress::GetAddress(1, 2, 1, 0, 0, 0);  // U1 L2 M0  6 cm
-    // Int_t stsAddress10 = CbmStsAddress::GetAddress(1, 2, 1, 1, 0, 0);  // U1 L2 M1  6 cm
-    // Int_t stsAddress11 = CbmStsAddress::GetAddress(1, 2, 1, 2, 0, 0);  // U1 L2 M2  6 cm
-
     std::cout << "STS address00 " << std::dec << stsAddress00 << " " << std::hex << stsAddress00 << std::endl;
     std::cout << "STS address01 " << std::dec << stsAddress01 << " " << std::hex << stsAddress01 << std::endl;
     std::cout << "STS address02 " << std::dec << stsAddress02 << " " << std::hex << stsAddress02 << std::endl;
diff --git a/macro/qa/configs/qa_tasks_config_mcbm.yaml b/macro/qa/configs/qa_tasks_config_mcbm.yaml
index 42b8cec662..610491e967 100644
--- a/macro/qa/configs/qa_tasks_config_mcbm.yaml
+++ b/macro/qa/configs/qa_tasks_config_mcbm.yaml
@@ -19,11 +19,15 @@ qa:
       pull_station_%_pull_x: false
   CbmCaInputQaSts:
     # Specific configuration, provided in the Config structure of the QA-task
-    specific:
-      PullMeanThrsh: 0.1
-      PullWidthThrsh: 2.0
-      EffThrsh: 0.5
-      MaxDiffZStHit: 1.0
+    #specific:
+    #  McTrackCuts:
+    #    MinMom: 0.1 # [GeV/c]
+    #    MaxTheta: 60. # [grad]
+    #    IsPrimary: true
+    #  PullMeanThrsh: 0.1
+    #  PullWidthThrsh: 2.0
+    #  EffThrsh: 0.5
+    #  MaxDiffZStHit: 1.0
     check_list:
       station_position_ordering: true
       station_position_hit_delta_z: false
diff --git a/macro/qa/run_recoQa.C b/macro/qa/run_recoQa.C
index b93f19c8b6..a5774ec2a0 100644
--- a/macro/qa/run_recoQa.C
+++ b/macro/qa/run_recoQa.C
@@ -149,6 +149,7 @@ void run_recoQa(Int_t nEvents = -1, TString recFile = "2391_node8_0_0000.rec.roo
   // L1 CA track finder setup
   auto l1 = new CbmL1("CA");
   l1->SetMcbmMode();
+  // TODO: add l1->SetInitMode(CbmL1::EInitMode::Param), after !1862 will be merged
 
   // User configuration example for CA:
   //l1->SetConfigUser(srcDir + "/macro/L1/configs/ca_params_user_example.yaml");
diff --git a/macro/run/CMakeLists.txt b/macro/run/CMakeLists.txt
index 6f12f24920..b8427e5223 100644
--- a/macro/run/CMakeLists.txt
+++ b/macro/run/CMakeLists.txt
@@ -604,7 +604,7 @@ EndIf() # If(DEFINED ENV{RAW_DATA_PATH} )
 # ============================================================================
 
 Install(FILES .rootrc run_tra_file.C run_tra_beam.C run_digi.C run_reco.C run_qa.C run_unpack_online.C run_unpack_tsa.C create_mcbm_geo_setup.C
-              qa_config.cbm.yaml
+              run_inspect_digievents.C run_inspect_digi_timeslice.C qa_config.cbm.yaml
         DESTINATION share/cbmroot/macro/run
        )
 Install(PROGRAMS run_tests.sh
diff --git a/macro/run/run_inspect_digi_timeslice.C b/macro/run/run_inspect_digi_timeslice.C
index 36ed95289c..83607a31a9 100644
--- a/macro/run/run_inspect_digi_timeslice.C
+++ b/macro/run/run_inspect_digi_timeslice.C
@@ -13,6 +13,7 @@
 #if !defined(__CLING__)
 #include "CbmSourceDigiTimeslice.h"
 #include "CbmTaskInspectDigiEvents.h"
+#include "CbmTsEventHeader.h"
 
 #include <FairRunAna.h>
 #include <FairSystemInfo.h>
@@ -55,6 +56,7 @@ void run_inspect_digi_timeslice(TString inputFileName, TString outputFileName, U
 
   // -----   FairRunAna   ---------------------------------------------------
   FairRunOnline* run = new FairRunOnline();
+  run->SetEventHeader(new CbmTsEventHeader{});
   FairSource* source = new CbmSourceDigiTimeslice(inputFileName);
   run->SetSource(source);
   auto sink = new FairRootFileSink(outputFileName);
diff --git a/reco/L1/CbmCaMCModule.h b/reco/L1/CbmCaMCModule.h
index d2cb8ed62e..a7c6eb2f4a 100644
--- a/reco/L1/CbmCaMCModule.h
+++ b/reco/L1/CbmCaMCModule.h
@@ -399,6 +399,7 @@ namespace cbm::ca
     // ----- Reject MC points falling out of the time slice
     // STS, MuCh, TRD, TOF
     if constexpr (DetID != ca::EDetectorID::kMvd) {
+      // TODO: SZh 18.06.2024: Avoid dependency from CbmTimeSlice, if possible (no TimeSlice branch in the online unpack)
       double startT = fpTimeSlice->GetStartTime();
       double endT   = fpTimeSlice->GetEndTime();
 
diff --git a/reco/steer/CbmSourceDigiTimeslice.cxx b/reco/steer/CbmSourceDigiTimeslice.cxx
index d01d7d77e2..f2d9479a94 100644
--- a/reco/steer/CbmSourceDigiTimeslice.cxx
+++ b/reco/steer/CbmSourceDigiTimeslice.cxx
@@ -4,7 +4,11 @@
 
 #include "CbmSourceDigiTimeslice.h"
 
+#include "CbmTimeSlice.h"
+#include "CbmTsEventHeader.h"
+
 #include <FairRootManager.h>
+#include <FairRun.h>
 #include <Logger.h>
 
 #include <utility>
@@ -59,6 +63,26 @@ Bool_t CbmSourceDigiTimeslice::Init()
   FairRootManager* ioman = FairRootManager::Instance();
   assert(ioman);
 
+  auto* pEvtHeader = FairRun::Instance()->GetEventHeader();
+
+  //if (!(fTsEventHeader = dynamic_cast<CbmTsEventHeader*>(FairRun::Instance()->GetEventHeader()))) {
+  //  LOG(fatal)
+  //        << "CbmSourceDigiTimeslice::Init() no CbmTsEventHeader was added to the run. Without it, we can not store the UTC of the "
+  //       "Timeslices correctly. Hence, this causes a fatal. Please add it in the steering macro to the Run.";
+  //  return kFALSE;
+  //}
+
+  // TimeSlice. branch initialization
+  if (ioman->GetObject("TimeSlice.")) {
+    LOG(fatal) << "Source: Branch TimeSlice. already exists!";
+    return kFALSE;
+  }
+  else {
+    // NOTE: the max time of timeslice is 1.28e8, taken from CbmRecoUnpack.cxx
+    fTimeslice = new CbmTimeSlice(0., 1.28e8 + 1.28e6);
+    ioman->Register("TimeSlice.", "DAQ", fTimeslice, kTRUE);
+  }
+
   fBmonDigis = new std::vector<CbmBmonDigi>();
   if (kFALSE == RegisterVector<CbmBmonDigi>(ioman, fBmonDigis)) {
     return kFALSE;
@@ -134,6 +158,11 @@ Int_t CbmSourceDigiTimeslice::ReadEvent(UInt_t)
   /// FIXME: Add PSD and FSD to the output of RawDigis in algo/global and reco/app
   // << " PSD=" << results->StsDigis().size() << " FSD=" << results->StsDigis().size();
 
+  //fTsEventHeader->SetTsIndex(results->TsIndex());
+  //fTsEventHeader->SetTsStartTime(results->TsStartTime());
+
+  fTimeslice->SetStartTime(results->TsStartTime());
+
   std::move(results->BmonDigis().begin(), results->BmonDigis().end(), std::back_inserter(*fBmonDigis));
   std::move(results->StsDigis().begin(), results->StsDigis().end(), std::back_inserter(*fStsDigis));
   std::move(results->MuchDigis().begin(), results->MuchDigis().end(), std::back_inserter(*fMuchDigis));
diff --git a/reco/steer/CbmSourceDigiTimeslice.h b/reco/steer/CbmSourceDigiTimeslice.h
index e9dc667630..023cf816b2 100644
--- a/reco/steer/CbmSourceDigiTimeslice.h
+++ b/reco/steer/CbmSourceDigiTimeslice.h
@@ -14,6 +14,8 @@
 #include <string>
 #include <vector>
 
+class CbmTimeSlice;
+class CbmTsEventHeader;
 class FairRootManager;
 
 /** @class CbmSourceDigiTimeslice
@@ -102,6 +104,15 @@ class CbmSourceDigiTimeslice : public FairSource {
    **/
   Source_Type fSourceType = Source_Type::kONLINE;
 
+  /** Time-slice header (old version)
+   *  @remark The CbmTimeSlice class is about to be deprecated, one should use the CbmTsEventHeader class instead.
+   **/
+  CbmTimeSlice* fTimeslice = nullptr;
+
+  /** Time-slice event header **/
+  CbmTsEventHeader* fTsEventHeader = nullptr;
+
+
   /** Time-slice counter **/
   size_t fNumTs = 0;
 
-- 
GitLab