From 719e08f8288ca157b57809ab9273cdd55a65d7b1 Mon Sep 17 00:00:00 2001 From: tontyoutoure Date: Wed, 25 Oct 2023 08:39:40 +0800 Subject: [PATCH 1/2] fix memory leak and summary output bug for MultiSensitiveDetector --- source/digits_hits/include/GateEmptySD.hh | 71 ++++++++++++++++++++++ source/digits_hits/src/GateActorManager.cc | 3 +- source/digits_hits/src/GateEmptySD.cc | 44 ++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 source/digits_hits/include/GateEmptySD.hh create mode 100644 source/digits_hits/src/GateEmptySD.cc diff --git a/source/digits_hits/include/GateEmptySD.hh b/source/digits_hits/include/GateEmptySD.hh new file mode 100644 index 000000000..a8b3d911d --- /dev/null +++ b/source/digits_hits/include/GateEmptySD.hh @@ -0,0 +1,71 @@ +/*---------------------- + Copyright (C): OpenGATE Collaboration + +This software is distributed under the terms +of the GNU Lesser General Public Licence (LGPL) +See LICENSE.md for further details +----------------------*/ + + +#ifndef GateEmptySD_h +#define GateEmptySD_h 1 + +#include "G4VSensitiveDetector.hh" +#include "G4SDManager.hh" + +#include "GateHit.hh" +class G4Step; +class G4HCofThisEvent; +class G4TouchableHistory; + +class GateVVolume; +class GateVSystem; + +//! List of typedefs for the multi-system usage. +typedef std::vector GateSystemList; +typedef GateSystemList::iterator GateSystemIterator; +typedef GateSystemList::const_iterator GateSystemConstIterator; + +/*! \class GateEmptySD + \brief The GateEmptySD is a sensitive detector , derived from G4VSensitiveDetector, + \brief to be used for removing existing SD from G4SDManager + + - This SD is not exposed to the user. It's sole purpose of existing is to fix issue with + GateMultiSensitiveDetector, for a volume with both SD and actor attached to it. + + - It will not ask for any dynamic memory allocation. + + - It will not be registered in the GateDigitizerMgr. +*/ +// Created by tontyoutoure@gmail.com 2023/10/24 + + + +class GateEmptySD : public G4VSensitiveDetector +{ + + public: + //! Constructor. + //! The argument is the name of the sensitive detector + GateEmptySD(const G4String& name); + //! Destructor + ~GateEmptySD() = default; + + //! Method overloading the virtual method Initialize() of G4VSensitiveDetector + void Initialize(G4HCofThisEvent*HCE) override; + + //! Implementation of the pure virtual method ProcessHits(). + G4bool ProcessHits(G4Step*aStep,G4TouchableHistory*ROhist) override; + + + private: + GateHitsCollection * crystalHitsCollection; //! Hit collection + G4int collectionID; + G4int HCID; + +}; + + + + +#endif diff --git a/source/digits_hits/src/GateActorManager.cc b/source/digits_hits/src/GateActorManager.cc index d4f2ac331..8957365ee 100644 --- a/source/digits_hits/src/GateActorManager.cc +++ b/source/digits_hits/src/GateActorManager.cc @@ -13,6 +13,7 @@ #include "GateActorManager.hh" #include "GateVActor.hh" #include "GateMultiSensitiveDetector.hh" +#include "GateEmptySD.hh" //----------------------------------------------------------------------------- GateActorManager::GateActorManager() @@ -311,7 +312,7 @@ void GateActorManager::SetMultiFunctionalDetector(GateVActor * actor, GateVVolum G4String detectorName2 = "MSD_"+ num.str(); // Remove attached SD by replacing with a deactivated clone SD - G4VSensitiveDetector* replacementSD = volume->GetLogicalVolume()->GetSensitiveDetector()->Clone(); + G4VSensitiveDetector* replacementSD = new GateEmptySD(volume->GetLogicalVolume()->GetSensitiveDetector()->GetName()); G4SDManager::GetSDMpointer()->AddNewDetector(replacementSD); replacementSD->Activate(false); diff --git a/source/digits_hits/src/GateEmptySD.cc b/source/digits_hits/src/GateEmptySD.cc new file mode 100644 index 000000000..d5e1eb593 --- /dev/null +++ b/source/digits_hits/src/GateEmptySD.cc @@ -0,0 +1,44 @@ +/*---------------------- + Copyright (C): OpenGATE Collaboration + + This software is distributed under the terms + of the GNU Lesser General Public Licence (LGPL) + See LICENSE.md for further details + ----------------------*/ + +#include "GateEmptySD.hh" +#include "G4HCofThisEvent.hh" +#include "G4TouchableHistory.hh" +#include "G4Step.hh" +#include "G4SDManager.hh" + + +//------------------------------------------------------------------------------ +// Constructor +GateEmptySD::GateEmptySD(const G4String& name) +:G4VSensitiveDetector(name) +{ + G4String collName=name+"Collection"; + collectionName.insert(collName); + + + HCID = G4SDManager::GetSDMpointer()->GetCollectionCapacity() ; + +} +//------------------------------------------------------------------------------ + +//------------------------------------------------------------------------------ +// Method overloading the virtual method Initialize() of G4VSensitiveDetector +// Called at the beginning of each event +void GateEmptySD::Initialize(G4HCofThisEvent*HCE) +{ + HCE->AddHitsCollection(HCID, nullptr); +} +//------------------------------------------------------------------------------ + +//------------------------------------------------------------------------------ +// Implementation of the pure virtual method ProcessHits(). +G4bool GateEmptySD::ProcessHits(G4Step*aStep, G4TouchableHistory*) +{ + return true; +} \ No newline at end of file From b8125f76f812e3a7e20a1522e3b893a2b728ad3f Mon Sep 17 00:00:00 2001 From: tontyoutoure Date: Wed, 25 Oct 2023 10:16:59 +0800 Subject: [PATCH 2/2] Avoid create new files --- .../digits_hits/include/GateActorManager.hh | 17 +++++ source/digits_hits/include/GateEmptySD.hh | 71 ------------------- source/digits_hits/src/GateActorManager.cc | 1 - source/digits_hits/src/GateEmptySD.cc | 44 ------------ 4 files changed, 17 insertions(+), 116 deletions(-) delete mode 100644 source/digits_hits/include/GateEmptySD.hh delete mode 100644 source/digits_hits/src/GateEmptySD.cc diff --git a/source/digits_hits/include/GateActorManager.hh b/source/digits_hits/include/GateActorManager.hh index f5b2e4a49..6b85bece9 100644 --- a/source/digits_hits/include/GateActorManager.hh +++ b/source/digits_hits/include/GateActorManager.hh @@ -127,4 +127,21 @@ private: static GateActorManager *singleton_ActorManager; }; +/*! + Empty SD class doing nothing for removing existing SD from G4SDManager +*/ +// Created by tontyoutoure@gmail.com 2023/10/24 + +class GateEmptySD : public G4VSensitiveDetector +{ + + public: + //! Constructor. + //! The argument is the name of the sensitive detector + GateEmptySD(const G4String& name):G4VSensitiveDetector(name){Activate(false);} + + //! Reinplementation is mendatory. + G4bool ProcessHits(G4Step*aStep,G4TouchableHistory*ROhist) override {return true;} +}; + #endif /* end #define GATEACTORMANAGER_HH */ diff --git a/source/digits_hits/include/GateEmptySD.hh b/source/digits_hits/include/GateEmptySD.hh deleted file mode 100644 index a8b3d911d..000000000 --- a/source/digits_hits/include/GateEmptySD.hh +++ /dev/null @@ -1,71 +0,0 @@ -/*---------------------- - Copyright (C): OpenGATE Collaboration - -This software is distributed under the terms -of the GNU Lesser General Public Licence (LGPL) -See LICENSE.md for further details -----------------------*/ - - -#ifndef GateEmptySD_h -#define GateEmptySD_h 1 - -#include "G4VSensitiveDetector.hh" -#include "G4SDManager.hh" - -#include "GateHit.hh" -class G4Step; -class G4HCofThisEvent; -class G4TouchableHistory; - -class GateVVolume; -class GateVSystem; - -//! List of typedefs for the multi-system usage. -typedef std::vector GateSystemList; -typedef GateSystemList::iterator GateSystemIterator; -typedef GateSystemList::const_iterator GateSystemConstIterator; - -/*! \class GateEmptySD - \brief The GateEmptySD is a sensitive detector , derived from G4VSensitiveDetector, - \brief to be used for removing existing SD from G4SDManager - - - This SD is not exposed to the user. It's sole purpose of existing is to fix issue with - GateMultiSensitiveDetector, for a volume with both SD and actor attached to it. - - - It will not ask for any dynamic memory allocation. - - - It will not be registered in the GateDigitizerMgr. -*/ -// Created by tontyoutoure@gmail.com 2023/10/24 - - - -class GateEmptySD : public G4VSensitiveDetector -{ - - public: - //! Constructor. - //! The argument is the name of the sensitive detector - GateEmptySD(const G4String& name); - //! Destructor - ~GateEmptySD() = default; - - //! Method overloading the virtual method Initialize() of G4VSensitiveDetector - void Initialize(G4HCofThisEvent*HCE) override; - - //! Implementation of the pure virtual method ProcessHits(). - G4bool ProcessHits(G4Step*aStep,G4TouchableHistory*ROhist) override; - - - private: - GateHitsCollection * crystalHitsCollection; //! Hit collection - G4int collectionID; - G4int HCID; - -}; - - - - -#endif diff --git a/source/digits_hits/src/GateActorManager.cc b/source/digits_hits/src/GateActorManager.cc index 8957365ee..db7093500 100644 --- a/source/digits_hits/src/GateActorManager.cc +++ b/source/digits_hits/src/GateActorManager.cc @@ -13,7 +13,6 @@ #include "GateActorManager.hh" #include "GateVActor.hh" #include "GateMultiSensitiveDetector.hh" -#include "GateEmptySD.hh" //----------------------------------------------------------------------------- GateActorManager::GateActorManager() diff --git a/source/digits_hits/src/GateEmptySD.cc b/source/digits_hits/src/GateEmptySD.cc deleted file mode 100644 index d5e1eb593..000000000 --- a/source/digits_hits/src/GateEmptySD.cc +++ /dev/null @@ -1,44 +0,0 @@ -/*---------------------- - Copyright (C): OpenGATE Collaboration - - This software is distributed under the terms - of the GNU Lesser General Public Licence (LGPL) - See LICENSE.md for further details - ----------------------*/ - -#include "GateEmptySD.hh" -#include "G4HCofThisEvent.hh" -#include "G4TouchableHistory.hh" -#include "G4Step.hh" -#include "G4SDManager.hh" - - -//------------------------------------------------------------------------------ -// Constructor -GateEmptySD::GateEmptySD(const G4String& name) -:G4VSensitiveDetector(name) -{ - G4String collName=name+"Collection"; - collectionName.insert(collName); - - - HCID = G4SDManager::GetSDMpointer()->GetCollectionCapacity() ; - -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -// Method overloading the virtual method Initialize() of G4VSensitiveDetector -// Called at the beginning of each event -void GateEmptySD::Initialize(G4HCofThisEvent*HCE) -{ - HCE->AddHitsCollection(HCID, nullptr); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -// Implementation of the pure virtual method ProcessHits(). -G4bool GateEmptySD::ProcessHits(G4Step*aStep, G4TouchableHistory*) -{ - return true; -} \ No newline at end of file