From dba31575c67ede41363f8caa2d17e63bd29c4d8c Mon Sep 17 00:00:00 2001
From: Felix Weiglhofer <weiglhofer@fias.uni-frankfurt.de>
Date: Mon, 6 May 2024 12:29:33 +0000
Subject: [PATCH] online: Load alignment matrix in par dump.

---
 reco/steer/CbmOnlineParWrite.cxx            | 48 ++++++++++++++++++---
 reco/steer/CbmOnlineParWrite.h              |  9 +++-
 services/online_par_dump/Application.cxx    |  6 ++-
 services/online_par_dump/ProgramOptions.cxx | 12 +++++-
 services/online_par_dump/ProgramOptions.h   |  1 +
 5 files changed, 66 insertions(+), 10 deletions(-)

diff --git a/reco/steer/CbmOnlineParWrite.cxx b/reco/steer/CbmOnlineParWrite.cxx
index b4d01d7238..14a6d5bd8d 100644
--- a/reco/steer/CbmOnlineParWrite.cxx
+++ b/reco/steer/CbmOnlineParWrite.cxx
@@ -94,7 +94,7 @@ void CbmOnlineParWrite::AddTof()
   TString cCalId        = "490.100.5.0";
   Int_t iCalSet         = 30040500;  // calibration settings
 
