From 9e64d8af22001b5ad82b4cb34c9856c8723f380e Mon Sep 17 00:00:00 2001
From: myliu <201916234@mail.sdu.edu.cn>
Date: Tue, 9 Feb 2021 18:45:20 +0800
Subject: [PATCH] Ecal fast simlation

---
 Simulation/DetSimFastEcal/CMakeLists.txt      | 13 ++++++
 .../DetSimFastEcal/src/EcalFastSimG4Model.cpp | 39 ++++++++++++++++++
 .../DetSimFastEcal/src/EcalFastSimG4Model.h   | 19 +++++++++
 .../DetSimFastEcal/src/EcalFastSimG4Tool.cpp  | 41 +++++++++++++++++++
 .../DetSimFastEcal/src/EcalFastSimG4Tool.h    | 21 ++++++++++
 5 files changed, 133 insertions(+)
 create mode 100644 Simulation/DetSimFastEcal/CMakeLists.txt
 create mode 100644 Simulation/DetSimFastEcal/src/EcalFastSimG4Model.cpp
 create mode 100644 Simulation/DetSimFastEcal/src/EcalFastSimG4Model.h
 create mode 100644 Simulation/DetSimFastEcal/src/EcalFastSimG4Tool.cpp
 create mode 100644 Simulation/DetSimFastEcal/src/EcalFastSimG4Tool.h

