From 2ad7ccc8950009fb33d9845d29c7f83701cce254 Mon Sep 17 00:00:00 2001
From: clantz <clantz@gitlab.cern.ch>
Date: Thu, 22 Aug 2019 10:31:07 -0500
Subject: [PATCH] Attempting to modify geometry interactively

---
 include/DetectorConstruction.hh | 23 ++++++++---
 include/DetectorMessenger.hh    |  7 ++++
 src/DetectorConstruction.cc     | 73 ++++++++++++++++++++++++++++++++-
 src/DetectorMessenger.cc        | 70 +++++++++++++++++++++++++++++++
 4 files changed, 166 insertions(+), 7 deletions(-)

diff --git a/include/DetectorConstruction.hh b/include/DetectorConstruction.hh
index 520a653..01cd727 100644
--- a/include/DetectorConstruction.hh
+++ b/include/DetectorConstruction.hh
@@ -51,14 +51,20 @@ class DetectorConstruction : public G4VUserDetectorConstruction
 
   public:
     virtual G4VPhysicalVolume* Construct();
-    void SetCADFilename     (std::string name){filename   = name;}
+    void SetCADFilename     (std::string name);
     void SetCADFiletype     (std::string type){filetype   = type;}
     void SetGDMLoutName     (std::string name){GDMLoutput = name;}
 
-    void SetSurfaceFinish(const G4OpticalSurfaceFinish finish);
-    void SetSurfaceType(const G4SurfaceType type);
-    void SetSurfaceModel(const G4OpticalSurfaceModel model);
-    void SetSurfaceSigmaAlpha(G4double v);
+    void SetSurfaceFinish     (const G4OpticalSurfaceFinish finish);
+    void SetSurfaceType       (const G4SurfaceType type);
+    void SetSurfaceModel      (const G4OpticalSurfaceModel model);
+    void SetSurfaceSigmaAlpha (G4double v);
+    void SetRotationX         (G4int arg);
+    void SetRotationY         (G4int arg);
+    void SetRotationZ         (G4int arg);
+    void SetOffsetX           (G4double arg);
+    void SetOffsetY           (G4double arg);
+    void SetOffsetZ           (G4double arg);
 
 
   private:
@@ -69,6 +75,13 @@ class DetectorConstruction : public G4VUserDetectorConstruction
     G4LogicalVolume*     cad_logical;
     G4VPhysicalVolume*   cad_physical;
 
+    G4int m_rotX;
+    G4int m_rotY;
+    G4int m_rotZ;
+    G4double m_offsetX;
+    G4double m_offsetY;
+    G4double m_offsetZ;
+
     Materials* materials;
 
     std::string filename = "";
diff --git a/include/DetectorMessenger.hh b/include/DetectorMessenger.hh
index 3313cc7..da0df45 100644
--- a/include/DetectorMessenger.hh
+++ b/include/DetectorMessenger.hh
@@ -64,6 +64,13 @@ class DetectorMessenger: public G4UImessenger{
     G4UIcmdWithAString*        fSurfaceModelCmd;
     G4UIcmdWithADouble*        fSurfaceSigmaAlphaCmd;
     G4UIcmdWithAString*        fSurfaceMatPropVectorCmd;
+    G4UIcmdWithAString*        fModelCmd;
+    G4UIcmdWithAnInteger*      fModelRotationXCmd;
+    G4UIcmdWithAnInteger*      fModelRotationYCmd;
+    G4UIcmdWithAnInteger*      fModelRotationZCmd;
+    G4UIcmdWithADouble*        fModelOffsetXCmd;
+    G4UIcmdWithADouble*        fModelOffsetYCmd;
+    G4UIcmdWithADouble*        fModelOffsetZCmd;
 
 
 };
diff --git a/src/DetectorConstruction.cc b/src/DetectorConstruction.cc
index 89a8831..6452bbd 100644
--- a/src/DetectorConstruction.cc
+++ b/src/DetectorConstruction.cc
@@ -39,6 +39,10 @@
 #include "G4Element.hh"
 #include "G4SDManager.hh"
 #include "G4RunManager.hh"
