Geant4-11
G4ITFinder.icc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
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. *
10// * *
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. *
17// * *
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// ********************************************************************
25//
26//
27// Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr)
28//
29// WARNING : This class is released as a prototype.
30// It might strongly evolve or even disapear in the next releases.
31//
32// History:
33// -----------
34// 10 Oct 2011 M.Karamitros created
35//
36// -------------------------------------------------------------------
37
38TEMPLATE
39G4ThreadLocal G4ITMANAGER * G4ITMANAGER::fInstance = 0;
40
41TEMPLATE
42G4ITMANAGER * G4ITMANAGER::Instance()
43{
44 if (!fInstance) fInstance = new G4ITFinder();
45
46 return fInstance;
47}
48
49TEMPLATE G4ITMANAGER::G4ITFinder() : G4VITFinder()
50{
51 fVerbose = 0;
52 G4AllITFinder::Instance()->RegisterManager(this);
53}
54
55TEMPLATE G4ITMANAGER::~G4ITFinder()
56{
57 Clear();
58 fInstance = 0;
59}
60
61TEMPLATE
62void G4ITMANAGER::Clear()
63{
64 {
65
66 TreeMap::iterator it;
67
68 for (it = fTree.begin(); it != fTree.end(); it++)
69 {
70 if (it->second) delete it->second;
71 }
72
73 fTree.clear();
74 }
75}
76
77TEMPLATE
78void G4ITMANAGER::iUpdatePositionMap()
79{
80 fInstance->UpdatePositionMap();
81}
82
83TEMPLATE
84void G4ITMANAGER::Push(G4Track* track)
85{
86 /*
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
90 */
91 T* aIT = T::GetMolecule(track);
92 aIT->RecordCurrentPositionNTime();
93
94 int key = aIT->GetMoleculeID();
95
96 if (!(aIT->GetNode()))
97 {
98 G4KDNode_Base* node = 0;
99
100 TreeMap::iterator it_fTree = fTree.find(key);
101
102 if (it_fTree != fTree.end())
103 {
104 node = it_fTree->second->Insert(aIT);
105 }
106 else
107 {
108 G4KDTree* aTree = new G4KDTree();
109 fTree.insert(std::make_pair(key, aTree));
110 node = aTree->Insert(aIT);
111 }
112
113 aIT->SetNode(node);
114 }
115}
116
117TEMPLATE
118G4KDTreeResultHandle G4ITMANAGER::FindNearest(const G4ThreeVector& position,
119 int key)
120{
121 TreeMap::iterator it = fTree.find(key);
122 if (it != fTree.end()) return it->second->Nearest(position);
123 else
124 {
125 return 0;
126 }
127 return 0;
128}
129
130TEMPLATE
131G4KDTreeResultHandle G4ITMANAGER::FindNearest(const T* point0,
132 int key)
133{
134 if (int(*point0) == key)
135 {
136 // DEBUG
137 // G4cout << "Equal keys !"<< G4endl;
138 G4KDNode_Base* node0 = point0->GetNode();
139
140 if (node0 == 0)
141 {
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
148 }
149
150 TreeMap::iterator it = fTree.find(key);
151 if (it != fTree.end())
152 {
153 G4KDTreeResultHandle output(it->second->Nearest(node0));
154
155 if (!output)
156 {
157 /*
158 * G4cout << "NO OUTPUT "
159 * << point0->GetName()
160 * << " " << key -> GetName()
161 * << G4endl;
162 */
163 return 0;
164 }
165 // G4cout << "OUTPUT" << G4endl;
166 return output;
167 }
168 else
169 {
170 // DEBUG
171 // G4cout << "Pas trouve dans la map"<< key->GetName() << G4endl;
172 return 0;
173 }
174 }
175 else
176 {
177 // DEBUG
178 // G4cout << "Not equal keys !"<< G4endl;
179 // const G4ThreeVector& position = point0->GetTrack()->GetPosition() ;
180 TreeMap::iterator it = fTree.find(key);
181
182 if (it != fTree.end())
183 {
184 G4KDTreeResultHandle output(it->second->Nearest(*point0));
185 if (!output)
186 {
187 // G4cout << "NO OUTPUT" << G4endl;
188 return 0;
189 }
190
191 // G4cout << "OUTPUT" << G4endl;
192 return output;
193 }
194 else
195 {
196 // DEBUG
197 // G4cout << "Pas trouve dans la map : "<< key->GetName() << G4endl;
198 return 0;
199 }
200 }
201 return 0;
202}
203
204TEMPLATE
205G4KDTreeResultHandle
206G4ITMANAGER::FindNearest(const T* source,
207 const T* type)
208{
209 return FindNearest(source,
210 int(*type));
211}
212
213
214TEMPLATE
215G4KDTreeResultHandle
216G4ITMANAGER::FindNearestInRange(const G4ThreeVector& position,
217 int key,
218 G4double R)
219{
220 TreeMap::iterator it = fTree.find(key);
221 if (it != fTree.end()) return it->second->NearestInRange(position, R);
222 else
223 {
224 return 0;
225 }
226 return 0;
227}
228
229TEMPLATE
230G4KDTreeResultHandle G4ITMANAGER::FindNearestInRange(const T* point0,
231 int key,
232 G4double R)
233{
234 if (point0->GetMoleculeID() == key)
235 {
236 G4KDNode_Base* node0 = point0->GetNode();
237 TreeMap::iterator it = fTree.find(key);
238 if (it != fTree.end()) return it->second->NearestInRange(node0, R);
239 else
240 {
241 return 0;
242 }
243 }
244 else
245 {
246 TreeMap::iterator it = fTree.find(key);
247 if (it != fTree.end()) return it->second->NearestInRange(*point0, R);
248 else
249 {
250 return 0;
251 }
252 }
253 return 0;
254}
255
256//#define DEBUG_MEM
257
258#ifdef DEBUG_MEM
259#include "G4MemStat.hh"
260using namespace G4MemStat;
261#endif
262
263TEMPLATE
264void G4ITMANAGER::UpdatePositionMap()
265{
266 G4KDTree* currentTree = 0;
267 G4IT* currentIT = 0;
268 G4KDNode_Base* currentNode = 0;
269
270#if defined (DEBUG_MEM)
271 MemStat mem_first, mem_second, mem_diff;
272#endif
273
274#if defined (DEBUG_MEM)
275 mem_first = MemoryUsage();
276#endif
277
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();
281
282 TreeMap::iterator it_Tree;
283
284 for (; it!= end; it++)
285 {
286 currentTree = 0;
287 int key = it->first;
288 it_Tree = fTree.find(key);
289
290 // G4cout << "Box : " << it_Box->first.GetName() << G4endl;
291 // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
292
293 // G4cout << "Will update : " << it_Box->first.GetName() <<G4endl;
294 if (it_Tree != fTree.end())
295 {
296 currentTree = it_Tree->second;
297 if (currentTree)
298 {
299 currentTree->Clear();
300 //delete currentTree;
301 //currentTree = 0;
302
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;
308#endif
309 }
310 }
311
312 PriorityList* listUnion=it->second;
313
314 if(listUnion == 0 || listUnion->GetMainList() == 0
315 || listUnion->GetMainList()->empty() )
316 {
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;
322#endif
323
324 continue;
325 }
326 else
327 {
328 if (currentTree == 0)
329 {
330 currentTree = new G4KDTree();
331 fTree[key] = currentTree;
332
333// G4cout << "**** " << "Create new tree for : " << key.GetName()
334// << G4endl;
335
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;
341#endif
342 }
343
344 G4TrackList* trackList = listUnion->GetMainList();
345 G4TrackList::iterator __it = trackList->begin();
346 G4TrackList::iterator __end = trackList->end();
347 // int i = 0;
348
349 // G4cout << "Box : " << key.GetName() << G4endl;
350 // G4cout << "N tracks : " << currentBox->GetNTrack() << G4endl;
351
352 for (; __it != __end; __it++)//, i++)
353 {
354 // G4cout << i << G4endl;
355 // G4cout << "track "
356 // << currentIT->GetTrack()->GetTrackID()
357 // << G4endl;
358 currentIT = GetIT(*__it);
359 currentNode = currentTree->Insert(currentIT);
360
361 /*G4KDNode_Base* */
362// currentNode = currentTree->InsertMap(currentIT);
363// if(currentIT->GetNode())
364// {
365// delete currentIT->GetNode();
366 // No need: deleted when tree is cleared
367// }
368 currentIT->SetNode(currentNode);
369
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;
376#endif
377 }
378
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;
384#endif
385
386 }
387// currentTree->Build();
388 }
389}