Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:32

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: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
0028 //
0029 // WARNING : This class is released as a prototype.
0030 // It might strongly evolve or even disapear in the next releases.
0031 //
0032 // History:
0033 // -----------
0034 // 10 Oct 2011 M.Karamitros created
0035 //
0036 // -------------------------------------------------------------------
0037 
0038 TEMPLATE
0039 G4ThreadLocal G4ITMANAGER* G4ITMANAGER::fInstance = nullptr;
0040 
0041 TEMPLATE
0042 G4ITMANAGER* G4ITMANAGER::Instance()
0043 {
0044   if(!fInstance)
0045   {
0046     fInstance = new G4ITFinder();
0047   }
0048   return fInstance;
0049 }
0050 
0051 TEMPLATE G4ITMANAGER::G4ITFinder()
0052   : G4VITFinder()
0053 {
0054   G4AllITFinder::Instance()->RegisterManager(this);
0055 }
0056 
0057 TEMPLATE G4ITMANAGER::~G4ITFinder()
0058 {
0059   for(auto & it : fTree)
0060   {
0061     delete it.second;
0062   }
0063   fTree.clear();
0064   fInstance = nullptr;
0065 }
0066 
0067 TEMPLATE
0068 void G4ITMANAGER::Clear()
0069 {
0070   for(auto & it : fTree)
0071   {
0072     delete it.second;
0073   }
0074   fTree.clear();
0075 }
0076 
0077 TEMPLATE
0078 void G4ITMANAGER::Push(G4Track* track)
0079 {
0080   /*
0081    * If you want to use this class with another type than G4Molecule
0082    * inheriting as well from G4IT, replace T::GetMolecule by GetIT
0083    * and aIT->GetMoleculeID by GetSubITType
0084    */
0085   T* aIT = T::GetMolecule(track);
0086   aIT->RecordCurrentPositionNTime();
0087 
0088   G4int key = aIT->GetMoleculeID();
0089 
0090   if(!(aIT->GetNode()))
0091   {
0092     G4KDNode_Base* node;
0093 
0094     auto it_fTree = fTree.find(key);
0095 
0096     if(it_fTree != fTree.end())
0097     {
0098       node = it_fTree->second->Insert(aIT);
0099     }
0100     else
0101     {
0102       auto  aTree = new G4KDTree();
0103       fTree.insert(std::make_pair(key, aTree));
0104       node = aTree->Insert(aIT);
0105     }
0106     aIT->SetNode(node);
0107   }
0108 }
0109 
0110 TEMPLATE
0111 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const G4ThreeVector& position,
0112                                               G4int key)
0113 {
0114   auto it = fTree.find(key);
0115   if(it != fTree.end())
0116   {
0117     return it->second->Nearest(position);
0118   }
0119   
0120   return nullptr;
0121 }
0122 
0123 TEMPLATE
0124 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* point0, G4int key)
0125 {
0126   if(G4int(*point0) == key)
0127   {
0128     G4KDNode_Base* node0 = point0->GetNode();
0129     if(node0 == nullptr)
0130     {
0131       G4ExceptionDescription exceptionDescription(
0132         "Bad request : no node found in the IT you are searching "
0133         "closest neighbourg for");
0134       G4Exception("G4ITManager::FindNearest", "ITManager002",
0135                   FatalErrorInArgument, exceptionDescription);
0136       return nullptr;  // coverity
0137     }
0138     auto it = fTree.find(key);
0139     if(it != fTree.end())
0140     {
0141       G4KDTreeResultHandle output(it->second->Nearest(node0));
0142       if(!output)
0143       {
0144         return nullptr;
0145       }
0146       return output;
0147     }
0148     
0149     return nullptr;
0150   }
0151   
0152   auto it = fTree.find(key);
0153   if(it != fTree.end())
0154   {
0155     G4KDTreeResultHandle output(it->second->Nearest(*point0));
0156     if(!output)
0157     {
0158       return nullptr;
0159     }
0160     return output;
0161   }
0162   
0163   return nullptr;
0164 }
0165 
0166 TEMPLATE
0167 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* source, const T* type)
0168 {
0169   return FindNearest(source, G4int(*type));
0170 }
0171 
0172 TEMPLATE
0173 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(
0174   const G4ThreeVector& position, G4int key, G4double R)
0175 {
0176   auto it = fTree.find(key);
0177   if(it != fTree.end())
0178   {
0179     return it->second->NearestInRange(position, R);
0180   }
0181   
0182   return nullptr;
0183 }
0184 
0185 TEMPLATE
0186 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0, G4int key,
0187                                                      G4double R)
0188 {
0189   if(point0->GetMoleculeID() == key)
0190   {
0191     G4KDNode_Base* node0 = point0->GetNode();
0192     auto it              = fTree.find(key);
0193     if(it != fTree.end())
0194       return it->second->NearestInRange(node0, R);
0195     
0196    return nullptr;
0197   }
0198   
0199   auto it = fTree.find(key);
0200   if(it != fTree.end())
0201     return it->second->NearestInRange(*point0, R);
0202     
0203   return nullptr;
0204 }
0205 
0206 //#define DEBUG_MEM
0207 
0208 #ifdef DEBUG_MEM
0209 #  include "G4MemStat.hh"
0210 using namespace G4MemStat;
0211 #endif
0212 TEMPLATE
0213 void G4ITMANAGER::UpdatePositionMap()
0214 {
0215 #if defined(DEBUG_MEM)
0216   MemStat mem_first, mem_second, mem_diff;
0217 #endif
0218 
0219 #if defined(DEBUG_MEM)
0220   mem_first = MemoryUsage();
0221 #endif
0222   Clear();
0223   const std::map<G4int, PriorityList*>& listMap =
0224     G4ITTrackHolder::Instance()->GetLists();
0225   auto it  = listMap.begin();
0226   auto end = listMap.end();
0227 
0228   for(; it != end; it++)
0229   {
0230     G4int key               = it->first;
0231     PriorityList* listUnion = it->second;
0232     if(listUnion == nullptr || listUnion->GetMainList() == nullptr ||
0233        listUnion->GetMainList()->empty())
0234     {
0235 #if defined(DEBUG_MEM)
0236       mem_second = MemoryUsage();
0237       mem_diff   = mem_second - mem_first;
0238       G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
0239                 "In if(currentBox->Empty()), diff is : "
0240              << mem_diff << G4endl;
0241 #endif
0242 
0243       continue;
0244     }
0245     
0246     auto currentTree = new G4KDTree();
0247     fTree[key]       = currentTree;
0248 #if defined(DEBUG_MEM)
0249     mem_second = MemoryUsage();
0250     mem_diff   = mem_second - mem_first;
0251     G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
0252               "after creating tree, diff is : "
0253            << mem_diff << G4endl;
0254 #endif
0255 
0256     G4TrackList* trackList      = listUnion->GetMainList();
0257     G4TrackList::iterator __it  = trackList->begin();
0258     G4TrackList::iterator __end = trackList->end();
0259 
0260      for(; __it != __end; __it++)
0261      {
0262        G4IT* currentIT            = GetIT(*__it);
0263        G4KDNode_Base* currentNode = currentTree->Insert(currentIT);
0264        currentIT->SetNode(currentNode);
0265 
0266 #if defined(DEBUG_MEM)
0267       mem_second = MemoryUsage();
0268       mem_diff   = mem_second - mem_first;
0269       G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
0270                 "after currentIT->SetNode(currentNode), diff is : "
0271              << mem_diff << G4endl;
0272 #endif
0273     }
0274 
0275 #if defined(DEBUG_MEM)
0276     mem_second = MemoryUsage();
0277     mem_diff   = mem_second - mem_first;
0278     G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
0279               "In else{...}, diff is : "
0280            << mem_diff << G4endl;
0281 #endif
0282    
0283   }
0284 }