+#include "G4GeometryManager.hh"
+#include "G4PhysicalVolumeStore.hh"
+#include "G4LogicalVolumeStore.hh"
+#include "G4SolidStore.hh"
 #include "G4LogicalBorderSurface.hh"
 #include "G4LogicalSkinSurface.hh"
 #include "G4OpticalSurface.hh"
@@ -126,8 +130,10 @@ G4VPhysicalVolume* DetectorConstruction::Construct(){
 
     // CAD model rotation.
     G4RotationMatrix * rot = new G4RotationMatrix();
-    rot->rotateZ(90*deg);
-    rot->rotateY(-90*deg);
+    rot->rotateX(m_rotX*deg);
+    rot->rotateY(m_rotY*deg);
+    rot->rotateZ(m_rotZ*deg);
+
 
     #ifdef CADMESH
     if(filetype == "stl"){
@@ -315,3 +321,66 @@ void DetectorConstruction::SetSurfaceModel(const G4OpticalSurfaceModel model){
   materials->AlSurface->SetModel(model);
   G4RunManager::GetRunManager()->GeometryHasBeenModified();
 }
+
+void DetectorConstruction::SetCADFilename(std::string name){
+  filename = name;
+  G4cout << "Using " << filename << G4endl;
+  delete G4SDManager::GetSDMpointer()->FindSensitiveDetector("MyPMT");
+  G4RunManager::GetRunManager()->GeometryHasBeenModified();
+  //G4RunManager::GetRunManager()->ResetNavigator();
+  G4GeometryManager::GetInstance()->OpenGeometry();
+  G4PhysicalVolumeStore::GetInstance()->Clean();
+  G4LogicalVolumeStore::GetInstance()->Clean();
+  G4SolidStore::GetInstance()->Clean();
+  Construct();
+  //G4RunManager::GetRunManager()->ReinitializeGeometry();
+  // /lightGuide/CADmodel ../zdclg/models/LightGuide2007BigPMT.stl
+}
+
+/*
+ *
+ */
+void DetectorConstruction::SetRotationX(G4int arg){
+  m_rotX = arg;
+  G4RunManager::GetRunManager()->GeometryHasBeenModified();
+}
+
+/*
+ *
+ */
+void DetectorConstruction::SetRotationY(G4int arg){
+  m_rotY = arg;
+  G4RunManager::GetRunManager()->GeometryHasBeenModified();
+}
+
+/*
+ *
+ */
+void DetectorConstruction::SetRotationZ(G4int arg){
+  m_rotZ = arg;
+  G4RunManager::GetRunManager()->GeometryHasBeenModified();
+}
+
+/*
+ *
+ */
+void DetectorConstruction::SetOffsetX(G4double arg){
+  m_offsetX = arg;
+  G4RunManager::GetRunManager()->GeometryHasBeenModified();
+}
+
+/*
+ *
+ */
+void DetectorConstruction::SetOffsetY(G4double arg){
+  m_offsetY = arg;
+  G4RunManager::GetRunManager()->GeometryHasBeenModified();
+}
+
+/*
+ *
+ */
+void DetectorConstruction::SetOffsetZ(G4double arg){
+  m_offsetZ = arg;
+  G4RunManager::GetRunManager()->GeometryHasBeenModified();
+}
diff --git a/src/DetectorMessenger.cc b/src/DetectorMessenger.cc
index cf0579f..2295729 100644
--- a/src/DetectorMessenger.cc
+++ b/src/DetectorMessenger.cc
@@ -82,7 +82,46 @@ DetectorMessenger::DetectorMessenger(DetectorConstruction * Det)
   fSurfaceMatPropVectorCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
   fSurfaceMatPropVectorCmd->SetToBeBroadcasted(false);
 
+  fModelCmd = new G4UIcmdWithAString("/lightGuide/CADmodel", this);
+  fModelCmd->SetGuidance("CAD model to be used");
+  fModelCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
+  fModelCmd->SetToBeBroadcasted(false);
 
+  fModelRotationXCmd =
+    new G4UIcmdWithAnInteger("/lightGuide/rotX", this);
+  fModelRotationXCmd->SetGuidance("Set light guide first rotation");
+  fModelRotationXCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
+  fModelRotationXCmd->SetToBeBroadcasted(false);
+
+  fModelRotationYCmd =
+    new G4UIcmdWithAnInteger("/lightGuide/rotY", this);
+  fModelRotationYCmd->SetGuidance("Set light guide second rotation");
+  fModelRotationYCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
+  fModelRotationYCmd->SetToBeBroadcasted(false);
+
+  fModelRotationZCmd =
+    new G4UIcmdWithAnInteger("/lightGuide/rotZ", this);
+  fModelRotationZCmd->SetGuidance("Set light guide second rotation");
+  fModelRotationZCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
+  fModelRotationZCmd->SetToBeBroadcasted(false);
+
+  fModelOffsetXCmd =
+    new G4UIcmdWithADouble("/lightGuide/offsetX", this);
+  fModelOffsetXCmd->SetGuidance("Set light guide X rotation");
+  fModelOffsetXCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
+  fModelOffsetXCmd->SetToBeBroadcasted(false);
+
+  fModelOffsetYCmd =
+    new G4UIcmdWithADouble("/lightGuide/offsetY", this);
+  fModelOffsetYCmd->SetGuidance("Set light guide Y rotation");
+  fModelOffsetYCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
+  fModelOffsetYCmd->SetToBeBroadcasted(false);
+
+  fModelOffsetZCmd =
+    new G4UIcmdWithADouble("/lightGuide/offsetZ", this);
+  fModelOffsetZCmd->SetGuidance("Set light guide Z rotation");
+  fModelOffsetZCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
+  fModelOffsetZCmd->SetToBeBroadcasted(false);
 }
 
 /*
@@ -94,6 +133,13 @@ DetectorMessenger::~DetectorMessenger(){
   delete fSurfaceModelCmd;
   delete fSurfaceSigmaAlphaCmd;
   delete fSurfaceMatPropVectorCmd;
+  delete fModelCmd;
+  delete fModelRotationXCmd;
+  delete fModelRotationYCmd;
+  delete fModelRotationZCmd;
+  delete fModelOffsetXCmd;
+  delete fModelOffsetYCmd;
+  delete fModelOffsetZCmd;
 }
 
 /*
@@ -277,4 +323,28 @@ void DetectorMessenger::SetNewValue(G4UIcommand* command,G4String newValue)
     fDetector->SetSurfaceSigmaAlpha(
       G4UIcmdWithADouble::GetNewDoubleValue(newValue));
   }
+  // MODEL LOCATION
+  else if(command == fModelCmd){
+    fDetector->SetCADFilename(newValue);
+  }
+  // MODEL ROTATION
+  else if(command == fModelRotationXCmd){
+    fDetector->SetRotationX(G4UIcmdWithAnInteger::GetNewIntValue(newValue));
+  }
+  else if(command == fModelRotationYCmd){
+    fDetector->SetRotationY(G4UIcmdWithAnInteger::GetNewIntValue(newValue));
+  }
+  else if(command == fModelRotationZCmd){
+    fDetector->SetRotationZ(G4UIcmdWithAnInteger::GetNewIntValue(newValue));
+  }
+  // MODEL TRANSLATION
+  else if(command == fModelOffsetXCmd){
+    fDetector->SetOffsetX(G4UIcmdWithADouble::GetNewDoubleValue(newValue));
+  }
+  else if(command == fModelOffsetYCmd){
+    fDetector->SetOffsetY(G4UIcmdWithADouble::GetNewDoubleValue(newValue));
+  }
+  else if(command == fModelOffsetZCmd){
+    fDetector->SetOffsetZ(G4UIcmdWithADouble::GetNewDoubleValue(newValue));
+  }
 }
-- 
GitLab