From a8b7de539535e2f68f263595e4d168fffd6af53a Mon Sep 17 00:00:00 2001
From: adrien-matta <a.matta@surrey.ac.uk>
Date: Wed, 22 Jul 2015 11:49:43 +0100
Subject: [PATCH] * Adding option to export the current geometry to a gdml file

---
 .gitignore                                | 17 ++++++
 NPSimulation/CMakeLists.txt               |  5 ++
 NPSimulation/Core/DetectorConstruction.cc | 12 ++++-
 NPSimulation/Core/DetectorConstruction.hh | 65 ++++++++++++-----------
 NPSimulation/Core/DetectorMessenger.cc    | 14 +++--
 NPSimulation/Core/DetectorMessenger.hh    |  1 +
 6 files changed, 78 insertions(+), 36 deletions(-)
 create mode 100644 .gitignore

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 000000000..a189af5ef
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,17 @@
+*.cmake
+*.G4Table
+*CMakeFiles*
+*.ninja*
+*.swp
+Makefiles
+*.gitignore
+CMakeCache.txt
+*.dylib
+*.so
+*.gdml
+*.root
+*.wrl
+install_manifest.txt
+.DS_store
+*NPAnalysis*
+.last_*
diff --git a/NPSimulation/CMakeLists.txt b/NPSimulation/CMakeLists.txt
index 9781c3815..62674a478 100755
--- a/NPSimulation/CMakeLists.txt
+++ b/NPSimulation/CMakeLists.txt
@@ -42,6 +42,11 @@ EXEC_PROGRAM(${ROOT_CONFIG_EXECUTABLE}
 
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${root_cflags}")
 
+if(Geant4_gdml_FOUND)  
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNPS_GDML")     
+endif()
+
+
 # If the compiler is Clang, silence the unrecognised flags
 if(${CMAKE_CXX_COMPILER_ID} MATCHES ".*Clang.*")
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments -fcolor-diagnostics -Wno-deprecated-register -Wno-shadow")
diff --git a/NPSimulation/Core/DetectorConstruction.cc b/NPSimulation/Core/DetectorConstruction.cc
index a8f4c0ce0..8de3fd16d 100644
--- a/NPSimulation/Core/DetectorConstruction.cc
+++ b/NPSimulation/Core/DetectorConstruction.cc
@@ -52,7 +52,10 @@
 #include "G4SolidStore.hh"
 #include "G4RunManager.hh"
 #include "G4SDManager.hh"
-// Detector class
+
+#ifdef NPS_GDML
+#include"G4GDMLParser.hh"
+#endif
 
 // STL
 #include<cstdlib>
@@ -293,3 +296,10 @@ void DetectorConstruction::RedefineGeometry(std::string file){
   G4RunManager::GetRunManager()->GeometryHasBeenModified();
   G4RunManager::GetRunManager()->Initialize();
 }
+
+//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
+void DetectorConstruction::ExportGeometry(string file){
+  G4GDMLParser parser ;
+  parser.Write(file.c_str(),world_log);
+}
+
diff --git a/NPSimulation/Core/DetectorConstruction.hh b/NPSimulation/Core/DetectorConstruction.hh
index 63c6b1d8b..451827ca7 100644
--- a/NPSimulation/Core/DetectorConstruction.hh
+++ b/NPSimulation/Core/DetectorConstruction.hh
@@ -42,45 +42,46 @@ class G4VPhysicalVolume;
 // ROOT headers
 #include "TTree.h"
 
-class DetectorConstruction : public G4VUserDetectorConstruction
-{
-public:
-   DetectorConstruction();
-   virtual ~DetectorConstruction();
+class DetectorConstruction : public G4VUserDetectorConstruction{
+  public:
+    DetectorConstruction();
+    virtual ~DetectorConstruction();
 
-   G4VPhysicalVolume* Construct();
-   G4LogicalVolume* GetWorldLogic();
-   // Delete every existing bit
-   void ClearGeometry();
-   // Reload the file configuration
-   void RedefineGeometry(string file="");
- 
-public:
-   G4VPhysicalVolume* ReadConfigurationFile();
-   void AddDetector(NPS::VDetector*);
-   void ReadAllSensitive(const G4Event*);
+    G4VPhysicalVolume* Construct();
+    G4LogicalVolume* GetWorldLogic();
+    // Delete every existing bit
+    void ClearGeometry();
+    // Reload the file configuration
+    void RedefineGeometry(string file="");
+    // Export the geometry to a gdml file
+    void ExportGeometry(string);
 
+  public:
+    G4VPhysicalVolume* ReadConfigurationFile();
+    void AddDetector(NPS::VDetector*);
+    void ReadAllSensitive(const G4Event*);
 
-private:
-   // Logical volumes
-   G4LogicalVolume* world_log;
-  //G4LogicalVolume* Chamber_log;
 
-   // Physical volumes
-   G4VPhysicalVolume* world_phys;
-  //G4VPhysicalVolume* Chamber_phys;
+  private:
+    // Logical volumes
+    G4LogicalVolume* world_log;
+    //G4LogicalVolume* Chamber_log;
 
-   // Virtual Detector Vector
-   vector<NPS::VDetector*> m_Detectors;
+    // Physical volumes
+    G4VPhysicalVolume* world_phys;
+    //G4VPhysicalVolume* Chamber_phys;
 
+    // Virtual Detector Vector
+    vector<NPS::VDetector*> m_Detectors;
 
-private: // Data about the target
-   Target*  m_Target;
-   Chamber* m_Chamber;
-   DetectorMessenger* m_Messenger;
-public:  // Getter and Setter
-   Target*  GetTarget()    {return m_Target;}
-   Chamber* GetChamber()   {return m_Chamber;}
+
+  private: // Data about the target
+    Target*  m_Target;
+    Chamber* m_Chamber;
+    DetectorMessenger* m_Messenger;
+  public:  // Getter and Setter
+    Target*  GetTarget()    {return m_Target;}
+    Chamber* GetChamber()   {return m_Chamber;}
 };
 
 #endif
diff --git a/NPSimulation/Core/DetectorMessenger.cc b/NPSimulation/Core/DetectorMessenger.cc
index 063964b86..ac8829982 100644
--- a/NPSimulation/Core/DetectorMessenger.cc
+++ b/NPSimulation/Core/DetectorMessenger.cc
@@ -42,6 +42,10 @@ DetectorMessenger::DetectorMessenger(DetectorConstruction* Det):Detector(Det){
   UpdateCmd->SetGuidance("Update detector geometry.");
   UpdateCmd->SetGuidance("Apply this command after editing your geometry file ");
   UpdateCmd->AvailableForStates(G4State_Idle);
+
+  ExportCmd = new G4UIcmdWithAString("/det/export_gdml",this);
+  ExportCmd->SetGuidance("export current geomtry to gdml");
+  ExportCmd->AvailableForStates(G4State_Idle);  
 }
 
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
@@ -53,9 +57,13 @@ DetectorMessenger::~DetectorMessenger(){
 
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 
-void DetectorMessenger::SetNewValue(G4UIcommand* command, G4String){ 
-  if( command == UpdateCmd )
-   { Detector->RedefineGeometry(""); }
+void DetectorMessenger::SetNewValue(G4UIcommand* command, G4String value){ 
+  if( command == UpdateCmd ){ 
+    Detector->RedefineGeometry(""); 
+  }
+  else if(command ==ExportCmd ){
+    Detector->ExportGeometry(value);
+  }
 }
 
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
diff --git a/NPSimulation/Core/DetectorMessenger.hh b/NPSimulation/Core/DetectorMessenger.hh
index 1708a1cf6..280762582 100644
--- a/NPSimulation/Core/DetectorMessenger.hh
+++ b/NPSimulation/Core/DetectorMessenger.hh
@@ -48,6 +48,7 @@ class DetectorMessenger: public G4UImessenger{
     
     G4UIdirectory* detDir;
     G4UIcmdWithoutParameter* UpdateCmd;
+    G4UIcmdWithAString* ExportCmd;
 };
 
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
-- 
GitLab