2// ********************************************************************
3// * License and Disclaimer *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
27// Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
29// WARNING : This class is released as a prototype.
30// It might strongly evolve or even disapear in the next releases.
34// 10 Oct 2011 M.Karamitros created
36// -------------------------------------------------------------------
39G4ThreadLocal G4ITMANAGER * G4ITMANAGER::fInstance = 0;
42G4ITMANAGER * G4ITMANAGER::Instance()
44 if (!fInstance) fInstance = new G4ITFinder();
49TEMPLATE G4ITMANAGER::G4ITFinder() : G4VITFinder()
52 G4AllITFinder::Instance()->RegisterManager(this);
55TEMPLATE G4ITMANAGER::~G4ITFinder()
62void G4ITMANAGER::Clear()
68 for (it = fTree.begin(); it != fTree.end(); it++)
70 if (it->second) delete it->second;
78void G4ITMANAGER::iUpdatePositionMap()
80 fInstance->UpdatePositionMap();
84void G4ITMANAGER::Push(G4Track* track)
87 * If you want to use this class with another type than G4Molecule
88 * inheriting as well from G4IT, replace T::GetMolecule by GetIT
89 * and aIT->GetMoleculeID by GetSubITType
91 T* aIT = T::GetMolecule(track);
92 aIT->RecordCurrentPositionNTime();
94 int key = aIT->GetMoleculeID();
96 if (!(aIT->GetNode()))
98 G4KDNode_Base* node = 0;
100 TreeMap::iterator it_fTree = fTree.find(key);
102 if (it_fTree != fTree.end())
104 node = it_fTree->second->Insert(aIT);
108 G4KDTree* aTree = new G4KDTree();
109 fTree.insert(std::make_pair(key, aTree));
110 node = aTree->Insert(aIT);
118G4KDTreeResultHandle G4ITMANAGER::FindNearest(const G4ThreeVector& position,
121 TreeMap::iterator it = fTree.find(key);
122 if (it != fTree.end()) return it->second->Nearest(position);
131G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* point0,
134 if (int(*point0) == key)
137 // G4cout << "Equal keys !"<< G4endl;
138 G4KDNode_Base* node0 = point0->GetNode();
142 G4ExceptionDescription exceptionDescription(
143 "Bad request : no node found in the IT you are searching "
144 "closest neighbourg for");
145 G4Exception("G4ITManager::FindNearest", "ITManager002",
146 FatalErrorInArgument, exceptionDescription);
147 return 0; // coverity
150 TreeMap::iterator it = fTree.find(key);
151 if (it != fTree.end())
153 G4KDTreeResultHandle output(it->second->Nearest(node0));
158 * G4cout << "NO OUTPUT "
159 * << point0->GetName()
160 * << " " << key -> GetName()
165 // G4cout << "OUTPUT" << G4endl;
171 // G4cout << "Pas trouve dans la map"<< key->GetName() << G4endl;
178 // G4cout << "Not equal keys !"<< G4endl;
179 // const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
180 TreeMap::iterator it = fTree.find(key);
182 if (it != fTree.end())
184 G4KDTreeResultHandle output(it->second->Nearest(*point0));
187 // G4cout << "NO OUTPUT" << G4endl;
191 // G4cout << "OUTPUT" << G4endl;
197 // G4cout << "Pas trouve dans la map : "<< key->GetName() << G4endl;
206G4ITMANAGER::FindNearest(const T* source,
209 return FindNearest(source,
216G4ITMANAGER::FindNearestInRange(const G4ThreeVector& position,
220 TreeMap::iterator it = fTree.find(key);
221 if (it != fTree.end()) return it->second->NearestInRange(position, R);
230G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0,
234 if (point0->GetMoleculeID() == key)
236 G4KDNode_Base* node0 = point0->GetNode();
237 TreeMap::iterator it = fTree.find(key);
238 if (it != fTree.end()) return it->second->NearestInRange(node0, R);
246 TreeMap::iterator it = fTree.find(key);
247 if (it != fTree.end()) return it->second->NearestInRange(*point0, R);
259#include "G4MemStat.hh"
260using namespace G4MemStat;
264void G4ITMANAGER::UpdatePositionMap()
266 G4KDTree* currentTree = 0;
268 G4KDNode_Base* currentNode = 0;
270#if defined (DEBUG_MEM)
271 MemStat mem_first, mem_second, mem_diff;
274#if defined (DEBUG_MEM)
275 mem_first = MemoryUsage();
278 std::map<int,PriorityList*>& listMap = G4ITTrackHolder::Instance()->GetLists();
279 std::map<int,PriorityList*>::iterator it = listMap.begin();
280 std::map<int,PriorityList*>::iterator end = listMap.end();
282 TreeMap::iterator it_Tree;
284 for (; it!= end; it++)
288 it_Tree = fTree.find(key);
290 // G4cout << "Box : " << it_Box->first.GetName() << G4endl;
291 // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
293 // G4cout << "Will update : " << it_Box->first.GetName() <<G4endl;
294 if (it_Tree != fTree.end())
296 currentTree = it_Tree->second;
299 currentTree->Clear();
300 //delete currentTree;
303#if defined (DEBUG_MEM)
304 mem_second = MemoryUsage();
305 mem_diff = mem_second-mem_first;
306 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
307 "after currentTree->Clear(), diff is : " << mem_diff << G4endl;
312 PriorityList* listUnion=it->second;
314 if(listUnion == 0 || listUnion->GetMainList() == 0
315 || listUnion->GetMainList()->empty() )
317#if defined (DEBUG_MEM)
318 mem_second = MemoryUsage();
319 mem_diff = mem_second-mem_first;
320 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
321 "In if(currentBox->Empty()), diff is : " << mem_diff << G4endl;
328 if (currentTree == 0)
330 currentTree = new G4KDTree();
331 fTree[key] = currentTree;
333// G4cout << "**** " << "Create new tree for : " << key.GetName()
336#if defined (DEBUG_MEM)
337 mem_second = MemoryUsage();
338 mem_diff = mem_second-mem_first;
339 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
340 "after creating tree, diff is : " << mem_diff << G4endl;
344 G4TrackList* trackList = listUnion->GetMainList();
345 G4TrackList::iterator __it = trackList->begin();
346 G4TrackList::iterator __end = trackList->end();
349 // G4cout << "Box : " << key.GetName() << G4endl;
350 // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
352 for (; __it != __end; __it++)//, i++)
354 // G4cout << i << G4endl;
355 // G4cout << "track "
356 // << currentIT->GetTrack()->GetTrackID()
358 currentIT = GetIT(*__it);
359 currentNode = currentTree->Insert(currentIT);
362// currentNode = currentTree->InsertMap(currentIT);
363// if(currentIT->GetNode())
365// delete currentIT->GetNode();
366 // No need: deleted when tree is cleared
368 currentIT->SetNode(currentNode);
370#if defined (DEBUG_MEM)
371 mem_second = MemoryUsage();
372 mem_diff = mem_second-mem_first;
373 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
374 "after currentIT->SetNode(currentNode), diff is : "
375 << mem_diff << G4endl;
379#if defined (DEBUG_MEM)
380 mem_second = MemoryUsage();
381 mem_diff = mem_second-mem_first;
382 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
383 "In else{...}, diff is : " << mem_diff << G4endl;
387// currentTree->Build();