Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Geant4/G4MoleculeCounterManager.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 
0027 // Author: Christian Velten (2025)
0028 
0029 #ifndef G4MOLECULECOUNTERMANAGER_HH
0030 #define G4MOLECULECOUNTERMANAGER_HH 1
0031 
0032 #include "G4Exception.hh"
0033 #include "G4Threading.hh"
0034 #include "G4Types.hh"
0035 #include "G4VMoleculeCounter.hh"
0036 #include "G4VMoleculeReactionCounter.hh"
0037 #include "G4ios.hh"
0038 
0039 #include <functional>
0040 #include <map>
0041 #include <vector>
0042 
0043 class G4Event;
0044 class G4Run;
0045 class G4Step;
0046 class G4StepPoint;
0047 class G4Track;
0048 class G4MoleculeCounterManagerMessenger;
0049 
0050 class G4MoleculeCounterManager final
0051 {
0052   private:
0053     struct Private
0054     {
0055         explicit Private() = default;
0056     };
0057 
0058   public:
0059     G4MoleculeCounterManager(Private);
0060     ~G4MoleculeCounterManager();
0061 
0062   private:
0063     G4bool fInstancesRegistered{false};
0064     void RegisterInstance();
0065 
0066   public:
0067     static G4MoleculeCounterManager* Instance();
0068     static G4MoleculeCounterManager* GetInstanceIfExists();
0069     static void DeleteInstance();
0070 
0071     void Initialize();
0072 
0073     //
0074     // Management
0075 
0076     G4int RegisterCounter(std::unique_ptr<G4VMoleculeCounter>);
0077     G4int RegisterCounter(std::unique_ptr<G4VMoleculeReactionCounter>);
0078     void DeregisterAllCounters();
0079 
0080   private:
0081     template<typename T>
0082     G4int RegisterCounter(std::map<G4int, T*>&, std::unique_ptr<T>, std::function<G4int()>);
0083     void InitializeMaster();
0084     void InitializeWorker();
0085 
0086   public:
0087     // methods to receive forwarding from G4DNAChemistryManager
0088     void BeginOfEventAction(const G4Event*);
0089     void BeginOfRunAction(const G4Run*);
0090     void EndOfEventAction(const G4Event*);
0091     void EndOfRunAction(const G4Run*);
0092     // Dumping counters
0093     void DumpMasterCounters() const;
0094     void DumpWorkerCounters() const;
0095     // Accumulation of counters into master
0096     void AbsorbWorkerManagerCounters(const G4MoleculeCounterManager* = nullptr);
0097 
0098     //
0099     // Calls to Molecule Counters
0100   public:
0101     // [[deprecated("This should only be used for IRT and may be replaced as well.")]]
0102     void AddMoleculeWithoutTrack(const G4MolecularConfiguration*, G4double, G4int = 1);
0103     // [[deprecated("This should only be used for IRT and may be replaced as well.")]]
0104     void RemoveMoleculeWithoutTrack(const G4MolecularConfiguration*, G4double, G4int = 1);
0105 
0106     void AddMolecule(const G4Track*, G4double, G4int = 1);
0107     void RemoveMolecule(const G4Track*, G4double, G4int = 1);
0108 
0109     void AddMolecule(const G4Track*, const G4StepPoint*, G4double, G4int = 1);
0110     void RemoveMolecule(const G4Track*, const G4StepPoint*, G4double, G4int = 1);
0111 
0112     void ActivateCounterAtTimes(G4int, G4double, G4double, G4bool = true, G4bool = true);
0113 
0114     //
0115     // Calls to Molecule Reaction Counters
0116 
0117     // [[deprecated("TBD")]]
0118     void RecordReaction(const G4DNAMolecularReactionData*, G4double, G4int = 1);
0119     // void RecordReaction(const G4Track*, const G4Track*, const G4DNAMolecularReactionData*, G4double,
0120     //                     G4int = 1);
0121 
0122     void ActivateReactionCounterAtTimes(G4int, G4double, G4double, G4bool = true, G4bool = true);
0123 
0124     //
0125     // Other Calls to both types or only one (e.g., BroadcastIgnoreMolecule)
0126 
0127     void ResetCounters();
0128 
0129     void NotifyOfStep(const G4Step*);
0130     void NotifyOfFinalize();
0131 
0132     // These will be broadcast to all counters registered at time of call
0133     void BroadcastIgnoreMolecule(const G4MoleculeDefinition*);
0134     void BroadcastIgnoreReactant(const G4MolecularConfiguration*);
0135     void BroadcastRegisterAllMoleculesAndReactants();
0136 
0137   private:
0138     static const G4MoleculeCounterManager* fpMasterInstance;
0139     static std::vector<const G4MoleculeCounterManager*> fWorkerInstances;
0140     G4ThreadLocalStatic std::unique_ptr<G4MoleculeCounterManager> fpInstance;
0141 
0142     std::unique_ptr<G4MoleculeCounterManagerMessenger> fpMessenger;
0143 
0144     G4int fVerbosity;
0145     G4bool fIsInitialized;
0146     G4bool fIsActive;
0147     static std::atomic<G4bool> fResetCountersBeforeEvent;
0148     static std::atomic<G4bool> fResetCountersBeforeRun;
0149     static std::atomic<G4bool> fResetMasterCounterWithWorkers;
0150     static std::atomic<G4bool> fAccumulateCounterIntoMaster;
0151 
0152     std::map<G4int, G4VMoleculeCounter*> fCounters{};
0153     std::map<G4int, G4VMoleculeReactionCounter*> fReactionCounters{};
0154 
0155   public:
0156     G4bool GetIsActive() const { return fIsActive; }
0157     void SetIsActive(G4bool flag) { fIsActive = flag; }
0158 
0159     G4int GetVerbosity() const { return fVerbosity; }
0160     void SetVerbosity(G4int v) { fVerbosity = v; }
0161 
0162     G4bool GetResetCountersBeforeEvent() const;
0163     void SetResetCountersBeforeEvent(G4bool = true);
0164 
0165     G4bool GetResetCountersBeforeRun() const;
0166     void SetResetCountersBeforeRun(G4bool = true);
0167 
0168     G4bool GetResetMasterCounterWithWorkers() const;
0169     void SetResetMasterCounterWithWorkers(G4bool = true);
0170 
0171     G4bool GetAccumulateCounterIntoMaster() const;
0172     void SetAccumulateCounterIntoMaster(G4bool = true);
0173 
0174     std::vector<const G4VMoleculeCounter*> GetMoleculeCounters() const;
0175     std::vector<const G4VMoleculeCounter*> GetMoleculeCounters(G4String) const;
0176     template<typename T = G4VMoleculeCounter>
0177     const T* GetMoleculeCounter(G4int) const;
0178     G4VMoleculeCounter* GetEditableMoleculeCounter(G4int) const;
0179 
0180     std::vector<const G4VMoleculeReactionCounter*> GetMoleculeReactionCounters() const;
0181     std::vector<const G4VMoleculeReactionCounter*> GetMoleculeReactionCounters(G4String) const;
0182     G4VMoleculeReactionCounter* GetEditableMoleculeReactionCounter(G4int) const;
0183     template<typename T = G4VMoleculeReactionCounter>
0184     const T* GetMoleculeReactionCounter(G4int) const;
0185 
0186   private:
0187     G4ThreadLocalStatic std::atomic<G4bool> fBeginOfEventTriggered;
0188     static std::atomic<G4bool> fBeginOfRunTriggered;
0189 };
0190 
0191 //------------------------------------------------------------------------------
0192 
0193 template<typename T>
0194 G4int G4MoleculeCounterManager::RegisterCounter(std::map<G4int, T*>& map,
0195                                                 std::unique_ptr<T> counter,
0196                                                 std::function<G4int()> idProvider)
0197 {
0198   // this template allows for more than one counter type to be registered in the same way without
0199   // duplicating code; see public methods for G4VMoleculeCounter and G4VMoleculeReactionCounter
0200 
0201   if (fVerbosity > 0) {
0202     G4cout << "G4MoleculeCounterManager::RegisterCounter ("
0203            << (G4Threading::IsMasterThread() ? "master" : "worker") << ")" << G4endl;
0204   }
0205 
0206   if (counter->GetManagedId() >= 0) {
0207     G4ExceptionDescription description;
0208     description << "Trying to add a counter whose id was already altered to be non-negative!\n";
0209     description << "  Id: " << counter->GetManagedId() << "\n";
0210     description << "Name: " << counter->GetName();
0211     G4Exception("G4MoleculeCounterManager::RegisterCounter", "MOLMAN002", FatalErrorInArgument,
0212                 description);
0213   }
0214 
0215   T* sp = counter.release();
0216   // Set managed Id
0217   sp->SetManagedId(idProvider());
0218 
0219   map.emplace(sp->GetManagedId(), sp);
0220 
0221   return sp->GetManagedId();
0222 }
0223 
0224 //------------------------------------------------------------------------------
0225 
0226 template<typename T>
0227 const T* G4MoleculeCounterManager::GetMoleculeCounter(G4int id) const
0228 {
0229   static_assert(std::is_base_of<G4VMoleculeCounter, T>::value,
0230                 "T must be derived from G4VMoleculeCounter!");
0231   auto base_ptr = GetEditableMoleculeCounter(id);
0232   if (base_ptr == nullptr) return nullptr;
0233   const T* ptr = dynamic_cast<const T*>(base_ptr);
0234   return ptr;
0235 }
0236 
0237 template<typename T>
0238 const T* G4MoleculeCounterManager::GetMoleculeReactionCounter(G4int id) const
0239 {
0240   static_assert(std::is_base_of<G4VMoleculeReactionCounter, T>::value,
0241                 "T must be derived from G4VMoleculeReactionCounter!");
0242   auto base_ptr = GetEditableMoleculeReactionCounter(id);
0243   if (base_ptr == nullptr) return nullptr;
0244   const T* ptr = dynamic_cast<const T*>(base_ptr);
0245   return ptr;
0246 }
0247 
0248 //------------------------------------------------------------------------------
0249 
0250 #endif  // G4MOLECULECOUNTERMANAGER_HH