-  switch (fSetupType) {
+  switch (fConfig.setupType) {
     case Setup::mCBM2022:
       cCalId  = "2391.5.000";
       iCalSet = 22002500;
@@ -161,7 +161,7 @@ void CbmOnlineParWrite::AddSts()
                                                                    "mcbm2021");
 
   // TODO: is it possible to read these values from a parameter file?
-  if (fSetupType == Setup::mCBM2022) {
+  if (fConfig.setupType == Setup::mCBM2022) {
 
     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
@@ -202,7 +202,7 @@ void CbmOnlineParWrite::AddSts()
     // auto *setup = CbmStsSetup::Instance();
     // setup->Init(); // reuse setup from global setup file
   }
-  else if (fSetupType == Setup::mCBM2024) {
+  else if (fConfig.setupType == Setup::mCBM2024) {
     uint32_t addr01 = 0x10008012;
     uint32_t addr02 = 0x10018012;
     uint32_t addr03 = 0x10008412;
@@ -270,7 +270,7 @@ void CbmOnlineParWrite::AddSts()
   fRun->AddTask(recoSts);
 }
 
-void CbmOnlineParWrite::Run(Setup setup)
+void CbmOnlineParWrite::Run(const Config& config)
 {
   // Copied and adjusted from macro/beamtime/mcbm2022/trd_hitfinder_run.C
 
@@ -280,7 +280,7 @@ void CbmOnlineParWrite::Run(Setup setup)
   }
   callOnce = false;
 
-  fSetupType = setup;
+  fConfig = config;
 
   // -----   Environment   --------------------------------------------------
   fSrcDir = gSystem->Getenv("VMCWORKDIR");  // top source directory
@@ -294,7 +294,7 @@ void CbmOnlineParWrite::Run(Setup setup)
   TString geoSetupTag = "";
   try {
     uint64_t runId = -1;
-    switch (setup) {
+    switch (fConfig.setupType) {
       case Setup::mCBM2022: runId = 2391; break;
       case Setup::mCBM2024: runId = 2918; break;
       default: throw std::runtime_error("Unknown setup type");
@@ -320,6 +320,42 @@ void CbmOnlineParWrite::Run(Setup setup)
   auto* inputSource = new CbmSourceDummy{};
   fRun->SetSource(inputSource);
 
+  // =========================================================================
+  // ===                   Alignment Correction                            ===
+  // =========================================================================
+  if (fConfig.doAlignment) {
+
+    TString alignmentMatrixFileName = fSrcDir + "/parameters/mcbm/AlignmentMatrices_" + geoSetupTag + ".root";
+    if (alignmentMatrixFileName.Length() == 0) {
+      throw std::runtime_error{"Alignment matrix file name is empty"};
+    }
+
+    LOG(info) << "Applying alignment for file '" << alignmentMatrixFileName << "'";
+
+    // Define the basic structure which needs to be filled with information
+    // This structure is stored in the output file and later passed to the
+    // FairRoot framework to do the (miss)alignment
+    std::map<std::string, TGeoHMatrix>* matrices{nullptr};
+
+    // read matrices from disk
+    LOG(info) << "Filename: " << alignmentMatrixFileName;
+    TFile* misalignmentMatrixRootfile = new TFile(alignmentMatrixFileName, "READ");
+    if (misalignmentMatrixRootfile->IsOpen()) {
+      gDirectory->GetObject("MisalignMatrices", matrices);
+      misalignmentMatrixRootfile->Close();
+    }
+    else {
+      throw std::runtime_error{"Could not open alignment matrix file: " + alignmentMatrixFileName};
+    }
+
+    if (matrices) {
+      fRun->AddAlignmentMatrices(*matrices);
+    }
+    else {
+      throw std::runtime_error{"Could not read alignment matrices from file: " + alignmentMatrixFileName};
+    }
+  }
+
 
   // -----   Add detectors   ------------------------------------------------
   AddDetectors();
diff --git a/reco/steer/CbmOnlineParWrite.h b/reco/steer/CbmOnlineParWrite.h
index 66b7c144c4..25e90d87c2 100644
--- a/reco/steer/CbmOnlineParWrite.h
+++ b/reco/steer/CbmOnlineParWrite.h
@@ -25,10 +25,15 @@ class CbmSetup;
 class CbmOnlineParWrite {
 
  public:
-  void Run(cbm::algo::Setup setup);
+  struct Config {
+    cbm::algo::Setup setupType = cbm::algo::Setup::mCBM2022;
+    bool doAlignment           = false;
+  };
+
+  void Run(const Config& config);
 
  private:
-  cbm::algo::Setup fSetupType = cbm::algo::Setup::mCBM2022;  // Setup
+  Config fConfig;
   TString fSrcDir             = "";                          // CbmRoot Source directory
   CbmSetup* fSetup            = nullptr;                     // Global Geometry setup
   FairRunAna* fRun            = nullptr;                     // FairRunAna object
diff --git a/services/online_par_dump/Application.cxx b/services/online_par_dump/Application.cxx
index 17116d0ccb..9321fedadf 100644
--- a/services/online_par_dump/Application.cxx
+++ b/services/online_par_dump/Application.cxx
@@ -28,7 +28,11 @@ void Application::Run()
   gSystem->cd(fOpts.outputDir.c_str());
 
   CbmOnlineParWrite writer;
-  writer.Run(fOpts.setup);
+  CbmOnlineParWrite::Config writeConfig{
+    .setupType   = fOpts.setup,
+    .doAlignment = !fOpts.skipAlignment,
+  };
+  writer.Run(writeConfig);
 
   LOG(info) << "Online parameter dump finished";
 }
diff --git a/services/online_par_dump/ProgramOptions.cxx b/services/online_par_dump/ProgramOptions.cxx
index dd0c22fcda..c5bd91c232 100644
--- a/services/online_par_dump/ProgramOptions.cxx
+++ b/services/online_par_dump/ProgramOptions.cxx
@@ -22,8 +22,18 @@ ProgramOptions::ProgramOptions(int argc, char** argv)
   ;
   // clang-format on
 
+  po::options_description optional{"Other options"};
+  // clang-format off
+  optional.add_options()
+    ("no-alignment", po::bool_switch(&skipAlignment),
+      "Do not do alignment")
+    ("help,h", "Print help message")
+  ;
+  // clang-format on
+
+
   po::options_description cmdline_options;
-  cmdline_options.add(required);
+  cmdline_options.add(required).add(optional);
 
   po::variables_map vm;
   po::command_line_parser parser{argc, argv};
diff --git a/services/online_par_dump/ProgramOptions.h b/services/online_par_dump/ProgramOptions.h
index 9dab8b7ca5..d3aaa55841 100644
--- a/services/online_par_dump/ProgramOptions.h
+++ b/services/online_par_dump/ProgramOptions.h
@@ -12,5 +12,6 @@ struct ProgramOptions {
   ProgramOptions(int argc, char** argv);
 
   cbm::algo::Setup setup;
+  bool skipAlignment;
   std::string outputDir;
 };
-- 
GitLab