From 67821abc166cccca0d7e91eb842176b0afb2a54e Mon Sep 17 00:00:00 2001
From: Eoin Clerkin <e.clerkin@gsi.de>
Date: Thu, 21 Sep 2023 22:38:01 +0200
Subject: [PATCH] Adds helper functions for CAD2ROOT

Includes binaries from files. Manipulate positional matrices. Generates empty TGeoManager with imported media.
---
 core/base/utils/CbmGeometryUtils.cxx | 83 +++++++++++++++++++++++++++-
 core/base/utils/CbmGeometryUtils.h   | 18 +++++-
 2 files changed, 97 insertions(+), 4 deletions(-)

diff --git a/core/base/utils/CbmGeometryUtils.cxx b/core/base/utils/CbmGeometryUtils.cxx
index caa113c604..40178940a7 100644
--- a/core/base/utils/CbmGeometryUtils.cxx
+++ b/core/base/utils/CbmGeometryUtils.cxx
@@ -1,6 +1,6 @@
-/* Copyright (C) 2018-2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+/* Copyright (C) 2018-2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
    SPDX-License-Identifier: GPL-3.0-only
-   Authors: Florian Uhlig [committer] */
+   Authors: Florian Uhlig [committer], Eoin Clerkin */
 
 #include "CbmGeometryUtils.h"
 
@@ -408,5 +408,84 @@ namespace Cbm
       covYY = rc10 * r10 + rc11 * r11;
     }
 
+
+
+   /* Populate a GeoManager with the full media and materials defined to return */
+   TGeoManager* pop_TGeoManager(const char* name){
+
+        // Use the FairRoot geometry interface to load the media which are already defined
+        FairGeoLoader* geoLoad    = new FairGeoLoader("TGeo", "FairGeoLoader");
+        FairGeoInterface* geoFace = geoLoad->getGeoInterface();
+        TString geoPath           = gSystem->Getenv("VMCWORKDIR");
+        TString geoFile           = geoPath + "/geometry/media.geo";
+        geoFace->setMediaFile(geoFile);
+        geoFace->readMedia();
+
+        // Read the required media and create them in the GeoManager
+        FairGeoMedia* geoMedia   = geoFace->getMedia();
+        FairGeoBuilder* geoBuild = geoLoad->getGeoBuilder();
+
+        int num = geoMedia->getListOfMedia()->GetSize();
+        FairGeoMedium* med = new FairGeoMedium();
+
+        for(int i=0; i<num; i++){
+                med = geoMedia->getMedium(geoMedia->getListOfMedia()->At(i)->GetName());
+                geoBuild->createMedium(med);
+        };
+
+	gGeoManager->SetTitle(name);
+	
+        return gGeoManager;
+   }
+
+/* Open root file geometry and add it to parent */
+bool add_binary(const char rootFile[], TGeoVolume* top, TGeoMedium* med, Int_t inum, TGeoMatrix* mat){
+
+        TFile* file = TFile::Open(rootFile,"OPEN");
+        if(file==NULL){
+                fprintf(stderr,"Error file %s not opened\n", rootFile);
+                return false;
+        }else{
+                ((TGeoVolume*)file->Get(file->GetListOfKeys()->First()->GetName()))->SetMedium(med);
+                        top->AddNode(
+                        (TGeoVolume *) (file->Get(file->GetListOfKeys()->First()->GetName()))->Clone(),
+                        inum,
+                        mat
+                        );
+                        };
+
+                (top->GetNode(top->GetNodes()->Last()->GetName()))->GetVolume()->SetMedium(med);
+                file->Close();
+        return true;
+}
+
+/* Take in the positional matrix and return it in TGeo format */
+TGeoMatrix* cad_matrix(
+        double XX, double XY, double XZ,
+        double YX, double YY, double YZ,
+        double ZX, double ZY, double ZZ,
+        double TX, double TY, double TZ){
+
+        TGeoHMatrix* hmat = new TGeoHMatrix();
+        (hmat->GetRotationMatrix())[0] = XX;
+        (hmat->GetRotationMatrix())[3] = XY;
+        (hmat->GetRotationMatrix())[6] = XZ;
+
+        (hmat->GetRotationMatrix())[1] = YX;
+        (hmat->GetRotationMatrix())[4] = YY;
+        (hmat->GetRotationMatrix())[7] = YZ;
+
+        (hmat->GetRotationMatrix())[2] = ZX;
+        (hmat->GetRotationMatrix())[5] = ZY;
+        (hmat->GetRotationMatrix())[8] = ZZ;
+
+        TGeoRotation* rot = new TGeoRotation();
+        rot->SetRotation(*hmat);
+
+        TGeoCombiTrans* mat = new TGeoCombiTrans(TX/1,TY/1,TZ/1,rot);
+        return mat;
+};
+
+
   }  // namespace GeometryUtils
 }  // namespace Cbm
diff --git a/core/base/utils/CbmGeometryUtils.h b/core/base/utils/CbmGeometryUtils.h
index 28dbe964f6..8caf0fae0e 100644
--- a/core/base/utils/CbmGeometryUtils.h
+++ b/core/base/utils/CbmGeometryUtils.h
@@ -1,8 +1,10 @@
-/* Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+/* Copyright (C) 2018-2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
    SPDX-License-Identifier: GPL-3.0-only
-   Authors: Florian Uhlig [committer] */
+   Authors: Florian Uhlig [committer], Eoin Clerkin */
 
 #include "Rtypes.h"
+#include "TGeoManager.h"
+#include "TSystem.h"
 
 class TGeoMatrix;
 class TGeoVolume;
@@ -35,5 +37,17 @@ namespace Cbm
     /// @param covYY covariance Y,Y
     void LocalToMasterCovarianceMatrix(const TGeoMatrix& m, Double_t& covXX, Double_t& covXY, Double_t& covYY);
 
+     /* Populate a GeoManager with the full media and materials defined to return */
+    TGeoManager* pop_TGeoManager(const char* name);
+
+/* Open root file geometry and add it to parent */
+bool add_binary(const char rootFile[], TGeoVolume* top, TGeoMedium* med, Int_t inum, TGeoMatrix* mat);
+
+TGeoMatrix* cad_matrix(
+	double XX, double XY, double XZ,
+        double YX, double YY, double YZ,
+        double ZX, double ZY, double ZZ,
+        double TX, double TY, double TZ);
+
   }  // namespace GeometryUtils
 }  // namespace Cbm
-- 
GitLab