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 // ********************************************************************
28 // Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
30 // WARNING : This class is released as a prototype.
31 // It might strongly evolve or even disapear in the next releases.
35 // 10 Oct 2011 M.Karamitros created
37 // -------------------------------------------------------------------
40 G4ThreadLocal G4ITMANAGER * G4ITMANAGER::fInstance = 0;
43 G4ITMANAGER * G4ITMANAGER::Instance()
45 if (!fInstance) fInstance = new G4ITFinder();
50 TEMPLATE G4ITMANAGER::G4ITFinder() : G4VITFinder()
53 G4AllITFinder::Instance()->RegisterManager(this);
56 TEMPLATE G4ITMANAGER::~G4ITFinder()
63 void G4ITMANAGER::Clear()
69 for (it = fTree.begin(); it != fTree.end(); it++)
71 if (it->second) delete it->second;
79 void G4ITMANAGER::iUpdatePositionMap()
81 fInstance->UpdatePositionMap();
85 void G4ITMANAGER::Push(G4Track* track)
88 * If you want to use this class with another type than G4Molecule
89 * inheriting as well from G4IT, replace T::GetMolecule by GetIT
90 * and aIT->GetMoleculeID by GetSubITType
92 T* aIT = T::GetMolecule(track);
93 aIT->RecordCurrentPositionNTime();
95 int key = aIT->GetMoleculeID();
97 if (!(aIT->GetNode()))
99 G4KDNode_Base* node = 0;
101 TreeMap::iterator it_fTree = fTree.find(key);
103 if (it_fTree != fTree.end())
105 node = it_fTree->second->Insert(aIT);
109 G4KDTree* aTree = new G4KDTree();
110 fTree.insert(std::make_pair(key, aTree));
111 node = aTree->Insert(aIT);
119 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const G4ThreeVector& position,
122 TreeMap::iterator it = fTree.find(key);
123 if (it != fTree.end()) return it->second->Nearest(position);
132 G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* point0,
135 if (int(*point0) == key)
138 // G4cout << "Equal keys !"<< G4endl;
139 G4KDNode_Base* node0 = point0->GetNode();
143 G4ExceptionDescription exceptionDescription(
144 "Bad request : no node found in the IT you are searching "
145 "closest neighbourg for");
146 G4Exception("G4ITManager::FindNearest", "ITManager002",
147 FatalErrorInArgument, exceptionDescription);
148 return 0; // coverity
151 TreeMap::iterator it = fTree.find(key);
152 if (it != fTree.end())
154 G4KDTreeResultHandle output(it->second->Nearest(node0));
159 * G4cout << "NO OUTPUT "
160 * << point0->GetName()
161 * << " " << key -> GetName()
166 // G4cout << "OUTPUT" << G4endl;
172 // G4cout << "Pas trouve dans la map"<< key->GetName() << G4endl;
179 // G4cout << "Not equal keys !"<< G4endl;
180 // const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
181 TreeMap::iterator it = fTree.find(key);
183 if (it != fTree.end())
185 G4KDTreeResultHandle output(it->second->Nearest(*point0));
188 // G4cout << "NO OUTPUT" << G4endl;
192 // G4cout << "OUTPUT" << G4endl;
198 // G4cout << "Pas trouve dans la map : "<< key->GetName() << G4endl;
207 G4ITMANAGER::FindNearest(const T* source,
210 return FindNearest(source,
217 G4ITMANAGER::FindNearestInRange(const G4ThreeVector& position,
221 TreeMap::iterator it = fTree.find(key);
222 if (it != fTree.end()) return it->second->NearestInRange(position, R);
231 G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0,
235 if (point0->GetMoleculeID() == key)
237 G4KDNode_Base* node0 = point0->GetNode();
238 TreeMap::iterator it = fTree.find(key);
239 if (it != fTree.end()) return it->second->NearestInRange(node0, R);
247 TreeMap::iterator it = fTree.find(key);
248 if (it != fTree.end()) return it->second->NearestInRange(*point0, R);
260 #include "G4MemStat.hh"
261 using namespace G4MemStat;
265 void G4ITMANAGER::UpdatePositionMap()
267 G4KDTree* currentTree = 0;
269 G4KDNode_Base* currentNode = 0;
271 #if defined (DEBUG_MEM)
272 MemStat mem_first, mem_second, mem_diff;
275 #if defined (DEBUG_MEM)
276 mem_first = MemoryUsage();
279 std::map<int,PriorityList*>& listMap = G4ITTrackHolder::Instance()->GetLists();
280 std::map<int,PriorityList*>::iterator it = listMap.begin();
281 std::map<int,PriorityList*>::iterator end = listMap.end();
283 TreeMap::iterator it_Tree;
285 for (; it!= end; it++)
289 it_Tree = fTree.find(key);
291 // G4cout << "Box : " << it_Box->first.GetName() << G4endl;
292 // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
294 // G4cout << "Will update : " << it_Box->first.GetName() <<G4endl;
295 if (it_Tree != fTree.end())
297 currentTree = it_Tree->second;
300 currentTree->Clear();
301 //delete currentTree;
304 #if defined (DEBUG_MEM)
305 mem_second = MemoryUsage();
306 mem_diff = mem_second-mem_first;
307 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
308 "after currentTree->Clear(), diff is : " << mem_diff << G4endl;
313 PriorityList* listUnion=it->second;
315 if(listUnion == 0 || listUnion->GetMainList() == 0
316 || listUnion->GetMainList()->empty() )
318 #if defined (DEBUG_MEM)
319 mem_second = MemoryUsage();
320 mem_diff = mem_second-mem_first;
321 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
322 "In if(currentBox->Empty()), diff is : " << mem_diff << G4endl;
329 if (currentTree == 0)
331 currentTree = new G4KDTree();
332 fTree[key] = currentTree;
334 // G4cout << "**** " << "Create new tree for : " << key.GetName()
337 #if defined (DEBUG_MEM)
338 mem_second = MemoryUsage();
339 mem_diff = mem_second-mem_first;
340 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
341 "after creating tree, diff is : " << mem_diff << G4endl;
345 G4TrackList* trackList = listUnion->GetMainList();
346 G4TrackList::iterator __it = trackList->begin();
347 G4TrackList::iterator __end = trackList->end();
350 // G4cout << "Box : " << key.GetName() << G4endl;
351 // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
353 for (; __it != __end; __it++)//, i++)
355 // G4cout << i << G4endl;
356 // G4cout << "track "
357 // << currentIT->GetTrack()->GetTrackID()
359 currentIT = GetIT(*__it);
360 currentNode = currentTree->Insert(currentIT);
363 // currentNode = currentTree->InsertMap(currentIT);
364 // if(currentIT->GetNode())
366 // delete currentIT->GetNode();
367 // No need: deleted when tree is cleared
369 currentIT->SetNode(currentNode);
371 #if defined (DEBUG_MEM)
372 mem_second = MemoryUsage();
373 mem_diff = mem_second-mem_first;
374 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
375 "after currentIT->SetNode(currentNode), diff is : "
376 << mem_diff << G4endl;
380 #if defined (DEBUG_MEM)
381 mem_second = MemoryUsage();
382 mem_diff = mem_second-mem_first;
383 G4cout << "\t || MEM || G4ITMANAGER::UpdatePositionMap || "
384 "In else{...}, diff is : " << mem_diff << G4endl;
388 // currentTree->Build();