diff --git a/Simulation/DetSimFastEcal/CMakeLists.txt b/Simulation/DetSimFastEcal/CMakeLists.txt
new file mode 100644
index 00000000..5b16718e
--- /dev/null
+++ b/Simulation/DetSimFastEcal/CMakeLists.txt
@@ -0,0 +1,13 @@
+
+find_package(Geant4 REQUIRED ui_all vis_all)
+include(${Geant4_USE_FILE})
+
+
+gaudi_add_module(DetSimFastEcal
+                 SOURCES src/EcalFastSimG4Tool.cpp
+                         src/EcalFastSimG4Model.cpp
+                 LINK DetSimInterface
+                      ${DD4hep_COMPONENT_LIBRARIES}
+                      Gaudi::GaudiKernel
+)
+
diff --git a/Simulation/DetSimFastEcal/src/EcalFastSimG4Model.cpp b/Simulation/DetSimFastEcal/src/EcalFastSimG4Model.cpp
new file mode 100644
index 00000000..8bb1234a
--- /dev/null
+++ b/Simulation/DetSimFastEcal/src/EcalFastSimG4Model.cpp
@@ -0,0 +1,39 @@
+#include "EcalFastSimG4Model.h"
+
+#include "G4Track.hh"
+#include "G4FastTrack.hh"
+
+EcalFastSimG4Model::EcalFastSimG4Model(G4String aModelName, G4Region* aEnvelope)
+    : G4VFastSimulationModel(aModelName, aEnvelope) {
+
+}
+
+EcalFastSimG4Model::~EcalFastSimG4Model() {
+
+}
+
+G4bool EcalFastSimG4Model::IsApplicable(const G4ParticleDefinition& aParticle) {
+    return aParticle.GetPDGCharge() != 0;
+}
+
+G4bool EcalFastSimG4Model::ModelTrigger(const G4FastTrack& aFastTrack) {
+     //G4cout << __FILE__ << __LINE__ << ": ModelTrigger." << G4endl;
+
+//    bool istrigged = false;
+    bool istrigged = true;
+    // only select the secondaries
+    const G4Track* track = aFastTrack.GetPrimaryTrack();
+    // secondaries
+//G4cout << "trackID = " << track->GetTrackID() <<G4endl;
+//    if (track->GetTrackID() != 0) {
+//        istrigged = true;
+//    }
+//G4cout << "istrigged = " << istrigged <<G4endl;
+    return istrigged;
+}
+
+void EcalFastSimG4Model::DoIt(const G4FastTrack& aFastTrack, G4FastStep& aFastStep) {
+     //G4cout << __FILE__ << __LINE__ << ": DoIt." << G4endl;
+
+    aFastStep.ProposeTrackStatus(fStopAndKill);
+}
diff --git a/Simulation/DetSimFastEcal/src/EcalFastSimG4Model.h b/Simulation/DetSimFastEcal/src/EcalFastSimG4Model.h
new file mode 100644
index 00000000..fc378343
--- /dev/null
+++ b/Simulation/DetSimFastEcal/src/EcalFastSimG4Model.h
@@ -0,0 +1,19 @@
+#ifndef EcalFastSimG4Model_h
+#define EcalFastSimG4Model_h
+
+#include "G4VFastSimulationModel.hh"
+
+class EcalFastSimG4Model: public G4VFastSimulationModel {
+public:
+
+    EcalFastSimG4Model(G4String aModelName, G4Region* aEnvelope);
+    ~EcalFastSimG4Model();
+
+    virtual G4bool IsApplicable( const G4ParticleDefinition& aParticle );
+    virtual G4bool ModelTrigger( const G4FastTrack& aFastTrack );
+    virtual void DoIt( const G4FastTrack& aFastTrack, G4FastStep& aFastStep );
+
+};
+
+#endif
+
diff --git a/Simulation/DetSimFastEcal/src/EcalFastSimG4Tool.cpp b/Simulation/DetSimFastEcal/src/EcalFastSimG4Tool.cpp
new file mode 100644
index 00000000..2ac48086
--- /dev/null
+++ b/Simulation/DetSimFastEcal/src/EcalFastSimG4Tool.cpp
@@ -0,0 +1,41 @@
+#include "EcalFastSimG4Tool.h"
+
+#include "G4Region.hh"
+#include "G4RegionStore.hh"
+
+#include "G4VFastSimulationModel.hh"
+#include "EcalFastSimG4Model.h"
+
+DECLARE_COMPONENT(EcalFastSimG4Tool);
+
+StatusCode EcalFastSimG4Tool::initialize() {
+    StatusCode sc;
+
+    return sc;
+}
+
+StatusCode EcalFastSimG4Tool::finalize() {
+    StatusCode sc;
+
+    return sc;
+}
+
+bool EcalFastSimG4Tool::CreateFastSimulationModel() {
+    // In this method:
+    // * Retrieve the G4Region
+    // * Create Model
+    // * Associate model and region
+
+    G4String model_name = "EcalFastSimG4Model";
+    for (auto region_name: m_regions.value()) {
+        G4Region* aEnvelope = G4RegionStore::GetInstance()->GetRegion(region_name);
+        if (!aEnvelope) {
+            error() << "Failed to find G4Region '" << region_name << "'" << endmsg;
+            return false;
+        }
+
+        EcalFastSimG4Model* model = new EcalFastSimG4Model(model_name+region_name, aEnvelope);
+        info() << "Create Model " << model_name << " for G4Region " << region_name << endmsg;
+    }
+    return true;
+}
diff --git a/Simulation/DetSimFastEcal/src/EcalFastSimG4Tool.h b/Simulation/DetSimFastEcal/src/EcalFastSimG4Tool.h
new file mode 100644
index 00000000..4f21f8cf
--- /dev/null
+++ b/Simulation/DetSimFastEcal/src/EcalFastSimG4Tool.h
@@ -0,0 +1,21 @@
+#ifndef EcalFastSimG4Tool_h
+#define EcalFastSimG4Tool_h
+
+#include "GaudiKernel/AlgTool.h"
+#include "DetSimInterface/IFastSimG4Tool.h"
+
+class EcalFastSimG4Tool: public extends<AlgTool, IFastSimG4Tool> {
+public:
+    using extends::extends;
+
+    StatusCode initialize() override;
+    StatusCode finalize() override;
+
+    bool CreateFastSimulationModel() override;
+
+private:
+    // the regions will be associated with the fast sim model
+    Gaudi::Property<std::vector<std::string>> m_regions{this, "Regions"};
+};
+
+#endif
-- 
GitLab